Skip to content

Commit

Permalink
Fixed issue with storage of fevals in curve data.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiive committed Mar 10, 2021
1 parent 106dfc1 commit a9d0b0f
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion mlrose_hiive/algorithms/ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def genetic_alg(problem, pop_size=200, pop_breed_percent=0.75, elite_dreg_ratio=
attempts += 1

if curve:
fitness_curve.append(problem.get_adjusted_fitness())
fitness_curve.append((problem.get_adjusted_fitness(), problem.fitness_evaluations))

# invoke callback
if state_fitness_callback is not None:
Expand Down
2 changes: 1 addition & 1 deletion mlrose_hiive/algorithms/gd.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def gradient_descent(problem, max_attempts=10, max_iters=np.inf,
attempts += 1

if curve:
fitness_curve.append(problem.get_adjusted_fitness())
fitness_curve.append((problem.get_adjusted_fitness(), problem.fitness_evaluations))

# invoke callback
if state_fitness_callback is not None:
Expand Down
2 changes: 1 addition & 1 deletion mlrose_hiive/algorithms/hc.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def hill_climb(problem, max_iters=np.inf, restarts=0, init_state=None,
next_fitness = problem.eval_fitness(next_state)

if curve:
fitness_curve.append(problem.get_adjusted_fitness())
fitness_curve.append((problem.get_adjusted_fitness(), problem.fitness_evaluations))

# invoke callback
if state_fitness_callback is not None:
Expand Down
2 changes: 1 addition & 1 deletion mlrose_hiive/algorithms/mimic.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def mimic(problem, pop_size=200, keep_pct=0.2, max_attempts=10,
attempts += 1

if curve:
fitness_curve.append(problem.get_adjusted_fitness())
fitness_curve.append((problem.get_adjusted_fitness(), problem.fitness_evaluations))

# invoke callback
if state_fitness_callback is not None:
Expand Down
13 changes: 10 additions & 3 deletions mlrose_hiive/algorithms/rhc.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,15 @@ def random_hill_climb(problem, max_attempts=10, max_iters=np.inf, restarts=0,
all_curves = []

continue_iterating = True
# problem.reset()
for current_restart in range(restarts + 1):
# Initialize optimization problem and attempts counter
fevals = problem.fitness_evaluations
if init_state is None:
problem.reset()
else:
problem.set_state(init_state)
problem.fitness_evaluations = fevals

fitness_curve = []
callback_extra_data = None
Expand Down Expand Up @@ -128,8 +131,13 @@ def random_hill_climb(problem, max_attempts=10, max_iters=np.inf, restarts=0,

if curve:
adjusted_fitness = problem.get_adjusted_fitness()
fitness_curve.append(adjusted_fitness)
all_curves.append({'current_restart': current_restart, 'Fitness': problem.get_adjusted_fitness()})
curve_value = (adjusted_fitness, problem.fitness_evaluations)
fitness_curve.append(curve_value)
all_curves.append(curve_value)
"""
all_curves.append({'current_restart': current_restart, 'Fitness': problem.get_adjusted_fitness(),
'FEval': problem.fitness_evaluations})
"""

# invoke callback
if state_fitness_callback is not None:
Expand Down Expand Up @@ -159,5 +167,4 @@ def random_hill_climb(problem, max_attempts=10, max_iters=np.inf, restarts=0,
break
best_fitness *= problem.get_maximize()


return best_state, best_fitness, np.asarray(best_fitness_curve) if curve else None
2 changes: 1 addition & 1 deletion mlrose_hiive/algorithms/sa.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def simulated_annealing(problem, schedule=GeomDecay(), max_attempts=10,
attempts += 1

if curve:
fitness_curve.append(problem.get_adjusted_fitness())
fitness_curve.append((problem.get_adjusted_fitness(), problem.fitness_evaluations))

# invoke callback
if state_fitness_callback is not None:
Expand Down
10 changes: 6 additions & 4 deletions mlrose_hiive/runners/_runner_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,12 @@ def _start_run_timing(self):

@staticmethod
def _create_curve_stat(iteration, curve_value, curve_data, t=None):
curve_fitness_value, curve_feval_value = curve_value
curve_stat = {
'Iteration': iteration,
'Time': t,
'Fitness': curve_value
'Fitness': curve_fitness_value,
'FEval': curve_feval_value
}

curve_stat.update(curve_data)
Expand Down Expand Up @@ -310,8 +312,6 @@ def _save_state(self, iteration, state, fitness, user_data,
current_iteration_stats.update({str(gd(k)): self._sanitize_value(v)
for k, v in {k: v for (k, v) in user_data}.items()})

if fitness_evaluations is not None:
current_iteration_stats['FEvals'] = fitness_evaluations

# check for additional info
gi = lambda k, v: {} if not hasattr(v, 'get_info__') else v.get_info__(t)
Expand All @@ -328,6 +328,7 @@ def _save_state(self, iteration, state, fitness, user_data,
run_stat = {
'Iteration': i,
'Fitness': fitness,
'FEvals': fitness_evaluations,
'Time': t,
'State': self._sanitize_value(state)
}
Expand All @@ -338,7 +339,7 @@ def _save_state(self, iteration, state, fitness, user_data,
if self.generate_curves and iteration == 0:
# capture first fitness value for iteration 0 if not already captured.
if curve is None or len(curve) == 0:
curve = [fitness]
curve = [(fitness, fitness_evaluations)]
self._first_curve_synthesized = True

if self.generate_curves and curve is not None: # and (done or iteration == max(self.iteration_list)):
Expand All @@ -364,6 +365,7 @@ def _save_state(self, iteration, state, fitness, user_data,

if self._copy_zero_curve_fitness_from_first and len(self._fitness_curves) > 1:
self._fitness_curves[0]['Fitness'] = self._fitness_curves[1]['Fitness']
# self._fitness_curves[0]['FEval'] = 0
self._copy_zero_curve_fitness_from_first = False
self._create_and_save_run_data_frames()

Expand Down
5 changes: 3 additions & 2 deletions problem_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@
{
"cell_type": "markdown",
"source": [
"The best run using these criteria is as follows:"
"The best runs using these criteria is as follows:"
],
"metadata": {
"collapsed": false
Expand Down Expand Up @@ -687,7 +687,8 @@
{
"cell_type": "markdown",
"source": [
"Which has the following identifying state information:"
"We will arbitrarily pick the first row for this example,\n",
"which has the following identifying state information:"
],
"metadata": {
"collapsed": false
Expand Down

0 comments on commit a9d0b0f

Please sign in to comment.