def best_cost_route_crossover(offsprings, nodes, rate=0.5): new_offsprings = [] half = int(len(offsprings)/2) for (indv1, indv2) in zip (offsprings[0:half], offsprings[half:]): tmp1 = copy.deepcopy(indv1) tmp2 = copy.deepcopy(indv2) if random.random() < rate: (tmp1.chromosome, tmp2.chromosome) = \ _best_cost_route_crossover(nodes, indv1.chromosome, indv2.chromosome) tmp1.chromosome = remove_null_route(tmp1.chromosome) # Remove the route which has no nodes tmp2.chromosome = remove_null_route(tmp2.chromosome) new_offsprings.append(tmp1) new_offsprings.append(tmp2) return new_offsprings
def _insertion(chromosome, nodes, rate=0.02): size = len(_flatten(chromosome)) for _ in range(size): if random.random() < rate: _random_choice_and_reinsert(chromosome, nodes) chromosome = remove_null_route(chromosome) return chromosome
def insertion(offsprings, nodes, rate=0.2, irate=0.02): new_offsprings = [] for indv in offsprings: tmp = copy.deepcopy(indv) if random.random() < rate: tmp.chromosome = _insertion(indv.chromosome, nodes, irate) tmp.chromosome = remove_null_route(tmp.chromosome) # Remove the route which has no nodes new_offsprings.append(tmp) return new_offsprings
def exchange_chromosome(nodes, ch1): deliver_size = nodes.get_deliver_customer_size() no_Null_Chromosome = copy.deepcopy(remove_null_route(ch1)) if len(no_Null_Chromosome) == 15: if random.random() > 0.5: # two route exchange two_index = random.sample(range(0, len(no_Null_Chromosome)), 2) route1 = no_Null_Chromosome[two_index[0]] route2 = no_Null_Chromosome[two_index[1]] raw_index1 = ch1.index(route1) raw_index2 = ch1.index(route2) reversed_route_1 = { node_id: index for (index, node_id) in route1.items() } reversed_route_2 = { node_id: index for (index, node_id) in route2.items() } route_deliver_nodes_1 = np.sort( np.array(list( reversed_route_1.keys())))[:int(len(reversed_route_1) / 2)] route_deliver_nodes_2 = np.sort( np.array(list( reversed_route_2.keys())))[:int(len(reversed_route_2) / 2)] exchange_start_node_1 = random.sample( route_deliver_nodes_1.tolist(), 1)[0] exchange_start_node_2 = random.sample( route_deliver_nodes_2.tolist(), 1)[0] exchange_end_node_1 = exchange_start_node_1 + deliver_size exchange_end_node_2 = exchange_start_node_2 + deliver_size tmp = route1[reversed_route_1[exchange_start_node_1]] route1[reversed_route_1[exchange_start_node_1]] = route2[ reversed_route_2[exchange_start_node_2]] route2[reversed_route_2[exchange_start_node_2]] = tmp tmp = route1[reversed_route_1[exchange_end_node_1]] route1[reversed_route_1[exchange_end_node_1]] = route2[ reversed_route_2[exchange_end_node_2]] route2[reversed_route_2[exchange_end_node_2]] = tmp ch1[raw_index1] = route1 ch1[raw_index2] = route2 else: # delete elements of one route and add them to another route two_index = random.sample(range(0, len(no_Null_Chromosome)), 2) route1 = no_Null_Chromosome[two_index[0]] route2 = no_Null_Chromosome[two_index[1]] raw_index1 = ch1.index(route1) raw_index2 = ch1.index(route2) route_nodes_1 = list(route1.values()) route_nodes_2 = list(route2.values()) exchange_node1 = random.sample(route_nodes_1, 1)[0] if exchange_node1 <= deliver_size: exchange_start_node_1 = exchange_node1 exchange_end_node_1 = exchange_node1 + deliver_size else: exchange_start_node_1 = exchange_node1 - deliver_size exchange_end_node_1 = exchange_node1 route_nodes_1.remove(exchange_start_node_1) route_nodes_1.remove(exchange_end_node_1) route2_size = len(route_nodes_2) random_insert = random.randint(0, route2_size) route_nodes_2.insert(random_insert, exchange_start_node_1) insert_index = route_nodes_2.index(exchange_start_node_1) route_nodes_2.insert( random.randint(insert_index + 1, len(route_nodes_2)), exchange_end_node_1) route1 = {(i + 1): int(node_id) for (i, node_id) in enumerate(route_nodes_1)} route2 = {(i + 1): int(node_id) for (i, node_id) in enumerate(route_nodes_2)} ch1[raw_index1] = route1 ch1[raw_index2] = route2 elif len(no_Null_Chromosome) < 15 and random.random() > 0.9: route = no_Null_Chromosome[0] lengths = [len(ch1[i]) for i in range(len(ch1))] raw_index = ch1.index(route) route_nodes = list(route.values()) exchange_node = random.sample(route_nodes, 1)[0] if exchange_node <= deliver_size: exchange_start_node_1 = exchange_node exchange_end_node_1 = exchange_node + deliver_size else: exchange_start_node_1 = exchange_node - deliver_size exchange_end_node_1 = exchange_node route_nodes.remove(exchange_start_node_1) route_nodes.remove(exchange_end_node_1) route = {(index + 1): node_id for index, node_id in enumerate(route_nodes)} ch1[raw_index] = route added_route = [exchange_start_node_1, exchange_end_node_1] added_route = {(index + 1): int(node_id) for index, node_id in enumerate(added_route)} add_index = lengths.index(0) '''if raw_index == 0: ch1[raw_index + 1] = added_route elif raw_index == (len(ch1) - 1): ch1[raw_index - 1] = added_route else: ch1[raw_index + 1] = added_route''' ch1[add_index] = added_route return ch1