Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
5.3.2 - Português

Change language to:
English - Français - 日本語 -

Please note that the recommended version of Scilab is 2025.0.0. This page might be outdated.
See the recommended documentation of this function

Ajuda Scilab >> Algoritmos Genéticos > optim_ga

optim_ga

A flexible genetic algorithm

Calling Sequence

[pop_opt,fobj_pop_opt,pop_init,fobj_pop_init] = optim_ga(ga_f,pop_size,nb_generation,p_mut,p_cross,Log,param)

Arguments

ga_f

the function to be optimized. The prototype if y = f(x) or y = list(f,p1,p2,...).

pop_size

the size of the population of individuals (default value: 100).

nb_generation

the number of generations (equivalent to the number of iterations in classical optimization) to be computed (default value: 10).

p_mut

the mutation probability (default value: 0.1).

p_cross

the crossover probability (default value: 0.7).

Log

if %T, we will display to information message during the run of the genetic algorithm.

param

a list of parameters.

  • "codage_func": the function which will perform the coding and decoding of individuals (default function: coding_ga_identity).

  • "init_func": the function which will perform the initialization of the population (default function: init_ga_default).

  • "crossover_func": the function which will perform the crossover between two individuals (default function: crossover_ga_default).

  • "mutation_func": the function which will perform the mutation of one individual (default function: mutation_ga_default).

  • "selection_func": the function whcih will perform the selection of individuals at the end of a generation (default function: selection_ga_elitist).

  • "nb_couples": the number of couples which will be selected so as to perform the crossover and mutation (default value: 100).

  • "pressure": the value the efficiency of the worst individual (default value: 0.05).

pop_opt

the population of optimal individuals.

fobj_pop_opt

the set of objective function values associated to pop_opt (optional).

pop_init

the initial population of individuals (optional).

fobj_pop_init

the set of objective function values associated to pop_init (optional).

Description

This function implements the classical genetic algorithm.

A great flexibility is authorized in customizing the behaviour of the optim_ga function. This flexibility is provided by the various functions which can be set in the param variable. In order to analyze the header of these functions (i.e. the input and output arguments), we may read the help page corresponding to the default function. For example, in order to understand what are the input and output arguments of the "codage_func" function, we may read the page of the coding_identity function.

See in the Demonstrations for more examples for this function.

Examples

The following session presents the simplest possible example. We minimize a quadratic function in dimension 3. By default, all the parameters are taken in the interval [0,1]^3. The "dimension" field is passed to the function which computes the initial population, which is init_ga_default function by default. In the case where the "dimension" field is not customized, the default value is used, which is equal to 2.

function y=f(x)
  y = sum(x.^2)
endfunction

PopSize     = 100;
Proba_cross = 0.7;
Proba_mut   = 0.1;
NbGen       = 10;
NbCouples   = 110;
Log         = %T;

ga_params = init_param();
// Parameters to control the initial population.
ga_params = add_param(ga_params,"dimension",3);

[pop_opt, fobj_pop_opt] = ..
  optim_ga(f, PopSize, NbGen, Proba_mut, Proba_cross, Log, ga_params);

Once the algorithm done, we can analyze the results. In the following script, we compute some basic statistics about the optimum population and get the best and the worst points.

// Display basic statistics
// min, mean and max function values of the population.
disp([min(fobj_pop_opt) mean(fobj_pop_opt) max(fobj_pop_opt)])
// Get the best x (i.e. the one which achieves the minimum function value)
[fmin ,k] = min(fobj_pop_opt)
xmin = pop_opt(k)
// Get the worst x
[fmax ,k] = max(fobj_pop_opt)
xmax = pop_opt(k)

In the following example, we customize all the options in order to show all the features of the algorithm.

function y=f(x)
  y = sum(x.^2)
endfunction

PopSize     = 100;
Proba_cross = 0.7;
Proba_mut   = 0.1;
NbGen       = 10;
NbCouples   = 110;
Log         = %T;
pressure    = 0.05;

