diff options
author | Harpreet | 2016-02-24 16:22:06 +0530 |
---|---|---|
committer | Harpreet | 2016-02-24 16:22:06 +0530 |
commit | 03241d180c9d65fa1e75ceac4c257df44438a1ce (patch) | |
tree | eb6e3875f136ec94ce138382adb90e925ff84750 /macros/fminunc.sci | |
parent | 7996c2704975d61e03cfb4204cfb8e202367a448 (diff) | |
download | FOSSEE-Optimization-toolbox-03241d180c9d65fa1e75ceac4c257df44438a1ce.tar.gz FOSSEE-Optimization-toolbox-03241d180c9d65fa1e75ceac4c257df44438a1ce.tar.bz2 FOSSEE-Optimization-toolbox-03241d180c9d65fa1e75ceac4c257df44438a1ce.zip |
fmincon examples added
Diffstat (limited to 'macros/fminunc.sci')
-rw-r--r-- | macros/fminunc.sci | 82 |
1 files changed, 63 insertions, 19 deletions
diff --git a/macros/fminunc.sci b/macros/fminunc.sci index a936aa1..b8ff7b5 100644 --- a/macros/fminunc.sci +++ b/macros/fminunc.sci @@ -75,6 +75,7 @@ function [xopt,fopt,exitflag,output,gradient,hessian] = fminunc (varargin) // <listitem>output.Cpu_Time: The total cpu-time spend during the search</listitem> // <listitem>output.Objective_Evaluation: The number of Objective Evaluations performed during the search</listitem> // <listitem>output.Dual_Infeasibility: The Dual Infeasiblity of the final soution</listitem> + // <listitem>output.Message: The output message for the problem</listitem> // </itemizedlist> // // Examples @@ -143,7 +144,7 @@ function [xopt,fopt,exitflag,output,gradient,hessian] = fminunc (varargin) [lhs , rhs] = argn(); //To check the number of arguments given by the user - if ( rhs<2 | rhs>5 ) then + if ( rhs<2 | rhs>3 ) then errmsg = msprintf(gettext("%s: Unexpected number of input arguments : %d provided while should be 2 or 5"), "fminunc", rhs); error(errmsg) end @@ -166,7 +167,7 @@ function [xopt,fopt,exitflag,output,gradient,hessian] = fminunc (varargin) //To check and convert the 2nd Input argument(x0) to a row vector if((size(x0,1)~=1) & (size(x0,2)~=1)) then - errmsg = msprintf(gettext("%s: Expected Row Vector or Column Vector for x0 (Initial Value) "), "fminunc", rhs); + errmsg = msprintf(gettext("%s: Expected Row Vector or Column Vector for x0 (Initial Value) "), "fminunc"); error(errmsg); else if(size(x0,2)==1) then @@ -266,22 +267,49 @@ function [xopt,fopt,exitflag,output,gradient,hessian] = fminunc (varargin) for i = 1:(size(param))/2 select convstr(param(2*i-1),'l') case "maxiter" then - options(2*i) = param(2*i); //Setting the maximum number of iterations as per user entry + if (type(param(2*i))~=1) then + errmsg = msprintf(gettext("%s: Value for Maximum Iteration should be a Constant"), "fminunc"); + error(errmsg); + else + options(2*i) = param(2*i); //Setting the maximum number of iterations as per user entry + end case "cputime" then - options(2*i) = param(2*i); //Setting the maximum CPU time as per user entry + if (type(param(2*i))~=1) then + errmsg = msprintf(gettext("%s: Value for Maximum Cpu-time should be a Constant"), "fminunc"); + error(errmsg); + else + options(2*i) = param(2*i); //Setting the maximum CPU time as per user entry + end case "gradient" then - flag1 = 1; - fGrad = param(2*i); + if (type(param(2*i))==10) then + if (convstr(param(2*i))=="off") then + flag1 =0; + else + errmsg = msprintf(gettext("%s: Unrecognized String [%s] entered for the option- %s."), "fminunc",param(2*i), param(2*i-1)); + error(errmsg); + end + else + flag1 = 1; + fGrad = param(2*i); + end case "hessian" then - flag2 = 1; - fHess = param(2*i); + if (type(param(2*i))==10) then + if (convstr(param(2*i))=="off") then + flag2 =0; + else + errmsg = msprintf(gettext("%s: Unrecognized String [%s] entered for the option- %s."), "fminunc",param(2*i), param(2*i-1)); + error(errmsg); + end + else + flag2 = 1; + fHess = param(2*i); + end else - errmsg = msprintf(gettext("%s: Unrecognized parameter name %s."), "fminbnd", param(2*i-1)); + errmsg = msprintf(gettext("%s: Unrecognized parameter name %s."), "fminunc", param(2*i-1)); error(errmsg) end end - //To check for correct input of Gradient and Hessian functions from the user if (flag1==1) then if (type(fGrad) ~= 13 & type(fGrad) ~= 11) then @@ -298,8 +326,8 @@ function [xopt,fopt,exitflag,output,gradient,hessian] = fminunc (varargin) if (size(samplefGrad,1)==s(2) & size(samplefGrad,2)==1) then elseif (size(samplefGrad,1)==1 & size(samplefGrad,2)==s(2)) then - elseif (size(samplefGrad,1)~=1 & size(samplefGrad,2)~=1) then - errmsg = msprintf(gettext("%s: Wrong Input for Objective Gradient function(3rd Parameter)---->Row Vector function is Expected"), "fminunc"); + else + errmsg = msprintf(gettext("%s: Wrong Input for Objective Gradient function(3rd Parameter)---->Row Vector function of size [1 X %d] is Expected"), "fminunc",s(2)); error(errmsg); end @@ -332,7 +360,7 @@ function [xopt,fopt,exitflag,output,gradient,hessian] = fminunc (varargin) samplefHess=fHess(x0); if(size(samplefHess,1)~=s(2) | size(samplefHess,2)~=s(2)) then - errmsg = msprintf(gettext("%s: Wrong Input for Objective Hessian function(3rd Parameter)---->Symmetric Matrix function is Expected "), "fminunc"); + errmsg = msprintf(gettext("%s: Wrong Input for Objective Hessian function(3rd Parameter)---->Symmetric Matrix function of size [%d X %d] is Expected "), "fminunc",s(2),s(2)); error(errmsg); end @@ -358,7 +386,7 @@ function [xopt,fopt,exitflag,output,gradient,hessian] = fminunc (varargin) //Calculating the values for output xopt = xopt'; exitflag = status; - output = struct("Iterations", [],"Cpu_Time",[],"Objective_Evaluation",[],"Dual_Infeasibility",[]); + output = struct("Iterations", [],"Cpu_Time",[],"Objective_Evaluation",[],"Dual_Infeasibility",[],"Message",""); output.Iterations = iter; output.Cpu_Time = cpu; output.Objective_Evaluation = obj_eval; @@ -377,7 +405,7 @@ function [xopt,fopt,exitflag,output,gradient,hessian] = fminunc (varargin) if( status~=0 & status~=1 & status~=2 & status~=3 & status~=4 & status~=7 ) then xopt=[] fopt=[] - output = struct("Iterations", [],"Cpu_Time",[]); + output = struct("Iterations", [],"Cpu_Time",[],"Message",""); output.Iterations = iter; output.Cpu_Time = cpu; gradient=[] @@ -390,38 +418,54 @@ function [xopt,fopt,exitflag,output,gradient,hessian] = fminunc (varargin) case 0 then printf("\nOptimal Solution Found.\n"); + output.Message="Optimal Solution Found"; case 1 then printf("\nMaximum Number of Iterations Exceeded. Output may not be optimal.\n"); + output.Message="Maximum Number of Iterations Exceeded. Output may not be optimal"; case 2 then printf("\nMaximum CPU Time exceeded. Output may not be optimal.\n"); + output.Message="Maximum CPU Time exceeded. Output may not be optimal"; case 3 then printf("\nStop at Tiny Step\n"); + output.Message="Stop at Tiny Step"; case 4 then printf("\nSolved To Acceptable Level\n"); + output.Message="Solved To Acceptable Level"; case 5 then printf("\nConverged to a point of local infeasibility.\n"); + output.Message="Converged to a point of local infeasibility"; case 6 then printf("\nStopping optimization at current point as requested by user.\n"); + output.Message="Stopping optimization at current point as requested by user"; case 7 then printf("\nFeasible point for square problem found.\n"); + output.Message="Feasible point for square problem found"; case 8 then printf("\nIterates diverging; problem might be unbounded.\n"); + output.Message="Iterates diverging; problem might be unbounded"; case 9 then printf("\nRestoration Failed!\n"); + output.Message="Restoration Failed!"; case 10 then printf("\nError in step computation (regularization becomes too large?)!\n"); - case 12 then + output.Message="Error in step computation (regularization becomes too large?)!"; + case 11 then printf("\nProblem has too few degrees of freedom.\n"); - case 13 then + output.Message="Problem has too few degrees of freedom"; + case 12 then printf("\nInvalid option thrown back by Ipopt\n"); - case 14 then + output.Message="Invalid option thrown back by Ipopt"; + case 13 then printf("\nNot enough memory.\n"); + output.Message="Not enough memory"; case 15 then printf("\nINTERNAL ERROR: Unknown SolverReturn value - Notify Ipopt Authors.\n"); + output.Message="INTERNAL ERROR: Unknown SolverReturn value - Notify Ipopt Authors"; else printf("\nInvalid status returned. Notify the Toolbox authors\n"); + output.Message="Invalid status returned. Notify the Toolbox authors"; break; - end + end endfunction |