Description:

cpso is a library that implements a Particle Swarm Optimization algorithm for continuous variables. It's small (< 200 lines of code) and tries to be simple.

Functions:

CpsoState* cpso_new(int n_dim, int n_pop)
Creates a new state, with n_dim dimensions and n_pop particles. Returns a pointer to the new state.
void cpso_free(CpsoState* st)
Releases the resources used by the state st.
void cpso_set_limits(CpsoState* st, int dim, double min, double max)
Sets the limits [min, max] of the dimension dim.
void cpso_set_params(CpsoState* st, double c1, double c2, double w, double r)
Sets the parameters of the state st. c1 is the cognitive learning factor (default = 2), c2 is the social learning factor (default = 2), w is the inertia (default = 0.5) and r is the replacement probability (default = 0.01). If you pass -1 to a parameter, then that parameters remains unchanged.
void cpso_set_eval(CpsoState* st, double (eval)(CpsoState*, const double*), void* ud)
Sets the fitness evaluation function (eval) to be used by the state st. Also sets ud as the state userdata (usefull for language bindings). The fitness evaluation function receives the state and a pointer to an array holding the evaluated particle data, and must return the calculated fitness (higher is better and negative values are allowed too).
void cpso_new_particle(CpsoState* st, int p, char clear)
Replaces the particle p in the state st by a new, pseudo-random, particle. If clear is different than 0, then the local best of that particle is also cleared.
void cpso_init(CpsoState* st)
Initializes state st particles.
void cpso_step(CpsoState* st)
Runs one step with the state st.
int cpso_run(CpsoState* st, int max_iters, double max_fit)
Runs the algorithm with the state st until reach max_iters iterations or the fitness max_fit.
double cpso_get_best(CpsoState* st, int p, const double** out)
Returns the fitness of the particle p (-1 for the best particle) of the state st. If out is not NULL, then out will receive the memory location of the particle values (read-only).

Examples:

See the tests/*.c files.

Contact:

http://oproj.tuxfamily.org