def construct(self,weightNum = 11,cmpltReverse = False): self._opt_solution = sol.Solution(self._instance) weightLst = self._generateWeightLst(weightNum) start = time.clock() for weight in weightLst: self._solution = sol.Solution(self._instance) self.c_weight = weight # self._robEncodeIndexLst = [1]*self._instance.robNum self._robTaskIDLst = [INF_INT_NUM] * self._instance.robNum self._robUnAllocateTaskLst = [] for taskID in range(self._instance.robNum): self._robUnAllocateTaskLst.append([x for x in range(self._instance.taskNum)]) self.constructFirstStep() self.estConstruct() # print(self._solution) self._solution.evaluate() if self._solution.objective < self._opt_solution.objective: self._opt_solution = self._solution minWeight = weight # print(self._robUnAllocateTaskLst) # print(self._solution) # break end = time.clock() self._methodPeriod = end - start vaild = False if self._opt_solution.objective != INF_NUM: vaild = True print(minWeight) return vaild,self._opt_solution
def construct(self, weightNum=11, cmpltReverse=False): ''' return a optimal solution ''' self._opt_solution = sol.Solution(self._instance) start = time.clock() weightLst = self._generateWeightLst(weightNum) # weightLst = [0.6]*11 for weight in weightLst: self._solution = sol.Solution(self._instance) self.c_weight = weight try: self.constructUint() except Exception as e: print(e) self.deg.write(str(e) + '\n') continue else: makespan = self.__calMakespan() self._solution.evaluate() if self._degBool: self.deg.write('weight = ' + str(self.c_weight) + '\n') print('makespan = ', makespan) print('realObjective = ', self._solution.objective) cmpltTimeLst = [task.cmpltTime for task in self._taskLst] realCmpltTimeLst = [ self._instance.decode.taskLst[i].cmpltTime for i in range(self._instance.taskNum) ] print(cmpltTimeLst) print(realCmpltTimeLst) self.deg.write('real decode \n') for i in range(self._instance.taskNum): self.deg.write( str(i) + ' cmpltTime = ' + str(self._instance.decode.taskLst[i].cmpltTime) + '\n') self.deg.write('decode Encode ' + str(self._solution)) self.deg.flush() if abs(makespan - self._solution.objective) > 0.01: raise Exception('the two makespans are not equal') if self._solution.objective < self._opt_solution.objective: self._opt_solution = self._solution # break vaild = False end = time.clock() self._methodPeriod = end - start if self._opt_solution.objective != INF_NUM: vaild = True else: self.deg.write('can not get a solution \n') self.deg.close() return vaild, self._opt_solution
def Gconstruct(self, weightNum=11, cmpltReverse=False): ''' Gao's method ''' start = time.clock() self._solution = sol.Solution(self._instance) self.__sortPreFirstArrTime() self.__sortPreFirstCmpltTime(cmpltReverse=cmpltReverse) weightLst = self._generateWeightLst(weightNum) # print(weightLst) solSet = [] for weight in weightLst: orderLst = [] for key in self.arrDic: arrOrder = self.arrDic[key] cmpltOrder = self.cmpltDic[key] syntheticalOrder = weight * arrOrder + (1 - weight) * cmpltOrder orderLst.append((key, syntheticalOrder)) # print(orderLst) orderLst = sorted(orderLst, key=lambda x: x[1]) resSolution = self.__orderLst2Sol(orderLst) resSolution.evaluate() resSolution.genNoBackTrackEncode() # print(resSolution) if resSolution not in solSet: solSet.append(resSolution) if resSolution.objective < self._solution.objective: self._solution = resSolution # print(len(solSet)) end = time.clock() self._methodPeriod = end - start return self._solution
def generateRandSol(self): resSolution = sol.Solution(self._instance) for i in range(self._instance.robNum): permLst = [x for x in range(self._instance.taskNum)] random.shuffle(permLst) for j in range(self._instance.taskNum): resSolution[(i, j)] = permLst[j] return resSolution
def __orderLst2Sol(self, orderLst=[]): encodeIndLst = [0] * self._instance.robNum resSol = sol.Solution(self._instance) for orderUnit in orderLst: robID = orderUnit[0][0] taskID = orderUnit[0][1] resSol[(robID, encodeIndLst[robID])] = taskID encodeIndLst[robID] += 1 # print(resSol) return resSol
def tryConstruct(self, sampleTimes=500): # vaildLst = [] # self._solution = sol.Solution(self._instance) self._opt_solution = sol.Solution(self._instance) for i in range(sampleTimes): resSolution = self.generateRandSol() # print(resSolution) resSolution.evaluate() # if resSolution.objective != sys.float_info.max: # vaildLst.append(resSolution.objective) self._evaluateNum = i + 1 if resSolution.objective < self._opt_solution.objective: self._opt_solution = resSolution
def construct(self): self._opt_solution = sol.Solution(self._instance) self._initState() while True: candLst = [] if self._grd_makespan == INF_NUM: for robID in range(self._instance.robNum): unAllocateTaskLst = self._robUnAllocated[robID] rob = self._robLst[robID] taskID = self._grd_pair.taskID if self._grd_pair.taskID in unAllocateTaskLst: candidateSol = copy.deepcopy(self._opt_solution) candidateSol[(robID,rob.encodeIndex)] = taskID vaild,solVal = self._evaluateSol(candidateSol) if vaild: candTuple = CandTupleClass(robID = robID, taskID = taskID, sol = candidateSol, solVal = solVal) candLst.append(candTuple) else: for robID in range(self._instance.robNum): unAllocateTaskLst = self._robUnAllocated[robID] rob = self._robLst[robID] for taskID in unAllocateTaskLst: candidateSol = copy.deepcopy(self._opt_solution) candidateSol[(robID,rob.encodeIndex)] = taskID vaild,solVal = self._evaluateSol(candidateSol) if vaild: candTuple = CandTupleClass(robID = robID, taskID = taskID, sol = candidateSol, solVal = solVal) candLst.append(candTuple) if len(candLst): bestCand = min(candLst,key = cmp_to_key(self._cmpState)) unAllocateTaskLst = self._robUnAllocated[bestCand.robID] unAllocateTaskLst.remove(bestCand.taskID) rob = self._robLst[bestCand.robID] rob.encodeIndex += 1 self._opt_solution = bestCand.sol print(bestCand.solVal.grdType) if len(self._grd_cmpltStateLst) == self._instance.taskNum: if bestCand.solVal.grdType == GrdTax.sTask_MS_UC_C or bestCand.solVal.grdType == GrdTax.sTask_MS_UC_UC: break self._grd_cmpltStateLst = bestCand.solVal.cmpltState self._grd_pair = RobTaskPair(robID = bestCand.robID, taskID = bestCand.taskID) self._grd_makespan = bestCand.solVal.makespan # raise Exception('sad') self.deg.write('_grd_pair = ' + str(self._grd_pair) + '\n') self.deg.write('_grd_makespan = ' + str(self._grd_makespan) + '\n') self.deg.write('solution = '+ str(self._opt_solution)+'\n') self.deg.write('ExecuteTaskNum = ' +str(len(self._grd_cmpltStateLst))+'\n') self.deg.flush() else: break return self._opt_solution
def constrcut(self, weightNum = 11,reverse = False): ''' return the the boolean of the optimal solution ''' weightLst = self._generateWeightLst(weightNum) self._opt_solution = sol.Solution(self._instance) start = time.clock() end = time.clock() self._methodPeriod = end - start vaild = False if self._opt_solution.objective != INF_NUM: vaild = True else: self.deg.write('can not get a solution \n') return vaild,self._opt_solution
def construct(self, sampleTimes=10000): if self._instance.robNum <= 4 and self._instance.taskNum <= 5: permLst = [x for x in range(self._instance.taskNum)] permLst = list( itertools.permutations(permLst, self._instance.taskNum)) encodeLst = itertools.product(permLst, repeat=self._instance.robNum) for encode in encodeLst: resSolution = sol.Solution(self._instance) for i in range(self._instance.robNum): for j in range(self._instance.taskNum): resSolution[(i, j)] = encode[i][j] resSolution.evaluate() if resSolution.objective < self._solution.objective: self._solution = resSolution pass else: raise PermutationException() return self._solution
def __init__(self, instance): self._instance = instance self._solution = sol.Solution(self._instance) self.taskLst = [] self.__initTaskStates()