-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjmoo_problem.py
executable file
·128 lines (103 loc) · 4.97 KB
/
jmoo_problem.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
"""
##########################################################
### @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"
"Representation of a Multi-Objective Problem."
from jmoo_individual import *
import random,csv
class jmoo_problem(object):
"a representation of a multi-objective problem"
def __init__(prob):
"jmoo problems are implemented via subclasses in jmoo_problems.py"
prob.name = ""
prob.decisions = []
prob.objectives = []
prob.numEvals = 0
prob.percentage=0
def generateInput(prob, center=False):
"a way to generate decisions for this problem"
while True: # repeat if we don't meet constraints
temp_value = []
if center is True:
for decision in prob.decisions:
temp_value.append(random.uniform(decision.low + (decision.up - decision.low)*0.2, decision.up - (decision.up - decision.low)*0.2 ))
else:
for decision in prob.decisions:
temp_value.append(random.uniform(decision.low, decision.up))
# if not prob.evalConstraints():
# break
if prob.validate(temp_value) is True: break
assert(prob.validate(temp_value) is True), "Something's wrong"
# print "Initial Population Generation Complete"
return temp_value
def generateExtreme(prob):
for decision in prob.decisions:
decision.value = decision.low
if prob.evalConstraints(): return prob.generateInput()
return [decision.value for decision in prob.decisions]
def loadInitialPopulation(problem, MU, path=""):
"a way to load *the* initial problem as used in jmoo_jmoea.py"
"this will load a csv as generated by the dataGen method of"
"jmoo_problems.py"
if path == "":
filename = "Data/" + problem.name + "-p" + str(MU) + "-d" + str(len(problem.decisions)) + "-o" + \
str(len(problem.objectives)) + "-dataset.txt"
elif path == "unittesting":
filename = "../../Data/Testing-dataset.txt"
else:
print "No accounted for"
exit()
input = open(filename, 'rb')
reader = csv.reader(input, delimiter=',')
population = []
#Use the csv file to build the initial population
for k,p in enumerate(reader):
if k > MU:
problem.objectives[k-MU-1].med = float(p[1])
lownotfound = False
upnotfound = False
if problem.objectives[k-MU-1].low == None:
problem.objectives[k-MU-1].low = float(p[0])
lownotfound = True
if problem.objectives[k-MU-1].up == None:
problem.objectives[k-MU-1].up = float(p[2])
upnotfound = True
elif k > 0:
population.append(jmoo_individual(problem,[float(p[n]) for n,dec in enumerate(problem.decisions)],None))
#population[-1].fitness = jmoo_fitness(problem, [float(p[n+len(problem.decisions)]) for n,obj in enumerate(problem.objectives)])
return population
def buildHeader(prob):
"a header used with rrsl in jmoo_algorithms.py"
header = ""
for decision in prob.decisions:
header += "$" + decision.name + ","
for objective in prob.objectives:
if objective.lismore:
header += ">>" + objective.name + ","
else:
header += "<<" + objective.name + ","
return header[:len(header)-1] # remove the last comma at the end
def validate(prob, decision_value):
assert(len(decision_value) == len(prob.decisions)), "Something is wrong with the usage of validate function"
for i, decision in enumerate(prob.decisions):
if decision.low <= decision_value[i] <= decision.up: pass
else:
print decision.low , decision_value[i] , decision.up
return False
return True