def getVisits(dists,order,nagents): ''' dists: a distance matrix order: the order in which nodes must be visited duplicates allowed nagents: the number of agents available to make the visits returns visits,time visits[i] = j means the ith visit should be performed by agent j time[i] is the number of meters a person could have walked walk since the start when visit i is made ''' # This callback prints the number of iterations print 'Planning',len(order),'agent movements:' c = [0] def cb(): c[0] += 1 print c[0], stdout.flush() root = OTSPstate(dists,order,nagents) LO = MAX_BRANCHES // nagents state,value = branch_bound.branch_bound(root, LO , LO*nagents , cb) return state.visit2agent,state.time
def getVisits(dists, order, nagents): ''' dists: a distance matrix order: the order in which nodes must be visited duplicates allowed nagents: the number of agents available to make the visits returns visits,time visits[i] = j means the ith visit should be performed by agent j time[i] is the number of meters a person could have walked walk since the start when visit i is made ''' # This callback prints the number of iterations # print 'Planning',len(order),'agent movements:' c = [0] def cb(): c[0] += 1 # print c[0], sys.stdout.flush() root = OTSPstate(dists, order, nagents) LO = MAX_BRANCHES // nagents state, value = branch_bound.branch_bound(root, LO, LO * nagents, cb) return state.visit2agent, state.time
def FindOptimal(self): raw_order = self.OrderEntry.get().split(",") order = [] #eliminate the right and left side's space for each item for item in raw_order: order.append(item.strip()) start = self.StartEntry.get().split(",") end = self.EndEntry.get().split(",") #insert the optimal order into the OptimalOrderEntry self.OptimalOrderEntry.delete(0, END) if self.alg.get() == 0: optimal_order = modified_NN(start, order, end) elif self.alg.get() == 1: optimal_order = branch_bound(start, order, end) print("optimal order: %s" % optimal_order) self.OptimalOrderEntry.insert(END, optimal_order[0]) for i in range(1, len(optimal_order)): self.OptimalOrderEntry.insert(END, "," + optimal_order[i]) #self.Refresh() #self.MarkItem(order) #delete the content of cost entry before inserting self.OptimalCostEntry.delete(0, END) #insert the cost to the cost entry if self.weight.get() == 0: self.ShowPath(start, optimal_order, end, 'green') self.OptimalCostEntry.insert( 0, TotalCostWithWeight(start, optimal_order, end)) elif self.weight.get() == 1: self.OptimalCostEntry.insert( 0, self.ShowPath(start, optimal_order, end, 'green'))
def getVisits(dists,order,nagents): ''' dists: a distance matrix order: the order in which nodes must be visited duplicates allowed nagents: the number of agents available to make the visits returns visits,time visits[i] = j means the ith visit should be performed by agent j time[i] is the number of meters a person could have walked walk since the start when visit i is made ''' root = OTSPstate(dists,order,nagents) LO = MAX_BRANCHES // nagents state,value = branch_bound.branch_bound(root, LO , LO*nagents) return state.visit2agent,state.time
def ProcessFile(self): #open the input file and output file inputfile = self.InputFileEntry.get() outputfile = self.OutputFileEntry.get() fi = open(inputfile, "r") fo = open(outputfile, "w") start = self.StartEntry.get().split(",") end = self.EndEntry.get().split(",") #store the result in the list self.optimal_orders = [] self.lines = fi.readlines() if self.weight.get() == 0: #means the user wants to consider about the weight capacity = float(self.WeightCapacity.get()) self.orderweight = {} self.message = [] for line in self.lines: order = line.split() w = 0 for item in order: w = w + FindWeight(item) self.orderweight[line] = w self.message.append("Original") print self.lines[0] f = zip(self.orderweight.values(), self.orderweight.keys()) print len(f) a = sorted(f) length = len(a) i = 0 print length while i < length: #print "test" #merge the small orders if i + 1 < length and a[i][0] + a[i + 1][0] < capacity: if len(a[i][1].split()) + len(a[i + 1][1].split()) < 20: first_index = self.lines.index(a[i][1]) second_index = self.lines.index(a[i + 1][1]) print self.lines[first_index] print self.lines[second_index] self.message[ first_index] = "Order is merged by " + self.lines[ first_index] + " and " + self.lines[ second_index] self.lines[first_index] = self.lines[ first_index] + '\t' + self.lines[second_index] self.message.pop(second_index) self.lines.pop(second_index) print("merge %d and %d" % (first_index, second_index)) i = i + 1 else: print "break merge" break i = i + 1 #split the large orders ''' elif a[i][0]>=20: first_index = self.lines.index(a[i][1]) order = a[i][1].split() first_order = order[0:int(round(len(order)/2))] second_order = order[int(round(len(order)/2)):len(order)] temp_string = '' for item in first_order: temp_string=temp_string+item+"\t" self.lines[first_index]=temp_string self.message[first_index] = "Order is split by "+a[i][1] temp_string = '' w = 0 for item in second_order: temp_string=temp_string+item+"\t" self.lines.insert(first_index+1,temp_string) self.message.insert(first_index+1,"Order is split by "+a[i][1]) print("split %d" %(first_index)) i = i+1 ''' length = len(self.lines) i = 0 print "test" while i < length: temp_weight = self.orderweight.get(self.lines[i]) print i if temp_weight == None: #means this order is merged by other two orders before and #does not to bee split i = i + 1 continue min_index = 0 if temp_weight > capacity: #means the order is too big and needs to be split temp_weight2 = 0 order = self.lines[i].split() split_index = 0 line = self.lines[i] #find out part of the order which is not big print len(order) print("temp weight:%s" % temp_weight) for j in range(len(order)): temp_weight2 = temp_weight2 + FindWeight(order[j]) if temp_weight2 > capacity: print("change split index: %d" % split_index) split_index = j temp_weight2 = temp_weight2 - FindWeight(order[j]) break print("temp weight2:%s" % temp_weight2) print("split index: %d" % split_index) if split_index == 0: print("item %s is out of capacity" % order[0]) temp_string = '' for j in range(1, len(order)): temp_string = temp_string + order[j] + "\t" self.orderweight.pop(self.lines[i]) self.lines[i] = temp_string self.orderweight[ self.lines[i]] = temp_weight - FindWeight(order[0]) continue #insert the first half of the order, change the message temp_string = '' for j in range(split_index): temp_string = temp_string + order[j] + "\t" #if temp_string=='': self.lines.insert(i, temp_string) self.message[ i] = self.message[i] + " Order is split by " + line self.orderweight[temp_string] = temp_weight2 print("First split: %s" % temp_string) print line #insert the second half of the order temp_string = '' for j in range(split_index, len(order)): temp_string = temp_string + order[j] + "\t" self.lines.insert(i + 1, temp_string) self.message.insert( i + 1, " Order is split by " + self.lines[i + 2]) self.orderweight[temp_string] = temp_weight - temp_weight2 print("Second split: %s" % temp_string) print line #remove the original order, its weight self.orderweight.pop(self.lines[i + 2]) self.lines.pop(i + 2) length = length + 1 i = i + 1 print "Finish Preprocessing" count = 0 #go through the whole order for line in self.lines: count = count + 1 order = line.split() #print order #get the optimal order if self.alg.get() == 0: optimal_order = modified_NN(start, order, end) elif self.alg.get() == 1: optimal_order = branch_bound(start, order, end) #write the result to the corresponding outputfile for order in optimal_order: fo.write("%s " % (order)) fo.write("\n\n") #add the result to the list self.optimal_orders.append(optimal_order) print count fo.write("\n\n") print "Finished" fi.close() fo.close()