def swap_flights(self,solution,f1,f2): old_gf1 = solution[f1] solution[f1] = solution[f2] solution[f2] = old_gf1 neighbor_ins = nb.Neighbor(solution, f1, old_gf1, solution[f1]) neighbor_ins.add_swap(f2, solution[f1], old_gf1) return neighbor_ins
def generate_neighborhood_one(self): for f in range(self.num_flights): for g in range(self.num_gates): if (self.current_solution[f] is not g) and (self.is_gate_compatible(g,f)) and (not self.tabu_list.is_tabu(f,self.current_solution[f],g,self.iteration)): neighbor_dict = self.current_solution.copy() neighbor_dict[f] = g # une fois une solution voisine est générée, on stocke le mouvement qui l'a générée neighbor_ins = nb.Neighbor(neighbor_dict, f, self.current_solution[f], g) self.neighborhood.append(neighbor_ins)
def action_process_lie(self, event_data): (protocol_packet, (from_address, from_port)) = event_data # TODO: This is a simplistic way of implementing the hold timer. Use a real timer instead. self._time_ticks_since_lie_received = 0 # Sections B.1.4.1 and B.1.4.2 new_neighbor = neighbor.Neighbor(protocol_packet, from_address, from_port) (accept, rule, offer_to_ztp, warning) = self.is_received_lie_acceptable(protocol_packet) if not accept: self._lie_accept_or_reject = "Rejected" self._lie_accept_or_reject_rule = rule if warning: self.rx_warning("Received LIE packet rejected: %s", rule) else: self.rx_info("Received LIE packet rejected: %s", rule) self.action_cleanup() if offer_to_ztp: self.send_offer_to_ztp_fsm(new_neighbor) self.fsm.push_event(self.Event.UNACCEPTABLE_HEADER) return self._lie_accept_or_reject = "Accepted" self._lie_accept_or_reject_rule = rule # Section B.1.4.3 # Note: We send an offer to the ZTP state machine directly from here instead of pushing an # UPDATE_ZTP_OFFER event (see deviation DEV-2 in doc/deviations) self.send_offer_to_ztp_fsm(new_neighbor) if not self.neighbor: self.info("New neighbor detected with system-id %s", utils.system_id_str(protocol_packet.header.sender)) self.neighbor = new_neighbor self.fsm.push_event(self.Event.NEW_NEIGHBOR) self.check_three_way() return # Section B.1.4.3.1 if new_neighbor.system_id != self.neighbor.system_id: self.info("Neighbor system-id changed from %s to %s", utils.system_id_str(self.neighbor.system_id), utils.system_id_str(new_neighbor.system_id)) self.fsm.push_event(self.Event.MULTIPLE_NEIGHBORS) return # Section B.1.4.3.2 if new_neighbor.level != self.neighbor.level: self.info("Neighbor level changed from %s to %s", self.neighbor.level, new_neighbor.level) self.fsm.push_event(self.Event.NEIGHBOR_CHANGED_LEVEL) return # Section B.1.4.3.3 if new_neighbor.address != self.neighbor.address: self.info("Neighbor address changed from %s to %s", self.neighbor.address, new_neighbor.address) self.fsm.push_event(self.Event.NEIGHBOR_CHANGED_ADDRESS) return # Section B.1.4.3.4 if self.check_minor_change(new_neighbor): self.fsm.push_event(self.Event.NEIGHBOR_CHANGED_MINOR_FIELDS) self.neighbor = new_neighbor # TODO: The draft does not specify this, but it is needed # Section B.1.4.3.5 self.check_three_way()
def train(self): """ 映画のレコメンドモデル(最近傍法)の学習を行う """ if not os.path.exists(self.pickle_path): #データセットpickleがない場合、作成する review_list = self.load_data(self.review_path, self.review_att, " ") user_list = self.load_data(self.user_path, self.user_att, "\|") movie_list = self.load_data(self.movie_path, self.movie_att, "\|") data_list = self.merge_data(review_list, user_list, movie_list) x_data, y_data, user_list = self.make_movie_vec(data_list) with open(self.pickle_path, "wb") as f: pickle.dump([x_data, y_data, user_list, movie_list], f) else: x_data, y_data, user_list, movie_list = self.load_dataset() #テスト用にデータセットを100分割して、内1つをテストセットとする n_fold = 100 k_fold = KFold(n_fold, shuffle=True) for train_idx, test_idx in k_fold.split(x_data, y_data): train_x = x_data[train_idx] test_x = x_data[test_idx] train_y = y_data[train_idx] test_y = y_data[test_idx] #最近傍法によるレコメンドシステムのインスタンスをつくる nn = neighbor.Neighbor(train_x, train_y) predict_test = nn.predict(test_x) #映画IDから映画のタイトルを表示 predict_movie = [] for movie_id in predict_test: predict_movie.append(self.id_to_title(movie_id, movie_list)) correct_movie = [] for movie_id in test_y: correct_movie.append(self.id_to_title(movie_id, movie_list)) print("correct_label") print(correct_movie) print("predict_label") print(predict_movie) break
def generate_neighborhood_one(self): """ 邻域动作1:考虑停机位与航班的兼容性,为每一个航班分配一个新的停机位。并只记录下不禁忌的邻域解。邻域解全部存储在内部变量 “self.neighborhood” 中。 两层for循环作用:采用2层循环以遍历所有航班与停机位。 if语句作用:1. 在循环遍历到的航班 f 中,循环遍历到的 g 是否与 f 当前搭配的停机位不同(相同就没必要换了); 2. 判断此时循环遍历到的 g 是否与此时循环遍历到的 f 在机型上匹配(兼容性); 3. 判断遍历到的 f 和 g 所组成的 “某航班 f 的停机位交换方案” 是否还在禁忌中; 只有:是、是、否,方可将此时遍历到的停机位 g 对此时遍历到的航班 f 的原本的停机位进行更改 """ for f in range(self.num_flights): # for g in range(self.num_gates): # for g in [random.randint(0, self.num_gates) for _ in range(int(self.num_gates / 2))]: for g in random.sample([_ for _ in range(self.num_gates)], int(self.num_gates / 2)): if (self.current_solution[f] is not g) \ and (self.is_gate_compatible(g, f)) \ and (not self.tabu_list.is_tabu(f, self.current_solution[f], g, self.iteration)): # 此句传入了:遍历到的航班f、f此时对应的停机位号、遍历到的停机位g、当前迭代次数 neighbor_dict = self.current_solution.copy( ) # 没必要使用deepcopy,copy()够用 neighbor_dict[f] = g # 将进入判断语句的 g 写入遍历到的 f 的对应停机位 # 一旦产生相邻的解决方案,就存储产生该解决方案的动作 neighbor_ins = nb.Neighbor(neighbor_dict, f, self.current_solution[f], g) self.neighborhood.append(neighbor_ins)