def test_compute_velocity_return_values(swarm, clamp, static): """Test if compute_velocity() gives the expected shape and range""" topology = Ring(static=static) v = topology.compute_velocity(swarm, clamp) assert v.shape == swarm.position.shape if clamp is not None: assert (clamp[0] <= v).all() and (clamp[1] >= v).all()
def test_compute_position_return_values(swarm, bounds, static): """Test if compute_position() gives the expected shape and range""" topology = Ring(static=static) p = topology.compute_position(swarm, bounds) assert p.shape == swarm.velocity.shape if bounds is not None: assert (bounds[0] <= p).all() and (bounds[1] >= p).all()
def __init__( self, n_particles, dimensions, options={}, init_pos=None, velocity_clamp=None, ftol=-np.inf, ): self.logger = logging.getLogger(__name__) super().__init__( n_particles=n_particles, dimensions=dimensions, options=options, binary=True, init_pos=init_pos, velocity_clamp=velocity_clamp, ftol=ftol, ) self.food_fitness = np.inf self.enemy_fitness = -np.inf self.food_pos = np.empty(0) self.enemy_pos = np.empty(0) self.assertions() # Initialize the resettable attributes self.reset() # Initialize the topology self.top = Ring(static=False)
def __init__(self, _vae: VaeWrapper, _classifier: LatentSpaceLEC, _model_under_test: LECUnderTest, dataset: str, output_dir: str, pso_options: dict, n_iter: int): """ Initialize a test generator that synthesizes new high-uncertainty image inputs for a given pair of VAE and a classifier. :param _vae: A VAE model :param _classifier: A classifier model attached to the latent layer of the VAE :param _model_under_test: Model under test :param dataset: name of a dataset :param output_dir: (str) Output directory path :param pso_options: a dictionary containing PSO hyper-parameters, which are {c1, c2, w, k, p}. :param n_iter: PSO iteration """ self.threshold = 1.0 self.vae = _vae self.classifier = _classifier self.model_under_test = _model_under_test if not (os.path.exists(output_dir) and os.path.isdir(output_dir)): os.mkdir(output_dir) self.output_dir = output_dir self.total_cnt = 0 # total number of test generation attempted self.xs, self.dim = load_dataset(dataset, 'train', normalize=True) self.ys, self.n_classes = load_dataset(dataset, 'train', label=True) # self.n_particle = testgen_config["optimizer"]["n_particle"] self.n_iter = n_iter self.options = pso_options self.topology = Ring(static=False) min_bound = np.array([-1.0] * self.vae.latent_dim) max_bound = np.array([1.0] * self.vae.latent_dim) self.bounds = (min_bound, max_bound)
def test_update_gbest_neighborhood(swarm, p, k, static): """Test if update_gbest_neighborhood gives the expected return values""" topology = Ring(static=static) pos, cost = topology.compute_gbest(swarm, p=p, k=k) expected_pos = np.array([9.90438476e-01, 2.50379538e-03, 1.87405987e-05]) expected_cost = 1.0002528364353296 assert cost == pytest.approx(expected_cost) assert pos == pytest.approx(expected_pos)
def test_update_gbest_neighborhood(swarm, p, k): """Test if update_gbest_neighborhood gives the expected return values""" topology = Ring() pos, cost = topology.compute_gbest(swarm, p=p, k=k) expected_pos = np.array([1,2,3]) expected_cost = 1 assert (pos == expected_pos).all() assert cost == expected_cost
def __init__(self, num_params, c1=0.5 + np.log(2.0), c2=0.5 + np.log(2.0), w=0.5 / np.log(2.0), popsize=256, sigma_init=0.1, weight_decay=0.01, communication_topology='star'): self.num_params = num_params self.c1 = c1 self.c2 = c2 self.w = w self.popsize = popsize self.sigma_init = sigma_init self.weight_decay = weight_decay self.best_param = np.zeros(self.num_params) self.best_reward = 0 self.pop_params = np.random.randn(self.popsize, self.num_params) * self.sigma_init self.pbest_params = self.pop_params self.pbest_rewards = np.zeros(self.popsize) self.pop_vel = np.zeros((self.popsize, self.num_params)) self.pop_rewards = np.zeros(self.popsize) self.gbest_param = self.pop_params[np.argmax(self.pop_rewards)] self.gbest_reward = np.max(self.pop_rewards) self.first_iteration = True # Import backend modules l_lims = -np.ones(self.num_params) u_lims = np.ones(self.num_params) bounds = (l_lims, u_lims) import pyswarms.backend as P self.P = P # Global topology will always be used to compute gbest from pyswarms.backend.topology import Star self.global_topology = Star() self.communication_topology = communication_topology # Unless specified, use the star topology. if self.communication_topology == 'random': from pyswarms.backend.topology import Random self.topology = Random() # The Topology Class elif self.communication_topology == 'local': from pyswarms.backend.topology import Ring self.topology = Ring() # The Topology Class else: from pyswarms.backend.topology import Star self.topology = Star() # The Topology Class self.options = {'c1': self.c1, 'c2': self.c2, 'w': self.w} self.swarm = self.P.create_swarm(n_particles=self.popsize, dimensions=self.num_params, options=self.options, center=self.sigma_init, bounds=bounds)
def __fitNBeta(self, dim, n_particles, itera, options, objetive_function, BetaChange, bound): my_topology = Ring() my_swarm = P.create_swarm(n_particles=n_particles, dimensions=dim, options=options, bounds=bound) my_swarm.pbest_cost = np.full(n_particles, np.inf) my_swarm.best_cost = np.inf for i in range(itera): for a in range(n_particles): my_swarm.position[a][0:BetaChange] = sorted( my_swarm.position[a][0:BetaChange]) for c in range(1, self.BetaChange): if my_swarm.position[a][c - 1] + 5 >= my_swarm.position[a][c]: my_swarm.position[a][c] = my_swarm.position[a][c] + 5 my_swarm.current_cost = objetive_function(my_swarm.position) my_swarm.pbest_pos, my_swarm.pbest_cost = P.operators.compute_pbest( my_swarm) #my_swarm.current_cost[np.isnan(my_swarm.current_cost)]=np.nanmax(my_swarm.current_cost) #my_swarm.pbest_cost = objetive_function(my_swarm.pbest_pos) my_swarm.best_pos, my_swarm.best_cost = my_topology.compute_gbest( my_swarm, options['p'], options['k']) if i % 20 == 0: print( 'Iteration: {} | my_swarm.best_cost: {:.4f} | days: {}'. format( i + 1, my_swarm.best_cost, str(my_swarm.pbest_pos[my_swarm.pbest_cost.argmin()]))) my_swarm.velocity = my_topology.compute_velocity(my_swarm, bounds=bound) my_swarm.position = my_topology.compute_position(my_swarm, bounds=bound) final_best_cost = my_swarm.best_cost.copy() final_best_pos = my_swarm.pbest_pos[ my_swarm.pbest_cost.argmin()].copy() return final_best_pos, final_best_cost
def test_keyword_exception_ring(options, static): """Tests if exceptions are thrown when keywords are missing and a Ring topology is chosen""" with pytest.raises(KeyError): GeneralOptimizerPSO(5, 2, options, Ring(static=static))
def test_invalid_k_or_p_values(options, static): """Tests if exception is thrown when passing an invalid value for k or p when using a Ring topology""" with pytest.raises(ValueError): GeneralOptimizerPSO(5, 2, options, Ring(static=static))
def test_neighbor_idx(swarm, static, p, k): """Test if the neighbor_idx attribute is assigned""" topology = Ring(static=static) topology.compute_gbest(swarm, p=p, k=k) assert topology.neighbor_idx is not None
@pytest.fixture(scope="module") def binary_reset(): """Returns a BinaryPSO instance that has been run and reset to check default value""" pso = BinaryPSO(10, 2, {"c1": 0.5, "c2": 0.7, "w": 0.5, "k": 2, "p": 2}) pso.optimize(sphere_func, 10, verbose=0) pso.reset() return pso @pytest.fixture def options(): """Default options dictionary for most PSO use-cases""" options_ = {"c1": 0.5, "c2": 0.7, "w": 0.5, "k": 2, "p": 2, "r": 1} return options_ @pytest.fixture(params=[ Star(), Ring(static=False), Ring(static=True), Pyramid(static=False), Pyramid(static=True), Random(static=False), Random(static=True), VonNeumann() ]) def topology(request): """Parametrized topology parameter""" topology_ = request.param return topology_
def __init__( self, n_particles, dimensions_discrete, options, bounds, bh_strategy="periodic", init_pos=None, velocity_clamp=None, vh_strategy="unmodified", ftol=-np.inf, ftol_iter=1, ): """Initialize the swarm Attributes ---------- n_particles : int number of particles in the swarm. dimensions_discrete : int number of discrete dimensions of the search space. options : dict with keys :code:`{'c1', 'c2', 'w', 'k', 'p'}` a dictionary containing the parameters for the specific optimization technique * c1 : float cognitive parameter * c2 : float social parameter * w : float inertia parameter * k : int number of neighbors to be considered. Must be a positive integer less than :code:`n_particles` * p: int {1,2} the Minkowski p-norm to use. 1 is the sum-of-absolute values (or L1 distance) while 2 is the Euclidean (or L2) distance. bounds : tuple of numpy.ndarray a tuple of size 2 where the first entry is the minimum bound while the second entry is the maximum bound. Each array must be of shape :code:`(dimensions,)`. init_pos : numpy.ndarray, optional option to explicitly set the particles' initial positions. Set to :code:`None` if you wish to generate the particles randomly. velocity_clamp : tuple, optional a tuple of size 2 where the first entry is the minimum velocity and the second entry is the maximum velocity. It sets the limits for velocity clamping. vh_strategy : String a strategy for the handling of the velocity of out-of-bounds particles. Only the "unmodified" and the "adjust" strategies are allowed. ftol : float relative error in objective_func(best_pos) acceptable for convergence ftol_iter : int number of iterations over which the relative error in objective_func(best_pos) is acceptable for convergence. Default is :code:`1` """ # Initialize logger self.rep = Reporter(logger=logging.getLogger(__name__)) # Assign k-neighbors and p-value as attributes self.k, self.p = options["k"], options["p"] self.dimensions_discrete = dimensions_discrete self.bits, self.bounds = self.discretePSO_to_binaryPSO( dimensions_discrete, bounds) # Initialize parent class super(BinaryPSO, self).__init__( n_particles=n_particles, dimensions=sum(self.bits), binary=True, options=options, init_pos=init_pos, velocity_clamp=velocity_clamp, ftol=ftol, ftol_iter=ftol_iter, ) # self.bounds = bounds # Initialize the resettable attributes self.reset() # Initialize the topology self.top = Ring(static=False) self.vh = VelocityHandler(strategy=vh_strategy) self.bh = BoundaryHandler(strategy=bh_strategy) self.name = __name__