-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExampleSolution.py
More file actions
69 lines (58 loc) · 2.73 KB
/
ExampleSolution.py
File metadata and controls
69 lines (58 loc) · 2.73 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
#
# * This class implements the Solution abstract class and is used to store
# * details of the initial solution.
# *
# * Nelishia Pillay
# *
# * 30 August 2016
#
from typing import List
from GeneticAlgorithm.Solution import Solution
class ExampleSolution(Solution):
# Data elements
# Stores the heuristic combination that will be used to create an initial
# solution.
heuristic_combination: str = ''
# Stores the fitness value to be used for the initial solution created.
fitness: float
# 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 travelling salesman problem it
# could be a string representing the tour.
initial_solution: List[str] = []
# 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.
# Implementation of abstract methods needed to extend Solution
def get_fitness(self) -> float:
return self.fitness
def set_heuristic_combination(self, heuristic_combination: str):
# Implements the abstract method to store the heuristic combination used to
# create an initial solution.
self.heuristic_combination = heuristic_combination
def get_heuristic_combination(self) -> str:
# Implements the abstract method to return the heuristic combination used to
# create the solution.
return self.heuristic_combination
def get_solution(self) -> List[str]:
# Implements the abstract method to return a solution.
return self.initial_solution
def fitter(self, other: Solution):
# This method is used to compare two initial 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
# Methods in addition to the abstract methods that need to be implemented.
def create_solution(self):
# This method creates a solution using the heuristic combination.
# Construct a solution to the problem using the heuristic combination.
temp = ["This", " is", " a", " solution", " created", " using ", self.heuristic_combination]
self.initial_solution = temp
# Calculate the fitness of the constructed solution. This is just an example
# so simply adds the length of the solution to a random double.
self.fitness = len(temp)