-
Notifications
You must be signed in to change notification settings - Fork 0
/
jmoo_jmoea.py
executable file
·176 lines (137 loc) · 6.45 KB
/
jmoo_jmoea.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
##########################################################
### @Author Joe Krall ###############################
### @copyright see below ###############################
This file is part of JMOO,
Copyright Joe Krall, 2014.
JMOO is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
JMOO is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with JMOO. If not, see <http://www.gnu.org/licenses/>.
### ###############################
##########################################################
"""
"Brief notes"
"Standardized MOEA code for running any MOEA"
from jmoo_algorithms import *
from jmoo_stats_box import *
from jmoo_properties import *
from Algorithms.GALE.Fastmap.Moo import *
# from pylab import *
import jmoo_properties
import os, sys, inspect
def readpf(problem):
filename = "./Testing/PF/" + problem.name.split("_")[0] + "(" + str(len(problem.objectives)) + ")-PF.txt"
return [[float(num) for num in line.split()] for line in open(filename, "r").readlines()]
def read_file(problem, filename):
population = []
for line in open(filename, "r").readlines():
if line == "\n": continue
decision = 7
objectives = 3
line = [float(a) for a in line.split()]
population.append(jmoo_individual(problem, line[:decision], None))
return population
def jmoo_evo(problem, algorithm, configurations, toStop = bstop):
"""
----------------------------------------------------------------------------
Inputs:
-@problem: a MOP to optimize
-@algorithm: the MOEA used to optimize the problem
-@toStop: stopping criteria method
----------------------------------------------------------------------------
Summary:
- Evolve a population for a problem using some algorithm.
- Return the best generation of that evolution
----------------------------------------------------------------------------
Outputs:
- A record (statBox) of the best generation of evolution
----------------------------------------------------------------------------
"""
# # # # # # # # # # #
# 1) Initialization #
# # # # # # # # # # #
stoppingCriteria = False # Just a flag for stopping criteria
statBox = jmoo_stats_box(problem,algorithm) # Record keeping device
gen = 0 # Just a number to track generations
numeval = 0
values_to_be_passed = {}
# # # # # # # # # # # # # # # #
# 2) Load Initial Population #
# # # # # # # # # # # # # # # #
# Though this is not important I am sticking to NSGA3 paper
# if algorithm.name == "NSGA3":
# print "-"*20 + "boom"
# jmoo_properties.PSI = jmoo_properties.max_generation[problem.name]
# jmoo_properties.MU = population_size[problem.name.split("_")[-1]]
population = problem.loadInitialPopulation(configurations["Universal"]["Population_Size"])
assert(len(population) == configurations["Universal"]["Population_Size"]), "The population loaded from the file must be equal to MU"
from time import time
old = time()
# # # # # # # # # # # # # # # #
# 3.1) Special Initialization #
# # # # # # # # # # # # # # # #
if algorithm.initializer is not None:
# TODO:fix MOEAD
population, numeval = algorithm.initializer(problem, population, configurations, values_to_be_passed)
# # # # # # # # # # # # # # #
# 3) Collect Initial Stats #
# # # # # # # # # # # # # # #
statBox.update(population, 0, numeval, initial=True)
# # # # # # # # # # # # # # #
# 4) Generational Evolution #
# # # # # # # # # # # # # # #
while gen < configurations["Universal"]["No_of_Generations"] and stoppingCriteria is False:
gen+= 1
print gen, " | "
import sys
sys.stdout.flush()
# # # # # # # # #
# 4a) Selection #
# # # # # # # # #
problem.referencePoint = statBox.referencePoint
selectees, evals = algorithm.selector(problem, population, configurations, values_to_be_passed)
numNewEvals = evals
# # # # # # # # # #
# 4b) Adjustment #
# # # # # # # # # #
selectees, evals = algorithm.adjustor(problem, selectees, configurations, gen, population)
numNewEvals += evals
# # # # # # # # # # #
# 4c) Recombination #
# # # # # # # # # # #
population, evals = algorithm.recombiner(problem, population, selectees, configurations, gen)
numNewEvals += evals
# assert(len(population) == configurations["Universal"]["Population_Size"]), \
# "Length of the population should be equal to MU"
# # # # # # # # # # #
# 4d) Collect Stats #
# # # # # # # # # # #
# statBox.update(population, gen, numNewEvals)
# print "This code has been changed for gale no mutation please change"
if algorithm.name == "GALE_no_mutation":
statBox.update(population+selectees, gen, numNewEvals)
else:
statBox.update(population, gen, numNewEvals)
# from PerformanceMetrics.IGD.IGD_Calculation import IGD
# resulting_pf = [[float(f) for f in individual.fitness.fitness] for individual in statBox.box[-1].population]
# fitnesses = statBox.box[-1].fitnesses
# median_fitness = []
# for i in xrange(len(problem.objectives)):
# temp_fitness = [fit[i] for fit in fitnesses]
# median_fitness.append(median(temp_fitness))
# print IGD(resulting_pf, readpf(problem)), median_fitness
# # # # # # # # # # # # # # # # # #
# 4e) Evaluate Stopping Criteria #
# # # # # # # # # # # # # # # # # #
# stoppingCriteria = toStop(statBox)
stoppingCriteria = False
# assert(len(statBox.box[-1].population) == configurations["Universal"]["Population_Size"]), \
# "Length in the statBox should be equal to MU"
return statBox