예제 #1
0
def INTERSWAP(routes, best, rand, dm, demand, capacity):
    if rand:
        ispossible = False
        new = copy.deepcopy(routes)
        count = 0
        while not ispossible and count < 100:
            ispossible = True
            c = random.sample(range(len(new)), 2)
            #print(new[c[0]],new[c[1]])#
            b = random.randint(1, len(new[c[0]]) - 2)
            a = random.randint(1, len(new[c[1]]) - 2)
            new[c[0]][b], new[c[1]][a] = new[c[1]][a], new[c[0]][b]
            for route in new:
                if con.ROUTE_DEMAND(route, demand) > capacity:
                    ispossible = False
                    new = copy.deepcopy(routes)
            count = count + 1
        if ispossible:
            #for x in new: print(x)
            return new
        else:
            return routes
    else:
        if best:
            bestinterswap = copy.deepcopy(routes)
            for i in range(len(routes)):
                for j in range(len(routes)):
                    if i != j:

                        for k in range(1, len(routes[i]) - 1):
                            for m in range(1, len(routes[j]) - 1):
                                ispossible = True
                                new = copy.deepcopy(routes)
                                new[i][k], new[j][m] = new[j][m], new[i][k]
                                for route in new:
                                    if con.ROUTE_DEMAND(route,
                                                        demand) > capacity:
                                        ispossible = False
                                if ispossible and con.COST(
                                        routes, dm) > con.COST(new, dm):
                                    bestinterswap = copy.deepcopy(new)
            return bestinterswap
        else:
            for i in range(len(routes)):
                for j in range(len(routes)):
                    if i != j:
                        for k in range(1, len(routes[i]) - 1):
                            for m in range(1, len(routes[j]) - 1):
                                ispossible = True
                                new = copy.deepcopy(routes)
                                new[i][k], new[j][m] = new[j][m], new[i][k]
                                for route in new:
                                    if con.ROUTE_DEMAND(route,
                                                        demand) > capacity:
                                        ispossible = False
                                if ispossible and con.COST(
                                        routes, dm) > con.COST(new, dm):
                                    return new
            return routes
예제 #2
0
def CLUSTERING(sba, demand, capacity):
    clusters = []
    while con.ROUTE_DEMAND(sba, demand) > capacity:
        for i in range(1, len(sba)):
            if con.ROUTE_DEMAND(sba[0:i + 1], demand) > capacity:
                clusters.append(sba[0:i])
                sba = np.delete(sba, range(i))
                break
    clusters.append(sba)
    for i in range(0, len(clusters)):
        clusters[i] = np.append(clusters[i], 1)
        clusters[i] = np.insert(clusters[i], 0, 1)
    return clusters
예제 #3
0
def GREEDY(capacity,dm,demand,dimension):
    routes = []
    available = list(range(2, dimension+1))
    while len(available)>0:
        route = BUILDROUTE(available,capacity,dm,demand)
        routes.append(route)
    print ("GREEDY SOLUTION")
    print ("number of vehicles: ", len(routes)) 
    for x in routes: print(x," cost:",con.ROUTE_COST(x,dm),", demand:",con.ROUTE_DEMAND(x,demand))  
    print("total cost: ",con.COST(routes,dm))
    print()
    return routes
    
            
예제 #4
0
def CLUSTER_FIRST(instance, demand, dimension, capacity, dm):
    #συναρτηση για τη δημιουργια των clusters ετσι ωστε να χρησιμοποιηθουν στην
    #κατασκευη αρχικης λυσης με τη μεθοδο cluster first - route second

    #sba --> sorted by angle
    coord = pf.COORD(instance, dimension)
    sba = np.zeros((dimension, 2))

    #για ευκολια κανουμε παραλληλη μετατοπιση ετσι ωστε το depot να ερθει στην
    #αρχη των αξονων
    for i in range(0, dimension):
        sba[i][0] = coord[i][0]
        sba[i][1] = ANGLE(coord[i][1] - coord[0][1], coord[i][2] - coord[0][2])

    sba = sorted(sba, key=getKey)
    sba = np.delete(sba, 1, 1)
    sba = sba.flatten()
    sba = sba.astype(int)
    sba = np.delete(sba, 0)

    # δημιουργια των clusters
    bestclustering = CLUSTERING(sba, demand, capacity)

    # δρομολογηση με 2opt
    improvement = True
    while improvement:
        t = 0
        for i in range(len(bestclustering)):
            c = twoopt.TWOOPT(bestclustering[i], True, False, dm)
            if con.ROUTE_COST(bestclustering[i], dm) > con.ROUTE_COST(c, dm):
                bestclustering[i] = copy.deepcopy(c)
                t = t + 1
                break
        if t > 0:
            continue
        improvement = False

    print("CLUSTERING:")
    print("number of clusters:", len(bestclustering))
    for x in bestclustering:
        print(x, " cost:", con.ROUTE_COST(x, dm), ", demand:",
              con.ROUTE_DEMAND(x, demand))
    print("total cost: ", con.COST(bestclustering, dm))
    print()
    return bestclustering
