Skip to content

Commit 33d4235

Browse files
committed
iterations, choosing elite and good solutions
1 parent 88cc4a3 commit 33d4235

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

app/main.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ def print_solution_pool(pool):
2626

2727

2828
def best_in_neighbourhood(solution, radius, number_of_bees):
29-
neighbours = [solution.random_neighbour(radius) for _ in range(number_of_bees)]
29+
neighbours = []
30+
for _ in range(number_of_bees):
31+
neighbours.append(solution.random_neighbour(radius))
3032
best_neighbour = min(neighbours, key=lambda s: s.evaluate())
3133
return best_neighbour if best_neighbour.evaluate() < solution.evaluate() else solution
3234

3335

3436
def main():
3537

36-
n = 10
38+
n = 5
3739
m = 3
3840
e = 1
3941
nep = 4
@@ -46,9 +48,7 @@ def main():
4648

4749
# bee = Solution(data, {data.servers[0]: [data.movies[0]], data.servers[1]: [data.movies[1]]})
4850
# print(bee)
49-
# print(bee.evaluate())
5051
# print(bee.random_neighbour(1))
51-
# print(bee.random_neighbour(1).evaluate())
5252

5353
pool = [random_solution(data) for _ in range(n)]
5454

@@ -71,8 +71,6 @@ def main():
7171
for _ in range(n-m):
7272
pool.append(random_solution(data))
7373

74-
# print_solution_pool(pool)
75-
7674

7775
if __name__ == '__main__':
7876
main()

app/solution.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,28 @@ def __str__(self):
1717
res += str(self.evaluate())
1818
return res
1919

20+
def __copy__(self):
21+
res = Solution(self.data, dict(self.solution))
22+
for l in res.solution:
23+
res.solution[l] = list(res.solution[l])
24+
return res
25+
2026
def random_neighbour(self, radius):
2127

28+
res = self.__copy__()
2229
if radius == 0:
23-
return self
30+
return res
2431

2532
acceptable_ops = [(s, f)
26-
for s in self.data.servers
27-
for f in self.data.movies
28-
if f in self.solution[s] or f.size <= self.free_space(s)]
33+
for s in res.data.servers
34+
for f in res.data.movies
35+
if f in res.solution[s] or f.size <= res.free_space(s)]
2936

3037
op = random.choice(acceptable_ops)
3138
s = op[0]
3239
f = op[1]
3340

34-
res = Solution(self.data, self.solution)
35-
if f in self.solution[s]:
41+
if f in res.solution[s]:
3642
res.solution[s].remove(f)
3743
else:
3844
res.solution[s].append(f)

0 commit comments

Comments
 (0)