def childWorker(inQueue, \ outQueue, \ schoolInfo, \ entriesList, \ config): #gc.set_debug(gc.DEBUG_LEAK) procName = multiprocessing.current_process().name newSched = None #Load config file wrkConfig = config #exec(open("settings.py").read(), wrkConfig) schedFitness.fitnessInitialize(schoolInfo, entriesList, wrkConfig) randGenWrk = random.SystemRandom() startTime = time.time() counter = 0 mutateCnt = 0 xOverCnt = 0 while True: workPackage = inQueue.get() counter += 1 # if procName == 'SchedWorker5' and counter%1000==0 : # msgStr = ('Wrkr5 GC Obj %d' % len(gc.get_objects())) # outQueue.put({'msg':msgStr}) if workPackage['cmd'] == 'STOP': break elif workPackage['cmd'] == 'Mutate': newSched = mutate(workPackage['sch1'], randGenWrk) if newSched['score'] < workPackage['sch1']['score']: #print ('Mutate child parent fitness %d %d' % (newSched['score'], workPackage['sch1']['score'])) outQueue.put ({'idx':workPackage['idx'], 'sch':newSched, 'method':'M'}) mutateCnt += 1 elif workPackage['cmd'] == 'XOver': newSched = scheduleSex(workPackage['sch1'], workPackage['sch2'], randGenWrk) if newSched['score'] < workPackage['sch1']['score']: #print ('XOver child parent fitness %d %d' % (newSched['score'], workPackage['sch1']['score'])) outQueue.put ({'idx':workPackage['idx'], 'sch':newSched, 'method':'X'}) xOverCnt += 1 elif workPackage['cmd'] == 'Done': outQueue.put({'done':1}) msgStr = '%s STOP Count %d Rate %1.1f xOver:%d Mutate:%d:' % \ (procName, counter, counter / (time.time()-startTime), xOverCnt, mutateCnt) outQueue.put({'msg':msgStr}) msgStr = procName + ' ' + schedFitness.getFitnessMetrics() outQueue.put({'msg':msgStr})
def childWorker(inQueue, \ outQueue, \ schoolInfo, \ entriesList, \ config): #gc.set_debug(gc.DEBUG_LEAK) procName = multiprocessing.current_process().name newSched = None #Load config file wrkConfig = config #exec(open("settings.py").read(), wrkConfig) schedFitness.fitnessInitialize(schoolInfo, entriesList, wrkConfig) randGenWrk = random.SystemRandom() startTime = time.time() counter = 0 mutateCnt = 0 xOverCnt = 0 while True: workPackage = inQueue.get() counter += 1 # if procName == 'SchedWorker5' and counter%1000==0 : # msgStr = ('Wrkr5 GC Obj %d' % len(gc.get_objects())) # outQueue.put({'msg':msgStr}) if workPackage['cmd'] == 'STOP': break elif workPackage['cmd'] == 'Mutate': newSched = mutate(workPackage['sch1'], randGenWrk) if newSched['score'] < workPackage['sch1']['score']: #print ('Mutate child parent fitness %d %d' % (newSched['score'], workPackage['sch1']['score'])) outQueue.put({ 'idx': workPackage['idx'], 'sch': newSched, 'method': 'M' }) mutateCnt += 1 elif workPackage['cmd'] == 'XOver': newSched = scheduleSex(workPackage['sch1'], workPackage['sch2'], randGenWrk) if newSched['score'] < workPackage['sch1']['score']: #print ('XOver child parent fitness %d %d' % (newSched['score'], workPackage['sch1']['score'])) outQueue.put({ 'idx': workPackage['idx'], 'sch': newSched, 'method': 'X' }) xOverCnt += 1 elif workPackage['cmd'] == 'Done': outQueue.put({'done': 1}) msgStr = '%s STOP Count %d Rate %1.1f xOver:%d Mutate:%d:' % \ (procName, counter, counter / (time.time()-startTime), xOverCnt, mutateCnt) outQueue.put({'msg': msgStr}) msgStr = procName + ' ' + schedFitness.getFitnessMetrics() outQueue.put({'msg': msgStr})
config = {} exec(open(os.path.join(contestDir,"settings.py")).read(), config) editsDir = os.path.join(schedDir, 'edits') if not os.path.isdir(editsDir): os.makedirs(editsDir) #Make these files happy logger = logger.Logger(editsDir) schedIO.setLogger(logger) if 'G' in config['CONTEST_TYPE'].upper(): schedIO.setCats('group') else: schedIO.setCats('indiv') schedFitness.setLogger(logger) schedFitness.fitnessInitialize(schoolInfo, entryList, config) #schedFitness.fitnessTest (schedl = schedule, \ # saveReport = False) #print ('Score %d' % schedule['score']) catList = [] for x in schedule['lst']: if 'entry' in x and x['entry']['catShort'] not in catList: catList.append(x['entry']['catShort']) userContinue = True while userContinue: print ('') for x in range(len(catList)):
for school, data in schoolInfo.items(): if data['inContest']: distMgr.addSchool(school, data['city'] + ',IA') hostSchoolId = config['HOST_SCHOOL_ID'] if not hostSchoolId in schoolInfo: print('ERROR: Host school not in contest') distMgr.setHostSchool(hostSchoolId) for school,data in schoolInfo.items(): if data['inContest']: data['driveTime'] = distMgr.driveTimeLookup(hostSchoolId,school) #print('Dist %s %d' % (data['city'], data['driveTime'])) #data['driveTime'] = 10 #Initialize fitness. Main thread uses it at the last step. schedFitness.fitnessInitialize(schoolInfo, entriesList, config) #Test creating a random schedule to make sure there are enough sessions # for all the entries. If it fails, quit before spinning off childWorkers tstRdm = [{'score':-1, 'lst':None}] randFails = createRandomSchedules (tstRdm, sessionList, entriesList, randGenMain) childPID = os.fork() if childPID: sys.exit('PID %d going to background\n' % childPID) #Creating a worker for each cpu core gives wacko results. There are queue issues #and garbage collection gets starved. cpuCount = multiprocessing.cpu_count() - 1 processes = [] taskQueue = multiprocessing.Queue()