Skip to content

Toy example

Joao Paulo Papa edited this page Mar 17, 2017 · 15 revisions

LibOPT is pretty easy to get started. In short, what you need to do is just to pick an optimization technique, find a problem and have fun! Here, we show you a step-by-step tutorial to accomplish your mission.

For example, consider the benchmarking function Sphere running under PSO optimization, available in examples/PSO.c. The source-code related to this file is displayed below:

 #include "common.h"
 #include "function.h"
 #include "pso.h"

int main(){
    
    SearchSpace *s = NULL;
    int i;
        
    s = CreateSearchSpace(10, 2, _PSO_); /* It creates a search space with 10 agents for a 2-D problem. We are going to use PSO to solve it. */

    /* It initializes PSO parameters */
    s->w = 1.4;
    s->c1 = 1.4;
    s->c2 = 0.6;
    s->iterations = 10;
    
    /* It initalizes the boundaries of decision variables within the range [-5.12,5.12]*/
    for (i = 0; i < s->n; i++){
        s->LB[i] = -5.12;
        s->UB[i] = 5.12;
    }
    
    InitializeSearchSpace(s); /* It initalizes the search space */

    if (CheckSearchSpace(s, _PSO_)) /* It checks wether the search space is valid or not */
        runPSO(s, Sphere); /* It minimizes function Sphere */
    
    DestroySearchSpace(&s, _PSO_); /* It deallocates the search space */
    
    return 0;
}

In order to get started, you need to keep in mind five steps:

  1. To create an instance of the search space: the line of code below creates a search space with 10 agents and 2 decision variables to be optimized though Particle Swarm Optimization (_PSO_ directive).
  2. s = CreateSearchSpace(10, 2, _PSO_);
  3. To initialize its parameters. The following lines initialize the inertia weight w, c1 and c2 parameters, as well as the number of iterations for convergence. The loop initializes the lower and upper boundaries of each decision variable.
  4. s->w = 1.4;
    s->c1 = 1.4;
    s->c2 = 0.6;
    s->iterations = 10;
        
    for (i = 0; i < s->n; i++){
            s->LB[i] = -5.12;
            s->UB[i] = 5.12;
    }
  5. To initialize the search space itself (something like to populate it).
  6. InitializeSearchSpace(s);
  7. Note that you can use the function CheckSearchSpace to check whether the search space is valid or not. To run the optimization technique:
  8. runPSO(s, Sphere);
  9. To deallocate memory.
  10. DestroySearchSpace(&s, _PSO_);

As a matter of fact, step 2 can be performed though of a model file. Therefore, instead of coding the values for each parameter in the source-code, you can specify them in a text file, and load it using the following command (this command then replaces all the code presented in step 2):

s = ReadSearchSpaceFromFile("example_model.txt", _PSO_);

Each technique has a proper model file, although we tried to keep standards here. For instance, let us take a look at PSO model file in examples/model_files/pso_model.txt:

10 2 10 #<n_particles> <dimension> <max_iterations>
1.7 1.7 # <c1> <c2>
0.7 0.5 1.5 #<w> <w_min> <w_max>
-5.12 5.12 #<LB> <UB> x[0]
-5.12 5.12 #<LB> <UB> x[1]
The first line contains three integers: number of agents (particles), number of decision variables (dimension) and number of iterations. Therefore, in this example, we are creating a search space with 10 agents and 2 dimensions concerning PSO, which is quite the same as we did in step 2 above. Notice everything right after the caracter # is considering a comment, thus not taking into account by the parser.

The next two lines configure PSO parameters, and the last two lines aim at setting up the range of each decision variable. Since we have two dimensions, each line stands for one variable, say x[0] and later x[1].

That's it. Simple as it should be!
Clone this wiki locally