1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2008 - Yann COLLETTE <yann.collette@renault.com>
3 // Copyright (C) 2013 - Scilab Enterprises - Paul Bignier: standardized and added tests
5 // This file must be used under the terms of the CeCILL.
6 // This source file is licensed as described in the file COPYING, which
7 // you should have received as part of this distribution. The terms
8 // are also available at
9 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
11 // <-- CLI SHELL MODE -->
13 // <-- ENGLISH IMPOSED -->
15 // Minimizing a quadratic function in dimension 1.
16 deff("y = test_func(x)", "y = x^2;");
17 ga_params = init_param();
18 ga_params = add_param(ga_params, "dimension", 1);
19 [pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = optim_ga(test_func, 50, 10, 0.1, 0.7, %F, ga_params);
21 assert_checkequal(length(pop_opt), length(pop_init));
22 assert_checkequal(size(fobj_pop_opt), size(fobj_pop_init));
25 // In the following example, we customize all the options in order to show all the features of the algorithm,
26 // and we increase the dimension.
37 ga_params = init_param();
38 // Parameters to adapt to the shape of the optimization problem
39 ga_params = add_param(ga_params, "minbound", [-2; -2]);
40 ga_params = add_param(ga_params, "maxbound", [2; 2]);
41 ga_params = add_param(ga_params, "dimension", 2);
42 ga_params = add_param(ga_params, "beta", 0);
43 ga_params = add_param(ga_params, "delta", 0.1);
44 // Parameters to fine tune the Genetic algorithm.
45 // All these parameters are optional for continuous optimization
46 // If you need to adapt the GA to a special problem, you
47 ga_params = add_param(ga_params, "init_func", init_ga_default);
48 ga_params = add_param(ga_params, "crossover_func", crossover_ga_default);
49 ga_params = add_param(ga_params, "mutation_func", mutation_ga_default);
50 ga_params = add_param(ga_params, "codage_func", coding_ga_identity);
51 ga_params = add_param(ga_params, "selection_func", selection_ga_elitist);
52 //ga_params = add_param(ga_params, "selection_func", selection_ga_random);
53 ga_params = add_param(ga_params, "nb_couples", NbCouples);
54 ga_params = add_param(ga_params, "pressure", pressure);
56 [pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = ..
57 optim_ga(f, PopSize, NbGen, Proba_mut, Proba_cross, Log, ga_params);
58 assert_checkequal(length(pop_opt), length(pop_init));
59 assert_checkequal(size(fobj_pop_opt), size(fobj_pop_init));
62 // Customizing the init function, which computes the initial population.
63 function Pop_init = myinitga(popsize, param)
64 // This message is to be displayed in the console
65 // for demonstration purpose only :
66 // remove it in a real application!
67 disp("Initializing the Population with grand")
68 // We deal with some parameters to take into account
69 // the boundary of the domain and the neighborhood size
70 [Dim, err] = get_param(param, "dimension", 2)
71 [MinBounds, err] = get_param(param, "minbound", -2*ones(1, Dim))
72 [MaxBounds, err] = get_param(param, "maxbound", 2*ones(1, Dim))
73 // Pop_init must be a list()
75 nr = size(MaxBounds, 1)
76 nc = size(MaxBounds, 2)
78 u = grand(nr, nc, "def")
79 Pop_init(i) = (MaxBounds - MinBounds).*u + MinBounds
82 ga_params = init_param();
83 // Parameters to adapt to the shape of the optimization problem
84 ga_params = add_param(ga_params, "minbound", [-2; -2]);
85 ga_params = add_param(ga_params, "maxbound", [2; 2]);
86 ga_params = add_param(ga_params, "dimension", 2);
87 ga_params = add_param(ga_params, "init_func", myinitga);
89 [pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = ..
90 optim_ga(f, PopSize, NbGen, Proba_mut, Proba_cross, Log, ga_params);
91 assert_checkequal(length(pop_opt), length(pop_init));
92 assert_checkequal(size(fobj_pop_opt), size(fobj_pop_init));
95 // Passing a list to the optim_ga function, where the first element of the list is
96 // the function and the remaining elements are the extra parameters. Dimension 3.
97 function y = f(x, a1, a2)
98 y = a1*sum(x.^2) + a2;
100 ga_params = init_param();
101 // Parameters to control the initial population.
102 ga_params = add_param(ga_params, "dimension", 3);
103 // Pass the extra parameters to the objective function
106 myobjfun = list(f, a1, a2);
109 [pop_opt, fobj_pop_opt] = optim_ga(myobjfun, PopSize, NbGen, Proba_mut, Proba_cross, Log, ga_params);
110 assert_checkequal(length(pop_opt), length(pop_init));
111 assert_checkequal(size(fobj_pop_opt), size(fobj_pop_init));