summaryrefslogtreecommitdiff
path: root/modules/optimization/demos/optim
diff options
context:
space:
mode:
authorShashank2017-05-29 12:40:26 +0530
committerShashank2017-05-29 12:40:26 +0530
commit0345245e860375a32c9a437c4a9d9cae807134e9 (patch)
treead51ecbfa7bcd3cc5f09834f1bb8c08feaa526a4 /modules/optimization/demos/optim
downloadscilab_for_xcos_on_cloud-0345245e860375a32c9a437c4a9d9cae807134e9.tar.gz
scilab_for_xcos_on_cloud-0345245e860375a32c9a437c4a9d9cae807134e9.tar.bz2
scilab_for_xcos_on_cloud-0345245e860375a32c9a437c4a9d9cae807134e9.zip
CMSCOPE changed
Diffstat (limited to 'modules/optimization/demos/optim')
-rwxr-xr-xmodules/optimization/demos/optim/optim.dem.gateway.sce14
-rwxr-xr-xmodules/optimization/demos/optim/optim_output.sce59
-rwxr-xr-xmodules/optimization/demos/optim/optim_plot.sce63
-rwxr-xr-xmodules/optimization/demos/optim/optim_rosenbrock.sce61
-rwxr-xr-xmodules/optimization/demos/optim/optim_withderivative.sce49
5 files changed, 246 insertions, 0 deletions
diff --git a/modules/optimization/demos/optim/optim.dem.gateway.sce b/modules/optimization/demos/optim/optim.dem.gateway.sce
new file mode 100755
index 000000000..5c157fa06
--- /dev/null
+++ b/modules/optimization/demos/optim/optim.dem.gateway.sce
@@ -0,0 +1,14 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2008 - INRIA
+// Copyright (C) 2010 - DIGITEO - Allan CORNET
+//
+// This file is released under the 3-clause BSD license. See COPYING-BSD.
+
+subdemolist = [_("Rosenbrock"), "optim_rosenbrock.sce"; ...
+_("Output"), "optim_output.sce"; ...
+_("Plot"), "optim_plot.sce"; ...
+_("Derivative"), "optim_withderivative.sce"];
+
+subdemolist(:,2) = SCI + "/modules/optimization/demos/optim/"+ subdemolist(:,2);
+
+
diff --git a/modules/optimization/demos/optim/optim_output.sce b/modules/optimization/demos/optim/optim_output.sce
new file mode 100755
index 000000000..daebb9121
--- /dev/null
+++ b/modules/optimization/demos/optim/optim_output.sce
@@ -0,0 +1,59 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2009 - DIGITEO - Michael Baudin
+// Copyright (C) 2010 - DIGITEO - Allan CORNET
+// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution. The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
+
+function demo_optim_output()
+
+ mprintf(_("Running optimization ...\n"));
+
+ xref = [1;2;3];
+ x0 = [1;-1;1];
+
+ function [f,g,ind] = cost(x,ind)
+ if ( ind == 1 | ind == 4 ) then
+ f = 0.5*norm(x-xref)^2;
+ end
+
+ if ( ind == 1 | ind == 4 ) then
+ g = x - xref;
+ end
+
+ if ( ind == 1 ) then
+ mprintf("===========\n")
+ mprintf("x = %s\n", strcat(string(x)," "))
+ mprintf("f = %e\n", f)
+ g=x-xref;
+ mprintf("g = %s\n", strcat(string(g)," "))
+ end
+
+ endfunction
+
+ [f, xopt] = optim(cost, x0, imp=-1)
+ //
+ // Load this script into the editor
+ //
+ m = messagebox(_("View Code?"), "Question", "question", [_("Yes") _("No")], "modal")
+ if(m == 1)
+ filename = "optim_output.sce";
+ dname = get_absolute_file_path(filename);
+ editor ( dname + filename );
+ end
+endfunction
+
+demo_optim_output()
+clear demo_optim_output;
+
+
+
+
+
+
+
+
diff --git a/modules/optimization/demos/optim/optim_plot.sce b/modules/optimization/demos/optim/optim_plot.sce
new file mode 100755
index 000000000..dd2adb462
--- /dev/null
+++ b/modules/optimization/demos/optim/optim_plot.sce
@@ -0,0 +1,63 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2009-2010 - DIGITEO - Michael Baudin
+// Copyright (C) 2010 - DIGITEO - Allan CORNET
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution. The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
+
+function demo_optim_plot()
+
+ mprintf(_("Running optimization ...\n"));
+
+ //
+ // 1. Define rosenbrock for contouring
+ function f = rosenbrockC ( x1 , x2 )
+ x = [x1 x2];
+ f = 100.0 *(x(2)-x(1)^2)^2 + (1-x(1))^2;
+ endfunction
+
+ x0 = [-1.2 1.0];
+ xopt = [1.0 1.0];
+
+ //
+ // 2. Draw the contour of Rosenbrock's function
+ xdata = linspace(-2,2,100);
+ ydata = linspace(-2,2,100);
+ mprintf("Draw contours...\n");
+ my_handle = scf(100001);
+ clf(my_handle,"reset");
+ demo_viewCode("optim_plot.sce");
+ contour ( xdata , ydata , rosenbrockC , [1 10 100 500 1000])
+ plot(x0(1) , x0(2) , "b.")
+ plot(xopt(1) , xopt(2) , "r*")
+
+ //
+ // 3. Define Rosenbrock for optimization
+ function [ f , g , ind ] = rosenbrock ( x , ind )
+ if ((ind == 1) | (ind == 4)) then
+ f = 100.0 *(x(2)-x(1)^2)^2 + (1-x(1))^2;
+ end
+
+ if ((ind == 1) | (ind == 4)) then
+ g(1) = - 400. * ( x(2) - x(1)**2 ) * x(1) -2. * ( 1. - x(1) )
+ g(2) = 200. * ( x(2) - x(1)**2 )
+ end
+
+ if (ind == 1) then
+ plot ( x(1) , x(2) , "g." )
+ end
+ endfunction
+
+ //
+ // 4. Plot the optimization process, during optimization
+ mprintf("Plot points during optimization...\n");
+ [ fopt , xopt ] = optim ( rosenbrock , x0 , imp = -1)
+
+endfunction
+
+demo_optim_plot();
+clear demo_optim_plot;
+
diff --git a/modules/optimization/demos/optim/optim_rosenbrock.sce b/modules/optimization/demos/optim/optim_rosenbrock.sce
new file mode 100755
index 000000000..49b67fc4c
--- /dev/null
+++ b/modules/optimization/demos/optim/optim_rosenbrock.sce
@@ -0,0 +1,61 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2009 - DIGITEO - Michael Baudin
+// Copyright (C) 2010 - DIGITEO - Allan CORNET
+// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution. The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
+
+function demo_rosenbrock()
+
+ mprintf(_("Running optimization ...\n"));
+
+ function [ f , g , ind ] = rosenbrock ( x , ind )
+ f = 100*(x(2)-x(1)^2)^2+(1-x(1))^2;
+ g(1) = - 400. * ( x(2) - x(1)**2 ) * x(1) -2. * ( 1. - x(1) )
+ g(2) = 200. * ( x(2) - x(1)**2 )
+ endfunction
+
+ x0 = [-1.2 1];
+ [f, x] = optim(rosenbrock, x0);
+
+ //
+ // Display results
+ //
+ mprintf("x = %s\n", strcat(string(x)," "));
+ mprintf("f = %e\n", f);
+
+ //
+ // Load this script into the editor
+ //
+ m = messagebox(_("View Code?"), "Question", "question", [_("Yes") _("No")], "modal")
+ if(m == 1)
+ filename = "optim_rosenbrock.sce";
+ dname = get_absolute_file_path(filename);
+ editor ( dname + filename );
+ end
+endfunction
+
+demo_rosenbrock();
+clear demo_rosenbrock;
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/modules/optimization/demos/optim/optim_withderivative.sce b/modules/optimization/demos/optim/optim_withderivative.sce
new file mode 100755
index 000000000..947e56bd7
--- /dev/null
+++ b/modules/optimization/demos/optim/optim_withderivative.sce
@@ -0,0 +1,49 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2009 - DIGITEO - Michael Baudin
+// Copyright (C) 2010 - DIGITEO - Allan CORNET
+// Copyright (C) 2012 - Scilab Enterprises - Adeline CARNIS
+//
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution. The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
+
+function demo_derivative()
+
+ mprintf(_("Running optimization ...\n"));
+
+ function f = rosenbrock ( x )
+ f = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
+ endfunction
+
+ function [ f , g , ind ] = rosenbrockCost2 ( x , ind )
+ if ind == 1 | ind == 4 then
+ f = rosenbrock ( x );
+ g = numderivative ( rosenbrock , x.' , [], order = 4 );
+ end
+ endfunction
+
+ x0 = [-1.2 1];
+ [ f , x ] = optim ( rosenbrockCost2 , x0 );
+
+ //
+ // Display results
+ //
+ mprintf("x = %s\n",strcat(string(x)," "));
+ mprintf("f = %e\n",f);
+
+ //
+ // Load this script into the editor
+ //
+ m = messagebox(_("View Code?"), "Question", "question", [_("Yes") _("No")], "modal")
+ if m == 1
+ filename = "optim_withderivative.sce";
+ dname = get_absolute_file_path(filename);
+ editor ( dname + filename );
+ end
+endfunction
+
+
+demo_derivative();
+clear demo_derivative;