-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
73f7be0
commit aefd81a
Showing
5 changed files
with
113 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .sl_pso_us import SL_PSO_US | ||
from .sl_pso_gs import SL_PSO_GS | ||
from .sl_pso_gs import SL_PSO_GS | ||
from .clpso import CLPSO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import jax | ||
import jax.numpy as jnp | ||
|
||
import evox as ex | ||
from evox.utils import * | ||
|
||
# SL-PSO: Social Learning PSO | ||
# SL-PSO-GS: Using Gaussian Sampling for Demonstator Choice | ||
# https://ieeexplore.ieee.org/document/6900227 | ||
@ex.jit_class | ||
class CLPSO(ex.Algorithm): | ||
def __init__( | ||
self, | ||
lb, # lower bound of problem | ||
ub, # upper bound of problem | ||
pop_size, # population size | ||
inertia_weight, # w | ||
const_coefficient, # c | ||
learning_probability, # P_c. shape:(pop_size,). It can be different for each particle | ||
): | ||
self.dim = lb.shape[0] | ||
self.lb = lb | ||
self.ub = ub | ||
self.pop_size = pop_size | ||
self.w = inertia_weight | ||
self.c = const_coefficient | ||
self.P_c = learning_probability | ||
|
||
def setup(self, key): | ||
state_key, init_pop_key, init_v_key = jax.random.split(key, 3) | ||
length = self.ub - self.lb | ||
population = jax.random.uniform( | ||
init_pop_key, shape=(self.pop_size, self.dim) | ||
) | ||
population = population * length + self.lb | ||
velocity = jax.random.uniform(init_v_key, shape=(self.pop_size, self.dim)) | ||
velocity = velocity * length * 2 - length | ||
|
||
return ex.State( | ||
population=population, | ||
velocity=velocity, | ||
pbest_position=population, | ||
pbest_fitness=jnp.full((self.pop_size,), jnp.inf), | ||
gbest_position=population[0], | ||
gbest_fitness=jnp.array([jnp.inf]), | ||
key=state_key, | ||
) | ||
|
||
def ask(self, state): | ||
return state.population, state | ||
|
||
def tell(self, state, fitness): | ||
key, random_coefficient_key, rand1_key, rand2_key, rand_key = jax.random.split(state.key, num=5) | ||
|
||
random_coefficient = jax.random.uniform(random_coefficient_key, shape=(self.pop_size, self.dim)) | ||
|
||
# ----------------- Update pbest ----------------- | ||
compare = state.pbest_fitness > fitness | ||
pbest_position = jnp.where( | ||
compare[:, jnp.newaxis], state.population, state.pbest_position | ||
) | ||
pbest_fitness = jnp.minimum(state.pbest_fitness, fitness) | ||
|
||
# ----------------- Update gbest ----------------- | ||
gbest_position, gbest_fitness = min_by( | ||
[state.gbest_position[jnp.newaxis, :], state.population], | ||
[state.gbest_position, fitness], | ||
) | ||
gbest_fitness = jnp.atleast_1d(gbest_fitness) | ||
|
||
# ------------------ Choose pbest ---------------------- | ||
|
||
rand1_index = jnp.floor(jax.random.uniform(rand1_key, shape=(self.pop_size,), minval=0, maxval=self.pop_size)).astype(int) | ||
rand2_index = jnp.floor(jax.random.uniform(rand2_key, shape=(self.pop_size,), minval=0, maxval=self.pop_size)).astype(int) | ||
learning_index = jnp.where(pbest_fitness[rand1_index] < pbest_fitness[rand2_index], rand1_index, rand2_index) | ||
learning_pbest = state.pbest_position[learning_index, :] | ||
rand_possibility = jax.random.uniform(rand_key, shape=(self.pop_size,)) | ||
rand_possibility = jnp.broadcast_to(rand_possibility[:, jnp.newaxis], shape=(self.pop_size, self.dim)) | ||
P_c = jnp.broadcast_to(self.P_c[:, jnp.newaxis], shape=(self.pop_size, self.dim)) | ||
pbest = jnp.where(rand_possibility < P_c, learning_pbest, state.pbest_position) | ||
|
||
# ------------------------------------------------------ | ||
|
||
velocity = ( | ||
self.w * state.velocity | ||
+ self.c * random_coefficient * (pbest - state.population) | ||
) | ||
population = state.population + velocity | ||
population = jnp.clip(population, self.lb, self.ub) | ||
return ex.State( | ||
population=population, | ||
velocity=velocity, | ||
pbest_position=pbest_position, | ||
pbest_fitness=pbest_fitness, | ||
gbest_position=gbest_position, | ||
gbest_fitness=gbest_fitness, | ||
key=key, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters