def cross_exchange(point, voisins, routes, inst, demand,capacity, fixedEdges, mode): # compute the two routes considered, and the NN of the point we remove (a). v is a point (r1, r2), v,c = route.another_route(point, voisins, routes, inst,demand,capacity, fixedEdges, "CE", mode) if v < 0: return routes # copy of the current solution current_cand = [r1.copy(), r2.copy()] cand = [] c_init = route.cost_sol(current_cand, inst, const.quality_cost) # for a future comparison i_v = current_cand[1].index(v) i_c = current_cand[0].index(c) if i_v != 1: current_cand[0][i_c], current_cand[1][i_v - 1] = current_cand[1][i_v-1], c else: current_cand[0][i_c], current_cand[1][i_v] = current_cand[1][i_v], c if mode == "RD": parcours_i = utile.permut([i for i in range(len(r2)-1)]) parcours_j = utile.permut([j for j in range(len(r1)-1)]) if mode == "DE": parcours_i = [i for i in range(len(r2)-1)] parcours_j = [j for j in range(len(r1)-1)] for i in parcours_i: if i != i_v-1: for j in parcours_j: if j != i_c-1: p1 = current_cand[0][j+1] p2 = current_cand[1][i+1] current_cand[0][j+1], current_cand[1][i + 1] = p2, p1 if route.cost_sol(current_cand, inst, const.quality_cost) < c_init and route.route_demand(current_cand[0], demand) <= capacity and route.route_demand(current_cand[1], demand) <= capacity: if mode == "RD": routes.remove(r1) routes.remove(r2) routes = routes + current_cand return routes elif mode == "DE" : c_init = route.cost_sol(current_cand, inst,const.quality_cost) cand = route.copy_sol(current_cand) current_cand = [r1.copy(), r2.copy()] if mode == "DE" and cand != []: routes.remove(r1) routes.remove(r2) routes = routes + cand return routes
def another_route(point, voisins, routes, fixedEdges, operator, mode): r1 = find_route(point, routes) adja = utile.fixed_adjacents(point, fixedEdges) if mode == "RD": permut_voisins = utile.permut(voisins[point]) elif mode == "DE": permut_voisins = voisins[point] if operator == "CE": for i in permut_voisins: r2 = find_route(i, routes) # we verify that the future demand on the route won't exceed his capacity if (r2 != r1 and i != 0) and ( route_demand(r1) - const.demand[point] + const.demand[i] <= const.capacity) and (route_demand(r2) - const.demand[i] + const.demand[point] <= const.capacity): return ((r1, r2), i, point) # error case, we haven't found a second route, so no modifications return ((r1, r1), -1, -1) elif operator == "EC": for i in permut_voisins: r2 = find_route(i, routes) if r2 != r1 and i != 0 and len(adja) == 0 and route_demand( r2) + const.demand[point] <= const.capacity: return ((r1, r2), i) return (r1, r1), -1
def rd_cand(route, np, voisins, routes, inst, demand,capacity, fe, mode): parcours = utile.permut([i for i in range(len(route))]) best = 0 for i in parcours: p = route[i] if p != np: cp = utile.rd_point((route[i-1], p), routes, inst) cand = eval_cand(cp, voisins, routes, inst, demand,capacity, fe,mode) if cand[0] > best: if mode == "RD": return cand elif mode == "DE": best = cand[0] best_cand = cand if best > 0: return best_cand return const.Error
def cross_exchange(point, voisins, routes, fixedEdges, mode): # compute the two routes considered, and the nearest neighbor of the point we remove. (r1, r2), neigh, c = route.another_route(point, voisins, routes, fixedEdges, "CE", mode) if neigh < 0: return routes # copy of the current solution current_cand = [r1.copy(), r2.copy()] cand = [] c_init = route.cost_sol(current_cand, const.quality_cost) i_neigh = current_cand[1].index(neigh) i_c = current_cand[0].index(c) # we verify that we won't exchange the depot if i_neigh != 1: current_cand[0][i_c], current_cand[1][i_neigh - 1] = current_cand[1][i_neigh-1], c else: current_cand[0][i_c], current_cand[1][i_neigh] = current_cand[1][i_neigh], c # random exploration if mode == "RD": parcours_i = utile.permut([i for i in range(len(r2)-1)]) parcours_j = utile.permut([j for j in range(len(r1)-1)]) # order exploration if mode == "DE": parcours_i = [i for i in range(len(r2)-1)] parcours_j = [j for j in range(len(r1)-1)] # we try to exchange two customers between the routes of c and neigh for i in parcours_i: if i != i_neigh-1: for j in parcours_j: if j != i_c-1: p1 = current_cand[0][j+1] p2 = current_cand[1][i+1] current_cand[0][j+1], current_cand[1][i + 1] = p2, p1 if route.cost_sol(current_cand, const.quality_cost) < c_init and route.route_demand(current_cand[0]) <= const.capacity and route.route_demand(current_cand[1]) <= const.capacity: # first improve if mode == "RD": routes.remove(r1) routes.remove(r2) routes = routes + current_cand return routes # return the best elif mode == "DE": c_init = route.cost_sol( current_cand, const.quality_cost) cand = route.copy_sol(current_cand) # reset the modification current_cand = [r1.copy(), r2.copy()] if mode == "DE" and cand != []: routes.remove(r1) routes.remove(r2) routes = routes + cand return routes