Skip to content

Commit 595bf8c

Browse files
committed
generate neighbouring solutions
1 parent d9384f6 commit 595bf8c

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

app/solution.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import random
2-
from functools import reduce
2+
from datarepresentation.movie import Movie
33

44

55
class Solution:
@@ -13,12 +13,15 @@ def __str__(self):
1313
for s in self.solution:
1414
res += str(s.ids) + "->["
1515
for m in self.solution[s]:
16-
res += str(m)
16+
res += str(m) + " "
1717
res += "], "
1818
return res
1919

2020
def random_neighbour(self, radius):
2121

22+
if radius==0:
23+
return self
24+
2225
acceptable_ops = [(s, f)
2326
for s in self.data.servers
2427
for f in self.data.movies
@@ -27,7 +30,14 @@ def random_neighbour(self, radius):
2730
op = random.choice(acceptable_ops)
2831
s = op[0]
2932
f = op[1]
30-
return self.solution[s].remove(f) if f in self.solution[s] else self.solution[s].append(f)
33+
34+
res = Solution(self.data, self.solution)
35+
if f in self.solution[s]:
36+
res.solution[s].remove(f)
37+
else:
38+
res.solution[s].append(f)
39+
40+
return res.random_neighbour(radius-1)
3141

3242
def free_space(self, server):
3343
return 899 - sum([f.size for f in self.solution[server]])

0 commit comments

Comments
 (0)