Advertisement
Guest User

ECAC Hockey Predictor

a guest
Feb 23rd, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. class team
  10. {
  11. public:
  12.   int id;
  13.   string name;
  14.   int wins;
  15.   int losses;
  16.   int ties;
  17.   int points;
  18.   double krach;
  19.   int h2h[12];
  20.   int scen[12];
  21.   double weight[12];
  22.   team(){}
  23.   team(team &t2)
  24.   {
  25.     id = t2.id;
  26.     name = t2.name;
  27.     wins = t2.wins;
  28.     losses = t2.losses;
  29.     ties = t2.ties;
  30.     points = t2.points;
  31.     krach = t2.krach;
  32.     memcpy(h2h,t2.h2h,12*4);
  33.   }
  34.   void h2hp()
  35.   {
  36.     for(int i = 0;i<12;i++)
  37.       cout << setw(3) << i;
  38.     cout << endl;
  39.     for(int i =0;i<12;i++)
  40.     {
  41.       if(i != id)
  42.         cout << setw(3) << h2h[i];
  43.       else
  44.         cout << setw(3) << "x";
  45.     }
  46.     cout << endl;
  47.   }
  48.   void pts()
  49.   {
  50.     points = wins*2;
  51.     points += ties;
  52.   }
  53. };
  54. class game
  55. {
  56. public:
  57.   int t1;
  58.   int t2;
  59. };
  60. class result
  61. {
  62. public:
  63.   int results[12];
  64.   int standings[12];
  65.   int tied;
  66.   double prob;
  67.   result(result &r2)
  68.   {
  69.     tied = r2.tied;
  70.     prob = r2.prob;
  71.     memcpy(results,r2.results,12*4);
  72.   }
  73.   result()
  74.   {
  75.     tied=0;
  76.     prob=1;
  77.   }
  78. };
  79.  
  80. int globalTied = 0;
  81.  
  82. bool naive_cmp(team *t1, team *t2)
  83. {
  84.   t1->pts();
  85.   t2->pts();
  86.   if(t1->points > t2->points)
  87.     return true;
  88.   if(t1->points < t2->points)
  89.     return false;
  90.   else
  91.   {
  92.     if(t1->h2h[t2->id] > 2)
  93.       return true;
  94.     else if(t1->h2h[t2->id]<2)
  95.       return false;
  96.     else
  97.     {
  98.       if(t1->wins > t2->wins)
  99.         return true;
  100.       else if(t1->wins < t2->wins)
  101.         return false;
  102.       else
  103.         return true;
  104.     }
  105.   }
  106. }
  107.  
  108. team *gteams[12];
  109.  
  110. bool cmp(team *t1, team *t2)
  111. {
  112.   t1->pts();
  113.   t2->pts();
  114.   if(t1->points > t2->points)
  115.     return true;
  116.   if(t1->points < t2->points)
  117.     return false;
  118.   else
  119.   {
  120.     if(t1->h2h[t2->id] > 2)
  121.       return true;
  122.     else if(t1->h2h[t2->id]<2)
  123.       return false;
  124.     else
  125.     {
  126.       if(t1->wins > t2->wins)
  127.         return true;
  128.       else if(t1->wins < t2->wins)
  129.         return false;
  130.       else
  131.       {
  132.         sort(gteams,gteams+12,naive_cmp);
  133.         int end4=3;
  134.         for(;end4<12;)
  135.         {
  136.           if(gteams[end4]->points == gteams[end4+1]->points)
  137.             end4++;
  138.           else
  139.            break;
  140.         }
  141.         int t1top4 =0;
  142.         int t2top4 = 0;
  143.         for(int i =0;i<end4+1;i++)
  144.         {
  145.           t1top4 += t1->h2h[gteams[i]->id];
  146.           t2top4 += t2->h2h[gteams[i]->id];
  147.         }
  148.         if(t1top4 > t2top4)
  149.           return true;
  150.         else if(t1top4 < t2top4)
  151.           return false;
  152.         else
  153.         {
  154.           int end8=7;
  155.           for(;end8<12;)
  156.           {
  157.             if(gteams[end8]->points == gteams[end8+1]->points)
  158.               end8++;
  159.             else
  160.              break;
  161.           }
  162.           int t1top8 =0;
  163.           int t2top8 = 0;
  164.           for(int i =0;i<end8+1;i++)
  165.           {
  166.             t1top8 += t1->h2h[gteams[i]->id];
  167.             t2top8 += t2->h2h[gteams[i]->id];
  168.           }
  169.           if(t1top8 > t2top8)
  170.             return true;
  171.           else if(t1top8 < t2top8)
  172.             return false;
  173.           else
  174.           {
  175.             globalTied=1;
  176.             if(rand()%2==1)
  177.               return true;
  178.             else
  179.               return false;
  180.           }
  181.         }
  182.       }
  183.     }
  184.   }
  185. }
  186.  
  187. void weight(result *r,game **games,team **teams,int verbose)
  188. {
  189.   for(int i=0;i<12;i++)
  190.   {
  191.     double krach = teams[games[i]->t1]->krach / (teams[games[i]->t1]->krach + teams[games[i]->t2]->krach);
  192.     if(r->results[i] == games[i]->t1)
  193.       r->prob *= krach-.066;
  194.     else if(r->results[i] == games[i]->t2)
  195.       r->prob *= 1-krach-.066;
  196.     else if(r->results[i] == 12)
  197.       r->prob *= .132;
  198.   }
  199. }
  200.  
  201. void prettyStandings(team **teams)
  202. {
  203.   for(int i=0;i<12;i++)
  204.   {
  205.     cout << setw(3) << left << i+1 << setw(15) << left << teams[i]->name;
  206.     teams[i]->pts();
  207.     cout << left << setw(3) << teams[i]->points << setw(2) << right << teams[i]->wins;
  208.     cout << "-" << setw(2) << teams[i]->losses << "-" << setw(1) << teams[i]->ties;
  209.     cout << endl;
  210.   }
  211. }
  212.  
  213. void standings(result *r,game **games,team ** teams,int verbose)
  214. {
  215.   team* lteams[12];
  216.   for(int i=0;i<12;i++)
  217.     lteams[i] = new team(*teams[i]);
  218.   for(int i=0;i<12;i++)
  219.   {
  220.     if(r->results[i] == 12)
  221.     {
  222.       lteams[games[i]->t1]->ties++;
  223.       lteams[games[i]->t1]->h2h[games[i]->t2]++;
  224.       lteams[games[i]->t2]->ties++;
  225.       lteams[games[i]->t2]->h2h[games[i]->t1]++;
  226.     }
  227.     else
  228.     {
  229.       lteams[r->results[i]]->wins++;
  230.       if(games[i]->t1 == r->results[i])
  231.         lteams[r->results[i]]->h2h[games[i]->t2] += 2;
  232.       else
  233.         lteams[r->results[i]]->h2h[games[i]->t1] += 2;
  234.     }
  235.   }
  236.   for(int i=0;i<12;i++)
  237.     gteams[i] = new team(*lteams[i]);
  238.   sort(lteams,lteams+12,cmp);
  239.   if (globalTied == 1)
  240.   {
  241.     r->tied=1;
  242.     //prettyStandings(lteams);
  243.     //exit(1);
  244.     globalTied = 0;
  245.   }
  246.   for(int i=0;i<12;i++)
  247.   {
  248.     r->standings[i] = lteams[i]->id;
  249.   }
  250.   if(verbose==1)
  251.   {
  252.     prettyStandings(lteams);
  253.     prettyStandings(gteams);
  254.   }
  255.   for(int i =0;i<12;i++)
  256.   {
  257.     delete lteams[i];
  258.     delete gteams[i];
  259.   }
  260. }
  261.  
  262. void gen_result(int &rid,int gid,team **teams,game **games, result **results, result *tr)
  263. {
  264.    if(gid == 12)
  265.   {
  266.     results[rid] = new result(*tr);
  267.     weight(results[rid],games,teams,0);
  268.     rid++;
  269.     if(rid%10000==0)
  270.       cout << rid << endl;
  271.   }
  272.   else
  273.   {
  274.     tr->results[gid] = games[gid]->t1;
  275.     gen_result(rid,gid+1,teams,games,results,tr);
  276.     tr->results[gid] = games[gid]->t2;
  277.     gen_result(rid,gid+1,teams,games,results,tr);
  278.     tr->results[gid] = 12;
  279.     gen_result(rid,gid+1,teams,games,results,tr);
  280.   }
  281. }
  282.  
  283. int main(int argc, char** argv)
  284. {
  285.   team *teams[12];
  286.   game *games[12];
  287.   result **results = (result**) malloc(sizeof(result*)*531441);
  288.  
  289.   ifstream teamfile("team_stats.txt");
  290.   for(int i=0;i<12;i++)
  291.   {
  292.     team *t = new team;
  293.     t->id = i;
  294.     teamfile >> t->name >> t->wins >> t->losses >> t->ties >> t->krach;
  295.     for(int j=0;j<12;j++)
  296.       teamfile >> t->h2h[j];
  297.     teams[i] = t;
  298.   }
  299.   ifstream gamefile("games.txt");
  300.   for(int i=0;i<12;i++)
  301.   {
  302.     game *g = new game;
  303.     gamefile >> g->t1 >> g->t2;
  304.     games[i] = g;
  305.   }
  306.  
  307.   int rid = 0;
  308.   result *tr = new result;
  309.   gen_result(rid,0,teams,games,results,tr);
  310.  
  311.   int match[12] = {11,8,10,0,9,12,11,4,10,12,0,7};
  312.  
  313.   for(int i=0;i<531441;i++)
  314.   {
  315.     int j=0;
  316.     for(;j<12;j++)
  317.     {
  318.       if(results[i]->results[j] != match[j])
  319.       {
  320.         break;
  321.       }
  322.     }
  323.     if(j>11)
  324.     {
  325.       cout << i << endl;
  326.     }
  327.   }
  328.  
  329.   double sum = 0;
  330.   for(int i =0;i < 531441;i++)
  331.   {
  332.     sum += results[i]->prob;
  333.     standings(results[i],games,teams,0);
  334.     if (i%10000 == 0)
  335.       cout << sum << endl;
  336.   }
  337.   cout << sum << endl;
  338.  
  339.   double ties = 0;
  340.   for(int i =0;i<531441;i++)
  341.     if(results[i]->tied == 1)
  342.       ties++;
  343.   cout << "Number of ties " << ties << " (that's " << (ties/531441)*100 <<"%)" << endl;
  344.  
  345.   for(int i=0;i<531441;i++)
  346.   {
  347.     for(int j=0;j<12;j++)
  348.     {
  349.       teams[results[i]->standings[j]]->scen[j]++;
  350.       teams[results[i]->standings[j]]->weight[j]+=results[i]->prob;
  351.     }
  352.   }
  353.  
  354.   for(int i=0;i<12;i++)
  355.   {
  356.     cout << setw(15) << left << teams[i]->name;
  357.     for(int j=0;j<12;j++)
  358.     {
  359.       cout << setw(13) << right << setprecision(6) << teams[i]->weight[j];
  360.     }
  361.     cout << endl;
  362.   }
  363.  
  364.   for(int i=0;i<12;i++)
  365.   {
  366.     cout << setw(15) << left << teams[i]->name;
  367.     for(int j=0;j<12;j++)
  368.     {
  369.       cout << setw(8) << right << teams[i]->scen[j];
  370.     }
  371.     cout << endl;
  372.   }
  373.  
  374.   return 0;
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement