我编写了以下程序来生成未来三年我可能拥有的所有可能的时间表(我在季度系统上)但它的效率低得离谱,而且正如所写的那样,在接下来的一百万年里不会给我答案.
为了澄清,我确实已经编写了文本文件,指定了我感兴趣的课程以及每个季度的数字、期限和年份(例如,2013-2014 年第 1 季度秋季)。我想将其限制为每季度生成 4-5 节课的时间表,希望在程序中有所体现。
我知道,考虑到所需结果的大小,我写这个的任何方式都会非常慢,但是如果有人可以帮助我加快速度,或者至少给我一个估计需要多长时间才能完成( 24 小时或 24 个月)我将非常感激。
我已经确定但我不知道如何解决的两个问题是:
- 我的比较功能(将列表转换回列表,然后在列表的每个成员中搜索另一个列表的每个成员)
- 一般来说,列表的广泛使用,根据我的研究,它非常占用内存(例如,我使用的组合函数会生成一个迭代器对象,根据我的研究,它应该比列表更有效,但我没有知道如何有效地使用迭代器,从而减少内存使用;还有我在程序结束时使用的“GrandMasterLists”方法,结合所有的双 for 循环和线性增加的比较函数调用次数每个 for 循环,也必须使这比它需要的效率低得多)。
以下内容很长(177 行)(尽管也是重复的),但如果您正在寻找挑战或者您是编程专家,我将非常感谢您的帮助。
(我也意识到我在每一点上尝试做的很多事情可能不是很透明——在很多时候我都很难想出与我想要做的代码相匹配的代码——所以如果您需要澄清,请向我提问。)
import itertools
class Course:
def __init__(self, NAME, TERM, YEAR):
self.term=TERM
self.year=YEAR
self.name=NAME
class Quarter:
def __init__(self, NUMBER, TERM, YEAR):
self.term=TERM
self.year=YEAR
self.number=NUMBER
courselist=[]
def coursegen(NAME, TERM, YEAR):
return Course(NAME, TERM, YEAR)
f=open('classlist.txt', 'r')
for line in f:
courselist.append(coursegen(line.rstrip().split(';')[0],line.rstrip().split(';') [1].split(','),line.rstrip().split(';')[2].split(',')))
f.close()
def quartergen(NUMBER,TERM,YEAR):
return Quarter(NUMBER,TERM,YEAR)
m=open('quarterlist.txt', 'r')
line1=m.readline()
line2=m.readline()
line3=m.readline()
line4=m.readline()
line5=m.readline()
line6=m.readline()
line7=m.readline()
line8=m.readline()
line9=m.readline()
Quarter1=quartergen(line1.rstrip().split(';')[0],line1.rstrip().split(';')[1].split(','),line1.rstrip().split(';')[2].split(','))
Quarter2=quartergen(line2.rstrip().split(';')[0],line2.rstrip().split(';')[1].split(','),line2.rstrip().split(';')[2].split(','))
Quarter3=quartergen(line3.rstrip().split(';')[0],line3.rstrip().split(';')[1].split(','),line3.rstrip().split(';')[2].split(','))
Quarter4=quartergen(line4.rstrip().split(';')[0],line4.rstrip().split(';')[1].split(','),line4.rstrip().split(';')[2].split(','))
Quarter5=quartergen(line5.rstrip().split(';')[0],line5.rstrip().split(';')[1].split(','),line5.rstrip().split(';')[2].split(','))
Quarter6=quartergen(line6.rstrip().split(';')[0],line6.rstrip().split(';')[1].split(','),line6.rstrip().split(';')[2].split(','))
Quarter7=quartergen(line7.rstrip().split(';')[0],line7.rstrip().split(';')[1].split(','),line7.rstrip().split(';')[2].split(','))
Quarter8=quartergen(line8.rstrip().split(';')[0],line8.rstrip().split(';')[1].split(','),line8.rstrip().split(';')[2].split(','))
Quarter9=quartergen(line9.rstrip().split(';')[0],line9.rstrip().split(';')[1].split(','),line9.rstrip().split(';')[2].split(','))
m.close()
def compare(x, y):
if set(x).isdisjoint(set(y))==False:
return True
else:
return False
def offeredcoursegen(Quarter, courselist):
offered=[]
for course in courselist:
if compare(course.year, Quarter.year)==True and compare(course.term, Quarter.term)==True:
offered.append(course)
return offered
def combo(x, courselist):
return list(itertools.combinations(offeredcoursegen(x,courselist), 4))+list(itertools.combinations(offeredcoursegen(x,courselist), 5))
Combo1=combo(Quarter1, courselist)
Combo2=combo(Quarter2, courselist)
Combo3=combo(Quarter3, courselist)
Combo4=combo(Quarter4, courselist)
Combo5=combo(Quarter5, courselist)
Combo6=combo(Quarter6, courselist)
Combo7=combo(Quarter7, courselist)
Combo8=combo(Quarter8, courselist)
Combo9=combo(Quarter9, courselist)
GrandMasterList=[]
for i in Combo1:
for j in Combo2:
if compare(i, j)==False:
GrandMasterList.append([i,j])
GrandMasterList2=[]
for i in GrandMasterList:
for j in Combo3:
if compare(i[0],j)==False and compare(i[1],j)==False:
GrandMasterList2.append(i+[j])
GrandMasterList3=[]
for i in GrandMasterList2:
for j in Combo4:
if compare(i[0],j)==False and compare(i[1],j)==False and compare(i[2],j)==False:
GrandMasterList3.append(i+[j])
GrandMasterList4=[]
for i in GrandMasterList3:
for j in Combo5:
if compare(i[0],j)==False and compare(i[1],j)==False and compare(i[2],j)==False and compare(i[3],j)==False:
GrandMasterList4.append(i+[j])
GrandMasterList5=[]
for i in GrandMasterList4:
for j in Combo6:
if compare(i[0],j)==False and compare(i[1],j)==False and compare(i[2],j)==False and compare(i[3],j)==False and compare(i[4],j)==False:
GrandMasterList5.append(i+[j])
GrandMasterList6=[]
for i in GrandMasterList5:
for j in Combo7:
if compare(i[0],j)==False and compare(i[1],j)==False and compare(i[2],j)==False and compare(i[3],j)==False and compare(i[4],j)==False and compare(i[5],j)==False:
GrandMasterList6.append(i+[j])
GrandMasterList7=[]
for i in GrandMasterList6:
for j in Combo8:
if compare(i[0],j)==False and compare(i[1],j)==False and compare(i[2],j)==False and compare(i[3],j)==False and compare(i[4],j)==False and compare(i[5],j)==False and compare(i[6],j)==False:
GrandMasterList7.append(i+[j])
GrandMasterList8=[]
for i in GrandMasterList7:
for j in Combo9:
if compare(i[0],j)==False and compare(i[1],j)==False and compare(i[2],j)==False and compare(i[3],j)==False and compare(i[4],j)==False and compare(i[5],j)==False and compare(i[6],j)==False and compare(i[7], j)==False:
GrandMasterList8.append(i+[j])
for i in range(len(GrandMasterList8)):
print 'Schedule %d \n' % (i)
print 'Quarter 1'
for j in range(len(GrandMasterList8[i][0])):
print '%s ' % (GrandMasterList8[i][0][j].name)
print '\n Quarter 2'
for j in range(len(GrandMasterList8[i][1])):
print '%s ' % (GrandMasterList8[i][1][j].name)
print '\n Quarter 3'
for j in range(len(GrandMasterList8[i][2])):
print '%s ' % (GrandMasterList8[i][2][j].name)
print '\n Quarter 4'
for j in range(len(GrandMasterList8[i][3])):
print '%s ' % (GrandMasterList8[i][3][j].name)
print '\n Quarter 5'
for j in range(len(GrandMasterList8[i][4])):
print '%s ' % (GrandMasterList8[i][4][j].name)
print '\n Quarter 6'
for j in range(len(GrandMasterList8[i][5])):
print '%s ' % (GrandMasterList8[i][5][j].name)
print '\n Quarter 7'
for j in range(len(GrandMasterList8[i][6])):
print '%s ' % (GrandMasterList8[i][6][j].name)
print '\n Quarter 8'
for j in range(len(GrandMasterList8[i][7])):
print '%s ' % (GrandMasterList8[i][7][j].name)
print '\n Quarter 9'
for j in range(len(GrandMasterList8[i][8])):
print '%s ' % (GrandMasterList8[i][8][j].name)
print '\n \n \n'