1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2008-2009 - INRIA - Michael Baudin
//
// 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-en.txt
//
// assert_close --
// Returns 1 if the two real matrices computed and expected are close,
// i.e. if the relative distance between computed and expected is lesser than epsilon.
// Arguments
// computed, expected : the two matrices to compare
// epsilon : a small number
//
function flag = assert_close ( computed, expected, epsilon )
if expected==0.0 then
shift = norm(computed-expected);
else
shift = norm(computed-expected)/norm(expected);
end
if shift < epsilon then
flag = 1;
else
flag = 0;
end
if flag <> 1 then bugmes();quit;end
endfunction
//
// assert_equal --
// Returns 1 if the two real matrices computed and expected are equal.
// Arguments
// computed, expected : the two matrices to compare
// epsilon : a small number
//
function flag = assert_equal ( computed , expected )
if computed==expected then
flag = 1;
else
flag = 0;
end
if flag <> 1 then bugmes();quit;end
endfunction
//
// gould.nonconvex --
// The Gould test case with additionnal inequality constraints.
// Arguments
// x : the point where to compute the cost
// index : a flag which states what is to compute
// * if index=1, or no index, returns the value of the cost
// function (default case)
// * if index=2, returns the value of the nonlinear inequality
// constraints, as a row array
// * if index=3, returns an array which contains
// at index #1, the value of the cost function
// at index #2 to the end is the list of the values of the nonlinear
// inequality constraints
// Discussion:
// The problem is to minimize a cost function with 4 non linear constraints.
// This is Problem 4.1 in Subrahmanyam, extracted from Gould.
// Non convex.
// The constraint region is a narrow winding (half-moon shaped) valley.
// Solution showed with tolerance 1.e-8.
//
// Reference:
// An extension of the simplex method to constrained
// nonlinear optimization
// M.B. Subrahmanyam
// Journal of optimization theory and applications
// Vol. 62, August 1989
//
// Gould F.J.
// Nonlinear Tolerance Programming
// Numerical methods for Nonlinear optimization
// Edited by F.A. Lootsma, pp 349-366, 1972
//
function result = gouldnonconvex ( x , index )
if (~isdef('index','local')) then
index = 1
end
if ( index==1 | index==3 ) then
f = (x(1) - 10.0 )^3 + ( x(2) - 20.0 ) ^ 3
end
if ( index==2 | index==3 ) then
c1 = x(1) - 13.0
c2 = ( x(1) - 5.0 )^2 + (x(2) - 5.0 )^2 - 100.0
c3 = -( x(1) - 6.0 )^2 - (x(2) - 5.0 )^2 + 82.81
c4 = x(2)
end
select index
case 1 then
result = f
mprintf( "Computed f = %e\n", f);
case 2
result = [c1 c2 c3 c4]
mprintf( "Computed constraints = %e %e %e %e\n", c1 , c2 , c3 , c4);
case 3
result = [f c1 c2 c3 c4]
mprintf( "Computed f = %e and constraints = %e %e %e %e\n", f , c1 , c2 , c3 , c4);
else
errmsg = sprintf("Unknown index %d", index )
error(errmsg)
end
endfunction
//
// The same cost function as before, with an
// additionnal argument, which contains parameters of the
// cost function and constraints.
// In this case, the mydata variable is passed
// explicitely by the optimization class.
// So the actual name "mydata" does not matter
// and whatever variable name can be used.
//
function result = gouldnonconvex2 ( x , index , mydata )
if (~isdef('index','local')) then
index = 1
end
if ( index==1 | index==3 ) then
f = (x(1) - mydata.f1 )^3 + ( x(2) - mydata.f2 ) ^ 3
end
if ( index==2 | index==3 ) then
c1 = x(1) - mydata.a1
c2 = ( x(1) - 5.0 )^2 + (x(2) - 5.0 )^2 - mydata.a2
c3 = -( x(1) - 6.0 )^2 - (x(2) - 5.0 )^2 + mydata.a3
c4 = x(2)
end
select index
case 1 then
result = f
mprintf( "Computed f = %e\n", f);
case 2
result = [c1 c2 c3 c4]
mprintf( "Computed constraints = %e %e %e %e\n", c1 , c2 , c3 , c4);
case 3
result = [f c1 c2 c3 c4]
mprintf( "Computed f = %e and constraints = %e %e %e %e\n", f , c1 , c2 , c3 , c4);
else
errmsg = sprintf("Unknown index %d", index )
error(errmsg)
end
endfunction
//
// Test optimbase_isfeasible method
//
// Test with bounds
opt = optimbase_new ();
opt = optimbase_configure(opt,"-numberofvariables",2);
opt = optimbase_configure(opt,"-verbose",1);
opt = optimbase_configure ( opt , "-boundsmin" , [-5.0 -5.0] );
opt = optimbase_configure ( opt , "-boundsmax" , [5.0 5.0] );
[ opt , isfeasible ] = optimbase_isinbounds ( opt , [0.0 0.0] );
assert_equal ( isfeasible , %t );
[ opt , isfeasible ] = optimbase_isinbounds ( opt , [-6.0 0.0] );
Component #1/2 of x is lower than min bound -5.000000e+00
assert_equal ( isfeasible , %f );
[ opt , isfeasible ] = optimbase_isinbounds ( opt , [0.0 6.0] );
Component #2/2 of x is greater than max bound 5.000000e+00
assert_equal ( isfeasible , %f );
opt = optimbase_destroy(opt);
//
// Test with nonlinear inequality constraints
opt = optimbase_new ();
opt = optimbase_configure(opt,"-numberofvariables",2);
opt = optimbase_configure(opt,"-verbose",1);
opt = optimbase_configure(opt,"-nbineqconst",4);
opt = optimbase_configure ( opt , "-function" , gouldnonconvex );
[ opt , isfeasible ] = optimbase_isinbounds ( opt , [ 14.0950013 , 0.8429636 ] );
assert_equal ( isfeasible , %t );
[ opt , isfeasible ] = optimbase_isinbounds ( opt , [ 14.0950013 , 0.0 ] );
assert_equal ( isfeasible , %t );
opt = optimbase_destroy(opt);
//
// Test with nonlinear inequality constraints and additionnal argument in cost function
mystuff = struct();
mystuff.f1 = 10.0;
mystuff.f2 = 20.0;
mystuff.a1 = 13.0;
mystuff.a2 = 100.0;
mystuff.a3 = 82.81;
opt = optimbase_new ();
opt = optimbase_configure ( opt , "-numberofvariables",2);
opt = optimbase_configure ( opt , "-verbose",1);
opt = optimbase_configure ( opt , "-nbineqconst",4);
opt = optimbase_configure ( opt , "-function" , gouldnonconvex2 );
opt = optimbase_configure ( opt , "-costfargument" , mystuff );
[ opt , isfeasible ] = optimbase_isinbounds ( opt , [ 14.0950013 , 0.8429636 ] );
assert_equal ( isfeasible , %t );
[ opt , isfeasible ] = optimbase_isinbounds ( opt , [ 14.0950013 , 0.0 ] );
assert_equal ( isfeasible , %t );
opt = optimbase_destroy(opt);
|