-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
211 lines (147 loc) · 5.04 KB
/
test.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from CONSTANT import import_data, Truck, Package, Process, random_seed
from copy import deepcopy
import random
random.seed(random_seed)
'''
encode:
- chromosome have length = length of packages
decode:
- each gene in chromosome represent the truck (gene) that delivery package (gene's index)
- loop through truck, loop through packages of that truck, Insert package to truck
fitness = max(truck.runtime), sum(truck.runtime)
'''
class Individual:
def __init__(self, trucks: list[Truck], packages: list[Package], chromosome = None):
# len of chromosome
self.n = len(packages)
if chromosome == None:
self.chromosome = [random.randint(1, len(trucks)) for _ in range(self.n)]
else:
self.chromosome = chromosome
self.trucks = deepcopy(trucks)
self.packages = deepcopy(packages)
self.fitness = 0
# decode
def decode(self):
for truck in self.trucks:
truck.packages = []
truck.solRoute = [Process(0, 0, 0, True), Process(0, 0, 0, False)]
truck.solRoute[-1].timeDone = 0
for i in range(len(self.chromosome)):
self.trucks[self.chromosome[i]-1].packages.append(self.packages[i])
# solve for route
def solve(self):
self.decode()
for truck in self.trucks:
for package in truck.packages:
truck.Insert(package)
# calc fitness
def calc_fitness(self):
self.solve()
fitnesses = [0 for i in range(len(self.trucks))]
for truck in self.trucks:
fitnesses[truck.ID-1] = truck.solRoute[-1].timeDone
return max(fitnesses), sum(fitnesses)
# normal crossover
def crossover(self, other):
mom_chromosome, dad_chromosome = random.sample( [self.chromosome, other.chromosome] , 2)
return mom_chromosome[:self.n//2] + dad_chromosome[self.n//2:]
#mutation
def mutation(self, rate):
if random.random() < rate:
choice = random.randint(0, self.n-1)
self.chromosome[choice] = random.randint(1, len(self.trucks))
class GA:
def __init__(self, trucks, packages, n, generations, mutation_rate):
self.trucks = trucks
self.packages = packages
#Populations contain n Individual
self.populations: list[Individual] = [Individual(trucks, packages) for _ in range(n)]
self.n = n
self.generations = generations
self.mutation_rate = mutation_rate
def solve(self):
self.calc_fitness()
# note that populations[-1] is the best individual
#initial best solution is populations[-1]
self.best_sol = self.populations[-1]
iteration = 0
max_iteration = 50
#run for ... generations
for generation in range(self.generations):
iteration += 1
Probs = self.calc_fitness()
if self.populations[-1].fitness < self.best_sol.fitness:
self.best_sol = self.populations[-1]
iteration = 0
# early stopping
if iteration > max_iteration:
break
new_gen = []
# create new popultions
for i in range(self.n):
# choose parent
parent: list[Individual] = self.natural_selection(Probs)
# cross over
child_chromosome = parent[0].crossover(parent[1])
child = Individual(self.trucks, self.packages, child_chromosome)
#mutation
child.mutation(self.mutation_rate)
#add new gen
new_gen.append(child)
self.populations = new_gen
#calc populations fitness
def calc_fitness(self):
for indiviudal in self.populations:
indiviudal.fitness = indiviudal.calc_fitness()
#sort in decreasing order of fitness
self.populations.sort(reverse= True, key=lambda x: x.fitness)
#rank selection
sp = 1.2
Probs = [1/self.n * (sp - (2*sp-2)*(i-1)/(self.n-1)) for i in range(1, self.n+1)]
Probs.reverse()
for i, individual in enumerate(self.populations):
individual.prob = Probs[i]
for i in range(1, len(Probs)):
Probs[i] += Probs[i-1]
return Probs
#choose 2 parents base on rank
def natural_selection(self, Probs):
parent = []
Probs = [0] + Probs
for i in range(2):
choice = random.uniform(0, Probs[-1])
for i in range(1, len(Probs)):
if Probs[i-1] <= choice <= Probs[i]:
parent.append(self.populations[i-1])
break
return parent
def print_sol(self):
print(len(self.trucks))
for truck in self.best_sol.trucks:
print(len(truck.solRoute))
for node in truck.solRoute:
print(node.ID, end = " ")
print()
def export_sol(self, file):
with open(file, 'w') as f:
f.write(f'{len(self.trucks)}\n')
for truck in self.best_sol.trucks:
f.write(f'{len(truck.solRoute)}\n')
for node in truck.solRoute:
f.write(f'{node.ID} ')
f.write("\n")
def main():
try:
trucks, packages = import_data('2/Users/pnt92/Downloads/code test/optimization/Project People and Parcel Share a Ride/test.txt')
except:
trucks, packages = import_data('/Users/pnt92/Downloads/code test/optimization/Project People and Parcel Share a Ride/test.txt')
sol = GA(trucks, packages, 100, 100, 0.1)
sol.solve()
sol.print_sol()
try:
sol.export_sol('2/Users/pnt92/Downloads/code test/optimization/Project People and Parcel Share a Ride/output.txt')
except:
sol.export_sol('/Users/pnt92/Downloads/code test/optimization/Project People and Parcel Share a Ride/output.txt')
if __name__ == "__main__":
main()