diff options
author | Harpreet | 2015-12-29 00:38:48 +0530 |
---|---|---|
committer | Harpreet | 2015-12-29 00:38:48 +0530 |
commit | eb9ca1191c94059cd7adcf69805906c809fe9712 (patch) | |
tree | f98e728341812b8a0eb46aa4159b062a22225f05 /macros/lsqlin.sci | |
parent | 0eee95dfb1edec6ce52ec3065a3adb1bf169c9f9 (diff) | |
download | FOSSEE-Optimization-toolbox-eb9ca1191c94059cd7adcf69805906c809fe9712.tar.gz FOSSEE-Optimization-toolbox-eb9ca1191c94059cd7adcf69805906c809fe9712.tar.bz2 FOSSEE-Optimization-toolbox-eb9ca1191c94059cd7adcf69805906c809fe9712.zip |
Bugs fixed 4
Diffstat (limited to 'macros/lsqlin.sci')
-rw-r--r-- | macros/lsqlin.sci | 108 |
1 files changed, 60 insertions, 48 deletions
diff --git a/macros/lsqlin.sci b/macros/lsqlin.sci index 08554e1..fba036d 100644 --- a/macros/lsqlin.sci +++ b/macros/lsqlin.sci @@ -22,22 +22,22 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin (varargin) // [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin( ... ) // // Parameters - // C : a matrix of double, represents the multiplier of the solution x in the expression C*x - d. C is M-by-N, where M is the number of equations, and N is the number of elements of x. - // d : a vector of double, represents the additive constant term in the expression C*x - d. d is M-by-1, where M is the number of equations. + // 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 vector of double, represents the linear coefficients in the inequality constraints // b : a vector of double, represents the linear coefficients in the inequality constraints // Aeq : a matrix of double, represents the linear coefficients in the equality constraints // beq : a vector of double, represents the linear coefficients in the equality constraints - // LB : a vector of double, contains lower bounds of the variables. - // UB : a vector of double, contains upper bounds of the variables. + // 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 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 C*x-d. - // exitflag : Integer identifying the reason the algorithm terminated. - // output : Structure containing information about the optimization. Right now it contains number of iteration. - // lambda : Structure containing the Lagrange multipliers at the solution x (separated by constraint type).It contains lower, upper and linear equality, inequality constraints. + // exitflag : Integer identifying the reason the algorithm terminated. It could be 0, 1 or 2 etc. i.e. Optimal, Maximum Number of Iterations Exceeded, CPU time exceeded. Other flags one can see in the lsqlin macro. + // output : Structure containing information about the optimization. This version only contains number of iterations. + // lambda : Structure containing the Lagrange multipliers at the solution x (separated by constraint type).It contains lower, upper bound multiplier and linear equality, inequality constraints. // // Description // Search the minimum of a constrained linear least square problem specified by : @@ -45,14 +45,14 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin (varargin) // <latex> // \begin{eqnarray} // &\mbox{min}_{x} - // & 1/2||C*x - d||_2^2 \\ - // & \text{subject to} & A*x \leq b \\ - // & & Aeq*x = beq \\ + // & 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> // - // We are calling IPOpt for solving the linear least square problem, IPOpt is a library written in C++. + // The routine calls Ipopt for solving the linear least square problem, Ipopt is a library written in C++. // // Examples // //A simple linear least square example @@ -76,7 +76,7 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin (varargin) // // Press ENTER to continue // // Examples - // //A basic example for equality, inequality and bounds + // //A basic example for equality, inequality constraints and variable bounds // C = [0.9501 0.7620 0.6153 0.4057 // 0.2311 0.4564 0.7919 0.9354 // 0.6068 0.0185 0.9218 0.9169 @@ -111,11 +111,22 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin (varargin) 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); + nbVar = size(C,2); if ( rhs<5 ) then Aeq = [] @@ -126,11 +137,11 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin (varargin) end if ( rhs<7 ) then - LB = repmat(-%inf,nbVar,1); - UB = repmat(%inf,nbVar,1); + lb = repmat(-%inf,nbVar,1); + ub = repmat(%inf,nbVar,1); else - LB = varargin(7); - UB = varargin(8); + lb = varargin(7); + ub = varargin(8); end @@ -146,12 +157,12 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin (varargin) param =varargin(10); end - if (size(LB,2)==0) then - LB = repmat(-%inf,nbVar,1); + if (size(lb,2)==0) then + lb = repmat(-%inf,nbVar,1); end - if (size(UB,2)==0) then - UB = repmat(%inf,nbVar,1); + if (size(ub,2)==0) then + ub = repmat(%inf,nbVar,1); end if (type(param) ~= 15) then @@ -193,12 +204,12 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin (varargin) d=d'; end - if (size(LB,2)== [nbVar]) then - LB = LB'; + if (size(lb,2)== [nbVar]) then + lb = lb'; end - if (size(UB,2)== [nbVar]) then - UB = UB'; + if (size(ub,2)== [nbVar]) then + ub = ub'; end if (size(b,2)==nbConInEq) then @@ -221,7 +232,7 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin (varargin) //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 elements of d"), "lsqlin"); + 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 @@ -232,20 +243,20 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin (varargin) end //Check the size of Lower Bound which should be equal to the number of variables - if ( size(LB,1) ~= nbVar) then + 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 + 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 elementsof b"), "lsqlin"); + 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 @@ -259,6 +270,7 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin (varargin) 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 @@ -268,12 +280,12 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin (varargin) error(errmsg); end - if (size(LB,1)~=1)& (size(LB,2)~=1) then + 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 + if (size(ub,1)~=1)& (size(ub,2)~=1) then errmsg = msprintf(gettext("%s: Upper Bound should be a vector"), "lsqlin"); error(errmsg); end @@ -294,31 +306,31 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin (varargin) for i = 1:nbConInEq if (b(i) == -%inf) - errmsg = msprintf(gettext("%s: Value of b can not be negative infinity"), "qpipoptmat"); + 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"), "qpipoptmat"); + errmsg = msprintf(gettext("%s: Value of beq can not be negative infinity"), "lsqlin"); error(errmsg); end end //Converting it into Quadratic Programming Problem - Q = C'*C; - p = [-C'*d]'; + H = C'*C; + f = [-C'*d]'; op_add = d'*d; - LB = LB'; - UB = UB'; + 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,Q,p,conMatrix,conLB,conUB,LB,UB,x0,options); + [xopt,fopt,status,iter,Zl,Zu,lmbda] = solveqp(nbVar,nbCon,H,f,conMatrix,conLB,conUB,lb,ub,x0,options); xopt = xopt'; residual = -1*(C*xopt-d); @@ -326,15 +338,15 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin (varargin) exitflag = status; output = struct("Iterations" , []); output.Iterations = iter; - lambda = struct("lower" , [], .. - "upper" , [], .. - "eqlin" , [], .. + lambda = struct("lower" , [], .. + "upper" , [], .. + "eqlin" , [], .. "ineqlin" , []); - - lambda.lower = Zl; - lambda.upper = Zu; - lambda.eqlin = lmbda(1:nbConEq); - lambda.ineqlin = lmbda(nbConEq+1:nbCon); + + lambda.lower = Zl; + lambda.upper = Zu; + lambda.eqlin = lmbda(1:nbConEq); + lambda.ineqlin = lmbda(nbConEq+1:nbCon); select status case 0 then @@ -362,11 +374,11 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin (varargin) case 12 then printf("\nProblem has too few degrees of freedom.\n"); case 13 then - printf("\nInvalid option thrown back by IPOpt\n"); + 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"); + printf("\nINTERNAL ERROR: Unknown SolverReturn value - Notify Ipopt Authors.\n"); else printf("\nInvalid status returned. Notify the Toolbox authors\n"); break; |