def run(self): print("The handle of the message has been started!") while True: if self.msg_list.empty(): time.sleep(2) continue else: msg = self.msg_list.get() new_msg = self.process(msg) if new_msg.statu == 0: print(new_msg.content) self.data = new_msg.content self.dist = self.init_distance(self.data) msg = message(2, self.server_port) send_to(msg) elif new_msg.statu == 1: send_to(message(2, new_msg.content), port=self.next_port) elif new_msg.statu == 444: #print("test content:",new_msg.content) pass elif new_msg.statu == 669: print("new_msg.content: ", new_msg.content) elif new_msg.statu == 666: self.next_port = new_msg.content msg = message(444, 'your message has been processed.') send_to(msg, port=self.next_port) dist_msg = message(801, self.dist) send_to(dist_msg, port=self.next_port) for i in range(self.nodes): solutions = self.init_solutions() #print('solutions:',solutions) msg = message(1, solutions).msg_encode() self.msg_list.put(msg) elif new_msg.statu == 700: print('a final result rcvd.') self.count += 1 self.result.append(new_msg.content) if self.count == self.nodes: sorted_solution = sorted( solutions, key=lambda x: cr.cost(x, self.dist))[0] print("final result is:", sorted_solution) print("least cost is :", cr.cost(sorted_solution, self.dist)) else: print("something wrong! error %d" % new_msg.statu, new_msg.content)
def next_generation(solutions, matrix=m): length = len(solutions) tmp = [] for i in range(length): for j in range(length): if i != j: tmp.append(cross_and_mutation(solutions[i], solutions[j])) tmp = np.array(tmp) #print("tmp.shape",tmp.shape) #print(solutions.shape) solutions = np.vstack((solutions, tmp)) sorted_solutions = sorted(solutions, key=lambda x: tc.cost(x, matrix))[:length] return np.array(sorted_solutions)
def get_res(s1,s2): if tc.cost(s1,m)<tc.cost(s2,m): return s1 else: return s2
return s1 else: return s2 print("start work.") while True: generations=to.wait_np_file(work_path,'generations.npy',delete=True) print("generation information getted. Now setting the gen_rdd.") gen_rdd=sc.parallelize(generations) print("rdd setted,now maping.") res=gen_rdd.map(it.iteration) print("mapping over,now reducing.") res2=res.reduce(get_res) print("The best solution is:",res2) c=tc.cost(res2,m) print("The lowest cost is :",c) to.set_np_file(work_path,'final_solution.npy',[res2,c]) # msg=to.wait_np_file_start(work_path,'solution',delete=True) # #print(msg) # solution,c_orig,file_name=msg # if file_name not in dic: # dic[file_name]=[solution,c_orig,0] # to.set_np_file(work_path,'cal_'+file_name[9:],msg) # else: # dic[file_name][:2]=[solution,c_orig] # dic[file_name][2]+=1 # if dic[file_name][2]==10:
import create as tc import os_file as to import numpy as np import time work_path='./work/' m=to.wait_np_file(work_path,'distance_matrix.npy') while True: s_orig,c_orig,name=to.wait_np_file_start(work_path,'cal',delete=True) s=tc.mutation_1(s_orig) c=tc.cost(s,m) if c<c_orig: s_orig=s c_orig=c s=tc.mutation_2(s_orig) c=tc.cost(s,m) if c<c_orig: s_orig=s c_orig=c res=np.array([s_orig,c_orig,name]) to.set_np_file(work_path,name,res)