I am trying to create a match program with 4 fields. There will be 4 games going on at the same time, one at each fields.
一支球队永远不会连续打 2 场比赛 - 并且不能同时在多个场地上比赛。对于 32 个匹配项和 4 个字段,这可能是不可能的。
我正在尝试使用 Python 以尽可能少的错误创建最佳匹配程序。然后,使用错误最少的程序 - 在发生错误的地方创建一个暂停,以便团队有机会休息。
这是我的 matches.txt 文件 Team1、Team2、Team1 ID、Team 2 ID
Trom 1,Trom 2,41,42
Trom 1,Trom 3,41,43
Trom 1,VAW,41,44
Trom 2,Trom 3,42,43
Trom 2,VAW,42,44
Trom 3,VAW,43,44
Team 1,Team 2,45,46
Team 1,Team 3,45,47
Team 1,Team 4,45,48
Team 2,Team 3,46,47
Team 2,Team 4,46,48
Team 3,Team 4,47,48
Team 5,Team 6,49,50
Team 5,Team 7,49,51
Team 5,Team 8,49,52
Team 5,Team 9,49,53
Team 6,Team 7,50,51
Team 6,Team 8,50,52
Team 6,Team 9,50,53
Team 7,Team 8,51,52
Team 7,Team 9,51,53
Team 8,Team 9,52,53
Team 10,Team 11,54,55
Team 10,Team 12,54,56
Team 10,Team 13,54,57
Team 10,Team 14,54,58
Team 11,Team 12,55,56
Team 11,Team 13,55,57
Team 11,Team 14,55,58
Team 12,Team 13,56,57
Team 12,Team 14,56,58
Team 13,Team 14,57,58
蟒蛇文件:
import csv, random, io, codecs
g = globals()
f = open("pymatches.txt", "w"); ## Create output file
f.close()
## Number of courts
f = open("num_court.txt", "r");
#numCourt = int(f.read())
numCourt = 4;
f.close()
counter = 0 ## Teller
matches = [] ## array for kampene
minErrors = 100000;
i = 1
while i < 250:
pyMatches = [];
rounds = [];
round = 1
errors = 0;
with codecs.open ('matches.txt', 'r') as f: ## Open files with matches
csv_reader = csv.reader(f, delimiter=',') ## Delimiter is comma (,)
numGames = 0 ## Count lines
for row in csv_reader:
numGames += 1
matches.append(row) ## Add match to array
random.shuffle(matches) ## Shuffle // random
for x in range(1, numCourt+1): ## numCourt is number of courts, usually 4.
for y in range(1, (numGames/numCourt)+1):
if y not in rounds:
rounds.append(y)
g[y] = []
maxTry = 150
Try = 0
while Try < maxTry:
Try += 1
if y == 1:
if matches[0][2] in g[y]:
random.shuffle(matches)
elif matches[0][3] in g[y]:
random.shuffle(matches)
else:
if matches[0][2] in g[y]:
random.shuffle(matches)
elif matches[0][3] in g[y]:
random.shuffle(matches)
elif matches[0][2] in g[y-1]:
random.shuffle(matches)
elif matches[0][3] in g[y-1]:
random.shuffle(matches)
if y >= 2:
if matches[0][2] in g[y]:
print(matches[0][0],"Error, samme runde ", x,y)
errors += 1
if matches[0][3] in g[y]:
print(matches[0][1],"Error, samme runde ", x,y)
errors += 1
if matches[0][2] in g[y-1]:
print(matches[0][0],"Error, forrige runde ", x,y)
errors += 1
if matches[0][3] in g[y-1]:
print(matches[0][1],"Error, forrige runde ", x,y)
errors += 1
else:
if matches[0][2] in g[y]:
#print(matches[0][0],"Error, samme runde ", x,y)
errors += 1
if matches[0][3] in g[y]:
#print(matches[0][1],"Error, samme runde ", x,y)
errors += 1
if matches[0][2] not in g[y]:
g[y].append(matches[0][2])
if matches[0][3] not in g[y]:
g[y].append(matches[0][3])
matches[0].append(x)
matches[0].append(y)
matchinfo = str(matches[0][0]) + "," + str(matches[0][1]) + "," + str(matches[0][2]) + "," + str(matches[0][3]) + "," + str(x) + "," + str(y)
pyMatches.append(matchinfo)
matches.pop(0) ## Remove match from array
if errors < minErrors:
minErrors = errors
f = open("pymatches.txt", "a+")
f.truncate(0)
f.write(str(errors) + "\n")
for line in pyMatches:
if line:
f.write(line + "\n")
f.close()
if errors == 0:
break
i += 1
它产生 0 个错误,但仍然会有一支连续打 2 场比赛的球队。我看不出这怎么可能?