-
Notifications
You must be signed in to change notification settings - Fork 4
/
Decision.py
106 lines (78 loc) · 2.69 KB
/
Decision.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
import abc
from . import DefaultParameters as dp
class Decision(abc.ABC):
@abc.abstractmethod
def makeDecision(self, components):
pass
@abc.abstractmethod
def getAlpha(self):
pass
@abc.abstractmethod
def getBeta(self):
pass
@abc.abstractmethod
def getPheromoneValue(self, component):
pass
@abc.abstractmethod
def getHeuristicValue(self, component):
pass
@abc.abstractmethod
def getComponentCost(self, component):
pass
@abc.abstractmethod
def getSolutionComponents(self):
pass
class DecisionComponentRecording(Decision):
def __init__(self, **kwargs):
self.components = set()
super().__init__(**kwargs)
def addSolutionComponent(self, component):
self.components.add(component)
def getSolutionComponents(self):
return (c for c in self.components)
def probabilisticDecision(self, components):
total = sum(self.getNumerator(c) for c in components)
import random
r = random.random()*total
s = 0
for c in components:
s += self.getNumerator(c)
if s > r:
return c
def getHeuristicValue(self, component):
return 1/self.getComponentCost(component)
def getNumerator(self, component):
return self.getPheromoneValue(component)**self.getAlpha() * self.getHeuristicValue(component)**self.getBeta()
class AS_Decision(DecisionComponentRecording):
def makeDecision(self, components):
component = self.probabilisticDecision(components)
self.addSolutionComponent(component)
return component
MMAS_Decision = AS_Decision
class ACS_Decision(DecisionComponentRecording):
def deterministicFactor(self, component):
return self.getPheromoneValue(component)*self.getHeuristicValue(component)**self.getBeta()
def deterministicDecision(self, components):
return max((c for c in components), key = lambda c: self.deterministicFactor(c))
def makeDecision(self, components):
import random
r = random.random()
if r <= self.getq0():
component = self.deterministicDecision(components)
else:
component = self.probabilisticDecision(components)
self.addSolutionComponent(component)
tau = self.getPheromoneValue(component)
tau0 = self.getTau0()
phi = self.getPhi()
self.setPheromoneValue(component, (1-phi)*tau + phi*tau0)
return component
@abc.abstractmethod
def setPheromoneValue(self, component):
pass
@abc.abstractmethod
def getq0(self):
pass
@abc.abstractmethod
def getPhi(self):
pass