-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontroller.py
185 lines (156 loc) · 6.98 KB
/
controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import random
from copy import copy, deepcopy
import time
import PMOEAD
import multiprocessing as mp
from Initial import initial
from Variation import CrossOver
from evaluate_solution import evaluate_single
INF = 1e9
Time_limit = 3600
STEP = 0.005 ## Overlapping adding issue
MAX_RATIO = 0.3 ## Maximum overlapping issue
class ParallelWorker(mp.Process):
def __init__(self, in_queue, out_queue, random_seed):
super(ParallelWorker, self).__init__(target=self.start)
self.inQ = in_queue
self.outQ = out_queue
random.seed(random_seed)
def run(self):
while True:
task = self.inQ.get()
population, neighbours, z, obj, weight_vector, dimension, fitness, iteration_num, begin, end, data = task
sol = parallel(population, neighbours, z, obj, weight_vector, dimension, fitness, iteration_num, begin, end,
data)
self.outQ.put(sol)
def create_worker(num):
workers = []
for i in range(num):
workers.append(ParallelWorker(mp.Queue(), mp.Queue(), random.randint(0, 10 ** 9)))
workers[i].start()
return workers
def finish_worker(workers):
for w in workers:
w.terminate()
def parallel(population, neighbours, z, obj, weight_vector, dimension, fitness, iteration_num, begin, end, data):
iteration = 0
neighbor_num = len(neighbours[0])
start_time = time.time()
while iteration < iteration_num and time.time() - start_time < Time_limit:
# print('iteration', iteration, 'time', time.time() - start_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]
PMOEAD.update_neighbour(population, neighbours[index], individual, i_obj, obj, fitness, weight_vector)
index += 1
return population, obj, fitness
def combine_population(result, cpu_num, population_size):
new_population = [0 for _ in range(population_size)]
new_obj = [None for _ in range(population_size)]
new_fitness = [0 for _ in range(population_size)]
for i in range(population_size):
index = 0
min_fitness = result[0][2][i]
for j in range(cpu_num):
if min_fitness > result[j][2][i]:
index = j
min_fitness = result[j][2][i]
new_population[i] = result[index][0][i]
new_obj[i] = result[index][1][i]
new_fitness[i] = result[index][2][i]
return new_population, new_obj, new_fitness
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 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
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 partition_performance_avg(fit, begin, end):
res = 0
begin = int(begin)
end = int(end)
for i in range(begin, end):
res = res + fit[i]
return res / (end - begin)