-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDescent.py
69 lines (59 loc) · 2.23 KB
/
Descent.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
from Method import Method
random.seed(17)
class BestImproventDescent(Method):
def __init__(self, solution):
super(BestImproventDescent, self).__init__(solution)
def run(self):
improved = True
it = 0
fo, route = self.solution.fo, self.solution.route
while improved:
best_i, best_j, fo_best_neighbour = self.best_neighbour()
if fo_best_neighbour < fo:
it+=1
route[best_i], route[best_j] = route[best_j], route[best_i] # Apply movement
self.fo = fo = fo_best_neighbour
else:
improved = False
self.solution.fo = fo # Update FO value on Solution object
return fo
class FirstImproventDescent(Method):
def __init__(self, solution):
super(FirstImproventDescent, self).__init__(solution)
def run(self):
improved = True
it = 0
fo, route = self.solution.fo, self.solution.route
while improved:
best_i, best_j, fo_best_neighbour = self.first_improvement_neighbour()
if fo_best_neighbour < fo:
it+=1
route[best_i], route[best_j] = route[best_j], route[best_i] # Apply movement
self.fo = fo = fo_best_neighbour
else:
improved = False
self.solution.fo = fo # Update FO value on Solution object
return fo
class RandomDescent(Method):
def __init__(self, solution):
super(RandomDescent, self).__init__(solution)
def run(self):
fo, route = self.solution.fo, self.solution.route
it, iter_max = 0, 100
while it < iter_max:
it += 1
improved = True
while improved:
new_i, new_j, fo_new_neighbour = self.random_neighbour()
if fo_new_neighbour < fo:
it+=1
route[new_i], route[new_j] = route[new_j], route[new_i] # Apply movement
self.fo = fo = fo_new_neighbour
else:
# it = 0
improved = False
self.solution.fo = fo # Update FO value on Solution object
return fo