- << Symphony Toolbox
+ << linprog
|
diff --git a/help/en_US/scilab_en_US_help/section_19f4f1e5726c01d683e8b82be0a7e910.html b/help/en_US/scilab_en_US_help/section_19f4f1e5726c01d683e8b82be0a7e910.html
index a79bad0..a03f091 100644
--- a/help/en_US/scilab_en_US_help/section_19f4f1e5726c01d683e8b82be0a7e910.html
+++ b/help/en_US/scilab_en_US_help/section_19f4f1e5726c01d683e8b82be0a7e910.html
@@ -31,7 +31,37 @@
Symphony Toolbox
-- lsqlin — Solves a linear quadratic problem.
+- fgoalattain — Solves a multiobjective goal attainment problem
+
+
+
+
+
+- fminbnd — Solves a multi-variable optimization problem on a bounded interval
+
+
+
+
+
+- fmincon — Solves a multi-variable constrainted optimization problem
+
+
+
+
+
+- fminunc — Solves a multi-variable unconstrainted optimization problem
+
+
+
+
+
+- linprog — Solves a linear programming problem.
+
+
+
+
+
+- lsqlin — Solves a linear quadratic problem.
diff --git a/jar/scilab_en_US_help.jar b/jar/scilab_en_US_help.jar
index 0289144..970425b 100644
Binary files a/jar/scilab_en_US_help.jar and b/jar/scilab_en_US_help.jar differ
diff --git a/macros/fgoalattain.bin b/macros/fgoalattain.bin
new file mode 100644
index 0000000..1476d8e
Binary files /dev/null and b/macros/fgoalattain.bin differ
diff --git a/macros/fgoalattain.sci b/macros/fgoalattain.sci
new file mode 100644
index 0000000..5f519b4
--- /dev/null
+++ b/macros/fgoalattain.sci
@@ -0,0 +1,514 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2015 - IIT Bombay - FOSSEE
+//
+// Authors: Prajwala TM,Sheetal Shalini
+// Organization: FOSSEE, IIT Bombay
+// Email: prajwala.tm@gmail.com,sheetalsh456@gmail.com
+// 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
+
+function [x,fval,attainfactor,exitflag,output,lambda] = fgoalattain(varargin)
+ // Solves a multiobjective goal attainment problem
+ //
+ // Calling Sequence
+ // x = fgoalattain(fun,x0,goal,weight)
+ // x = fgoalattain(fun,x0,goal,weight,A,b)
+ // x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq)
+ // x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq,lb,ub)
+ // x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq,lb,ub,nonlcon)
+ // x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq,lb,ub,nonlcon,options)
+ // [x,fval] = fgoalattain(...)
+ // [x,fval,attainfactor] = fgoalattain(...)
+ // [x,fval,attainfactor,exitflag] = fgoalattain(...)
+ // [x,fval,attainfactor,exitflag,output] = fgoalattain(...)
+ // [x,fval,attainfactor,exitflag,output,lambda] = fgoalattain(...)
+ //
+ // Parameters
+ // fun: a function that accepts a vector x and returns a vector F
+ // x0: a nx1 or 1xn matrix of double, where n is the number of variables.
+ // The initial guess for the optimization algorithm
+ // A: a nil x n matrix of double, where n is the number of variables and
+ // nil is the number of linear inequalities.
+ //
+ // b: a nil x 1 matrix of double, where nil is the number of linear
+ // inequalities
+ // Aeq: a nel x n matrix of double, where n is the number of variables
+ // and nel is the number of linear equalities.
+ // beq: a nel x 1 matrix of double, where nel is the number of linear
+ // equalities
+ // lb: a nx1 or 1xn matrix of double, where n is the number of variables.
+ // The lower bound for x. If lb==[], then the lower bound is
+ // automatically set to -inf
+ // ub: a nx1 or 1xn matrix of double, where n is the number of variables.
+ // The upper bound for x. If ub==[], then the upper bound is
+ // automatically set to +inf
+ // nonlcon: a function, the nonlinear constraints
+ // options : a list, containing the option for user to specify. See below for details.
+ // x: a nx1 matrix of double, the computed solution of the optimization problem
+ // fval: a vector of double, the value of functions at x
+ // attainfactor: The amount of over- or underachievement of the goals,γ at the solution.
+ // exitflag: a 1x1 matrix of floating point integers, the exit status
+ // output: a struct, the details of the optimization process
+ // lambda: a struct, the Lagrange multipliers at optimum
+ //
+ // Description
+ // fgoalattain solves the goal attainment problem, which is one formulation for minimizing a multiobjective optimization problem.
+ // Finds the minimum of a problem specified by:
+ // Minimise Y such that
+ //
+ //
+ //\begin{eqnarray}
+ //\mbox{min}_{x,\gamma} & f(x)-weight \ast \gamma \leq goal \\
+ //\mbox{subject to} & c(x) \leq 0 \\
+ // & c_{eq}(x) = 0 \\
+ // & Ax \leq b \\
+ // & A_{eq} x = b_{eq} \\
+ // & lb \leq x \leq ub
+ //\end{eqnarray}
+ //
+ //
+ // The solver makes use of fmincon to find the minimum.
+ //
+ // The fgoalattain finds out the maximum value of Y for the objectives evaluated at the starting point and
+ // adds that as another variable to the vector x
+ // This is passed to the fmincon function to get the optimised value of Y
+ // Hence, the algorithm used mainly is "ipopt" to obtain the optimum solution
+ // The relations between f(x), Y, weights and goals are added as additional non-linear inequality constraints
+ //
+ // 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.
+ //
+ // Syntax : options= list("MaxIter", [---], "CpuTime", [---], "GradObj", ---, "GradCon", ---);
+ // MaxIter : a Scalar, containing the Maximum Number of Iteration that the solver should take.
+ // CpuTime : a Scalar, containing the Maximum amount of CPU Time that the solver should take.
+ // GradObj : a function, representing the gradient function of the Objective in Vector Form.
+ // GradCon : a function, representing the gradient of the Non-Linear Constraints (both Equality and Inequality) of the problem. It is declared in such a way that gradient of non-linear inequality constraints are defined first as a separate Matrix (cg of size m2 X n or as an empty), followed by gradient of non-linear equality constraints as a separate Matrix (ceqg of size m2 X n or as an empty) where m2 & m3 are number of non-linear inequality and equality constraints respectively.
+ // Default Values : options = list("MaxIter", [3000], "CpuTime", [600]);
+ //
+ //
+ // By default, the gradient options for fminimax are turned off and and fmincon does the gradient opproximation of minmaxObjfun. In case the GradObj option is off and GradConstr option is on, fminimax approximates minmaxObjfun gradient using numderivative toolbox.
+ //
+ // If we can provide exact gradients, we should do so since it improves the convergence speed of the optimization algorithm.
+ //
+ // Furthermore, we must enable the "GradObj" option with the statement :
+ //
+ // minimaxOptions = list("GradObj",fGrad);
+ //
+ // This will let fminimax know that the exact gradient of the objective function is known, so that it can change the calling sequence to the objective function. Note that, fGrad should be mentioned in the form of N x n where n is the number of variables, N is the number of functions in objective function.
+ //
+ // The constraint function must have header :
+ //
+ // [c, ceq] = confun(x)
+ //
+ // where x is a n x 1 matrix of dominmaxUbles, c is a 1 x nni matrix of doubles and ceq is a 1 x nne matrix of doubles (nni : number of nonlinear inequality constraints, nne : number of nonlinear equality constraints).
+ // On input, the variable x contains the current point and, on output, the variable c must contain the nonlinear inequality constraints and ceq must contain the nonlinear equality constraints.
+ //
+ // By default, the gradient options for fminimax are turned off and and fmincon does the gradient opproximation of confun. In case the GradObj option is on and GradCons option is off, fminimax approximates confun gradient using numderivative toolbox.
+ //
+ // If we can provide exact gradients, we should do so since it improves the convergence speed of the optimization algorithm.
+ //
+ // Furthermore, we must enable the "GradCon" option with the statement :
+ //
+ // minimaxOptions = list("GradCon",confunGrad);
+ //
+ // This will let fminimax know that the exact gradient of the objective function is known, so that it can change the calling sequence to the objective function.
+ //
+ // The constraint derivative function must have header :
+ //
+ // [dc,dceq] = confungrad(x)
+ //
+ // where dc is a nni x n matrix of doubles and dceq is a nne x n matrix of doubles.
+ //
+ // The exitflag allows to know the status of the optimization which is given back by Ipopt.
+ //
+ // exitflag=0 : Optimal Solution Found
+ // exitflag=1 : Maximum Number of Iterations Exceeded. Output may not be optimal.
+ // exitflag=2 : Maximum amount of CPU Time exceeded. Output may not be optimal.
+ // exitflag=3 : Stop at Tiny Step.
+ // exitflag=4 : Solved To Acceptable Level.
+ // exitflag=5 : Converged to a point of local infeasibility.
+ //
+ //
+ // 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.
+ //
+ // output.Iterations: The number of iterations performed during the search
+ // output.Cpu_Time: The total cpu-time spend during the search
+ // output.Objective_Evaluation: The number of Objective Evaluations performed during the search
+ // output.Dual_Infeasibility: The Dual Infeasiblity of the final soution
+ //
+ //
+ // 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.
+ //
+ // lambda.lower: The Lagrange multipliers for the lower bound constraints.
+ // lambda.upper: The Lagrange multipliers for the upper bound constraints.
+ // lambda.eqlin: The Lagrange multipliers for the linear equality constraints.
+ // lambda.ineqlin: The Lagrange multipliers for the linear inequality constraints.
+ // lambda.eqnonlin: The Lagrange multipliers for the non-linear equality constraints.
+ // lambda.ineqnonlin: The Lagrange multipliers for the non-linear inequality constraints.
+ //
+ //
+ // Examples
+ // function f1 = fun(x)
+ // f1(1)=2*x(1)*x(1)+x(2)*x(2)-48*x(1)-40*x(2)+304
+ // f1(2)=-x(1)*x(1)-3*x(2)*x(2)
+ // f1(3)=x(1)+3*x(2)-18
+ // f1(4)=-x(1)-x(2)
+ // f1(5)=x(1)+x(2)-8
+ // endfunction
+ // x0=[-1,1];
+ //
+ // goal=[-5,-3,-2,-1,-4];
+ // weight=abs(goal)
+ // //xopt = [-0.0000011 -63.999998 -2.0000002 -8 3.485D-08]
+ // //fval = [4 3.99]
+ //
+ // //Run fgoalattain
+ // [xopt,fval,attainfactor,exitflag,output,lambda]=fgoalattain(fun,x0,goal,weight)
+ //
+ // Authors
+ // Prajwala TM, Sheetal Shalini , 2015
+
+ // Check number of input and output arguments
+ [gattainLhs,gattainRhs] = argn()
+ fgoalattainCheckrhs("fgoalattain", gattainRhs, [4 6 8 10 11 12])
+ fgoalattainChecklhs("fgoalattain", gattainLhs, 1:6)
+
+ // initialisation of fun
+ gattainObjfun = varargin(1)
+ fgoalattainChecktype("fgoalattain", gattainObjfun, "fun", 1, "function")
+
+ // initialisation of x0
+ gattainStartpoint = varargin(2)
+ fgoalattainChecktype("fgoalattain", gattainStartpoint, "x0", 2, "constant")
+
+ gattainNumvar = size(gattainStartpoint,"*")
+ fgoalattainCheckvector("fgoalattain", gattainStartpoint, "x0", 2, gattainNumvar)
+ gattainStartpoint = gattainStartpoint(:)
+
+ // initialisation of goal
+ goal=varargin(3)
+ fgoalattainChecktype("fgoalattain",goal,"goal",3,"constant")
+
+ // initialisation of weight
+ weight=varargin(4)
+ fgoalattainChecktype("fgoalattain",weight,"weight",4,"constant")
+
+ //initialisation of A and b
+ if(gattainRhs < 5) then
+ gattainA = []
+ gattainB = []
+ else
+ gattainA = varargin(5)
+ gattainB = varargin(6)
+ end
+
+ fgoalattainChecktype("fgoalattain", gattainA, "A", 5, "constant")
+ fgoalattainChecktype("fgoalattain", gattainB, "b", 6, "constant")
+
+ gattainNumrowA = size(gattainA,"r")
+ if(gattainA <> []) then
+ fgoalattainCheckdims("fgoalattain", gattainA, "A", 5, [gattainNumrowA gattainNumvar])
+ fgoalattainCheckvector("fgoalattain", gattainB, "b", 6, gattainNumrowA)
+ gattainB = gattainB(:)
+ end
+
+ //initialisation of Aeq and beq
+ if(gattainRhs < 7) then
+ gattainAeq = []
+ gattainBeq = []
+ else
+ gattainAeq = varargin(7)
+ gattainBeq = varargin(8)
+ end
+
+ fgoalattainChecktype("fgoalattain", gattainAeq, "Aeq", 7, "constant")
+ fgoalattainChecktype("fgoalattain", gattainBeq, "beq", 8, "constant")
+
+ gattainNumrowAeq = size(gattainAeq,"r")
+ if(gattainAeq <> []) then
+ fgoalattainCheckdims("fgoalattain", gattainAeq, "Aeq", 7, [gattainNumrowAeq gattainNumvar])
+ fgoalattainCheckvector("fgoalattain", gattainBeq, "beq", 8, gattainNumrowAeq)
+ gattainBeq = gattainBeq(:)
+ end
+
+ // initialisation of lb and ub
+ if(gattainRhs < 9) then
+ gattainLb = []
+ gattainUb = []
+ else
+ gattainLb = varargin(9)
+ gattainUb = varargin(10)
+ end
+
+ fgoalattainChecktype("fgoalattain", gattainLb, "lb", 9, "constant")
+ fgoalattainChecktype("fgoalattain", gattainUb, "ub", 10, "constant")
+
+ // Check dimensions of lb and ub
+ if(gattainLb <> []) then
+ fgoalattainCheckvector("fgoalattain", gattainLb, "lb", 9, gattainNumvar)
+ gattainLb = gattainLb(:)
+ end
+
+ if(gattainUb <> []) then
+ fgoalattainCheckvector("fgoalattain", gattainUb, "ub", 10, gattainNumvar)
+ gattainUb = gattainUb(:)
+ end
+
+ // Initialisation of nonlcon
+ function [c,ceq] = constr(z)
+ c = []
+ ceq = []
+ endfunction
+
+ if(gattainRhs < 11) then
+ gattainNonlinfun = constr
+ else
+ gattainNonlinfun = varargin(11)
+ end
+
+ fgoalattainChecktype("fgoalattain", gattainNonlinfun, "nonlcon", 11, "function")
+
+ // initialisation of default options
+ if(gattainRhs < 12) then
+ gattainUserOptions = list()
+ else
+ gattainUserOptions = varargin(12)
+ end
+
+ //If gattainOptions is entered then checking its type for 'list'
+ if (type(gattainUserOptions) ~= 15) then
+ errmsg = msprintf(gettext("%s: gattainOptions (10th parameter) should be a list"), "fgoalattain");
+ error(errmsg);
+ end
+
+ //If minimaxOptions is entered then checking whether even number of entires are entered
+ if (modulo(size(gattainUserOptions),2)) then
+ errmsg = msprintf(gettext("%s: Size of gattainOptions (list) should be even"), "fgoalattain");
+ error(errmsg);
+ end
+
+ //Flags to check whether Gradient is "ON"/"OFF" & Hessian is "ON"/"OFF"
+ flag1=0;
+ flag2=0;
+ fgaMaxIter = 3000;
+ fgaCPU = 600;
+ gattainFGrad=[];
+ gattainCGrad=[];
+
+ //To check the User Entry for Options and storing it
+ for i = 1:(size(gattainUserOptions))/2
+ select gattainUserOptions(2*i-1)
+ case "MaxIter" then
+ fgaMaxIter = gattainUserOptions(2*i); //Setting the Maximum Iteration as per user entry
+
+ case "CpuTime" then
+ fgaCPU = gattainUserOptions(2*i); //Setting the Maximum CPU Time as per user entry
+
+ case "GradObj" then
+ flag1=1;
+ gattainFGrad = gattainUserOptions(2*i);
+
+ case "GradCon" then
+ flag2=1;
+ gattainCGrad = gattainUserOptions(2*i);
+
+ else
+ errmsg = msprintf(gettext("%s: Unrecognized gattainUserOptionseter name ''%s''."), "fminimax", gattainUserOptions(2*i-1));
+ error(errmsg)
+ end
+ end
+
+ // Checking if minmaxFGrad and minmaxCGrad are functions
+ if (flag1==1) then
+ if (type(gattainFGrad) ~= 11 & type(gattainFGrad) ~= 13) then
+ errmsg = msprintf(gettext("%s: Expected function for Gradient of Objective"), "fminimax");
+ error(errmsg);
+ end
+ end
+
+ if (flag2==1) then
+ if (type(gattainCGrad) ~= 11 & type(gattainCGrad) ~= 13) then
+ errmsg = msprintf(gettext("%s: Expected function for Gradient of Nonlinfun"), "fminimax");
+ error(errmsg);
+ end
+ end
+
+ gattainObjfunval = gattainObjfun(gattainStartpoint)
+ gattainObjfunval=gattainObjfunval(:)
+ goal=goal(:)
+ weight=weight(:)
+ gaVal=[]
+
+ // appending the gamma value as another variable
+ for i=1:size(gattainObjfunval,'r')
+ if(weight(i)<>0) then
+ gaVal(i)=((gattainObjfunval(i)-goal(i))/weight(i))
+ end
+ end
+
+ gattainStartpoint(gattainNumvar+1)=max(gaVal)
+
+ if(gattainA <> []) then
+ gattainA = [gattainA, zeros(gattainNumrowA,1)]
+ end
+ if(gattainAeq <> []) then
+ gattainAeq = [gattainAeq, zeros(gattainNumrowAeq,1)]
+ end
+ if(gattainLb <> []) then
+ gattainLb(gattainNumvar+1) = -%inf
+ end
+ if(gattainUb <> []) then
+ gattainUb(gattainNumvar+1) = +%inf
+ end
+
+ // function handle defining the additional inequalities
+ function temp = gattainAddIneq(z)
+ gaVar = gattainObjfun(z)
+ gattainAddIneqWithWt = []
+ gattainAddIneqWitoutWt = []
+ for i = 1:size(gaVar,'r')
+ if(weight(i) <> 0) then
+ gattainAddIneqWithWt = [gattainAddIneqWithWt; ( (gaVar(i)-goal(i))/weight(i) )]
+ else
+ gattainAddIneqWitoutWt = [gattainAddIneqWitoutWt; gaVar(i)-goal(i)]
+ end
+ end
+ temp = [gattainAddIneqWithWt - z(gattainNumvar+1); gattainAddIneqWitoutWt]
+ endfunction
+
+ // function handle defining new objective function
+ function newfunc = newObjfun(z)
+ newfunc = z(gattainNumvar+1)
+ endfunction
+
+ // function handle defining add_ineq derivative using numderivative
+ function func = gattainIneqDer(z)
+ func = numderivative(gattainAddIneq,z)
+ endfunction
+
+ // function handle defining nonlcon derivative using numderivative
+ function [dc,dceq] = gattainNonlinDer(z)
+
+ // function handle extracting c and ceq components from nonlcon
+ function foo = gattainC(z)
+ [foo,tmp1] = gattainNonlinfun(z)
+ foo = foo'
+ endfunction
+
+ function foo = gattainCEQ(z)
+ [tmp1,foo] = gattainNonlinfun(z)
+ foo = foo'
+ endfunction
+
+ dc = numderivative(gattainC,z)
+ dceq = numderivative(gattainCEQ,z)
+ endfunction
+
+ // function handle defining new nonlcon function
+ function [nc,nceq] = newNonlinfun(z)
+ [nc,nceq] = gattainNonlinfun(z)
+ tmp = [gattainAddIneq(z)]'
+ nc = [nc, tmp]
+ endfunction
+
+ function [dnc,dnceq] = newCGrad(z)
+ // check if "GradCon" option is turned on
+ // if "GradCon" is turned on, use it
+ if(flag2 == 1) then
+ [dnc,dnceq] = gattainCGrad(z)
+ dnc = [dnc, zeros(size(dnc,'r'),1)]
+ dnceq = [dnceq, zeros(size(dnceq,'r'),1)]
+ // else, calculate it using finite differences
+ else
+ [dnc,dnceq] = gattainNonlinDer(z)
+ end
+
+ // check if "GradObj" option is turned on
+ // if "GradObj" is turned on, use it
+ if(flag1 == 1) then
+ derObjfun = gattainFGrad(z)
+ tmp1 = []
+ tmp2 = []
+ for i = 1:size(gattainObjfun(gattainStartpoint),'r')
+ if weight(i) <> 0 then
+ gaVal = [derObjfun(i,:)/weight(i) , -1]
+ tmp1 = [ tmp1; gaVal ]
+ else
+ gaVal = [derObjfun(i,:) , 0]
+ tmp2 = [ tmp2; gaVal ]
+ end
+ end
+
+ dnc = [ dnc; tmp1; tmp2 ]
+ // else, calculate it using finite differences
+ else
+ deraddineq = gattainIneqDer(z)
+ dnc = [dnc; deraddineq]
+ end
+ endfunction
+
+
+ // disp(gattainStartpoint)
+ // disp(gattainObjfun(gattainStartpoint))
+ // disp(newObjfun(gattainStartpoint))
+ // disp(goal)
+ // disp(weight)
+ //
+ // disp(gattainA)
+ // disp(gattainB)
+ // disp(gattainAeq)
+ // disp(gattainBeq)
+ // disp(gattainLb)
+ // disp(gattainUb)
+ //
+ // [f,g] = gattainNonlinfun(gattainStartpoint)
+ // disp(f)
+ // disp(g)
+ // [f,g] = newNonlinfun(gattainStartpoint)
+ // disp(f)
+ // disp(g)
+ //
+ // // function foo = CgattainC(z)
+ // // [foo,tmp1] = newNonlinfun(z)
+ // // endfunction
+ // //
+ // // function foo = CgattainCEQ(z)
+ // // [tmp1,foo] = newNonlinfun(z)
+ // // endfunction
+ // //
+ // // // function handle defining gattainNonlinfun derivative using numderivative
+ // // function [dc,dceq] = derrNonlinApp(z)
+ // // dc = numderivative(CgattainC,z)
+ // // dceq = numderivative(CgattainCEQ,z)
+ // // endfunction
+ //
+ // // [f,g] = derrNonlinApp(gattainStartpoint)
+ // // disp(f)
+ // // disp(g)
+ //
+ // [f,g] = newCGrad(gattainStartpoint)
+ // disp(f)
+ // disp(g)
+
+ //to be passed as options to fmincon
+ if (flag1 == 1 | flag2 == 1) then
+ gattainPassOptions = list("MaxIter", fgaMaxIter, "CpuTime", fgaCPU, "GradCon", newCGrad)
+ [x,attainfactor,exitflag,output,lambda] = fmincon(newObjfun,gattainStartpoint,gattainA,gattainB,gattainAeq,gattainBeq,gattainLb,gattainUb,newNonlinfun,gattainPassOptions)
+ x= x(1:gattainNumvar)
+ fval = gattainObjfun(x)
+ else
+ gattainPassOptions = list("MaxIter", fgaMaxIter, "CpuTime", fgaCPU)
+ [x,attainfactor,exitflag,output,lambda] = fmincon(newObjfun,gattainStartpoint,gattainA,gattainB,gattainAeq,gattainBeq,gattainLb,gattainUb,newNonlinfun,gattainPassOptions)
+ x= x(1:gattainNumvar)
+ fval = gattainObjfun(x)
+ end
+
+endfunction
diff --git a/macros/fgoalattainFunctions.bin b/macros/fgoalattainFunctions.bin
new file mode 100644
index 0000000..eb72f4d
Binary files /dev/null and b/macros/fgoalattainFunctions.bin differ
diff --git a/macros/fgoalattainFunctions.sci b/macros/fgoalattainFunctions.sci
new file mode 100644
index 0000000..8745fa6
--- /dev/null
+++ b/macros/fgoalattainFunctions.sci
@@ -0,0 +1,76 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2015 - IIT Bombay - FOSSEE
+//
+// Authors: Prajwala TM,Sheetal Shalini
+// Organization: FOSSEE, IIT Bombay
+// Email: prajwala.tm@gmail.com,sheetalsh456@gmail.com
+// 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
+
+// Define subsidiary functions used by fgoalattain
+
+// to check if the number of input arguments is equal to the numbers mentioned in the set
+function errmsg = fgoalattainCheckrhs ( funname , rhs , rhsset )
+ errmsg = []
+ if ( and(rhs <> rhsset) ) then
+ rhsstr = strcat(string(rhsset)," ")
+ errmsg = msprintf(gettext("%s: Unexpected number of input arguments : %d provided while the number of expected input arguments should be in the set [%s]."), funname , rhs , rhsstr );
+ error(errmsg)
+ end
+endfunction
+
+// to check if the number of output arguments is equal to the numbers mentioned in the set, which is any number between 1 to 6 (both inclusive)
+function errmsg = fgoalattainChecklhs ( funname , lhs , lhsset )
+errmsg = []
+ if ( and ( lhs <> lhsset ) ) then
+ lhsstr = strcat(string(lhsset)," ")
+ errmsg = msprintf(gettext("%s: Unexpected number of output arguments : %d provided while the expected number of output arguments should be in the set [%s]."), funname , lhs , lhsstr );
+ error(errmsg)
+ end
+endfunction
+
+// Generates an error if the given variable is not of expected type.
+function errmsg = fgoalattainChecktype ( funname , var , varname , ivar , expectedtype )
+errmsg = []
+ if ( and ( typeof ( var ) <> expectedtype ) ) then
+ strexp = """" + strcat(expectedtype,""" or """) + """"
+ errmsg = msprintf(gettext("%s: Expected type [%s] for input argument %s at input #%d, but got ""%s"" instead."),funname, strexp, varname , ivar , typeof(var) );
+ error(errmsg);
+ end
+endfunction
+
+// Generates an error if the variable is not a vector.
+function errmsg = fgoalattainCheckvector ( funname , var , varname , ivar , nbval )
+errmsg = []
+ nrows = size(var,"r")
+ ncols = size(var,"c")
+ if ( nrows <> 1 & ncols <> 1 ) then
+ strcomp = strcat(string(size(var))," ")
+ errmsg = msprintf(gettext("%s: Expected a vector matrix for input argument %s at input #%d, but got [%s] instead."), funname, varname , ivar , strcomp );
+ error(errmsg)
+ end
+ if ( ( nrows == 1 & ncols <> nbval ) | ( ncols == 1 & nrows <> nbval ) ) then
+ strcomp = strcat(string(size(var))," ")
+ errmsg = msprintf(gettext("%s: Expected %d entries for input argument %s at input #%d, but current dimensions are [%s] instead."), funname, nbval , varname , ivar , strcomp );
+ error(errmsg)
+ end
+endfunction
+
+// Generates an error if the variable has not the required size.
+function errmsg = fgoalattainCheckdims ( funname , var , varname , ivar , matdims )
+[lhs,rhs]=argn()
+ fgoalattainCheckrhs ( "fgoalattainCheckdims" , rhs , 5 )
+ fgoalattainChecklhs ( "fgoalattainCheckdims" , lhs , [0 1] )
+ errmsg = []
+ if ( or ( size(var) <> matdims ) ) then
+ strexp = strcat(string(matdims)," ")
+ strcomp = strcat(string(size(var))," ")
+ errmsg = msprintf(gettext("%s: Expected size [%s] for input argument %s at input #%d, but got [%s] instead."), funname, strexp, varname , ivar , strcomp );
+ error(errmsg)
+ end
+endfunction
+
+
diff --git a/macros/fminbnd.bin b/macros/fminbnd.bin
new file mode 100644
index 0000000..76db1fb
Binary files /dev/null and b/macros/fminbnd.bin differ
diff --git a/macros/fminbnd.sci b/macros/fminbnd.sci
new file mode 100644
index 0000000..2ebaa8c
--- /dev/null
+++ b/macros/fminbnd.sci
@@ -0,0 +1,356 @@
+// 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: R.Vidyadhar & Vignesh Kannan
+// Organization: FOSSEE, IIT Bombay
+// Email: toolbox@scilab.in
+
+
+function [xopt,fopt,exitflag,output,lambda] = fminbnd (varargin)
+ // Solves a multi-variable optimization problem on a bounded interval
+ //
+ // Calling Sequence
+ // xopt = fminbnd(f,x1,x2)
+ // xopt = fminbnd(f,x1,x2,options)
+ // [xopt,fopt] = fminbnd(.....)
+ // [xopt,fopt,exitflag]= fminbnd(.....)
+ // [xopt,fopt,exitflag,output]=fminbnd(.....)
+ // [xopt,fopt,exitflag,output,lambda]=fminbnd(.....)
+ //
+ // Parameters
+ // f : a function, representing the objective function of the problem
+ // x1 : a vector, containing the lower bound of the variables of size (1 X n) or (n X 1) where 'n' is the number of Variables, where n is number of Variables
+ // x2 : a vector, containing the upper bound of the variables of size (1 X n) or (n X 1) or (0 X 0) where 'n' is the number of Variables. If x2 is empty it means upper bound is +infinity
+ // options : a list, containing the option for user to specify. See below for details.
+ // xopt : a vector of doubles, containing the the computed solution of the optimization problem.
+ // fopt : a scalar of double, containing the the function value at x.
+ // exitflag : a scalar of integer, containing the flag which denotes the reason for termination of algorithm. See below for details.
+ // output : a structure, containing the information about the optimization. See below for details.
+ // lambda : a structure, containing the Lagrange multipliers of lower bound and upper bound at the optimized point. See below for details.
+ //
+ // Description
+ // Search the minimum of a multi-variable function on bounded interval specified by :
+ // Find the minimum of f(x) such that
+ //
+ //
+ // \begin{eqnarray}
+ // &\mbox{min}_{x}
+ // & f(x)\\
+ // & \text{subject to} & x1 \ < x \ < x2 \\
+ // \end{eqnarray}
+ //
+ //
+ // The routine calls Ipopt for solving the Bounded Optimization 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.
+ //
+ // Syntax : options= list("MaxIter", [---], "CpuTime", [---], TolX, [----]);
+ // MaxIter : a Scalar, containing the Maximum Number of Iteration that the solver should take.
+ // CpuTime : a Scalar, containing the Maximum amount of CPU Time that the solver should take.
+ // TolX : a Scalar, containing the Tolerance value that the solver should take.
+ // Default Values : options = list("MaxIter", [3000], "CpuTime", [600], TolX, [1e-4]);
+ //
+ //
+ // The exitflag allows to know the status of the optimization which is given back by Ipopt.
+ //
+ // exitflag=0 : Optimal Solution Found
+ // exitflag=1 : Maximum Number of Iterations Exceeded. Output may not be optimal.
+ // exitflag=2 : Maximum CPU Time exceeded. Output may not be optimal.
+ // exitflag=3 : Stop at Tiny Step.
+ // exitflag=4 : Solved To Acceptable Level.
+ // exitflag=5 : Converged to a point of local infeasibility.
+ //
+ //
+ // 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.
+ //
+ // output.Iterations: The number of iterations performed during the search
+ // output.Cpu_Time: The total cpu-time spend during the search
+ // output.Objective_Evaluation: The number of Objective Evaluations performed during the search
+ // output.Dual_Infeasibility: The Dual Infeasiblity of the final soution
+ //
+ //
+ // 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.
+ //
+ // lambda.lower: The Lagrange multipliers for the lower bound constraints.
+ // lambda.upper: The Lagrange multipliers for the upper bound constraints.
+ //
+ //
+ // Examples
+ // //Find x in R^6 such that it minimizes:
+ // //f(x)= sin(x1) + sin(x2) + sin(x3) + sin(x4) + sin(x5) + sin(x6)
+ // //-2 <= x1,x2,x3,x4,x5,x6 <= 2
+ // //Objective function to be minimised
+ // function y=f(x)
+ // y=0
+ // for i =1:6
+ // y=y+sin(x(i));
+ // end
+ // endfunction
+ // //Variable bounds
+ // x1 = [-2, -2, -2, -2, -2, -2];
+ // x2 = [2, 2, 2, 2, 2, 2];
+ // //Options
+ // options=list("MaxIter",[1500],"CpuTime", [100],"TolX",[1e-6])
+ // //Calling Ipopt
+ // [x,fval] =fminbnd(f, x1, x2, options)
+ // // Press ENTER to continue
+ //
+ // Examples
+ // //Find x in R such that it minimizes:
+ // //f(x)= 1/x^2
+ // //0 <= x <= 1000
+ // //Objective function to be minimised
+ // function y=f(x)
+ // y=1/x^2
+ // endfunction
+ // //Variable bounds
+ // x1 = [0];
+ // x2 = [1000];
+ // //Calling Ipopt
+ // [x,fval,exitflag,output,lambda] =fminbnd(f, x1, x2)
+ // // Press ENTER to continue
+ //
+ // Examples
+ // //The below problem is an unbounded problem:
+ // //Find x in R^2 such that it minimizes:
+ // //f(x)= -[(x1-1)^2 + (x2-1)^2]
+ // //-inf <= x1,x2 <= inf
+ // //Objective function to be minimised
+ // function y=f(x)
+ // y=-((x(1)-1)^2+(x(2)-1)^2);
+ // endfunction
+ // //Variable bounds
+ // x1 = [-%inf , -%inf];
+ // x2 = [];
+ // //Options
+ // options=list("MaxIter",[1500],"CpuTime", [100],"TolX",[1e-6])
+ // //Calling Ipopt
+ // [x,fval,exitflag,output,lambda] =fminbnd(f, x1, x2, options)
+ // Authors
+ // R.Vidyadhar , Vignesh Kannan
+
+
+ //To check the number of input and output arguments
+ [lhs , rhs] = argn();
+
+ //To check the number of arguments given by the user
+ if ( rhs<3 | rhs>4 ) then
+ errmsg = msprintf(gettext("%s: Unexpected number of input arguments : %d provided while should be 3 or 4"), "fminbnd", rhs);
+ error(errmsg)
+ end
+
+ //Storing the 1st and 2nd Input Parameters
+ fun = varargin(1);
+ x1 = varargin(2);
+ x2 = varargin(3);
+
+ //Converting the User defined Objective function into Required form (Error Detectable)
+ function [y,check] = f(x)
+ if(execstr('y=fun(x)','errcatch')==32 | execstr('y=fun(x)','errcatch')==27)
+ y=zeros(1,1);
+ check=1;
+ else
+ if (isreal(y)==%F) then
+ y=zeros(1,1);
+ check=1;
+ else
+ y=fun(x);
+ check=0;
+ end
+ end
+ endfunction
+
+ //To check whether the 2nd Input argument (x1) is a vector/scalar
+ if (type(x1) ~= 1) then
+ errmsg = msprintf(gettext("%s: Expected Vector/Scalar for Lower Bound Vector (2nd Parameter)"), "fminbnd");
+ error(errmsg);
+ end
+
+ if (size(x1,2)==0) then
+ errmsg = msprintf(gettext("%s: Lower Bound (2nd Parameter) cannot be empty"), "fminbnd");
+ error(errmsg);
+ end
+
+ if (size(x1,1)~=1) & (size(x1,2)~=1) then
+ errmsg = msprintf(gettext("%s: Lower Bound (2nd Parameter) should be a vector"), "fminbnd");
+ error(errmsg);
+ elseif (size(x1,2)==1) then
+ x1=x1;
+ elseif (size(x1,1)==1) then
+ x1=x1';
+ end
+ s=size(x1);
+
+ //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");
+ error(errmsg);
+ end
+
+ //To check for the correct size and data of x2 (3rd paramter) and convert it to a column vector as required by Ipopt
+ if (size(x2,2)==0) then
+ x2 = repmat(%inf,s(1),1);
+ end
+
+
+ if (size(x2,1)~=1) & (size(x2,2)~=1) then
+ errmsg = msprintf(gettext("%s: Upper Bound (3rd Parameter) should be a vector"), "fminbnd");
+ error(errmsg);
+ elseif(size(x2,1)~=s(1) & size(x2,2)==1) then
+ errmsg = msprintf(gettext("%s: Upper Bound and Lower Bound are not matching"), "fminbnd");
+ error(errmsg);
+ elseif(size(x2,1)==s(1) & size(x2,2)==1) then
+ x2=x2;
+ elseif(size(x2,1)==1 & size(x2,2)~=s(1)) then
+ errmsg = msprintf(gettext("%s: Upper Bound and Lower Bound are not matching"), "fminbnd");
+ error(errmsg);
+ elseif(size(x2,1)==1 & size(x2,2)==s(1)) then
+ x2=x2';
+ end
+
+ //To check the contents of x1 and x2 (2nd & 3rd Parameter)
+
+ for i = 1:s(2)
+ if (x1(i) == %inf) then
+ errmsg = msprintf(gettext("%s: Value of Lower Bound can not be infinity"), "fminbnd");
+ error(errmsg);
+ end
+ if (x2(i) == -%inf) then
+ errmsg = msprintf(gettext("%s: Value of Upper Bound can not be negative infinity"), "fminbnd");
+ 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);
+ error(errmsg)
+ end
+ end
+
+ //To check, whether options has been entered by the user
+ if ( rhs<4 | size(varargin(4)) ==0 ) then
+ param = list();
+ else
+ param =varargin(4); //Storing the 3rd Input Parameter in an intermediate list named 'param'
+ end
+
+ options = list("MaxIter",[3000],"CpuTime",[600],"TolX", [1e-4]);
+
+ //To check the user entry for options and storing it
+ for i = 1:(size(param))/2
+ select param(2*i-1)
+ case "MaxIter" then
+ options(2*i) = param(2*i);
+ case "CpuTime" then
+ options(2*i) = param(2*i);
+ case "TolX" then
+ options(2*i) = param(2*i);
+ else
+ errmsg = msprintf(gettext("%s: Unrecognized parameter name %s."), "fminbnd", param(2*i-1));
+ error(errmsg)
+ end
+ end
+
+
+ //Defining a function to calculate Gradient or Hessian in an error deductable form.
+ function [y,check]=gradhess(x,t)
+ if t==1 then
+ if(execstr('y=numderivative(fun,x)','errcatch')==10000)
+ y=zeros(s(1),1);
+ check=1;
+ else
+ y=numderivative(fun,x);
+ if (isreal(y)==%F) then
+ y=zeros(s(1),1);
+ check=1;
+ else
+ check=0;
+ end
+ end
+ else
+ if(execstr('[grad,y]=numderivative(fun,x)','errcatch')==10000)
+ y=zeros(s(1),1);
+ check=1;
+ else
+ [grad,y]=numderivative(fun,x);
+ if (isreal(y)==%F) then
+ y=zeros(s(1),s(1));
+ check=1;
+ else
+ check=0;
+ end
+ end
+ end
+ endfunction
+
+ //Calling the Ipopt function for solving the above problem
+ [xopt,fopt,status,iter,cpu,obj_eval,dual,zl,zu] = solveminbndp(f,gradhess,x1,x2,options);
+
+ //Calculating the values for the output
+ xopt = xopt';
+ exitflag = status;
+ output = struct("Iterations", [],"Cpu_Time",[],"Objective_Evaluation",[],"Dual_Infeasibility",[]);
+ output.Iterations = iter;
+ output.Cpu_Time = cpu;
+ output.Objective_Evaluation = obj_eval;
+ output.Dual_Infeasibility = dual;
+ lambda = struct("lower", zl,"upper",zu);
+
+ //In the cases of the problem not being solved, return NULL to the output matrices
+ if( status~=0 & status~=1 & status~=2 & status~=3 & status~=4 & status~=7 ) then
+ xopt=[]
+ fopt=[]
+ output = struct("Iterations", [],"Cpu_Time",[]);
+ output.Iterations = iter;
+ output.Cpu_Time = cpu;
+ lambda = struct("lower",[],"upper",[]);
+ end
+
+ //To print output message
+ 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/fmincon.bin b/macros/fmincon.bin
new file mode 100644
index 0000000..3ea45b3
Binary files /dev/null and b/macros/fmincon.bin differ
diff --git a/macros/fmincon.sci b/macros/fmincon.sci
new file mode 100644
index 0000000..2630683
--- /dev/null
+++ b/macros/fmincon.sci
@@ -0,0 +1,928 @@
+// 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: R.Vidyadhar & Vignesh Kannan
+// Organization: FOSSEE, IIT Bombay
+// Email: toolbox@scilab.in
+
+function [xopt,fopt,exitflag,output,lambda,gradient,hessian] = fmincon (varargin)
+ // Solves a multi-variable constrainted optimization problem
+ //
+ // Calling Sequence
+ // xopt = fmincon(f,x0,A,b)
+ // xopt = fmincon(f,x0,A,b,Aeq,beq)
+ // xopt = fmincon(f,x0,A,b,Aeq,beq,lb,ub)
+ // xopt = fmincon(f,x0,A,b,Aeq,beq,lb,ub,nlc)
+ // xopt = fmincon(f,x0,A,b,Aeq,beq,lb,ub,nlc,options)
+ // [xopt,fopt] = fmincon(.....)
+ // [xopt,fopt,exitflag]= fmincon(.....)
+ // [xopt,fopt,exitflag,output]= fmincon(.....)
+ // [xopt,fopt,exitflag,output,lambda]=fmincon(.....)
+ // [xopt,fopt,exitflag,output,lambda,gradient]=fmincon(.....)
+ // [xopt,fopt,exitflag,output,lambda,gradient,hessian]=fmincon(.....)
+ //
+ // Parameters
+ // f : a function, representing the objective function of the problem
+ // x0 : a vector of doubles, containing the starting values of variables of size (1 X n) or (n X 1) where 'n' is the number of Variables
+ // A : a matrix of doubles, containing the coefficients of linear inequality constraints of size (m X n) where 'm' is the number of linear inequality constraints
+ // b : a vector of doubles, related to 'A' and containing the the Right hand side equation of the linear inequality constraints of size (m X 1)
+ // Aeq : a matrix of doubles, containing the coefficients of linear equality constraints of size (m1 X n) where 'm1' is the number of linear equality constraints
+ // beq : a vector of doubles, related to 'Aeq' and containing the the Right hand side equation of the linear equality constraints of size (m1 X 1)
+ // lb : a vector of doubles, containing the lower bounds of the variables of size (1 X n) or (n X 1) where 'n' is the number of Variables
+ // ub : a vector of doubles, containing the upper bounds of the variables of size (1 X n) or (n X 1) where 'n' is the number of Variables
+ // nlc : a function, representing the Non-linear Constraints functions(both Equality and Inequality) of the problem. It is declared in such a way that non-linear inequality constraints are defined first as a single row vector (c), followed by non-linear equality constraints as another single row vector (ceq). Refer Example for definition of Constraint function.
+ // options : a list, containing the option for user to specify. See below for details.
+ // xopt : a vector of doubles, cointating the computed solution of the optimization problem
+ // fopt : a scalar of double, containing the the function value at x
+ // exitflag : a scalar of integer, containing the flag which denotes the reason for termination of algorithm. See below for details.
+ // output : a structure, containing the information about the optimization. See below for details.
+ // lambda : a structure, containing the Lagrange multipliers of lower bound, upper bound and constraints at the optimized point. See below for details.
+ // gradient : a vector of doubles, containing the Objective's gradient of the solution.
+ // hessian : a matrix of doubles, containing the Lagrangian's hessian of the solution.
+ //
+ // Description
+ // Search the minimum of a constrained optimization problem specified by :
+ // Find the minimum of f(x) such that
+ //
+ //
+ // \begin{eqnarray}
+ // &\mbox{min}_{x}
+ // & f(x) \\
+ // & \text{subject to} & A*x \leq b \\
+ // & & Aeq*x \ = beq\\
+ // & & c(x) \leq 0\\
+ // & & ceq(x) \ = 0\\
+ // & & lb \leq x \leq ub \\
+ // \end{eqnarray}
+ //
+ //
+ // The routine calls Ipopt for solving the Constrained Optimization 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.
+ //
+ // Syntax : options= list("MaxIter", [---], "CpuTime", [---], "GradObj", ---, "Hessian", ---, "GradCon", ---);
+ // MaxIter : a Scalar, containing the Maximum Number of Iteration that the solver should take.
+ // CpuTime : a Scalar, containing the Maximum amount of CPU Time that the solver should take.
+ // GradObj : a function, representing the gradient function of the Objective in Vector Form.
+ // Hessian : a function, representing the hessian function of the Lagrange in Symmetric Matrix Form with Input parameters x, Objective factor and Lambda. Refer Example for definition of Lagrangian Hessian function.
+ // GradCon : a function, representing the gradient of the Non-Linear Constraints (both Equality and Inequality) of the problem. It is declared in such a way that gradient of non-linear inequality constraints are defined first as a separate Matrix (cg of size m2 X n or as an empty), followed by gradient of non-linear equality constraints as a separate Matrix (ceqg of size m2 X n or as an empty) where m2 & m3 are number of non-linear inequality and equality constraints respectively.
+ // Default Values : options = list("MaxIter", [3000], "CpuTime", [600]);
+ //
+ //
+ // The exitflag allows to know the status of the optimization which is given back by Ipopt.
+ //
+ // exitflag=0 : Optimal Solution Found
+ // exitflag=1 : Maximum Number of Iterations Exceeded. Output may not be optimal.
+ // exitflag=2 : Maximum amount of CPU Time exceeded. Output may not be optimal.
+ // exitflag=3 : Stop at Tiny Step.
+ // exitflag=4 : Solved To Acceptable Level.
+ // exitflag=5 : Converged to a point of local infeasibility.
+ //
+ //
+ // 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.
+ //
+ // output.Iterations: The number of iterations performed during the search
+ // output.Cpu_Time: The total cpu-time spend during the search
+ // output.Objective_Evaluation: The number of Objective Evaluations performed during the search
+ // output.Dual_Infeasibility: The Dual Infeasiblity of the final soution
+ //
+ //
+ // 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.
+ //
+ // lambda.lower: The Lagrange multipliers for the lower bound constraints.
+ // lambda.upper: The Lagrange multipliers for the upper bound constraints.
+ // lambda.eqlin: The Lagrange multipliers for the linear equality constraints.
+ // lambda.ineqlin: The Lagrange multipliers for the linear inequality constraints.
+ // lambda.eqnonlin: The Lagrange multipliers for the non-linear equality constraints.
+ // lambda.ineqnonlin: The Lagrange multipliers for the non-linear inequality constraints.
+ //
+ //
+ // Examples
+ // //Find x in R^2 such that it minimizes:
+ // //f(x)= -x1 -x2/3
+ // //x0=[0,0]
+ // //constraint-1 (c1): x1 + x2 <= 2
+ // //constraint-2 (c2): x1 + x2/4 <= 1
+ // //constraint-3 (c3): x1 - x2 <= 2
+ // //constraint-4 (c4): -x1/4 - x2 <= 1
+ // //constraint-5 (c5): -x1 - x2 <= -1
+ // //constraint-6 (c6): -x1 + x2 <= 2
+ // //constraint-7 (c7): x1 + x2 = 2
+ // //Objective function to be minimised
+ // function y=f(x)
+ // y=-x(1)-x(2)/3;
+ // endfunction
+ // //Starting point, linear constraints and variable bounds
+ // x0=[0 , 0];
+ // A=[1,1 ; 1,1/4 ; 1,-1 ; -1/4,-1 ; -1,-1 ; -1,1];
+ // b=[2;1;2;1;-1;2];
+ // Aeq=[1,1];
+ // beq=[2];
+ // lb=[];
+ // ub=[];
+ // nlc=[];
+ // //Gradient of objective function
+ // function y= fGrad(x)
+ // y= [-1,-1/3];
+ // endfunction
+ // //Hessian of lagrangian
+ // function y= lHess(x,obj,lambda)
+ // y= obj*[0,0;0,0]
+ // endfunction
+ // //Options
+ // options=list("GradObj", fGrad, "Hessian", lHess);
+ // //Calling Ipopt
+ // [x,fval,exitflag,output,lambda,grad,hessian] =fmincon(f, x0,A,b,Aeq,beq,lb,ub,nlc,options)
+ // // Press ENTER to continue
+ //
+ // Examples
+ // //Find x in R^3 such that it minimizes:
+ // //f(x)= x1*x2 + x2*x3
+ // //x0=[0.1 , 0.1 , 0.1]
+ // //constraint-1 (c1): x1^2 - x2^2 + x3^2 <= 2
+ // //constraint-2 (c2): x1^2 + x2^2 + x3^2 <= 10
+ // //Objective function to be minimised
+ // function y=f(x)
+ // y=x(1)*x(2)+x(2)*x(3);
+ // endfunction
+ // //Starting point, linear constraints and variable bounds
+ // x0=[0.1 , 0.1 , 0.1];
+ // A=[];
+ // b=[];
+ // Aeq=[];
+ // beq=[];
+ // lb=[];
+ // ub=[];
+ // //Nonlinear constraints
+ // function [c,ceq]=nlc(x)
+ // c = [x(1)^2 - x(2)^2 + x(3)^2 - 2 , x(1)^2 + x(2)^2 + x(3)^2 - 10];
+ // ceq = [];
+ // endfunction
+ // //Gradient of objective function
+ // function y= fGrad(x)
+ // y= [x(2),x(1)+x(3),x(2)];
+ // endfunction
+ // //Hessian of the Lagrange Function
+ // function y= lHess(x,obj,lambda)
+ // y= obj*[0,1,0;1,0,1;0,1,0] + lambda(1)*[2,0,0;0,-2,0;0,0,2] + lambda(2)*[2,0,0;0,2,0;0,0,2]
+ // endfunction
+ // //Gradient of Non-Linear Constraints
+ // function [cg,ceqg] = cGrad(x)
+ // cg=[2*x(1) , -2*x(2) , 2*x(3) ; 2*x(1) , 2*x(2) , 2*x(3)];
+ // ceqg=[];
+ // endfunction
+ // //Options
+ // options=list("MaxIter", [1500], "CpuTime", [500], "GradObj", fGrad, "Hessian", lHess,"GradCon", cGrad);
+ // //Calling Ipopt
+ // [x,fval,exitflag,output] =fmincon(f, x0,A,b,Aeq,beq,lb,ub,nlc,options)
+ // // Press ENTER to continue
+ //
+ // Examples
+ // //The below problem is an unbounded problem:
+ // //Find x in R^3 such that it minimizes:
+ // //f(x)= -(x1^2 + x2^2 + x3^2)
+ // //x0=[0.1 , 0.1 , 0.1]
+ // // x1 <= 0
+ // // x2 <= 0
+ // // x3 <= 0
+ // //Objective function to be minimised
+ // function y=f(x)
+ // y=-(x(1)^2+x(2)^2+x(3)^2);
+ // endfunction
+ // //Starting point, linear constraints and variable bounds
+ // x0=[0.1 , 0.1 , 0.1];
+ // A=[];
+ // b=[];
+ // Aeq=[];
+ // beq=[];
+ // lb=[];
+ // ub=[0,0,0];
+ // //Options
+ // options=list("MaxIter", [1500], "CpuTime", [500]);
+ // //Calling Ipopt
+ // [x,fval,exitflag,output,lambda,grad,hessian] =fmincon(f, x0,A,b,Aeq,beq,lb,ub,[],options)
+ // // Press ENTER to continue
+ //
+ // Examples
+ // //The below problem is an infeasible problem:
+ // //Find x in R^3 such that in minimizes:
+ // //f(x)=x1*x2 + x2*x3
+ // //x0=[1,1,1]
+ // //constraint-1 (c1): x1^2 <= 1
+ // //constraint-2 (c2): x1^2 + x2^2 <= 1
+ // //constraint-3 (c3): x3^2 <= 1
+ // //constraint-4 (c4): x1^3 = 0.5
+ // //constraint-5 (c5): x2^2 + x3^2 = 0.75
+ // // 0 <= x1 <=0.6
+ // // 0.2 <= x2 <= inf
+ // // -inf <= x3 <= 1
+ // //Objective function to be minimised
+ // function y=f(x)
+ // y=x(1)*x(2)+x(2)*x(3);
+ // endfunction
+ // //Starting point, linear constraints and variable bounds
+ // x0=[1,1,1];
+ // A=[];
+ // b=[];
+ // Aeq=[];
+ // beq=[];
+ // lb=[0 0.2,-%inf];
+ // ub=[0.6 %inf,1];
+ // //Nonlinear constraints
+ // function [c,ceq]=nlc(x)
+ // c=[x(1)^2-1,x(1)^2+x(2)^2-1,x(3)^2-1];
+ // ceq=[x(1)^3-0.5,x(2)^2+x(3)^2-0.75];
+ // endfunction
+ // //Gradient of objective function
+ // function y= fGrad(x)
+ // y= [x(2),x(1)+x(3),x(2)];
+ // endfunction
+ // //Hessian of the Lagrange Function
+ // function y= lHess(x,obj,lambda)
+ // y= obj*[0,1,0;1,0,1;0,1,0] + lambda(1)*[2,0,0;0,0,0;0,0,0] + lambda(2)*[2,0,0;0,2,0;0,0,0] +lambda(3)*[0,0,0;0,0,0;0,0,2] + lambda(4)*[6*x(1 ),0,0;0,0,0;0,0,0] + lambda(5)*[0,0,0;0,2,0;0,0,2];
+ // endfunction
+ // //Gradient of Non-Linear Constraints
+ // function [cg,ceqg] = cGrad(x)
+ // cg = [2*x(1),0,0;2*x(1),2*x(2),0;0,0,2*x(3)];
+ // ceqg = [3*x(1)^2,0,0;0,2*x(2),2*x(3)];
+ // endfunction
+ // //Options
+ // 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)
+ // Authors
+ // R.Vidyadhar , Vignesh Kannan
+
+
+ //To check the number of input and output arguments
+ [lhs , rhs] = argn();
+
+ //To check the number of arguments given by the user
+ if ( rhs<4 | rhs>13 ) then
+ errmsg = msprintf(gettext("%s: Unexpected number of input arguments : %d provided while it should be 4,6,8,9,10,11,12,13"), "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,10,11,12,13"), "fmincon", rhs);
+ error(errmsg)
+ end
+
+ //Storing the Input Parameters
+ fun = varargin(1);
+ x0 = varargin(2);
+ A = varargin(3);
+ b = varargin(4);
+ Aeq = [];
+ beq = [];
+ lb = [];
+ ub = [];
+ nlc = [];
+
+ if (rhs>4) then
+ Aeq = varargin(5);
+ beq = varargin(6);
+ end
+
+ if (rhs>6) then
+ lb = varargin(7);
+ ub = varargin(8);
+ end
+
+ if (rhs>8) then
+ nlc = varargin(9);
+ end
+
+ //To check whether the 1st Input argument (f) is a function or not
+ if (type(f) ~= 13 & type(f) ~= 11) then
+ errmsg = msprintf(gettext("%s: Expected function for Objective (1st Parameter)"), "fmincon");
+ error(errmsg);
+ end
+
+ //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
+
+ //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 (Starting Point) or Starting Point cannot be Empty"), "fmincon");
+ error(errmsg);
+ end
+
+ if(size(x0,2)==1) then
+ x0=x0'; //Converting x0 to a row vector, if it is a column vector
+ else
+ x0=x0; //Retaining the same, if it is already a row vector
+ end
+ s=size(x0);
+
+ //To check the match between fun (1st Parameter) and x0 (2nd Parameter)
+ if(execstr('init=fun(x0)','errcatch')==21) then
+ errmsg = msprintf(gettext("%s: Objective function and x0 did not match"), "fmincon");
+ error(errmsg);
+ end
+
+ //Converting the User defined Objective function into Required form (Error Detectable)
+ function [y,check] = f(x)
+ if(execstr('y=fun(x)','errcatch')==32 | execstr('y=fun(x)','errcatch')==27)
+ y=0;
+ check=1;
+ else
+ y=fun(x);
+ if (isreal(y)==%F) then
+ y=0;
+ check=1;
+ else
+ check=0;
+ end
+ end
+ 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
+
+ //To check for correct size of A(3rd paramter)
+ if(size(A,2)~=s(2) & size(A,2)~=0) then
+ errmsg = msprintf(gettext("%s: Expected Matrix of size (No of linear inequality constraints X No of Variables) or an Empty Matrix for Linear Inequality Constraint coefficient Matrix A"), "fmincon");
+ error(errmsg);
+ end
+
+ 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
+
+ //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
+ if(size(b,2)~=0) then
+ errmsg = msprintf(gettext("%s: As Linear Inequality Constraint coefficient Matrix A (3rd parameter) is empty, b (4th Parameter) should also be empty"), "fmincon");
+ error(errmsg);
+ end
+ else
+ if((size(b,1)~=1) & (size(b,2)~=1)) then
+ errmsg = msprintf(gettext("%s: Expected Non empty Row/Column Vector for b (4th Parameter) for your Inputs "), "fmincon");
+ error(errmsg);
+ elseif(size(b,1)~=s1(1) & size(b,2)==1) then
+ errmsg = msprintf(gettext("%s: Expected Column Vector (number of linear inequality constraints X 1) for b (4th Parameter) "), "fmincon");
+ error(errmsg);
+ elseif(size(b,1)==s1(1) & size(b,2)==1) then
+ b=b;
+ elseif(size(b,1)==1 & size(b,2)~=s1(1)) then
+ errmsg = msprintf(gettext("%s: Expected Row Vector (1 X number of linear inequality constraints) for b (4th Parameter) "), "fmincon");
+ error(errmsg);
+ elseif(size(b,1)==1 & size(b,2)==s1(1)) then
+ b=b';
+ end
+ 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
+
+ //To check for the correct size of Aeq (5th paramter)
+ if(size(Aeq,2)~=s(2) & size(Aeq,2)~=0) then
+ errmsg = msprintf(gettext("%s: Expected Matrix of size (No of linear equality constraints X No of Variables) or an Empty Matrix for Linear Equality Constraint coefficient Matrix Aeq"), "fmincon");
+ error(errmsg);
+ end
+
+ 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
+
+ //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
+ if(size(beq,2)~=0) then
+ errmsg = msprintf(gettext("%s: As Linear Equality Constraint coefficient Matrix Aeq (5th parameter) is empty, beq (6th Parameter) should also be empty"), "fmincon");
+ error(errmsg);
+ end
+ else
+ if((size(beq,1)~=1) & (size(beq,2)~=1)) then
+ errmsg = msprintf(gettext("%s: Expected Non empty Row/Column Vector for beq (6th Parameter)"), "fmincon");
+ error(errmsg);
+ elseif(size(beq,1)~=s2(1) & size(beq,2)==1) then
+ errmsg = msprintf(gettext("%s: Expected Column Vector (number of linear equality constraints X 1) for beq (6th Parameter) "), "fmincon");
+ error(errmsg);
+ elseif(size(beq,1)==s2(1) & size(beq,2)==1) then
+ beq=beq;
+ elseif(size(beq,1)==1 & size(beq,2)~=s2(1)) then
+ errmsg = msprintf(gettext("%s: Expected Row Vector (1 X number of linear equality constraints) for beq (6th Parameter) "), "fmincon");
+ error(errmsg);
+ elseif(size(beq,1)==1 & size(beq,2)==s2(1)) then
+ beq=beq';
+ end
+ end
+
+
+ //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
+
+ //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
+ lb = repmat(-%inf,1,s(2));
+ end
+
+ if (size(lb,1)~=1) & (size(lb,2)~=1) then
+ errmsg = msprintf(gettext("%s: Lower Bound (7th Parameter) should be a vector"), "fmincon");
+ error(errmsg);
+ elseif(size(lb,1)~=s(2) & size(lb,2)==1) then
+ errmsg = msprintf(gettext("%s: Expected Column Vector (number of Variables X 1) for lower bound (7th Parameter) "), "fmincon");
+ error(errmsg);
+ elseif(size(lb,1)==s(2) & size(lb,2)==1) then
+ lb=lb;
+ elseif(size(lb,1)==1 & size(lb,2)~=s(2)) then
+ errmsg = msprintf(gettext("%s: Expected Row Vector (1 X number of Variables) for lower bound (7th Parameter) "), "fmincon");
+ error(errmsg);
+ elseif(size(lb,1)==1 & size(lb,2)==s(2)) then
+ lb=lb';
+ 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
+
+ //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
+ ub = repmat(%inf,1,s(2));
+ end
+
+ if (size(ub,1)~=1)& (size(ub,2)~=1) then
+ errmsg = msprintf(gettext("%s: Upper Bound (8th Parameter) should be a vector"), "fmincon");
+ error(errmsg);
+ elseif(size(ub,1)~=s(2) & size(ub,2)==1) then
+ errmsg = msprintf(gettext("%s: Expected Column Vector (number of Variables X 1) for upper bound (8th Parameter) "), "fmincon");
+ error(errmsg);
+ elseif(size(ub,1)==s(2) & size(ub,2)==1) then
+ ub=ub;
+ elseif(size(ub,1)==1 & size(ub,2)~=s(2)) then
+ errmsg = msprintf(gettext("%s: Expected Row Vector (1 X number of Variables) for upper bound (8th Parameter) "), "fmincon");
+ error(errmsg);
+ elseif(size(ub,1)==1 & size(ub,2)==s(2)) then
+ ub=ub';
+ end
+
+ //To check the contents of lb & ub (7th & 8th Parameter)
+ for i = 1:s(2)
+ if (lb(i) == %inf) then
+ errmsg = msprintf(gettext("%s: Value of Lower Bound can not be infinity"), "fmincon");
+ error(errmsg);
+ end
+
+ if (ub(i) == -%inf) then
+ errmsg = msprintf(gettext("%s: Value of Upper Bound can not be negative infinity"), "fmincon");
+ error(errmsg);
+ 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);
+ error(errmsg)
+ end
+ end
+
+ //To check whether the 10th Input argument (nlc) is a function or an empty matrix
+ if (type(nlc) == 1 & size(nlc,2)==0 ) then
+ addnlc=[];
+ addnlc1=[];
+ no_nlc=0;
+ no_nlic=0;
+ no_nlec=0;
+ elseif (type(nlc) == 13 | type(nlc) == 11) then
+
+ if(execstr('[sample_c,sample_ceq] = nlc(x0)','errcatch')==21)
+ errmsg = msprintf(gettext("%s: Non-Linear Constraint function(9th Parameter) and x0(2nd Parameter) did not match"), "fmincon");
+ error(errmsg);
+ end
+ [sample_c,sample_ceq] = nlc(x0);
+
+ if (size(sample_c,1)~=1 & size(sample_c,1)~=0) then
+ errmsg = msprintf(gettext("%s: Definition of c in Non-Linear Constraint function(9th Parameter) should be in the form of Row Vector or Empty Vector"), "fmincon");
+ error(errmsg)
+ end
+
+ if (size(sample_ceq,1)~=1 & size(sample_ceq,1)~=0) then
+ errmsg = msprintf(gettext("%s: Definition of ceq in Non-Linear Constraint function(9th Parameter) should be in the form of Row Vector or Empty Vector"), "fmincon");
+ error(errmsg)
+ end
+
+ 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);
+ y = [c';ceq'];
+ endfunction
+
+ //To check the addnlc function
+ if(execstr('sample_allcon = addnlc(x0)','errcatch')==21)
+ errmsg = msprintf(gettext("%s: Non-Linear Constraint function(9th Parameter) and x0(2nd Parameter) did not match"), "fmincon");
+ error(errmsg);
+ end
+ sample_allcon = addnlc(x0);
+
+ if (size(sample_allcon,1)==0 & size(sample_allcon,2)==0) then
+
+ elseif (size(sample_allcon,1)~=no_nlc | size(sample_allcon,2)~=1) then
+ errmsg = msprintf(gettext("%s: Please check the Non-Linear Constraint function (9th Parameter) function"), "fmincon");
+ error(errmsg)
+ end
+
+ //Constructing a nlc function with error deduction
+ function [y,check] = addnlc1(x)
+ if(execstr('y = addnlc(x)','errcatch')==32 | execstr('y = addnlc(x)','errcatch')==27)
+ y = 0;
+ check=1;
+ else
+ y = addnlc(x);
+ if (isreal(y)==%F) then
+ y = 0;
+ check=1;
+ else
+ check=0;
+ end
+ end
+ endfunction
+
+ else
+ errmsg = msprintf(gettext("%s: Non Linear Constraint (9th Parameter) should be a function or an Empty Matrix"), "fmincon");
+ error(errmsg)
+ end
+
+ //To check whether options has been entered by the user
+ if ( rhs<10 ) then
+ param = list();
+ else
+ param =varargin(10); //Storing the 3rd Input Parameter in an intermediate list named 'param'
+ 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
+
+ //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]);
+
+ //Flags to check whether Gradient is "ON"/"OFF" and Hessian is "ON"/"OFF"
+ flag1=0;
+ flag2=0;
+ flag3=0;
+
+ //Function for Gradient and Hessian
+ fGrad=[];
+ fGrad1=[];
+ lHess=[];
+ lHess1=[];
+ cGrad=[];
+ addcGrad=[];
+ addcGrad1=[];
+
+ //To check the user entry for options and storing it
+ for i = 1:(size(param))/2
+ select param(2*i-1)
+ case "MaxIter" then
+ options(2*i) = param(2*i); //Setting the maximum number of iterations as per user entry
+ case "CpuTime" then
+ options(2*i) = param(2*i); //Setting the maximum CPU time as per user entry
+ case "GradObj" then
+ flag1=1;
+ fGrad=param(2*i);
+ case "Hessian" then
+ flag2=1;
+ lHess=param(2*i);
+ case "GradCon" then
+ flag3=1;
+ cGrad=param(2*i);
+ else
+ errmsg = msprintf(gettext("%s: Unrecognized parameter name %s."), "fminbnd", 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) ~= 11 & type(fGrad) ~= 13) then
+ errmsg = msprintf(gettext("%s: Expected function for Gradient of Objective"), "fmincon");
+ error(errmsg);
+ end
+
+ if(execstr('sample_fGrad=fGrad(x0)','errcatch')==21)
+ errmsg = msprintf(gettext("%s: Gradient function of Objective and x0 did not match "), "fmincon");
+ error(errmsg);
+ end
+
+ sample_fGrad=fGrad(x0);
+
+ 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");
+ 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
+ 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
+ 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);
+ end
+ sample_lHess=lHess(x0,1,1:no_nlc);
+ 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
+ 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
+
+ if(execstr('[sample_cGrad,sample_ceqg]=cGrad(x0)','errcatch')==21)
+ errmsg = msprintf(gettext("%s: Gradient function of Constraint and x0 did not match "), "fmincon");
+ error(errmsg);
+ end
+ [sample_cGrad,sample_ceqg]=cGrad(x0);
+
+ if (size(sample_cGrad,2)==0) then
+ elseif (size(sample_cGrad,1)~=no_nlic | size(sample_cGrad,2)~=s(2)) then
+ errmsg = msprintf(gettext("%s: Definition of (cGrad) in Non-Linear Constraint function(10th Parameter) should be in the form of (m X n) or Empty Matrix where m is number of Non- linear inequality constraints and n is number of Variables"), "fmincon");
+ error(errmsg);
+ end
+
+ if (size(sample_ceqg,2)==0) then
+ elseif (size(sample_ceqg,1)~=no_nlec | size(sample_ceqg,2)~=s(2)) then
+ errmsg = msprintf(gettext("%s: Definition of (ceqg) in Non-Linear Constraint function(10th Parameter) should be in the form of (m X n) or Empty Matrix where m is number of Non- linear equality constraints and n is number of Variables"), "fmincon");
+ error(errmsg);
+ end
+
+ function y = addcGrad(x)
+ [sample_cGrad,sample_ceqg] = cGrad(x);
+ y = [sample_cGrad;sample_ceqg];
+ endfunction
+
+ sample_addcGrad=addcGrad(x0);
+ if(size(sample_addcGrad,1)~=no_nlc | size(sample_addcGrad,2)~=s(2)) then
+ 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
+ y = 0;
+ check=1;
+ else
+ check=0;
+ end
+ end
+ endfunction
+ end
+
+ //To Convert the Gradient and Hessian into Error Debugable form
+
+
+ //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)
+
+ //Calculating the values for the output
+ xopt = xopt';
+ exitflag = status;
+ output = struct("Iterations", [],"Cpu_Time",[],"Objective_Evaluation",[],"Dual_Infeasibility",[]);
+ output.Iterations = iter;
+ output.Cpu_Time = cpu;
+ output.Objective_Evaluation = obj_eval;
+ output.Dual_Infeasibility = dual;
+ lambda = struct("lower", zl,"upper",zu,"ineqlin",[],"eqlin",[],"ineqnonlin",[],"eqnonlin",[]);
+
+ if (no_nlic ~= 0) then
+ for i = 1:no_nlic
+ lambda.ineqnonlin (i) = lambda1(i)
+ end
+ lambda.ineqnonlin = lambda.ineqnonlin'
+ end
+
+ if (no_nlec ~= 0) then
+ j=1;
+ for i = no_nlic+1 : no_nlc
+ lambda.eqnonlin (j) = lambda1(i)
+ j= j+1;
+ end
+ lambda.eqnonlin = lambda.eqnonlin'
+ end
+
+ if (Aeq ~=[]) then
+ j=1;
+ for i = no_nlc+1 : no_nlc + size(Aeq,1)
+ lambda.eqlin (j) = lambda1(i)
+ j= j+1;
+ end
+ lambda.eqlin = lambda.eqlin'
+ end
+
+ if (A ~=[]) then
+ j=1;
+ for i = no_nlc+ size(Aeq,1)+ 1 : no_nlc + size(Aeq,1) + size(A,1)
+ lambda.ineqlin (j) = lambda1(i)
+ j= j+1;
+ end
+ lambda.ineqlin = lambda.ineqlin'
+ end
+
+ //Converting hessian of order (1 x (numberOfVariables)^2) received from Ipopt to order (numberOfVariables x numberOfVariables)
+ s1=size(gradient)
+ for i =1:s1(2)
+ for j =1:s1(2)
+ hessian(i,j)= hessian1(j+((i-1)*s1(2)))
+ end
+ end
+
+ //In the cases of the problem not being solved, return NULL to the output matrices
+ if( status~=0 & status~=1 & status~=2 & status~=3 & status~=4 & status~=7 ) then
+ xopt=[];
+ fopt=[];
+ output = struct("Iterations", [],"Cpu_Time",[]);
+ output.Iterations = iter;
+ output.Cpu_Time = cpu;
+ lambda = struct("lower",[],"upper",[],"ineqlin",[],"eqlin",[],"ineqnonlin",[],"eqnonlin",[]);
+ gradient=[];
+ hessian=[];
+ end
+
+
+ //To print output message
+ 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 11 then
+ printf("\nProblem has too few degrees of freedom.\n");
+ case 12 then
+ printf("\nInvalid option thrown back by Ipopt\n");
+ case 13 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/fminimax.bin b/macros/fminimax.bin
new file mode 100644
index 0000000..c40fcce
Binary files /dev/null and b/macros/fminimax.bin differ
diff --git a/macros/fminimax.sci b/macros/fminimax.sci
new file mode 100644
index 0000000..5084475
--- /dev/null
+++ b/macros/fminimax.sci
@@ -0,0 +1,511 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2015 - IIT Bombay - FOSSEE
+//
+// Authors: Animesh Baranawal
+// Organization: FOSSEE, IIT Bombay
+// Email: animeshbaranawal@gmail.com
+// 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
+
+function [x,fval,maxfval,exitflag,output,lambda] = fminimax(varargin)
+ // Solves minimax constraint problem
+ //
+ // Calling Sequence
+ // x = fminimax(fun,x0)
+ // x = fminimax(fun,x0,A,b)
+ // x = fminimax(fun,x0,A,b,Aeq,beq)
+ // x = fminimax(fun,x0,A,b,Aeq,beq,lb,ub)
+ // x = fminimax(fun,x0,A,b,Aeq,beq,lb,ub,nonlinfun)
+ // x = fminimax(fun,x0,A,b,Aeq,beq,lb,ub,nonlinfun,options)
+ // [x, fval] = fmincon(.....)
+ // [x, fval, maxfval]= fmincon(.....)
+ // [x, fval, maxfval, exitflag]= fmincon(.....)
+ // [x, fval, maxfval, exitflag, output]= fmincon(.....)
+ // [x, fval, maxfval, exitflag, output, lambda]= fmincon(.....)
+ //
+ // 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.
+ // x0: a nx1 or 1xn matrix of doubles, where n is the number of variables, the initial guess for the optimization algorithm
+ // A: a nil x n matrix of doubles, where n is the number of variables and nil is the number of linear inequalities. If A==[] and b==[], it is assumed that there is no linear inequality constraints. If (A==[] & b<>[]), fminimax generates an error (the same happens if (A<>[] & b==[]))
+ // b: a nil x 1 matrix of doubles, where nil is the number of linear inequalities
+ // Aeq: a nel x n matrix of doubles, where n is the number of variables and nel is the number of linear equalities. If Aeq==[] and beq==[], it is assumed that there is no linear equality constraints. If (Aeq==[] & beq<>[]), fminimax generates an error (the same happens if (Aeq<>[] & beq==[]))
+ // beq: a nel x 1 matrix of doubles, where nel is the number of linear equalities
+ // lb: a nx1 or 1xn matrix of doubles, where n is the number of variables. The lower bound for x. If lb==[], then the lower bound is automatically set to -inf
+ // ub: a nx1 or 1xn matrix of doubles, where n is the number of variables. The upper bound for x. If ub==[], then the upper bound is automatically set to +inf
+ // nonlinfun: function that computes the nonlinear inequality constraints c(x) <= 0 and nonlinear equality constraints ceq(x) = 0.
+ // x: a nx1 matrix of doubles, the computed solution of the optimization problem
+ // fval: a vector of doubles, the value of fun at x
+ // maxfval: a 1x1 matrix of doubles, the maximum value in vector fval
+ // exitflag: a 1x1 matrix of floating point integers, the exit status
+ // output: a struct, the details of the optimization process
+ // lambda: a struct, the Lagrange multipliers at optimum
+ // options: a list, containing the option for user to specify. See below for details.
+ //
+ // Description
+ // fminimax minimizes the worst-case (largest) value of a set of multivariable functions, starting at an initial estimate. This is generally referred to as the minimax problem.
+ //
+ //
+ // \min_{x} \max_{i} F_{i}(x)\: \textrm{such that} \:\begin{cases}
+ // & c(x) \leq 0 \\
+ // & ceq(x) = 0 \\
+ // & A.x \leq b \\
+ // & Aeq.x = beq \\
+ // & minmaxLb \leq x \leq minmaxUb
+ // \end{cases}
+ //
+ //
+ // Currently, fminimax calls fmincon which uses the ip-opt algorithm.
+ //
+ // max-min problems can also be solved with fminimax, using the identity
+ //
+ //
+ // \max_{x} \min_{i} F_{i}(x) = -\min_{x} \max_{i} \left( -F_{i}(x) \right)
+ //
+ //
+ // 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.
+ //
+ // Syntax : options= list("MaxIter", [---], "CpuTime", [---], "GradObj", ---, "GradCon", ---);
+ // MaxIter : a Scalar, containing the Maximum Number of Iteration that the solver should take.
+ // CpuTime : a Scalar, containing the Maximum amount of CPU Time that the solver should take.
+ // GradObj : a function, representing the gradient function of the Objective in Vector Form.
+ // GradCon : a function, representing the gradient of the Non-Linear Constraints (both Equality and Inequality) of the problem. It is declared in such a way that gradient of non-linear inequality constraints are defined first as a separate Matrix (cg of size m2 X n or as an empty), followed by gradient of non-linear equality constraints as a separate Matrix (ceqg of size m2 X n or as an empty) where m2 & m3 are number of non-linear inequality and equality constraints respectively.
+ // Default Values : options = list("MaxIter", [3000], "CpuTime", [600]);
+ //
+ //
+ // The objective function must have header :
+ //
+ // F = fun(x)
+ //
+ // where x is a n x 1 matrix of doubles and F is a m x 1 matrix of doubles where m is the total number of objective functions inside F.
+ // On input, the variable x contains the current point and, on output, the variable F must contain the objective function values.
+ //
+ // By default, the gradient options for fminimax are turned off and and fmincon does the gradient opproximation of minmaxObjfun. In case the GradObj option is off and GradConstr option is on, fminimax approximates minmaxObjfun gradient using numderivative toolbox.
+ //
+ // If we can provide exact gradients, we should do so since it improves the convergence speed of the optimization algorithm.
+ //
+ // Furthermore, we must enable the "GradObj" option with the statement :
+ //
+ // minimaxOptions = list("GradObj",fGrad);
+ //
+ // This will let fminimax know that the exact gradient of the objective function is known, so that it can change the calling sequence to the objective function. Note that, fGrad should be mentioned in the form of N x n where n is the number of variables, N is the number of functions in objective function.
+ //
+ // The constraint function must have header :
+ //
+ // [c, ceq] = confun(x)
+ //
+ // where x is a n x 1 matrix of dominmaxUbles, c is a 1 x nni matrix of doubles and ceq is a 1 x nne matrix of doubles (nni : number of nonlinear inequality constraints, nne : number of nonlinear equality constraints).
+ // On input, the variable x contains the current point and, on output, the variable c must contain the nonlinear inequality constraints and ceq must contain the nonlinear equality constraints.
+ //
+ // By default, the gradient options for fminimax are turned off and and fmincon does the gradient opproximation of confun. In case the GradObj option is on and GradCons option is off, fminimax approximates confun gradient using numderivative toolbox.
+ //
+ // If we can provide exact gradients, we should do so since it improves the convergence speed of the optimization algorithm.
+ //
+ // Furthermore, we must enable the "GradCon" option with the statement :
+ //
+ // minimaxOptions = list("GradCon",confunGrad);
+ //
+ // This will let fminimax know that the exact gradient of the objective function is known, so that it can change the calling sequence to the objective function.
+ //
+ // The constraint derivative function must have header :
+ //
+ // [dc,dceq] = confungrad(x)
+ //
+ // where dc is a nni x n matrix of doubles and dceq is a nne x n matrix of doubles.
+ //
+ // The exitflag allows to know the status of the optimization which is given back by Ipopt.
+ //
+ // exitflag=0 : Optimal Solution Found
+ // exitflag=1 : Maximum Number of Iterations Exceeded. Output may not be optimal.
+ // exitflag=2 : Maximum amount of CPU Time exceeded. Output may not be optimal.
+ // exitflag=3 : Stop at Tiny Step.
+ // exitflag=4 : Solved To Acceptable Level.
+ // exitflag=5 : Converged to a point of local infeasibility.
+ //
+ //
+ // 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.
+ //
+ // output.Iterations: The number of iterations performed during the search
+ // output.Cpu_Time: The total cpu-time spend during the search
+ // output.Objective_Evaluation: The number of Objective Evaluations performed during the search
+ // output.Dual_Infeasibility: The Dual Infeasiblity of the final soution
+ //
+ //
+ // 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.
+ //
+ // lambda.lower: The Lagrange multipliers for the lower bound constraints.
+ // lambda.upper: The Lagrange multipliers for the upper bound constraints.
+ // lambda.eqlin: The Lagrange multipliers for the linear equality constraints.
+ // lambda.ineqlin: The Lagrange multipliers for the linear inequality constraints.
+ // lambda.eqnonlin: The Lagrange multipliers for the non-linear equality constraints.
+ // lambda.ineqnonlin: The Lagrange multipliers for the non-linear inequality constraints.
+ //
+ //
+ // Examples
+ // // A basic case :
+ // // we provide only the objective function and the nonlinear constraint
+ // // function
+ // function f = myfun(x)
+ // f(1)= 2*x(1)^2 + x(2)^2 - 48*x(1) - 40*x(2) + 304; //Objectives
+ // f(2)= -x(1)^2 - 3*x(2)^2;
+ // f(3)= x(1) + 3*x(2) -18;
+ // f(4)= -x(1) - x(2);
+ // f(5)= x(1) + x(2) - 8;
+ // endfunction
+ // // The initial guess
+ // x0 = [0.1,0.1];
+ // // The expected solution : only 4 digits are guaranteed
+ // //xopt = [4 4]
+ // //fopt = [0 -64 -2 -8 0]
+ // maxfopt = 0
+ // // Run fminimax
+ // [xopt,fopt,maxfval,exitflag,output,lambda] = fminimax(myfun, x0)
+ // // Press ENTER to continue
+ //
+ // Examples
+ // // A case where we provide the gradient of the objective
+ // // functions and the Jacobian matrix of the constraints.
+ // // The objective function and its gradient
+ // function f = myfun(x)
+ // f(1)= 2*x(1)^2 + x(2)^2 - 48*x(1) - 40*x(2) + 304;
+ // f(2)= -x(1)^2 - 3*x(2)^2;
+ // f(3)= x(1) + 3*x(2) -18;
+ // f(4)= -x(1) - x(2);
+ // f(5)= x(1) + x(2) - 8;
+ // endfunction
+ // // Defining gradient of myfun
+ // function G = myfungrad(x)
+ // G = [ 4*x(1) - 48, -2*x(1), 1, -1, 1;
+ // 2*x(2) - 40, -6*x(2), 3, -1, 1; ]'
+ // endfunction
+ // // The nonlinear constraints and the Jacobian
+ // // matrix of the constraints
+ // function [c,ceq] = confun(x)
+ // // Inequality constraints
+ // c = [1.5 + x(1)*x(2) - x(1) - x(2), -x(1)*x(2) - 10]
+ // // No nonlinear equality constraints
+ // ceq=[]
+ // endfunction
+ // // Defining gradient of confungrad
+ // function [DC,DCeq] = cgrad(x)
+ // // DC(:,i) = gradient of the i-th constraint
+ // // DC = [
+ // // Dc1/Dx1 Dc1/Dx2
+ // // Dc2/Dx1 Dc2/Dx2
+ // // ]
+ // DC= [
+ // x(2)-1, -x(2)
+ // x(1)-1, -x(1)
+ // ]'
+ // DCeq = []'
+ // endfunction
+ // // Test with both gradient of objective and gradient of constraints
+ // minimaxOptions = list("GradObj",myfungrad,"GradCon",cgrad);
+ // // The initial guess
+ // x0 = [0,10];
+ // // The expected solution : only 4 digits are guaranteed
+ // //xopt = [0.92791 7.93551]
+ // //fopt = [6.73443 -189.778 6.73443 -8.86342 0.86342]
+ // maxfopt = 6.73443
+ // // Run fminimax
+ // [xopt,fopt,maxfval,exitflag,output] = fminimax(myfun,x0,[],[],[],[],[],[], confun, minimaxOptions)
+ // Authors
+ // Animesh Baranawal
+
+ // Check number of input and output arguments
+ [minmaxLhs,minmaxRhs] = argn()
+ fminimaxCheckrhs("fminimax", minmaxRhs, [2 4 6 8 9 10])
+ fminimaxChecklhs("fminimax", minmaxLhs, 1:7)
+
+ // Proper initialisation of objective function
+ minmaxObjfun = varargin(1)
+ fminimaxChecktype("fminimax", minmaxObjfun, "minmaxObjfun", 1, "function")
+
+ // Proper initialisation of starting point
+ minmaxStartpoint = varargin(2)
+ fminimaxChecktype("fminimax", minmaxStartpoint, "minmaxStartpoint", 2, "constant")
+
+ minmaxNumvar = size(minmaxStartpoint,"*")
+ fminimaxCheckvector("fminimax", minmaxStartpoint, "minmaxStartpoint", 2, minmaxNumvar)
+ minmaxStartpoint = minmaxStartpoint(:)
+
+ // Proper initialisation of A and b
+ if(minmaxRhs < 3) then // if A and b are not provided, declare as empty
+ minmaxA = []
+ minmaxB = []
+ else
+ minmaxA = varargin(3)
+ minmaxB = varargin(4)
+ end
+
+ fminimaxChecktype("fminimax", minmaxA, "A", 3, "constant")
+ fminimaxChecktype("fminimax", minmaxB, "b", 4, "constant")
+
+ // Check if A and b of proper dimensions
+ if(minmaxA <> [] & minmaxB == []) then
+ errmsg = msprintf(gettext("%s: Incompatible input arguments #%d and #%d: matrix A is empty, but the column vector b is not empty"), "fminimax", 3, 4)
+ error(errmsg)
+ end
+
+ if(minmaxA == [] & minmaxB <> []) then
+ errmsg = msprintf(gettext("%s: Incompatible input arguments #%d and #%d: matrix A is not empty, but the column vector b is empty"), "fminimax", 3, 4)
+ error(errmsg)
+ end
+
+ minmaxNumrowA = size(minmaxA,"r")
+ if(minmaxA <> []) then
+ fminimaxCheckdims("fminimax", minmaxA, "A", 3, [minmaxNumrowA minmaxNumvar])
+ fminimaxCheckvector("fminimax", minmaxB, "b", 4, minmaxNumrowA)
+ minmaxB = minmaxB(:)
+ end
+
+ // Proper initialisation of Aeq and beq
+ if(minmaxRhs < 5) then // if Aeq and beq are not provided, declare as empty
+ minmaxAeq = []
+ minmaxBeq = []
+ else
+ minmaxAeq = varargin(5)
+ minmaxBeq = varargin(6)
+ end
+
+ fminimaxChecktype("fminimax", minmaxAeq, "Aeq", 5, "constant")
+ fminimaxChecktype("fminimax", minmaxBeq, "beq", 6, "constant")
+
+ // Check if Aeq and beq of proper dimensions
+ if(minmaxAeq <> [] & minmaxBeq == []) then
+ errmsg = msprintf(gettext("%s: Incompatible input arguments #%d and #%d: matrix Aeq is empty, but the column vector beq is not empty"), "fminimax", 5, 6)
+ error(errmsg)
+ end
+
+ if(minmaxAeq == [] & minmaxBeq <> []) then
+ errmsg = msprintf(gettext("%s: Incompatible input arguments #%d and #%d: matrix Aeq is not empty, but the column vector beq is empty"), "fminimax", 5, 6)
+ error(errmsg)
+ end
+
+ minmaxNumrowAeq = size(minmaxAeq,"r")
+ if(minmaxAeq <> []) then
+ fminimaxCheckdims("fminimax", minmaxAeq, "Aeq", 5, [minmaxNumrowAeq minmaxNumvar])
+ fminimaxCheckvector("fminimax", minmaxBeq, "beq", 6, minmaxNumrowAeq)
+ minmaxBeq = minmaxBeq(:)
+ end
+
+ // Proper initialisation of minmaxLb and minmaxUb
+ if(minmaxRhs < 7) then // if minmaxLb and minmaxUb are not provided, declare as empty
+ minmaxLb = []
+ minmaxUb = []
+ else
+ minmaxLb = varargin(7)
+ minmaxUb = varargin(8)
+ end
+
+ fminimaxChecktype("fminimax", minmaxLb, "lb", 7, "constant")
+ fminimaxChecktype("fminimax", minmaxUb, "ub", 8, "constant")
+
+ // Check dimensions of minmaxLb and minmaxUb
+ if(minmaxLb <> []) then
+ fminimaxCheckvector("fminimax", minmaxLb, "lb", 7, minmaxNumvar)
+ minmaxLb = minmaxLb(:)
+ end
+
+ if(minmaxUb <> []) then
+ fminimaxCheckvector("fminimax", minmaxUb, "ub", 8, minmaxNumvar)
+ minmaxUb = minmaxUb(:)
+ end
+
+ // Proper Initialisation of minmaxNonlinfun
+ if(minmaxRhs < 9) then // if minmaxNonlinfun is not provided, declare as empty
+ minmaxNonlinfun = []
+ else
+ minmaxNonlinfun = varargin(9)
+ end
+
+ // fmincon library of scilab gives error when 'c' component of minmaxNonlinfun empty
+ // add a trivial case of -5 <= 0 to c to bypass this error
+ if(minmaxNonlinfun == []) then
+ function [c,ceq] = t(z)
+ c = []
+ ceq = []
+ endfunction
+ minmaxNonlinfun = t
+ end
+
+ fminimaxChecktype("fminimax", minmaxNonlinfun, "nonlinfun", 9, "function")
+
+ //To check, Whether minimaxOptions is been entered by user
+ if ( minmaxRhs<10 ) then
+ minmaxUserOptions = list();
+ else
+ minmaxUserOptions = varargin(10); //Storing the 3rd Input minmaxUserOptionseter in intermediate list named 'minmaxUserOptions'
+ end
+
+ //If minimaxOptions is entered then checking its type for 'list'
+ if (type(minmaxUserOptions) ~= 15) then
+ errmsg = msprintf(gettext("%s: minimaxOptions (10th parameter) should be a list"), "fminimax");
+ error(errmsg);
+ end
+
+ //If minimaxOptions is entered then checking whether even number of entires are entered
+ if (modulo(size(minmaxUserOptions),2)) then
+ errmsg = msprintf(gettext("%s: Size of minimaxOptions (list) should be even"), "fminimax");
+ error(errmsg);
+ end
+
+ //Flags to check whether Gradient is "ON"/"OFF" and store values of user options
+ flag1=0;
+ flag2=0;
+ minmaxMaxIter = 3000
+ minmaxCPU = 600
+ minmaxFGrad=[];
+ minmaxCGrad=[];
+
+ //To check the User Entry for Options and storing it
+ for i = 1:(size(minmaxUserOptions))/2
+ select minmaxUserOptions(2*i-1)
+ case "MaxIter" then
+ minmaxIter = minmaxUserOptions(2*i); //Setting the Maximum Iteration as per user entry
+
+ case "CpuTime" then
+ minmaxCPU = minmaxUserOptions(2*i); //Setting the Maximum CPU Time as per user entry
+
+ case "GradObj" then
+ flag1=1;
+ minmaxFGrad = minmaxUserOptions(2*i);
+
+ case "GradCon" then
+ flag2=1;
+ minmaxCGrad = minmaxUserOptions(2*i);
+
+ else
+ errmsg = msprintf(gettext("%s: Unrecognized minmaxUserOptionseter name ''%s''."), "fminimax", minmaxUserOptions(2*i-1));
+ error(errmsg)
+ end
+ end
+
+ // Checking if minmaxFGrad and minmaxCGrad are functions
+ if (flag1==1) then
+ if (type(minmaxFGrad) ~= 11 & type(minmaxFGrad) ~= 13) then
+ errmsg = msprintf(gettext("%s: Expected function for Gradient of Objective"), "fminimax");
+ error(errmsg);
+ end
+ end
+
+ if (flag2==1) then
+ if (type(minmaxCGrad) ~= 11 & type(minmaxCGrad) ~= 13) then
+ errmsg = msprintf(gettext("%s: Expected function for Gradient of Nonlinfun"), "fminimax");
+ error(errmsg);
+ end
+ end
+
+
+ // Reformulating the problem fminimax to fmincon
+ minmaxObjfunval = minmaxObjfun(minmaxStartpoint)
+ minmaxStartpoint(minmaxNumvar+1) = max(minmaxObjfunval)
+
+ if(minmaxA <> []) then
+ minmaxA = [minmaxA, zeros(minmaxNumrowA,1)]
+ end
+ if(minmaxAeq <> []) then
+ minmaxAeq = [minmaxAeq, zeros(minmaxNumrowAeq,1)]
+ end
+ if(minmaxLb <> []) then
+ minmaxLb(minmaxNumvar+1) = -%inf
+ end
+ if(minmaxUb <> []) then
+ minmaxUb(minmaxNumvar+1) = +%inf
+ end
+
+ // function handle defining the additional inequalities
+ function temp = minmaxAddIneq(z)
+ temp = minmaxObjfun(z) - z(minmaxNumvar+1)
+ endfunction
+
+ // function handle defining new objective function
+ function newfunc = newObjfun(z)
+ newfunc = z(minmaxNumvar+1)
+ endfunction
+
+ // function handle defining add_ineq derivative using numderivative
+ function func = minmaxObjDer(z)
+ func = numderivative(minmaxAddIneq,z)
+ endfunction
+
+ // function handle defining minmaxNonlinfun derivative using numderivative
+ function [dc,dceq] = minmaxNonlinDer(z)
+ // function handle extracting c and ceq components from minmaxNonlinfun
+ function foo = minmaxC(z)
+ [foo,tmp1] = minmaxNonlinfun(z)
+ foo = foo'
+ endfunction
+
+ function foo = minmaxCEQ(z)
+ [tmp1,foo] = minmaxNonlinfun(z)
+ foo = foo'
+ endfunction
+
+ dc = numderivative(minmaxC,z)
+ dceq = numderivative(minmaxCEQ,z)
+ endfunction
+
+ // function handle defining new minmaxNonlinfun function
+ function [nc,nceq] = newNonlinfun(z)
+ [nc,nceq] = minmaxNonlinfun(z)
+ // add inequalities of the form Fi(x) - y <= 0
+ tmp = [minmaxObjfun(z) - z(minmaxNumvar+1)]'
+ nc = [nc, tmp]
+ endfunction
+
+ // function handle defining new gradient function for non-linear constraints
+ // this function passed when the gradient feature is on
+ function [dnc,dnceq] = newCGrad(z)
+
+ // if constraint gradient present use it
+ if(flag2 == 1) then
+ [dnc, dnceq] = minmaxCGrad(z)
+ dnc = [dnc, zeros(size(dnc,'r'),1)]
+ dnceq = [dnceq, zeros(size(dnceq,'r'),1)]
+ else
+ // else use numderivative method to calculate gradient of constraints
+ [dnc, dnceq] = minmaxNonlinDer(z)
+ end
+
+ // if objective gradient is present use it
+ if(flag1 == 1) then
+ derObjfun = minmaxFGrad(z)
+ mderObjfun = [derObjfun, -1*ones(size(derObjfun,'r'),1)]
+ dnc = [dnc; mderObjfun]
+ else
+ // else use numderivative to calculate gradient of set of obj functions
+ derObjfun = minmaxObjDer(z)
+ dnc = [dnc; derObjfun]
+ end
+ endfunction
+
+ // to be passed as minimaxOptions to fmincon
+ if(flag1 == 1 | flag2 == 1) then
+ minmaxPassOptions = list("MaxIter", minmaxMaxIter, "CpuTime", minmaxCPU, "GradCon", newCGrad)
+ [x,fval,exitflag,output,lambda] = ...
+ fmincon(newObjfun,minmaxStartpoint,minmaxA,minmaxB,minmaxAeq,minmaxBeq,minmaxLb,minmaxUb,newNonlinfun,minmaxPassOptions)
+
+ x = x(1:minmaxNumvar)
+ fval = minmaxObjfun(x)
+ maxfval = max(fval)
+ else
+ minmaxPassOptions = list("MaxIter", minmaxMaxIter, "CpuTime", minmaxCPU)
+ [x,fval,exitflag,output,lambda] = ...
+ fmincon(newObjfun,minmaxStartpoint,minmaxA,minmaxB,minmaxAeq,minmaxBeq,minmaxLb,minmaxUb,newNonlinfun,minmaxPassOptions)
+
+ x = x(1:minmaxNumvar)
+ fval = minmaxObjfun(x)
+ maxfval = max(fval)
+ end
+
+
+endfunction
diff --git a/macros/fminimaxCheckdims.bin b/macros/fminimaxCheckdims.bin
new file mode 100644
index 0000000..be1ed46
Binary files /dev/null and b/macros/fminimaxCheckdims.bin differ
diff --git a/macros/fminimaxCheckdims.sci b/macros/fminimaxCheckdims.sci
new file mode 100644
index 0000000..83cb5b5
--- /dev/null
+++ b/macros/fminimaxCheckdims.sci
@@ -0,0 +1,56 @@
+
+// Copyright (C) 2010 - DIGITEO - Michael Baudin
+//
+// This file must be used under the terms of the GNU LGPL license.
+
+function errmsg = fminimaxCheckdims ( funname , var , varname , ivar , matdims )
+ // Generates an error if the variable has not the required size.
+ //
+ // Calling Sequence
+ // errmsg = fminimaxCheckdims ( funname , var , varname , ivar , matdims )
+ //
+ // Parameters
+ // funname : a 1 x 1 matrix of strings, the name of the calling function.
+ // var : a 1 x 1 matrix of valid Scilab data type, the variable
+ // varname : a 1 x 1 matrix of string, the name of the variable
+ // ivar : a 1 x 1 matrix of floating point integers, the index of the input argument in the calling sequence
+ // matdims : 1 x 2 matrix of floating point integers, the number of rows, columns for the variable #ivar
+ // errmsg : a 1 x 1 matrix of strings, the error message. If there was no error, the error message is the empty matrix.
+ //
+ // Description
+ // This function is designed to be used to design functions where
+ // the input argument has a known shape.
+ // This function cannot be use when var is a function, or more
+ // generally, for any input argument for which the size function
+ // does not work.
+ // Last update : 05/08/2010.
+ //
+ // Examples
+ // // The function takes a 2 x 3 matrix of doubles.
+ // function y = myfunction ( x )
+ // fminimaxCheckdims ( "myfunction" , x , "x" , 1 , [2 3] )
+ // y = x
+ // endfunction
+ // // Calling sequences which work
+ // y = myfunction ( ones(2,3) )
+ // y = myfunction ( zeros(2,3) )
+ // // Calling sequences which generate an error
+ // y = myfunction ( ones(1,3) )
+ // y = myfunction ( zeros(2,4) )
+ //
+ // Authors
+ // Michael Baudin - 2010 - DIGITEO
+ //
+
+ [lhs,rhs]=argn()
+ fminimaxCheckrhs ( "fminimaxCheckdims" , rhs , 5 )
+ fminimaxChecklhs ( "fminimaxCheckdims" , lhs , [0 1] )
+
+ errmsg = []
+ if ( or ( size(var) <> matdims ) ) then
+ strexp = strcat(string(matdims)," ")
+ strcomp = strcat(string(size(var))," ")
+ errmsg = msprintf(gettext("%s: Expected size [%s] for input argument %s at input #%d, but got [%s] instead."), funname, strexp, varname , ivar , strcomp );
+ error(errmsg)
+ end
+endfunction
diff --git a/macros/fminimaxChecklhs.bin b/macros/fminimaxChecklhs.bin
new file mode 100644
index 0000000..7889084
Binary files /dev/null and b/macros/fminimaxChecklhs.bin differ
diff --git a/macros/fminimaxChecklhs.sci b/macros/fminimaxChecklhs.sci
new file mode 100644
index 0000000..78e4013
--- /dev/null
+++ b/macros/fminimaxChecklhs.sci
@@ -0,0 +1,79 @@
+// Copyright (C) 2010 - DIGITEO - Michael Baudin
+//
+// This file must be used under the terms of the GNU LGPL license.
+
+function errmsg = fminimaxChecklhs ( funname , lhs , lhsset )
+ // Generates an error if the number of LHS is not in given set.
+ //
+ // Calling Sequence
+ // errmsg = fminimaxChecklhs ( funname , lhs , lhsset )
+ //
+ // Parameters
+ // funname : a 1 x 1 matrix of strings, the name of the calling function.
+ // lhs : a 1 x 1 matrix of floating point integers, the actual number of output arguments
+ // lhsset : a 1 x n or n x 1 matrix of floating point integers, the authorized number of output arguments
+ // errmsg : a 1 x 1 matrix of strings, the error message. If there was no error, the error message is the empty matrix.
+ //
+ // Description
+ // This function is designed to be used to design functions with
+ // variable number of output arguments.
+ // Notice that it is useless to call this function if the
+ // function definition does not use the varargout statement.
+ // Notice that a function as a minimum of 1 output argument.
+ // Last update : 29/07/2010.
+ //
+ // Examples
+ // // The function takes 3 input arguments and 1/2 output arguments
+ // function varargout = myfunction ( x1 , x2 , x3 )
+ // [lhs, rhs] = argn()
+ // fminimaxCheckrhs ( "myfunction" , rhs , 3 : 3 )
+ // fminimaxChecklhs ( "myfunction" , lhs , 1 : 2 )
+ // y1 = x1 + x2
+ // y2 = x2 + x3
+ // varargout(1) = y1
+ // if ( lhs == 2 ) then
+ // varargout(2) = y2
+ // end
+ // endfunction
+ // // Calling sequences which work
+ // myfunction ( 1 , 2 , 3 )
+ // y1 = myfunction ( 1 , 2 , 3 )
+ // [ y1 , y2 ] = myfunction ( 1 , 2 , 3 )
+ // // Calling sequences which generate an error
+ // [ y1 , y2 , y3 ] = myfunction ( 1 , 2 , 3 )
+ //
+ // // The function takes 1 or 3 output arguments, but not 2
+ // function varargout = myfunction ( x1 , x2 , x3 )
+ // [lhs, rhs] = argn()
+ // fminimaxCheckrhs ( "myfunction" , rhs , 3 : 3 )
+ // fminimaxChecklhs ( "myfunction" , lhs , [1 3] )
+ // y1 = x1 + x2
+ // y2 = x2 + x3
+ // y3 = x1 + x3
+ // varargout(1) = y1
+ // if ( lhs == 3 ) then
+ // varargout(2) = y2
+ // varargout(3) = y3
+ // end
+ // endfunction
+ // // Calling sequences which work
+ // myfunction ( 1 , 2 , 3 )
+ // y1 = myfunction ( 1 , 2 , 3 )
+ // [ y1 , y2 , y3 ] = myfunction ( 1 , 2 , 3 )
+ // // Calling sequences which generate an error
+ // [y1 , y2] = myfunction ( 1 , 2 , 3 )
+ //
+ // Authors
+ // Michael Baudin - 2010 - DIGITEO
+ //
+
+ errmsg = []
+ if ( and ( lhs <> lhsset ) ) then
+ lhsstr = strcat(string(lhsset)," ")
+ errmsg = msprintf(gettext("%s: Unexpected number of output arguments : %d provided while the expected number of output arguments should be in the set [%s]."), funname , lhs , lhsstr );
+ error(errmsg)
+ end
+endfunction
+
+
+
diff --git a/macros/fminimaxCheckrhs.bin b/macros/fminimaxCheckrhs.bin
new file mode 100644
index 0000000..6b190f7
Binary files /dev/null and b/macros/fminimaxCheckrhs.bin differ
diff --git a/macros/fminimaxCheckrhs.sci b/macros/fminimaxCheckrhs.sci
new file mode 100644
index 0000000..7877153
--- /dev/null
+++ b/macros/fminimaxCheckrhs.sci
@@ -0,0 +1,102 @@
+// Copyright (C) 2010 - DIGITEO - Michael Baudin
+//
+// This file must be used under the terms of the GNU LGPL license.
+
+function errmsg = fminimaxCheckrhs ( funname , rhs , rhsset )
+ // Generates an error if the number of RHS is not in given set.
+ //
+ // Calling Sequence
+ // errmsg = fminimaxCheckrhs ( funname , rhs , rhsset )
+ //
+ // Parameters
+ // funname : a 1 x 1 matrix of strings, the name of the calling function.
+ // rhs : a 1 x 1 matrix of floating point integers, the actual number of input arguments
+ // rhsset : a 1 x n or n x 1 matrix of floating point integers, the authorized number of input arguments
+ // errmsg : a 1 x 1 matrix of strings, the error message. If there was no error, the error message is the empty matrix.
+ //
+ // Description
+ // This function is designed to be used to design functions with
+ // variable number of input arguments.
+ // Notice that it is useless to call this function if the
+ // function definition does not use the varargin statement.
+ // Last update : 05/08/2010.
+ // Last update : 29/07/2010.
+ //
+ // Examples
+ // // The function takes 2/3 input arguments and 1 output arguments
+ // function y = myfunction ( varargin )
+ // [lhs, rhs] = argn()
+ // fminimaxCheckrhs ( "myfunction" , rhs , 2:3 )
+ // fminimaxChecklhs ( "myfunction" , lhs , 1 )
+ // x1 = varargin(1)
+ // x2 = varargin(2)
+ // if ( rhs >= 3 ) then
+ // x3 = varargin(3)
+ // else
+ // x3 = 2
+ // end
+ // y = x1 + x2 + x3
+ // endfunction
+ // // Calling sequences which work
+ // y = myfunction ( 1 , 2 )
+ // y = myfunction ( 1 , 2 , 3 )
+ // // Calling sequences which generate an error
+ // y = myfunction ( 1 )
+ // y = myfunction ( 1 , 2 , 3 , 4 )
+ //
+ // // The function takes 2 or 4 input arguments, but not 3
+ // function y = myfunction ( varargin )
+ // [lhs, rhs] = argn()
+ // fminimaxCheckrhs ( "myfunction" , rhs , [2 4] )
+ // fminimaxChecklhs ( "myfunction" , lhs , 1 )
+ // x1 = varargin(1)
+ // x2 = varargin(2)
+ // if ( rhs >= 3 ) then
+ // x3 = varargin(3)
+ // x4 = varargin(4)
+ // else
+ // x3 = 2
+ // x4 = 3
+ // end
+ // y = x1 + x2 + x3 + x4
+ // endfunction
+ // // Calling sequences which work
+ // y = myfunction ( 1 , 2 )
+ // y = myfunction ( 1 , 2 , 3 , 4 )
+ // // Calling sequences which generate an error
+ // y = myfunction ( 1 )
+ // y = myfunction ( 1 , 2 , 3 )
+ // y = myfunction ( 1 , 2 , 3 , 4, 5 )
+ //
+ // // The function takes 2 input arguments and 0/1 output arguments.
+ // // Notice that if the checkrhs function is not called,
+ // // the variable x2 might be used from the user's context,
+ // // that is, if the caller has defined the variable x2, it
+ // // is used in the callee.
+ // // Here, we want to avoid this.
+ // function y = myfunction ( x1 , x2 )
+ // [lhs, rhs] = argn()
+ // fminimaxCheckrhs ( "myfunction" , rhs , 2 )
+ // fminimaxChecklhs ( "myfunction" , lhs , [0 1] )
+ // y = x1 + x2
+ // endfunction
+ // // Calling sequences which work
+ // y = myfunction ( 1 , 2 )
+ // // Calling sequences which generate an error
+ // y = myfunction ( 1 )
+ // y = myfunction ( 1 , 2 , 3 )
+ //
+ // Authors
+ // Michael Baudin - 2010 - DIGITEO
+ //
+
+ errmsg = []
+ if ( and(rhs <> rhsset) ) then
+ rhsstr = strcat(string(rhsset)," ")
+ errmsg = msprintf(gettext("%s: Unexpected number of input arguments : %d provided while the number of expected input arguments should be in the set [%s]."), funname , rhs , rhsstr );
+ error(errmsg)
+ end
+endfunction
+
+
+
diff --git a/macros/fminimaxChecktype.bin b/macros/fminimaxChecktype.bin
new file mode 100644
index 0000000..4f42fe0
Binary files /dev/null and b/macros/fminimaxChecktype.bin differ
diff --git a/macros/fminimaxChecktype.sci b/macros/fminimaxChecktype.sci
new file mode 100644
index 0000000..48514b6
--- /dev/null
+++ b/macros/fminimaxChecktype.sci
@@ -0,0 +1,65 @@
+// Copyright (C) 2010 - DIGITEO - Michael Baudin
+//
+// This file must be used under the terms of the GNU LGPL license.
+
+function errmsg = fminimaxChecktype ( funname , var , varname , ivar , expectedtype )
+ // Generates an error if the given variable is not of expected type.
+ //
+ // Calling Sequence
+ // errmsg = fminimaxChecktype ( funname , var , varname , ivar , expectedtype )
+ //
+ // Parameters
+ // funname : a 1 x 1 matrix of strings, the name of the calling function.
+ // var : a 1 x 1 matrix of valid Scilab data type, the variable
+ // varname : a 1 x 1 matrix of string, the name of the variable
+ // ivar : a 1 x 1 matrix of floating point integers, the index of the input argument in the calling sequence
+ // expectedtype : a n x 1 or 1 x n matrix of strings, the available types for the variable #ivar
+ // errmsg : a 1 x 1 matrix of strings, the error message. If there was no error, the error message is the empty matrix.
+ //
+ // Description
+ // This function is designed to be used to design functions with
+ // input arguments with variable type.
+ // We use the typeof function to compute the type of the variable:
+ // see help typeof to get the list of all available values for expectedtype.
+ // Last update : 29/07/2010.
+ //
+ // Examples
+ // // The function takes a string argument.
+ // function myfunction ( x )
+ // fminimaxChecktype ( "myfunction" , x , "x" , 1 , "string" )
+ // disp("This is a string")
+ // endfunction
+ // // Calling sequences which work
+ // myfunction ( "Scilab" )
+ // // Calling sequences which generate an error
+ // myfunction ( 123456 )
+ //
+ // // The function takes a string or a matrix of doubles argument.
+ // function myfunction ( x )
+ // fminimaxChecktype ( "myfunction" , x , "x" , 1 , [ "string" "constant" ] )
+ // if ( typeof(x) == "string" ) then
+ // disp("This is a matrix of strings")
+ // else
+ // disp("This is a matrix of doubles")
+ // end
+ // endfunction
+ // // Calling sequences which work
+ // myfunction ( "Scilab" )
+ // myfunction ( 123456 )
+ // // Calling sequences which generate an error
+ // myfunction ( uint8(2) )
+ //
+ // Authors
+ // Michael Baudin - 2010 - DIGITEO
+ //
+
+ errmsg = []
+ if ( and ( typeof ( var ) <> expectedtype ) ) then
+ strexp = """" + strcat(expectedtype,""" or """) + """"
+ errmsg = msprintf(gettext("%s: Expected type [%s] for input argument %s at input #%d, but got ""%s"" instead."),funname, strexp, varname , ivar , typeof(var) );
+ error(errmsg);
+ end
+endfunction
+
+
+
diff --git a/macros/fminimaxCheckvector.bin b/macros/fminimaxCheckvector.bin
new file mode 100644
index 0000000..28af2c5
Binary files /dev/null and b/macros/fminimaxCheckvector.bin differ
diff --git a/macros/fminimaxCheckvector.sci b/macros/fminimaxCheckvector.sci
new file mode 100644
index 0000000..87c8553
--- /dev/null
+++ b/macros/fminimaxCheckvector.sci
@@ -0,0 +1,63 @@
+// Copyright (C) 2010 - DIGITEO - Michael Baudin
+//
+// This file must be used under the terms of the GNU LGPL license.
+
+function errmsg = fminimaxCheckvector ( funname , var , varname , ivar , nbval )
+ // Generates an error if the variable is not a vector.
+ //
+ // Calling Sequence
+ // errmsg = fminimaxCheckvector ( funname , var , varname , ivar )
+ //
+ // Parameters
+ // funname : a 1 x 1 matrix of strings, the name of the calling function.
+ // var : a 1 x 1 matrix of valid Scilab data type, the variable
+ // varname : a 1 x 1 matrix of string, the name of the variable
+ // ivar : a 1 x 1 matrix of floating point integers, the index of the input argument in the calling sequence
+ // nbval : a 1 x 1 matrix of floating point integers, the number of entries in the vector.
+ // errmsg : a 1 x 1 matrix of strings, the error message. If there was no error, the error message is the empty matrix.
+ //
+ // Description
+ // This function is designed to be used to design functions where
+ // the input argument is a vector, that is, a matrix for which
+ // nrows == 1 or ncols == 1.
+ // This function cannot be use when var is a function, or more
+ // generally, for any input argument for which the size function
+ // does not work.
+ //
+ // Examples
+ // // The function takes a vector of 3 doubles.
+ // function y = myfunction ( x )
+ // fminimaxCheckvector ( "myfunction" , x , "x" , 1 , 3 )
+ // y = x
+ // endfunction
+ // // Calling sequences which work
+ // y = myfunction ( ones(1,3) )
+ // y = myfunction ( zeros(3,1) )
+ // // Calling sequences which generate an error
+ // // The following are not vectors
+ // y = myfunction ( ones(2,3) )
+ // y = myfunction ( zeros(3,2) )
+ // // The following have the wrong number of entries
+ // y = myfunction ( ones(1,3) )
+ //
+ // Authors
+ // Michael Baudin - 2010 - DIGITEO
+ //
+
+ errmsg = []
+ nrows = size(var,"r")
+ ncols = size(var,"c")
+ if ( nrows <> 1 & ncols <> 1 ) then
+ strcomp = strcat(string(size(var))," ")
+ errmsg = msprintf(gettext("%s: Expected a vector matrix for input argument %s at input #%d, but got [%s] instead."), funname, varname , ivar , strcomp );
+ error(errmsg)
+ end
+ if ( ( nrows == 1 & ncols <> nbval ) | ( ncols == 1 & nrows <> nbval ) ) then
+ strcomp = strcat(string(size(var))," ")
+ errmsg = msprintf(gettext("%s: Expected %d entries for input argument %s at input #%d, but current dimensions are [%s] instead."), funname, nbval , varname , ivar , strcomp );
+ error(errmsg)
+ end
+endfunction
+
+
+
diff --git a/macros/fminunc.bin b/macros/fminunc.bin
new file mode 100644
index 0000000..b65c851
Binary files /dev/null and b/macros/fminunc.bin differ
diff --git a/macros/fminunc.sci b/macros/fminunc.sci
new file mode 100644
index 0000000..07fa92a
--- /dev/null
+++ b/macros/fminunc.sci
@@ -0,0 +1,427 @@
+// 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: R.Vidyadhar & Vignesh Kannan
+// Organization: FOSSEE, IIT Bombay
+// Email: toolbox@scilab.in
+
+function [xopt,fopt,exitflag,output,gradient,hessian] = fminunc (varargin)
+ // Solves a multi-variable unconstrainted optimization problem
+ //
+ // Calling Sequence
+ // xopt = fminunc(f,x0)
+ // xopt = fminunc(f,x0,options)
+ // [xopt,fopt] = fminunc(.....)
+ // [xopt,fopt,exitflag]= fminunc(.....)
+ // [xopt,fopt,exitflag,output]= fminunc(.....)
+ // [xopt,fopt,exitflag,output,gradient]=fminunc(.....)
+ // [xopt,fopt,exitflag,output,gradient,hessian]=fminunc(.....)
+ //
+ // Parameters
+ // f : a function, representing the objective function of the problem
+ // x0 : a vector of doubles, containing the starting of variables.
+ // options: a list, containing the option for user to specify. See below for details.
+ // xopt : a vector of doubles, the computed solution of the optimization problem.
+ // fopt : a scalar of double, the function value at x.
+ // exitflag : a scalar of integer, containing the flag which denotes the reason for termination of algorithm. See below for details.
+ // output : a structure, containing the information about the optimization. See below for details.
+ // gradient : a vector of doubles, containing the the gradient of the solution.
+ // hessian : a matrix of doubles, containing the the hessian of the solution.
+ //
+ // Description
+ // Search the minimum of an unconstrained optimization problem specified by :
+ // Find the minimum of f(x) such that
+ //
+ //
+ // \begin{eqnarray}
+ // &\mbox{min}_{x}
+ // & f(x)\\
+ // \end{eqnarray}
+ //
+ //
+ // The routine calls Ipopt for solving the Un-constrained Optimization 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.
+ //
+ // Syntax : options= list("MaxIter", [---], "CpuTime", [---], "Gradient", ---, "Hessian", ---);
+ // MaxIter : a Scalar, containing the Maximum Number of Iteration that the solver should take.
+ // CpuTime : a Scalar, containing the Maximum amount of CPU Time that the solver should take.
+ // Gradient : a function, representing the gradient function of the Objective in Vector Form.
+ // Hessian : a function, representing the hessian function of the Objective in Symmetric Matrix Form.
+ // Default Values : options = list("MaxIter", [3000], "CpuTime", [600]);
+ //
+ //
+ // The exitflag allows to know the status of the optimization which is given back by Ipopt.
+ //
+ // exitflag=0 : Optimal Solution Found
+ // exitflag=1 : Maximum Number of Iterations Exceeded. Output may not be optimal.
+ // exitflag=2 : Maximum CPU Time exceeded. Output may not be optimal.
+ // exitflag=3 : Stop at Tiny Step.
+ // exitflag=4 : Solved To Acceptable Level.
+ // exitflag=5 : Converged to a point of local infeasibility.
+ //
+ //
+ // 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.
+ //
+ // output.Iterations: The number of iterations performed during the search
+ // output.Cpu_Time: The total cpu-time spend during the search
+ // output.Objective_Evaluation: The number of Objective Evaluations performed during the search
+ // output.Dual_Infeasibility: The Dual Infeasiblity of the final soution
+ //
+ //
+ // Examples
+ // //Find x in R^2 such that it minimizes the Rosenbrock function
+ // //f = 100*(x2 - x1^2)^2 + (1-x1)^2
+ // //Objective function to be minimised
+ // function y= f(x)
+ // y= 100*(x(2) - x(1)^2)^2 + (1-x(1))^2;
+ // endfunction
+ // //Starting point
+ // x0=[-1,2];
+ // //Gradient of objective function
+ // function y= fGrad(x)
+ // y= [-400*x(1)*x(2) + 400*x(1)^3 + 2*x(1)-2, 200*(x(2)-x(1)^2)];
+ // endfunction
+ // //Hessian of Objective Function
+ // function y= fHess(x)
+ // y= [1200*x(1)^2- 400*x(2) + 2, -400*x(1);-400*x(1), 200 ];
+ // endfunction
+ // //Options
+ // options=list("MaxIter", [1500], "CpuTime", [500], "Gradient", fGrad, "Hessian", fHess);
+ // //Calling Ipopt
+ // [xopt,fopt,exitflag,output,gradient,hessian]=fminunc(f,x0,options)
+ // // Press ENTER to continue
+ //
+ // Examples
+ // //Find x in R^2 such that the below function is minimum
+ // //f = x1^2 + x2^2
+ // //Objective function to be minimised
+ // function y= f(x)
+ // y= x(1)^2 + x(2)^2;
+ // endfunction
+ // //Starting point
+ // x0=[2,1];
+ // //Calling Ipopt
+ // [xopt,fopt]=fminunc(f,x0)
+ // // Press ENTER to continue
+ //
+ // Examples
+ // //The below problem is an unbounded problem:
+ // //Find x in R^2 such that the below function is minimum
+ // //f = - x1^2 - x2^2
+ // //Objective function to be minimised
+ // function y= f(x)
+ // y= -x(1)^2 - x(2)^2;
+ // endfunction
+ // //Starting point
+ // x0=[2,1];
+ // //Gradient of objective function
+ // function y= fGrad(x)
+ // y= [-2*x(1),-2*x(2)];
+ // endfunction
+ // //Hessian of Objective Function
+ // function y= fHess(x)
+ // y= [-2,0;0,-2];
+ // endfunction
+ // //Options
+ // options=list("MaxIter", [1500], "CpuTime", [500], "Gradient", fGrad, "Hessian", fHess);
+ // //Calling Ipopt
+ // [xopt,fopt,exitflag,output,gradient,hessian]=fminunc(f,x0,options)
+ // Authors
+ // R.Vidyadhar , Vignesh Kannan
+
+
+ //To check the number of input and output arguments
+ [lhs , rhs] = argn();
+
+ //To check the number of arguments given by the user
+ if ( rhs<2 | rhs>5 ) then
+ errmsg = msprintf(gettext("%s: Unexpected number of input arguments : %d provided while should be 2 or 5"), "fminunc", rhs);
+ error(errmsg)
+ end
+
+ //Storing the 1st and 2nd Input Parameters
+ fun = varargin(1);
+ x0 = varargin(2);
+
+ //To check whether the 1st Input argument(f) is a function or not
+ if (type(f) ~= 13 & type(f) ~= 11) then
+ errmsg = msprintf(gettext("%s: Expected function for Objective "), "fminunc");
+ error(errmsg);
+ end
+
+ //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"), "fminunc");
+ error(errmsg);
+ end
+
+ //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);
+ error(errmsg);
+ else
+ if(size(x0,2)==1) then
+ x0=x0'; //Converting x0 to a row vector, if it is a column vector
+ else
+ x0=x0; //Retaining the same, if it is already a row vector
+ end
+ s=size(x0);
+ end
+
+
+ //To check the match between f (1st Parameter) and x0 (2nd Parameter)
+ if(execstr('init=fun(x0)','errcatch')==21) then
+ errmsg = msprintf(gettext("%s: Objective function and x0 did not match"), "fminunc");
+ error(errmsg);
+ end
+
+ //Converting the User defined Objective function into Required form (Error Detectable)
+ function [y,check] = f(x)
+ if(execstr('y=fun(x)','errcatch')==32 | execstr('y=fun(x)','errcatch')==27)
+ y=0;
+ check=1;
+ else
+ y=fun(x);
+ if (isreal(y)==%F) then
+ y=0;
+ check=1;
+ else
+ check=0;
+ end
+ end
+ endfunction
+
+ //To check whether options has been entered by user
+ if ( rhs<3 ) then
+ param = list();
+ else
+ param =varargin(3); //Storing the 3rd Input Parameter in an intermediate list named 'param'
+
+ end
+
+ //If options has been entered, then check its type for 'list'
+ if (type(param) ~= 15) then
+ errmsg = msprintf(gettext("%s: 3rd Input parameter should be a list (ie. Options) "), "fminunc");
+ error(errmsg);
+ end
+
+ //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 parameters should be even"), "fminunc");
+ 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
+ else //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
+ end
+ endfunction
+
+ //To set default values for options, if user doesn't enter options
+ options = list("MaxIter", [3000], "CpuTime", [600]);
+
+ //Flags to check whether Gradient is "ON"/"OFF" and Hessian is "ON"/"OFF"
+ flag1=0;
+ flag2=0;
+ fGrad=[];
+ fGrad1=[];
+ fHess=[];
+ fHess1=[];
+
+ //To check the user entry for options and store it
+ for i = 1:(size(param))/2
+ select param(2*i-1)
+ case "MaxIter" then
+ options(2*i) = param(2*i); //Setting the maximum number of iterations as per user entry
+ case "CpuTime" then
+ options(2*i) = param(2*i); //Setting the maximum CPU time as per user entry
+ case "Gradient" then
+ flag1 = 1;
+ fGrad = param(2*i);
+ case "Hessian" then
+ flag2 = 1;
+ fHess = param(2*i);
+ else
+ errmsg = msprintf(gettext("%s: Unrecognized parameter name %s."), "fminbnd", 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
+ errmsg = msprintf(gettext("%s: Expected function for Gradient of Objective"), "fminunc");
+ error(errmsg);
+ end
+
+ if(execstr('samplefGrad=fGrad(x0)','errcatch')==21)
+ errmsg = msprintf(gettext("%s: Gradient function of Objective and x0 did not match"), "fminunc");
+ error(errmsg);
+ end
+
+ samplefGrad=fGrad(x0);
+
+ 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");
+ 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
+ if (flag2==1) then
+ if (type(fHess) ~= 13 & type(fHess) ~= 11) then
+ errmsg = msprintf(gettext("%s: Expected function for Hessian of Objective"), "fminunc");
+ error(errmsg);
+ end
+
+ if(execstr('samplefHess=fHess(x0)','errcatch')==21)
+ errmsg = msprintf(gettext("%s: Hessian function of Objective and x0 did not match"), "fminunc");
+ error(errmsg);
+ end
+
+ 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");
+ error(errmsg);
+ end
+
+ function [y,check] = fHess1(x)
+ if(execstr('y=fHess(x)','errcatch')==32 | execstr('y=fHess(x)','errcatch')==27)
+ y = 0;
+ check=1;
+ else
+ y=fHess(x);
+ if (isreal(y)==%F) then
+ y = 0;
+ check=1;
+ else
+ check=0;
+ end
+ end
+ endfunction
+ end
+
+ //Calling the Ipopt function for solving the above problem
+ [xopt,fopt,status,iter,cpu,obj_eval,dual,gradient, hessian1] = solveminuncp(f,gradhess,flag1,fGrad1,flag2,fHess1,x0,options);
+
+ //Calculating the values for output
+ xopt = xopt';
+ exitflag = status;
+ output = struct("Iterations", [],"Cpu_Time",[],"Objective_Evaluation",[],"Dual_Infeasibility",[]);
+ output.Iterations = iter;
+ output.Cpu_Time = cpu;
+ output.Objective_Evaluation = obj_eval;
+ output.Dual_Infeasibility = dual;
+
+ //Converting hessian of order (1 x (numberOfVariables)^2) received from Ipopt to order (numberOfVariables x numberOfVariables)
+ s=size(gradient)
+ for i =1:s(2)
+ for j =1:s(2)
+ hessian(i,j)= hessian1(j+((i-1)*s(2)))
+ end
+ end
+
+
+ //In the cases of the problem not being solved, return NULL to the output matrices
+ if( status~=0 & status~=1 & status~=2 & status~=3 & status~=4 & status~=7 ) then
+ xopt=[]
+ fopt=[]
+ output = struct("Iterations", [],"Cpu_Time",[]);
+ output.Iterations = iter;
+ output.Cpu_Time = cpu;
+ gradient=[]
+ hessian=[]
+ end
+
+
+ //To print output message
+ 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/lib b/macros/lib
index 60efd81..b034ee7 100644
Binary files a/macros/lib and b/macros/lib differ
diff --git a/macros/linprog.bin b/macros/linprog.bin
new file mode 100644
index 0000000..b9ba12a
Binary files /dev/null and b/macros/linprog.bin differ
diff --git a/macros/linprog.sci b/macros/linprog.sci
new file mode 100644
index 0000000..4c949ba
--- /dev/null
+++ b/macros/linprog.sci
@@ -0,0 +1,199 @@
+// 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: Guru Pradeep Reddy, Bhanu Priya Sayal
+// Organization: FOSSEE, IIT Bombay
+// Email: toolbox@scilab.in
+
+
+function [xopt,fopt,exitflag,output,lambda] = linprog (varargin)
+ // Solves a linear programming problem.
+ //
+ // Calling Sequence
+ // xopt = linprog(c,A,b)
+ // xopt = linprog(c,A,b,Aeq,beq)
+ // xopt = linprog(c,A,b,Aeq,beq,lb,ub)
+ // xopt = linprog(c,A,b,Aeq,beq,lb,ub,param)
+ // [xopt, fopt, exitflag, output, lambda] = linprog(file)
+ // [xopt,fopt,exitflag,output,lambda] = linprog( ... )
+ //
+ // Parameters
+ // c : a vector of double, contains coefficients of the variables in the objective
+ // 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 : Lower bounds, specified as a vector or array of double. lb represents the lower bounds elementwise in lb ≤ x ≤ ub.
+ // ub : Upper bounds, specified as a vector or array of double. ub represents the upper bounds elementwise in lb ≤ x ≤ ub.
+ // options : a list containing the parameters to be set.
+ // file : a string describing the path to the mps file.
+ // xopt : a vector of double, the computed solution of the optimization problem.
+ // fopt : a double, the value of the function at x.
+ // status : status flag returned from symphony. See below for details.
+ // output : The output data structure contains detailed information about the optimization process. See below for details.
+ // lambda : The structure consist of the Lagrange multipliers at the solution of problem. See below for details.
+ //
+ // Description
+ // OSI-CLP is used for solving the linear programming problems, OSI-CLP is a library written in C++.
+ // Search the minimum of a constrained linear programming problem specified by :
+ //
+ //
+ // \begin{eqnarray}
+ // &\mbox{min}_{x}
+ // & c^T⋅x \\
+ // & \text{subject to} & A⋅x \leq b \\
+ // & & Aeq⋅x = beq \\
+ // & & lb \leq x \leq ub \\
+ // \end{eqnarray}
+ //
+ // The routine calls Clp for solving the linear programming problem, Clp is a library written in C++.
+ //
+ // The exitflag allows to know the status of the optimization which is given back by Ipopt.
+ //
+ // exitflag=0 : Optimal Solution Found
+ // exitflag=1 : Primal Infeasible
+ // exitflag=2 : Dual Infeasible
+ // exitflag=3 : Maximum Number of Iterations Exceeded. Output may not be optimal.
+ // exitflag=4 : Solution Abandoned
+ // exitflag=5 : Primal objective limit reached.
+ // exitflag=6 : Dual objective limit reached.
+ //
+ //
+ // 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.
+ //
+ // output.iterations: The number of iterations performed during the search
+ // output.constrviolation: The max-norm of the constraint violation.
+ //
+ //
+ // 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.
+ //
+ // lambda.lower: The Lagrange multipliers for the lower bound constraints.
+ // lambda.upper: The Lagrange multipliers for the upper bound constraints.
+ // lambda.eqlin: The Lagrange multipliers for the linear equality constraints.
+ // lambda.ineqlin: The Lagrange multipliers for the linear inequality constraints.
+ //
+ //
+ // Examples
+ // //Optimal problems
+ // //Linear program, linear inequality constraints
+ // c=[-1,-1/3]'
+ // A=[1,1;1,1/4;1,-1;-1/4,-1;-1,-1;-1,1]
+ // b=[2,1,2,1,-1,2]
+ // [xopt,fopt,exitflag,output,lambda]=linprog(c, A, b)
+ // // Press ENTER to continue
+ //
+ // Examples
+ // //Linear program with Linear Inequalities and Equalities`
+ // c=[-1,-1/3]'
+ // A=[1,1;1,1/4;1,-1;-1/4,-1;-1,-1;-1,1]
+ // b=[2,1,2,1,-1,2]
+ // Aeq=[1,1/4]
+ // beq=[1/2]
+ // [xopt,fopt,exitflag,output,lambda]=linprog(c, A, b, Aeq, beq)
+ // // Press ENTER to continue
+ //
+ // Examples
+ // //Linear program with all constraint types
+ // c=[-1,-1/3]'
+ // A=[1,1;1,1/4;1,-1;-1/4,-1;-1,-1;-1,1]
+ // b=[2,1,2,1,-1,2]
+ // Aeq=[1,1/4]
+ // beq=[1/2]
+ // lb=[-1,-0.5]
+ // ub=[1.5,1.25]
+ // [xopt,fopt,exitflag,output,lambda]=linprog(c, A, b, Aeq, beq, lb, ub)
+ // // Press ENTER to continue
+ //
+ // Examples
+ // //Primal Infeasible Problem
+ // c=[-1,-1,-1]'
+ // A=[1,2,-1]
+ // b=[-4]
+ // Aeq=[1,5,3;1,1,0]
+ // beq=[10,100]
+ // lb=[0,0,0]
+ // ub=[%inf,%inf,%inf]
+ // [xopt,fopt,exitflag,output,lambda]= linprog(c,A,b,Aeq,beq,lb,ub)
+ // // Press ENTER to continue
+ //
+ // Examples
+ // //Dual Infeasible Problem
+ // c=[3,5,-7]'
+ // A=[-1,-1,4;1,1,4]
+ // b=[-8,5]
+ // Aeq=[]
+ // beq=[]
+ // lb=[-%inf,-%inf,-%inf]
+ // ub=[%inf,%inf,%inf]
+ // [xopt,fopt,exitflag,output,lambda]= linprog(c,A,b,Aeq,beq,lb,ub)
+ // // Press ENTER to continue
+ //
+ // Examples
+ // filepath = get_absolute_file_path('linprog.dem.sce');
+ // filepath = filepath + "exmip1.mps"
+ // [xopt,fopt,exitflag,output,lambda] =linprog(filepath)
+ // Authors
+ // Bhanu Priya Sayal, Guru Pradeep Reddy
+
+ if(type(varargin(1))==1) then
+ [lhs , rhs] = argn();
+ //To check the number of argument given by user
+ if ( rhs < 3 | rhs == 4 | rhs == 6 | rhs >8 ) then
+ errmsg = msprintf(gettext("%s: Unexpected number of input arguments : %d provided while should be in the set of [3 5 7 8]"), "linprog", rhs);
+ error(errmsg)
+ end
+
+ c = varargin(1);
+ A = varargin(2);
+ b = varargin(3);
+
+ if ( rhs<4 ) then
+ Aeq = []
+ beq = []
+ else
+ Aeq = varargin(4);
+ beq = varargin(5);
+ end
+
+ if ( rhs<6 ) then
+ lb = [];
+ ub = [];
+ else
+ lb = varargin(6);
+ ub = varargin(7);
+ end
+
+ if ( rhs<8 | size(varargin(8)) ==0 ) then
+ param = list();
+ else
+ param =varargin(8);
+ end
+ [xopt,fopt,exitflag,output,lambda]=matrix_linprog(c,A,b,Aeq,beq,lb,ub,param);
+ elseif(type(varargin(1))==10) then
+
+ [lhs , rhs] = argn();
+
+ //To check the number of argument given by user
+ if ( rhs < 1 | rhs > 2) then
+ errmsg = msprintf(gettext("%s: Unexpected number of input arguments : %d provided while should be in the set of [1 2]"),"linprog",rhs);
+ error(errmsg)
+ end
+ mpsFile = varargin(1);
+ if ( rhs<2 | size(varargin(2)) ==0 ) then
+ param = list();
+ else
+ param =varargin(2);
+ end
+ [xopt,fopt,exitflag,output,lambda]=mps_linprog(mpsFile,param);
+ end
+
+endfunction
diff --git a/macros/lsqlin.bin b/macros/lsqlin.bin
index eb02534..7018f4a 100644
Binary files a/macros/lsqlin.bin and b/macros/lsqlin.bin differ
diff --git a/macros/lsqlin.sci b/macros/lsqlin.sci
index 960b4db..97aaae6 100644
--- a/macros/lsqlin.sci
+++ b/macros/lsqlin.sci
@@ -151,6 +151,11 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin (varargin)
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 = []
diff --git a/macros/lsqnonneg.bin b/macros/lsqnonneg.bin
index 7ea7f4a..39c553b 100644
Binary files a/macros/lsqnonneg.bin and b/macros/lsqnonneg.bin differ
diff --git a/macros/lsqnonneg.sci b/macros/lsqnonneg.sci
index 3b152bf..3e5ea81 100644
--- a/macros/lsqnonneg.sci
+++ b/macros/lsqnonneg.sci
@@ -97,6 +97,12 @@ function [xopt,resnorm,residual,exitflag,output,lambda] = lsqnonneg (varargin)
C = varargin(1);
d = varargin(2);
nbVar = size(C,2);
+
+ if(nbVar == 0) then
+ errmsg = msprintf(gettext("%s: Cannot determine the number of variables because input objective coefficients is empty"), "lsqnonneg");
+ error(errmsg);
+ end
+
if ( rhs<3 | size(varargin(3)) ==0 ) then
param = list();
else
diff --git a/macros/matrix_linprog.bin b/macros/matrix_linprog.bin
new file mode 100644
index 0000000..21e525c
Binary files /dev/null and b/macros/matrix_linprog.bin differ
diff --git a/macros/matrix_linprog.sci b/macros/matrix_linprog.sci
new file mode 100755
index 0000000..1f6a6bf
--- /dev/null
+++ b/macros/matrix_linprog.sci
@@ -0,0 +1,245 @@
+// Copyright (C) 2015 - IIT Bombay - FOSSEE
+//
+// Author: Guru Pradeep Reddy, Bhanu Priya Sayal
+// Organization: FOSSEE, IIT Bombay
+// Email: gurupradeept@gmail.com, bhanupriyasayal@gmail.com
+// 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
+
+
+function [xopt,fopt,exitflag,output,lambda] = matrix_linprog (varargin)
+//To check the number of input and output argument
+ [lhs , rhs] = argn();
+
+ c = [];
+ A = [];
+ b = [];
+ Aeq = [];
+ beq = [];
+ lb = [];
+ ub = [];
+ options = list();
+
+//To check the number of argument given by user
+ if ( rhs < 3 | rhs == 4 | rhs == 6 | rhs >8 ) then
+ errmsg = msprintf(gettext("%s: Unexpected number of input arguments : %d provided while should be in the set of [3 5 7 8]"), "linprog", rhs);
+ error(errmsg)
+ end
+
+ c = varargin(1);
+
+ if (size(c,2)~=1) then
+ errmsg = msprintf(gettext("%s: Objective Coefficients should be a column matrix"), "linprog");
+ error(errmsg);
+ end
+
+ if(size(c,2) == 0) then
+ errmsg = msprintf(gettext("%s: Cannot determine the number of variables because input objective coefficients is empty"), "linprog");
+ error(errmsg);
+ end
+
+ nbVar = size(c,1);
+ A = varargin(2);
+ b = varargin(3);
+
+ if ( rhs<4 ) then
+ Aeq = []
+ beq = []
+ else
+ Aeq = varargin(4);
+ beq = varargin(5);
+ end
+
+ if ( rhs<6 ) then
+ lb = [];
+ ub = [];
+ else
+ lb = varargin(6);
+ ub = varargin(7);
+ end
+
+ if ( rhs<8 | size(varargin(8)) ==0 ) then
+ param = list();
+ else
+ param =varargin(8);
+ end
+
+ nbConInEq = size(A,1);
+ nbConEq = size(Aeq,1);
+
+ 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(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: options should be a list "), "linprog");
+ error(errmsg);
+ end
+
+
+ if (modulo(size(param),2)) then
+ errmsg = msprintf(gettext("%s: Size of parameters should be even"), "linprog");
+ error(errmsg);
+ end
+
+ options = list("MaxIter", [3000]);
+
+ for i = 1:(size(param))/2
+
+ select param(2*i-1)
+ case "MaxIter" then
+ options(2*i) = param(2*i);
+ else
+ errmsg = msprintf(gettext("%s: Unrecognized parameter name ''%s''."), "qpipoptmat", param(2*i-1));
+ error(errmsg)
+ end
+ 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 elements of c"), "linprog");
+ 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 elements of c"), "linprog");
+ 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"), "linprog");
+ 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"), "linprog");
+ 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,2) ~= 0) then
+ errmsg = msprintf(gettext("%s: The number of rows in A must be the same as the number of elements of b"), "linprog");
+ 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,2) ~= 0) then
+ errmsg = msprintf(gettext("%s: The number of rows in Aeq must be the same as the number of elements of beq"), "linprog");
+ error(errmsg);
+ end
+
+ //Check if the user gives a matrix instead of a vector
+
+ if (size(lb,1)~=1)& (size(lb,2)~=1) then
+ errmsg = msprintf(gettext("%s: Lower Bound should be a vector"), "linprog");
+ error(errmsg);
+ end
+
+ if (size(ub,1)~=1)& (size(ub,2)~=1) then
+ errmsg = msprintf(gettext("%s: Upper Bound should be a vector"), "linprog");
+ 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"), "linprog");
+ 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"), "linprog");
+ error(errmsg);
+ end
+ end
+
+ for i = 1:nbConInEq
+ if (b(i) == -%inf)
+ errmsg = msprintf(gettext("%s: Value of b can not be negative infinity"), "linprog");
+ error(errmsg);
+ end
+ end
+
+ for i = 1:nbConEq
+ if (beq(i) == -%inf)
+ errmsg = msprintf(gettext("%s: Value of beq can not be negative infinity"), "linprog");
+ error(errmsg);
+ end
+ end
+
+ lb = lb(:);
+ ub = ub(:)
+ b = b(:);
+ beq = beq(:);
+ nbVar = size(c,1);
+ c = c';
+ conMatrix = [Aeq;A];
+ nbCon = size(conMatrix,1);
+ conlb = [beq; repmat(-%inf,nbConInEq,1)];
+ conub = [beq;b];
+
+ [xopt,fopt,status,iter,Zl,dual] = linearprog(nbVar,nbCon,c,conMatrix,conlb,conub,lb',ub',options);
+
+ xopt = xopt';
+ 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("reduced_cost" , [], ..
+ "ineqlin" , [], ..
+ "eqlin" , []);
+
+ lambda.reduced_cost = Zl;
+ lambda.eqlin = dual(1:nbConEq);
+ lambda.ineqlin = dual(nbConEq+1:nbCon);
+ select status
+
+ case 0 then
+ printf("\nOptimal Solution.\n");
+ case 1 then
+ printf("\nPrimal Infeasible.\n");
+ case 2 then
+ printf("\nDual Infeasible.\n");
+ case 3 then
+ printf("\nIteration limit reached.\n");
+ case 4 then
+ printf("\nNumerical Difficulties.\n");
+ case 5 then
+ printf("\nPrimal Objective limit reached.\n");
+ case 6 then
+ printf("\nDual Objective limit reached.\n");
+ else
+ printf("\nInvalid status returned. Notify the Toolbox authors\n");
+ break;
+ end
+
+endfunction
diff --git a/macros/mps_linprog.bin b/macros/mps_linprog.bin
new file mode 100644
index 0000000..ee7d822
Binary files /dev/null and b/macros/mps_linprog.bin differ
diff --git a/macros/mps_linprog.sci b/macros/mps_linprog.sci
new file mode 100644
index 0000000..55f3edf
--- /dev/null
+++ b/macros/mps_linprog.sci
@@ -0,0 +1,90 @@
+// Copyright (C) 2015 - IIT Bombay - FOSSEE
+//
+// Author: Bhanu Priya Sayal, Guru Pradeep Reddy
+// Organization: FOSSEE, IIT Bombay
+// Email:bhanupriyasayal@gmail.com,gurupradeept@gmail.com
+// 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
+
+
+function [xopt,fopt,exitflag,output,lambda] =mps_linprog(varargin)
+
+ //To check the number of input and output argument
+ [lhs , rhs] = argn();
+
+ //To check the number of argument given by user
+ if ( rhs < 1 | rhs > 2) then
+ errmsg = msprintf(gettext("%s: Unexpected number of input arguments : %d provided while should be in the set of [1 2]"),"linprog",rhs);
+ error(errmsg)
+ end
+ mpsFile = varargin(1);
+
+ [sizeFile isFile] = fileinfo(mpsFile);
+ if(isFile ~= 0) then
+ errmsg = msprintf(gettext("%s: File is not present at the given location"),"linprog",rhs);
+ error(errmsg)
+ end
+
+ if ( rhs<2 | size(varargin(2)) ==0 ) then
+ param = list();
+ else
+ param =varargin(2);
+ end
+
+ if (type(param) ~= 15) then
+ errmsg = msprintf(gettext("%s: options should be a list "), "mps_linprog");
+ error(errmsg);
+ end
+
+ if (modulo(size(param),2)) then
+ errmsg = msprintf(gettext("%s: Size of parameters should be even"), "mps_linprog");
+ error(errmsg);
+ end
+ options = list("MaxIter" , [3000],);
+
+ for i = 1:(size(param))/2
+ select param(2*i-1)
+ case "MaxIter" then
+ options(2*i) = param(2*i);
+ end
+ end
+ //Calling the function by passing the required parameters
+
+ [xopt,fopt,status,iter,Zl,dual] = rmps(mpsFile,options);
+
+ xopt = xopt';
+ fopt=fopt;
+ exitflag = status;
+ output = struct("Iterations" , []);
+ output.Iterations = iter;
+ lambda = struct("reduced_cost" , [], ..
+ "dual" ,[]);
+
+ lambda.reduced_cost = Zl;
+ lambda.dual =dual;
+
+ select status
+
+ case 0 then
+ printf("\nOptimal Solution.\n");
+ case 1 then
+ printf("\nPrimal Infeasible.\n");
+ case 2 then
+ printf("\nDual Infeasible.\n");
+ case 3 then
+ printf("\nIteration limit reached.\n");
+ case 4 then
+ printf("\nNumerical Difficulties.\n");
+ case 5 then
+ printf("\nPrimal Objective limit reached.\n");
+ case 6 then
+ printf("\nDual Objective limit reached.\n");
+ else
+ printf("\nInvalid status returned. Notify the Toolbox authors\n");
+ break;
+ end
+
+endfunction
diff --git a/macros/names b/macros/names
index e04f783..1055c2a 100644
--- a/macros/names
+++ b/macros/names
@@ -1,5 +1,19 @@
+fgoalattain
+fgoalattainFunctions
+fminbnd
+fmincon
+fminimax
+fminimaxCheckdims
+fminimaxChecklhs
+fminimaxCheckrhs
+fminimaxChecktype
+fminimaxCheckvector
+fminunc
+linprog
lsqlin
lsqnonneg
+matrix_linprog
+mps_linprog
qpipopt
qpipoptmat
setOptions
diff --git a/macros/qpipoptmat.bin b/macros/qpipoptmat.bin
index 64e0b44..58424ec 100644
Binary files a/macros/qpipoptmat.bin and b/macros/qpipoptmat.bin differ
diff --git a/macros/qpipoptmat.sci b/macros/qpipoptmat.sci
index b74c718..ca32f8e 100644
--- a/macros/qpipoptmat.sci
+++ b/macros/qpipoptmat.sci
@@ -145,6 +145,10 @@ function [xopt,fopt,exitflag,output,lambda] = qpipoptmat (varargin)
f = varargin(2);
nbVar = size(H,1);
+ if(nbVar == 0) then
+ errmsg = msprintf(gettext("%s: Cannot determine the number of variables because input objective coefficients is empty"), "qpipoptmat");
+ error(errmsg);
+ end
if ( rhs<3 ) then
A = []
@@ -424,5 +428,4 @@ function [xopt,fopt,exitflag,output,lambda] = qpipoptmat (varargin)
break;
end
-
endfunction
diff --git a/macros/symphonymat.bin b/macros/symphonymat.bin
index ec35ee2..a5e68ab 100644
Binary files a/macros/symphonymat.bin and b/macros/symphonymat.bin differ
diff --git a/macros/symphonymat.sci b/macros/symphonymat.sci
index 2d51b84..3848850 100644
--- a/macros/symphonymat.sci
+++ b/macros/symphonymat.sci
@@ -193,12 +193,16 @@ function [xopt,fopt,status,iter] = symphonymat (varargin)
A = varargin(3)
b = varargin(4)
+ if(size(c,2) == 0) then
+ errmsg = msprintf(gettext("%s: Cannot determine the number of variables because input objective coefficients is empty"),"Symphonymat");
+ error(errmsg);
+ end
+
if (size(c,2)~=1) then
errmsg = msprintf(gettext("%s: Objective Coefficients should be a column matrix"), "Symphonymat");
error(errmsg);
end
-
nbVar = size(c,1);
if ( rhs<5 ) then
diff --git a/sci_gateway/cpp/LinCLP.hpp b/sci_gateway/cpp/LinCLP.hpp
new file mode 100755
index 0000000..90964f4
--- /dev/null
+++ b/sci_gateway/cpp/LinCLP.hpp
@@ -0,0 +1,82 @@
+/*
+ * Linear Solver Toolbox for Scilab using CLP library
+ * Authors :
+ Guru Pradeep Reddy
+ Bhanu Priya Sayal
+
+* Optimizing (minimizing) the linear objective function having any number of
+ variables and linear constraints(equality/inequality).
+ *
+*/
+
+#ifndef __LinCLP_HPP__
+#define __LinCLP_HPP__
+
+#include"OsiSolverInterface.hpp"
+#include "OsiClpSolverInterface.hpp"
+#include "CoinPackedMatrix.hpp"
+#include "CoinPackedVector.hpp"
+
+class LinCLP
+{
+ private:
+
+ int numVars_; //Number of variables
+
+ int numCons_; //Number of inequality constraints
+
+ double* objMatrix_[]; //Objective function vector
+
+ double* conMatrix_[]; //Inequality constraint matrix
+
+ double* conlb_[]; //Inequality constraint vector
+
+ double* conub_[]; //Equality constraint vector
+
+ double* lb_[]; //Lower bounds for all variables
+
+ double* ub_[]; //Upper bounds for all variables
+
+ double options_[]; //options for setting maximum iterations and writing mps and lp files
+
+ double* xValue_ = NULL; //Optimal value of variables
+
+ double objValue_ =0 ; //Optimal values of objective
+
+ double status_ = 0; //Return Status
+
+ double iterations_ = 0; //Number of iteration
+
+ double* reducedCost_ = NULL; //Reduced cost
+
+ double* dual_ = NULL; // Dual of the solution
+
+
+ public:
+/*
+ * Constructor
+*/
+ LinCLP(int numVars_ , int numCons_ ,double objMatrix_[] , double conMatrix_[] , double conlb_[] , double conub_[] ,double lb_[] , double ub_[], double options_[]);
+
+
+ virtual ~LinCLP(); //Destructor to free memory
+
+ const double* getX(); //Returns a pointer to matrix of size
+ //1*numVars with final values for the objective variables
+
+ double getObjVal(); //Returns the output of the final value of the objective
+
+ int returnStatus(); //Returns the status of the problem
+
+ double iterCount(); //Returns the iteration count
+
+ const double* getReducedCost(); //Returns a pointer to matrix of size
+ //1*numVars with values for lower dual vector
+
+ double* getDual(); //Returns a pointer to matrix of size
+ //1*numCons with values for dual vector
+
+};
+
+#endif __LinCLP_HPP__
+
diff --git a/sci_gateway/cpp/builder_gateway_cpp.sce b/sci_gateway/cpp/builder_gateway_cpp.sce
index 6e4adf7..3503996 100644
--- a/sci_gateway/cpp/builder_gateway_cpp.sce
+++ b/sci_gateway/cpp/builder_gateway_cpp.sce
@@ -1,13 +1,13 @@
// Copyright (C) 2015 - IIT Bombay - FOSSEE
//
+// Author: Keyur Joshi, Sai Kiran, Iswarya and Harpreet Singh
+// Organization: FOSSEE, IIT Bombay
+// Email: harpreet.mertia@gmail.com
// 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
mode(-1)
lines(0)
@@ -21,9 +21,9 @@ path_builder = get_absolute_file_path('builder_gateway_cpp.sce');
tools_path = path_builder + "../../thirdparty/linux/";
-C_Flags=["-w -fpermissive -I"+tools_path+"include/coin -Wl,-rpath="+tools_path+"lib/"+Version+filesep()+" "]
+C_Flags=["-D__USE_DEPRECATED_STACK_FUNCTIONS__ -w -fpermissive -I"+tools_path+"include/coin -Wl,-rpath="+tools_path+"lib/"+Version+filesep()+" "]
-Linker_Flag = ["-L"+tools_path+"lib/"+Version+filesep()+"libSym"+" "+"-L"+tools_path+"lib/"+Version+filesep()+"libipopt" ]
+Linker_Flag = ["-L"+tools_path+"lib/"+Version+filesep()+"libSym"+" "+"-L"+tools_path+"lib/"+Version+filesep()+"libipopt"+" "+"-L"+tools_path+"lib/"+Version+filesep()+"libClp"+" "+"-L"+tools_path+"lib/"+Version+filesep()+"libOsiClp"+" "+"-L"+tools_path+"lib/"+Version+filesep()+"libCoinUtils" ]
//Name of All the Functions
@@ -108,8 +108,17 @@ Function_Names = [
"sym_getIterCount","sci_sym_get_iteration_count";
"sym_getConstrActivity","sci_sym_getRowActivity";
+ //Linprog function
+ "linearprog","sci_linearprog"
+ "rmps","sci_rmps"
+
//QP function
"solveqp","sci_solveqp"
+
+ //fminunc function and fminbnd function
+ "solveminuncp","sci_solveminuncp"
+ "solveminbndp","sci_solveminbndp"
+ "solveminconp","sci_solveminconp"
];
//Name of all the files to be compiled
@@ -140,8 +149,21 @@ Files = [
"sci_sym_remove.cpp",
"sci_QuadNLP.cpp",
"QuadNLP.hpp",
- "sci_ipopt.cpp"
-
+ "sci_ipopt.cpp",
+ "minuncNLP.hpp",
+ "sci_minuncNLP.cpp",
+ "sci_ipoptfminunc.cpp",
+ "minbndNLP.hpp",
+ "sci_minbndNLP.cpp",
+ "sci_ipoptfminbnd.cpp",
+ "minconNLP.hpp",
+ "sci_minconNLP.cpp",
+ "sci_ipoptfmincon.cpp",
+ "sci_ipopt.cpp",
+ "sci_LinProg.cpp",
+ "sci_LinCLP.cpp",
+ "LinCLP.hpp",
+ "read_mps.cpp"
]
tbx_build_gateway(toolbox_title,Function_Names,Files,get_absolute_file_path("builder_gateway_cpp.sce"), [], Linker_Flag, C_Flags, [], "g++");
diff --git a/sci_gateway/cpp/libFAMOS.c b/sci_gateway/cpp/libFAMOS.c
index 61990ad..d7911de 100644
--- a/sci_gateway/cpp/libFAMOS.c
+++ b/sci_gateway/cpp/libFAMOS.c
@@ -64,7 +64,12 @@ extern Gatefunc sci_sym_getVarSoln;
extern Gatefunc sci_sym_getObjVal;
extern Gatefunc sci_sym_get_iteration_count;
extern Gatefunc sci_sym_getRowActivity;
+extern Gatefunc sci_linearprog;
+extern Gatefunc sci_rmps;
extern Gatefunc sci_solveqp;
+extern Gatefunc sci_solveminuncp;
+extern Gatefunc sci_solveminbndp;
+extern Gatefunc sci_solveminconp;
static GenericTable Tab[]={
{(Myinterfun)sci_gateway,sci_sym_open,"sym_open"},
{(Myinterfun)sci_gateway,sci_sym_close,"sym_close"},
@@ -124,7 +129,12 @@ static GenericTable Tab[]={
{(Myinterfun)sci_gateway,sci_sym_getObjVal,"sym_getObjVal"},
{(Myinterfun)sci_gateway,sci_sym_get_iteration_count,"sym_getIterCount"},
{(Myinterfun)sci_gateway,sci_sym_getRowActivity,"sym_getConstrActivity"},
+ {(Myinterfun)sci_gateway,sci_linearprog,"linearprog"},
+ {(Myinterfun)sci_gateway,sci_rmps,"rmps"},
{(Myinterfun)sci_gateway,sci_solveqp,"solveqp"},
+ {(Myinterfun)sci_gateway,sci_solveminuncp,"solveminuncp"},
+ {(Myinterfun)sci_gateway,sci_solveminbndp,"solveminbndp"},
+ {(Myinterfun)sci_gateway,sci_solveminconp,"solveminconp"},
};
int C2F(libFAMOS)()
diff --git a/sci_gateway/cpp/libFAMOS.so b/sci_gateway/cpp/libFAMOS.so
index 4ab40e5..c8462c6 100755
Binary files a/sci_gateway/cpp/libFAMOS.so and b/sci_gateway/cpp/libFAMOS.so differ
diff --git a/sci_gateway/cpp/loader.sce b/sci_gateway/cpp/loader.sce
index fe1d630..37305c7 100644
--- a/sci_gateway/cpp/loader.sce
+++ b/sci_gateway/cpp/loader.sce
@@ -68,7 +68,12 @@ list_functions = [ 'sym_open';
'sym_getObjVal';
'sym_getIterCount';
'sym_getConstrActivity';
+ 'linearprog';
+ 'rmps';
'solveqp';
+ 'solveminuncp';
+ 'solveminbndp';
+ 'solveminconp';
];
addinter(libFAMOS_path + filesep() + 'libFAMOS' + getdynlibext(), 'libFAMOS', list_functions);
// remove temp. variables on stack
diff --git a/sci_gateway/cpp/minbndNLP.hpp b/sci_gateway/cpp/minbndNLP.hpp
new file mode 100644
index 0000000..17d5a7e
--- /dev/null
+++ b/sci_gateway/cpp/minbndNLP.hpp
@@ -0,0 +1,116 @@
+// Copyright (C) 2015 - IIT Bombay - FOSSEE
+//
+// Author: R.Vidyadhar & Vignesh Kannan
+// Organization: FOSSEE, IIT Bombay
+// Email: rvidhyadar@gmail.com & vignesh2496@gmail.com
+// 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
+
+
+#ifndef __minbndNLP_HPP__
+#define __minbndNLP_HPP__
+#include "IpTNLP.hpp"
+
+using namespace Ipopt;
+
+class minbndNLP : public TNLP
+{
+ private:
+
+ Index numVars_; //Number of input variables
+
+ Index numConstr_; //Number of constraints
+
+ Number *finalX_= NULL; //finalX_ is a pointer to a matrix of size of 1*1
+ //with final value for the primal variable.
+
+ Number *finalZl_= NULL; //finalZl_ is a pointer to a matrix of size of 1*numVar_
+ // with final values for the lower bound multipliers
+
+ Number *finalZu_= NULL; //finalZu_ is a pointer to a matrix of size of 1*numVar_
+ // with final values for the upper bound multipliers
+
+ Number finalObjVal_; //finalObjVal_ is a scalar with the final value of the objective.
+
+ int iter_; //Number of iteration.
+
+ int status_; //Solver return status
+
+
+ const Number *varUB_= NULL; //varUB_ is a pointer to a matrix of size of 1*1
+ // with upper bounds of all variable.
+
+ const Number *varLB_= NULL; //varLB_ is a pointer to a matrix of size of 1*1
+ // with lower bounds of all variable.
+
+ minbndNLP(const minbndNLP&);
+ minbndNLP& operator=(const minbndNLP&);
+
+ public:
+
+ /** user defined constructor */
+ minbndNLP(Index nV, Index nC,Number *LB,Number *UB):numVars_(nV),numConstr_(nC),finalX_(0),finalZl_(0), finalZu_(0),varLB_(LB),varUB_(UB),finalObjVal_(1e20){ }
+
+ /** default destructor */
+ virtual ~minbndNLP();
+
+ /** Method to return some info about the nlp */
+ virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
+ Index& nnz_h_lag, IndexStyleEnum& index_style);
+
+ /** Method to return the bounds for my problem */
+ virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
+ Index m, Number* g_l, Number* g_u);
+
+ /** Method to return the starting point for the algorithm */
+ virtual bool get_starting_point(Index n, bool init_x, Number* x,
+ bool init_z, Number* z_L, Number* z_U,
+ Index m, bool init_lambda,
+ Number* lambda);
+
+ /** Method to return the objective value */
+ virtual bool eval_f(Index n, const Number* x, bool new_x, Number& obj_value);
+
+ /** Method to return the gradient of the objective */
+ virtual bool eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f);
+
+ /** Method to return the constraint residuals */
+ virtual bool eval_g(Index n, const Number* x, bool new_x, Index m, Number* g);
+
+ /** Method to return:
+ * 1) The structure of the jacobian (if "values" is NULL)
+ * 2) The values of the jacobian (if "values" is not NULL)
+ */
+ virtual bool eval_jac_g(Index n, const Number* x, bool new_x,Index m, Index nele_jac, Index* iRow, Index *jCol,Number* values);
+
+ /** Method to return:
+ * 1) The structure of the hessian of the lagrangian (if "values" is NULL)
+ * 2) The values of the hessian of the lagrangian (if "values" is not NULL)
+ */
+ virtual bool eval_h(Index n, const Number* x, bool new_x,Number obj_factor, Index m, const Number* lambda,bool new_lambda, Index nele_hess, Index* iRow,Index* jCol, Number* values);
+
+ /** This method is called when the algorithm is complete so the TNLP can store/write the solution */
+ virtual void finalize_solution(SolverReturn status,Index n, const Number* x, const Number* z_L, const Number* z_U,Index m, const Number* g, const Number* lambda,Number obj_value,const IpoptData* ip_data,IpoptCalculatedQuantities* ip_cq);
+
+ const double * getX(); //Returns a pointer to a matrix of size of 1*1
+ //with final value for the primal variable.
+
+ const double * getZl(); //Returns a pointer to a matrix of size of 1*numVars_
+ // with final values for the lower bound multipliers
+
+ const double * getZu(); //Returns a pointer to a matrix of size of 1*numVars_
+ //with final values for the upper bound multipliers
+
+ double getObjVal(); //Returns the output of the final value of the objective.
+
+ double iterCount(); //Returns the iteration count
+
+ int returnStatus(); //Returns the status count
+
+};
+
+
+#endif
diff --git a/sci_gateway/cpp/minconNLP.hpp b/sci_gateway/cpp/minconNLP.hpp
new file mode 100644
index 0000000..df496ce
--- /dev/null
+++ b/sci_gateway/cpp/minconNLP.hpp
@@ -0,0 +1,173 @@
+// Copyright (C) 2015 - IIT Bombay - FOSSEE
+//
+// Author: R.Vidyadhar & Vignesh Kannan
+// Organization: FOSSEE, IIT Bombay
+// Email: rvidhyadar@gmail.com & vignesh2496@gmail.com
+// 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
+
+
+#ifndef __minconNLP_HPP__
+#define __minconNLP_HPP__
+#include "IpTNLP.hpp"
+
+using namespace Ipopt;
+
+class minconNLP : public TNLP
+{
+ private:
+
+ Index numVars_; //Number of input variables
+
+ Index numConstr_; //Number of constraints
+
+ Number flag1_; //Gradient of objective ON or OFF
+
+ Number flag2_; //Hessian of objective ON or OFF
+
+ Number flag3_; //Jacobian of constraints ON or OFF
+
+ Number nonlinCon_; //Number of non-linear constraints
+
+ Number nonlinIneqCon_; //Number of non-linear inequality constraints
+
+ const Number *A_= NULL; //Matrix for linear inequality constraints
+
+ const Number *b_= NULL; //Matrix for bounds of linear inequality constraints
+
+ const Number *Aeq_= NULL; //Matrix for linear equality constraints
+
+ const Number *beq_= NULL; //Matrix for bounds of linear equality constraints
+
+ Index Arows_; //Number of rows of linear inequality constraints
+
+ Index Acols_; //Number of columns of linear inequality constraints
+
+ Index brows_; //Number of rows of bounds of linear inequality constraints
+
+ Index bcols_; //Number of columns of bounds of linear inequality constraints
+
+ Index Aeqrows_; //Number of rows of linear equality constraints
+
+ Index Aeqcols_; //Number of columns of linear equality constraints
+
+ Index beqrows_; //Number of rows of bounds of linear equality constraints
+
+ Index beqcols_; //Number of columns of bounds of linear equality constraints
+
+
+ const Number *varGuess_= NULL; //varGuess_ is a pointer to a matrix of size of 1*numVars_
+ //with initial guess of all variables.
+
+ const Number *varUB_= NULL; //varUB_ is a pointer to a matrix of size of 1*numVar_
+ // with upper bounds of all variables.
+
+ const Number *varLB_= NULL; //varLB_ is a pointer to a matrix of size of 1*numVar_
+ // with lower bounds of all variables.
+
+ Number *finalZl_= NULL; //finalZl_ is a pointer to a matrix of size of 1*numVar_
+ // with final values for the lower bound multipliers
+
+ Number *finalZu_= NULL; //finalZu_ is a pointer to a matrix of size of 1*numVar_
+ // with final values for the upper bound multipliers
+
+ Number *finalLambda_= NULL; //finalLambda_ is a pointer to a matrix of size of 1*numConstr_
+ // with final values for the upper bound multipliers
+
+ Number *finalX_= NULL; //finalX_ is a pointer to a matrix of size of 1*numVars_
+ //with final value for the primal variables.
+
+ Number *finalGradient_=NULL; //finalGradient_ is a pointer to a matrix of size of numVars_*numVars_
+ //with final value of gradient for the primal variables.
+
+
+ Number *finalHessian_=NULL; //finalHessian_ is a pointer to a matrix of size of 1*numVar_
+ //with final value of hessian for the primal variables.
+
+
+ Number finalObjVal_; //finalObjVal_ is a scalar with the final value of the objective.
+
+ int iter_; //Number of iteration.
+
+ int status_; //Solver return status
+
+
+ minconNLP(const minconNLP&);
+ minconNLP& operator=(const minconNLP&);
+
+ public:
+
+ /** user defined constructor */
+ minconNLP(Index nV, Index nC, Number *x0 ,Number *A, Number *b, Number* Aeq, Number *beq, Index Arows, Index Acols, Index brows, Index bcols, Index Aeqrows, Index Aeqcols, Index beqrows, Index beqcols, Number* LB, Number* UB, Number nlC, Number nlIC, Number f1, Number f2, Number f3) : numVars_(nV), numConstr_(nC), varGuess_(x0), A_(A), b_(b), Aeq_(Aeq), beq_(beq), Arows_(Arows), Acols_(Acols), brows_(brows), bcols_(bcols), Aeqrows_(Aeqrows), Aeqcols_(Aeqcols), beqrows_(beqrows), beqcols_(beqcols), varLB_(LB), varUB_(UB), nonlinCon_(nlC), nonlinIneqCon_(nlIC), flag1_(f1), flag2_(f2), flag3_(f3), finalX_(0), finalZl_(0), finalZu_(0), finalGradient_(0), finalHessian_(0), finalObjVal_(1e20){ }
+
+ /** default destructor */
+ virtual ~minconNLP();
+
+ /** Method to return some info about the nlp */
+ virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
+ Index& nnz_h_lag, IndexStyleEnum& index_style);
+
+ /** Method to return the bounds for my problem */
+ virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
+ Index m, Number* g_l, Number* g_u);
+
+ /** Method to return the starting point for the algorithm */
+ virtual bool get_starting_point(Index n, bool init_x, Number* x,
+ bool init_z, Number* z_L, Number* z_U,
+ Index m, bool init_lambda,
+ Number* lambda);
+
+ /** Method to return the objective value */
+ virtual bool eval_f(Index n, const Number* x, bool new_x, Number& obj_value);
+
+ /** Method to return the gradient of the objective */
+ virtual bool eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f);
+
+ /** Method to return the constraint residuals */
+ virtual bool eval_g(Index n, const Number* x, bool new_x, Index m, Number* g);
+
+ /** Method to return:
+ * 1) The structure of the jacobian (if "values" is NULL)
+ * 2) The values of the jacobian (if "values" is not NULL)
+ */
+ virtual bool eval_jac_g(Index n, const Number* x, bool new_x,Index m, Index nele_jac, Index* iRow, Index *jCol,Number* values);
+
+ /** Method to return:
+ * 1) The structure of the hessian of the lagrangian (if "values" is NULL)
+ * 2) The values of the hessian of the lagrangian (if "values" is not NULL)
+ */
+ virtual bool eval_h(Index n, const Number* x, bool new_x,Number obj_factor, Index m, const Number* lambda,bool new_lambda, Index nele_hess, Index* iRow,Index* jCol, Number* values);
+
+ /** This method is called when the algorithm is complete so the TNLP can store/write the solution */
+ virtual void finalize_solution(SolverReturn status,Index n, const Number* x, const Number* z_L, const Number* z_U,Index m, const Number* g, const Number* lambda,Number obj_value,const IpoptData* ip_data,IpoptCalculatedQuantities* ip_cq);
+
+ const double * getX(); //Returns a pointer to a matrix of size of 1*numVars_
+ //with final value for the primal variables.
+
+ const double * getGrad(); //Returns a pointer to a matrix of size of 1*numVars_
+ //with final value of gradient for the primal variables.
+
+ const double * getHess(); //Returns a pointer to a matrix of size of numVars_*numVars_
+ //with final value of hessian for the primal variables.
+
+ const double * getZl(); //Returns a pointer to a matrix of size of 1*numVars_
+ // with final values for the lower bound multipliers
+
+ const double * getZu(); //Returns a pointer to a matrix of size of 1*numVars_
+ //with final values for the upper bound multipliers
+
+ const double * getLambda(); //Returns a pointer to a matrix of size of 1*numConstr_
+ //with final values for the constraint multipliers
+
+ double getObjVal(); //Returns the output of the final value of the objective.
+
+ double iterCount(); //Returns the iteration count
+
+ int returnStatus(); //Returns the status count
+
+};
+
+#endif
diff --git a/sci_gateway/cpp/minuncNLP.hpp b/sci_gateway/cpp/minuncNLP.hpp
new file mode 100644
index 0000000..70910e5
--- /dev/null
+++ b/sci_gateway/cpp/minuncNLP.hpp
@@ -0,0 +1,113 @@
+// Copyright (C) 2015 - IIT Bombay - FOSSEE
+//
+// Author: R.Vidyadhar & Vignesh Kannan
+// Organization: FOSSEE, IIT Bombay
+// Email: rvidhyadar@gmail.com & vignesh2496@gmail.com
+// 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
+
+
+#ifndef __minuncNLP_HPP__
+#define __minuncNLP_HPP__
+#include "IpTNLP.hpp"
+
+using namespace Ipopt;
+
+class minuncNLP : public TNLP
+{
+ private:
+
+ Index numVars_; //Number of input variables
+
+ Index numConstr_; //Number of constraints
+
+ Number flag1_; //Used for Gradient On/OFF
+
+ Number flag2_; //Used for Hessian ON/OFF
+
+ const Number *varGuess_= NULL; //varGuess_ is a pointer to a matrix of size of 1*numVars_ with initial guess of all variables.
+
+ Number *finalX_= NULL; //finalX_ is a pointer to a matrix of size of 1*numVars_ with final value for the primal variables.
+
+ Number *finalGradient_=NULL; //finalGradient_ is a pointer to a matrix of size of numVars_*numVars_ with final value of gradient for the primal variables.
+
+ Number *finalHessian_=NULL; //finalHessian_ is a pointer to a matrix of size of 1*numVar_ with final value of hessian for the primal variables.
+
+ Number finalObjVal_; //finalObjVal_ is a scalar with the final value of the objective.
+
+ int iter_; //Number of iteration.
+
+ int status_; //Solver return status
+
+
+ minuncNLP(const minuncNLP&);
+ minuncNLP& operator=(const minuncNLP&);
+
+ public:
+
+ /** user defined constructor */
+ minuncNLP(Index nV, Index nC,Number *x0,Number f1, Number f2):numVars_(nV),numConstr_(nC),varGuess_(x0),flag1_(f1),flag2_(f2),finalX_(0),finalGradient_(0),finalHessian_(0),finalObjVal_(1e20){ }
+
+ /** default destructor */
+ virtual ~minuncNLP();
+
+ /** Method to return some info about the nlp */
+ virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
+ Index& nnz_h_lag, IndexStyleEnum& index_style);
+
+ /** Method to return the bounds for my problem */
+ virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
+ Index m, Number* g_l, Number* g_u);
+
+ /** Method to return the starting point for the algorithm */
+ virtual bool get_starting_point(Index n, bool init_x, Number* x,
+ bool init_z, Number* z_L, Number* z_U,
+ Index m, bool init_lambda,
+ Number* lambda);
+
+ /** Method to return the objective value */
+ virtual bool eval_f(Index n, const Number* x, bool new_x, Number& obj_value);
+
+ /** Method to return the gradient of the objective */
+ virtual bool eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f);
+
+ /** Method to return the constraint residuals */
+ virtual bool eval_g(Index n, const Number* x, bool new_x, Index m, Number* g);
+
+ /** Method to return:
+ * 1) The structure of the jacobian (if "values" is NULL)
+ * 2) The values of the jacobian (if "values" is not NULL)
+ */
+ virtual bool eval_jac_g(Index n, const Number* x, bool new_x,Index m, Index nele_jac, Index* iRow, Index *jCol,Number* values);
+
+ /** Method to return:
+ * 1) The structure of the hessian of the lagrangian (if "values" is NULL)
+ * 2) The values of the hessian of the lagrangian (if "values" is not NULL)
+ */
+ virtual bool eval_h(Index n, const Number* x, bool new_x,Number obj_factor, Index m, const Number* lambda,bool new_lambda, Index nele_hess, Index* iRow,Index* jCol, Number* values);
+
+ /** This method is called when the algorithm is complete so the TNLP can store/write the solution */
+ virtual void finalize_solution(SolverReturn status,Index n, const Number* x, const Number* z_L, const Number* z_U,Index m, const Number* g, const Number* lambda,Number obj_value,const IpoptData* ip_data,IpoptCalculatedQuantities* ip_cq);
+
+ const double * getX(); //Returns a pointer to a matrix of size of 1*numVars_
+ //with final value for the primal variables.
+
+ const double * getGrad(); //Returns a pointer to a matrix of size of 1*numVars_
+ //with final value of gradient for the primal variables.
+
+ const double * getHess(); //Returns a pointer to a matrix of size of numVars_*numVars_
+ //with final value of hessian for the primal variables.
+
+ double getObjVal(); //Returns the output of the final value of the objective.
+
+ double iterCount(); //Returns the iteration count
+
+ int returnStatus(); //Returns the status count
+
+};
+
+
+#endif
diff --git a/sci_gateway/cpp/read_mps.cpp b/sci_gateway/cpp/read_mps.cpp
new file mode 100644
index 0000000..31f71b8
--- /dev/null
+++ b/sci_gateway/cpp/read_mps.cpp
@@ -0,0 +1,113 @@
+/*
+ * Linear Solver Toolbox for Scilab using CLP library
+ * Authors :
+ Guru Pradeep Reddy
+ Bhanu Priya Sayal
+*/
+
+#include "sci_iofunc.hpp"
+#include "OsiClpSolverInterface.hpp"
+
+extern "C"{
+#include
+#include
+#include
+#include
+#include
+
+//Solver function
+int sci_rmps(char *fname)
+{
+ //creating a problem pointer using base class of OsiSolverInterface and
+ //instantiate the object using derived class of ClpSolverInterface
+ OsiSolverInterface* si = new OsiClpSolverInterface();
+
+ // Error management variable
+ SciErr sciErr;
+
+ //data declarations
+ int *piAddressVarOne = NULL; //pointer used to access argument of the function
+ char* ptr; //pointer to point to address of file name
+ double* options_; //options to set maximum iterations
+ CheckInputArgument(pvApiCtx, 2,2 ); //Check we have exactly two arguments as input or not
+ CheckOutputArgument(pvApiCtx, 6, 6); //Check we have exactly six arguments on output side or not
+ //Getting the input arguments from Scilab
+ //Getting the MPS file path
+ //Reading mps file
+ getStringFromScilab(1,&ptr);
+
+ std::cout<readMps(ptr);
+
+ //setting options for maximum iterations
+ si->setIntParam(OsiMaxNumIteration,options_[0]);
+
+ //Solve the problem
+ si->initialSolve();
+
+ //Quering about the problem
+ //get number of variables
+ double numVars_;
+ numVars_ = si->getNumCols();
+
+ //get number of constraint equations
+ double numCons_;
+ numCons_ = si->getNumRows();
+
+ //Output the solution to Scilab
+ //get solution for x
+ const double* xValue = si->getColSolution();
+
+ //get objective value
+ double objValue = si->getObjValue();
+
+ //get Status value
+ double status;
+ if(si->isProvenOptimal())
+ status=0;
+ else if(si->isProvenPrimalInfeasible())
+ status=1;
+ else if(si->isProvenDualInfeasible())
+ status=2;
+ else if(si->isIterationLimitReached())
+ status=3;
+ else if(si->isAbandoned())
+ status=4;
+ else if(si->isPrimalObjectiveLimitReached())
+ status=5;
+ else if(si->isDualObjectiveLimitReached())
+ status=6;
+
+ //get number of iterations
+ double iterations = si->getIterationCount();
+
+ //get reduced cost
+ const double* reducedCost = si->getReducedCost();
+
+ //get dual vector
+ const double* dual = si->getRowPrice();
+
+ returnDoubleMatrixToScilab(1 , 1 , numVars_ , xValue);
+ returnDoubleMatrixToScilab(2 , 1 , 1 , &objValue);
+ returnDoubleMatrixToScilab(3 , 1 , 1 , &status);
+ returnDoubleMatrixToScilab(4 , 1 , 1 , &iterations);
+ returnDoubleMatrixToScilab(5 , 1 , numVars_ , reducedCost);
+ returnDoubleMatrixToScilab(6 , 1 , numCons_ , dual);
+
+ free(xValue);
+ free(dual);
+ free(reducedCost);
+}
+}
+
+
+
+
diff --git a/sci_gateway/cpp/sci_LinCLP.cpp b/sci_gateway/cpp/sci_LinCLP.cpp
new file mode 100644
index 0000000..7996bfc
--- /dev/null
+++ b/sci_gateway/cpp/sci_LinCLP.cpp
@@ -0,0 +1,119 @@
+/*
+ * Linear Solver Toolbox for Scilab using CLP library
+ * Authors :
+ Guru Pradeep Reddy
+ Bhanu Priya Sayal
+*/
+
+#include "LinCLP.hpp"
+extern "C"{
+#include "api_scilab.h"
+#include "Scierror.h"
+#include "localization.h"
+#include "sciprint.h"
+#include "sci_iofunc.hpp"
+
+//creating a problem pointer using Base class of OsiSolverInterface and
+//Instantiate the object using specific derived class of ClpSolver
+OsiSolverInterface* si = new OsiClpSolverInterface();
+
+LinCLP::~LinCLP()
+ {
+ free(objMatrix_);
+ free(conMatrix_);
+ free(conlb_);
+ free(conub_);
+ free(lb_);
+ free(ub_);
+ free(xValue_);
+ free(reducedCost_);
+ free(dual_);}
+
+//Clp Solver function definition
+LinCLP::LinCLP(int numVars_ , int numCons_ ,double objMatrix_[] , double conMatrix_[] , double conlb_[] , double conub_[] ,double lb_[] , double ub_[], double options_[])
+{
+
+ //Defining the constraint matrix
+ CoinPackedMatrix *matrix = new CoinPackedMatrix(false , 0 , 0);
+ matrix->setDimensions(0 , numVars_);
+ for(int i=0 ; iappendRow(row);
+ }
+
+ //setting options for maximum iterations
+ si->setIntParam(OsiMaxNumIteration,options_[0]);
+
+ //Load the problem to OSI
+ si->loadProblem(*matrix , lb_ , ub_, objMatrix_ , conlb_ , conub_);
+
+ //Solve the problem
+ si->initialSolve();
+
+}
+
+ //Output the solution to Scilab
+ //get solution for x
+ const double* LinCLP::getX()
+ {
+ xValue_ = si->getColSolution();
+ return xValue_;
+ }
+
+ //get objective value
+ double LinCLP::getObjVal()
+ {
+ objValue_ = si->getObjValue();
+ return objValue_;
+ }
+
+ //get exit status
+ int LinCLP::returnStatus()
+ {
+ status_;
+ if(si->isProvenOptimal())
+ status_=0;
+ else if(si->isProvenPrimalInfeasible())
+ status_=1;
+ else if(si->isProvenDualInfeasible())
+ status_=2;
+ else if(si->isIterationLimitReached())
+ status_=3;
+ else if(si->isAbandoned())
+ status_=4;
+ else if(si->isPrimalObjectiveLimitReached())
+ status_=5;
+ else if(si->isDualObjectiveLimitReached())
+ status_=6;
+ return status_;
+ }
+
+ //get number of iterations
+ double LinCLP::iterCount()
+ {
+ iterations_ = si->getIterationCount();
+ return iterations_;
+ }
+
+ //get lower vector
+ const double* LinCLP::getReducedCost()
+ {
+ reducedCost_ = si->getReducedCost();
+ return reducedCost_;
+ }
+
+ //get dual vector
+ double* LinCLP::getDual()
+ {
+ dual_ = si->getRowPrice();
+ return dual_;
+ }
+
+}
+
diff --git a/sci_gateway/cpp/sci_LinProg.cpp b/sci_gateway/cpp/sci_LinProg.cpp
new file mode 100644
index 0000000..8a4ec25
--- /dev/null
+++ b/sci_gateway/cpp/sci_LinProg.cpp
@@ -0,0 +1,150 @@
+/*
+ * Linear Solver Toolbox for Scilab using CLP library
+ * Authors :
+ Guru Pradeep Reddy
+ Bhanu Priya Sayal
+*/
+
+#include "sci_iofunc.hpp"
+#include "LinCLP.hpp"
+
+extern "C"{
+#include
+#include
+#include
+#include
+
+//Solver function
+int sci_linearprog(char *fname)
+{
+ //Objective function
+ double* obj;
+ //Constraint matrix coefficients
+ double* conMatrix;
+ //Constraints upper bound
+ double* conlb;
+ //Constraints lower bound
+ double* conub;
+ //Lower bounds for variables
+ double* lb;
+ //Upper bounds for variables
+ double* ub;
+ //options for maximum iterations and writing mps
+ double* options;
+ //Flag for Mps
+ double flagMps;
+ //mps file path
+ char * mpsFile;
+ //Error structure in Scilab
+ SciErr sciErr;
+ //Number of rows and columns in objective function
+ int nVars=0, nCons=0,temp1=0,temp2=0;
+
+ CheckInputArgument(pvApiCtx , 9 , 9); //Checking the input arguments
+ CheckOutputArgument(pvApiCtx , 6, 6); //Checking the output arguments
+
+ ////////// Manage the input argument //////////
+
+ //Number of Variables
+ if(getIntFromScilab(1,&nVars))
+ {
+ return 1;
+ }
+
+ //Number of Constraints
+ if (getIntFromScilab(2,&nCons))
+ {
+ return 1;
+ }
+
+ //Objective function from Scilab
+ temp1 = nVars;
+ temp2 = nCons;
+ if (getFixedSizeDoubleMatrixFromScilab(3,1,temp1,&obj))
+ {
+ return 1;
+ }
+
+ if (nCons!=0)
+ {
+ //conMatrix matrix from scilab
+ temp1 = nCons;
+ temp2 = nVars;
+
+ if (getFixedSizeDoubleMatrixFromScilab(4,temp1,temp2,&conMatrix))
+ {
+ return 1;
+ }
+
+ //conLB matrix from scilab
+ temp1 = nCons;
+ temp2 = 1;
+ if (getFixedSizeDoubleMatrixFromScilab(5,temp1,temp2,&conlb))
+ {
+ return 1;
+ }
+
+ //conUB matrix from scilab
+ if (getFixedSizeDoubleMatrixFromScilab(6,temp1,temp2,&conub))
+ {
+ return 1;
+ }
+
+ }
+
+ //lb matrix from scilab
+ temp1 = 1;
+ temp2 = nVars;
+ if (getFixedSizeDoubleMatrixFromScilab(7,temp1,temp2,&lb))
+ {
+ return 1;
+ }
+
+
+ //ub matrix from scilab
+ if (getFixedSizeDoubleMatrixFromScilab(8,temp1,temp2,&ub))
+ {
+ return 1;
+ }
+
+ //get options from scilab
+ if(getFixedSizeDoubleMatrixInList(9 , 2 , 1 , 1 , &options))
+ {
+ return 1;
+ }
+
+ //Call to the Clp Solver
+ LinCLP* Prob = new LinCLP(nVars,nCons,obj,conMatrix,conlb,conub,lb,ub,options);
+
+ //Output the solution to Scilab
+ //get solution for x
+ double* xValue = Prob->getX();
+
+ //get objective value
+ double objValue = Prob->getObjVal();
+
+ //get Status value
+ double status = Prob->returnStatus();
+
+ //get number of iterations
+ double iterations = Prob->iterCount();
+
+ //get reduced cost
+ double* reducedCost = Prob->getReducedCost();
+
+ //get dual vector
+ double* dual = Prob->getDual();
+
+ returnDoubleMatrixToScilab(1 , 1 , nVars , xValue);
+ returnDoubleMatrixToScilab(2 , 1 , 1 , &objValue);
+ returnDoubleMatrixToScilab(3 , 1 , 1 , &status);
+ returnDoubleMatrixToScilab(4 , 1 , 1 , &iterations);
+ returnDoubleMatrixToScilab(5 , 1 , nVars , reducedCost);
+ returnDoubleMatrixToScilab(6 , 1 , nCons , dual);
+
+ }
+}
+
+
+
+
diff --git a/sci_gateway/cpp/sci_iofunc.cpp b/sci_gateway/cpp/sci_iofunc.cpp
index e92c318..8dc2acf 100644
--- a/sci_gateway/cpp/sci_iofunc.cpp
+++ b/sci_gateway/cpp/sci_iofunc.cpp
@@ -1,14 +1,6 @@
-// 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: Keyur Joshi, Harpreet Singh
-// Organization: FOSSEE, IIT Bombay
-// Email: toolbox@scilab.in
-
+// Symphony Toolbox for Scilab
+// (Definition of) Functions for input and output from Scilab
+// By Keyur Joshi
#include "api_scilab.h"
#include "Scierror.h"
@@ -16,6 +8,35 @@
#include "BOOL.h"
#include
+using namespace std;
+
+int getFunctionFromScilab(int argNum, int **dest)
+{
+ //data declarations
+ SciErr sciErr;
+ int iRet,*varAddress, iType;
+ double inputDouble;
+ const char errMsg[]="Wrong type for input argument #%d: A function is expected.\n";
+ const int errNum=999;
+ //get variable address
+ sciErr = getVarAddressFromPosition(pvApiCtx, 1, dest);
+ if(sciErr.iErr)
+ {
+ printError(&sciErr, 0);
+ return 1;
+ }
+ //check that the variable is necessarily a function
+ sciErr = getVarType(pvApiCtx, *dest, &iType);
+ if(sciErr.iErr || iType != sci_c_function)
+ {
+ Scierror(errNum,errMsg,argNum);
+ return 1;
+ }
+ return 0;
+
+}
+
+
int getDoubleFromScilab(int argNum, double *dest)
{
//data declarations
@@ -187,6 +208,29 @@ int getFixedSizeDoubleMatrixInList(int argNum, int itemPos, int rows, int cols,
printError(&sciErr, 0);
return 1;
}
+ return 0;
+}
+
+int getStringFromScilab(int argNum,char **dest)
+{
+ int *varAddress,inputMatrixRows,inputMatrixCols;
+ SciErr sciErr;
+ sciErr = getVarAddressFromPosition(pvApiCtx, argNum, &varAddress);
+
+ //check whether there is an error or not.
+ if (sciErr.iErr)
+ {
+ printError(&sciErr, 0);
+ return 1;
+ }
+ if ( !isStringType(pvApiCtx,varAddress) )
+ {
+ Scierror(999,"Wrong type for input argument 1: A file name is expected.\n");
+ return 1;
+ }
+ //read the value in that pointer pointing to file name
+ getAllocatedSingleString(pvApiCtx, varAddress, dest);
+
}
int return0toScilab()
@@ -255,3 +299,4 @@ int returnIntegerMatrixToScilab(int itemPos, int rows, int cols, int *dest)
return 0;
}
+
diff --git a/sci_gateway/cpp/sci_iofunc.hpp b/sci_gateway/cpp/sci_iofunc.hpp
index fc379f4..2c84c82 100644
--- a/sci_gateway/cpp/sci_iofunc.hpp
+++ b/sci_gateway/cpp/sci_iofunc.hpp
@@ -1,25 +1,19 @@
-// 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: Keyur Joshi, Harpreet Singh
-// Organization: FOSSEE, IIT Bombay
-// Email: toolbox@scilab.in
+// Symphony Toolbox for Scilab
+// (Declaration of) Functions for input and output from Scilab
+// By Keyur Joshi
#ifndef SCI_IOFUNCHEADER
#define SCI_IOFUNCHEADER
//input
+int getFunctionFromScilab(int argNum, int **dest);
int getDoubleFromScilab(int argNum, double *dest);
int getUIntFromScilab(int argNum, int *dest);
int getIntFromScilab(int argNum, int *dest);
int getFixedSizeDoubleMatrixFromScilab(int argNum, int rows, int cols, double **dest);
int getDoubleMatrixFromScilab(int argNum, int *rows, int *cols, double **dest);
int getFixedSizeDoubleMatrixInList(int argNum, int itemPos, int rows, int cols, double **dest);
-
+int getStringFromScilab(int argNum,char** dest);
//output
int return0toScilab();
diff --git a/sci_gateway/cpp/sci_ipoptfminbnd.cpp b/sci_gateway/cpp/sci_ipoptfminbnd.cpp
new file mode 100644
index 0000000..aa5addf
--- /dev/null
+++ b/sci_gateway/cpp/sci_ipoptfminbnd.cpp
@@ -0,0 +1,195 @@
+// Copyright (C) 2015 - IIT Bombay - FOSSEE
+//
+// Author: R.Vidyadhar & Vignesh Kannan
+// Organization: FOSSEE, IIT Bombay
+// Email: rvidhyadar@gmail.com & vignesh2496@gmail.com
+// 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
+
+
+#include "sci_iofunc.hpp"
+#include "IpIpoptApplication.hpp"
+#include "minbndNLP.hpp"
+#include
+
+extern "C"
+{
+#include
+#include
+#include
+#include
+#include
+#include
+
+using namespace std;
+
+int sci_solveminbndp(char *fname)
+{
+ using namespace Ipopt;
+
+ CheckInputArgument(pvApiCtx, 5, 5);
+ CheckOutputArgument(pvApiCtx, 9, 9);
+
+ // Error management variable
+ SciErr sciErr;
+
+ //Function pointers,lower bound and upper bound pointers
+ int* funptr=NULL;
+ int* gradhesptr=NULL;
+ double* varLB=NULL;
+ double* varUB=NULL;
+
+ // Input arguments
+ double *cpu_time=NULL,*max_iter=NULL,*tol_val=NULL;
+ static unsigned int nVars = 0,nCons = 0;
+ unsigned int temp1 = 0,temp2 = 0, iret = 0;
+ int x1_rows, x1_cols, x2_rows, x2_cols;
+
+ // Output arguments
+ double *fX = NULL, ObjVal=0,iteration=0,cpuTime=0,fobj_eval=0;
+ double *fZl=NULL;
+ double *fZu=NULL;
+ double dual_inf, constr_viol, complementarity, kkt_error;
+ int rstatus = 0;
+ int int_fobj_eval, int_constr_eval, int_fobj_grad_eval, int_constr_jac_eval, int_hess_eval;
+
+ ////////// Manage the input argument //////////
+
+ //Objective Function
+ if(getFunctionFromScilab(1,&funptr))
+ {
+ return 1;
+ }
+
+ //Function for gradient and hessian
+ if(getFunctionFromScilab(2,&gradhesptr))
+ {
+ return 1;
+ }
+
+ //x1(lower bound) matrix from scilab
+ if(getDoubleMatrixFromScilab(3, &x1_rows, &x1_cols, &varLB))
+ {
+ return 1;
+ }
+
+ //x2(upper bound) matrix from scilab
+ if(getDoubleMatrixFromScilab(4, &x2_rows, &x2_cols, &varUB))
+ {
+ return 1;
+ }
+
+ //Getting number of iterations
+ if(getFixedSizeDoubleMatrixInList(5,2,temp1,temp2,&max_iter))
+ {
+ return 1;
+ }
+
+ //Getting Cpu Time
+ if(getFixedSizeDoubleMatrixInList(5,4,temp1,temp2,&cpu_time))
+ {
+ return 1;
+ }
+
+ //Getting Tolerance Value
+ if(getFixedSizeDoubleMatrixInList(5,6,temp1,temp2,&tol_val))
+ {
+ return 1;
+ }
+
+
+ //Initialization of parameters
+ nVars=x1_rows;
+ nCons=0;
+
+ // Starting Ipopt
+
+ SmartPtr Prob = new minbndNLP(nVars,nCons,varLB,varUB);
+
+ SmartPtr app = IpoptApplicationFactory();
+ app->RethrowNonIpoptException(true);
+
+ ////////// Managing the parameters //////////
+
+ app->Options()->SetNumericValue("tol", *tol_val);
+ app->Options()->SetIntegerValue("max_iter", (int)*max_iter);
+ app->Options()->SetNumericValue("max_cpu_time", *cpu_time);
+
+ ///////// Initialize the IpoptApplication and process the options /////////
+ ApplicationReturnStatus status;
+ status = app->Initialize();
+ if (status != Solve_Succeeded) {
+ sciprint("\n*** Error during initialization!\n");
+ return (int) status;
+ }
+ // Ask Ipopt to solve the problem
+ status = app->OptimizeTNLP(Prob);
+
+ //Get the solve statistics
+ cpuTime = app->Statistics()->TotalCPUTime();
+ app->Statistics()->NumberOfEvaluations(int_fobj_eval, int_constr_eval, int_fobj_grad_eval, int_constr_jac_eval, int_hess_eval);
+ app->Statistics()->Infeasibilities(dual_inf, constr_viol, complementarity, kkt_error);
+ rstatus = Prob->returnStatus();
+
+ ////////// Manage the output argument //////////
+
+
+ fX = Prob->getX();
+ ObjVal = Prob->getObjVal();
+ iteration = Prob->iterCount();
+ fobj_eval=(double)int_fobj_eval;
+ fZl = Prob->getZl();
+ fZu = Prob->getZu();
+
+ if (returnDoubleMatrixToScilab(1, 1, nVars, fX))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(2, 1, 1, &ObjVal))
+ {
+ return 1;
+ }
+
+ if (returnIntegerMatrixToScilab(3, 1, 1, &rstatus))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(4, 1, 1, &iteration))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(5, 1, 1, &cpuTime))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(6, 1, 1, &fobj_eval))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(7, 1, 1, &dual_inf))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(8, 1, nVars, fZl))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(9, 1, nVars, fZu))
+ {
+ return 1;
+ }
+
+
+ return 0;
+}
+}
diff --git a/sci_gateway/cpp/sci_ipoptfmincon.cpp b/sci_gateway/cpp/sci_ipoptfmincon.cpp
new file mode 100644
index 0000000..551af41
--- /dev/null
+++ b/sci_gateway/cpp/sci_ipoptfmincon.cpp
@@ -0,0 +1,273 @@
+// Copyright (C) 2015 - IIT Bombay - FOSSEE
+//
+// Author: R.Vidyadhar & Vignesh Kannan
+// Organization: FOSSEE, IIT Bombay
+// Email: rvidhyadar@gmail.com & vignesh2496@gmail.com
+// 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
+
+
+#include "sci_iofunc.hpp"
+#include "IpIpoptApplication.hpp"
+#include "minconNLP.hpp"
+#include
+
+extern "C"
+{
+#include
+#include
+#include
+#include
+#include
+#include
+
+using namespace std;
+
+int sci_solveminconp(char *fname)
+{
+ using namespace Ipopt;
+
+ CheckInputArgument(pvApiCtx, 20, 20);
+ CheckOutputArgument(pvApiCtx, 12, 12);
+
+ // Error management variable
+ SciErr sciErr;
+
+ //Function pointers, input matrix(Starting point) pointer, flag variable
+ int* funptr=NULL;
+ int* gradhesptr=NULL;
+ double *x0ptr=NULL, *lbptr=NULL, *ubptr=NULL,*Aptr=NULL, *bptr=NULL, *Aeqptr=NULL, *beqptr=NULL;
+ double flag1=0,flag2=0,flag3=0,nonlinCon=0,nonlinIneqCon=0;
+
+
+ // Input arguments
+ double *cpu_time=NULL,*max_iter=NULL;
+ static unsigned int nVars = 0,nCons = 0;
+ unsigned int temp1 = 0,temp2 = 0, iret = 0;
+ int x0_rows=0, x0_cols=0, lb_rows=0, lb_cols=0, ub_rows=0, ub_cols=0, A_rows=0, A_cols=0, b_rows=0, b_cols=0, Aeq_rows=0, Aeq_cols=0, beq_rows=0, beq_cols=0;
+
+ // Output arguments
+ double *fX = NULL, ObjVal=0,iteration=0,cpuTime=0,fobj_eval=0;
+ double dual_inf, constr_viol, complementarity, kkt_error;
+ double *fGrad = NULL;
+ double *fHess = NULL;
+ double *fLambda = NULL;
+ double *fZl=NULL;
+ double *fZu=NULL;
+ int rstatus = 0;
+ int int_fobj_eval, int_constr_eval, int_fobj_grad_eval, int_constr_jac_eval, int_hess_eval;
+
+ ////////// Manage the input argument //////////
+
+ //Objective Function
+ if(getFunctionFromScilab(1,&funptr))
+ {
+ return 1;
+ }
+
+ //Function for gradient and hessian
+ if(getFunctionFromScilab(2,&gradhesptr))
+ {
+ return 1;
+ }
+
+ //x0(starting point) matrix from scilab
+ if(getDoubleMatrixFromScilab(18, &x0_rows, &x0_cols, &x0ptr))
+ {
+ return 1;
+ }
+
+ //Getting number of iterations
+ if(getFixedSizeDoubleMatrixInList(19,2,temp1,temp2,&max_iter))
+ {
+ return 1;
+ }
+
+ //Getting Cpu Time
+ if(getFixedSizeDoubleMatrixInList(19,4,temp1,temp2,&cpu_time))
+ {
+ return 1;
+ }
+
+ //Getting matrix representing linear inequality constraints
+ if(getDoubleMatrixFromScilab(3, &A_rows, &A_cols, &Aptr))
+ {
+ return 1;
+ }
+
+ //Getting matrix representing bounds of linear inequality constraints
+ if(getDoubleMatrixFromScilab(4, &b_rows, &b_cols, &bptr))
+ {
+ return 1;
+ }
+
+ //Getting matrix representing linear equality constraints
+ if(getDoubleMatrixFromScilab(5, &Aeq_rows, &Aeq_cols, &Aeqptr))
+ {
+ return 1;
+ }
+
+ //Getting matrix representing bounds of linear inequality constraints
+ if(getDoubleMatrixFromScilab(6, &beq_rows, &beq_cols, &beqptr))
+ {
+ return 1;
+ }
+
+ //Getting matrix representing linear inequality constraints
+ if(getDoubleMatrixFromScilab(7, &lb_rows, &lb_cols, &lbptr))
+ {
+ return 1;
+ }
+
+ //Getting matrix representing linear inequality constraints
+ if(getDoubleMatrixFromScilab(8, &ub_rows, &ub_cols, &ubptr))
+ {
+ return 1;
+ }
+
+ //Number of non-linear constraints
+ if(getDoubleFromScilab(9, &nonlinCon))
+ {
+ return 1;
+ }
+
+ //Number of non-linear inequality constraints
+ if(getDoubleFromScilab(10, &nonlinIneqCon))
+ {
+ return 1;
+ }
+
+ //Getting the required flag variables
+
+ if(getDoubleFromScilab(12, &flag1))
+ {
+ return 1;
+ }
+
+ if(getDoubleFromScilab(14, &flag2))
+ {
+ return 1;
+ }
+
+ if(getDoubleFromScilab(16, &flag3))
+ {
+ return 1;
+ }
+
+ //Number of variables and constraints
+ nVars = x0_cols;
+ nCons = A_rows + Aeq_rows + nonlinCon;
+
+
+ // Starting Ipopt
+
+ SmartPtr Prob = new minconNLP(nVars, nCons, x0ptr, Aptr, bptr, Aeqptr, beqptr, A_rows, A_cols, b_rows, b_cols, Aeq_rows, Aeq_cols, beq_rows, beq_cols, lbptr, ubptr, nonlinCon, nonlinIneqCon, flag1, flag2, flag3);
+ SmartPtr app = IpoptApplicationFactory();
+ app->RethrowNonIpoptException(true);
+
+ ////////// Managing the parameters //////////
+
+ app->Options()->SetNumericValue("tol", 1e-7);
+ app->Options()->SetIntegerValue("max_iter", (int)*max_iter);
+ app->Options()->SetNumericValue("max_cpu_time", *cpu_time);
+
+ ///////// Initialize the IpoptApplication and process the options /////////
+ ApplicationReturnStatus status;
+ status = app->Initialize();
+ if (status != Solve_Succeeded)
+ {
+ sciprint("\n*** Error during initialization!\n");
+ return (int) status;
+ }
+
+ // Ask Ipopt to solve the problem
+ status = app->OptimizeTNLP(Prob);
+
+ //Get the solve statistics
+ cpuTime = app->Statistics()->TotalCPUTime();
+ app->Statistics()->NumberOfEvaluations(int_fobj_eval, int_constr_eval, int_fobj_grad_eval, int_constr_jac_eval, int_hess_eval);
+ app->Statistics()->Infeasibilities(dual_inf, constr_viol, complementarity, kkt_error);
+ rstatus = Prob->returnStatus();
+ fobj_eval=(double)int_fobj_eval;
+
+ ////////// Manage the output argument //////////
+
+ fX = Prob->getX();
+ fGrad = Prob->getGrad();
+ fHess = Prob->getHess();
+ fLambda = Prob->getLambda();
+ fZl = Prob->getZl();
+ fZu = Prob->getZu();
+ ObjVal = Prob->getObjVal();
+ iteration = Prob->iterCount();
+
+ if (returnDoubleMatrixToScilab(1, 1, nVars, fX))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(2, 1, 1, &ObjVal))
+ {
+ return 1;
+ }
+
+ if (returnIntegerMatrixToScilab(3, 1, 1, &rstatus))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(4, 1, 1, &iteration))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(5, 1, 1, &cpuTime))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(6, 1, 1, &fobj_eval))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(7, 1, 1, &dual_inf))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(8, 1, nCons, fLambda))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(9, 1, nVars, fZl))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(10, 1, nVars, fZu))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(11, 1, nVars, fGrad))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(12, 1, nVars*nVars, fHess))
+ {
+ return 1;
+ }
+
+ // As the SmartPtrs go out of scope, the reference count
+ // will be decremented and the objects will automatically
+ // be deleted.*/
+
+ return 0;
+}
+}
diff --git a/sci_gateway/cpp/sci_ipoptfminunc.cpp b/sci_gateway/cpp/sci_ipoptfminunc.cpp
new file mode 100644
index 0000000..19c59ac
--- /dev/null
+++ b/sci_gateway/cpp/sci_ipoptfminunc.cpp
@@ -0,0 +1,201 @@
+// Copyright (C) 2015 - IIT Bombay - FOSSEE
+//
+// Author: R.Vidyadhar & Vignesh Kannan
+// Organization: FOSSEE, IIT Bombay
+// Email: rvidhyadar@gmail.com & vignesh2496@gmail.com
+// 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
+
+
+#include "sci_iofunc.hpp"
+#include "IpIpoptApplication.hpp"
+#include "minuncNLP.hpp"
+#include
+
+extern "C"
+{
+#include
+#include
+#include
+#include
+#include
+#include
+
+using namespace std;
+
+int sci_solveminuncp(char *fname)
+{
+ using namespace Ipopt;
+
+ CheckInputArgument(pvApiCtx, 8, 8);
+ CheckOutputArgument(pvApiCtx, 9, 9);
+
+ // Error management variable
+ SciErr sciErr;
+
+ //Function pointers, input matrix(Starting point) pointer, flag variable
+ int* funptr=NULL;
+ int* gradhesptr=NULL;
+ double* x0ptr=NULL;
+ double flag1,flag2;
+
+
+ // Input arguments
+ double *cpu_time=NULL,*max_iter=NULL;
+ static unsigned int nVars = 0,nCons = 0;
+ unsigned int temp1 = 0,temp2 = 0, iret = 0;
+ int x0_rows, x0_cols;
+
+ // Output arguments
+ double *fX = NULL, ObjVal=0,iteration=0,cpuTime=0,fobj_eval=0;
+ double dual_inf, constr_viol, complementarity, kkt_error;
+ double *fGrad= NULL;
+ double *fHess= NULL;
+ int rstatus = 0;
+ int int_fobj_eval, int_constr_eval, int_fobj_grad_eval, int_constr_jac_eval, int_hess_eval;
+
+ ////////// Manage the input argument //////////
+
+ //Objective Function
+ if(getFunctionFromScilab(1,&funptr))
+ {
+ return 1;
+ }
+
+ //Function for gradient and hessian
+ if(getFunctionFromScilab(2,&gradhesptr))
+ {
+ return 1;
+ }
+
+ //Flag for Gradient from Scilab
+ if(getDoubleFromScilab(3, &flag1))
+ {
+ return 1;
+ }
+
+ //Flag for Hessian from Scilab
+ if(getDoubleFromScilab(5, &flag2))
+ {
+ return 1;
+ }
+
+ //x0(starting point) matrix from scilab
+ if(getDoubleMatrixFromScilab(7, &x0_rows, &x0_cols, &x0ptr))
+ {
+ return 1;
+ }
+
+ //Getting number of iterations
+ if(getFixedSizeDoubleMatrixInList(8,2,temp1,temp2,&max_iter))
+ {
+ return 1;
+ }
+
+ //Getting Cpu Time
+ if(getFixedSizeDoubleMatrixInList(8,4,temp1,temp2,&cpu_time))
+ {
+ return 1;
+ }
+
+
+ //Initialization of parameters
+ nVars=x0_cols;
+ nCons=0;
+
+ // Starting Ipopt
+
+ SmartPtr Prob = new minuncNLP(nVars, nCons, x0ptr, flag1, flag2);
+ SmartPtr app = IpoptApplicationFactory();
+ app->RethrowNonIpoptException(true);
+
+ ////////// Managing the parameters //////////
+
+ app->Options()->SetNumericValue("tol", 1e-7);
+ app->Options()->SetIntegerValue("max_iter", (int)*max_iter);
+ app->Options()->SetNumericValue("max_cpu_time", *cpu_time);
+
+ ///////// Initialize the IpoptApplication and process the options /////////
+ ApplicationReturnStatus status;
+ status = app->Initialize();
+ if (status != Solve_Succeeded)
+ {
+ sciprint("\n*** Error during initialization!\n");
+ return (int) status;
+ }
+ // Ask Ipopt to solve the problem
+
+ status = app->OptimizeTNLP(Prob);
+
+ cpuTime = app->Statistics()->TotalCPUTime();
+
+ app->Statistics()->NumberOfEvaluations(int_fobj_eval, int_constr_eval, int_fobj_grad_eval, int_constr_jac_eval, int_hess_eval);
+
+ app->Statistics()->Infeasibilities(dual_inf, constr_viol, complementarity, kkt_error);
+
+ rstatus = Prob->returnStatus();
+
+ ////////// Manage the output argument //////////
+
+ fX = Prob->getX();
+ fGrad = Prob->getGrad();
+ fHess = Prob->getHess();
+ ObjVal = Prob->getObjVal();
+ iteration = Prob->iterCount();
+ fobj_eval = (double)int_fobj_eval;
+
+ if (returnDoubleMatrixToScilab(1, 1, nVars, fX))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(2, 1, 1, &ObjVal))
+ {
+ return 1;
+ }
+
+ if (returnIntegerMatrixToScilab(3, 1, 1, &rstatus))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(4, 1, 1, &iteration))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(5, 1, 1, &cpuTime))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(6, 1, 1, &fobj_eval))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(7, 1, 1, &dual_inf))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(8, 1, nVars, fGrad))
+ {
+ return 1;
+ }
+
+ if (returnDoubleMatrixToScilab(9, 1, nVars*nVars, fHess))
+ {
+ return 1;
+ }
+
+ // As the SmartPtrs go out of scope, the reference count
+ // will be decremented and the objects will automatically
+ // be deleted.*/
+
+ return 0;
+}
+}
diff --git a/sci_gateway/cpp/sci_minbndNLP.cpp b/sci_gateway/cpp/sci_minbndNLP.cpp
new file mode 100644
index 0000000..9a7024e
--- /dev/null
+++ b/sci_gateway/cpp/sci_minbndNLP.cpp
@@ -0,0 +1,353 @@
+// Copyright (C) 2015 - IIT Bombay - FOSSEE
+//
+// Author: R.Vidyadhar & Vignesh Kannan
+// Organization: FOSSEE, IIT Bombay
+// Email: rvidhyadar@gmail.com & vignesh2496@gmail.com
+// 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
+
+
+#include "minbndNLP.hpp"
+#include "IpIpoptData.hpp"
+#include "sci_iofunc.hpp"
+
+extern "C"
+{
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+using namespace std;
+using namespace Ipopt;
+
+minbndNLP::~minbndNLP()
+{
+ free(finalX_);
+ free(finalZu_);
+ free(finalZl_);
+}
+
+//get NLP info such as number of variables,constraints,no.of elements in jacobian and hessian to allocate memory
+bool minbndNLP::get_nlp_info(Index& n, Index& m, Index& nnz_jac_g, Index& nnz_h_lag, IndexStyleEnum& index_style)
+{
+ n=numVars_; // Number of variables
+ m=numConstr_; // Number of constraints
+ nnz_jac_g = 0; // No. of elements in Jacobian of constraints
+ nnz_h_lag = n*(n+1)/2; // No. of elements in lower traingle of Hessian of the Lagrangian.
+ index_style=C_STYLE; // Index style of matrices
+
+ return true;
+}
+
+//get variable and constraint bound info
+bool minbndNLP::get_bounds_info(Index n, Number* x_l, Number* x_u, Index m, Number* g_l, Number* g_u)
+{
+ for(Index i=0;iiter_count();
+ }
+}
+
+
+const double * minbndNLP::getX()
+{
+ return finalX_;
+}
+
+double minbndNLP::getObjVal()
+{
+ return finalObjVal_;
+}
+
+const double * minbndNLP::getZl()
+{
+ return finalZl_;
+}
+
+const double * minbndNLP::getZu()
+{
+ return finalZu_;
+}
+
+double minbndNLP::iterCount()
+{
+ return (double)iter_;
+}
+
+int minbndNLP::returnStatus()
+{
+ return status_;
+}
+
+}
+
diff --git a/sci_gateway/cpp/sci_minconNLP.cpp b/sci_gateway/cpp/sci_minconNLP.cpp
new file mode 100644
index 0000000..2c6d6af
--- /dev/null
+++ b/sci_gateway/cpp/sci_minconNLP.cpp
@@ -0,0 +1,797 @@
+// Copyright (C) 2015 - IIT Bombay - FOSSEE
+//
+// Author: R.Vidyadhar & Vignesh Kannan
+// Organization: FOSSEE, IIT Bombay
+// Email: rvidhyadar@gmail.com & vignesh2496@gmail.com
+// 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
+
+
+#include "minconNLP.hpp"
+#include "IpIpoptData.hpp"
+#include "sci_iofunc.hpp"
+
+extern "C"
+{
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+using namespace std;
+using namespace Ipopt;
+
+minconNLP::~minconNLP()
+{
+ free(finalX_);
+ free(finalGradient_);
+ free(finalHessian_);
+ free(finalZu_);
+ free(finalZl_);
+ free(finalLambda_);
+}
+
+//get NLP info such as number of variables,constraints,no.of elements in jacobian and hessian to allocate memory
+bool minconNLP::get_nlp_info(Index& n, Index& m, Index& nnz_jac_g, Index& nnz_h_lag, IndexStyleEnum& index_style)
+{
+ finalGradient_ = (double*)malloc(sizeof(double) * numVars_ * 1);
+ finalHessian_ = (double*)malloc(sizeof(double) * numVars_ * numVars_);
+
+ n=numVars_; // Number of variables
+ m=numConstr_; // Number of constraints
+
+ nnz_jac_g = n*m; // No. of elements in Jacobian of constraints
+ nnz_h_lag = n*(n+1)/2; // No. of elements in lower traingle of Hessian of the Lagrangian.
+
+ index_style=C_STYLE; // Index style of matrices
+ return true;
+}
+
+//get variable and constraint bound info
+bool minconNLP::get_bounds_info(Index n, Number* x_l, Number* x_u, Index m, Number* g_l, Number* g_u)
+{
+ unsigned int i;
+
+ //assigning bounds for the variables
+ for(i=0;iiter_count();
+}
+
+
+const double * minconNLP::getX()
+{
+ return finalX_;
+}
+
+const double * minconNLP::getGrad()
+{
+ return finalGradient_;
+}
+
+const double * minconNLP::getHess()
+{
+ return finalHessian_;
+}
+
+const double * minconNLP::getZl()
+{
+ return finalZl_;
+}
+
+const double * minconNLP::getZu()
+{
+ return finalZu_;
+}
+
+const double * minconNLP::getLambda()
+{
+ return finalLambda_;
+}
+
+double minconNLP::getObjVal()
+{
+ return finalObjVal_;
+}
+
+double minconNLP::iterCount()
+{
+ return (double)iter_;
+}
+
+int minconNLP::returnStatus()
+{
+ return status_;
+}
+
+}
+
+
+
diff --git a/sci_gateway/cpp/sci_minuncNLP.cpp b/sci_gateway/cpp/sci_minuncNLP.cpp
new file mode 100644
index 0000000..874c093
--- /dev/null
+++ b/sci_gateway/cpp/sci_minuncNLP.cpp
@@ -0,0 +1,377 @@
+// Copyright (C) 2015 - IIT Bombay - FOSSEE
+//
+// Author: R.Vidyadhar & Vignesh Kannan
+// Organization: FOSSEE, IIT Bombay
+// Email: rvidhyadar@gmail.com & vignesh2496@gmail.com
+// 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
+
+
+#include "minuncNLP.hpp"
+#include "IpIpoptData.hpp"
+#include "sci_iofunc.hpp"
+
+extern "C"
+{
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+using namespace std;
+using namespace Ipopt;
+
+minuncNLP::~minuncNLP()
+{
+ free(finalX_);
+ free(finalGradient_);
+ free(finalHessian_);
+}
+
+//get NLP info such as number of variables,constraints,no.of elements in jacobian and hessian to allocate memory
+bool minuncNLP::get_nlp_info(Index& n, Index& m, Index& nnz_jac_g, Index& nnz_h_lag, IndexStyleEnum& index_style)
+{
+ finalGradient_ = (double*)malloc(sizeof(double) * numVars_ * 1);
+ finalHessian_ = (double*)malloc(sizeof(double) * numVars_ * numVars_);
+ n=numVars_; // Number of variables
+ m=numConstr_; // Number of constraints
+ nnz_jac_g = 0; // No. of elements in Jacobian of constraints
+ nnz_h_lag = n*(n+1)/2; // No. of elements in lower traingle of Hessian of the Lagrangian.
+ index_style=C_STYLE; // Index style of matrices
+ return true;
+}
+
+//get variable and constraint bound info
+bool minuncNLP::get_bounds_info(Index n, Number* x_l, Number* x_u, Index m, Number* g_l, Number* g_u)
+{
+ unsigned int i;
+ for(i=0;iiter_count();
+}
+
+
+const double * minuncNLP::getX()
+{
+ return finalX_;
+}
+
+const double * minuncNLP::getGrad()
+{
+ return finalGradient_;
+}
+
+const double * minuncNLP::getHess()
+{
+ return finalHessian_;
+}
+
+double minuncNLP::getObjVal()
+{
+ return finalObjVal_;
+}
+
+double minuncNLP::iterCount()
+{
+ return (double)iter_;
+}
+
+int minuncNLP::returnStatus()
+{
+ return status_;
+}
+
+}
+
+
+
--
cgit
|