def parallel_run(rounds, iteration_num, cpu_num, file_name, dimension, population_size): population, weight_vector, neighbours, obj, z, fitness, data = initial( population_size, dimension, file_name) workers = create_worker(cpu_num) length = population_size // cpu_num result = [[None for _ in range(3)] for _ in range(cpu_num)] while rounds > 0: for i in range(cpu_num): begin = length * i end = length * (i + 1) if i == cpu_num: end = population_size workers[i].inQ.put( (deepcopy(population), neighbours, deepcopy(z), deepcopy(obj), weight_vector, dimension, deepcopy(fitness), iteration_num, begin, end, data)) for i in range(cpu_num): result[i][0], result[i][1], result[i][2] = workers[i].outQ.get() population, obj, fitness = combine_population(result, cpu_num, population_size) print(rounds) rounds -= 1 finish_worker(workers) return population, obj
def PMOEAD(file_name, dimension, population_size, max_iteration, begin, end): population, weight_vecotr, neighbours, obj, z, fitness, data = initial( population_size, dimension, file_name) iteration = 0 negihbour_num = len(neighbours[0]) while iteration < max_iteration: iteration += 1 index = begin while index < end: p = random.sample(range(0, negihbour_num), 2) # select two parents from its neighbour p1 = int(neighbours[index][p[0]]) p2 = int(neighbours[index][p[1]]) indiviual = CrossOver(population[p1], population[p2], dimension) i_obj = evaluate_single(indiviual, copy(data)) if i_obj[0] < z[0]: z[0] = i_obj[0] if i_obj[1] < z[1]: z[1] = i_obj[1] update_neighbour(population, neighbours[index], indiviual, obj, fitness, weight_vecotr) index += 1 # print(fiteration {iteration}' print("iteration:", iteration) return population, obj
def parallel_run_bytime(max_time, iteration_num, cpu_num, file_name, dimension, population_size, overlapping_ratio=0, auto_adjust=False): TIME = time.time() round_turn = 1 population, weight_vector, neighbours, obj, z, fitness, data = initial( population_size, dimension, file_name) workers = create_worker(cpu_num) length = population_size // cpu_num ratios = [0 for _ in range(cpu_num)] partition_performance = [INF for _ in range(cpu_num)] overlapping_part = int(overlapping_ratio * length) result = [[None for _ in range(3)] for _ in range(cpu_num)] while time.time() - TIME < max_time: for i in range(cpu_num): begin = length * i end = min(length * (i + 1) + overlapping_part, population_size) if auto_adjust: begin = length * i end = min(length * (i + 1) + ratios[i] * length, population_size) per = partition_performance_avg(fitness, begin, end) if partition_performance[i] <= per and ratios[i] < MAX_RATIO: end = max(end + STEP * length, population_size) ratios[i] += STEP print('ratio:', ratios[i]) partition_performance[i] = per if i == cpu_num: end = population_size begin = int(begin) end = int(end) workers[i].inQ.put( (deepcopy(population), neighbours, deepcopy(z), deepcopy(obj), weight_vector, dimension, deepcopy(fitness), iteration_num, begin, end, data)) for i in range(cpu_num): result[i][0], result[i][1], result[i][2] = workers[i].outQ.get() population, obj, fitness = combine_population(result, cpu_num, population_size) # print(round_turn) round_turn += 1 finish_worker(workers) return population, obj
def pmoead_by_time(file_name, dimension, population_size, max_time, begin, end): current_time = time.time() population, weight_vector, neighbours, obj, z, fitness, data = initial(population_size, dimension, file_name) iteration = 0 neighbor_num = len(neighbours[0]) while time.time() - current_time < max_time: iteration += 1 index = begin while index < end: p = random.sample(range(0, neighbor_num), 2) # select two parents from its neighbour p1 = int(neighbours[index][p[0]]) p2 = int(neighbours[index][p[1]]) individual = CrossOver(population[p1], population[p2], dimension) i_obj = evaluate_single(individual, copy(data)) if i_obj[0] < z[0]: z[0] = i_obj[0] if i_obj[1] < z[1]: z[1] = i_obj[1] update_neighbour(population, neighbours[index], individual, obj, fitness, weight_vector) index += 1 print("iteration:", iteration) return population, obj
def naive_paralle(total_iteration, cpu_num, file_name, dimension, population_size): population, weight_vecotr, neighbours, obj, z, fitness, data = initial( population_size, dimension, file_name) workers = create_worker(cpu_num) length = population_size // cpu_num result = [[None for _ in range(3)] for _ in range(cpu_num)] for i in range(cpu_num): begin = length * i end = length * (i + 1) if i == cpu_num: end = population_size workers[i].inQ.put( (deepcopy(population), neighbours, deepcopy(z), deepcopy(obj), weight_vecotr, dimension, deepcopy(fitness), total_iteration, begin, end, data)) # result[i][0] population , result[i][1] obj The value of erate and frate # result[i][1] obj,[[frate,erate],[],,] for i in range(cpu_num): result[i][0], result[i][1], result[i][2] = workers[i].outQ.get() population, obj, fitness = combine_population(result, cpu_num, population_size) finish_worker(workers) return population, obj
from Transaction import transaction from Evaluation import evaluation from Determine import determine from Plot import plot_hc import matplotlib.pyplot as plt iteration = sys.argv[1] city = sys.argv[2] dim = sys.argv[3] best_value = 0 eachiteration = 0 data = np.loadtxt('dataSet/' + city + '.txt', dtype=np.str, delimiter=" ") solution = initial(data, int(city), int(dim)) best_solution = solution.copy() best_value = evaluation(solution) plt.figure() plt.ion() while (eachiteration != int(iteration)): old_solution, solution = transaction(solution) distance = evaluation(solution) best_value, best_solution = determine(best_solution, best_value, solution, distance) solution = best_solution.copy()
import matplotlib.pyplot as plt iteration = sys.argv[1] city = sys.argv[2] dim = sys.argv[3] neighbor_number = sys.argv[4] temperature = sys.argv[5] rate = sys.argv[6] best_value = 0 eachiteration = 0 solution = [] data = np.loadtxt('dataSet/' + city + '.txt', dtype=np.str, delimiter=" ") temp = initial(data, int(city), int(dim)) for each_neighbor_number in range(int(neighbor_number)): solution.append(temp) solution = np.asarray(solution) old_solution = solution.copy() best_solution = solution[0].copy() best_value = evaluation(solution[0]) plt.figure() plt.ion() while ((eachiteration != int(iteration)) and (float(temperature) > 0)): distance = np.zeros(int(neighbor_number))