* Bug #8415 fixed - Genetic_algorithms: optim_* functions can now take lists as input 90/12490/3
Paul BIGNIER [Wed, 11 Sep 2013 12:15:13 +0000 (14:15 +0200)]
optim_moga(), optim_nsga(), optim_nsga2() can now take lists as input arguments,
as announced in the doc.

Change-Id: Ie618f37891a99c9dfb8b09592d474ace92982541

scilab/CHANGES_5.5.X
scilab/modules/genetic_algorithms/macros/optim_moga.sci
scilab/modules/genetic_algorithms/macros/optim_nsga.sci
scilab/modules/genetic_algorithms/macros/optim_nsga2.sci
scilab/modules/genetic_algorithms/tests/nonreg_tests/bug_8415.dia.ref [new file with mode: 0644]
scilab/modules/genetic_algorithms/tests/nonreg_tests/bug_8415.tst [new file with mode: 0644]

index 0ef4030..0aeb0bc 100644 (file)
@@ -317,6 +317,9 @@ Bug Fixes
 
 * Bug #8373 fixed - clear can now handle a matrix of strings argument.
 
+* Bug #8415 fixed - optim_moga, optim_nsga, optim_nsga2 can now take list as input arguments,
+                    as explained in their help page.
+
 * Bug #8462 fixed - bvodeS could make Scilab unstable.
 
 * Bug #8470 fixed - bvode displayed some output in terminal window and not in Scilab console.