ga_params = init_param();
// Parameters to adapt to the shape of the optimization problem
ga_params = add_param(ga_params,"minbound",[-2; -2]);
ga_params = add_param(ga_params,"maxbound",[2; 2]);
ga_params = add_param(ga_params,"dimension",2);
ga_params = add_param(ga_params,"beta",0);
ga_params = add_param(ga_params,"delta",0.1);
// Parameters to fine tune the Genetic algorithm. 
// All these parameters are optional for continuous optimization
// If you need to adapt the GA to a special problem, you 
ga_params = add_param(ga_params,"init_func",init_ga_default);
ga_params = add_param(ga_params,"crossover_func",crossover_ga_default);
ga_params = add_param(ga_params,"mutation_func",mutation_ga_default);
ga_params = add_param(ga_params,"codage_func",coding_ga_identity);
ga_params = add_param(ga_params,"selection_func",selection_ga_elitist);
//ga_params = add_param(ga_params,"selection_func",selection_ga_random);
ga_params = add_param(ga_params,"nb_couples",NbCouples);
ga_params = add_param(ga_params,"pressure",pressure);

[pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = ..
  optim_ga(f, PopSize, NbGen, Proba_mut, Proba_cross, Log, ga_params);

In the following example, we customize the init function, which computes the initial population. In the myinitga function, we use the grand function (instead of the default rand used in init_ga_default). We could use any other type of population generator, including, for example, a low discrepancy sequence such as the Halton or Sobol sequence.

function y=f(x)
  y = sum(x.^2)
endfunction

function Pop_init=myinitga(popsize, param)
  // This message is to be displayed in the console
  // for demonstration purpose only : 
  // remove it in a real application!
  disp("Initializing the Population with grand")
  // We deal with some parameters to take into account
  // the boundary of the domain and the neighborhood size
  [Dim,err]       = get_param(param,"dimension",2)
  [MinBounds,err] = get_param(param,"minbound",-2*ones(1,Dim))
  [MaxBounds,err] = get_param(param,"maxbound",2*ones(1,Dim))
  
  // Pop_init must be a list()
  Pop_init = list()
  nr = size(MaxBounds,1)
  nc = size(MaxBounds,2)
  for i=1:popsize
    u = grand(nr,nc,"def")
    Pop_init(i) = (MaxBounds - MinBounds).*u + MinBounds
  end
endfunction

PopSize     = 100;
Proba_cross = 0.7;
Proba_mut   = 0.1;
NbGen       = 10;
NbCouples   = 110;
Log         = %T;

ga_params = init_param();
// Parameters to adapt to the shape of the optimization problem
ga_params = add_param(ga_params,"minbound",[-2; -2]);
ga_params = add_param(ga_params,"maxbound",[2; 2]);
ga_params = add_param(ga_params,"dimension",2);
ga_params = add_param(ga_params,"init_func",myinitga);

[pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = ..
  optim_ga(f, PopSize, NbGen, Proba_mut, Proba_cross, Log, ga_params);

Extra parameters for the function

In some cases, the objective function needs additionnal parameters in order to be evaluated. In this case, we can pass a list to the optim_ga function, where the first element of the list is the function and the remaining elements are the extra parameters.

This is done in the following script, where the function f needs the two extra parameters a1 and a2. This is why we define the list myobjfun and pass it to the optim_ga solver.

function y=f(x, a1, a2)
  y = a1*sum(x.^2) + a2
endfunction

PopSize     = 100;
Proba_cross = 0.7;
Proba_mut   = 0.1;
NbGen       = 10;
NbCouples   = 110;
Log         = %T;

ga_params = init_param();
// Parameters to control the initial population.
ga_params = add_param(ga_params,"dimension",3);

// Pass the extra parameters to the objective function
a1 = 12;
a2 = 7;
myobjfun = list(f,a1,a2);

// Optimize !
[pop_opt, fobj_pop_opt] = ..
  optim_ga(myobjfun, PopSize, NbGen, Proba_mut, Proba_cross, Log, ga_params);

See Also

  • optim_moga — multi-objective genetic algorithm
  • optim_nsga — A multi-objective Niched Sharing Genetic Algorithm
  • optim_nsga2 — A multi-objective Niched Sharing Genetic Algorithm version 2

References

  • Michalewicz Zbigniew "Genetic Algorithms + Data Structures = Evolution Programs"

Authors

2008, Yann COLLETTE, ycollet@freesurf.fr

2010 - DIGITEO - Michael Baudin (updated this help page)

<< mutation_ga_default Algoritmos Genéticos optim_moga >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu May 12 11:45:25 CEST 2011