-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExampleSolution.py
More file actions
144 lines (126 loc) · 4.45 KB
/
ExampleSolution.py
File metadata and controls
144 lines (126 loc) · 4.45 KB
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
#
# * This class extends the abstract class Solution to define a solution for the
# * problem domain.
# *
# * Nelishia Pillay
# *
# * 8 October 2016
#
from random import Random
from typing import List
from GeneticProgram.Evaluator import Evaluator
from GeneticProgram.Node import Node
from GeneticProgram.Solution import Solution
from GeneticProgram.examples.Entity import Entity
import sys
class ExampleSolution(Solution):
# * Data elements
#
#
# * Stores the entities.
#
entities: List[Entity]
#
# * Stores the fitness value to be used for the initial solution created.
#
fitness: float
#
# * Stores the evolved heuristic.
#
heuristic: Node
#
# * Stores the initial solution created using the heuristic. In this problem
# * this is stored as an array of strings just as an example. However, the
# * solution can be of any type, e.g. for the traveling salesman problem it
# * could be a string representing the tour.
#
solution: List[Entity]
# It may be necessary to store other values that are specific to problem being
# solved that is different from the fitness or needed to calculate the fitness.
# For example, for the examination timetabling problem the hard and soft
# constraint cost also needs to be stored.
#
# * Stores the attributes. Each character in the string represents a different
# * attribute.
#
attributes: str
def set_heuristic(self, heuristic):
#
# * Sets the evolved heuristic.
#
self.heuristic = heuristic
def get_heuristic(self):
#
# * Returns the evolved heuristic.
#
return self.heuristic
def get_solution(self) -> List[Entity]:
#
# * Returns the solution created using the evolved heuristic.
#
return self.solution
def get_fitness(self):
#
# * Returns the fitness calculated using the solution constructed using the
# * evolved heuristic.
#
return self.fitness
def fitter(self, other):
#
# * This method is used to compare two solutions to determine which of
# * the two is fitter.
#
if other.get_fitness() < self.fitness:
return -1
elif other.get_fitness() > self.fitness:
return 1
else:
return 0
# OTHER METHODS
def __init__(self):
super(ExampleSolution, self).__init__()
self.solution = []
# Initialize the problem domain
self.initialize_entities()
def initialize_entities(self):
#
# * Initializes the entities to illustrate how a solution can be created
# * used the evolved heuristic. Creates and stores three entities. The
# * attribute values for the entities are randomly selected.
#
# Initialize random number generator.
random_generator = Random()
self.entities = []
# Loop to create entities.
for i in range(3):
attribs = [random_generator.randrange(10) + 1 for _ in range(3)]
new_entity = Entity()
new_entity.set_attributes(attribs)
self.entities.append(new_entity)
def calculate_heuristics(self):
for entity in self.entities:
if entity.get_heuristic() != sys.float_info.max:
evaluator = Evaluator(self.attributes, entity.get_attributes())
entity.set_heuristic(evaluator.eval(self.heuristic))
def create_solution(self, attributes):
self.attributes = attributes
self.calculate_heuristics()
self.entities.sort()
while self.entities[0].get_heuristic() != sys.float_info.max:
if self.solution is None:
self.solution = []
self.solution.append(self.entities[0])
self.update_entities()
self.calculate_heuristics()
self.entities.sort()
entity = self.solution[0]
a = entity.get_attributes()
self.fitness = a[0] + a[1] + a[2]
def update_entities(self):
self.entities[0].set_heuristic(sys.float_info.max)
count = 1
while count < len(self.entities):
attribs = self.entities[count].get_attributes()
attribs[1] -= 1
self.entities[count].set_attributes(attribs)
count += 1