diff options
Diffstat (limited to 'macros')
-rw-r--r-- | macros/Checkdims.bin | bin | 0 -> 8188 bytes | |||
-rw-r--r-- | macros/Checklhs.bin | bin | 0 -> 10312 bytes | |||
-rw-r--r-- | macros/Checkrhs.bin | bin | 0 -> 12852 bytes | |||
-rw-r--r-- | macros/Checktype.bin | bin | 0 -> 9156 bytes | |||
-rw-r--r-- | macros/Checkvector.bin | bin | 0 -> 9696 bytes | |||
-rw-r--r-- | macros/fgoalattain.bin | bin | 0 -> 76660 bytes | |||
-rw-r--r-- | macros/fminbnd.bin | bin | 0 -> 54876 bytes | |||
-rw-r--r-- | macros/fminbnd.sci | 70 | ||||
-rw-r--r-- | macros/fmincon.bin | bin | 0 -> 156196 bytes | |||
-rw-r--r-- | macros/fmincon.sci | 404 | ||||
-rw-r--r-- | macros/fminimax.bin | bin | 0 -> 86532 bytes | |||
-rw-r--r-- | macros/fminimax.sci | 10 | ||||
-rw-r--r-- | macros/fminunc.bin | bin | 0 -> 60896 bytes | |||
-rw-r--r-- | macros/fminunc.sci | 82 | ||||
-rw-r--r-- | macros/lib | bin | 0 -> 864 bytes | |||
-rw-r--r-- | macros/linprog.bin | bin | 0 -> 30012 bytes | |||
-rw-r--r-- | macros/lsqcurvefit.bin | bin | 0 -> 64252 bytes | |||
-rw-r--r-- | macros/lsqcurvefit.sci | 439 | ||||
-rw-r--r-- | macros/lsqlin.bin | bin | 0 -> 64528 bytes | |||
-rw-r--r-- | macros/lsqnonneg.bin | bin | 0 -> 33808 bytes | |||
-rw-r--r-- | macros/matrix_linprog.bin | bin | 0 -> 31100 bytes | |||
-rw-r--r-- | macros/mps_linprog.bin | bin | 0 -> 10064 bytes | |||
-rw-r--r-- | macros/names | 22 | ||||
-rw-r--r-- | macros/qpipopt.bin | bin | 0 -> 63432 bytes | |||
-rw-r--r-- | macros/qpipoptmat.bin | bin | 0 -> 65684 bytes | |||
-rw-r--r-- | macros/setOptions.bin | bin | 0 -> 3040 bytes | |||
-rw-r--r-- | macros/symphony.bin | bin | 0 -> 62044 bytes | |||
-rw-r--r-- | macros/symphony_call.bin | bin | 0 -> 4592 bytes | |||
-rw-r--r-- | macros/symphonymat.bin | bin | 0 -> 65436 bytes |
29 files changed, 814 insertions, 213 deletions
diff --git a/macros/Checkdims.bin b/macros/Checkdims.bin Binary files differnew file mode 100644 index 0000000..40e385a --- /dev/null +++ b/macros/Checkdims.bin diff --git a/macros/Checklhs.bin b/macros/Checklhs.bin Binary files differnew file mode 100644 index 0000000..7156107 --- /dev/null +++ b/macros/Checklhs.bin diff --git a/macros/Checkrhs.bin b/macros/Checkrhs.bin Binary files differnew file mode 100644 index 0000000..2c45876 --- /dev/null +++ b/macros/Checkrhs.bin diff --git a/macros/Checktype.bin b/macros/Checktype.bin Binary files differnew file mode 100644 index 0000000..bbe1585 --- /dev/null +++ b/macros/Checktype.bin diff --git a/macros/Checkvector.bin b/macros/Checkvector.bin Binary files differnew file mode 100644 index 0000000..dfe03ab --- /dev/null +++ b/macros/Checkvector.bin diff --git a/macros/fgoalattain.bin b/macros/fgoalattain.bin Binary files differnew file mode 100644 index 0000000..faa0821 --- /dev/null +++ b/macros/fgoalattain.bin diff --git a/macros/fminbnd.bin b/macros/fminbnd.bin Binary files differnew file mode 100644 index 0000000..97b00fc --- /dev/null +++ b/macros/fminbnd.bin diff --git a/macros/fminbnd.sci b/macros/fminbnd.sci index 2c29b03..b8772ae 100644 --- a/macros/fminbnd.sci +++ b/macros/fminbnd.sci @@ -75,6 +75,7 @@ function [xopt,fopt,exitflag,output,lambda] = fminbnd (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> // // The lambda data structure contains the Lagrange multipliers at the end @@ -157,7 +158,7 @@ function [xopt,fopt,exitflag,output,lambda] = fminbnd (varargin) //To check whether the 1st Input argument (fun) is a function or not if (type(fun) ~= 13 & type(fun) ~= 11) then - errmsg = msprintf(gettext("%s: Expected function for Objective (1st Parameter)"), "fmincon"); + errmsg = msprintf(gettext("%s: Expected function for Objective (1st Parameter)"), "fminbnd"); error(errmsg); end @@ -198,6 +199,12 @@ function [xopt,fopt,exitflag,output,lambda] = fminbnd (varargin) end s=size(x1); + //To check the match between f (1st Parameter) and x1 (2nd Parameter) + if(execstr('init=fun(x1)','errcatch')==21) then + errmsg = msprintf(gettext("%s: Objective function and x1 did not match"), "fminbnd"); + error(errmsg); + end + //To check whether the 3rd Input argument (x2) is a vector/scalar if (type(x2) ~= 1) then errmsg = msprintf(gettext("%s: Expected Vector/Scalar for Upper Bound Vector (3rd Parameter)"), "fminbnd"); @@ -225,9 +232,15 @@ function [xopt,fopt,exitflag,output,lambda] = fminbnd (varargin) x2=x2'; end + //To check the match between f (1st Parameter) and x2 (3rd Parameter) + if(execstr('init=fun(x2)','errcatch')==21) then + errmsg = msprintf(gettext("%s: Objective function and x2 did not match"), "fminbnd"); + error(errmsg); + end + //To check the contents of x1 and x2 (2nd & 3rd Parameter) - for i = 1:s(2) + for i = 1:s(1) if (x1(i) == %inf) then errmsg = msprintf(gettext("%s: Value of Lower Bound can not be infinity"), "fminbnd"); error(errmsg); @@ -237,7 +250,7 @@ function [xopt,fopt,exitflag,output,lambda] = fminbnd (varargin) error(errmsg); end if(x2(i)-x1(i)<=1e-6) then - errmsg = msprintf(gettext("%s: Difference between Upper Bound and Lower bound should be atleast > 10^6 for variable number= %d "), "fminbnd", i); + errmsg = msprintf(gettext("%s: Difference between Upper Bound and Lower bound should be atleast > 10^-6 for variable number= %d "), "fminbnd", i); error(errmsg) end end @@ -255,11 +268,26 @@ function [xopt,fopt,exitflag,output,lambda] = fminbnd (varargin) for i = 1:(size(param))/2 select convstr(param(2*i-1),'l') case "maxiter" then - options(2*i) = param(2*i); + if (type(param(2*i))~=1) then + errmsg = msprintf(gettext("%s: Value for Maximum Iteration should be a Constant"), "fminbnd"); + 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); + if (type(param(2*i))~=1) then + errmsg = msprintf(gettext("%s: Value for Maximum Cpu-time should be a Constant"), "fminbnd"); + error(errmsg); + else + options(2*i) = param(2*i); //Setting the maximum CPU time as per user entry + end case "tolx" then - options(2*i) = param(2*i); + if (type(param(2*i))~=1) then + errmsg = msprintf(gettext("%s: Value for Tolerance should be a Constant"), "fminbnd"); + error(errmsg); + else + options(2*i) = param(2*i); //Setting the tolerance as per user entry + end else errmsg = msprintf(gettext("%s: Unrecognized parameter name %s."), "fminbnd", param(2*i-1)); error(errmsg) @@ -304,7 +332,7 @@ function [xopt,fopt,exitflag,output,lambda] = fminbnd (varargin) //Calculating the values for the 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; @@ -315,7 +343,7 @@ function [xopt,fopt,exitflag,output,lambda] = fminbnd (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; lambda = struct("lower",[],"upper",[]); @@ -326,38 +354,54 @@ function [xopt,fopt,exitflag,output,lambda] = fminbnd (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"); + 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"); + output.Message="Problem has too few degrees of freedom"; + case 12 then + printf("\nInvalid option thrown back by Ipopt\n"); + output.Message="Invalid option thrown back by Ipopt"; case 13 then - printf("\nInvalid option thrown back by Ipopt\n"); - case 14 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 diff --git a/macros/fmincon.bin b/macros/fmincon.bin Binary files differnew file mode 100644 index 0000000..c9ba515 --- /dev/null +++ b/macros/fmincon.bin diff --git a/macros/fmincon.sci b/macros/fmincon.sci index 2393649..9faefc4 100644 --- a/macros/fmincon.sci +++ b/macros/fmincon.sci @@ -93,6 +93,7 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (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> // // The lambda data structure contains the Lagrange multipliers at the end @@ -260,6 +261,7 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin // options=list("MaxIter", [1500], "CpuTime", [500], "GradObj", fGrad, "Hessian", lHess,"GradCon", cGrad); // //Calling Ipopt // [x,fval,exitflag,output,lambda,grad,hessian] =fmincon(f, x0,A,b,Aeq,beq,lb,ub,nlc,options) + // // Press ENTER to continue // Authors // R.Vidyadhar , Vignesh Kannan @@ -268,10 +270,15 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin [lhs , rhs] = argn(); //To check the number of arguments given by the user - if ( rhs<4 | rhs==5 | rhs==7 | rhs>10 ) then + if ( rhs<4 | rhs>10 ) then errmsg = msprintf(gettext("%s: Unexpected number of input arguments : %d provided while it should be 4,6,8,9,10"), "fmincon", rhs); error(errmsg) end + + if (rhs==5 | rhs==7) then + errmsg = msprintf(gettext("%s: Unexpected number of input arguments : %d provided while it should be 4,6,8,9,10s"), "fmincon", rhs); + error(errmsg) + end //Storing the Input Parameters fun = varargin(1); @@ -299,16 +306,10 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin end //To check whether the 1st Input argument (fun) is a function or not - if (type(fun) ~= 13 & type(fun) ~= 11) then - errmsg = msprintf(gettext("%s: Expected function for Objective (1st Parameter)"), "fmincon"); - error(errmsg); - end + Checktype("fmincon", fun, "f", 1, "function"); //To check whether the 2nd Input argument (x0) is a vector/scalar - if (type(x0) ~= 1) then - errmsg = msprintf(gettext("%s: Expected Vector/Scalar for Starting Point (2nd Parameter)"), "fmincon"); - error(errmsg); - end + Checktype("fmincon", x0, "x0", 2, "constant"); //To check and convert the 2nd Input argument (x0) to a row vector if((size(x0,1)~=1) & (size(x0,2)~=1)) then @@ -346,10 +347,7 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin endfunction //To check whether the 3rd Input argument (A) is a Matrix/Vector - if (type(A) ~= 1) then - errmsg = msprintf(gettext("%s: Expected Matrix/Vector for Constraint Matrix A (3rd parameter)"), "fmincon"); - error(errmsg); - end + Checktype("fmincon", A, "A", 3, "constant"); //To check for correct size of A(3rd paramter) if(size(A,2)~=s(2) & size(A,2)~=0) then @@ -360,10 +358,7 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin s1=size(A); //To check whether the 4th Input argument (b) is a vector/scalar - if (type(b) ~= 1) then - errmsg = msprintf(gettext("%s: Expected Vector/Scalar for b (4th Parameter)"), "fmincon"); - error(errmsg); - end + Checktype("fmincon", b, "b", 4, "constant"); //To check for the correct size of b (4th paramter) and convert it into a column vector which is required for Ipopt if(s1(2)==0) then @@ -389,10 +384,7 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin end //To check whether the 5th Input argument (Aeq) is a matrix/vector - if (type(Aeq) ~= 1) then - errmsg = msprintf(gettext("%s: Expected Matrix/Vector for Equality Constraint Matrix Aeq (5th Parameter)"), "fmincon"); - error(errmsg); - end + Checktype("fmincon", Aeq, "Aeq", 5, "constant"); //To check for the correct size of Aeq (5th paramter) if(size(Aeq,2)~=s(2) & size(Aeq,2)~=0) then @@ -403,10 +395,7 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin s2=size(Aeq); //To check whether the 6th Input argument(beq) is a vector/scalar - if (type(beq) ~= 1) then - errmsg = msprintf(gettext("%s: Expected Vector/Scalar for beq (6th Parameter)"), "fmincon"); - error(errmsg); - end + Checktype("fmincon", beq, "beq", 6, "constant"); //To check for the correct size of beq(6th paramter) and convert it into a column vector which is required for Ipopt if(s2(2)==0) then @@ -433,10 +422,7 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin //To check whether the 7th Input argument (lb) is a vector/scalar - if (type(lb) ~= 1) then - errmsg = msprintf(gettext("%s: Expected Vector/Scalar for Lower Bound Vector (7th Parameter)"), "fmincon"); - error(errmsg); - end + Checktype("fmincon", lb, "lb", 7, "constant"); //To check for the correct size and data of lb (7th paramter) and convert it into a column vector as required by Ipopt if (size(lb,2)==0) then @@ -459,10 +445,7 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin end //To check whether the 8th Input argument (ub) is a vector/scalar - if (type(ub) ~= 1) then - errmsg = msprintf(gettext("%s: Expected Vector/Scalar for Upper Bound Vector (8th Parameter)"), "fmincon"); - error(errmsg); - end + Checktype("fmincon", ub, "ub", 8, "constant"); //To check for the correct size and data of ub (8th paramter) and convert it into a column vector as required by Ipopt if (size(ub,2)==0) then @@ -497,7 +480,7 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin end if(ub(i)-lb(i)<=1e-6) then - errmsg = msprintf(gettext("%s: Difference between Upper Bound and Lower bound should be atleast > 10^6 for variable number= %d "), "fmincon", i); + errmsg = msprintf(gettext("%s: Difference between Upper Bound and Lower bound should be atleast > 10^-6 for variable number= %d "), "fmincon", i); error(errmsg) end end @@ -530,7 +513,6 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin no_nlic = size(sample_c,2); no_nlec = size(sample_ceq,2); no_nlc = no_nlic + no_nlec; - //Constructing a single output variable function for nlc function y = addnlc(x) [c,ceq] = nlc(x); @@ -580,74 +562,13 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin end //If options has been entered, then check its type for 'list' - if (type(param) ~= 15) then - errmsg = msprintf(gettext("%s: Options (10th parameter) should be a list"), "fmincon"); - error(errmsg); - end + Checktype("fmincon", param, "options", 10, "list"); //If options has been entered, then check whether an even number of entires has been entered if (modulo(size(param),2)) then errmsg = msprintf(gettext("%s: Size of Options (list) should be even"), "fmincon"); error(errmsg); end - - - //Defining a function to calculate Gradient or Hessian if the respective user entry is OFF - function [y,check] = gradhess(x,t) - if t==1 then //To return Gradient - if(execstr('y=numderivative(fun,x)','errcatch')==10000) - y=0; - check=1; - else - y=numderivative(fun,x); - if (isreal(y)==%F) then - y=0; - check=1; - else - check=0; - end - end - elseif t==2 then //To return Hessian - if(execstr('[grad,y]=numderivative(fun,x)','errcatch')==10000) - y=0; - check=1; - else - [grad,y]=numderivative(fun,x); - if (isreal(y)==%F) then - y=0; - check=1; - else - check=0; - end - end - elseif t==3 then //To return Gradient - if(execstr('y=numderivative(addnlc,x)','errcatch')==10000) - y=0; - check=1; - else - y=numderivative(addnlc,x); - if (isreal(y)==%F) then - y=0; - check=1; - else - check=0; - end - end - elseif t==4 then //To return Hessian - if(execstr('[grad,y]=numderivative(addnlc,x)','errcatch')==10000) - y=0; - check=1; - else - [grad,y]=numderivative(addnlc,x); - if (isreal(y)==%F) then - y=0; - check=1; - else - check=0; - end - end - end - endfunction //To set default values for options, if user doesn't enter options options = list("MaxIter", [3000], "CpuTime", [600]); @@ -669,31 +590,65 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin //To check the user entry for options and storing it 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 + case "maxiter" then + if (type(param(2*i))~=1) then + errmsg = msprintf(gettext("%s: Value for Maximum Iteration should be a Constant"), "fmincon"); + 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"), "fmincon"); + error(errmsg); + else + options(2*i) = param(2*i); //Setting the maximum CPU time as per user entry + end case "gradobj" 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."), "fmincon",param(2*i), param(2*i-1)); + error(errmsg); + end + else + flag1 = 1; + fGrad = param(2*i); + end case "hessian" then - flag2=1; - lHess=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."), "fmincon",param(2*i), param(2*i-1)); + error(errmsg); + end + else + flag2 = 1; + lHess = param(2*i); + end case "gradcon" then - flag3=1; - cGrad=param(2*i); + if (type(param(2*i))==10) then + if (convstr(param(2*i))=="off") then + flag3 =0; + else + errmsg = msprintf(gettext("%s: Unrecognized String [%s] entered for the option- %s."), "fmincon",param(2*i), param(2*i-1)); + error(errmsg); + end + else + flag3 = 1; + cGrad = param(2*i); + end else errmsg = msprintf(gettext("%s: Unrecognized parameter name %s."), "fmincon", param(2*i-1)); error(errmsg); end end - //To check for correct input of Gradient and Hessian functions from the user + //To check for correct input of Objective Gradient function from the user if (flag1==1) then - if (type(fGrad) ~= 11 & type(fGrad) ~= 13) then - errmsg = msprintf(gettext("%s: Expected function for Gradient of Objective"), "fmincon"); - error(errmsg); - end + Checktype("fmincon", fGrad, "fGrad", 10, "function"); if(execstr('sample_fGrad=fGrad(x0)','errcatch')==21) errmsg = msprintf(gettext("%s: Gradient function of Objective and x0 did not match "), "fmincon"); @@ -704,32 +659,16 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin if (size(sample_fGrad,1)==s(2) & size(sample_fGrad,2)==1) then elseif (size(sample_fGrad,1)==1 & size(sample_fGrad,2)==s(2)) then - elseif (size(sample_fGrad,1)~=1 & size(sample_fGrad,2)~=1) then - errmsg = msprintf(gettext("%s: Wrong Input for Objective Gradient function(10th Parameter)---->Vector function is Expected"), "fmincon"); + else + errmsg = msprintf(gettext("%s: Wrong Input for Objective Gradient function(3rd Parameter)---->Row Vector function of size [1 X %d] is Expected"), "fmincon",s(2)); error(errmsg); - end - - function [y,check] = fGrad1(x) - if(execstr('y=fGrad(x)','errcatch')==32 | execstr('y=fGrad(x)','errcatch')==27) - y = 0; - check=1; - else - y=fGrad(x); - if (isreal(y)==%F) then - y = 0; - check=1; - else - check=0; - end - end - endfunction - + end end + + //To check for correct input of Lagrangian Hessian function from the user if (flag2==1) then - if (type(lHess) ~= 11 & type(lHess) ~= 13) then - errmsg = msprintf(gettext("%s: Expected function for Hessian of Objective"), "fmincon"); - error(errmsg); - end + Checktype("fmincon", lHess, "lHess", 10, "function"); + if(execstr('sample_lHess=lHess(x0,1,1:no_nlc)','errcatch')==21) errmsg = msprintf(gettext("%s: Hessian function of Objective and x0 did not match "), "fmincon"); error(errmsg); @@ -738,29 +677,12 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin if(size(sample_lHess,1)~=s(2) | size(sample_lHess,2)~=s(2)) then errmsg = msprintf(gettext("%s: Wrong Input for Objective Hessian function(10th Parameter)---->Symmetric Matrix function is Expected "), "fmincon"); error(errmsg); - end - - function [y,check] = lHess1(x,obj,lambda) - if(execstr('y=lHess(x,obj,lambda)','errcatch')==32 | execstr('y=lHess(x,obj,lambda)','errcatch')==27) - y = 0; - check=1; - else - y=lHess(x,obj,lambda); - if (isreal(y)==%F) then - y = 0; - check=1; - else - check=0; - end - end - endfunction - + end end + + //To check for correct input of Constraint Gradient function from the user if (flag3==1) then - if (type(cGrad) ~= 11 & type(cGrad) ~= 13) then - errmsg = msprintf(gettext("%s: Expected function for Gradient of Constraint function"), "fmincon"); - error(errmsg); - end + Checktype("fmincon", cGrad, "cGrad", 10, "function"); if(execstr('[sample_cGrad,sample_ceqg]=cGrad(x0)','errcatch')==21) errmsg = msprintf(gettext("%s: Gradient function of Constraint and x0 did not match "), "fmincon"); @@ -790,36 +712,149 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin errmsg = msprintf(gettext("%s: Wrong Input for Constraint Gradient function(10th Parameter) (Refer Help)"), "fmincon"); error(errmsg); end - - function [y,check] = addcGrad1(x) - if(execstr('y=addcGrad(x)','errcatch')==32 | execstr('y=addcGrad(x)','errcatch')==27) - y = 0; - check=1; - else - y=addcGrad(x); - if (isreal(y)==%F) then + end + + //Defining an inbuilt Objective gradient function + function [y,check] = fGrad1(x) + if flag1==1 then + if(execstr('y=fGrad(x)','errcatch')==32 | execstr('y=fGrad(x)','errcatch')==27) + y = 0; + check=1; + else + y=fGrad(x); + if (isreal(y)==%F) then + y = 0; + check=1; + else + check=0; + end + end + else + if(execstr('y=numderivative(fun,x)','errcatch')==10000) + y=0; + check=1; + else + y=numderivative(fun,x); + if (isreal(y)==%F) then + y=0; + check=1; + else + check=0; + end + end + end + endfunction + + //Defining an inbuilt Lagrangian Hessian function + function [y,check] = lHess1(x,obj,lambda) + if flag2==1 then + if(execstr('y=lHess(x,obj,lambda)','errcatch')==32 | execstr('y=lHess(x,obj,lambda)','errcatch')==27) y = 0; check=1; - else - check=0; + else + y=lHess(x,obj,lambda); + if (isreal(y)==%F) then + y = 0; + check=1; + else + check=0; + end end + else + if(execstr('[grad,y]=numderivative(fun,x)','errcatch')==10000) + check1=1; + else + [grad,y1]=numderivative(fun,x); + if (isreal(y1)==%F) then + check1=1; + else + check1=0; + end + end + + + if check1==0 then + if no_nlc~=0 then + if(execstr('[grad,y]=numderivative(addnlc,x)','errcatch')==10000) + check2=1; + else + [grad,y2]=numderivative(addnlc,x); + if (isreal(y2)==%F) then + check2=1; + else + check2=0; + end + end + if check2==0 then + y2=matrix(y2, no_nlc*s(2)*s(2),1) + for i = 1:s(2)*s(2) + y(i)=0; + for j = 1:no_nlc + y(i)= y(i) + lambda(j)*y2((i-1)*no_nlc+j); + end + end + + for i = 1:s(2)*s(2) + y(i) = y(i)+ obj*y1(i); + end + check=0; + else + check=1; + end + else + check=0; + for i = 1:s(2)*s(2) + y(i) = obj*y1(i); + end + end + else + check=1; + y=[0]; + end end - endfunction - end - - //To Convert the Gradient and Hessian into Error Debugable form + endfunction + //Defining an inbuilt Constraint gradient function + function [y,check] = addcGrad1(x) + if flag3==1 then + if(execstr('y=addcGrad(x)','errcatch')==32 | execstr('y=addcGrad(x)','errcatch')==27) + y = 0; + check=1; + else + y=addcGrad(x); + if (isreal(y)==%F) then + y = 0; + check=1; + else + check=0; + end + end + else + if(execstr('y=numderivative(addnlc,x)','errcatch')==10000) + y=0; + check=1; + else + y=numderivative(addnlc,x); + if (isreal(y)==%F) then + y=0; + check=1; + else + check=0; + end + end + end + endfunction + + //Creating a Dummy Variable for IPopt use + empty=[0]; - //Dummy variable which is used by Ipopt - empty=0; - //Calling the Ipopt function for solving the above problem - [xopt,fopt,status,iter,cpu,obj_eval,dual,lambda1,zl,zu,gradient,hessian1] = solveminconp (f,gradhess,A,b,Aeq,beq,lb,ub,no_nlc,no_nlic,addnlc1,flag1,fGrad1,flag2,lHess1,flag3,addcGrad1,x0,options,empty) + [xopt,fopt,status,iter,cpu,obj_eval,dual,lambda1,zl,zu,gradient,hessian1] = solveminconp(f,A,b,Aeq,beq,lb,ub,no_nlc,no_nlic,addnlc1,fGrad1,lHess1,addcGrad1,x0,options,empty) //Calculating the values for the 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; @@ -857,7 +892,7 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin lambda.ineqlin (j) = lambda1(i) j= j+1; end - lambda.ineqlin = lambda.ineqlin'; + lambda.ineqlin = lambda.ineqlin' end //Converting hessian of order (1 x (numberOfVariables)^2) received from Ipopt to order (numberOfVariables x numberOfVariables) @@ -872,7 +907,7 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (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; lambda = struct("lower",[],"upper",[],"ineqlin",[],"eqlin",[],"ineqnonlin",[],"eqnonlin",[]); @@ -886,37 +921,54 @@ function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (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"); + output.Message="Error in step computation (regularization becomes too large?)!"; case 11 then printf("\nProblem has too few degrees of freedom.\n"); + output.Message="Problem has too few degrees of freedom"; case 12 then printf("\nInvalid option thrown back by Ipopt\n"); + 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 + endfunction diff --git a/macros/fminimax.bin b/macros/fminimax.bin Binary files differnew file mode 100644 index 0000000..c023720 --- /dev/null +++ b/macros/fminimax.bin diff --git a/macros/fminimax.sci b/macros/fminimax.sci index c775b1b..b649640 100644 --- a/macros/fminimax.sci +++ b/macros/fminimax.sci @@ -19,11 +19,11 @@ function [x,fval,maxfval,exitflag,output,lambda] = fminimax(varargin) // xopt = fminimax(fun,x0,A,b,Aeq,beq,lb,ub) // xopt = fminimax(fun,x0,A,b,Aeq,beq,lb,ub,nonlinfun) // xopt = fminimax(fun,x0,A,b,Aeq,beq,lb,ub,nonlinfun,options) - // [xopt, fval] = fmincon(.....) - // [xopt, fval, maxfval]= fmincon(.....) - // [xopt, fval, maxfval, exitflag]= fmincon(.....) - // [xopt, fval, maxfval, exitflag, output]= fmincon(.....) - // [xopt, fval, maxfval, exitflag, output, lambda]= fmincon(.....) + // [xopt, fval] = fminimax(.....) + // [xopt, fval, maxfval]= fminimax(.....) + // [xopt, fval, maxfval, exitflag]= fminimax(.....) + // [xopt, fval, maxfval, exitflag, output]= fminimax(.....) + // [xopt, fval, maxfval, exitflag, output, lambda]= fminimax(.....) // // Parameters // fun: The function to be minimized. fun is a function that accepts a vector x and returns a vector F, the objective functions evaluated at x. diff --git a/macros/fminunc.bin b/macros/fminunc.bin Binary files differnew file mode 100644 index 0000000..aa82fc3 --- /dev/null +++ b/macros/fminunc.bin 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 diff --git a/macros/lib b/macros/lib Binary files differnew file mode 100644 index 0000000..612b878 --- /dev/null +++ b/macros/lib diff --git a/macros/linprog.bin b/macros/linprog.bin Binary files differnew file mode 100644 index 0000000..1d3a5aa --- /dev/null +++ b/macros/linprog.bin diff --git a/macros/lsqcurvefit.bin b/macros/lsqcurvefit.bin Binary files differnew file mode 100644 index 0000000..20a8d0d --- /dev/null +++ b/macros/lsqcurvefit.bin diff --git a/macros/lsqcurvefit.sci b/macros/lsqcurvefit.sci new file mode 100644 index 0000000..10e8e48 --- /dev/null +++ b/macros/lsqcurvefit.sci @@ -0,0 +1,439 @@ +// Copyright (C) 2015 - IIT Bombay - FOSSEE +// +// 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 +// Author: Harpreet Singh +// Organization: FOSSEE, IIT Bombay +// Email: toolbox@scilab.in + +function [xopt,resnorm,residual,exitflag,output,lambda] = lsqcurvefit (varargin) + // Solves a non linear data fitting problems. + // + // Calling Sequence + // xopt = lsqcurvefit(fun,x0,xdata,ydata) + // xopt = lsqcurvefit(fun,x0,xdata,ydata,lb,ub) + // xopt = lsqcurvefit(fun,x0,xdata,ydata,lb,ub,options) + // [xopt,resnorm] = lsqcurvefit( ... ) + // + // Parameters + // C : a matrix of double, represents the multiplier of the solution x in the expression C⋅x - d. Number of columns in C is equal to the number of elements in x. + // d : a vector of double, represents the additive constant term in the expression C⋅x - d. Number of elements in d is equal to the number of rows in C matrix. + // A : a matrix of double, represents the linear coefficients in the inequality constraints A⋅x ≤ b. + // b : a vector of double, represents the linear coefficients in the inequality constraints A⋅x ≤ b. + // Aeq : a matrix of double, represents the linear coefficients in the equality constraints Aeq⋅x = beq. + // beq : a vector of double, represents the linear coefficients in the equality constraints Aeq⋅x = beq. + // lb : a vector of double, contains lower bounds of the variables. + // ub : a vector of double, contains upper bounds of the variables. + // x0 : a vector of double, contains initial guess of variables. + // param : a list containing the parameters to be set. + // xopt : a vector of double, the computed solution of the optimization problem. + // resnorm : a double, objective value returned as the scalar value norm(C⋅x-d)^2. + // residual : a vector of double, solution residuals returned as the vector d-C⋅x. + // exitflag : The exit status. See below for details. + // output : The structure consist of statistics about the optimization. See below for details. + // lambda : The structure consist of the Lagrange multipliers at the solution of problem. See below for details. + // + // Description + // Search the minimum of a constrained linear least square problem specified by : + // + // <latex> + // \begin{eqnarray} + // &\mbox{min}_{x} + // & 1/2||C⋅x - d||_2^2 \\ + // & \text{subject to} & A⋅x \leq b \\ + // & & Aeq⋅x = beq \\ + // & & lb \leq x \leq ub \\ + // \end{eqnarray} + // </latex> + // + // The routine calls Ipopt for solving the linear least square problem, Ipopt is a library written in C++. + // + // The options allows the user to set various parameters of the Optimization problem. + // It should be defined as type "list" and contains the following fields. + // <itemizedlist> + // <listitem>Syntax : options= list("MaxIter", [---], "CpuTime", [---]);</listitem> + // <listitem>MaxIter : a Scalar, containing the Maximum Number of Iteration that the solver should take.</listitem> + // <listitem>CpuTime : a Scalar, containing the Maximum amount of CPU Time that the solver should take.</listitem> + // <listitem>Default Values : options = list("MaxIter", [3000], "CpuTime", [600]);</listitem> + // </itemizedlist> + // + // The exitflag allows to know the status of the optimization which is given back by Ipopt. + // <itemizedlist> + // <listitem>exitflag=0 : Optimal Solution Found </listitem> + // <listitem>exitflag=1 : Maximum Number of Iterations Exceeded. Output may not be optimal.</listitem> + // <listitem>exitflag=2 : Maximum CPU Time exceeded. Output may not be optimal.</listitem> + // <listitem>exitflag=3 : Stop at Tiny Step.</listitem> + // <listitem>exitflag=4 : Solved To Acceptable Level.</listitem> + // <listitem>exitflag=5 : Converged to a point of local infeasibility.</listitem> + // </itemizedlist> + // + // For more details on exitflag see the ipopt documentation, go to http://www.coin-or.org/Ipopt/documentation/ + // + // The output data structure contains detailed informations about the optimization process. + // It has type "struct" and contains the following fields. + // <itemizedlist> + // <listitem>output.iterations: The number of iterations performed during the search</listitem> + // <listitem>output.constrviolation: The max-norm of the constraint violation.</listitem> + // </itemizedlist> + // + // The lambda data structure contains the Lagrange multipliers at the end + // of optimization. In the current version the values are returned only when the the solution is optimal. + // It has type "struct" and contains the following fields. + // <itemizedlist> + // <listitem>lambda.lower: The Lagrange multipliers for the lower bound constraints.</listitem> + // <listitem>lambda.upper: The Lagrange multipliers for the upper bound constraints.</listitem> + // <listitem>lambda.eqlin: The Lagrange multipliers for the linear equality constraints.</listitem> + // <listitem>lambda.ineqlin: The Lagrange multipliers for the linear inequality constraints.</listitem> + // </itemizedlist> + // + // Examples + // //A simple linear least square example + // C = [ 2 0; + // -1 1; + // 0 2] + // d = [1 + // 0 + // -1]; + // A = [10 -2; + // -2 10]; + // b = [4 + // -4]; + // [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin(C,d,A,b) + // // Press ENTER to continue + // + // Examples + // //A basic example for equality, inequality constraints and variable bounds + // C = [1 1 1; + // 1 1 0; + // 0 1 1; + // 1 0 0; + // 0 0 1] + // d = [89; + // 67; + // 53; + // 35; + // 20;] + // A = [3 2 1; + // 2 3 4; + // 1 2 3]; + // b = [191 + // 209 + // 162]; + // Aeq = [1 2 1]; + // beq = 10; + // lb = repmat(0.1,3,1); + // ub = repmat(4,3,1); + // [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin(C,d,A,b,Aeq,beq,lb,ub) + // Authors + // Harpreet Singh + + + //To check the number of input and output argument + [lhs , rhs] = argn(); + + //To check the number of argument given by user + if ( rhs < 4 | rhs == 5 | rhs == 7 | rhs > 10 ) then + errmsg = msprintf(gettext("%s: Unexpected number of input arguments : %d provided while should be in the set of [4 6 8 9 10]"), "lsqlin", rhs); + error(errmsg) + end + +// Initializing all the values to empty matrix + C=[]; + d=[]; + A=[]; + b=[]; + Aeq=[]; + beq=[]; + lb=[]; + ub=[]; + x0=[]; + + C = varargin(1); + d = varargin(2); + A = varargin(3); + b = varargin(4); + nbVar = size(C,2); + + if(nbVar == 0) then + errmsg = msprintf(gettext("%s: Cannot determine the number of variables because input objective coefficients is empty"), "lsqlin"); + error(errmsg); + end + + if ( rhs<5 ) then + Aeq = [] + beq = [] + else + Aeq = varargin(5); + beq = varargin(6); + end + + if ( rhs<7 ) then + lb = repmat(-%inf,nbVar,1); + ub = repmat(%inf,nbVar,1); + else + lb = varargin(7); + ub = varargin(8); + end + + + if ( rhs<9 | size(varargin(9)) ==0 ) then + x0 = repmat(0,nbVar,1) + else + x0 = varargin(9); + end + + if ( rhs<10 | size(varargin(10)) ==0 ) then + param = list(); + else + param =varargin(10); + end + + if (size(lb,2)==0) then + lb = repmat(-%inf,nbVar,1); + end + + if (size(ub,2)==0) then + ub = repmat(%inf,nbVar,1); + end + + if (type(param) ~= 15) then + errmsg = msprintf(gettext("%s: param should be a list "), "lsqlin"); + error(errmsg); + end + + //Check type of variables + Checktype("lsqlin", C, "C", 1, "constant") + Checktype("lsqlin", d, "d", 2, "constant") + Checktype("lsqlin", A, "A", 3, "constant") + Checktype("lsqlin", b, "b", 4, "constant") + Checktype("lsqlin", Aeq, "Aeq", 5, "constant") + Checktype("lsqlin", beq, "beq", 6, "constant") + Checktype("lsqlin", lb, "lb", 7, "constant") + Checktype("lsqlin", ub, "ub", 8, "constant") + Checktype("lsqlin", x0, "x0", 9, "constant") + + if (modulo(size(param),2)) then + errmsg = msprintf(gettext("%s: Size of parameters should be even"), "lsqlin"); + error(errmsg); + end + + options = list( "MaxIter" , [3000], ... + "CpuTime" , [600] ... + ); + + for i = 1:(size(param))/2 + + select convstr(param(2*i-1),'l') + case "maxiter" then + options(2*i) = param(2*i); + case "cputime" then + options(2*i) = param(2*i); + else + errmsg = msprintf(gettext("%s: Unrecognized parameter name ''%s''."), "lsqlin", param(2*i-1)); + error(errmsg) + end + end + + nbConInEq = size(A,1); + nbConEq = size(Aeq,1); + + // Check if the user gives row vector + // and Changing it to a column matrix + + if (size(d,2)== [nbVar]) then + d=d'; + end + + if (size(lb,2)== [nbVar]) then + lb = lb'; + end + + if (size(ub,2)== [nbVar]) then + ub = ub'; + end + + if (size(b,2)==nbConInEq) then + b = b'; + end + + if (size(beq,2)== nbConEq) then + beq = beq'; + end + + if (size(x0,2)== [nbVar]) then + x0=x0'; + end + + //Check the size of d which should equal to the number of variable + if ( size(d,1) ~= size(C,1)) then + errmsg = msprintf(gettext("%s: The number of rows in C must be equal the number of elements of d"), "lsqlin"); + error(errmsg); + end + + //Check the size of inequality constraint which should be equal to the number of variables + if ( size(A,2) ~= nbVar & size(A,2) ~= 0) then + errmsg = msprintf(gettext("%s: The number of columns in A must be the same as the number of columns in C"), "lsqlin"); + error(errmsg); + end + + //Check the size of equality constraint which should be equal to the number of variables + if ( size(Aeq,2) ~= nbVar & size(Aeq,2) ~= 0 ) then + errmsg = msprintf(gettext("%s: The number of columns in Aeq must be the same as the number of columns in C"), "lsqlin"); + error(errmsg); + end + + //Check the size of Lower Bound which should be equal to the number of variables + if ( size(lb,1) ~= nbVar) then + errmsg = msprintf(gettext("%s: The Lower Bound is not equal to the number of variables"), "lsqlin"); + error(errmsg); + end + + //Check the size of Upper Bound which should equal to the number of variables + if ( size(ub,1) ~= nbVar) then + errmsg = msprintf(gettext("%s: The Upper Bound is not equal to the number of variables"), "lsqlin"); + error(errmsg); + end + + //Check the size of constraints of Lower Bound which should equal to the number of constraints + if ( size(b,1) ~= nbConInEq & size(b,1) ~= 0) then + errmsg = msprintf(gettext("%s: The number of rows in A must be the same as the number of elements of b"), "lsqlin"); + error(errmsg); + end + + //Check the size of constraints of Upper Bound which should equal to the number of constraints + if ( size(beq,1) ~= nbConEq & size(beq,1) ~= 0) then + errmsg = msprintf(gettext("%s: The number of rows in Aeq must be the same as the number of elements of beq"), "lsqlin"); + error(errmsg); + end + + //Check the size of initial of variables which should equal to the number of variables + if ( size(x0,1) ~= nbVar) then + warnmsg = msprintf(gettext("%s: Ignoring initial guess of variables as it is not equal to the number of variables"), "lsqlin"); + warning(warnmsg); + x0 = repmat(0,nbVar,1); + end + + //Check if the user gives a matrix instead of a vector + + if ((size(d,1)~=1)& (size(d,2)~=1)) then + errmsg = msprintf(gettext("%s: d should be a vector"), "lsqlin"); + error(errmsg); + end + + if (size(lb,1)~=1)& (size(lb,2)~=1) then + errmsg = msprintf(gettext("%s: Lower Bound should be a vector"), "lsqlin"); + error(errmsg); + end + + if (size(ub,1)~=1)& (size(ub,2)~=1) then + errmsg = msprintf(gettext("%s: Upper Bound should be a vector"), "lsqlin"); + error(errmsg); + end + + if (nbConInEq) then + if ((size(b,1)~=1)& (size(b,2)~=1)) then + errmsg = msprintf(gettext("%s: Constraint Lower Bound should be a vector"), "lsqlin"); + error(errmsg); + end + end + + if (nbConEq) then + if (size(beq,1)~=1)& (size(beq,2)~=1) then + errmsg = msprintf(gettext("%s: Constraint should be a vector"), "lsqlin"); + error(errmsg); + end + end + + for i = 1:nbConInEq + if (b(i) == -%inf) + errmsg = msprintf(gettext("%s: Value of b can not be negative infinity"), "lsqlin"); + error(errmsg); + end + end + + for i = 1:nbConEq + if (beq(i) == -%inf) + errmsg = msprintf(gettext("%s: Value of beq can not be negative infinity"), "lsqlin"); + error(errmsg); + end + end + + for i = 1:nbVar + if(lb(i)>ub(i)) + errmsg = msprintf(gettext("%s: Problem has inconsistent variable bounds"), "lsqlin"); + error(errmsg); + end + end + + //Converting it into Quadratic Programming Problem + + H = C'*C; + f = [-C'*d]'; + op_add = d'*d; + lb = lb'; + ub = ub'; + x0 = x0'; + conMatrix = [Aeq;A]; + nbCon = size(conMatrix,1); + conLB = [beq; repmat(-%inf,nbConInEq,1)]'; + conUB = [beq;b]' ; + [xopt,fopt,status,iter,Zl,Zu,lmbda] = solveqp(nbVar,nbCon,H,f,conMatrix,conLB,conUB,lb,ub,x0,options); + + xopt = xopt'; + residual = d-C*xopt; + resnorm = residual'*residual; + exitflag = status; + output = struct("Iterations" , [], .. + "ConstrViolation" ,[]); + output.Iterations = iter; + output.ConstrViolation = max([0;norm(Aeq*xopt-beq, 'inf');(lb'-xopt);(xopt-ub');(A*xopt-b)]); + lambda = struct("lower" , [], .. + "upper" , [], .. + "eqlin" , [], .. + "ineqlin" , []); + + lambda.lower = Zl; + lambda.upper = Zu; + lambda.eqlin = lmbda(1:nbConEq); + lambda.ineqlin = lmbda(nbConEq+1:nbCon); + + select status + case 0 then + printf("\nOptimal Solution Found.\n"); + case 1 then + printf("\nMaximum Number of Iterations Exceeded. Output may not be optimal.\n"); + case 2 then + printf("\nMaximum CPU Time exceeded. Output may not be optimal.\n"); + case 3 then + printf("\nStop at Tiny Step\n"); + case 4 then + printf("\nSolved To Acceptable Level\n"); + case 5 then + printf("\nConverged to a point of local infeasibility.\n"); + case 6 then + printf("\nStopping optimization at current point as requested by user.\n"); + case 7 then + printf("\nFeasible point for square problem found.\n"); + case 8 then + printf("\nIterates diverging; problem might be unbounded.\n"); + case 9 then + printf("\nRestoration Failed!\n"); + case 10 then + printf("\nError in step computation (regularization becomes too large?)!\n"); + case 12 then + printf("\nProblem has too few degrees of freedom.\n"); + case 13 then + printf("\nInvalid option thrown back by Ipopt\n"); + case 14 then + printf("\nNot enough memory.\n"); + case 15 then + printf("\nINTERNAL ERROR: Unknown SolverReturn value - Notify Ipopt Authors.\n"); + else + printf("\nInvalid status returned. Notify the Toolbox authors\n"); + break; + end + +endfunction diff --git a/macros/lsqlin.bin b/macros/lsqlin.bin Binary files differnew file mode 100644 index 0000000..7baf7be --- /dev/null +++ b/macros/lsqlin.bin diff --git a/macros/lsqnonneg.bin b/macros/lsqnonneg.bin Binary files differnew file mode 100644 index 0000000..32620d0 --- /dev/null +++ b/macros/lsqnonneg.bin diff --git a/macros/matrix_linprog.bin b/macros/matrix_linprog.bin Binary files differnew file mode 100644 index 0000000..e3a9db3 --- /dev/null +++ b/macros/matrix_linprog.bin diff --git a/macros/mps_linprog.bin b/macros/mps_linprog.bin Binary files differnew file mode 100644 index 0000000..c0d2e3e --- /dev/null +++ b/macros/mps_linprog.bin diff --git a/macros/names b/macros/names new file mode 100644 index 0000000..36793aa --- /dev/null +++ b/macros/names @@ -0,0 +1,22 @@ +Checkdims +Checklhs +Checkrhs +Checktype +Checkvector +fgoalattain +fminbnd +fmincon +fminimax +fminunc +linprog +lsqcurvefit +lsqlin +lsqnonneg +matrix_linprog +mps_linprog +qpipopt +qpipoptmat +setOptions +symphony +symphony_call +symphonymat diff --git a/macros/qpipopt.bin b/macros/qpipopt.bin Binary files differnew file mode 100644 index 0000000..e2ba3de --- /dev/null +++ b/macros/qpipopt.bin diff --git a/macros/qpipoptmat.bin b/macros/qpipoptmat.bin Binary files differnew file mode 100644 index 0000000..b9c741f --- /dev/null +++ b/macros/qpipoptmat.bin diff --git a/macros/setOptions.bin b/macros/setOptions.bin Binary files differnew file mode 100644 index 0000000..8d23e73 --- /dev/null +++ b/macros/setOptions.bin diff --git a/macros/symphony.bin b/macros/symphony.bin Binary files differnew file mode 100644 index 0000000..2c3a43a --- /dev/null +++ b/macros/symphony.bin diff --git a/macros/symphony_call.bin b/macros/symphony_call.bin Binary files differnew file mode 100644 index 0000000..5e0d5e1 --- /dev/null +++ b/macros/symphony_call.bin diff --git a/macros/symphonymat.bin b/macros/symphonymat.bin Binary files differnew file mode 100644 index 0000000..7dffa2f --- /dev/null +++ b/macros/symphonymat.bin |