def runGreedyByOrder(csvFile,orderMethod): taskList = createTasksFromCsv.getTaskList(csvFile) helperFunctions.preprocessTimeWindows(taskList) # create a list of tuples (time of latest time window ending, taskId) timeWindowsAndPriorities = [] for task in taskList: latestTime = 0 for day in task.timeWindows: for window in day: if window[1] >= latestTime: latestTime = window[1] timeWindowsAndPriorities.append([latestTime, task.priority, task.id, float(task.priority)/max(float(task.getNumTimeWindows()), 1), task.dependencyTasks]) # sorting by deadline timeWindowsAndPriorities = orderMethod(timeWindowsAndPriorities) # sorting by priority taskOrdering = [] for timeTaskTuple in timeWindowsAndPriorities: taskOrdering.append(timeTaskTuple[2]) return helperFunctions.createOptimalSchedule(taskList, taskOrdering)
def runGreedyByOrder(csvFile, orderMethod): taskList = createTasksFromCsv.getTaskList(csvFile) helperFunctions.preprocessTimeWindows(taskList) # create a list of tuples (time of latest time window ending, taskId) timeWindowsAndPriorities = [] for task in taskList: latestTime = 0 for day in task.timeWindows: for window in day: if window[1] >= latestTime: latestTime = window[1] timeWindowsAndPriorities.append([ latestTime, task.priority, task.id, float(task.priority) / max(float(task.getNumTimeWindows()), 1), task.dependencyTasks ]) # sorting by deadline timeWindowsAndPriorities = orderMethod(timeWindowsAndPriorities) # sorting by priority taskOrdering = [] for timeTaskTuple in timeWindowsAndPriorities: taskOrdering.append(timeTaskTuple[2]) return helperFunctions.createOptimalSchedule(taskList, taskOrdering)
def findBestSchedule(taskList, permutations): helperFunctions.preprocessTimeWindows(taskList) bestSchedule = Objects.Schedule() for perm in permutations: newSchedule = helperFunctions.createOptimalSchedule(taskList, perm) if isBetterSchedule(newSchedule, bestSchedule): bestSchedule = newSchedule return bestSchedule
def findBestScheduleWithTimeLimit(taskList, permutations, timeLimit): ''' A function given a list of task objects and all the potential task permutations will create a schedule for each ordering and output one of the best schedules in the time it has that is has tried. ''' start = time.time() helperFunctions.preprocessTimeWindows(taskList) bestSchedule = Objects.Schedule() for perm in permutations: newSchedule = helperFunctions.createOptimalSchedule(taskList, perm) if isBetterSchedule(newSchedule, bestSchedule): bestSchedule = newSchedule if time.time() - start >= timeLimit: return bestSchedule return bestSchedule
def randomlyPickBestScheduleUnderTime(csvFile, numSeconds): taskList = createTasksFromCsv.getTaskList(csvFile) helperFunctions.preprocessTimeWindows(taskList) bestSchedule, bestProfit = None, 0 taskIds = [task.id for task in taskList] start = time.time() while time.time() < (start + numSeconds): # Get a random permutation orderToTry = random.sample(taskIds, len(taskIds)) schedule = helperFunctions.createOptimalSchedule(taskList, orderToTry) if schedule.getProfit() > bestProfit: bestProfit = schedule.getProfit() bestSchedule = schedule return bestSchedule
def solve(csvFile): global timeLimit, taskList, delta, tShoe global startTime startTime = time.time() ''' The primal bound holds the priority of best possible schedule we have found so far. Initialize it to 0. If we find a feasible path with a higher priority (score), then we update the primal bound. A multiprocessing manager does all the locking work for you. When you pass it into a function and the function accesses the managed resource, it is accessed as a proxy, and the manager makes sure all threads get the updated value. It works with python objects, which is why it is a list. ''' manager = multiprocessing.Manager() #initialize our list of all nodes taskList = createTasksFromCsv.getTaskList(csvFile) primalBound = manager.list([0, 0, 0, 0, 0] + [0, 0, 0, 0]) timeLimit = helperFunctions.getLatestDeadline(taskList) print "timeLimit:", timeLimit allNodes = [x.id for x in taskList] allNodes = manager.list(allNodes) #2-dimensional list with first index value as tao (currentTime), second index value as node #we can mess with this as we see fit. Make delta larger to speed things up. Make it smaller to have more values. #delta = timeLimit/10, tShoe = timeLimit/10 make us reach maximum recursion depth delta = max(10, (timeLimit/dayLength * 3)) tShoe = 9 * timeLimit / 13 print "tShoe", tShoe boundsInfo = [delta, tShoe] defineBounds(boundsInfo, primalBound, allNodes) bestPaths = [] order = reducedBoundsMatrix[min(reducedBoundsMatrix.keys())] if float("inf") in order: otherKeys = reducedBoundsMatrix.keys() otherKeys.remove(min(reducedBoundsMatrix.keys())) order = reducedBoundsMatrix[min(otherKeys)] print order allNodes = sorted(allNodes, key = lambda x: order[x]) print allNodes print "setting primal bound to ", max(order), " initially" primalBound[0] = max(order) ''' Creates a pool of worker threads. Automatically set to the number of cores in the machine, but you can set that differently with an int argument. ''' pool = multiprocessing.Pool(len(allNodes)/2 + 1) ''' This is a partial function, which means it creates an in-between function that has some set arguments. You can then call it later with the remaining arguments We use this here so that we can use the map function of the thread pool later ''' partialPulse = functools.partial(pulse, primalBound, [0], allNodes, allNodes[:], 0) ''' Calling map on the thread pool with our partial pulse function with added node args. ''' #Uncomment the following line for parallel bestPaths = pool.map(partialPulse, allNodes) #Uncomment the following line for sequential # bestPaths = map(partialPulse, allNodes) print bestPaths bestSched = max(bestPaths, key = lambda x: getProfit(x)) bestProfit = getProfit(bestSched) print bestSched # you have to call this on threads so they stop pool.close() # you have to call this on threads after you've closed them # (read documentation) pool.join() bestSched = helperFunctions.createOptimalSchedule(taskList, bestSched[1:]) print "Wow you made it" print "best schedule\n", bestSched print "profit", bestProfit print "primal bound", primalBound return bestSched
def calendarDrawInit(): fileName = "" #get the name of the test file we are to use for b in range(len(fileBooleans)): if fileBooleans[b]: fileName = fileListBackEnd[b] + ".csv" #get an integer value for running time for b in range(len(timeBooleans)): if timeBooleans[b]: timeValue.append(int(timeList[b])) #do stuff so that we can call different algorithms depending on user's selection csvFile = pwd(fileName) #setup for VNS if buttonList[1] == True: stoppingCondition = timeValue[0] schedule[0], useless, useless2 = solve(csvFile, stoppingCondition) # schedule os.system("python " + vns + " " + file) #setup for greedy elif buttonList[2] == True: sched = greedyByOrder.runGreedyByOrder(csvFile, greedyByOrder.orderByPriority) greedyByPrioritySol = greedyByOrder.runGreedyByOrder(csvFile, greedyByOrder.orderByPriority) greedyByDeadlineSol = greedyByOrder.runGreedyByOrder(csvFile, greedyByOrder.orderOptionalByDeadline) greedyByPresentChoiceSol = greedyByPresentChoice.runGreedyByPresentChoice(csvFile) solutionList = [greedyByPrioritySol, greedyByDeadlineSol, greedyByPresentChoiceSol] bestGreedy = max(solutionList, key = lambda sched : sched.getProfit()) schedule[0] = bestGreedy #setup for Integer elif buttonList[3] == True: fileName = pwd(fileName) output = os.system("python " + pwd("integerProgram.py") + " " + fileName + " " + str(timeValue[0])) print output sched = createTasksFromCsv.getTaskList("intProgSched.csv") order = range(len(sched)) schedule[0] = helperFunctions.createOptimalSchedule(sched, order) #setup for Pulse elif buttonList[4]: fileName = pwd(fileName) output = os.system("python " + pwd("pulseOPTW.py") + " " + fileName + " " + str(timeValue[0])) print output sched = createTasksFromCsv.getTaskList("pulseSched.csv") order = range(len(sched)) schedule[0] = helperFunctions.createOptimalSchedule(sched, order) #setup for bruteForce elif buttonList[5]: fileName = pwd(fileName) schedule[0] = runBruteForceAlgWithTimeLimit(csvFile, timeValue[0]) else: #ERROR: none of our known algorithms were selected when okay when selected #Default to VNS pass #Globals for reference later dayWidth[0] = (width -sideBarWidth) / len(schedule[0]) #populate the rectColors list with some rainbow colors setColors() #determine maximum coordinate values maxX[0] = 0 maxY[0] = 0 maxNumTimeWindows[0] = 0 for day in range(len(schedule[0])): for t in range(len(schedule[0][day])): task = schedule[0][day][t] taskX = task.x taskY = task.y if taskX > maxX[0]: maxX[0] = taskX if taskY > maxY[0]: maxY[0] = taskY #get maxNumTimeWindows so we can make ghost time windows appropriate width in highlight() for j in range(len(schedule[0][day][t].timeWindows)): if len(schedule[0][day][t].timeWindows[j]) > maxNumTimeWindows[0]: maxNumTimeWindows[0] = len(schedule[0][day][t].timeWindows[j]) background(bgColor)