Exemple #1
0
def global_opti(solution, inst, demand, capacity, v, l):
    edges = learn.all_edges(solution)
    fixed_edges = []
    c_init = route.cost_sol(solution, inst, const.quality_cost)

    routes = route.copy_sol(solution)
    new_solution = route.copy_sol(routes)
    for e in edges:
        cp = utile.rd_point(e, solution, inst)

        routes = EC.ejection_chain(l, cp, v, routes, inst, demand, capacity,
                                   fixed_edges, "DE")
        for i in routes:
            if len(i) == 2:
                routes = EC.reject(i, routes, v, inst, demand, capacity)

        for i in range(len(routes)):
            routes[i] = LK.LK(routes[i], inst)
        # apply cross-exchange

        routes = CE.cross_exchange(cp, v, routes, inst, demand, capacity,
                                   fixed_edges, "DE")

        # apply LK
        for i in range(len(routes)):
            routes[i] = LK.LK(routes[i], inst)

        c_final = route.cost_sol(routes, inst, const.quality_cost)
        if c_init - c_final > 0:
            c_init = c_final
            new_solution = route.copy_sol(routes)

    return new_solution
Exemple #2
0
def ejection_chain(point, voisins, solution, fe, mode):
    S = 0  # global cost modification of the current solution
    copy = route.copy_sol(solution)  # In case of we don't find improvements

    s, I, R = eval_cand(point, voisins, solution, fe, mode)
    if (s, I, R) == const.Error:
        return solution

    S += s
    relocated_cust = R[0][I[0]]

    # update the solution
    R[1].insert(I[1]+1, relocated_cust)
    R[0].remove(relocated_cust)

    for k in range(const.relocation-1):
        curr_route = R[1]
        s, I, R = rd_cand(curr_route, relocated_cust,
                          voisins, solution, fe, mode)
        if (s, I, R) == const.Error:
            return solution
        S += s

        relocated_cust = R[0][I[0]]
        R[1].insert(I[1]+1, relocated_cust)
        R[0].remove(relocated_cust)

    if S > 0:
        return solution
    else:
        return copy
Exemple #3
0
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
Exemple #4
0
def rd_generate(nb, instance, demand,capacity, initial):

    Base = []
    me = 0
    for j in range(nb):
        l = round(0.1*rd.randint(1, 20), 1)
        m = round(0.1*rd.randint(1, 20), 1)
        n = round(0.1*rd.randint(1, 20), 1)
        detailed_cust = [0 for i in range(len(instance))]
        for r in range(len(initial)):
            for i in initial[r]:
                detailed_cust[i-1] = r
        routes = CW.ClarkeWright(route.copy_sol(initial), instance,
                              demand,capacity, l, m, n, detailed_cust)
        for i in range(len(routes)):
            routes[i] = LK.LK(routes[i].copy(), instance)
        routes = normalize_solution(routes)
        me += route.cost_sol(routes, instance,const.quality_cost)
        Base.append((route.cost_sol(routes, instance,const.quality_cost), routes, (l, m, n)))
    Base.sort()

    return Base, [Base[0][0], Base[len(Base)-1][0], me/nb]
