Exemplo n.º 1
0
import create as tc
import os_file as to
import numpy as np
import time
work_path = './work/'
nodes = 10

time.sleep(3)

while True:
    cities_file = to.wait_msg_file(work_path, 'new_job.txt', delete=True)
    cities = to.wait_np_file(work_path, cities_file, delete=False)
    to.clean_work_path(work_path)
    dist = tc.distance_matrix(cities)
    to.set_np_file(work_path, 'distance_matrix.npy', dist)
    num = len(cities)
    population = 10

    generations = []
    print("generate the solution of routes.")
    for i in range(nodes):
        routes = tc.init_solutions(population, num)
        generations.append(routes)
        # hash_num=np.random.randint(100000,999999)
        # file_name='solution_'+str(hash_num)+'.npy'
    to.set_np_file(work_path, 'generations.npy', generations)
    print("solution file setting over.waiting for results.")
    # for i in range(population):
    #     hash_num=np.random.randint(100000,999999)
    #     file_name='solution_'+str(hash_num)+'.npy'
    #     to.set_np_file(work_path,file_name,np.array([solutions[i],9999999999999999,file_name]))
Exemplo n.º 2
0
    for i in range(num):
        solutions.append(random_solution(cities))
    return solutions


def mutation_1(solution):
    n = len(solution)
    start = np.random.randint(0, n - 2)
    end = np.random.randint(start + 1, n)
    k = solution[start:end]
    solution[start:end] = k[::-1]
    return solution


def mutation_2(solution):
    n = len(solution)
    start = np.random.randint(0, n - 2)
    end = np.random.randint(start + 1, n)
    k = solution[start]
    solution[start:end - 1] = solution[start + 1:end]
    solution[end - 1] = k
    return solution


if __name__ == "__main__":
    tos.clean_work_path(work_path)
    cities = create_cities(100)
    tos.set_np_file(work_path, 'cities.npy', cities)
    with open(work_path + 'new_job.txt', 'w') as ff:
        ff.write('cities.npy')
Exemplo n.º 3
0
        return s2

print("start work.")

while True:
    generations=to.wait_np_file(work_path,'generations.npy',delete=True)
    print("generation information getted. Now setting the gen_rdd.")
    gen_rdd=sc.parallelize(generations)
    print("rdd setted,now maping.")
    res=gen_rdd.map(it.iteration)
    print("mapping over,now reducing.")
    res2=res.reduce(get_res)
    print("The best solution is:",res2)
    c=tc.cost(res2,m)
    print("The lowest cost is :",c)
    to.set_np_file(work_path,'final_solution.npy',[res2,c])


    # msg=to.wait_np_file_start(work_path,'solution',delete=True)
    # #print(msg)
    # solution,c_orig,file_name=msg
    # if file_name not in dic:
    #     dic[file_name]=[solution,c_orig,0]

    #     to.set_np_file(work_path,'cal_'+file_name[9:],msg)
    # else:
    #     dic[file_name][:2]=[solution,c_orig]
    #     dic[file_name][2]+=1
    #     if dic[file_name][2]==10:
    #         #print("Get one final solution.")
    #         final_res.append(dic[file_name][:2])
Exemplo n.º 4
0
import create as tc
import os_file as to
import numpy as np
import time

work_path='./work/'
m=to.wait_np_file(work_path,'distance_matrix.npy')

while True:
    s_orig,c_orig,name=to.wait_np_file_start(work_path,'cal',delete=True)
    s=tc.mutation_1(s_orig)
    c=tc.cost(s,m)
    if c<c_orig:
        s_orig=s
        c_orig=c
    s=tc.mutation_2(s_orig)
    c=tc.cost(s,m)
    if c<c_orig:
        s_orig=s
        c_orig=c
    res=np.array([s_orig,c_orig,name])
    to.set_np_file(work_path,name,res)