Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/MDCHAMP/opt2 into main
Browse files Browse the repository at this point in the history
  • Loading branch information
MDCHAMP committed Oct 26, 2021
2 parents d60a174 + 5df188b commit 59b656c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
9 changes: 6 additions & 3 deletions src/freelunch/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, obj=None, hypers={}, bounds=None):
Unlikely to instance the base class but idk what else goes here
'''
self.hypers = dict(self.hyper_defaults, **hypers) # Hyperparamters/ methods (dictionary of )
self.bounds = bounds # Bounds / constraints
self.bounds = np.array(bounds) # Bounds / constraints
self.nfe = 0
self.obj = self.wrap_obj_with_nfe(obj) # Objective function

Expand Down Expand Up @@ -86,8 +86,11 @@ def wrap_obj_with_nfe(self, obj):
if obj is None: return None
def w_obj(vec):
self.nfe +=1
# TODO: inf, nan and None handling
return obj(vec)
fit = obj(vec)
try:
return float(fit)
except(ValueError, TypeError):
return None
return w_obj

# Subclasses for granularity
Expand Down
9 changes: 2 additions & 7 deletions src/freelunch/darwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,9 @@ class binary_tournament(genetic_operation):
def op(self, olds, news):
out = np.empty_like(olds, dtype=object)
for old, new, i in zip(olds, news, range(len(out))):
if new.fitness < old.fitness:
if new < old:
out[i] = new
new.on_win()
elif old.fitness <= new.fitness:
out[i] = old
else:
raise BadObjectiveFunctionScores(
'Winner could not be determined by comparing objective scores. scores:{} and {}'.format(
old.fitness, new.fitness
))
out[i] = old
return out
2 changes: 1 addition & 1 deletion src/freelunch/optimisers.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def run(self):
for i, o, n in zip(range(self.hypers['N']), old, new):
if self.P(o.fitness, n.fitness, T) >= np.random.uniform(0,1):
old[i] = new[i]
if n.fitness < best.fitness:
if n < best:
best = n
return np.array([best])

Expand Down
2 changes: 0 additions & 2 deletions src/freelunch/tech.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ def uniform_continuous_init(bounds, N, creature=animal):
def compute_obj(pop, obj):
for sol in pop:
sol.fitness = obj(sol.dna)
if np.isnan(sol.fitness):
sol.fitness = None
return pop


Expand Down
13 changes: 12 additions & 1 deletion src/freelunch/zoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,18 @@ def on_win(self):
t.win()
self.tech = []


def __lt__(self, other):
'''overlaod the < operator for convenient handling of tournament selction in the presence onf nonetype fitnesses'''
if self.fitness is None:
if other.fitness is None:
# ? what to do here
return False
return False # Other has lower fitness
elif other.fitness is None:
return True # We have the lower fitness
else:
return self.fitness < other.fitness

# %% All that the light touches is our domain


Expand Down
1 change: 0 additions & 1 deletion tests/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def test_base_optimiser():
opt.obj = lambda x:x
opt(nruns=2)

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

Expand Down

0 comments on commit 59b656c

Please sign in to comment.