Skip to content

Commit 59b656c

Browse files
committed
Merge branch 'main' of https://github.com/MDCHAMP/opt2 into main
2 parents d60a174 + 5df188b commit 59b656c

File tree

6 files changed

+21
-15
lines changed

6 files changed

+21
-15
lines changed

src/freelunch/base.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, obj=None, hypers={}, bounds=None):
2323
Unlikely to instance the base class but idk what else goes here
2424
'''
2525
self.hypers = dict(self.hyper_defaults, **hypers) # Hyperparamters/ methods (dictionary of )
26-
self.bounds = bounds # Bounds / constraints
26+
self.bounds = np.array(bounds) # Bounds / constraints
2727
self.nfe = 0
2828
self.obj = self.wrap_obj_with_nfe(obj) # Objective function
2929

@@ -86,8 +86,11 @@ def wrap_obj_with_nfe(self, obj):
8686
if obj is None: return None
8787
def w_obj(vec):
8888
self.nfe +=1
89-
# TODO: inf, nan and None handling
90-
return obj(vec)
89+
fit = obj(vec)
90+
try:
91+
return float(fit)
92+
except(ValueError, TypeError):
93+
return None
9194
return w_obj
9295

9396
# Subclasses for granularity

src/freelunch/darwin.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,9 @@ class binary_tournament(genetic_operation):
135135
def op(self, olds, news):
136136
out = np.empty_like(olds, dtype=object)
137137
for old, new, i in zip(olds, news, range(len(out))):
138-
if new.fitness < old.fitness:
138+
if new < old:
139139
out[i] = new
140140
new.on_win()
141-
elif old.fitness <= new.fitness:
142-
out[i] = old
143141
else:
144-
raise BadObjectiveFunctionScores(
145-
'Winner could not be determined by comparing objective scores. scores:{} and {}'.format(
146-
old.fitness, new.fitness
147-
))
142+
out[i] = old
148143
return out

src/freelunch/optimisers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def run(self):
185185
for i, o, n in zip(range(self.hypers['N']), old, new):
186186
if self.P(o.fitness, n.fitness, T) >= np.random.uniform(0,1):
187187
old[i] = new[i]
188-
if n.fitness < best.fitness:
188+
if n < best:
189189
best = n
190190
return np.array([best])
191191

src/freelunch/tech.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ def uniform_continuous_init(bounds, N, creature=animal):
3737
def compute_obj(pop, obj):
3838
for sol in pop:
3939
sol.fitness = obj(sol.dna)
40-
if np.isnan(sol.fitness):
41-
sol.fitness = None
4240
return pop
4341

4442

src/freelunch/zoo.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,18 @@ def on_win(self):
3737
t.win()
3838
self.tech = []
3939

40-
40+
def __lt__(self, other):
41+
'''overlaod the < operator for convenient handling of tournament selction in the presence onf nonetype fitnesses'''
42+
if self.fitness is None:
43+
if other.fitness is None:
44+
# ? what to do here
45+
return False
46+
return False # Other has lower fitness
47+
elif other.fitness is None:
48+
return True # We have the lower fitness
49+
else:
50+
return self.fitness < other.fitness
51+
4152
# %% All that the light touches is our domain
4253

4354

tests/test_opt.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def test_base_optimiser():
2727
opt.obj = lambda x:x
2828
opt(nruns=2)
2929

30-
@pytest.mark.skip(reason="Objective sanitation needs propper handling")
3130
def test_naughty_objective():
3231
out = DE(obj=naughty_objective, bounds=[[-1, 1]])(nruns=1, full_output=True)
3332

0 commit comments

Comments
 (0)