diff options
| author | Florian Fischer <florian.fl.fischer@fau.de> | 2020-05-13 22:43:17 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fl.fischer@fau.de> | 2020-05-13 22:43:17 +0200 |
| commit | 825a80b7efe9d40c7b9da61b8a508e89f47da856 (patch) | |
| tree | f8b9c209f7697377882c37e16fd593295fa9c34d /problems.h | |
| download | pso-825a80b7efe9d40c7b9da61b8a508e89f47da856.tar.gz pso-825a80b7efe9d40c7b9da61b8a508e89f47da856.zip | |
My simple particle swarm optimization
Diffstat (limited to 'problems.h')
| -rw-r--r-- | problems.h | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/problems.h b/problems.h new file mode 100644 index 0000000..d612f8a --- /dev/null +++ b/problems.h @@ -0,0 +1,82 @@ +#pragma once + +#include "pso.h" + +typedef struct problem { + char* name; + interval_t interval; + double(*func)(particle_t*); +} problem_t; + +static const interval_t sphere_interval = {-500, 500}; +static double sphere_func(particle_t* particle) { + size_t size = particle->x->size; + double sum = 0; + + for (size_t i = 0; i < size; ++i) { + double x = particle->x->elements[i]; + if (x < sphere_interval.start || x > sphere_interval.end) + return INFINITY; + + sum += pow(x, 2); + } + return sum; +} +static const problem_t sphere = {"sphere", sphere_interval, sphere_func}; + + +static const interval_t rosenbrock_interval = {-30, 30}; +static double rosenbrock_func(particle_t* particle) { + size_t size = particle->x->size; + double sum = 0; + + for (size_t i = 0; i < size - 1; ++i) { + double x = particle->x->elements[i]; + double next_x = particle->x->elements[i + 1]; + + if (x < rosenbrock_interval.start || x > rosenbrock_interval.end + || next_x < rosenbrock_interval.start || next_x > rosenbrock_interval.end) + return INFINITY; + + sum += 100 * pow(next_x - pow(x, 2), 2) + pow((1 - x), 2); + } + return sum; +} +static const problem_t rosenbrock = {"rosenbrock", rosenbrock_interval, rosenbrock_func}; + +static const interval_t rastrigin_interval = {-5.12, 5.12}; +static double rastrigin_func(particle_t* particle) { + size_t size = particle->x->size; + double sum = 0; + + for (size_t i = 0; i < size; ++i) { + double x = particle->x->elements[i]; + + if (x < rastrigin_interval.start || x > rastrigin_interval.end) + return INFINITY; + + sum += pow(x, 2) - 10 * cos(2 * M_PI * x); + } + return 10 * size + sum; +} +static const problem_t rastrigin = {"rastrigin", rastrigin_interval, rastrigin_func}; + +static const interval_t schwefel_interval = {-500, 500}; +static double schwefel_func(particle_t* particle) { + size_t size = particle->x->size; + double sum = 0; + + for (size_t i = 0; i < size; ++i) { + double x = particle->x->elements[i]; + + if (x < schwefel_interval.start || x > schwefel_interval.end) + return INFINITY; + + sum += -1 * x * sin(sqrt(fabs(x))); + } + return sum; +} +static const problem_t schwefel = {"schwefel", schwefel_interval, schwefel_func}; + +#define NUM_PROBLEMS 4 +static problem_t problems[NUM_PROBLEMS] = {sphere, rosenbrock, rastrigin, schwefel}; |