Exemple #5
0
def learning_heuristic(instance, demand,capacity, l):
    # compute global variables
    namefile = "resultats/Heuristic_results/Values/all/golden7.txt"
    all_sol = []
    tps_deb = time.time()
    max_d = opt.max_depth(instance)
    v = utile.voisins(const.KNN, instance)
    initial = CW.init_routes(instance, demand)
    edges, param = learn.learning_results(0.5, 2, 100, instance, demand,capacity, initial)
    initial_routes = learn.complete(learn.destruction(learn.ignore_0(edges)), instance, demand,capacity)
    tps_learn = time.time()

    rw.writef(namefile, 'Time = ' + str(tps_learn-tps_deb))

    base = []
    costs = 0
    fixed_edges = []
   
    best_cost = route.cost_sol(initial_routes, instance,const.quality_cost)
    for i in range(40):
        print(i)

        (lam, mu, nu) = param[0]
        init, sol = opt.optimisation_heuristic(
            route.copy_sol(initial_routes), instance, demand,capacity, lam, mu, nu, l, max_d, v,fixed_edges)
        base.append(sol)
        c_sol = route.cost_sol(sol, instance,const.quality_cost)
        all_sol.append((c_sol, sol))
        
        if c_sol < best_cost:
            
            best_sol = sol
            best_cost = c_sol


        if i%4 == 0 and i!=0 :
            print("learn")
            edges = []
            fixed_edges = []
            base = []
            mat_qual = learn.init_matrix(len(instance))
            mat_qual = learn.learn(mat_qual, base)
            e_qual = learn.mat_info_rg(int(len(demand)*0.8), mat_qual)
            for e in e_qual:
                if not learn.is_edge_in(e, edges) and not learn.unfeasable_edge(e, edges):
                    edges.append(e)
            initial_routes = learn.complete(learn.destruction(
                learn.ignore_0(edges)), instance, demand,capacity)
            edges, param = learn.learning_results(
                0.8, 2, 100, instance, demand,capacity, initial_routes)
            initial_routes = learn.complete(learn.destruction(
                learn.ignore_0(edges)), instance, demand,capacity)
        
        else :
            print("best learn")
            edges = utile.fixed_alea(learn.all_edges(best_sol),0.95)
            initial_routes = learn.complete(learn.destruction(
                learn.ignore_0(edges)), instance, demand,capacity)
            """
            edges,param = learning_results(0.95,2,100,instance,demand,initial_routes)
            initial_routes = complete(destruction2(
                ignore_0(edges)), instance, demand)
            """    
                

    all_sol.sort()
    tps_fin = time.time()
    print(tps_fin-tps_deb)
    costs = 0
    for i in range(10):
        c_sol, sol = all_sol[i]
        costs += c_sol

        rw.writef(namefile, '')
        rw.writef(namefile, 'res = ' + str(round(c_sol, 3)))
        rw.writef(namefile, 'res_int = ' + str(round(route.cost_sol(sol, instance, "Int"))))
        rw.writef(namefile, 'solution = ' + str(sol))

    rw.writef(namefile, '')
    rw.writef(namefile, 'Mean = ' + str(costs/10))
    rw.writef(namefile, 'Execution = ' + str(tps_fin-tps_deb))
    rw.writef(namefile, '')
Exemple #6
0
def optimisation_heuristic(initial_routes, inst, demand, capacity, lam, mu, nu,
                           l, max_d, v, fixed_edges):
    tps1 = time.time()
    B = [
        penalization_function(1, 0, 0, max_d),
        penalization_function(1, 1, 0, max_d),
        penalization_function(1, 0, 1, max_d),
        penalization_function(1, 1, 1, max_d),
        penalization_function(0, 1, 0, max_d),
        penalization_function(0, 1, 1, max_d)
    ]

    b_i = 0
    b = B[b_i]
    p = [[0 for j in range(len(inst))] for i in range(len(inst))]

    detailed_cust = [0 for i in range(len(inst))]
    for r in range(len(initial_routes)):
        for i in initial_routes[r]:
            detailed_cust[i - 1] = r
    initial_routes = CW.ClarkeWright(initial_routes, inst, demand, capacity,
                                     lam, mu, nu, detailed_cust)

    routes = route.copy_sol(initial_routes)
    routes2 = route.copy_sol(routes)

    c_init = route.cost_sol(routes, inst, const.quality_cost)

    tps2 = time.time()
    tpsGS = time.time()
    tpsCH = time.time()

    while tps2 - tps1 < len(demand) / 8:

        # find the worst edge
        worst = bad_edge(b, p, routes, inst, fixed_edges)[1]

        p[worst[0]][worst[1]] += 1
        p[worst[1]][worst[0]] += 1

        # apply ejection-chain
        cp = utile.rd_point(worst, routes, inst)

        routes = EC.ejection_chain(l, cp, v, routes, inst, demand, capacity,
                                   fixed_edges, "RD")
        for i in routes:
            if len(i) == 2:
                routes = EC.reject(i, routes, v, inst, demand, capacity)

        for i in range(len(routes)):
            routes[i] = LK.LK(routes[i], inst)
        # apply cross-exchange

        routes = CE.cross_exchange(cp, v, routes, inst, demand, capacity,
                                   fixed_edges, "RD")

        # apply LK
        for i in range(len(routes)):
            routes[i] = LK.LK(routes[i], inst)

        #routes = global_opti(routes,inst,demand,v,l)
        c_final = route.cost_sol(routes, inst, const.quality_cost)

        if c_final < c_init:
            routes2 = route.copy_sol(routes)  # new optimum

            for i in routes2:
                if len(i) == 2:
                    routes2 = EC.reject(i, routes2, v, inst, demand, capacity)
            c_init = route.cost_sol(routes2, inst, const.quality_cost)
            print(round(tps2 - tps1, 2), round(c_init, 3))

            tps1 = time.time()
            tpsCH = time.time()
            tpsGS = time.time()

        if tps2 - tpsGS > 10:
            # return to the last best solution, for gs iterations

            routes = route.copy_sol(routes2)

            tpsGS = time.time()

        if tps2 - tpsCH > 5:
            tpsCH = time.time()
            b_i += 1

            if b_i < len(B):
                b = B[b_i]
                p = [[0 for j in range(len(inst))] for i in range(len(inst))]

            else:
                b_i = 0
                b = B[b_i]
                p = [[0 for j in range(len(inst))] for i in range(len(inst))]

        tps2 = time.time()

    for i in (routes2):
        if len(i) == 2:
            routes2 = EC.reject(i, routes2, v, inst, demand, capacity)
        if len(i) == 1:
            routes2.remove(i)

    for i in range(len(routes2)):
        routes2[i] = LK.LK(routes2[i], inst)

    return initial_routes, routes2
