Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <algorithm>
- #include <iomanip>
- using namespace std;
- class team
- {
- public:
- int id;
- string name;
- int wins;
- int losses;
- int ties;
- int points;
- double krach;
- int h2h[12];
- int scen[12];
- double weight[12];
- team(){}
- team(team &t2)
- {
- id = t2.id;
- name = t2.name;
- wins = t2.wins;
- losses = t2.losses;
- ties = t2.ties;
- points = t2.points;
- krach = t2.krach;
- memcpy(h2h,t2.h2h,12*4);
- }
- void h2hp()
- {
- for(int i = 0;i<12;i++)
- cout << setw(3) << i;
- cout << endl;
- for(int i =0;i<12;i++)
- {
- if(i != id)
- cout << setw(3) << h2h[i];
- else
- cout << setw(3) << "x";
- }
- cout << endl;
- }
- void pts()
- {
- points = wins*2;
- points += ties;
- }
- };
- class game
- {
- public:
- int t1;
- int t2;
- };
- class result
- {
- public:
- int results[12];
- int standings[12];
- int tied;
- double prob;
- result(result &r2)
- {
- tied = r2.tied;
- prob = r2.prob;
- memcpy(results,r2.results,12*4);
- }
- result()
- {
- tied=0;
- prob=1;
- }
- };
- int globalTied = 0;
- bool naive_cmp(team *t1, team *t2)
- {
- t1->pts();
- t2->pts();
- if(t1->points > t2->points)
- return true;
- if(t1->points < t2->points)
- return false;
- else
- {
- if(t1->h2h[t2->id] > 2)
- return true;
- else if(t1->h2h[t2->id]<2)
- return false;
- else
- {
- if(t1->wins > t2->wins)
- return true;
- else if(t1->wins < t2->wins)
- return false;
- else
- return true;
- }
- }
- }
- team *gteams[12];
- bool cmp(team *t1, team *t2)
- {
- t1->pts();
- t2->pts();
- if(t1->points > t2->points)
- return true;
- if(t1->points < t2->points)
- return false;
- else
- {
- if(t1->h2h[t2->id] > 2)
- return true;
- else if(t1->h2h[t2->id]<2)
- return false;
- else
- {
- if(t1->wins > t2->wins)
- return true;
- else if(t1->wins < t2->wins)
- return false;
- else
- {
- sort(gteams,gteams+12,naive_cmp);
- int end4=3;
- for(;end4<12;)
- {
- if(gteams[end4]->points == gteams[end4+1]->points)
- end4++;
- else
- break;
- }
- int t1top4 =0;
- int t2top4 = 0;
- for(int i =0;i<end4+1;i++)
- {
- t1top4 += t1->h2h[gteams[i]->id];
- t2top4 += t2->h2h[gteams[i]->id];
- }
- if(t1top4 > t2top4)
- return true;
- else if(t1top4 < t2top4)
- return false;
- else
- {
- int end8=7;
- for(;end8<12;)
- {
- if(gteams[end8]->points == gteams[end8+1]->points)
- end8++;
- else
- break;
- }
- int t1top8 =0;
- int t2top8 = 0;
- for(int i =0;i<end8+1;i++)
- {
- t1top8 += t1->h2h[gteams[i]->id];
- t2top8 += t2->h2h[gteams[i]->id];
- }
- if(t1top8 > t2top8)
- return true;
- else if(t1top8 < t2top8)
- return false;
- else
- {
- globalTied=1;
- if(rand()%2==1)
- return true;
- else
- return false;
- }
- }
- }
- }
- }
- }
- void weight(result *r,game **games,team **teams,int verbose)
- {
- for(int i=0;i<12;i++)
- {
- double krach = teams[games[i]->t1]->krach / (teams[games[i]->t1]->krach + teams[games[i]->t2]->krach);
- if(r->results[i] == games[i]->t1)
- r->prob *= krach-.066;
- else if(r->results[i] == games[i]->t2)
- r->prob *= 1-krach-.066;
- else if(r->results[i] == 12)
- r->prob *= .132;
- }
- }
- void prettyStandings(team **teams)
- {
- for(int i=0;i<12;i++)
- {
- cout << setw(3) << left << i+1 << setw(15) << left << teams[i]->name;
- teams[i]->pts();
- cout << left << setw(3) << teams[i]->points << setw(2) << right << teams[i]->wins;
- cout << "-" << setw(2) << teams[i]->losses << "-" << setw(1) << teams[i]->ties;
- cout << endl;
- }
- }
- void standings(result *r,game **games,team ** teams,int verbose)
- {
- team* lteams[12];
- for(int i=0;i<12;i++)
- lteams[i] = new team(*teams[i]);
- for(int i=0;i<12;i++)
- {
- if(r->results[i] == 12)
- {
- lteams[games[i]->t1]->ties++;
- lteams[games[i]->t1]->h2h[games[i]->t2]++;
- lteams[games[i]->t2]->ties++;
- lteams[games[i]->t2]->h2h[games[i]->t1]++;
- }
- else
- {
- lteams[r->results[i]]->wins++;
- if(games[i]->t1 == r->results[i])
- lteams[r->results[i]]->h2h[games[i]->t2] += 2;
- else
- lteams[r->results[i]]->h2h[games[i]->t1] += 2;
- }
- }
- for(int i=0;i<12;i++)
- gteams[i] = new team(*lteams[i]);
- sort(lteams,lteams+12,cmp);
- if (globalTied == 1)
- {
- r->tied=1;
- //prettyStandings(lteams);
- //exit(1);
- globalTied = 0;
- }
- for(int i=0;i<12;i++)
- {
- r->standings[i] = lteams[i]->id;
- }
- if(verbose==1)
- {
- prettyStandings(lteams);
- prettyStandings(gteams);
- }
- for(int i =0;i<12;i++)
- {
- delete lteams[i];
- delete gteams[i];
- }
- }
- void gen_result(int &rid,int gid,team **teams,game **games, result **results, result *tr)
- {
- if(gid == 12)
- {
- results[rid] = new result(*tr);
- weight(results[rid],games,teams,0);
- rid++;
- if(rid%10000==0)
- cout << rid << endl;
- }
- else
- {
- tr->results[gid] = games[gid]->t1;
- gen_result(rid,gid+1,teams,games,results,tr);
- tr->results[gid] = games[gid]->t2;
- gen_result(rid,gid+1,teams,games,results,tr);
- tr->results[gid] = 12;
- gen_result(rid,gid+1,teams,games,results,tr);
- }
- }
- int main(int argc, char** argv)
- {
- team *teams[12];
- game *games[12];
- result **results = (result**) malloc(sizeof(result*)*531441);
- ifstream teamfile("team_stats.txt");
- for(int i=0;i<12;i++)
- {
- team *t = new team;
- t->id = i;
- teamfile >> t->name >> t->wins >> t->losses >> t->ties >> t->krach;
- for(int j=0;j<12;j++)
- teamfile >> t->h2h[j];
- teams[i] = t;
- }
- ifstream gamefile("games.txt");
- for(int i=0;i<12;i++)
- {
- game *g = new game;
- gamefile >> g->t1 >> g->t2;
- games[i] = g;
- }
- int rid = 0;
- result *tr = new result;
- gen_result(rid,0,teams,games,results,tr);
- int match[12] = {11,8,10,0,9,12,11,4,10,12,0,7};
- for(int i=0;i<531441;i++)
- {
- int j=0;
- for(;j<12;j++)
- {
- if(results[i]->results[j] != match[j])
- {
- break;
- }
- }
- if(j>11)
- {
- cout << i << endl;
- }
- }
- double sum = 0;
- for(int i =0;i < 531441;i++)
- {
- sum += results[i]->prob;
- standings(results[i],games,teams,0);
- if (i%10000 == 0)
- cout << sum << endl;
- }
- cout << sum << endl;
- double ties = 0;
- for(int i =0;i<531441;i++)
- if(results[i]->tied == 1)
- ties++;
- cout << "Number of ties " << ties << " (that's " << (ties/531441)*100 <<"%)" << endl;
- for(int i=0;i<531441;i++)
- {
- for(int j=0;j<12;j++)
- {
- teams[results[i]->standings[j]]->scen[j]++;
- teams[results[i]->standings[j]]->weight[j]+=results[i]->prob;
- }
- }
- for(int i=0;i<12;i++)
- {
- cout << setw(15) << left << teams[i]->name;
- for(int j=0;j<12;j++)
- {
- cout << setw(13) << right << setprecision(6) << teams[i]->weight[j];
- }
- cout << endl;
- }
- for(int i=0;i<12;i++)
- {
- cout << setw(15) << left << teams[i]->name;
- for(int j=0;j<12;j++)
- {
- cout << setw(8) << right << teams[i]->scen[j];
- }
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement