Skip to content

Toy example

Joao Paulo Papa edited this page Jun 2, 2016 · 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, available in examples/sphere.c. The source-code related to this file is displayed below:

 #include "opt.h"
 #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 */
    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. To run the optimization technique.
  7. To deallocate memory.

As a matter of fact, you can use in two different ways: 1. From the source-code

Clone this wiki locally