[scilab-Users] Help with function init_func_default - genetic algorithms

Michaël Baudin michael.baudin at scilab.org
Thu Aug 5 11:24:35 CEST 2010


I give an example of customization of the init function at :

http://bugzilla.scilab.org/show_bug.cgi?id=7706

Notice that a side effect of this function is that the state
of the random number generator is updated.

The example is the following :

// An example with a customized init function :
// we use grand (instead of the default rand used in
// init_ga_default).
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);

After that, you may run the following statements in order
to get a brief overview of the optimization results.

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


Best regards,

Michaël


Michaël Baudin a écrit :
> Oups, forgot to paste the link :
>
> http://bugzilla.scilab.org/show_bug.cgi?id=7704
>
> Best regards,
>
> Michaël
>
> Michaël Baudin a écrit :
>> Dear Marcelo,
>>
>> I posted a bug fix for the help page of the init_ga_default function.
>> You will find the example :
>>
>> // Generate 10 points in 2 dimensions, in the
>> // interval [-2,2]^2.
>> popsize = 10;
>> ga_params = init_param();
>> 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);
>> Pop_init = init_ga_default(popsize,ga_params);
>> for k = 1 : popsize
>>  x = Pop_init(k);
>>  xstr = strcat(string(x)," ");
>>  mprintf("x[%d]=[%s]\n",k,xstr);
>> end
>>
>> An example is missing in the optim_ga function help page, which
>> shows how to customize the initial population generator function.
>> I work on this and let you know about this in some minutes.
>>
>> Best regards,
>>
>> Michaël
>>
>>
>> Marcelo Menezes Reis a écrit :
>>> Greetings
>>>
>>> I have been trying to develop a genetic algorithm in Scilab. I 
>>> intend to use binary coding. But, I am experiencing some problems 
>>> with function *init_func_default*. In help file I found:
>>>
>>>
>>>       Calling Sequence
>>>
>>>     Pop_init = init_func_default(popsize,param)
>>>
>>>       Parameters
>>>
>>>     * *popsize* : the number of individuals to generate.
>>>     * *param* : a list of parameters. - 'dimension': the size of the
>>>       vector X. - 'minbound': a vector of minimum bounds for the
>>>       variable X. - 'maxbound': a vector of maximum bounds for the
>>>       variable X.
>>>     * *Pop_init* : a list which contains the initial population of
>>>       individuals.
>>>
>>> I don´t know how to put in  "param"  the list  dimension, minbound 
>>> and  maxbound.  Can you help me with  that?
>>> -- 
>>> Marcelo Menezes Reis, Dr.Eng.
>>> Professor Adjunto - Adjunct Professor
>>> Departamento de Informática e Estatística Department of Computer 
>>> Science and Statistics
>>> Universidade Federal de Santa Catarina
>>> http://www.inf.ufsc.br/~marcelo/
>>> _____________________________________________
>>>   
>>
>>
>
>


-- 
Michaël Baudin
Ingénieur de développement
michael.baudin at scilab.org
-------------------------
Consortium Scilab - Digiteo
Domaine de Voluceau - Rocquencourt
B.P. 105 - 78153 Le Chesnay Cedex
Tel. : 01 39 63 56 87 - Fax : 01 39 63 55 94





More information about the users mailing list