From f31c9a44ceff1d92968deaf5d998812b221e6aa5 Mon Sep 17 00:00:00 2001 From: Andrew Rollings Date: Mon, 17 Feb 2020 18:12:01 -0500 Subject: [PATCH] added continuous_peaks_generator.py Ported six peaks bug from GKHayes --- mlrose_hiive/__init__.py | 3 ++- mlrose_hiive/fitness/six_peaks.py | 9 ++++++--- mlrose_hiive/generators/__init__.py | 1 + .../generators/continuous_peaks_generator.py | 17 +++++++++++++++++ mlrose_hiive/generators/flip_flop_generator.py | 2 +- 5 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 mlrose_hiive/generators/continuous_peaks_generator.py diff --git a/mlrose_hiive/__init__.py b/mlrose_hiive/__init__.py index 9966c40f..106a536c 100644 --- a/mlrose_hiive/__init__.py +++ b/mlrose_hiive/__init__.py @@ -26,4 +26,5 @@ from .runners import GARunner, MIMICRunner, RHCRunner, SARunner, NNGSRunner from .runners import (build_data_filename) -from .generators import MaxKColorGenerator, QueensGenerator, FlipFlopGenerator, TSPGenerator, KnapsackGenerator +from .generators import (MaxKColorGenerator, QueensGenerator, FlipFlopGenerator, TSPGenerator, KnapsackGenerator, + ContinuousPeaksGenerator) diff --git a/mlrose_hiive/fitness/six_peaks.py b/mlrose_hiive/fitness/six_peaks.py index 5058b2e9..ac4dabdb 100644 --- a/mlrose_hiive/fitness/six_peaks.py +++ b/mlrose_hiive/fitness/six_peaks.py @@ -86,10 +86,13 @@ def evaluate(self, state): tail_1 = self.tail(1, state) # Calculate R(X, T) - if (tail_0 > _t and head_1 > _t) or (tail_1 > _t and head_0 > _t): + _r = 0 + _max_score = max(tail_0, head_1) + if tail_0 > _t and head_1 > _t: _r = _n - else: - _r = 0 + elif tail_1 > _t and head_0 > _t: + _r = _n + _max_score = max(tail_1, head_0) # Evaluate function fitness = max(tail_0, head_1) + _r diff --git a/mlrose_hiive/generators/__init__.py b/mlrose_hiive/generators/__init__.py index 42bec758..ea7eb10c 100644 --- a/mlrose_hiive/generators/__init__.py +++ b/mlrose_hiive/generators/__init__.py @@ -8,3 +8,4 @@ from .flip_flop_generator import FlipFlopGenerator from .queens_generator import QueensGenerator from .tsp_generator import TSPGenerator +from .continuous_peaks_generator import ContinuousPeaksGenerator diff --git a/mlrose_hiive/generators/continuous_peaks_generator.py b/mlrose_hiive/generators/continuous_peaks_generator.py new file mode 100644 index 00000000..b5d0e449 --- /dev/null +++ b/mlrose_hiive/generators/continuous_peaks_generator.py @@ -0,0 +1,17 @@ +""" Classes for defining optimization problem objects.""" + +# Author: Andrew Rollings +# License: BSD 3 clause + +import numpy as np + +from mlrose_hiive import DiscreteOpt, ContinuousPeaks + + +class ContinuousPeaksGenerator: + @staticmethod + def generate(seed, size=20, t_pct=0.1): + np.random.seed(seed) + fitness = ContinuousPeaks(t_pct=t_pct) + problem = DiscreteOpt(length=size, fitness_fn=fitness) + return problem \ No newline at end of file diff --git a/mlrose_hiive/generators/flip_flop_generator.py b/mlrose_hiive/generators/flip_flop_generator.py index 640b40f6..114bdb15 100644 --- a/mlrose_hiive/generators/flip_flop_generator.py +++ b/mlrose_hiive/generators/flip_flop_generator.py @@ -1,6 +1,6 @@ """ Classes for defining optimization problem objects.""" -# Author: Genevieve Hayes +# Author: Andrew Rollings # License: BSD 3 clause import numpy as np