1// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab 2// Copyright (C) 2008-2009 - INRIA - Michael Baudin 3// Copyright (C) 2011 - DIGITEO - Michael Baudin 4// 5// Copyright (C) 2012 - 2016 - Scilab Enterprises 6// 7// This file is hereby licensed under the terms of the GNU GPL v2.0, 8// pursuant to article 5.3.4 of the CeCILL v.2.1. 9// This file was originally licensed under the terms of the CeCILL v2.1, 10// and continues to be available under such terms. 11// For more information, see the COPYING file which you should have received 12// along with this program. 13 14// <-- CLI SHELL MODE --> 15 16// 17// optimtestcase -- 18// Non linear inequality constraints are positive. 19// 20// Arguments 21// x: the point where to compute the function 22// index : the stuff to compute 23// 24function [ f , c , index ] = optimtestcase ( x , index ) 25 f = [] 26 c = [] 27 if ( index == 2 | index == 6 ) then 28 f = x(1)^2 + x(2)^2 + 2.0 * x(3)^2 + x(4)^2 ... 29 - 5.0 * x(1) - 5.0 * x(2) - 21.0 * x(3) + 7.0 * x(4) 30 end 31 if ( index == 5 | index == 6 ) then 32 c1 = - x(1)^2 - x(2)^2 - x(3)^2 - x(4)^2 ... 33 - x(1) + x(2) - x(3) + x(4) + 8 34 c2 = - x(1)^2 - 2.0 * x(2)^2 - x(3)^2 - 2.0 * x(4)^2 ... 35 + x(1) + x(4) + 10.0 36 c3 = - 2.0 * x(1)^2 - x(2)^2 - x(3)^2 - 2.0 * x(1) ... 37 + x(2) + x(4) + 5.0 38 c = [c1 c2 c3] 39 end 40endfunction 41 42warning("off"); // WARNING_EMPTY_OPS 43// 44// Test search with various error cases 45// 46nm = neldermead_new (); 47nm = neldermead_configure(nm,"-numberofvariables",4); 48nm = neldermead_configure(nm,"-function",optimtestcase); 49nm = neldermead_configure(nm,"-x0",[0.0 0.0 0.0 0.0]'); 50nm = neldermead_configure(nm,"-maxiter",200); 51nm = neldermead_configure(nm,"-maxfunevals",1000); 52nm = neldermead_configure(nm,"-tolsimplexizerelative",1.e-4); 53nm = neldermead_configure(nm,"-simplex0method","axes"); 54nm = neldermead_configure(nm,"-method","box"); 55nm = neldermead_configure(nm,"-nbineqconst",3); 56nm = neldermead_configure(nm,"-simplex0length",20.0); 57// 58// Test with inconsistent bounds 59// 60nm = neldermead_configure(nm,"-boundsmin",[10.0 -10.0 -10.0 -10.0]); 61nm = neldermead_configure(nm,"-boundsmax",[-10.0 10.0 10.0 10.0]); 62instr = "nm = neldermead_search(nm)"; 63lclmsg = gettext("%s: The max bound %s for variable #%d is lower than the min bound %s.\n"); 64assert_checkerror ( instr , lclmsg , [] , "optimbase_checkbounds","-10",1,"10" ); 65// 66// Test with wrong number of min bounds 67// 68nm = neldermead_configure(nm,"-boundsmin",[10.0]); 69nm = neldermead_configure(nm,"-boundsmax",[-10.0 10.0 10.0 10.0]); 70cmd = "nm = neldermead_search(nm)"; 71lclmsg = gettext("%s: The number of variables %d does not match the number of min bounds: %d.\n"); 72assert_checkerror ( cmd , lclmsg , [] , "optimbase_checkbounds" , 4 , 1); 73// 74// Test with wrong number of max bounds 75// 76nm = neldermead_configure(nm,"-boundsmin",[10.0 -10.0 -10.0 -10.0]); 77nm = neldermead_configure(nm,"-boundsmax",[-10.0]); 78cmd = "nm = neldermead_search(nm)"; 79lclmsg = gettext("%s: The number of variables %d does not match the number of max bounds: %d.\n"); 80assert_checkerror ( cmd , lclmsg , [] , "optimbase_checkbounds" , 4 , 1); 81// 82// Test with Box algorithm and randomized bounds simplex and no bounds 83// 84nm = neldermead_configure(nm,"-boundsmin",[]); 85nm = neldermead_configure(nm,"-boundsmax",[]); 86nm = neldermead_configure(nm,"-simplex0method","randbounds"); 87cmd = "nm = neldermead_search(nm)"; 88assert_checkerror(cmd,"%s: Randomized bounds initial simplex is not available without bounds.",[],"neldermead_startup"); 89// 90// Clean-up 91// 92nm = neldermead_destroy(nm); 93 94 95// 96// Test search with verbose to log file 97// 98nm = neldermead_new (); 99nm = neldermead_configure(nm,"-numberofvariables",4); 100nm = neldermead_configure(nm,"-function",optimtestcase); 101nm = neldermead_configure(nm,"-x0",[0.0 0.0 0.0 0.0]'); 102nm = neldermead_configure(nm,"-maxiter",10); 103nm = neldermead_configure(nm,"-verbose",1); 104nm = neldermead_configure(nm,"-logfile" , fullfile(TMPDIR,"search.txt" )); 105nm = neldermead_configure(nm,"-verbosetermination",1); 106nm = neldermead_configure(nm,"-nbineqconst",3); 107nm = neldermead_configure(nm,"-method","box"); 108nm = neldermead_search(nm, "off"); 109nm = neldermead_destroy(nm); 110computed = deletefile(fullfile(TMPDIR,"search.txt")); 111assert_checkequal ( computed , %t ); 112 113 114