예제 #5
0
def INTER_SWAP(routes,dm,demand,best,capacity):
    if best:
        bswap = routes
        print (bswap)
        
        for i in range(0,len(routes)):
            new=routes
            for j in range(i+1,len(routes)):
                new=routes
                for k in range (1,len(routes[i])-1):
                    new=routes                            
                    for l in range (1,len(routes[j])-1):
                        new = routes
                        print (routes)
                        n = demand[routes[i][k]-1][1]
                        m = demand[routes[j][l]-1][1]

                        print (routes[i][k]," ",n,"    ",routes[j][l]," ",m)
                        
                        print ()
                        if con.ROUTE_DEMAND(routes[i],demand)-n+m<=capacity and con.ROUTE_DEMAND(routes[j],demand)-m+n<=capacity:                            
                            
                            
                            new[i][k],new[j][l] = new[j][l],new[i][k]
                            #new[i][k] = routes[j][l]
                            #new[j][l] = routes[i][k]
                            if con.COST(new,dm)<con.COST(bswap,dm) and con.COST(new,dm)<con.COST(bswap,dm):
                                bswap = new
        return bswap
    else:
        for i in range(0,len(routes)):
            for j in range(i+1,len(routes)):
                for k in range (1,len(routes[i])-1):
                    n = demand[routes[i][k]-1][1]
                    for l in range (1,len(routes[j])-1):                        
                        m = demand[routes[j][l]-1][1]
                        if con.ROUTE_DEMAND(routes[i],demand)-n+m<=capacity and con.ROUTE_DEMAND(routes[j],demand)-m+n<=capacity:
                            g = routes[i][k]
                            h = routes[j][l]
                            new = routes
                            print (new)
                            new[i][k] = h
                            new[j][l] = g
                            print()
                            print(new)
                            """
                            for y in new: 
                                print (y)
                            print ()
                            """
                            if con.COST(new,dm)<con.COST(routes,dm) and con.COST(new,dm)<con.COST(routes,dm):
                                return new
        return routes
        
                            
                        
            
                

 
#RANDOM_2OPT(data) 
#RANDOM_RELOCATE(data) 
#print (RANDOM_SWAP(data))
#THREE_OPT(data)
#for i in TWOOPT(data,True): print (i)
예제 #6
0
    # gvr --> greedy vehicle routing
    gvr = greedy.GREEDY(capacity, distance_matrix, demand, dimension)
    costgvr = con.COST(gvr, distance_matrix)

    sumnn = 0
    print("set time limit")
    time = int(input())
    for i in range(0, 10):
        newnn = vns.VNS(gvr, time, costgvr, distance_matrix, demand, capacity,
                        10)

        for x in newnn:
            #x= x - np.ones((len(x),), dtype=int)
            print(x - np.ones((len(x), ), dtype=int), " cost:",
                  con.ROUTE_COST(x, distance_matrix), ", demand:",
                  con.ROUTE_DEMAND(x, demand))

        costnewnn = con.COST(newnn, distance_matrix)
        print(costnewnn, len(newnn))
        print()
        sumnn = sumnn + costnewnn
    print(sumnn / 10)
    print()
elif constr == "sw":

    clustersnew = clusterfirst.CLUSTER_FIRST(instance, demand, dimension,
                                             capacity, distance_matrix)
    costcl = con.COST(clustersnew, distance_matrix)
    sumc = 0
    print("set time limit")
    time = int(input())
예제 #7
0
def INTERRELOCATE(routes, best, rand, dm, demand, capacity):
    if rand:
        new = copy.deepcopy(routes)
        ispossible = False
        count = 0
        while not ispossible and count < 100:
            from1 = random.randint(0, len(new) - 1)
            to = random.randint(0, len(new) - 1)

            while from1 == to:
                from1 = random.randint(0, len(new) - 1)
                to = random.randint(0, len(new) - 1)
            #print ("from",from1,"  to",to)
            if len(new[from1]) - 2 == 1:
                ran = 1
            else:
                #print(len(new[from1])-2)
                ran = random.randint(1, len(new[from1]) - 2)
            count = count + 1
            if con.ROUTE_DEMAND(new[to], demand) + demand[new[from1][ran] -
                                                          1][1] <= capacity:
                ispossible = True
                if len(new[to]) - 2 == 1:
                    ranspot = 1
                else:
                    ranspot = random.randint(1, len(new[to]) - 2)
                new[to] = np.insert(new[to], ranspot, new[from1][ran])
                new[from1] = np.delete(new[from1], ran)

        if ispossible:
            #for l in new:print(l)
            new = [x for x in new if len(x) > 2]
            return new
        else:
            #for l in routes:print(routes)
            return routes
    else:
        if best:
            bir = copy.deepcopy(routes)
            for i in range(len(routes)):
                for j in range(len(routes)):
                    if i != j:
                        for k in range(1, len(routes[i]) - 1):
                            if con.ROUTE_DEMAND(routes[j], demand) + demand[
                                    routes[i][k] - 1][1] <= capacity:

                                for m in range(1, len(routes[j])):
                                    new = copy.deepcopy(routes)
                                    new[j] = np.insert(new[j], m, new[i][k])
                                    new[i] = np.delete(new[i], k)

                                    if con.COST(new, dm) < con.COST(bir, dm):
                                        bir = copy.deepcopy(new)
            return bir
        else:
            for i in range(len(routes)):
                for j in range(len(routes)):
                    if i != j:
                        for k in range(1, len(routes[i]) - 1):
                            if con.ROUTE_DEMAND(routes[j], demand) + demand[
                                    routes[i][k] - 1][1] <= capacity:

                                for m in range(1, len(routes[j])):
                                    new = copy.deepcopy(routes)
                                    new[j] = np.insert(new[j], m, new[i][k])
                                    new[i] = np.delete(new[i], k)
                                    if con.COST(new, dm) < con.COST(
                                            routes, dm):
                                        return new
            return routes