From 95094e66a5ed8b358c04d0664fecb952f621bf16 Mon Sep 17 00:00:00 2001 From: Chris Granade Date: Wed, 19 Apr 2017 18:14:21 +1000 Subject: [PATCH 1/4] =?UTF-8?q?Fixed=20import,=20tabs=20=E2=86=92=20spaces?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/qinfer/tests/test_optimiser.py | 300 +++++++++++++++-------------- 1 file changed, 151 insertions(+), 149 deletions(-) diff --git a/src/qinfer/tests/test_optimiser.py b/src/qinfer/tests/test_optimiser.py index e773796..c0adc8d 100644 --- a/src/qinfer/tests/test_optimiser.py +++ b/src/qinfer/tests/test_optimiser.py @@ -30,178 +30,180 @@ ## IMPORTS #################################################################### -import qinfer.rb as rb -import qinfer.distributions as dist +from functools import partial import numpy as np import random as rnd -from functools import partial +import qinfer.rb as rb +import qinfer.distributions as dist + +from qinfer.tests.base_test import DerandomizedTestCase ## CLASSES #################################################################### class TestPSO(DerandomizedTestCase): - def test_pso_quad(self): - f_quad = lambda x: numpy.sum(10 * (x-0.5)**2) - hh_opt = ParticleSwarmOptimizer(['x','y','z','a'], fitness_function = f_quad) - hh_opt() - - def test_pso_sin_sq(self): - f_sin_sq = lambda x: numpy.sum(np.sin(x - 0.2)**2) - hh_opt = ParticleSwarmOptimizer(['x','y','z','a'], fitness_function = f_sin_sq) - hh_opt() - - def test_pso_rosenbrock(self): - f_rosenbrock = lambda x: numpy.sum([((x[i+1] - x[i]**2)**2 + (1 - x[i])**2)/len(x) for i in range(len(x)-1)]) - hh_opt = ParticleSwarmOptimizer(['x','y','z','a'], fitness_function = f_rosenbrock) - hh_opt() - - - def test_pso_perf_test_multiple_short(self): - # Define our experiment - n_trials = 20 # Times we repeat the set of experiments - n_exp = 100 # Number of experiments in the set - n_particles = 4000 # Number of points we track during the experiment - - # Model for the experiment - model = rb.RandomizedBenchmarkingModel() - - #Ordering of RB is 'p', 'A', 'B' - # A + B < 1, 0 < p < 1 - #Prior distribution of the experiment - prior = dist.PostselectedDistribution( - dist.MultivariateNormalDistribution(mean=[0.5,0.1,0.25], cov=np.diag([0.1, 0.1, 0.1])), - model - ) - - #Heuristic used in the experiment - heuristic_class = qi.expdesign.ExpSparseHeuristic - - #Heuristic Parameters - params = ['base', 'scale'] - - #Fitness function to evaluate the performance of the experiment - EXPERIMENT_FITNESS = lambda performance: performance['loss'][:,-1].mean(axis=0) - - hh_opt = ParticleSwarmOptimizer(params, - n_trials = n_trials, - n_particles = n_particles, - prior = prior, - model = model, - n_exp = n_exp, - heuristic_class = heuristic_class - ) - hh_opt(n_pso_iterations=5, - n_pso_particles=6) + def test_pso_quad(self): + f_quad = lambda x: numpy.sum(10 * (x-0.5)**2) + hh_opt = ParticleSwarmOptimizer(['x','y','z','a'], fitness_function = f_quad) + hh_opt() + + def test_pso_sin_sq(self): + f_sin_sq = lambda x: numpy.sum(np.sin(x - 0.2)**2) + hh_opt = ParticleSwarmOptimizer(['x','y','z','a'], fitness_function = f_sin_sq) + hh_opt() + + def test_pso_rosenbrock(self): + f_rosenbrock = lambda x: numpy.sum([((x[i+1] - x[i]**2)**2 + (1 - x[i])**2)/len(x) for i in range(len(x)-1)]) + hh_opt = ParticleSwarmOptimizer(['x','y','z','a'], fitness_function = f_rosenbrock) + hh_opt() + + + def test_pso_perf_test_multiple_short(self): + # Define our experiment + n_trials = 20 # Times we repeat the set of experiments + n_exp = 100 # Number of experiments in the set + n_particles = 4000 # Number of points we track during the experiment + + # Model for the experiment + model = rb.RandomizedBenchmarkingModel() + + #Ordering of RB is 'p', 'A', 'B' + # A + B < 1, 0 < p < 1 + #Prior distribution of the experiment + prior = dist.PostselectedDistribution( + dist.MultivariateNormalDistribution(mean=[0.5,0.1,0.25], cov=np.diag([0.1, 0.1, 0.1])), + model + ) + + #Heuristic used in the experiment + heuristic_class = qi.expdesign.ExpSparseHeuristic + + #Heuristic Parameters + params = ['base', 'scale'] + + #Fitness function to evaluate the performance of the experiment + EXPERIMENT_FITNESS = lambda performance: performance['loss'][:,-1].mean(axis=0) + + hh_opt = ParticleSwarmOptimizer(params, + n_trials = n_trials, + n_particles = n_particles, + prior = prior, + model = model, + n_exp = n_exp, + heuristic_class = heuristic_class + ) + hh_opt(n_pso_iterations=5, + n_pso_particles=6) def TestPSSAO(DerandomizedTestCase): - def test_pssao_quad(self): - f_quad = lambda x: numpy.sum(10 * (x-0.5)**2) - hh_opt = ParticleSwarmSimpleAnnealingOptimizer(['x','y','z','a'], fitness_function = f_quad) - hh_opt() + def test_pssao_quad(self): + f_quad = lambda x: numpy.sum(10 * (x-0.5)**2) + hh_opt = ParticleSwarmSimpleAnnealingOptimizer(['x','y','z','a'], fitness_function = f_quad) + hh_opt() - def test_pssao_sin_sq(self): - f_sin_sq = lambda x: numpy.sum(np.sin(x - 0.2)**2) - hh_opt = ParticleSwarmSimpleAnnealingOptimizer(['x','y','z','a'], fitness_function = f_sin_sq) - hh_opt() + def test_pssao_sin_sq(self): + f_sin_sq = lambda x: numpy.sum(np.sin(x - 0.2)**2) + hh_opt = ParticleSwarmSimpleAnnealingOptimizer(['x','y','z','a'], fitness_function = f_sin_sq) + hh_opt() - def test_pssao_rosenbrock(self): - f_rosenbrock = lambda x: numpy.sum([((x[i+1] - x[i]**2)**2 + (1 - x[i])**2)/len(x) for i in range(len(x)-1)]) - hh_opt = ParticleSwarmSimpleAnnealingOptimizer(['x','y','z','a'], fitness_function = f_rosenbrock) - hh_opt() + def test_pssao_rosenbrock(self): + f_rosenbrock = lambda x: numpy.sum([((x[i+1] - x[i]**2)**2 + (1 - x[i])**2)/len(x) for i in range(len(x)-1)]) + hh_opt = ParticleSwarmSimpleAnnealingOptimizer(['x','y','z','a'], fitness_function = f_rosenbrock) + hh_opt() - def test_pssao_perf_test_multiple_short(self): - # Define our experiment - n_trials = 20 # Times we repeat the set of experiments - n_exp = 150 # Number of experiments in the set - n_particles = 4000 # Number of points we track during the experiment + def test_pssao_perf_test_multiple_short(self): + # Define our experiment + n_trials = 20 # Times we repeat the set of experiments + n_exp = 150 # Number of experiments in the set + n_particles = 4000 # Number of points we track during the experiment - # Model for the experiment - model = rb.RandomizedBenchmarkingModel() + # Model for the experiment + model = rb.RandomizedBenchmarkingModel() - #Ordering of RB is 'p', 'A', 'B' - # A + B < 1, 0 < p < 1 - #Prior distribution of the experiment - prior = dist.PostselectedDistribution( - dist.MultivariateNormalDistribution(mean=[0.5,0.1,0.25], cov=np.diag([0.1, 0.1, 0.1])), - model - ) + #Ordering of RB is 'p', 'A', 'B' + # A + B < 1, 0 < p < 1 + #Prior distribution of the experiment + prior = dist.PostselectedDistribution( + dist.MultivariateNormalDistribution(mean=[0.5,0.1,0.25], cov=np.diag([0.1, 0.1, 0.1])), + model + ) - #Heuristic used in the experiment - heuristic_class = qi.expdesign.ExpSparseHeuristic + #Heuristic used in the experiment + heuristic_class = qi.expdesign.ExpSparseHeuristic - #Heuristic Parameters - params = ['base', 'scale'] + #Heuristic Parameters + params = ['base', 'scale'] - #Fitness function to evaluate the performance of the experiment - EXPERIMENT_FITNESS = lambda performance: performance['loss'][:,-1].mean(axis=0) + #Fitness function to evaluate the performance of the experiment + EXPERIMENT_FITNESS = lambda performance: performance['loss'][:,-1].mean(axis=0) - hh_opt = ParticleSwarmSimpleAnnealingOptimizer(params, - n_trials = n_trials, - n_particles = n_particles, - prior = prior, - model = model, - n_exp = n_exp, - heuristic_class = heuristic_class - ) - hh_opt(n_pso_iterations=5, - n_pso_particles=6) + hh_opt = ParticleSwarmSimpleAnnealingOptimizer(params, + n_trials = n_trials, + n_particles = n_particles, + prior = prior, + model = model, + n_exp = n_exp, + heuristic_class = heuristic_class + ) + hh_opt(n_pso_iterations=5, + n_pso_particles=6) def TestPSTO(DerandomizedTestCase): - def test_psto_quad(self): - f_quad = lambda x: numpy.sum(10 * (x-0.5)**2) - hh_opt = ParticleSwarmTemperingOptimizer(['x','y','z','a'], fitness_function = f_quad) - hh_opt() - - def test_psto_sin_sq(self): - f_sin_sq = lambda x: numpy.sum(np.sin(x - 0.2)**2) - hh_opt = ParticleSwarmTemperingOptimizer(['x','y','z','a'], fitness_function = f_sin_sq) - hh_opt() - - def test_psto_rosenbrock(self): - f_rosenbrock = lambda x: numpy.sum([((x[i+1] - x[i]**2)**2 + (1 - x[i])**2)/len(x) for i in range(len(x)-1)]) - hh_opt = ParticleSwarmTemperingOptimizer(['x','y','z','a'], fitness_function = f_rosenbrock) - hh_opt() - - - def test_psto_perf_test_multiple_short(self): - # Define our experiment - n_trials = 20 # Times we repeat the set of experiments - n_exp = 150 # Number of experiments in the set - n_particles = 4000 # Number of points we track during the experiment - - # Model for the experiment - model = rb.RandomizedBenchmarkingModel() - - #Ordering of RB is 'p', 'A', 'B' - # A + B < 1, 0 < p < 1 - #Prior distribution of the experiment - prior = dist.PostselectedDistribution( - dist.MultivariateNormalDistribution(mean=[0.5,0.1,0.25], cov=np.diag([0.1, 0.1, 0.1])), - model - ) - - #Heuristic used in the experiment - heuristic_class = qi.expdesign.ExpSparseHeuristic - - #Heuristic Parameters - params = ['base', 'scale'] - - #Fitness function to evaluate the performance of the experiment - EXPERIMENT_FITNESS = lambda performance: performance['loss'][:,-1].mean(axis=0) - - hh_opt = ParticleSwarmTemperingOptimizer(params, - n_trials = n_trials, - n_particles = n_particles, - prior = prior, - model = model, - n_exp = n_exp, - heuristic_class = heuristic_class - ) - hh_opt(n_pso_iterations=5, - n_pso_particles=6) + def test_psto_quad(self): + f_quad = lambda x: numpy.sum(10 * (x-0.5)**2) + hh_opt = ParticleSwarmTemperingOptimizer(['x','y','z','a'], fitness_function = f_quad) + hh_opt() + + def test_psto_sin_sq(self): + f_sin_sq = lambda x: numpy.sum(np.sin(x - 0.2)**2) + hh_opt = ParticleSwarmTemperingOptimizer(['x','y','z','a'], fitness_function = f_sin_sq) + hh_opt() + + def test_psto_rosenbrock(self): + f_rosenbrock = lambda x: numpy.sum([((x[i+1] - x[i]**2)**2 + (1 - x[i])**2)/len(x) for i in range(len(x)-1)]) + hh_opt = ParticleSwarmTemperingOptimizer(['x','y','z','a'], fitness_function = f_rosenbrock) + hh_opt() + + + def test_psto_perf_test_multiple_short(self): + # Define our experiment + n_trials = 20 # Times we repeat the set of experiments + n_exp = 150 # Number of experiments in the set + n_particles = 4000 # Number of points we track during the experiment + + # Model for the experiment + model = rb.RandomizedBenchmarkingModel() + + #Ordering of RB is 'p', 'A', 'B' + # A + B < 1, 0 < p < 1 + #Prior distribution of the experiment + prior = dist.PostselectedDistribution( + dist.MultivariateNormalDistribution(mean=[0.5,0.1,0.25], cov=np.diag([0.1, 0.1, 0.1])), + model + ) + + #Heuristic used in the experiment + heuristic_class = qi.expdesign.ExpSparseHeuristic + + #Heuristic Parameters + params = ['base', 'scale'] + + #Fitness function to evaluate the performance of the experiment + EXPERIMENT_FITNESS = lambda performance: performance['loss'][:,-1].mean(axis=0) + + hh_opt = ParticleSwarmTemperingOptimizer(params, + n_trials = n_trials, + n_particles = n_particles, + prior = prior, + model = model, + n_exp = n_exp, + heuristic_class = heuristic_class + ) + hh_opt(n_pso_iterations=5, + n_pso_particles=6) From 339fc5db58862c1e35c7662e0ea88bc0ace236d1 Mon Sep 17 00:00:00 2001 From: Chris Granade Date: Wed, 19 Apr 2017 18:15:55 +1000 Subject: [PATCH 2/4] PEP8 / pylint fixes --- src/qinfer/tests/test_optimiser.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/qinfer/tests/test_optimiser.py b/src/qinfer/tests/test_optimiser.py index c0adc8d..3f37452 100644 --- a/src/qinfer/tests/test_optimiser.py +++ b/src/qinfer/tests/test_optimiser.py @@ -45,18 +45,21 @@ class TestPSO(DerandomizedTestCase): def test_pso_quad(self): - f_quad = lambda x: numpy.sum(10 * (x-0.5)**2) - hh_opt = ParticleSwarmOptimizer(['x','y','z','a'], fitness_function = f_quad) + f_quad = lambda x: numpy.sum(10 * (x - 0.5) ** 2) + hh_opt = ParticleSwarmOptimizer(['x', 'y', 'z', 'a'], fitness_function=f_quad) hh_opt() def test_pso_sin_sq(self): - f_sin_sq = lambda x: numpy.sum(np.sin(x - 0.2)**2) - hh_opt = ParticleSwarmOptimizer(['x','y','z','a'], fitness_function = f_sin_sq) + f_sin_sq = lambda x: numpy.sum(np.sin(x - 0.2) ** 2) + hh_opt = ParticleSwarmOptimizer(['x', 'y', 'z', 'a'], fitness_function=f_sin_sq) hh_opt() def test_pso_rosenbrock(self): - f_rosenbrock = lambda x: numpy.sum([((x[i+1] - x[i]**2)**2 + (1 - x[i])**2)/len(x) for i in range(len(x)-1)]) - hh_opt = ParticleSwarmOptimizer(['x','y','z','a'], fitness_function = f_rosenbrock) + f_rosenbrock = lambda x: numpy.sum([ + ((x[i + 1] - x[i] ** 2) ** 2 + (1 - x[i])** 2) / len(x) + for i in range(len(x) - 1) + ]) + hh_opt = ParticleSwarmOptimizer(['x', 'y', 'z', 'a'], fitness_function=f_rosenbrock) hh_opt() From 60f3874bec9f0143541e44b7b0c72c97e683e11e Mon Sep 17 00:00:00 2001 From: Chris Granade Date: Wed, 19 Apr 2017 18:21:23 +1000 Subject: [PATCH 3/4] Import fixes, some tests now pass. --- src/qinfer/tests/test_optimiser.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/qinfer/tests/test_optimiser.py b/src/qinfer/tests/test_optimiser.py index 3f37452..c23cbdd 100644 --- a/src/qinfer/tests/test_optimiser.py +++ b/src/qinfer/tests/test_optimiser.py @@ -35,27 +35,30 @@ import numpy as np import random as rnd +from qinfer.tests.base_test import DerandomizedTestCase + import qinfer.rb as rb import qinfer.distributions as dist -from qinfer.tests.base_test import DerandomizedTestCase +from qinfer.hyper_heuristic_optimisers import ParticleSwarmOptimizer +from qinfer.expdesign import ExpSparseHeuristic ## CLASSES #################################################################### class TestPSO(DerandomizedTestCase): def test_pso_quad(self): - f_quad = lambda x: numpy.sum(10 * (x - 0.5) ** 2) + f_quad = lambda x: np.sum(10 * (x - 0.5) ** 2) hh_opt = ParticleSwarmOptimizer(['x', 'y', 'z', 'a'], fitness_function=f_quad) hh_opt() def test_pso_sin_sq(self): - f_sin_sq = lambda x: numpy.sum(np.sin(x - 0.2) ** 2) + f_sin_sq = lambda x: np.sum(np.sin(x - 0.2) ** 2) hh_opt = ParticleSwarmOptimizer(['x', 'y', 'z', 'a'], fitness_function=f_sin_sq) hh_opt() def test_pso_rosenbrock(self): - f_rosenbrock = lambda x: numpy.sum([ + f_rosenbrock = lambda x: np.sum([ ((x[i + 1] - x[i] ** 2) ** 2 + (1 - x[i])** 2) / len(x) for i in range(len(x) - 1) ]) @@ -81,14 +84,12 @@ def test_pso_perf_test_multiple_short(self): ) #Heuristic used in the experiment - heuristic_class = qi.expdesign.ExpSparseHeuristic + heuristic_class = ExpSparseHeuristic #Heuristic Parameters params = ['base', 'scale'] #Fitness function to evaluate the performance of the experiment - EXPERIMENT_FITNESS = lambda performance: performance['loss'][:,-1].mean(axis=0) - hh_opt = ParticleSwarmOptimizer(params, n_trials = n_trials, n_particles = n_particles, From cab23a986131dfc61a882d65fca9fb094d28d295 Mon Sep 17 00:00:00 2001 From: Chris Granade Date: Wed, 19 Apr 2017 18:31:20 +1000 Subject: [PATCH 4/4] More import fixes, refactored to test annealing+tempering. --- src/qinfer/tests/test_optimiser.py | 160 ++++++----------------------- 1 file changed, 31 insertions(+), 129 deletions(-) diff --git a/src/qinfer/tests/test_optimiser.py b/src/qinfer/tests/test_optimiser.py index c23cbdd..957280b 100644 --- a/src/qinfer/tests/test_optimiser.py +++ b/src/qinfer/tests/test_optimiser.py @@ -30,43 +30,47 @@ ## IMPORTS #################################################################### -from functools import partial - import numpy as np -import random as rnd from qinfer.tests.base_test import DerandomizedTestCase import qinfer.rb as rb import qinfer.distributions as dist -from qinfer.hyper_heuristic_optimisers import ParticleSwarmOptimizer +from qinfer.hyper_heuristic_optimisers import ( + ParticleSwarmOptimizer, + ParticleSwarmSimpleAnnealingOptimizer, + ParticleSwarmTemperingOptimizer +) from qinfer.expdesign import ExpSparseHeuristic ## CLASSES #################################################################### -class TestPSO(DerandomizedTestCase): +class OptimizerTestMethods(object): + # See http://stackoverflow.com/a/1323554/267841 for why this works. + + optimizer_class = None - def test_pso_quad(self): + def test_quad(self): f_quad = lambda x: np.sum(10 * (x - 0.5) ** 2) - hh_opt = ParticleSwarmOptimizer(['x', 'y', 'z', 'a'], fitness_function=f_quad) + hh_opt = self.optimizer_class(['x', 'y', 'z', 'a'], fitness_function=f_quad) hh_opt() - def test_pso_sin_sq(self): + def test_sin_sq(self): f_sin_sq = lambda x: np.sum(np.sin(x - 0.2) ** 2) - hh_opt = ParticleSwarmOptimizer(['x', 'y', 'z', 'a'], fitness_function=f_sin_sq) + hh_opt = self.optimizer_class(['x', 'y', 'z', 'a'], fitness_function=f_sin_sq) hh_opt() - def test_pso_rosenbrock(self): + def test_rosenbrock(self): f_rosenbrock = lambda x: np.sum([ ((x[i + 1] - x[i] ** 2) ** 2 + (1 - x[i])** 2) / len(x) for i in range(len(x) - 1) ]) - hh_opt = ParticleSwarmOptimizer(['x', 'y', 'z', 'a'], fitness_function=f_rosenbrock) + hh_opt = self.optimizer_class(['x', 'y', 'z', 'a'], fitness_function=f_rosenbrock) hh_opt() - def test_pso_perf_test_multiple_short(self): + def test_perf_test_multiple_short(self): # Define our experiment n_trials = 20 # Times we repeat the set of experiments n_exp = 100 # Number of experiments in the set @@ -90,124 +94,22 @@ def test_pso_perf_test_multiple_short(self): params = ['base', 'scale'] #Fitness function to evaluate the performance of the experiment - hh_opt = ParticleSwarmOptimizer(params, - n_trials = n_trials, - n_particles = n_particles, - prior = prior, - model = model, - n_exp = n_exp, - heuristic_class = heuristic_class - ) - hh_opt(n_pso_iterations=5, - n_pso_particles=6) - -def TestPSSAO(DerandomizedTestCase): - - def test_pssao_quad(self): - f_quad = lambda x: numpy.sum(10 * (x-0.5)**2) - hh_opt = ParticleSwarmSimpleAnnealingOptimizer(['x','y','z','a'], fitness_function = f_quad) - hh_opt() - - def test_pssao_sin_sq(self): - f_sin_sq = lambda x: numpy.sum(np.sin(x - 0.2)**2) - hh_opt = ParticleSwarmSimpleAnnealingOptimizer(['x','y','z','a'], fitness_function = f_sin_sq) - hh_opt() - - def test_pssao_rosenbrock(self): - f_rosenbrock = lambda x: numpy.sum([((x[i+1] - x[i]**2)**2 + (1 - x[i])**2)/len(x) for i in range(len(x)-1)]) - hh_opt = ParticleSwarmSimpleAnnealingOptimizer(['x','y','z','a'], fitness_function = f_rosenbrock) - hh_opt() - - - def test_pssao_perf_test_multiple_short(self): - # Define our experiment - n_trials = 20 # Times we repeat the set of experiments - n_exp = 150 # Number of experiments in the set - n_particles = 4000 # Number of points we track during the experiment - - # Model for the experiment - model = rb.RandomizedBenchmarkingModel() - - #Ordering of RB is 'p', 'A', 'B' - # A + B < 1, 0 < p < 1 - #Prior distribution of the experiment - prior = dist.PostselectedDistribution( - dist.MultivariateNormalDistribution(mean=[0.5,0.1,0.25], cov=np.diag([0.1, 0.1, 0.1])), - model - ) - - #Heuristic used in the experiment - heuristic_class = qi.expdesign.ExpSparseHeuristic - - #Heuristic Parameters - params = ['base', 'scale'] - - #Fitness function to evaluate the performance of the experiment - EXPERIMENT_FITNESS = lambda performance: performance['loss'][:,-1].mean(axis=0) - - hh_opt = ParticleSwarmSimpleAnnealingOptimizer(params, - n_trials = n_trials, - n_particles = n_particles, - prior = prior, - model = model, - n_exp = n_exp, - heuristic_class = heuristic_class - ) + hh_opt = self.optimizer_class(params, + n_trials=n_trials, + n_particles=n_particles, + prior=prior, + model=model, + n_exp=n_exp, + heuristic_class=heuristic_class + ) hh_opt(n_pso_iterations=5, - n_pso_particles=6) - - -def TestPSTO(DerandomizedTestCase): - - def test_psto_quad(self): - f_quad = lambda x: numpy.sum(10 * (x-0.5)**2) - hh_opt = ParticleSwarmTemperingOptimizer(['x','y','z','a'], fitness_function = f_quad) - hh_opt() - - def test_psto_sin_sq(self): - f_sin_sq = lambda x: numpy.sum(np.sin(x - 0.2)**2) - hh_opt = ParticleSwarmTemperingOptimizer(['x','y','z','a'], fitness_function = f_sin_sq) - hh_opt() + n_pso_particles=6) - def test_psto_rosenbrock(self): - f_rosenbrock = lambda x: numpy.sum([((x[i+1] - x[i]**2)**2 + (1 - x[i])**2)/len(x) for i in range(len(x)-1)]) - hh_opt = ParticleSwarmTemperingOptimizer(['x','y','z','a'], fitness_function = f_rosenbrock) - hh_opt() - - - def test_psto_perf_test_multiple_short(self): - # Define our experiment - n_trials = 20 # Times we repeat the set of experiments - n_exp = 150 # Number of experiments in the set - n_particles = 4000 # Number of points we track during the experiment +class TestPSO(DerandomizedTestCase, OptimizerTestMethods): + optimizer_class = ParticleSwarmOptimizer - # Model for the experiment - model = rb.RandomizedBenchmarkingModel() +class TestPSSAO(DerandomizedTestCase, OptimizerTestMethods): + optimizer_class = ParticleSwarmSimpleAnnealingOptimizer - #Ordering of RB is 'p', 'A', 'B' - # A + B < 1, 0 < p < 1 - #Prior distribution of the experiment - prior = dist.PostselectedDistribution( - dist.MultivariateNormalDistribution(mean=[0.5,0.1,0.25], cov=np.diag([0.1, 0.1, 0.1])), - model - ) - - #Heuristic used in the experiment - heuristic_class = qi.expdesign.ExpSparseHeuristic - - #Heuristic Parameters - params = ['base', 'scale'] - - #Fitness function to evaluate the performance of the experiment - EXPERIMENT_FITNESS = lambda performance: performance['loss'][:,-1].mean(axis=0) - - hh_opt = ParticleSwarmTemperingOptimizer(params, - n_trials = n_trials, - n_particles = n_particles, - prior = prior, - model = model, - n_exp = n_exp, - heuristic_class = heuristic_class - ) - hh_opt(n_pso_iterations=5, - n_pso_particles=6) +class TestPSTO(DerandomizedTestCase, OptimizerTestMethods): + optimizer_class = ParticleSwarmTemperingOptimizer