Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2025.0.0 - Русский


optim_ga

A flexible genetic algorithm

Syntax

[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, will call the output function at the end of each iteration, see "output_func" under param variable below.

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).

  • "dimension", "minbounds" and "maxbounds": parameters used by the initialization function to define the initial population.

  • "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 which 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 of the efficiency of the worst individual (default value: 0.05).

  • "output_func": a callback function called after each generation if Log is %T (default function output_ga_default).

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. The header of these functions (i.e. the input and output arguments), can be checked through 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, please refer to the help page of the coding_identity function.

See 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;
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.

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

// Output function with a stop criterion
function stop=output_ga_custom(gen_index, nb_generation, Pop, FObj_Pop, param)
    [threshold, err] = get_param(param, "threshold", 1E-10); // default value for the threshold
    printf(_("%s: iteration %d / %d \n"), "optim_ga", gen_index, nb_generation);
    printf(_("    min / max value found = %.4E / %.4E\n"), min(FObj_Pop), max(FObj_Pop));
    stop = %f
    if abs(max(FObj_Pop) - min(FObj_Pop)) < threshold then
        printf(_("    Stop criterion reached: Delta Max to Min under threshold"));
        stop = %t
    end
endfunction

PopSize     = 100;
Proba_cross = 0.7;
Proba_mut   = 0.1;
NbGen       = 20;
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);

// Customized output function with a stop criterion added
ga_params = add_param(ga_params, "threshold", 1E-6); // User defined parameter for the output function
ga_params = add_param(ga_params, "output_func", output_ga_custom);

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

Customizing the initial population

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 additional 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"

Report an issue
<< Algorithms Algorithms 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 Oct 24 11:18:32 CEST 2024