index 74aa3dc..91f098b 100644 (file)
@@ -25,6 +25,12 @@ function [pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = optim_moga(ga_f, pop
 
     if ~isdef("ga_f","local") then
         error(gettext("optim_moga: ga_f is mandatory"));
+    else
+        if typeof(ga_f)=="list" then
+            deff("y=_ga_f(x)","y=ga_f(1)(x, ga_f(2:$))");
+        else
+            deff("y=_ga_f(x)","y=ga_f(x)");
+        end
     end
 
     if ~isdef("pop_size","local") then
@@ -59,7 +65,7 @@ function [pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = optim_moga(ga_f, pop
     Pop = codage_func(Pop,"code",param);
 
     for i=1:length(Pop)
-        MO_FObj_Pop(i,:) = ga_f(Pop(i));
+        MO_FObj_Pop(i,:) = _ga_f(Pop(i));
     end
 
     // Compute the domination rank
@@ -145,8 +151,8 @@ function [pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = optim_moga(ga_f, pop
         // Computation of the objective functions
         //
         for j=1:length(Indiv1)
-            if ToCompute_I1(j) then MO_FObj_Indiv1(j,:) = ga_f(Indiv1(j)); end
-            if ToCompute_I2(j) then MO_FObj_Indiv2(j,:) = ga_f(Indiv2(j)); end
+            if ToCompute_I1(j) then MO_FObj_Indiv1(j,:) = _ga_f(Indiv1(j)); end
+            if ToCompute_I2(j) then MO_FObj_Indiv2(j,:) = _ga_f(Indiv2(j)); end
         end
 
         // Reinit ToCompute lists
index b276598..666476c 100644 (file)
@@ -25,6 +25,12 @@ function [pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = optim_nsga(ga_f, pop
 
     if ~isdef("ga_f","local") then
         error(gettext("optim_moga: ga_f is mandatory"));
+    else
+        if typeof(ga_f)=="list" then
+            deff("y=_ga_f(x)","y=ga_f(1)(x, ga_f(2:$))");
+        else
+            deff("y=_ga_f(x)","y=ga_f(x)");
+        end
     end
 
     if ~isdef("pop_size","local") then
@@ -66,7 +72,7 @@ function [pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = optim_nsga(ga_f, pop
     Pop = codage_func(Pop,"code",param);
 
     for i=1:length(Pop)
-        MO_FObj_Pop(i,:) = ga_f(Pop(i));
+        MO_FObj_Pop(i,:) = _ga_f(Pop(i));
     end
 
     // Compute the domination rank
@@ -165,8 +171,8 @@ function [pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = optim_nsga(ga_f, pop
         // Computation of the objective functions
         //
         for j=1:length(Indiv1)
-            if ToCompute_I1(j) then MO_FObj_Indiv1(j,:) = ga_f(Indiv1(j)); end
-            if ToCompute_I2(j) then MO_FObj_Indiv2(j,:) = ga_f(Indiv2(j)); end
+            if ToCompute_I1(j) then MO_FObj_Indiv1(j,:) = _ga_f(Indiv1(j)); end
+            if ToCompute_I2(j) then MO_FObj_Indiv2(j,:) = _ga_f(Indiv2(j)); end
         end
 
         // Reinit ToCompute lists
index f405541..a449118 100644 (file)
@@ -23,6 +23,12 @@ function [pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = optim_nsga2(ga_f, po
 
     if ~isdef("ga_f","local") then
         error(gettext("optim_nsga2: ga_f is mandatory"));
+    else
+        if typeof(ga_f)=="list" then
+            deff("y=_ga_f(x)","y=ga_f(1)(x, ga_f(2:$))");
+        else
+            deff("y=_ga_f(x)","y=ga_f(x)");
+        end
     end
 
     if ~isdef("pop_size","local") then
@@ -56,7 +62,7 @@ function [pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = optim_nsga2(ga_f, po
     Pop = codage_func(Pop,"code",param);
 
     for i=1:length(Pop)
-        FObj_Pop(i,:) = ga_f(Pop(i));
+        FObj_Pop(i,:) = _ga_f(Pop(i));
     end
 
     // Compute the domination rank
@@ -149,8 +155,8 @@ function [pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = optim_nsga2(ga_f, po
         // Computation of the objective functions
         //
         for j=1:length(Indiv1)
-            if ToCompute_I1(j) then FObj_Indiv1(j,:) = ga_f(Indiv1(j)); end
-            if ToCompute_I2(j) then FObj_Indiv2(j,:) = ga_f(Indiv2(j)); end
+            if ToCompute_I1(j) then FObj_Indiv1(j,:) = _ga_f(Indiv1(j)); end
+            if ToCompute_I2(j) then FObj_Indiv2(j,:) = _ga_f(Indiv2(j)); end
         end
 
         // Reinit ToCompute lists
diff --git a/scilab/modules/genetic_algorithms/tests/nonreg_tests/bug_8415.dia.ref b/scilab/modules/genetic_algorithms/tests/nonreg_tests/bug_8415.dia.ref
new file mode 100644 (file)
index 0000000..ef56d43
--- /dev/null
@@ -0,0 +1,65 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Paul Bignier
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- CLI SHELL MODE -->
+//
+// <-- ENGLISH IMPOSED -->
+//
+// <-- Non-regression test for bug 8415 -->
+//
+// <-- Bugzilla URL -->
+// http://bugzilla.scilab.org/show_bug.cgi?id=8415
+//
+// <-- Short Description -->
+// optim_moga(), optim_nsga() and optim_nsga2() can now take list as arguments, as announced in the doc.
+// With a list for cost function
+function f = deb_2(x, p1, p2)
+    f1_x1 = x(1);
+    g_x2  = p1 + p2 * sum((x(2:$)-x(1)).^2) / (length(x) - 1);
+    h     = 1 - sqrt(f1_x1 / g_x2);
+    f(1,1) = f1_x1;
+    f(1,2) = g_x2 * h;
+endfunction
+PopSize     = 100;
+Proba_cross = 0.5;
+Proba_mut   = 0.3;
+NbGen       = 4;
+NbCouples   = 110;
+Log         = %T;
+nb_disp     = 10; // Nb point to display from the optimal population
+pressure    = 0.1;
+ga_params = init_param();
+ga_params = add_param(ga_params, "dimension", 2);
+ga_params = add_param(ga_params, "minbound", zeros(2, 1));
+ga_params = add_param(ga_params, "maxbound", ones(2, 1));
+[pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = ..
+optim_moga(list(deb_2, 1, 9), PopSize, NbGen, Proba_mut, Proba_cross, Log, ga_params);
+optim_moga: Initialization of the population
+optim_moga: iteration 1 / 4 - min / max value found = -117.000000 / -59.000000
+optim_moga: iteration 2 / 4 - min / max value found = -62.000000 / -32.000000
+optim_moga: iteration 3 / 4 - min / max value found = -54.000000 / -15.000000
+optim_moga: iteration 4 / 4 - min / max value found = -59.000000 / -22.000000
+assert_checkequal(length(pop_opt), length(pop_init));
+assert_checkequal(size(fobj_pop_opt), size(fobj_pop_init));
+[pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = ..
+optim_nsga(list(deb_2, 1, 9), PopSize, NbGen, Proba_mut, Proba_cross, Log, ga_params);
+optim_nsga: Initialization of the population
+optim_nsga: iteration 1 / 4 - min / max value found = -148.000000 / -54.000000
+optim_nsga: iteration 2 / 4 - min / max value found = -76.000000 / -30.000000
+optim_nsga: iteration 3 / 4 - min / max value found = -69.000000 / -15.000000
+optim_nsga: iteration 4 / 4 - min / max value found = -84.000000 / -8.000000
+assert_checkequal(length(pop_opt), length(pop_init));
+assert_checkequal(size(fobj_pop_opt), size(fobj_pop_init));
+[pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = ..
+optim_nsga2(list(deb_2, 1, 9), PopSize, NbGen, Proba_mut, Proba_cross, Log, ga_params);
+optim_nsga2: Initialization of the population
+optim_nsga2: iteration 1 / 4
+optim_nsga2: iteration 2 / 4
+optim_nsga2: iteration 3 / 4
+optim_nsga2: iteration 4 / 4
+assert_checkequal(length(pop_opt), length(pop_init));
+assert_checkequal(size(fobj_pop_opt), size(fobj_pop_init));
diff --git a/scilab/modules/genetic_algorithms/tests/nonreg_tests/bug_8415.tst b/scilab/modules/genetic_algorithms/tests/nonreg_tests/bug_8415.tst
new file mode 100644 (file)
index 0000000..7e7a65a
--- /dev/null
@@ -0,0 +1,59 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Paul Bignier
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- CLI SHELL MODE -->
+//
+// <-- ENGLISH IMPOSED -->
+//
+// <-- Non-regression test for bug 8415 -->
+//
+// <-- Bugzilla URL -->
+// http://bugzilla.scilab.org/show_bug.cgi?id=8415
+//
+// <-- Short Description -->
+// optim_moga(), optim_nsga() and optim_nsga2() can now take list as arguments, as announced in the doc.
+
+// With a list for cost function
+function f = deb_2(x, p1, p2)
+    f1_x1 = x(1);
+    g_x2  = p1 + p2 * sum((x(2:$)-x(1)).^2) / (length(x) - 1);
+    h     = 1 - sqrt(f1_x1 / g_x2);
+    f(1,1) = f1_x1;
+    f(1,2) = g_x2 * h;
+endfunction
+
+PopSize     = 100;
+Proba_cross = 0.5;
+Proba_mut   = 0.3;
+NbGen       = 4;
+NbCouples   = 110;
+Log         = %T;
+nb_disp     = 10; // Nb point to display from the optimal population
+pressure    = 0.1;
+
+ga_params = init_param();
+ga_params = add_param(ga_params, "dimension", 2);
+ga_params = add_param(ga_params, "minbound", zeros(2, 1));
+ga_params = add_param(ga_params, "maxbound", ones(2, 1));
+
+[pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = ..
+optim_moga(list(deb_2, 1, 9), PopSize, NbGen, Proba_mut, Proba_cross, Log, ga_params);
+
+assert_checkequal(length(pop_opt), length(pop_init));
+assert_checkequal(size(fobj_pop_opt), size(fobj_pop_init));
+
+[pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = ..
+optim_nsga(list(deb_2, 1, 9), PopSize, NbGen, Proba_mut, Proba_cross, Log, ga_params);
+
+assert_checkequal(length(pop_opt), length(pop_init));
+assert_checkequal(size(fobj_pop_opt), size(fobj_pop_init));
+
+[pop_opt, fobj_pop_opt, pop_init, fobj_pop_init] = ..
+optim_nsga2(list(deb_2, 1, 9), PopSize, NbGen, Proba_mut, Proba_cross, Log, ga_params);
+
+assert_checkequal(length(pop_opt), length(pop_init));
+assert_checkequal(size(fobj_pop_opt), size(fobj_pop_init));