From 0d173b8b03fdb5967f3225b5cf17247093fa816c Mon Sep 17 00:00:00 2001 From: martin Date: Wed, 28 Feb 2024 16:08:09 +0100 Subject: [PATCH] UEFA fastsims --- uefa/feb24_checkandschedule/uefa_tester_24.py | 152 +++++-- .../uefa_tester_24_gurobi.py | 327 +++++++++++++++ .../uefa_tester_24_xpress.py | 372 ++++++++++++++++++ 3 files changed, 809 insertions(+), 42 deletions(-) create mode 100644 uefa/feb24_checkandschedule/uefa_tester_24_gurobi.py create mode 100644 uefa/feb24_checkandschedule/uefa_tester_24_xpress.py diff --git a/uefa/feb24_checkandschedule/uefa_tester_24.py b/uefa/feb24_checkandschedule/uefa_tester_24.py index 71788db..f90c1c6 100644 --- a/uefa/feb24_checkandschedule/uefa_tester_24.py +++ b/uefa/feb24_checkandschedule/uefa_tester_24.py @@ -19,9 +19,17 @@ except: import json from gurobipy import * +import xpress as xp +from multiprocessing import Pool, cpu_count # import config + +# def async_simulate(filename,nSimulations): +nSimulations = 10 + + + getTeamsOfPot = { p: [ p+str(i) for i in range(1,10) ] for p in ["A","B","C","D"] } allTeams = getTeamsOfPot['A']+getTeamsOfPot['B']+getTeamsOfPot['C']+getTeamsOfPot['D'] @@ -49,22 +57,27 @@ nMatchDays=8 cntr=0 -mbus = Model("Draw") +# mbus = Model("Draw") +mbus = xp.problem("Draw") possAssigns= [ (t1,t2) for t1 in allTeams for t2 in allTeams if t1!=t2] # print (possAssigns) -plays = mbus.addVars(possAssigns, vtype=GRB.BINARY) +# plays = mbus.addVars(possAssigns, vtype=GRB.BINARY) +plays = {pa:xp.var(vartype=xp.binary) for pa in possAssigns} +mbus.addVariable(plays) for (t1,t2) in possAssigns: if t11: nSims=int(sys.argv[1]) +report_time = 0 solutions =[] solution_strings =[] for i in range(nSims): if i%10==0: print(i) + # print("report_time:", report_time) + cntr+=1 - mbus.setObjective(quicksum([ int(100*random.random()) * plays[pa] for pa in possAssigns ])) + # mbus.setObjective(quicksum([ int(100*random.random()) * plays[pa] for pa in possAssigns ])) + mbus.setObjective(xp.Sum([ int(100*random.random()) * plays[pa] for pa in possAssigns ])) # mbus.setObjective(quicksum([ max(0,int(pa[0][1]) - int(pa[1][1] )) * plays[pa] for pa in possAssigns ])) # print ("DRAWING !!!!") - mbus.optimize() - sol= [ pa for pa in possAssigns if plays[pa].x>0.9] + # mbus.optimize() + mbus.solve() + checker_sol = mbus.getSolution(plays) + + ttt = time.time() + + sol= [ pa for pa in possAssigns if checker_sol[pa]>0.9] solutions.append(sol.copy()) - + + report_time += time.time()-ttt + ttt = time.time() + if debug: print ("Solution") # exit(0) @@ -156,18 +183,23 @@ for i in range(nSims): solstring ="" for pa in possAssigns: - solstring+= str(int(plays[pa].x +0.1) ) + solstring+= str(int(checker_sol[pa] +0.1) ) if nSims < 1000: solution_strings.append(solstring) - + report_time += time.time()-ttt + ttt = time.time() + mds =range(1,nMatchDays+1) pas2= [ (t1,t2,md) for md in mds for (t1,t2) in sol] # print ("pas2", pas2) - cal = Model("Calendar") + # cal = Model("Calendar") + cal = xp.problem("Calendar") - x = cal.addVars(pas2, vtype=GRB.BINARY) + # x = cal.addVars(pas2, vtype=GRB.BINARY) + x = {p:xp.var(vartype=xp.binary) for p in pas2} + cal.addVariable(x) computeCalendarsHA = True computeCalendarsHA = False @@ -191,44 +223,59 @@ for i in range(nSims): positions = range (len(pos_patterns)) - pos = cal.addVars( [(t,p) for t in allTeams for p in positions], vtype=GRB.CONTINUOUS) + # pos = cal.addVars( [(t,p) for t in allTeams for p in positions], vtype=GRB.CONTINUOUS) + pos = {p:xp.var(vartype=xp.continuous) for p in positions} + cal.addVariable(pos) for t in allTeams: - cal.addConstr( quicksum( [ pos[t,p] for p in positions ]) ==1) + # cal.addConstr( quicksum( [ pos[t,p] for p in positions ]) ==1) + cal.addConstraint( xp.Sum( [ pos[t,p] for p in positions ]) ==1) for md in mds: - cal.addConstr( quicksum(x[(t1,t2,md)] for (t1,t2) in sol if t ==t1 ) == quicksum( [ pos[t,p] for p in positions if pos_patterns[p][md-1]==1 ]) ) + # cal.addConstr( quicksum(x[(t1,t2,md)] for (t1,t2) in sol if t ==t1 ) == quicksum( [ pos[t,p] for p in positions if pos_patterns[p][md-1]==1 ]) ) + cal.addConstraint( xp.Sum(x[(t1,t2,md)] for (t1,t2) in sol if t ==t1 ) == xp.Sum( [ pos[t,p] for p in positions if pos_patterns[p][md-1]==1 ]) ) for (t1,t2) in sol : if t1= int(0.5*gamesAgainst[(p,p2)]) ) + # cal.addConstr( quicksum(x[(t1,t2,md)] for (t1,t2) in sol if t ==t1 and t2 in getTeamsOfPot[p2] for md in mds ) >= int(0.5*gamesAgainst[(p,p2)]) ) + cal.addConstraint( xp.Sum(x[(t1,t2,md)] for (t1,t2) in sol if t ==t1 and t2 in getTeamsOfPot[p2] for md in mds ) >= int(0.5*gamesAgainst[(p,p2)]) ) else: for (t1,t2) in sol : if t10.9 ] # print( cal_sol) @@ -237,41 +284,62 @@ for i in range(nSims): # # print (cal.status) # print(cal.objVal) - if cal.status!=2 or cal.objVal>-288: + # if cal.status!=2 or cal.objVal>-288: + if cal.getProbStatus()!=6 or cal.getObjVal()>-288: sol= [ (t1,t2) for (t1,t2) in sol if t10.9] + # pas2_sol= [ pas for pas in pas2 if cal.getSolution(x[pas])>0.9] # print (pas2_sol) - showSolution= False - showSolution= True - if showSolution and debug: - for md in mds: - print (md,":") - for (t1,t2,md2) in pas2_sol: - # if md2==md and t11 and showSolution: - newGm = [ gg for gg in solutions[-1] if gg not in solutions[-2]] + # if len(solutions)>1 and showSolution: + # newGm = [ gg for gg in solutions[-1] if gg not in solutions[-2]] # print (oldGm) # print (newGm) # print (solution_strings) + +# n = sys.maxsize +# n = 1000 + +# pool = Pool() +# result = {} +# answer = {} +# # threads = cpu_count() +# threads = 1 + +# for cpu in range(threads): +# result[cpu] = pool.apply_async(async_simulate, args=(f'thread_{cpu}', n,)) + +# for cpu in range(threads): +# answer[cpu] = result[cpu].get() + + + print (len(set(solution_strings)) ) f = open("draw_feasibility/log_"+datetime.now().strftime("%Y%m%d_%H%M%S")+"_"+str(nSims)+".txt", "a") diff --git a/uefa/feb24_checkandschedule/uefa_tester_24_gurobi.py b/uefa/feb24_checkandschedule/uefa_tester_24_gurobi.py new file mode 100644 index 0000000..a06d36c --- /dev/null +++ b/uefa/feb24_checkandschedule/uefa_tester_24_gurobi.py @@ -0,0 +1,327 @@ +import csv +import operator +import os +import random +import sys +import time +import timeit +import datetime +# import networkx as nx + +from datetime import datetime + +from dateutil.parser import parse +from math import sqrt, pow, sin, cos, atan2, pi + +try: + import simplejson as json +except: + import json + +from gurobipy import * + +# import config + +getTeamsOfPot = { p: [ p+str(i) for i in range(1,10) ] for p in ["A","B","C","D"] } + +allTeams = getTeamsOfPot['A']+getTeamsOfPot['B']+getTeamsOfPot['C']+getTeamsOfPot['D'] +activeTeams= allTeams +pots = getTeamsOfPot.keys() +# print (pots) + +gamesAgainst = { (p1,p2) : 2 for p1 in pots for p2 in pots} + +# for p in pots: +# gamesAgainst[(p,p)]=2 + +# for (p1,p2) in [('A','D'), ('D','A'), ('B','C'), ('C','B') ]: +# gamesAgainst[(p1,p2)]=2 + +nMatchDays=8 + + +# for p1 in pots: +# for p2 in pots: +# print (p1,p2,gamesAgainst[(p1,p2)]) +# print() + +# print (allTeams) + +cntr=0 + +mbus = Model("Draw") + +possAssigns= [ (t1,t2) for t1 in allTeams for t2 in allTeams if t1!=t2] +# print (possAssigns) + + +plays = mbus.addVars(possAssigns, vtype=GRB.BINARY) + +for (t1,t2) in possAssigns: + if t11: + nSims=int(sys.argv[1]) + + +mbus_time = 0 +cal_time0 = 0 +cal_time1 = 0 +cal_time2 = 0 +cal_time3 = 0 +report_time = 0 + +solutions =[] +solution_strings =[] +for i in range(nSims): + + ttt = time.time() + + if i%100==0: + print(i) + print("mbus_time:", mbus_time) + print("cal_time0:", cal_time0) + print("cal_time1:", cal_time1) + print("cal_time2:", cal_time2) + print("cal_time3:", cal_time3) + print("report_time:", report_time) + cntr+=1 + mbus.setObjective(quicksum([ int(100*random.random()) * plays[pa] for pa in possAssigns ])) + # mbus.setObjective(quicksum([ max(0,int(pa[0][1]) - int(pa[1][1] )) * plays[pa] for pa in possAssigns ])) + + # print ("DRAWING !!!!") + + mbus.optimize() + mbus_time += time.time()-ttt + ttt = time.time() + sol= [ pa for pa in possAssigns if plays[pa].x>0.9] + solutions.append(sol.copy()) + + report_time += time.time()-ttt + ttt = time.time() + + if debug: + print ("Solution") + # exit(0) + print ("INPOT") + for p in pots: + print ("") + print ("POT",p) + for (t1,t2) in [(t1,t2) for (t1,t2) in sol if t1= int(0.5*gamesAgainst[(p,p2)]) ) + else: + for (t1,t2) in sol : + if t10.9 ] + # print( cal_sol) + # for pa in cal_sol : + # print (pa) + # # print (cal.status) + # print(cal.objVal) + + if cal.status!=2 or cal.objVal>-288: + sol= [ (t1,t2) for (t1,t2) in sol if t10.9] + # # print (pas2_sol) + + + # showSolution= False + # showSolution= True + # if showSolution and debug: + # for md in mds: + # print (md,":") + # for (t1,t2,md2) in pas2_sol: + # # if md2==md and t11 and showSolution: + # newGm = [ gg for gg in solutions[-1] if gg not in solutions[-2]] + + cal_time3 += time.time()-ttt + ttt = time.time() + + # print (oldGm) + # print (newGm) +# print (solution_strings) + +print (len(set(solution_strings)) ) + +f = open("draw_feasibility/log_"+datetime.now().strftime("%Y%m%d_%H%M%S")+"_"+str(nSims)+".txt", "a") +for s in solutions: + for s2 in s: + f.write(s2[0]+s2[1]) + f.write("\n") +f.close() + +print ("done" , datetime.now()-now ) + +print("mbus_time:", mbus_time) +print("cal_time0:", cal_time0) +print("cal_time1:", cal_time1) +print("cal_time2:", cal_time2) +print("cal_time3:", cal_time3) +print("report_time:", report_time) diff --git a/uefa/feb24_checkandschedule/uefa_tester_24_xpress.py b/uefa/feb24_checkandschedule/uefa_tester_24_xpress.py new file mode 100644 index 0000000..fd4d928 --- /dev/null +++ b/uefa/feb24_checkandschedule/uefa_tester_24_xpress.py @@ -0,0 +1,372 @@ +import csv +import operator +import os +import random +import sys +import time +import timeit +import datetime +# import networkx as nx + +from datetime import datetime + +from dateutil.parser import parse +from math import sqrt, pow, sin, cos, atan2, pi + +try: + import simplejson as json +except: + import json + +from gurobipy import * +import xpress as xp +from multiprocessing import Pool, cpu_count + +# import config +def simulate_craw_and_calendar(thread, nSimulations): + logfile = f'draw_feasibility/log_{datetime.now().strftime("%Y%m%d_%H%M%S")}_{thread}' + + getTeamsOfPot = { p: [ p+str(i) for i in range(1,10) ] for p in ["A","B","C","D"] } + + allTeams = getTeamsOfPot['A']+getTeamsOfPot['B']+getTeamsOfPot['C']+getTeamsOfPot['D'] + activeTeams= allTeams + pots = getTeamsOfPot.keys() + # print (pots) + + gamesAgainst = { (p1,p2) : 2 for p1 in pots for p2 in pots} + + # for p in pots: + # gamesAgainst[(p,p)]=2 + + # for (p1,p2) in [('A','D'), ('D','A'), ('B','C'), ('C','B') ]: + # gamesAgainst[(p1,p2)]=2 + + nMatchDays=8 + + + # for p1 in pots: + # for p2 in pots: + # print (p1,p2,gamesAgainst[(p1,p2)]) + # print() + + # print (allTeams) + + cntr=0 + + # mbus = Model("Draw") + mbus = xp.problem("Draw") + + possAssigns= [ (t1,t2) for t1 in allTeams for t2 in allTeams if t1!=t2] + # print (possAssigns) + + + # plays = mbus.addVars(possAssigns, vtype=GRB.BINARY) + plays = {pa:xp.var(vartype=xp.binary) for pa in possAssigns} + mbus.addVariable(plays) + + for (t1,t2) in possAssigns: + if t11: + # nSims=int(sys.argv[1]) + + + mbus_time = 0 + cal_vartime = 0 + cal_constrtime = 0 + cal_solvetime = 0 + cal_time3 = 0 + report_time = 0 + + solutions =[] + solution_strings =[] + for i in range(nSims): + + ttt = time.time() + + if i%1000==0: + with open(f"{logfile}.log", "a") as f: + f.write(f"{thread}: {i} after {datetime.now()-now}\n") + f.write(f"\tmbus_time: {mbus_time}\n") + f.write(f"\tcal_vartime: {cal_vartime}\n") + f.write(f"\tcal_constrtime: {cal_constrtime}\n") + f.write(f"\tcal_solvetime: {cal_solvetime}\n") + f.write(f"\tcal_time3: {cal_time3}\n") + f.write(f"\treport_time: {report_time}\n") + + with open(f"{logfile}.txt", "a") as f: + for s in solutions: + for s2 in s: + f.write(s2[0]+s2[1]) + f.write("\n") + solutions = [] + + cntr+=1 + # mbus.setObjective(quicksum([ int(100*random.random()) * plays[pa] for pa in possAssigns ])) + mbus.setObjective(xp.Sum([ int(100*random.random()) * plays[pa] for pa in possAssigns ])) + # mbus.setObjective(quicksum([ max(0,int(pa[0][1]) - int(pa[1][1] )) * plays[pa] for pa in possAssigns ])) + + # print ("DRAWING !!!!") + + # mbus.optimize() + mbus.solve() + checker_sol = mbus.getSolution(plays) + + mbus_time += time.time()-ttt + ttt = time.time() + sol= [ pa for pa in possAssigns if checker_sol[pa]>0.9] + solutions.append(sol.copy()) + + report_time += time.time()-ttt + ttt = time.time() + + if debug: + print ("Solution") + # exit(0) + print ("INPOT") + for p in pots: + print ("") + print ("POT",p) + for (t1,t2) in [(t1,t2) for (t1,t2) in sol if t1= int(0.5*gamesAgainst[(p,p2)]) ) + cal.addConstraint( xp.Sum(x[(t1,t2,md)] for (t1,t2) in sol if t ==t1 and t2 in getTeamsOfPot[p2] for md in mds ) >= int(0.5*gamesAgainst[(p,p2)]) ) + else: + for (t1,t2) in sol : + if t10.9 ] + # print( cal_sol) + # for pa in cal_sol : + # print (pa) + # # print (cal.status) + # print(cal.objVal) + + # if cal.status!=2 or cal.objVal>-288: + if cal.getProbStatus()!=6 or cal.getObjVal()>-288: + sol= [ (t1,t2) for (t1,t2) in sol if t10.9] + # print (pas2_sol) + + + # showSolution= False + # showSolution= True + # if showSolution and debug: + # for md in mds: + # print (md,":") + # for (t1,t2,md2) in pas2_sol: + # # if md2==md and t11 and showSolution: + # newGm = [ gg for gg in solutions[-1] if gg not in solutions[-2]] + + cal_time3 += time.time()-ttt + ttt = time.time() + + # print (oldGm) + # print (newGm) + + + +n = sys.maxsize +n = 2500 + +pool = Pool() +result = {} +answer = {} +threads = cpu_count() +threads = 2 + +for cpu in range(threads): + result[cpu] = pool.apply_async(simulate_craw_and_calendar, args=(f'thread_{cpu}', n,)) + +for cpu in range(threads): + answer[cpu] = result[cpu].get() +