3030
3131## IMPORTS ####################################################################
3232
33- from functools import partial
34-
3533import numpy as np
36- import random as rnd
3734
3835from qinfer .tests .base_test import DerandomizedTestCase
3936
4037import qinfer .rb as rb
4138import qinfer .distributions as dist
4239
43- from qinfer .hyper_heuristic_optimisers import ParticleSwarmOptimizer
40+ from qinfer .hyper_heuristic_optimisers import (
41+ ParticleSwarmOptimizer ,
42+ ParticleSwarmSimpleAnnealingOptimizer ,
43+ ParticleSwarmTemperingOptimizer
44+ )
4445from qinfer .expdesign import ExpSparseHeuristic
4546
4647## CLASSES ####################################################################
4748
48- class TestPSO (DerandomizedTestCase ):
49+ class OptimizerTestMethods (object ):
50+ # See http://stackoverflow.com/a/1323554/267841 for why this works.
51+
52+ optimizer_class = None
4953
50- def test_pso_quad (self ):
54+ def test_quad (self ):
5155 f_quad = lambda x : np .sum (10 * (x - 0.5 ) ** 2 )
52- hh_opt = ParticleSwarmOptimizer (['x' , 'y' , 'z' , 'a' ], fitness_function = f_quad )
56+ hh_opt = self . optimizer_class (['x' , 'y' , 'z' , 'a' ], fitness_function = f_quad )
5357 hh_opt ()
5458
55- def test_pso_sin_sq (self ):
59+ def test_sin_sq (self ):
5660 f_sin_sq = lambda x : np .sum (np .sin (x - 0.2 ) ** 2 )
57- hh_opt = ParticleSwarmOptimizer (['x' , 'y' , 'z' , 'a' ], fitness_function = f_sin_sq )
61+ hh_opt = self . optimizer_class (['x' , 'y' , 'z' , 'a' ], fitness_function = f_sin_sq )
5862 hh_opt ()
5963
60- def test_pso_rosenbrock (self ):
64+ def test_rosenbrock (self ):
6165 f_rosenbrock = lambda x : np .sum ([
6266 ((x [i + 1 ] - x [i ] ** 2 ) ** 2 + (1 - x [i ])** 2 ) / len (x )
6367 for i in range (len (x ) - 1 )
6468 ])
65- hh_opt = ParticleSwarmOptimizer (['x' , 'y' , 'z' , 'a' ], fitness_function = f_rosenbrock )
69+ hh_opt = self . optimizer_class (['x' , 'y' , 'z' , 'a' ], fitness_function = f_rosenbrock )
6670 hh_opt ()
6771
6872
69- def test_pso_perf_test_multiple_short (self ):
73+ def test_perf_test_multiple_short (self ):
7074 # Define our experiment
7175 n_trials = 20 # Times we repeat the set of experiments
7276 n_exp = 100 # Number of experiments in the set
@@ -90,124 +94,22 @@ def test_pso_perf_test_multiple_short(self):
9094 params = ['base' , 'scale' ]
9195
9296 #Fitness function to evaluate the performance of the experiment
93- hh_opt = ParticleSwarmOptimizer (params ,
94- n_trials = n_trials ,
95- n_particles = n_particles ,
96- prior = prior ,
97- model = model ,
98- n_exp = n_exp ,
99- heuristic_class = heuristic_class
100- )
101- hh_opt (n_pso_iterations = 5 ,
102- n_pso_particles = 6 )
103-
104- def TestPSSAO (DerandomizedTestCase ):
105-
106- def test_pssao_quad (self ):
107- f_quad = lambda x : numpy .sum (10 * (x - 0.5 )** 2 )
108- hh_opt = ParticleSwarmSimpleAnnealingOptimizer (['x' ,'y' ,'z' ,'a' ], fitness_function = f_quad )
109- hh_opt ()
110-
111- def test_pssao_sin_sq (self ):
112- f_sin_sq = lambda x : numpy .sum (np .sin (x - 0.2 )** 2 )
113- hh_opt = ParticleSwarmSimpleAnnealingOptimizer (['x' ,'y' ,'z' ,'a' ], fitness_function = f_sin_sq )
114- hh_opt ()
115-
116- def test_pssao_rosenbrock (self ):
117- 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 )])
118- hh_opt = ParticleSwarmSimpleAnnealingOptimizer (['x' ,'y' ,'z' ,'a' ], fitness_function = f_rosenbrock )
119- hh_opt ()
120-
121-
122- def test_pssao_perf_test_multiple_short (self ):
123- # Define our experiment
124- n_trials = 20 # Times we repeat the set of experiments
125- n_exp = 150 # Number of experiments in the set
126- n_particles = 4000 # Number of points we track during the experiment
127-
128- # Model for the experiment
129- model = rb .RandomizedBenchmarkingModel ()
130-
131- #Ordering of RB is 'p', 'A', 'B'
132- # A + B < 1, 0 < p < 1
133- #Prior distribution of the experiment
134- prior = dist .PostselectedDistribution (
135- dist .MultivariateNormalDistribution (mean = [0.5 ,0.1 ,0.25 ], cov = np .diag ([0.1 , 0.1 , 0.1 ])),
136- model
137- )
138-
139- #Heuristic used in the experiment
140- heuristic_class = qi .expdesign .ExpSparseHeuristic
141-
142- #Heuristic Parameters
143- params = ['base' , 'scale' ]
144-
145- #Fitness function to evaluate the performance of the experiment
146- EXPERIMENT_FITNESS = lambda performance : performance ['loss' ][:,- 1 ].mean (axis = 0 )
147-
148- hh_opt = ParticleSwarmSimpleAnnealingOptimizer (params ,
149- n_trials = n_trials ,
150- n_particles = n_particles ,
151- prior = prior ,
152- model = model ,
153- n_exp = n_exp ,
154- heuristic_class = heuristic_class
155- )
97+ hh_opt = self .optimizer_class (params ,
98+ n_trials = n_trials ,
99+ n_particles = n_particles ,
100+ prior = prior ,
101+ model = model ,
102+ n_exp = n_exp ,
103+ heuristic_class = heuristic_class
104+ )
156105 hh_opt (n_pso_iterations = 5 ,
157- n_pso_particles = 6 )
158-
159-
160- def TestPSTO (DerandomizedTestCase ):
161-
162- def test_psto_quad (self ):
163- f_quad = lambda x : numpy .sum (10 * (x - 0.5 )** 2 )
164- hh_opt = ParticleSwarmTemperingOptimizer (['x' ,'y' ,'z' ,'a' ], fitness_function = f_quad )
165- hh_opt ()
166-
167- def test_psto_sin_sq (self ):
168- f_sin_sq = lambda x : numpy .sum (np .sin (x - 0.2 )** 2 )
169- hh_opt = ParticleSwarmTemperingOptimizer (['x' ,'y' ,'z' ,'a' ], fitness_function = f_sin_sq )
170- hh_opt ()
106+ n_pso_particles = 6 )
171107
172- def test_psto_rosenbrock (self ):
173- 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 )])
174- hh_opt = ParticleSwarmTemperingOptimizer (['x' ,'y' ,'z' ,'a' ], fitness_function = f_rosenbrock )
175- hh_opt ()
176-
177-
178- def test_psto_perf_test_multiple_short (self ):
179- # Define our experiment
180- n_trials = 20 # Times we repeat the set of experiments
181- n_exp = 150 # Number of experiments in the set
182- n_particles = 4000 # Number of points we track during the experiment
108+ class TestPSO (DerandomizedTestCase , OptimizerTestMethods ):
109+ optimizer_class = ParticleSwarmOptimizer
183110
184- # Model for the experiment
185- model = rb . RandomizedBenchmarkingModel ()
111+ class TestPSSAO ( DerandomizedTestCase , OptimizerTestMethods ):
112+ optimizer_class = ParticleSwarmSimpleAnnealingOptimizer
186113
187- #Ordering of RB is 'p', 'A', 'B'
188- # A + B < 1, 0 < p < 1
189- #Prior distribution of the experiment
190- prior = dist .PostselectedDistribution (
191- dist .MultivariateNormalDistribution (mean = [0.5 ,0.1 ,0.25 ], cov = np .diag ([0.1 , 0.1 , 0.1 ])),
192- model
193- )
194-
195- #Heuristic used in the experiment
196- heuristic_class = qi .expdesign .ExpSparseHeuristic
197-
198- #Heuristic Parameters
199- params = ['base' , 'scale' ]
200-
201- #Fitness function to evaluate the performance of the experiment
202- EXPERIMENT_FITNESS = lambda performance : performance ['loss' ][:,- 1 ].mean (axis = 0 )
203-
204- hh_opt = ParticleSwarmTemperingOptimizer (params ,
205- n_trials = n_trials ,
206- n_particles = n_particles ,
207- prior = prior ,
208- model = model ,
209- n_exp = n_exp ,
210- heuristic_class = heuristic_class
211- )
212- hh_opt (n_pso_iterations = 5 ,
213- n_pso_particles = 6 )
114+ class TestPSTO (DerandomizedTestCase , OptimizerTestMethods ):
115+ optimizer_class = ParticleSwarmTemperingOptimizer
0 commit comments