Exemple #7
0
def learning_heuristic():
    # compute global variables
    instance, demand = const.instance, const.demand
    namefile = const.namefile
    costs = 0
    all_sol = []
    fixed_edges = []
    BaseSolution = []
    tps_deb = time.time()

    # learning
    initial = CW.init_routes()
    edges, param = learn.learning_results(0.7, 2, 50, initial, const.typeBase,
                                          const.percent,
                                          const.learningCriterion)
    initial_routes = route.complete(utile.destruction(utile.ignore_0(edges)))

    tps_learn = time.time()

    rw.writef(namefile, 'Learning time = ' + str(tps_learn - tps_deb))

    # start

    cpt = 0
    for i in range(const.NbIterations):
        print(i)
        edges = []
        (lam, mu, nu) = param[0]  # best tuple of the learning phase
        BaseSolution = opt.optimisation_heuristic(
            route.copy_sol(initial_routes), lam, mu, nu, fixed_edges)

        all_sol += BaseSolution
        # conserve best and worst costs
        stat = [BaseSolution[0][0], BaseSolution[-1][0]]

        # New learning phase
        quality = (stat[1] - stat[0]) / 10 + stat[0]
        crit = max(const.upBound - cpt / 10, const.lowBound)
        cpt += 1
        if crit == const.lowBound:
            cpt = 0
        ls_qual = learn.learning_set_quality(BaseSolution, quality)
        mat_qual = learn.init_matrix(len(instance))
        mat_qual = learn.learn(mat_qual, ls_qual)
        e_qual = learn.mat_info_rg(int(len(demand) * crit), mat_qual)

        initial_routes = route.complete(
            utile.destruction(utile.ignore_0(e_qual)))

    # write results in a file

    all_sol.sort()
    tps_fin = time.time()
    print(tps_fin - tps_deb)
    costs = 0
    for i in range(10):
        c_sol, sol = all_sol[i]
        costs += c_sol

        rw.writef(namefile, '')
        rw.writef(namefile, 'res = ' + str(round(c_sol, 3)))
        rw.writef(namefile,
                  'res_int = ' + str(round(route.cost_sol(sol, "Int"))))
        rw.writef(namefile, 'solution = ' + str(sol))

    rw.writef(namefile, '')
    rw.writef(namefile, 'Mean = ' + str(costs / 10))
    rw.writef(namefile, 'Execution = ' + str(tps_fin - tps_deb))
    rw.writef(namefile, '')
Exemple #8
0
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