diff options
author | Georgey | 2017-07-05 11:43:12 +0530 |
---|---|---|
committer | Georgey | 2017-07-05 11:43:12 +0530 |
commit | 66089674c189f557b401f2ad0cf6b35354caadfa (patch) | |
tree | d174ef3f680e241e8bcbc460727c622af260b942 | |
parent | 536bbd0cd8e4ca5b29d07e472e682bbb5db63a97 (diff) | |
download | FOSSEE-Optimization-toolbox-66089674c189f557b401f2ad0cf6b35354caadfa.tar.gz FOSSEE-Optimization-toolbox-66089674c189f557b401f2ad0cf6b35354caadfa.tar.bz2 FOSSEE-Optimization-toolbox-66089674c189f557b401f2ad0cf6b35354caadfa.zip |
Added gateway files
64 files changed, 3524 insertions, 26 deletions
diff --git a/sci_gateway/cpp/FOSSEE_Optimization_Toolbox.c b/sci_gateway/cpp/FOSSEE_Optimization_Toolbox.c new file mode 100644 index 0000000..46e2b1d --- /dev/null +++ b/sci_gateway/cpp/FOSSEE_Optimization_Toolbox.c @@ -0,0 +1,154 @@ +#ifdef __cplusplus
+extern "C" {
+#endif
+#include <mex.h>
+#include <sci_gateway.h>
+#include <api_scilab.h>
+#include <MALLOC.h>
+static int direct_gateway(char *fname,void F(void)) { F();return 0;};
+extern Gatefunc sci_sym_open;
+extern Gatefunc sci_sym_close;
+extern Gatefunc sci_sym_isEnvActive;
+extern Gatefunc sci_sym_set_defaults;
+extern Gatefunc sci_sym_set_int_param;
+extern Gatefunc sci_sym_get_int_param;
+extern Gatefunc sci_sym_set_dbl_param;
+extern Gatefunc sci_sym_get_dbl_param;
+extern Gatefunc sci_sym_set_str_param;
+extern Gatefunc sci_sym_get_str_param;
+extern Gatefunc sci_sym_getInfinity;
+extern Gatefunc sci_sym_loadProblemBasic;
+extern Gatefunc sci_sym_loadProblem;
+extern Gatefunc sci_sym_load_mps;
+extern Gatefunc sci_sym_get_num_int;
+extern Gatefunc sci_sym_get_num_int;
+extern Gatefunc sci_sym_get_num_int;
+extern Gatefunc sci_sym_isContinuous;
+extern Gatefunc sci_sym_isBinary;
+extern Gatefunc sci_sym_isInteger;
+extern Gatefunc sci_sym_set_continuous;
+extern Gatefunc sci_sym_set_integer;
+extern Gatefunc sci_sym_get_dbl_arr;
+extern Gatefunc sci_sym_get_dbl_arr;
+extern Gatefunc sci_sym_setVarBound;
+extern Gatefunc sci_sym_setVarBound;
+extern Gatefunc sci_sym_get_dbl_arr;
+extern Gatefunc sci_sym_setObjCoeff;
+extern Gatefunc sci_sym_getObjSense;
+extern Gatefunc sci_sym_setObjSense;
+extern Gatefunc sci_sym_get_dbl_arr;
+extern Gatefunc sci_sym_get_dbl_arr;
+extern Gatefunc sci_sym_get_dbl_arr;
+extern Gatefunc sci_sym_get_dbl_arr;
+extern Gatefunc sci_sym_setConstrBound;
+extern Gatefunc sci_sym_setConstrBound;
+extern Gatefunc sci_sym_setConstrType;
+extern Gatefunc sci_sym_get_matrix;
+extern Gatefunc sci_sym_addConstr;
+extern Gatefunc sci_sym_addVar;
+extern Gatefunc sci_sym_delete_cols;
+extern Gatefunc sci_sym_delete_rows;
+extern Gatefunc sci_sym_getPrimalBound;
+extern Gatefunc sci_sym_setPrimalBound;
+extern Gatefunc sci_sym_setColSoln;
+extern Gatefunc sci_sym_solve;
+extern Gatefunc sci_sym_get_status;
+extern Gatefunc sci_sym_get_solver_status;
+extern Gatefunc sci_sym_get_solver_status;
+extern Gatefunc sci_sym_get_solver_status;
+extern Gatefunc sci_sym_get_solver_status;
+extern Gatefunc sci_sym_get_solver_status;
+extern Gatefunc sci_sym_get_solver_status;
+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"},
+ {(Myinterfun)sci_gateway,sci_sym_isEnvActive,"sym_isEnvActive"},
+ {(Myinterfun)sci_gateway,sci_sym_set_defaults,"sym_resetParams"},
+ {(Myinterfun)sci_gateway,sci_sym_set_int_param,"sym_setIntParam"},
+ {(Myinterfun)sci_gateway,sci_sym_get_int_param,"sym_getIntParam"},
+ {(Myinterfun)sci_gateway,sci_sym_set_dbl_param,"sym_setDblParam"},
+ {(Myinterfun)sci_gateway,sci_sym_get_dbl_param,"sym_getDblParam"},
+ {(Myinterfun)sci_gateway,sci_sym_set_str_param,"sym_setStrParam"},
+ {(Myinterfun)sci_gateway,sci_sym_get_str_param,"sym_getStrParam"},
+ {(Myinterfun)sci_gateway,sci_sym_getInfinity,"sym_getInfinity"},
+ {(Myinterfun)sci_gateway,sci_sym_loadProblemBasic,"sym_loadProblemBasic"},
+ {(Myinterfun)sci_gateway,sci_sym_loadProblem,"sym_loadProblem"},
+ {(Myinterfun)sci_gateway,sci_sym_load_mps,"sym_loadMPS"},
+ {(Myinterfun)sci_gateway,sci_sym_get_num_int,"sym_getNumConstr"},
+ {(Myinterfun)sci_gateway,sci_sym_get_num_int,"sym_getNumVar"},
+ {(Myinterfun)sci_gateway,sci_sym_get_num_int,"sym_getNumElements"},
+ {(Myinterfun)sci_gateway,sci_sym_isContinuous,"sym_isContinuous"},
+ {(Myinterfun)sci_gateway,sci_sym_isBinary,"sym_isBinary"},
+ {(Myinterfun)sci_gateway,sci_sym_isInteger,"sym_isInteger"},
+ {(Myinterfun)sci_gateway,sci_sym_set_continuous,"sym_setContinuous"},
+ {(Myinterfun)sci_gateway,sci_sym_set_integer,"sym_setInteger"},
+ {(Myinterfun)sci_gateway,sci_sym_get_dbl_arr,"sym_getVarLower"},
+ {(Myinterfun)sci_gateway,sci_sym_get_dbl_arr,"sym_getVarUpper"},
+ {(Myinterfun)sci_gateway,sci_sym_setVarBound,"sym_setVarLower"},
+ {(Myinterfun)sci_gateway,sci_sym_setVarBound,"sym_setVarUpper"},
+ {(Myinterfun)sci_gateway,sci_sym_get_dbl_arr,"sym_getObjCoeff"},
+ {(Myinterfun)sci_gateway,sci_sym_setObjCoeff,"sym_setObjCoeff"},
+ {(Myinterfun)sci_gateway,sci_sym_getObjSense,"sym_getObjSense"},
+ {(Myinterfun)sci_gateway,sci_sym_setObjSense,"sym_setObjSense"},
+ {(Myinterfun)sci_gateway,sci_sym_get_dbl_arr,"sym_getRhs"},
+ {(Myinterfun)sci_gateway,sci_sym_get_dbl_arr,"sym_getConstrRange"},
+ {(Myinterfun)sci_gateway,sci_sym_get_dbl_arr,"sym_getConstrLower"},
+ {(Myinterfun)sci_gateway,sci_sym_get_dbl_arr,"sym_getConstrUpper"},
+ {(Myinterfun)sci_gateway,sci_sym_setConstrBound,"sym_setConstrLower"},
+ {(Myinterfun)sci_gateway,sci_sym_setConstrBound,"sym_setConstrUpper"},
+ {(Myinterfun)sci_gateway,sci_sym_setConstrType,"sym_setConstrType"},
+ {(Myinterfun)sci_gateway,sci_sym_get_matrix,"sym_getMatrix"},
+ {(Myinterfun)sci_gateway,sci_sym_addConstr,"sym_addConstr"},
+ {(Myinterfun)sci_gateway,sci_sym_addVar,"sym_addVar"},
+ {(Myinterfun)sci_gateway,sci_sym_delete_cols,"sym_deleteVars"},
+ {(Myinterfun)sci_gateway,sci_sym_delete_rows,"sym_deleteConstrs"},
+ {(Myinterfun)sci_gateway,sci_sym_getPrimalBound,"sym_getPrimalBound"},
+ {(Myinterfun)sci_gateway,sci_sym_setPrimalBound,"sym_setPrimalBound"},
+ {(Myinterfun)sci_gateway,sci_sym_setColSoln,"sym_setVarSoln"},
+ {(Myinterfun)sci_gateway,sci_sym_solve,"sym_solve"},
+ {(Myinterfun)sci_gateway,sci_sym_get_status,"sym_getStatus"},
+ {(Myinterfun)sci_gateway,sci_sym_get_solver_status,"sym_isOptimal"},
+ {(Myinterfun)sci_gateway,sci_sym_get_solver_status,"sym_isInfeasible"},
+ {(Myinterfun)sci_gateway,sci_sym_get_solver_status,"sym_isAbandoned"},
+ {(Myinterfun)sci_gateway,sci_sym_get_solver_status,"sym_isIterLimitReached"},
+ {(Myinterfun)sci_gateway,sci_sym_get_solver_status,"sym_isTimeLimitReached"},
+ {(Myinterfun)sci_gateway,sci_sym_get_solver_status,"sym_isTargetGapAchieved"},
+ {(Myinterfun)sci_gateway,sci_sym_getVarSoln,"sym_getVarSoln"},
+ {(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(FOSSEE_Optimization_Toolbox)()
+{
+ Rhs = Max(0, Rhs);
+ if (*(Tab[Fin-1].f) != NULL)
+ {
+ if(pvApiCtx == NULL)
+ {
+ pvApiCtx = (StrCtx*)MALLOC(sizeof(StrCtx));
+ }
+ pvApiCtx->pstName = (char*)Tab[Fin-1].name;
+ (*(Tab[Fin-1].f))(Tab[Fin-1].name,Tab[Fin-1].F);
+ }
+ return 0;
+}
+#ifdef __cplusplus
+}
+#endif
diff --git a/sci_gateway/cpp/FOSSEE_Optimization_Toolbox.def b/sci_gateway/cpp/FOSSEE_Optimization_Toolbox.def new file mode 100644 index 0000000..a7ea8ca --- /dev/null +++ b/sci_gateway/cpp/FOSSEE_Optimization_Toolbox.def @@ -0,0 +1,53 @@ +LIBRARY FOSSEE_Optimization_Toolbox.dll
+EXPORTS
+ FOSSEE_Optimization_Toolbox_
+ process_ret_val
+ sci_rmps
+ sci_linearprog
+ sci_solveqp
+ sci_solveminbndp
+ sci_solveminconp
+ sci_solveminuncp
+ sci_sym_get_status
+ sci_sym_get_solver_status
+ sci_sym_addConstr
+ sci_sym_addVar
+ sci_sym_get_dbl_arr
+ sci_sym_get_iteration_count
+ column_major_to_row_major
+ sci_sym_get_matrix
+ sci_sym_get_num_int
+ sci_sym_getInfinity
+ sci_sym_getObjSense
+ sci_sym_getRowActivity
+ sci_sym_isEnvActive
+ sci_sym_load_mps
+ sci_sym_loadProblemBasic
+ sci_sym_loadProblem
+ sci_sym_open
+ sci_sym_close
+ sci_sym_getPrimalBound
+ sci_sym_setPrimalBound
+ sci_sym_delete_cols
+ sci_sym_delete_rows
+ sci_sym_setConstrBound
+ sci_sym_setConstrType
+ sci_sym_set_continuous
+ sci_sym_set_integer
+ sci_sym_set_defaults
+ sci_sym_set_int_param
+ sci_sym_get_int_param
+ sci_sym_set_dbl_param
+ sci_sym_get_dbl_param
+ sci_sym_set_str_param
+ sci_sym_get_str_param
+ sci_sym_setColSoln
+ sci_sym_setObjCoeff
+ sci_sym_setObjSense
+ sci_sym_getVarSoln
+ sci_sym_getObjVal
+ sci_sym_solve
+ sci_sym_setVarBound
+ sci_sym_isContinuous
+ sci_sym_isBinary
+ sci_sym_isInteger
diff --git a/sci_gateway/cpp/FOSSEE_Optimization_Toolbox.dll b/sci_gateway/cpp/FOSSEE_Optimization_Toolbox.dll Binary files differnew file mode 100644 index 0000000..9cc560c --- /dev/null +++ b/sci_gateway/cpp/FOSSEE_Optimization_Toolbox.dll diff --git a/sci_gateway/cpp/FOSSEE_Optimization_Toolbox.exp b/sci_gateway/cpp/FOSSEE_Optimization_Toolbox.exp Binary files differnew file mode 100644 index 0000000..5b951f0 --- /dev/null +++ b/sci_gateway/cpp/FOSSEE_Optimization_Toolbox.exp diff --git a/sci_gateway/cpp/FOSSEE_Optimization_Toolbox.lib b/sci_gateway/cpp/FOSSEE_Optimization_Toolbox.lib Binary files differnew file mode 100644 index 0000000..9675651 --- /dev/null +++ b/sci_gateway/cpp/FOSSEE_Optimization_Toolbox.lib diff --git a/sci_gateway/cpp/Makelib.mak b/sci_gateway/cpp/Makelib.mak index 03a0fe6..e8dc6b1 100644 --- a/sci_gateway/cpp/Makelib.mak +++ b/sci_gateway/cpp/Makelib.mak @@ -60,11 +60,11 @@ OTHERLIBS = # ------------------------------------------------------
#CC =
# ------------------------------------------------------
-CFLAGS = $(CC_OPTIONS) -D__SCILAB_TOOLBOX__ -DFORDLL -D__USE_DEPRECATED_STACK_FUNCTIONS__ -w -I C:\Users\FOSSEE\Desktop\build\FOSSEE-Optimization-Toolbox-src\sci_gateway\cpp\ -I C:\Users\FOSSEE\Desktop\build\FOSSEE-Optimization-Toolbox-src\sci_gateway\cpp\\..\..\thirdparty\windows\include\coin
+CFLAGS = $(CC_OPTIONS) -D__SCILAB_TOOLBOX__ -DFORDLL -D__USE_DEPRECATED_STACK_FUNCTIONS__ -w -I C:\Users\FOSSEE\Downloads\FOSSEE-Optimization-toolbox-master\FOSSEE-Optimization-toolbox-master\sci_gateway\cpp\ -I C:\Users\FOSSEE\Downloads\FOSSEE-Optimization-toolbox-master\FOSSEE-Optimization-toolbox-master\sci_gateway\cpp\\..\..\thirdparty\windows\include\coin
# ------------------------------------------------------
FFLAGS = $(FC_OPTIONS) -DFORDLL
# ------------------------------------------------------
-EXTRA_LDFLAGS = C:\Users\FOSSEE\Desktop\build\FOSSEE-Optimization-Toolbox-src\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\libClp.lib C:\Users\FOSSEE\Desktop\build\FOSSEE-Optimization-Toolbox-src\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\libCgl.lib C:\Users\FOSSEE\Desktop\build\FOSSEE-Optimization-Toolbox-src\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\libOsi.lib C:\Users\FOSSEE\Desktop\build\FOSSEE-Optimization-Toolbox-src\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\libOsiClp.lib C:\Users\FOSSEE\Desktop\build\FOSSEE-Optimization-Toolbox-src\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\libCoinUtils.lib C:\Users\FOSSEE\Desktop\build\FOSSEE-Optimization-Toolbox-src\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\libSymphony.lib C:\Users\FOSSEE\Desktop\build\FOSSEE-Optimization-Toolbox-src\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\IpOptFSS.lib C:\Users\FOSSEE\Desktop\build\FOSSEE-Optimization-Toolbox-src\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\IpOpt-vc10.lib
+EXTRA_LDFLAGS = C:\Users\FOSSEE\Downloads\FOSSEE-Optimization-toolbox-master\FOSSEE-Optimization-toolbox-master\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\libClp.lib C:\Users\FOSSEE\Downloads\FOSSEE-Optimization-toolbox-master\FOSSEE-Optimization-toolbox-master\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\libCgl.lib C:\Users\FOSSEE\Downloads\FOSSEE-Optimization-toolbox-master\FOSSEE-Optimization-toolbox-master\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\libOsi.lib C:\Users\FOSSEE\Downloads\FOSSEE-Optimization-toolbox-master\FOSSEE-Optimization-toolbox-master\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\libOsiClp.lib C:\Users\FOSSEE\Downloads\FOSSEE-Optimization-toolbox-master\FOSSEE-Optimization-toolbox-master\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\libCoinUtils.lib C:\Users\FOSSEE\Downloads\FOSSEE-Optimization-toolbox-master\FOSSEE-Optimization-toolbox-master\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\libSymphony.lib C:\Users\FOSSEE\Downloads\FOSSEE-Optimization-toolbox-master\FOSSEE-Optimization-toolbox-master\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\IpOptFSS.lib C:\Users\FOSSEE\Downloads\FOSSEE-Optimization-toolbox-master\FOSSEE-Optimization-toolbox-master\sci_gateway\cpp\\..\..\thirdparty\windows\lib\x86\IpOpt-vc10.lib
# ------------------------------------------------------
!include $(SCIDIR)\modules\dynamic_link\src\scripts\Makedll.incl
# ------------------------------------------------------
diff --git a/sci_gateway/cpp/QuadTMINLP.hpp b/sci_gateway/cpp/QuadTMINLP.hpp new file mode 100644 index 0000000..84704be --- /dev/null +++ b/sci_gateway/cpp/QuadTMINLP.hpp @@ -0,0 +1,134 @@ +// Copyright (C) 2016 - IIT Bombay - FOSSEE +// +// This file must be used under the terms of the CeCILL. +// This source file is licensed as described in the file COPYING, which +// you should have received as part of this distribution. The terms +// are also available at +// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt +// Author: Harpreet Singh, Pranav Deshpande and Akshay Miterani +// Organization: FOSSEE, IIT Bombay +// Email: toolbox@scilab.in + +#ifndef QuadTMINLP_HPP +#define QuadTMINLP_HPP + +#include "BonTMINLP.hpp" +#include "IpTNLP.hpp" + +using namespace Ipopt; +using namespace Bonmin; + +class QuadTMINLP : public TMINLP +{ + private: + Index numVars_; // Number of variables. + + Index numCons_; // Number of constraints. + + Index intconSize_; // Number of integer constraints + + const Number *qMatrix_ = NULL; //qMatrix_ is a pointer to matrix of size numVars X numVars_ + // with coefficents of quadratic terms in objective function. + + const Number *lMatrix_ = NULL;//lMatrix_ is a pointer to matrix of size 1*numVars_ + // with coefficents of linear terms in objective function. + + const Number *intcon_ = NULL; // The matrix containing the integer constraints + + const Number *conMatrix_ = NULL;//conMatrix_ is a pointer to matrix of size numCons X numVars + // with coefficients of terms in a each objective in each row. + + const Number *conUB_= NULL; //conUB_ is a pointer to a matrix of size of 1*numCons_ + // with upper bounds of all constraints. + + const Number *conLB_ = NULL; //conLB_ is a pointer to a matrix of size of 1*numConsn_ + // with lower bounds of all constraints. + + 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. + + const Number *varGuess_= NULL; //varGuess_ is a pointer to a matrix of size of 1*numVar_ + // with initial guess of all variables. + + Number *finalX_= NULL; //finalX_ is a pointer to a matrix of size of 1*numVar_ + // with final value for the primal 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 finalObjVal_; //finalObjVal_ is a scalar with the final value of the objective. + + int status_; //Solver return status + +public: + // Constructor + QuadTMINLP(Index nV, Index nC, Index intconSize,Number *qM, Number *lM, Number *intcon,Number *cM, Number *cLB, Number *cUB, Number *vLB, Number *vUB,Number *vG): + numVars_(nV),numCons_(nC),intconSize_(intconSize),qMatrix_(qM),lMatrix_(lM),intcon_(intcon),conMatrix_(cM),conLB_(cLB),conUB_(cUB),varLB_(vLB),varUB_(vUB),varGuess_(vG), finalObjVal_(0){ } + + // virtual destructor. + virtual ~QuadTMINLP(){} + + /* Copy constructor.*/ + QuadTMINLP(const QuadTMINLP &other){} + + // Go to http://coin-or.org/Bonmin for the details of the below methods + + virtual bool get_variables_types(Index n, VariableType* var_types); + + virtual bool get_variables_linearity(Index n, Ipopt::TNLP::LinearityType* var_types); + + virtual bool get_constraints_linearity(Index m, Ipopt::TNLP::LinearityType* const_types); + + + virtual bool get_nlp_info(Index& n, Index&m, Index& nnz_jac_g, + Index& nnz_h_lag, TNLP::IndexStyleEnum& index_style); + + virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u, + Index m, Number* g_l, Number* g_u); + + 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); + + virtual bool eval_f(Index n, const Number* x, bool new_x, Number& obj_value); + + virtual bool eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f); + + virtual bool eval_g(Index n, const Number* x, bool new_x, Index m, Number* g); + + virtual bool eval_jac_g(Index n, const Number* x, bool new_x, + Index m, Index nele_jac, Index* iRow, Index *jCol, + Number* values); + + 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); + + virtual void QuadTMINLP::finalize_solution(TMINLP::SolverReturn status, + Index n, const Number* x,Number obj_value); + + virtual const SosInfo * sosConstraints() const{return NULL;} + virtual const BranchingInfo* branchingInfo() const{return NULL;} + + // Bonmin methods end here + + virtual const double * getX(); //Returns a pointer to a matrix of size of 1*numVar + // with final value for the primal variables. + + virtual double getObjVal(); //Returns the output of the final value of the objective. + + virtual int returnStatus(); //Returns the status count +}; + +#endif diff --git a/sci_gateway/cpp/Release/FOSSEE_Optimization_Toolbox.obj b/sci_gateway/cpp/Release/FOSSEE_Optimization_Toolbox.obj Binary files differnew file mode 100644 index 0000000..9fa3085 --- /dev/null +++ b/sci_gateway/cpp/Release/FOSSEE_Optimization_Toolbox.obj diff --git a/sci_gateway/cpp/Release/globals.obj b/sci_gateway/cpp/Release/globals.obj Binary files differnew file mode 100644 index 0000000..6f52656 --- /dev/null +++ b/sci_gateway/cpp/Release/globals.obj diff --git a/sci_gateway/cpp/Release/read_mps.obj b/sci_gateway/cpp/Release/read_mps.obj Binary files differnew file mode 100644 index 0000000..f80670c --- /dev/null +++ b/sci_gateway/cpp/Release/read_mps.obj diff --git a/sci_gateway/cpp/Release/sci_LinProg.obj b/sci_gateway/cpp/Release/sci_LinProg.obj Binary files differnew file mode 100644 index 0000000..b64676d --- /dev/null +++ b/sci_gateway/cpp/Release/sci_LinProg.obj diff --git a/sci_gateway/cpp/Release/sci_QuadNLP.obj b/sci_gateway/cpp/Release/sci_QuadNLP.obj Binary files differnew file mode 100644 index 0000000..baa4a85 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_QuadNLP.obj diff --git a/sci_gateway/cpp/Release/sci_iofunc.obj b/sci_gateway/cpp/Release/sci_iofunc.obj Binary files differnew file mode 100644 index 0000000..434e2b1 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_iofunc.obj diff --git a/sci_gateway/cpp/Release/sci_ipopt.obj b/sci_gateway/cpp/Release/sci_ipopt.obj Binary files differnew file mode 100644 index 0000000..8de41c5 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_ipopt.obj diff --git a/sci_gateway/cpp/Release/sci_ipoptfminbnd.obj b/sci_gateway/cpp/Release/sci_ipoptfminbnd.obj Binary files differnew file mode 100644 index 0000000..7c1cb3c --- /dev/null +++ b/sci_gateway/cpp/Release/sci_ipoptfminbnd.obj diff --git a/sci_gateway/cpp/Release/sci_ipoptfmincon.obj b/sci_gateway/cpp/Release/sci_ipoptfmincon.obj Binary files differnew file mode 100644 index 0000000..eca2fb8 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_ipoptfmincon.obj diff --git a/sci_gateway/cpp/Release/sci_ipoptfminunc.obj b/sci_gateway/cpp/Release/sci_ipoptfminunc.obj Binary files differnew file mode 100644 index 0000000..e8a4da8 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_ipoptfminunc.obj diff --git a/sci_gateway/cpp/Release/sci_minbndNLP.obj b/sci_gateway/cpp/Release/sci_minbndNLP.obj Binary files differnew file mode 100644 index 0000000..228d08a --- /dev/null +++ b/sci_gateway/cpp/Release/sci_minbndNLP.obj diff --git a/sci_gateway/cpp/Release/sci_minconNLP.obj b/sci_gateway/cpp/Release/sci_minconNLP.obj Binary files differnew file mode 100644 index 0000000..53ebb8f --- /dev/null +++ b/sci_gateway/cpp/Release/sci_minconNLP.obj diff --git a/sci_gateway/cpp/Release/sci_minuncNLP.obj b/sci_gateway/cpp/Release/sci_minuncNLP.obj Binary files differnew file mode 100644 index 0000000..b6bd938 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_minuncNLP.obj diff --git a/sci_gateway/cpp/Release/sci_solver_status_query_functions.obj b/sci_gateway/cpp/Release/sci_solver_status_query_functions.obj Binary files differnew file mode 100644 index 0000000..c1d5e70 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_solver_status_query_functions.obj diff --git a/sci_gateway/cpp/Release/sci_sym_addrowcol.obj b/sci_gateway/cpp/Release/sci_sym_addrowcol.obj Binary files differnew file mode 100644 index 0000000..edbfa1b --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_addrowcol.obj diff --git a/sci_gateway/cpp/Release/sci_sym_get_dbl_arr.obj b/sci_gateway/cpp/Release/sci_sym_get_dbl_arr.obj Binary files differnew file mode 100644 index 0000000..bec62f6 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_get_dbl_arr.obj diff --git a/sci_gateway/cpp/Release/sci_sym_get_iteration_count.obj b/sci_gateway/cpp/Release/sci_sym_get_iteration_count.obj Binary files differnew file mode 100644 index 0000000..dbd5c70 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_get_iteration_count.obj diff --git a/sci_gateway/cpp/Release/sci_sym_get_matrix.obj b/sci_gateway/cpp/Release/sci_sym_get_matrix.obj Binary files differnew file mode 100644 index 0000000..bfc94b6 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_get_matrix.obj diff --git a/sci_gateway/cpp/Release/sci_sym_get_num_int.obj b/sci_gateway/cpp/Release/sci_sym_get_num_int.obj Binary files differnew file mode 100644 index 0000000..43907b2 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_get_num_int.obj diff --git a/sci_gateway/cpp/Release/sci_sym_getinfinity.obj b/sci_gateway/cpp/Release/sci_sym_getinfinity.obj Binary files differnew file mode 100644 index 0000000..9c49b54 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_getinfinity.obj diff --git a/sci_gateway/cpp/Release/sci_sym_getobjsense.obj b/sci_gateway/cpp/Release/sci_sym_getobjsense.obj Binary files differnew file mode 100644 index 0000000..754b975 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_getobjsense.obj diff --git a/sci_gateway/cpp/Release/sci_sym_getrowact.obj b/sci_gateway/cpp/Release/sci_sym_getrowact.obj Binary files differnew file mode 100644 index 0000000..2f2d242 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_getrowact.obj diff --git a/sci_gateway/cpp/Release/sci_sym_isenvactive.obj b/sci_gateway/cpp/Release/sci_sym_isenvactive.obj Binary files differnew file mode 100644 index 0000000..47ce6ac --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_isenvactive.obj diff --git a/sci_gateway/cpp/Release/sci_sym_load_mps.obj b/sci_gateway/cpp/Release/sci_sym_load_mps.obj Binary files differnew file mode 100644 index 0000000..19f6d02 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_load_mps.obj diff --git a/sci_gateway/cpp/Release/sci_sym_loadproblem.obj b/sci_gateway/cpp/Release/sci_sym_loadproblem.obj Binary files differnew file mode 100644 index 0000000..78bcb59 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_loadproblem.obj diff --git a/sci_gateway/cpp/Release/sci_sym_openclose.obj b/sci_gateway/cpp/Release/sci_sym_openclose.obj Binary files differnew file mode 100644 index 0000000..dc875b9 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_openclose.obj diff --git a/sci_gateway/cpp/Release/sci_sym_primalbound.obj b/sci_gateway/cpp/Release/sci_sym_primalbound.obj Binary files differnew file mode 100644 index 0000000..bd1ee94 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_primalbound.obj diff --git a/sci_gateway/cpp/Release/sci_sym_remove.obj b/sci_gateway/cpp/Release/sci_sym_remove.obj Binary files differnew file mode 100644 index 0000000..03248f2 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_remove.obj diff --git a/sci_gateway/cpp/Release/sci_sym_rowmod.obj b/sci_gateway/cpp/Release/sci_sym_rowmod.obj Binary files differnew file mode 100644 index 0000000..d25eec9 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_rowmod.obj diff --git a/sci_gateway/cpp/Release/sci_sym_set_indices.obj b/sci_gateway/cpp/Release/sci_sym_set_indices.obj Binary files differnew file mode 100644 index 0000000..4b819c3 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_set_indices.obj diff --git a/sci_gateway/cpp/Release/sci_sym_set_variables.obj b/sci_gateway/cpp/Release/sci_sym_set_variables.obj Binary files differnew file mode 100644 index 0000000..7cc9733 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_set_variables.obj diff --git a/sci_gateway/cpp/Release/sci_sym_setcolsoln.obj b/sci_gateway/cpp/Release/sci_sym_setcolsoln.obj Binary files differnew file mode 100644 index 0000000..b39f76b --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_setcolsoln.obj diff --git a/sci_gateway/cpp/Release/sci_sym_setobj.obj b/sci_gateway/cpp/Release/sci_sym_setobj.obj Binary files differnew file mode 100644 index 0000000..815dda6 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_setobj.obj diff --git a/sci_gateway/cpp/Release/sci_sym_solution.obj b/sci_gateway/cpp/Release/sci_sym_solution.obj Binary files differnew file mode 100644 index 0000000..8f56e27 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_solution.obj diff --git a/sci_gateway/cpp/Release/sci_sym_solve.obj b/sci_gateway/cpp/Release/sci_sym_solve.obj Binary files differnew file mode 100644 index 0000000..38c6e57 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_solve.obj diff --git a/sci_gateway/cpp/Release/sci_sym_varbounds.obj b/sci_gateway/cpp/Release/sci_sym_varbounds.obj Binary files differnew file mode 100644 index 0000000..14fb728 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_sym_varbounds.obj diff --git a/sci_gateway/cpp/Release/sci_vartype.obj b/sci_gateway/cpp/Release/sci_vartype.obj Binary files differnew file mode 100644 index 0000000..a3dd597 --- /dev/null +++ b/sci_gateway/cpp/Release/sci_vartype.obj diff --git a/sci_gateway/cpp/builder_gateway_cpp.sce b/sci_gateway/cpp/builder_gateway_cpp.sce index f0890d9..ffc08b9 100644 --- a/sci_gateway/cpp/builder_gateway_cpp.sce +++ b/sci_gateway/cpp/builder_gateway_cpp.sce @@ -18,6 +18,7 @@ Build_64Bits = %t; path_builder = get_absolute_file_path('builder_gateway_cpp.sce'); +if getos()=="Windows" then //Name of All the Functions Function_Names = [ //for opening/closing environment and checking if it is open/close @@ -110,6 +111,7 @@ Function_Names = [ "solveminuncp","sci_solveminuncp" "solveminbndp","sci_solveminbndp" "solveminconp","sci_solveminconp" + ]; //Name of all the files to be compiled @@ -153,6 +155,169 @@ Files = [ "sci_LinProg.cpp", "read_mps.cpp" ] +else +//Name of All the Functions +Function_Names = [ + //for opening/closing environment and checking if it is open/close + "sym_open","sci_sym_open"; + "sym_close","sci_sym_close"; + "sym_isEnvActive","sci_sym_isEnvActive"; + + //run time parameters + "sym_resetParams","sci_sym_set_defaults"; + "sym_setIntParam","sci_sym_set_int_param"; + "sym_getIntParam","sci_sym_get_int_param"; + "sym_setDblParam","sci_sym_set_dbl_param"; + "sym_getDblParam","sci_sym_get_dbl_param"; + "sym_setStrParam","sci_sym_set_str_param"; + "sym_getStrParam","sci_sym_get_str_param"; + "sym_getInfinity","sci_sym_getInfinity"; + + //problem loaders + "sym_loadProblemBasic","sci_sym_loadProblemBasic"; + "sym_loadProblem","sci_sym_loadProblem"; + "sym_loadMPS","sci_sym_load_mps"; + + //basic data + "sym_getNumConstr","sci_sym_get_num_int"; + "sym_getNumVar","sci_sym_get_num_int"; + "sym_getNumElements","sci_sym_get_num_int"; + + //variable and objective data + "sym_isContinuous","sci_sym_isContinuous"; + "sym_isBinary","sci_sym_isBinary"; + "sym_isInteger","sci_sym_isInteger"; + "sym_setContinuous","sci_sym_set_continuous"; + "sym_setInteger","sci_sym_set_integer"; + "sym_getVarLower","sci_sym_get_dbl_arr"; + "sym_getVarUpper","sci_sym_get_dbl_arr"; + "sym_setVarLower","sci_sym_setVarBound"; + "sym_setVarUpper","sci_sym_setVarBound"; + "sym_getObjCoeff","sci_sym_get_dbl_arr"; + "sym_setObjCoeff","sci_sym_setObjCoeff"; + "sym_getObjSense","sci_sym_getObjSense"; + "sym_setObjSense","sci_sym_setObjSense"; + + //constraint data + "sym_getRhs","sci_sym_get_dbl_arr"; + "sym_getConstrRange","sci_sym_get_dbl_arr"; + "sym_getConstrLower","sci_sym_get_dbl_arr"; + "sym_getConstrUpper","sci_sym_get_dbl_arr"; + "sym_setConstrLower","sci_sym_setConstrBound"; + "sym_setConstrUpper","sci_sym_setConstrBound"; + "sym_setConstrType","sci_sym_setConstrType"; + "sym_getMatrix","sci_sym_get_matrix"; + + //add/remove variables and constraints + "sym_addConstr","sci_sym_addConstr"; + "sym_addVar","sci_sym_addVar"; + "sym_deleteVars","sci_sym_delete_cols"; + "sym_deleteConstrs","sci_sym_delete_rows"; + + //primal bound + "sym_getPrimalBound","sci_sym_getPrimalBound"; + "sym_setPrimalBound","sci_sym_setPrimalBound"; + + //set preliminary solution + "sym_setVarSoln","sci_sym_setColSoln"; + + //solve + "sym_solve","sci_sym_solve"; + + //post solve functions + "sym_getStatus","sci_sym_get_status"; + "sym_isOptimal","sci_sym_get_solver_status"; + "sym_isInfeasible","sci_sym_get_solver_status"; + "sym_isAbandoned","sci_sym_get_solver_status"; + "sym_isIterLimitReached","sci_sym_get_solver_status"; + "sym_isTimeLimitReached","sci_sym_get_solver_status"; + "sym_isTargetGapAchieved","sci_sym_get_solver_status"; + "sym_getVarSoln","sci_sym_getVarSoln"; + "sym_getObjVal","sci_sym_getObjVal"; + "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" + + //Integer programming functions + 'inter_fminunc', 'cpp_intfminunc'; + 'inter_fminbnd', 'cpp_intfminbnd'; + 'inter_fmincon', 'cpp_intfmincon'; + 'sci_intqpipopt', 'cpp_intqpipopt'; + 'sci_matrix_intlinprog', 'matrix_cppintlinprog'; + 'sci_mps_intlinprog','mps_cppintlinprog'; + + //ecos function + "solveecos","sci_ecos" + ]; + +//Name of all the files to be compiled +Files = [ + "globals.cpp", + "sci_iofunc.cpp", + "sci_sym_openclose.cpp", + "sci_solver_status_query_functions.cpp", + "sci_sym_solve.cpp", + "sci_sym_loadproblem.cpp", + "sci_sym_isenvactive.cpp", + "sci_sym_load_mps.cpp", + "sci_vartype.cpp", + "sci_sym_getinfinity.cpp", + "sci_sym_solution.cpp", + "sci_sym_get_dbl_arr.cpp", + "sci_sym_get_iteration_count.cpp", + "sci_sym_get_matrix.cpp", + "sci_sym_get_num_int.cpp", + "sci_sym_set_variables.cpp", + "sci_sym_setobj.cpp", + "sci_sym_varbounds.cpp", + "sci_sym_rowmod.cpp", + "sci_sym_set_indices.cpp", + "sci_sym_addrowcol.cpp", + "sci_sym_primalbound.cpp", + "sci_sym_setcolsoln.cpp", + "sci_sym_getrowact.cpp", + "sci_sym_getobjsense.cpp", + "sci_sym_remove.cpp", + "sci_QuadNLP.cpp", + "sci_ipopt.cpp", + "sci_QuadNLP.cpp", + "sci_ipopt.cpp", + "sci_minuncNLP.cpp", + "sci_ipoptfminunc.cpp", + "sci_minbndNLP.cpp", + "sci_ipoptfminbnd.cpp", + "sci_minconNLP.cpp", + "sci_ipoptfmincon.cpp", + "sci_LinProg.cpp", + "read_mps.cpp" + + 'sci_minuncTMINLP.cpp', + 'cpp_intfminunc.cpp', + 'sci_minbndTMINLP.cpp', + 'cpp_intfminbnd.cpp', + 'sci_minconTMINLP.cpp', + 'cpp_intfmincon.cpp', + 'cbcintlinprog_matrixcpp.cpp', + 'sci_QuadTMINLP.cpp', + 'cpp_intqpipopt.cpp', + 'cbcintlinprog_mpscpp.cpp' + + 'ecos.cpp' + ] + +end + [a, opt] = getversion(); Version = opt(2); diff --git a/sci_gateway/cpp/cbcintlinprog_matrixcpp.cpp b/sci_gateway/cpp/cbcintlinprog_matrixcpp.cpp new file mode 100644 index 0000000..a73abba --- /dev/null +++ b/sci_gateway/cpp/cbcintlinprog_matrixcpp.cpp @@ -0,0 +1,225 @@ +// MILP with CBC library, Matrix +// Code Authors: Akshay Miterani and Pranav Deshpande + +#include <sci_iofunc.hpp> + +// For Branch and bound +#include "OsiSolverInterface.hpp" +#include "CbcModel.hpp" +#include "CbcCutGenerator.hpp" +#include "CbcHeuristicLocal.hpp" +#include "OsiClpSolverInterface.hpp" +extern "C"{ +#include <api_scilab.h> +#include "sciprint.h" + +int matrix_cppintlinprog(){ + + //Objective function + double* obj; + //Constraint matrix coefficients + double* conMatrix; + //intcon Matrix + double* intcon; + //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; + int numintcons=0; + double valobjsense; + + CheckInputArgument(pvApiCtx , 11 , 11); //Checking the input arguments + CheckOutputArgument(pvApiCtx , 8, 8); //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; + } + + //intcon matrix + if (getDoubleMatrixFromScilab(4,&numintcons,&temp2,&intcon)) + { + return 1; + } + + if (nCons!=0) + { + //conMatrix matrix from scilab + temp1 = nCons; + temp2 = nVars; + + if (getFixedSizeDoubleMatrixFromScilab(5,temp1,temp2,&conMatrix)) + { + return 1; + } + + //conLB matrix from scilab + temp1 = nCons; + temp2 = 1; + if (getFixedSizeDoubleMatrixFromScilab(6,temp1,temp2,&conlb)) + { + return 1; + } + + //conUB matrix from scilab + if (getFixedSizeDoubleMatrixFromScilab(7,temp1,temp2,&conub)) + { + return 1; + } + + } + + //lb matrix from scilab + temp1 = 1; + temp2 = nVars; + if (getFixedSizeDoubleMatrixFromScilab(8,temp1,temp2,&lb)) + { + return 1; + } + + + //ub matrix from scilab + if (getFixedSizeDoubleMatrixFromScilab(9,temp1,temp2,&ub)) + { + return 1; + } + + //Object Sense + if(getDoubleFromScilab(10,&valobjsense)) + { + return 1; + } + + //get options from scilab + if(getFixedSizeDoubleMatrixFromScilab(11 , 1 , 4 , &options)) + { + return 1; + } + + //------------Temporary Version to make coin packed matrix------ + OsiClpSolverInterface solver1; + + CoinPackedMatrix *matrix = new CoinPackedMatrix(false , 0 , 0); + matrix->setDimensions(0 , nVars); + for(int i=0 ; i<nCons ; i++) + { + CoinPackedVector row; + for(int j=0 ; j<nVars ; j++) + { + row.insert(j, conMatrix[i+j*nCons]); + } + matrix->appendRow(row); + } + + + solver1.loadProblem(*matrix, lb, ub, obj, conlb, conub); + + for(int i=0;i<numintcons;i++) + solver1.setInteger(intcon[i]-1); + + solver1.setObjSense(valobjsense); + + //------------------------------------------------------------- + + CbcModel model(solver1); + + model.solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry); + + if((int)options[0]!=0) + model.setIntegerTolerance(options[0]); + if((int)options[1]!=0) + model.setMaximumNodes((int)options[1]); + if((int)options[2]!=0) + model.setMaximumSeconds(options[2]); + if((int)options[3]!=0) + model.setAllowableGap(options[3]); + + model.branchAndBound(); + + double *val = model.getColSolution(); + + //Output the solution to Scilab + + //get solution for x + double* xValue = model.getColSolution(); + + //get objective value + double objValue = model.getObjValue(); + + //Output status + double status_=-1; + if(model.isProvenOptimal()){ + status_=0; + } + else if(model.isProvenInfeasible()){ + status_=1; + } + else if(model.isSolutionLimitReached()){ + status_=2; + } + else if(model. isNodeLimitReached()){ + status_=3; + } + else if(model.isAbandoned()){ + status_=4; + } + else if(model.isSecondsLimitReached()){ + status_=5; + } + else if(model.isContinuousUnbounded()){ + status_=6; + } + else if(model.isProvenDualInfeasible()){ + status_=7; + } + double nodeCount=model.getNodeCount(); + double nfps=model.numberIntegers(); + double U=model.getObjValue(); + double L=model.getBestPossibleObjValue(); + double iterCount=model.getIterationCount(); + + returnDoubleMatrixToScilab(1 , nVars, 1 , xValue); + returnDoubleMatrixToScilab(2 , 1 , 1 , &objValue); + returnDoubleMatrixToScilab(3 , 1 , 1 , &status_); + returnDoubleMatrixToScilab(4 , 1 , 1 , &nodeCount); + returnDoubleMatrixToScilab(5 , 1 , 1 , &nfps); + returnDoubleMatrixToScilab(6 , 1 , 1 , &L); + returnDoubleMatrixToScilab(7 , 1 , 1 , &U); + returnDoubleMatrixToScilab(8 , 1 , 1 , &iterCount); + + //------------------------------------------------------------- + + return 0; +} +} diff --git a/sci_gateway/cpp/cbcintlinprog_mpscpp.cpp b/sci_gateway/cpp/cbcintlinprog_mpscpp.cpp new file mode 100644 index 0000000..8292ab1 --- /dev/null +++ b/sci_gateway/cpp/cbcintlinprog_mpscpp.cpp @@ -0,0 +1,115 @@ +// MILP with CBC library, mps +// Finds the solution by using CBC Library +// Code Authors: Akshay Miterani and Pranav Deshpande + +#include <sci_iofunc.hpp> + +// For Branch and bound +#include "OsiSolverInterface.hpp" +#include "CbcModel.hpp"= +#include "CbcCutGenerator.hpp" +#include "CbcHeuristicLocal.hpp" +#include "OsiClpSolverInterface.hpp" +extern "C" { +#include <api_scilab.h> + +int mps_cppintlinprog() +{ + OsiClpSolverInterface solver; + + // Path to the MPS file + char *mpsFilePath; + + // Options to set maximum iterations + double *options; + + // Input - 1 or 2 arguments allowed. + CheckInputArgument(pvApiCtx, 2, 2); + + // Get the MPS File Path from Scilab + getStringFromScilab(1, &mpsFilePath); + + // Receive the options for setting the maximum number of iterations etc. + if( getFixedSizeDoubleMatrixFromScilab(2, 1, 4, &options)) + { + return 1; + } + + // Read the MPS file + solver.readMps(mpsFilePath); + + // Cbc Library used from here + CbcModel model(solver); + + model.solver()->setHintParam(OsiDoReducePrint, true, OsiHintTry); + + if((int)options[0]!=0) + model.setIntegerTolerance(options[0]); + if((int)options[1]!=0) + model.setMaximumNodes((int)options[1]); + if((int)options[2]!=0) + model.setMaximumSeconds(options[2]); + if((int)options[3]!=0) + model.setAllowableGap(options[3]); + + model.branchAndBound(); + + int nVars = model.getNumCols(); + int nCons = model.getNumRows(); + + const double *val = model.getColSolution(); + + //Output the solution to Scilab + + //get solution for x + double* xValue = model.getColSolution(); + + //get objective value + double objValue = model.getObjValue(); + + //Output status + double status_=-1; + if(model.isProvenOptimal()){ + status_=0; + } + else if(model.isProvenInfeasible()){ + status_=1; + } + else if(model.isSolutionLimitReached()){ + status_=2; + } + else if(model. isNodeLimitReached()){ + status_=3; + } + else if(model.isAbandoned()){ + status_=4; + } + else if(model.isSecondsLimitReached()){ + status_=5; + } + else if(model.isContinuousUnbounded()){ + status_=6; + } + else if(model.isProvenDualInfeasible()){ + status_=7; + } + + double nodeCount = model.getNodeCount(); + double nfps = model.numberIntegers(); + double U = model.getObjValue(); + double L = model.getBestPossibleObjValue(); + double iterCount = model.getIterationCount(); + + returnDoubleMatrixToScilab(1 , nVars, 1 , xValue); + returnDoubleMatrixToScilab(2 , 1 , 1 , &objValue); + returnDoubleMatrixToScilab(3 , 1 , 1 , &status_); + returnDoubleMatrixToScilab(4 , 1 , 1 , &nodeCount); + returnDoubleMatrixToScilab(5 , 1 , 1 , &nfps); + returnDoubleMatrixToScilab(6 , 1 , 1 , &L); + returnDoubleMatrixToScilab(7 , 1 , 1 , &U); + returnDoubleMatrixToScilab(8 , 1 , 1 , &iterCount); + + return 0; +} + +} diff --git a/sci_gateway/cpp/cpp_intfminbnd.cpp b/sci_gateway/cpp/cpp_intfminbnd.cpp new file mode 100644 index 0000000..4914111 --- /dev/null +++ b/sci_gateway/cpp/cpp_intfminbnd.cpp @@ -0,0 +1,172 @@ +// Copyright (C) 2016 - IIT Bombay - FOSSEE +// +// This file must be used under the terms of the CeCILL. +// This source file is licensed as described in the file COPYING, which +// you should have received as part of this distribution. The terms +// are also available at +// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt +// Author: Harpreet Singh +// Organization: FOSSEE, IIT Bombay +// Email: toolbox@scilab.in + +#include "CoinPragma.hpp" +#include "CoinTime.hpp" +#include "CoinError.hpp" + +#include "BonOsiTMINLPInterface.hpp" +#include "BonIpoptSolver.hpp" +#include "minbndTMINLP.hpp" +#include "BonCbc.hpp" +#include "BonBonminSetup.hpp" + +#include "BonOACutGenerator2.hpp" +#include "BonEcpCuts.hpp" +#include "BonOaNlpOptim.hpp" + +#include "sci_iofunc.hpp" +extern "C" +{ +#include "call_scilab.h" +#include <api_scilab.h> +#include <Scierror.h> +#include <BOOL.h> +#include <localization.h> +#include <sciprint.h> + +int cpp_intfminbnd(char *fname) +{ + using namespace Ipopt; + using namespace Bonmin; + + CheckInputArgument(pvApiCtx, 8, 8); + CheckOutputArgument(pvApiCtx, 3, 3); + + // Input arguments + Number *integertolerance=NULL, *maxnodes=NULL, *allowablegap=NULL, *cputime=NULL,*max_iter=NULL, *lb = NULL, *ub = NULL; + static unsigned int nVars = 0; + unsigned int temp1 = 0,temp2 = 0, iret = 0; + int x0_rows, x0_cols,intconSize; + Number *intcon = NULL,*options=NULL, *ifval=NULL; + + // Output arguments + Number *fX = NULL, ObjVal=0,iteration=0,cpuTime=0,fobj_eval=0; + Number dual_inf, constr_viol, complementarity, kkt_error; + int rstatus = 0; + + if(getDoubleMatrixFromScilab(4, &x0_rows, &x0_cols, &lb)) + { + return 1; + } + + if(getDoubleMatrixFromScilab(5, &x0_rows, &x0_cols, &ub)) + { + return 1; + } + + // Getting intcon + if (getDoubleMatrixFromScilab(6,&intconSize,&temp2,&intcon)) + { + return 1; + } + + //Initialization of parameters + nVars=x0_rows; + temp1 = 1; + temp2 = 1; + + //Getting parameters + if (getFixedSizeDoubleMatrixInList(7,2,temp1,temp2,&integertolerance)) + { + return 1; + } + if (getFixedSizeDoubleMatrixInList(7,4,temp1,temp2,&maxnodes)) + { + return 1; + } + if (getFixedSizeDoubleMatrixInList(7,6,temp1,temp2,&cputime)) + { + return 1; + } + if (getFixedSizeDoubleMatrixInList(7,8,temp1,temp2,&allowablegap)) + { + return 1; + } + if (getFixedSizeDoubleMatrixInList(7,10,temp1,temp2,&max_iter)) + { + return 1; + } + + SmartPtr<minbndTMINLP> tminlp = new minbndTMINLP(nVars,lb,ub,intconSize,intcon); + + BonminSetup bonmin; + bonmin.initializeOptionsAndJournalist(); + + bonmin.options()->SetStringValue("mu_oracle","loqo"); + bonmin.options()->SetNumericValue("bonmin.integer_tolerance", *integertolerance); + bonmin.options()->SetIntegerValue("bonmin.node_limit", (int)*maxnodes); + bonmin.options()->SetNumericValue("bonmin.time_limit", *cputime); + bonmin.options()->SetNumericValue("bonmin.allowable_gap", *allowablegap); + bonmin.options()->SetIntegerValue("bonmin.iteration_limit", (int)*max_iter); + + //Now initialize from tminlp + bonmin.initialize(GetRawPtr(tminlp)); + + //Set up done, now let's branch and bound + try { + Bab bb; + bb(bonmin);//process parameter file using Ipopt and do branch and bound using Cbc + } + catch(TNLPSolver::UnsolvedError *E) { + Scierror(999, "\nIpopt has failed to solve the problem!\n"); + } + catch(OsiTMINLPInterface::SimpleError &E) { + Scierror(999, "\nFailed to solve a problem!\n"); + } + catch(CoinError &E) { + Scierror(999, "\nFailed to solve a problem!\n"); + } + rstatus=tminlp->returnStatus(); + + if(rstatus==0 ||rstatus== 3) + { + fX = tminlp->getX(); + ObjVal = tminlp->getObjVal(); + if (returnDoubleMatrixToScilab(1, nVars, 1, fX)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(2, 1, 1, &ObjVal)) + { + return 1; + } + + if (returnIntegerMatrixToScilab(3, 1, 1, &rstatus)) + { + return 1; + } + + } + else + { + if (returnDoubleMatrixToScilab(1, 0, 0, fX)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(2, 1, 1, &ObjVal)) + { + return 1; + } + + if (returnIntegerMatrixToScilab(3, 1, 1, &rstatus)) + { + return 1; + } + + } + + return 0; + } +} + diff --git a/sci_gateway/cpp/cpp_intfmincon.cpp b/sci_gateway/cpp/cpp_intfmincon.cpp new file mode 100644 index 0000000..d921128 --- /dev/null +++ b/sci_gateway/cpp/cpp_intfmincon.cpp @@ -0,0 +1,189 @@ +// Copyright (C) 2016 - IIT Bombay - FOSSEE +// +// This file must be used under the terms of the CeCILL. +// This source file is licensed as described in the file COPYING, which +// you should have received as part of this distribution. The terms +// are also available at +// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt +// Author: Harpreet Singh +// Organization: FOSSEE, IIT Bombay +// Email: toolbox@scilab.in + +#include "CoinPragma.hpp" +#include "CoinTime.hpp" +#include "CoinError.hpp" + +#include "BonOsiTMINLPInterface.hpp" +#include "BonIpoptSolver.hpp" +#include "minconTMINLP.hpp" +#include "BonCbc.hpp" +#include "BonBonminSetup.hpp" + +#include "BonOACutGenerator2.hpp" +#include "BonEcpCuts.hpp" +#include "BonOaNlpOptim.hpp" + +#include "sci_iofunc.hpp" +extern "C" +{ +#include "call_scilab.h" +#include <api_scilab.h> +#include <Scierror.h> +#include <BOOL.h> +#include <localization.h> +#include <sciprint.h> + +int cpp_intfmincon(char *fname) +{ + using namespace Ipopt; + using namespace Bonmin; + + CheckInputArgument(pvApiCtx, 13, 13); + CheckOutputArgument(pvApiCtx, 3, 3); + + // Input arguments + Number *integertolerance=NULL, *maxnodes=NULL, *allowablegap=NULL, *cputime=NULL,*max_iter=NULL; + Number *x0 = NULL, *lb = NULL, *ub = NULL,*conLb = NULL, *conUb = NULL,*LC = NULL; + static unsigned int nVars = 0,nCons = 0; + unsigned int temp1 = 0,temp2 = 0, iret = 0; + int x0_rows, x0_cols,intconSize; + Number *intcon = NULL,*options=NULL, *ifval=NULL; + + // Output arguments + Number *fX = NULL, ObjVal=0,iteration=0,cpuTime=0,fobj_eval=0; + Number dual_inf, constr_viol, complementarity, kkt_error; + int rstatus = 0; + + if(getDoubleMatrixFromScilab(6, &nVars, &x0_cols, &x0)) + { + return 1; + } + + if(getDoubleMatrixFromScilab(7, &x0_rows, &x0_cols, &lb)) + { + return 1; + } + + if(getDoubleMatrixFromScilab(8, &x0_rows, &x0_cols, &ub)) + { + return 1; + } + + if(getDoubleMatrixFromScilab(9, &nCons, &x0_cols, &conLb)) + { + return 1; + } + + if(getDoubleMatrixFromScilab(10, &x0_rows, &x0_cols, &conUb)) + { + return 1; + } + + // Getting intcon + if (getDoubleMatrixFromScilab(11,&intconSize,&temp2,&intcon)) + { + return 1; + } + + if (getDoubleMatrixFromScilab(13,&temp1,&temp2,&LC)) + { + return 1; + } + + //Initialization of parameters + temp1 = 1; + temp2 = 1; + + //Getting parameters + if (getFixedSizeDoubleMatrixInList(12,2,temp1,temp2,&integertolerance)) + { + return 1; + } + if (getFixedSizeDoubleMatrixInList(12,4,temp1,temp2,&maxnodes)) + { + return 1; + } + if (getFixedSizeDoubleMatrixInList(12,6,temp1,temp2,&cputime)) + { + return 1; + } + if (getFixedSizeDoubleMatrixInList(12,8,temp1,temp2,&allowablegap)) + { + return 1; + } + if (getFixedSizeDoubleMatrixInList(12,10,temp1,temp2,&max_iter)) + { + return 1; + } + + SmartPtr<minconTMINLP> tminlp = new minconTMINLP(nVars,x0,lb,ub,(unsigned int)LC,nCons,conLb,conUb,intconSize,intcon); + + BonminSetup bonmin; + bonmin.initializeOptionsAndJournalist(); + bonmin.options()->SetStringValue("mu_oracle","loqo"); + bonmin.options()->SetIntegerValue("bonmin.print_level",5); + bonmin.options()->SetNumericValue("bonmin.integer_tolerance", *integertolerance); + bonmin.options()->SetIntegerValue("bonmin.node_limit", (int)*maxnodes); + bonmin.options()->SetNumericValue("bonmin.time_limit", *cputime); + bonmin.options()->SetNumericValue("bonmin.allowable_gap", *allowablegap); + bonmin.options()->SetIntegerValue("bonmin.iteration_limit", (int)*max_iter); + + //Now initialize from tminlp + bonmin.initialize(GetRawPtr(tminlp)); + + //Set up done, now let's branch and bound + try { + Bab bb; + bb(bonmin);//process parameter file using Ipopt and do branch and bound using Cbc + } + catch(TNLPSolver::UnsolvedError *E) { + } + catch(OsiTMINLPInterface::SimpleError &E) { + } + catch(CoinError &E) { + } + rstatus=tminlp->returnStatus(); + + if(rstatus==0 ||rstatus== 3) + { + fX = tminlp->getX(); + ObjVal = tminlp->getObjVal(); + if (returnDoubleMatrixToScilab(1, nVars, 1, fX)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(2, 1, 1, &ObjVal)) + { + return 1; + } + + if (returnIntegerMatrixToScilab(3, 1, 1, &rstatus)) + { + return 1; + } + + } + else + { + if (returnDoubleMatrixToScilab(1, 0, 0, fX)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(2, 1, 1, &ObjVal)) + { + return 1; + } + + if (returnIntegerMatrixToScilab(3, 1, 1, &rstatus)) + { + return 1; + } + + } + + return 0; + } +} + diff --git a/sci_gateway/cpp/cpp_intfminunc.cpp b/sci_gateway/cpp/cpp_intfminunc.cpp new file mode 100644 index 0000000..3e1abcd --- /dev/null +++ b/sci_gateway/cpp/cpp_intfminunc.cpp @@ -0,0 +1,174 @@ +// Copyright (C) 2016 - IIT Bombay - FOSSEE +// +// This file must be used under the terms of the CeCILL. +// This source file is licensed as described in the file COPYING, which +// you should have received as part of this distribution. The terms +// are also available at +// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt +// Author: Harpreet Singh, Pranav Deshpande and Akshay Miterani +// Organization: FOSSEE, IIT Bombay +// Email: toolbox@scilab.in + +#include "CoinPragma.hpp" +#include "CoinTime.hpp" +#include "CoinError.hpp" + +#include "BonOsiTMINLPInterface.hpp" +#include "BonIpoptSolver.hpp" +#include "minuncTMINLP.hpp" +#include "BonCbc.hpp" +#include "BonBonminSetup.hpp" + +#include "BonOACutGenerator2.hpp" +#include "BonEcpCuts.hpp" +#include "BonOaNlpOptim.hpp" + +#include "sci_iofunc.hpp" +extern "C" +{ +#include "call_scilab.h" +#include <api_scilab.h> +#include <Scierror.h> +#include <BOOL.h> +#include <localization.h> +#include <sciprint.h> + +int cpp_intfminunc(char *fname) +{ + using namespace Ipopt; + using namespace Bonmin; + + CheckInputArgument(pvApiCtx, 8, 8); + CheckOutputArgument(pvApiCtx, 3, 3); // 3 output arguments + + //Function pointers, input matrix(Starting point) pointer, flag variable + int* funptr=NULL; + double* x0ptr=NULL; + + // Input arguments + Number *integertolerance=NULL, *maxnodes=NULL, *allowablegap=NULL, *cputime=NULL,*max_iter=NULL; + static unsigned int nVars = 0,nCons = 0; + unsigned int temp1 = 0,temp2 = 0, iret = 0; + int x0_rows, x0_cols; + double *intcon = NULL,*options=NULL, *ifval=NULL; + int intconSize; + + // Output arguments + double *fX = NULL, ObjVal=0,iteration=0,cpuTime=0,fobj_eval=0; + 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; + + //x0(starting point) matrix from scilab + if(getDoubleMatrixFromScilab(4, &x0_rows, &x0_cols, &x0ptr)) + { + return 1; + } + + nVars=x0_rows; + + // Getting intcon + if (getDoubleMatrixFromScilab(5,&intconSize,&temp2,&intcon)) + { + return 1; + } + + temp1 = 1; + temp2 = 1; + + //Getting parameters + if (getFixedSizeDoubleMatrixInList(6,2,temp1,temp2,&integertolerance)) + { + return 1; + } + if (getFixedSizeDoubleMatrixInList(6,4,temp1,temp2,&maxnodes)) + { + return 1; + } + if (getFixedSizeDoubleMatrixInList(6,6,temp1,temp2,&cputime)) + { + return 1; + } + if (getFixedSizeDoubleMatrixInList(6,8,temp1,temp2,&allowablegap)) + { + return 1; + } + if (getFixedSizeDoubleMatrixInList(6,10,temp1,temp2,&max_iter)) + { + return 1; + } + + SmartPtr<minuncTMINLP> tminlp = new minuncTMINLP(nVars, x0ptr, intconSize, intcon); + + BonminSetup bonmin; + bonmin.initializeOptionsAndJournalist(); + + // Here we can change the default value of some Bonmin or Ipopt option + bonmin.options()->SetStringValue("mu_oracle","loqo"); + bonmin.options()->SetNumericValue("bonmin.integer_tolerance", *integertolerance); + bonmin.options()->SetIntegerValue("bonmin.node_limit", (int)*maxnodes); + bonmin.options()->SetNumericValue("bonmin.time_limit", *cputime); + bonmin.options()->SetNumericValue("bonmin.allowable_gap", *allowablegap); + bonmin.options()->SetIntegerValue("bonmin.iteration_limit", (int)*max_iter); + + //Now initialize from tminlp + bonmin.initialize(GetRawPtr(tminlp)); + + //Set up done, now let's branch and bound + try { + Bab bb; + bb(bonmin);//process parameter file using Ipopt and do branch and bound using Cbc + } + catch(TNLPSolver::UnsolvedError *E) { + //There has been a failure to solve a problem with Ipopt. + Scierror(999, "\nIpopt has failed to solve the problem!\n"); + } + catch(OsiTMINLPInterface::SimpleError &E) { + Scierror(999, "\nFailed to solve a problem!\n"); + } + catch(CoinError &E) { + Scierror(999, "\nFailed to solve a problem!\n"); + } + rstatus=tminlp->returnStatus(); + if(rstatus==0 ||rstatus== 3) + { + fX = tminlp->getX(); + ObjVal = tminlp->getObjVal(); + if (returnDoubleMatrixToScilab(1, nVars, 1, fX)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(2, 1, 1, &ObjVal)) + { + return 1; + } + + if (returnIntegerMatrixToScilab(3, 1, 1, &rstatus)) + { + return 1; + } + + } + else + { + if (returnDoubleMatrixToScilab(1, 0, 0, fX)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(2, 1, 1, &ObjVal)) + { + return 1; + } + + if (returnIntegerMatrixToScilab(3, 1, 1, &rstatus)) + { + return 1; + } + } + + return 0; + } +} + diff --git a/sci_gateway/cpp/cpp_intqpipopt.cpp b/sci_gateway/cpp/cpp_intqpipopt.cpp new file mode 100644 index 0000000..d89d643 --- /dev/null +++ b/sci_gateway/cpp/cpp_intqpipopt.cpp @@ -0,0 +1,267 @@ +// Copyright (C) 2016 - IIT Bombay - FOSSEE +// +// This file must be used under the terms of the CeCILL. +// This source file is licensed as described in the file COPYING, which +// you should have received as part of this distribution. The terms +// are also available at +// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt +// Author: Harpreet Singh, Pranav Deshpande and Akshay Miterani +// Organization: FOSSEE, IIT Bombay +// Email: toolbox@scilab.in + +#include <iomanip> +#include <fstream> +#include <iostream> +#include "CoinPragma.hpp" +#include "CoinTime.hpp" +#include "CoinError.hpp" + +#include "BonOsiTMINLPInterface.hpp" +#include "BonIpoptSolver.hpp" +#include "QuadTMINLP.hpp" +#include "BonCbc.hpp" +#include "BonBonminSetup.hpp" + +#include "BonOACutGenerator2.hpp" +#include "BonEcpCuts.hpp" +#include "BonOaNlpOptim.hpp" + +#include "sci_iofunc.hpp" +extern "C" +{ +#include <api_scilab.h> +#include <Scierror.h> +#include <BOOL.h> +#include <localization.h> +#include <sciprint.h> + +int cpp_intqpipopt(char *fname) +{ + using namespace Ipopt; + using namespace Bonmin; + + CheckInputArgument(pvApiCtx, 15, 15); // We need total 15 input arguments. + CheckOutputArgument(pvApiCtx, 3, 3); // 3 output arguments + + // Error management variable + SciErr sciErr; + + // Input arguments + double *QItems=NULL,*PItems=NULL, *intcon = NULL, *ConItems=NULL,*conUB=NULL,*conLB=NULL; + double *varUB=NULL,*varLB=NULL,*init_guess=NULL,*options=NULL, *ifval=NULL; + static unsigned int nVars = 0,nCons = 0, intconSize = 0; + unsigned int temp1 = 0,temp2 = 0; + char *bonmin_options_file = NULL; + // Output arguments + double *fX = NULL, ObjVal = 0,iteration=0; + int rstatus = 0; + + //Number of Variables + if(getIntFromScilab(1,&nVars)) + { + return 1; + } + + //Number of Constraints + if (getIntFromScilab(2,&nCons)) + { + return 1; + } + + //Number of variables constrained to be integers + if (getIntFromScilab(3,&intconSize)) + { + return 1; + } + + //Q matrix from scilab + temp1 = nVars; + temp2 = nVars; + if (getFixedSizeDoubleMatrixFromScilab(4,temp1,temp1,&QItems)) + { + return 1; + } + + //P matrix from scilab + temp1 = 1; + temp2 = nVars; + if (getFixedSizeDoubleMatrixFromScilab(5,temp1,temp2,&PItems)) + { + return 1; + } + + temp1 = 1; + temp2 = intconSize; + // Getting intcon + if (getDoubleMatrixFromScilab(6,&temp1,&temp2,&intcon)) + { + return 1; + } + + if (nCons!=0) + { + //conMatrix matrix from scilab + temp1 = nCons; + temp2 = nVars; + + if (getFixedSizeDoubleMatrixFromScilab(7,temp1,temp2,&ConItems)) + { + return 1; + } + + //conLB matrix from scilab + temp1 = 1; + temp2 = nCons; + if (getFixedSizeDoubleMatrixFromScilab(8,temp1,temp2,&conLB)) + { + return 1; + } + + //conUB matrix from scilab + if (getFixedSizeDoubleMatrixFromScilab(9,temp1,temp2,&conUB)) + { + return 1; + } + } + + //varLB matrix from scilab + temp1 = 1; + temp2 = nVars; + if (getFixedSizeDoubleMatrixFromScilab(10,temp1,temp2,&varLB)) + { + return 1; + } + + //varUB matrix from scilab + if (getFixedSizeDoubleMatrixFromScilab(11,temp1,temp2,&varUB)) + { + return 1; + } + + //Initial Value of variables from scilab + if (getFixedSizeDoubleMatrixFromScilab( 12,temp1,temp2,&init_guess)) + { + return 1; + } + + temp1=1; + temp2=5; + if (getFixedSizeDoubleMatrixFromScilab(13,temp1,temp2,&options)) + { + return 1; + } + + temp1=1; + temp2=5; + if (getFixedSizeDoubleMatrixFromScilab(14,temp1,temp2,&ifval)) + { + return 1; + } + + if (getStringFromScilab(15, &bonmin_options_file)) + { + return 1; + } + + + + + SmartPtr<QuadTMINLP> tminlp = new QuadTMINLP(nVars,nCons,intconSize,QItems, PItems, intcon,ConItems,conLB,conUB,varLB,varUB,init_guess); + + BonminSetup bonmin; + bonmin.initializeOptionsAndJournalist(); + + // Here we can change the default value of some Bonmin or Ipopt option + bonmin.options()->SetStringValue("mu_oracle","loqo"); + + + //Register an additional option + if((int)ifval[0]) + bonmin.options()->SetNumericValue("bonmin.integer_tolerance", (options[0])); + if((int)ifval[1]) + bonmin.options()->SetIntegerValue("bonmin.node_limit", (int)(options[1])); + if((int)ifval[2]) + bonmin.options()->SetNumericValue("bonmin.time_limit", (options[2])); + if((int)ifval[3]) + bonmin.options()->SetNumericValue("bonmin.allowable_gap", (options[3])); + if((int)ifval[4]) + bonmin.options()->SetIntegerValue("bonmin.iteration_limit", (int)(options[4])); + + + //Here we read the option file + //if ( bonmin_options_file!=NULL ) + // bonmin.readOptionsFile(bonmin_options_file); + + //Now initialize from tminlp + bonmin.initialize(GetRawPtr(tminlp)); + + //Set up done, now let's branch and bound + try { + Bab bb; + bb(bonmin);//process parameter file using Ipopt and do branch and bound using Cbc + } + catch(TNLPSolver::UnsolvedError *E) { + //There has been a failure to solve a problem with Ipopt. + std::cerr<<"Ipopt has failed to solve a problem!"<<std::endl; + sciprint(999, "\nIpopt has failed to solve the problem!\n"); + } + catch(OsiTMINLPInterface::SimpleError &E) { + std::cerr<<E.className()<<"::"<<E.methodName() + <<std::endl + <<E.message()<<std::endl; + sciprint(999, "\nFailed to solve a problem!\n"); + } + catch(CoinError &E) { + std::cerr<<E.className()<<"::"<<E.methodName() + <<std::endl + <<E.message()<<std::endl; + sciprint(999, "\nFailed to solve a problem!\n"); + } + rstatus=tminlp->returnStatus(); + if (rstatus >= 0 | rstatus <= 5){ + fX = tminlp->getX(); + ObjVal = tminlp->getObjVal(); + if (returnDoubleMatrixToScilab(1, 1, nVars, fX)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(2, 1, 1, &ObjVal)) + { + return 1; + } + + if (returnIntegerMatrixToScilab(3, 1, 1, &rstatus)) + { + return 1; + } + + } + else + { + if (returnDoubleMatrixToScilab(1, 0, 0, fX)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(2, 1, 1, &ObjVal)) + { + return 1; + } + + if (returnIntegerMatrixToScilab(3, 1, 1, &rstatus)) + { + return 1; + } + + sciprint(999, "\nThe problem could not be solved!\n"); + } + + // As the SmartPtrs go out of scope, the reference count + // will be decremented and the objects will automatically + // be deleted(No memory leakage). + + return 0; +} +} + diff --git a/sci_gateway/cpp/ecos.cpp b/sci_gateway/cpp/ecos.cpp new file mode 100644 index 0000000..5dee6a4 --- /dev/null +++ b/sci_gateway/cpp/ecos.cpp @@ -0,0 +1,398 @@ +// Copyright (C) 2016 - 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: Georgey John +// Organization: FOSSEE, IIT Bombay +// Email: toolbox@scilab.in + +#include "sci_iofunc.hpp" +extern "C" +{ + #include <api_scilab.h> + #include <Scierror.h> + #include <BOOL.h> + #include <localization.h> + #include <sciprint.h> + #include <ecos.h> + #include <ecos_bb.h> + + // function to convert int to idxint + idxint * int2idxint(int * sci_int, int n) { + // int *sci_int; + idxint *sci_idxint; + sci_idxint = (idxint *) malloc(n * sizeof(idxint)); + for (int i = 0; i < n; i++) { + sci_idxint[i] = sci_int[i]; + } + return(sci_idxint); + } + + int sci_ecos(char *fname){ + + CheckInputArgument(pvApiCtx, 17, 17); + CheckOutputArgument(pvApiCtx, 5, 5); + // Error management variable + SciErr sciErr; + + double *cptr=NULL, *Gprptr=NULL, *hptr=NULL, *Aprptr=NULL, *bptr=NULL; + int *Girptr=NULL,*Gjcptr=NULL, *Airptr=NULL, *Ajcptr=NULL, *qptr=NULL, mptr=0,nptr=0,pptr=0,nconesptr=0; + static int lptr=0, eptr=0; + unsigned int temp1 = 0,temp2 = 0, iret = 0; + double *maxit=NULL,*feastol=NULL,*reltol=NULL,*abstol=NULL,*feastol_inacc=NULL,*reltol_inacc=NULL,*abstol_inacc=NULL,*verbose=NULL,*mi_max_iter=NULL,*mi_int_tol=NULL,*mi_abs_eps=NULL,*mi_rel_eps=NULL; + int c_rows=0, Gpr_rows=0, Gir_rows=0, Gjc_rows=0, h_rows=0, Apr_rows=0, Air_rows=0, Ajc_rows=0, b_rows=0, l_rows=0, q_rows=0, e_rows=0; + int c_cols=0, Gpr_cols=0, Gir_cols=0, Gjc_cols=0, h_cols=0, Apr_cols=0, Air_cols=0, Ajc_cols=0, b_cols=0, l_cols=0, q_cols=0, e_cols=0; + + idxint exitflag = ECOS_FATAL; + + + //Getting objective matrix + if(getDoubleMatrixFromScilab(1, &c_rows, &c_cols, &cptr)) + { + return 1; + } + + //Getting Gpr matrix linear inequality constraints + + if(getDoubleMatrixFromScilab(2, &Gpr_rows, &Gpr_cols, &Gprptr)) + { + return 1; + } + + //Getting Gjc integer matrix representing coloumn indices of linear inequality constraints + if(getIntMatrixFromScilab(3, &Gjc_rows, &Gjc_cols, &Gjcptr)) + { + return 1; + } + + //Getting Gir integer matrix representing row indices of linear inequality constraints + if(getIntMatrixFromScilab(4, &Gir_rows, &Gir_cols, &Girptr)) + { + return 1; + } + + //Getting matrix representing RHS of linear inequality constraints + if(getDoubleMatrixFromScilab(5, &h_rows, &h_cols, &hptr)) + { + return 1; + } + + //Getting Apr matrix representing linear equality constraints + if(getDoubleMatrixFromScilab(6, &Apr_rows, &Apr_cols, &Aprptr)) + { + return 1; + } + + //Getting Ajc integer matrix representing coloumn indices linear inequality constraints + if(getIntMatrixFromScilab(7, &Ajc_rows, &Ajc_cols, &Ajcptr)) + { + return 1; + } + + //Getting Air integer matrix representing row indices of linear inequality constraints + if(getIntMatrixFromScilab(8, &Air_rows, &Air_cols, &Airptr)) + { + return 1; + } + + //Getting matrix representing RHS of linear inequality constraints + if(getDoubleMatrixFromScilab(9, &b_rows, &b_cols, &bptr)) + { + return 1; + } + + //Getting Integer representing dimensions of positive orthant + if(getIntFromScilab(10, &lptr)) + { + return 1; + } + + //Getting integer matrix representing dimensions of each cone + if(getIntMatrixFromScilab(11, &q_rows, &q_cols, &qptr)) + { + return 1; + } + + //Getting integer representing number of exponential cone + if(getIntFromScilab(12, &eptr)) + { + return 1; + } + + //Getting matrix representing maximum iteration + if(getFixedSizeDoubleMatrixInList(13,2,temp1,temp2,&maxit)) + { + return 1; + } + + //Getting matrix representing feasible tolerance + if(getFixedSizeDoubleMatrixInList(13,4,temp1,temp2,&feastol)) + { + return 1; + } + + //Getting matrix representing relative tolerance + if(getFixedSizeDoubleMatrixInList(13,6,temp1,temp2,&reltol)) + { + return 1; + } + + //Getting matrix representing absolute tolerance + if(getFixedSizeDoubleMatrixInList(13,8,temp1,temp2,&abstol)) + { + return 1; + } + + //Getting matrix representing feastol_inacc + if(getFixedSizeDoubleMatrixInList(13,10,temp1,temp2,&feastol_inacc)) + { + return 1; + } + + //Getting matrix representing reltol_inacc + if(getFixedSizeDoubleMatrixInList(13,12,temp1,temp2,&reltol_inacc)) + { + return 1; + } + + //Getting matrix representing abstol_inacc + if(getFixedSizeDoubleMatrixInList(13,14,temp1,temp2,&abstol_inacc)) + { + return 1; + } + //Getting matrix representing verbose mode + if(getFixedSizeDoubleMatrixInList(13,16,temp1,temp2,&verbose)) + { + return 1; + } + + // /ecos_bb options + // if(getFixedSizeDoubleMatrixInList(13,18,temp1,temp2,&mi_max_iter)) + // { + // return 1; + // } + + // if(getFixedSizeDoubleMatrixInList(13,20,temp1,temp2,&mi_int_tol)) + // { + // return 1; + // } + + // if(getFixedSizeDoubleMatrixInList(13,22,temp1,temp2,&mi_rel_eps)) + // { + // return 1; + // } + + // if(getFixedSizeDoubleMatrixInList(13,24,temp1,temp2,&mi_abs_eps)) + // { + // return 1; + // } + + // //Getting integer representing number of inequality constraints + if(getIntFromScilab(14, &mptr)) + { + return 1; + } + + //Getting integer representing the number of primal variable + if(getIntFromScilab(15, &nptr)) + { + return 1; + } + + //Getting integer representing the number of equality constraints + if(getIntFromScilab(16, &pptr)) + { + return 1; + } + + //Getting integer representing the number of second order cones + if(getIntFromScilab(17, &nconesptr)) + { + return 1; + } + + // Intializing ECOS variables + pfloat *Gpr=NULL,*c=NULL,*h=NULL,*Apr=NULL,*b=NULL; + idxint *Gjc=NULL,*Gir=NULL,*Ajc=NULL,*Air=NULL, n=0, m=0, p=0, l=0, *q=NULL,ncones=0, e=0; + double *x = NULL, *y = NULL, *s = NULL, *z = NULL; + const char* infostring; + + c = (pfloat *)cptr; + + if (Gpr_rows != 0) + { + Gpr = (pfloat *) Gprptr; + Gjc = int2idxint(Gjcptr,Gjc_rows); + Gir = int2idxint(Girptr,Gir_rows); + h = (pfloat *) hptr; + } + + if (Apr_rows != 0) + { + Apr = (pfloat *)Aprptr; + Ajc = int2idxint(Ajcptr,Ajc_rows); + Air = int2idxint(Airptr,Air_rows); + b = (pfloat *)bptr; + } + + n = (idxint)nptr; + m = (idxint)mptr; + p = (idxint)pptr; + l = (idxint)lptr; + ncones = (idxint)nconesptr; + + if (ncones != 0) + { + q = int2idxint(qptr,ncones); + } + + e = (idxint)eptr; + + + pwork* input_struct; + + // for (int i = 0; i < Gpr_rows; i++) + // { + // sciprint("%f\t",Gpr[i]); + // } + // sciprint("\n"); + + // for (int i = 0; i < Gjc_rows; i++) + // { + // sciprint("%d\t",Gjc[i]); + // } + // sciprint("\n"); + + // for (int i = 0; i < Gir_rows; i++) + // { + // sciprint("%d\t",Gir[i]); + // } + // sciprint("\n"); + // sciprint("%d\t",n);sciprint("%d\t",m);sciprint("%d\t",p);sciprint("%d\t",l);sciprint("%d\t",ncones); + + // setup ecos + input_struct = ECOS_setup(n, m, p, l, ncones, q, e, Gpr, Gjc, Gir, Apr, Ajc, Air,c, h, b); + + // if setup is successful + if( input_struct != NULL ){ + // passing options to ecos + input_struct->stgs->feastol = (pfloat) *feastol; + input_struct->stgs->reltol = (pfloat) *reltol; + input_struct->stgs->abstol = (pfloat) *abstol; + input_struct->stgs->feastol_inacc = (pfloat) *feastol_inacc; + input_struct->stgs->abstol_inacc = (pfloat) *abstol_inacc; + input_struct->stgs->reltol_inacc = (pfloat) *reltol_inacc; + input_struct->stgs->maxit = (idxint) maxit; + input_struct->stgs->verbose = (idxint) verbose; + + // solve + exitflag = ECOS_solve(input_struct); + + // output + x = input_struct->x; + y = input_struct->y; + s = input_struct->s; + z = input_struct->z; + + double pcost = (double)input_struct->info->pcost; + double dcost = (double)input_struct->info->dcost; + double pres = (double)input_struct->info->pres; + double dres = (double)input_struct->info->dres; + double pinf = (double)input_struct->info->pinf; + double dinf = (double)input_struct->info->dinf; + double pinfres = (double)input_struct->info->pinfres; + double dinfres = (double)input_struct->info->dinfres; + double gap = (double)input_struct->info->gap; + double rel_gap = (double)input_struct->info->relgap; + double iter = (double)input_struct->info->iter; + + // #if PROFILING > 1 + // double tkktcreate = (double)input_struct->info->tkktcreate; + // double tkktsolve = (double)input_struct->info->tkktsolve; + // double tkktfactor = (double)input_struct->info->tfactor; + // double torder = (double)input_struct->info->torder; + // double ttranspose = (double)input_struct->info->ttranspose; + // #else + double tsetup = (double)input_struct->info->tsetup; + double tsolve = (double)input_struct->info->tsolve; + // #endif + + // #if PROFILING > 1 + // double info[19]={tsetup,tsolve,pcost,dcost,pres,dres,pinf,dinf,pinfres,dinfres,gap,rel_gap,iter,(double) exitflag,tkktcreate,tkktsolve,tfactor,torder,ttranspose}; + // int o = 19; + // #else + double info[14]={tsetup,tsolve,pcost,dcost,pres,dres,pinf,dinf,pinfres,dinfres,gap,rel_gap,iter,(double) exitflag}; + // int o = 14; + // #endif + + if (returnDoubleMatrixToScilab(1, n, 1, x)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(2, p, 1, y)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(3, 14, 1, info)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(4, m, 1, s)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(5, m, 1, z)) + { + return 1; + } + + /* clean up memory */ + ECOS_cleanup(input_struct, 0); + } + else{ + // if setup fails + exitflag =-8; + + // #if PROFILING > 1 + // double info[19]={0,0,0,0,0,0,0,0,0,0,0,0,0,(double) exitflag,0,0,0,0,0}; + // int o = 19; + // #else + double info[14]={0,0,0,0,0,0,0,0,0,0,0,0,0,(double) exitflag}; + // int o = 14; + // #endif + + if (returnDoubleMatrixToScilab(1, 0, 0, x)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(2, 0, 0, y)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(3, 14, 1, info)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(4, 0, 0, s)) + { + return 1; + } + + if (returnDoubleMatrixToScilab(5, 0, 0, z)) + { + return 1; + } + } + + } +}
\ No newline at end of file diff --git a/sci_gateway/cpp/libFOSSEE_Optimization_Toolbox.c b/sci_gateway/cpp/libFOSSEE_Optimization_Toolbox.c index 5e5ce31..86dab72 100644 --- a/sci_gateway/cpp/libFOSSEE_Optimization_Toolbox.c +++ b/sci_gateway/cpp/libFOSSEE_Optimization_Toolbox.c @@ -69,6 +69,13 @@ extern Gatefunc sci_solveqp; extern Gatefunc sci_solveminuncp; extern Gatefunc sci_solveminbndp; extern Gatefunc sci_solveminconp; +extern Gatefunc cpp_intfminunc; +extern Gatefunc cpp_intfminbnd; +extern Gatefunc cpp_intfmincon; +extern Gatefunc cpp_intqpipopt; +extern Gatefunc matrix_cppintlinprog; +extern Gatefunc mps_cppintlinprog; +extern Gatefunc sci_ecos; static GenericTable Tab[]={ {(Myinterfun)sci_gateway,sci_sym_open,"sym_open"}, {(Myinterfun)sci_gateway,sci_sym_close,"sym_close"}, @@ -133,6 +140,13 @@ static GenericTable Tab[]={ {(Myinterfun)sci_gateway,sci_solveminuncp,"solveminuncp"}, {(Myinterfun)sci_gateway,sci_solveminbndp,"solveminbndp"}, {(Myinterfun)sci_gateway,sci_solveminconp,"solveminconp"}, + {(Myinterfun)sci_gateway,cpp_intfminunc,"inter_fminunc"}, + {(Myinterfun)sci_gateway,cpp_intfminbnd,"inter_fminbnd"}, + {(Myinterfun)sci_gateway,cpp_intfmincon,"inter_fmincon"}, + {(Myinterfun)sci_gateway,cpp_intqpipopt,"sci_intqpipopt"}, + {(Myinterfun)sci_gateway,matrix_cppintlinprog,"sci_matrix_intlinprog"}, + {(Myinterfun)sci_gateway,mps_cppintlinprog,"sci_mps_intlinprog"}, + {(Myinterfun)sci_gateway,sci_ecos,"solveecos"}, }; int C2F(libFOSSEE_Optimization_Toolbox)() diff --git a/sci_gateway/cpp/libFOSSEE_Optimization_Toolbox.so b/sci_gateway/cpp/libFOSSEE_Optimization_Toolbox.so Binary files differindex 5a05bab..9a02993 100755..100644 --- a/sci_gateway/cpp/libFOSSEE_Optimization_Toolbox.so +++ b/sci_gateway/cpp/libFOSSEE_Optimization_Toolbox.so diff --git a/sci_gateway/cpp/loader.sce b/sci_gateway/cpp/loader.sce index 8b15fde..5b90703 100644 --- a/sci_gateway/cpp/loader.sce +++ b/sci_gateway/cpp/loader.sce @@ -73,6 +73,13 @@ list_functions = [ 'sym_open'; 'solveminuncp'; 'solveminbndp'; 'solveminconp'; + 'inter_fminunc'; + 'inter_fminbnd'; + 'inter_fmincon'; + 'sci_intqpipopt'; + 'sci_matrix_intlinprog'; + 'sci_mps_intlinprog'; + 'solveecos'; ]; addinter(libFOSSEE_Optimizat_path + filesep() + 'libFOSSEE_Optimization_Toolbox' + getdynlibext(), 'libFOSSEE_Optimization_Toolbox', list_functions); // remove temp. variables on stack diff --git a/sci_gateway/cpp/minbndTMINLP.hpp b/sci_gateway/cpp/minbndTMINLP.hpp new file mode 100644 index 0000000..581d5ce --- /dev/null +++ b/sci_gateway/cpp/minbndTMINLP.hpp @@ -0,0 +1,114 @@ +// Copyright (C) 2016 - IIT Bombay - FOSSEE +// +// This file must be used under the terms of the CeCILL. +// This source file is licensed as described in the file COPYING, which +// you should have received as part of this distribution. The terms +// are also available at +// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt +// Author: Harpreet Singh +// Organization: FOSSEE, IIT Bombay +// Email: toolbox@scilab.in + +#ifndef minbndTMINLP_HPP +#define minbndTMINLP_HPP + +#include "BonTMINLP.hpp" +#include "IpTNLP.hpp" +#include "call_scilab.h" + +using namespace Ipopt; +using namespace Bonmin; + +class minbndTMINLP : public TMINLP +{ + private: + + Index numVars_; //Number of input variables + + Index intconSize_; + + Number *lb_= NULL; //lb_ is a pointer to a matrix of size of 1*numVars_ with lower bound of all variables. + + Number *ub_= NULL; //ub_ is a pointer to a matrix of size of 1*numVars_ with upper bound 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 finalObjVal_; //finalObjVal_ is a scalar with the final value of the objective. + + Number *intcon_ = NULL; + + int status_; //Solver return status + minbndTMINLP(const minbndTMINLP&); + minbndTMINLP& operator=(const minbndTMINLP&); + +public: + // Constructor + minbndTMINLP(Index nV, Number *lb, Number *ub, Index intconSize, Number *intcon):numVars_(nV),lb_(lb),ub_(ub),intconSize_(intconSize),intcon_(intcon),finalX_(0),finalObjVal_(1e20){ } + + /** default destructor */ + virtual ~minbndTMINLP(); + + virtual bool get_variables_types(Index n, VariableType* var_types); + + virtual bool get_variables_linearity(Index n, Ipopt::TNLP::LinearityType* var_types); + + virtual bool get_constraints_linearity(Index m, Ipopt::TNLP::LinearityType* const_types); + + /** 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, TNLP::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, Number obj_value); + + virtual const SosInfo * sosConstraints() const{return NULL;} + virtual const BranchingInfo* branchingInfo() const{return NULL;} + + 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/minconTMINLP.hpp b/sci_gateway/cpp/minconTMINLP.hpp new file mode 100644 index 0000000..5b3006a --- /dev/null +++ b/sci_gateway/cpp/minconTMINLP.hpp @@ -0,0 +1,124 @@ +// Copyright (C) 2016 - IIT Bombay - FOSSEE +// +// This file must be used under the terms of the CeCILL. +// This source file is licensed as described in the file COPYING, which +// you should have received as part of this distribution. The terms +// are also available at +// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt +// Author: Harpreet Singh, Pranav Deshpande and Akshay Miterani +// Organization: FOSSEE, IIT Bombay +// Email: toolbox@scilab.in + +#ifndef minconTMINLP_HPP +#define minconTMINLP_HPP + +#include "BonTMINLP.hpp" +#include "IpTNLP.hpp" +#include "call_scilab.h" + +using namespace Ipopt; +using namespace Bonmin; + +class minconTMINLP : public TMINLP +{ + private: + + Index numVars_; //Number of variables + + Index numCons_; //Number of constraints + + Index numLC_; //Number of Linear constraints + + Index intconSize_; + + Number *x0_= NULL; //lb_ is a pointer to a matrix of size of 1*numVars_ with lower bound of all variables. + + Number *lb_= NULL; //lb_ is a pointer to a matrix of size of 1*numVars_ with lower bound of all variables. + + Number *ub_= NULL; //ub_ is a pointer to a matrix of size of 1*numVars_ with upper bound of all variables. + + Number *conLb_= NULL; //conLb_ is a pointer to a matrix of size of numCon_*1 with lower bound of all constraints. + + Number *conUb_= NULL; //conUb_ is a pointer to a matrix of size of numCon_*1 with upper bound of all constraints. + + Number *finalX_= NULL; //finalX_ is a pointer to a matrix of size of 1*numVars_ with final value for the primal variables. + + Number finalObjVal_; //finalObjVal_ is a scalar with the final value of the objective. + + Number *intcon_ = NULL; + + int status_; //Solver return status + minconTMINLP(const minconTMINLP&); + minconTMINLP& operator=(const minconTMINLP&); + +public: + // Constructor + minconTMINLP(Index nV, Number *x0, Number *lb, Number *ub, Index nLC, Index nCons, Number *conlb, Number *conub, Index intconSize, Number *intcon):numVars_(nV),x0_(x0),lb_(lb),ub_(ub),numLC_(nLC),numCons_(nCons),conLb_(conlb),conUb_(conub),intconSize_(intconSize),intcon_(intcon),finalX_(0),finalObjVal_(1e20){ } + + /** default destructor */ + virtual ~minconTMINLP(); + + virtual bool get_variables_types(Index n, VariableType* var_types); + + virtual bool get_variables_linearity(Index n, Ipopt::TNLP::LinearityType* var_types); + + virtual bool get_constraints_linearity(Index m, Ipopt::TNLP::LinearityType* const_types); + + /** 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, TNLP::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, Number obj_value); + + virtual const SosInfo * sosConstraints() const{return NULL;} + virtual const BranchingInfo* branchingInfo() const{return NULL;} + + 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/minuncTMINLP.hpp b/sci_gateway/cpp/minuncTMINLP.hpp new file mode 100644 index 0000000..2b6e954 --- /dev/null +++ b/sci_gateway/cpp/minuncTMINLP.hpp @@ -0,0 +1,113 @@ +// Copyright (C) 2016 - IIT Bombay - FOSSEE +// +// This file must be used under the terms of the CeCILL. +// This source file is licensed as described in the file COPYING, which +// you should have received as part of this distribution. The terms +// are also available at +// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt +// Author: Harpreet Singh, Pranav Deshpande and Akshay Miterani +// Organization: FOSSEE, IIT Bombay +// Email: toolbox@scilab.in + +#define __USE_DEPRECATED_STACK_FUNCTIONS__ +#ifndef minuncTMINLP_HPP +#define minuncTMINLP_HPP + +#include "BonTMINLP.hpp" +#include "IpTNLP.hpp" +#include "call_scilab.h" + +using namespace Ipopt; +using namespace Bonmin; + +class minuncTMINLP : public TMINLP +{ + private: + + Index numVars_; //Number of input variables + + Index intconSize_; + + 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 finalObjVal_; //finalObjVal_ is a scalar with the final value of the objective. + + Number *intcon_ = NULL; + + int status_; //Solver return status + minuncTMINLP(const minuncTMINLP&); + minuncTMINLP& operator=(const minuncTMINLP&); + +public: + // Constructor + minuncTMINLP(Index nV, Number *x0, Index intconSize, Number *intcon):numVars_(nV),varGuess_(x0),intconSize_(intconSize),intcon_(intcon),finalX_(0),finalObjVal_(1e20){ } + + /** default destructor */ + virtual ~minuncTMINLP(); + + virtual bool get_variables_types(Index n, VariableType* var_types); + + virtual bool get_variables_linearity(Index n, Ipopt::TNLP::LinearityType* var_types); + + virtual bool get_constraints_linearity(Index m, Ipopt::TNLP::LinearityType* const_types); + + /** 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, TNLP::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, Number obj_value); + + virtual const SosInfo * sosConstraints() const{return NULL;} + virtual const BranchingInfo* branchingInfo() const{return NULL;} + + 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/sci_QuadTMINLP.cpp b/sci_gateway/cpp/sci_QuadTMINLP.cpp new file mode 100644 index 0000000..a424b47 --- /dev/null +++ b/sci_gateway/cpp/sci_QuadTMINLP.cpp @@ -0,0 +1,230 @@ +// Copyright (C) 2016 - IIT Bombay - FOSSEE +// +// This file must be used under the terms of the CeCILL. +// This source file is licensed as described in the file COPYING, which +// you should have received as part of this distribution. The terms +// are also available at +// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt +// Author: Harpreet Singh, Pranav Deshpande and Akshay Miterani +// Organization: FOSSEE, IIT Bombay +// Email: toolbox@scilab.in + +#include "QuadTMINLP.hpp" +#include "IpIpoptData.hpp" + +extern "C"{ +#include <sciprint.h> +} + +// Go to http://coin-or.org/Ipopt and http://coin-or.org/Bonmin for the details of the below methods + +// Set the type of every variable - CONTINUOUS or INTEGER +bool QuadTMINLP::get_variables_types(Index n, VariableType* var_types) +{ + n = numVars_; + for(int i=0; i < n; i++) + var_types[i] = CONTINUOUS; + for(int i=0 ; i < intconSize_ ; ++i) + var_types[(int)(intcon_[i]-1)] = INTEGER; + return true; +} + +// The linearity of the variables - LINEAR or NON_LINEAR +bool QuadTMINLP::get_variables_linearity(Index n, Ipopt::TNLP::LinearityType* var_types) +{ + /* + n = numVars_; + for(int i = 0; i < n; i++) + var_types[i] = Ipopt::TNLP::LINEAR; + */ + return true; +} + +// The linearity of the constraints - LINEAR or NON_LINEAR +bool QuadTMINLP::get_constraints_linearity(Index m, Ipopt::TNLP::LinearityType* const_types) +{ + m = numCons_; + for(int i = 0; i < m; i++) + const_types[i] = Ipopt::TNLP::LINEAR; + return true; +} + +// Get MINLP info such as the number of variables,constraints,no.of elements in jacobian and hessian to allocate memory +bool QuadTMINLP::get_nlp_info(Index& n, Index&m, Index& nnz_jac_g, Index& nnz_h_lag, TNLP::IndexStyleEnum& index_style) +{ + n=numVars_; // Number of variables + m=numCons_; // 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=TNLP::C_STYLE; // Index style of matrices + return true; +} + +// Get the variables and constraints bound info +bool QuadTMINLP::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;i<n;i++){ + x_l[i]=varLB_[i]; + x_u[i]=varUB_[i]; + } + + for(i=0;i<m;i++){ + g_l[i]=conLB_[i]; + g_u[i]=conUB_[i]; + } + return true; +} + +// This method sets initial values for all the required vectors. We take 0 by default. +bool QuadTMINLP::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) +{ + if (init_x == true){ //we need to set initial values for vector x + for (Index var=0;var<n;var++) + x[var]=varGuess_[var];//initialize with 0 or we can change. + } + + if (init_z == true){ //we need to provide initial values for vector bound multipliers + for (Index var=0;var<n;++var){ + z_L[var]=0.0; //initialize with 0 or we can change. + z_U[var]=0.0;//initialize with 0 or we can change. + } + } + + if (init_lambda == true){ //we need to provide initial values for lambda values. + for (Index var=0;var<m;++var){ + lambda[var]=0.0; //initialize with 0 or we can change. + } + } + + return true; +} + +// Evaluate the objective function at a point +bool QuadTMINLP::eval_f(Index n, const Number* x, bool new_x, Number& obj_value) +{ + unsigned int i,j; + obj_value=0; + for (i=0;i<n;i++){ + for (j=0;j<n;j++){ + obj_value+=0.5*x[i]*x[j]*qMatrix_[n*i+j]; + } + obj_value+=x[i]*lMatrix_[i]; + } + return true; +} + +// Get the value of gradient of objective function at vector x. +bool QuadTMINLP::eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f) +{ + unsigned int i,j; + for(i=0;i<n;i++) + { + grad_f[i]=lMatrix_[i]; + for(j=0;j<n;j++) + { + grad_f[i]+=(qMatrix_[n*i+j])*x[j]; + } + } + return true; +} + +// Get the values of constraints at vector x. +bool QuadTMINLP::eval_g(Index n, const Number* x, bool new_x, Index m, Number* g) +{ + unsigned int i,j; + for(i=0;i<m;i++) + { + g[i]=0; + for(j=0;j<n;j++) + { + g[i]+=x[j]*conMatrix_[i+j*m]; + } + } + return true; +} + +// The Jacobian Matrix +bool QuadTMINLP::eval_jac_g(Index n, const Number* x, bool new_x, + Index m, Index nnz_jac, Index* iRow, Index *jCol, + Number* values) +{ + //It asks for the structure of the jacobian. + if (values==NULL){ //Structure of jacobian (full structure) + int index=0; + for (int var=0;var<m;++var)//no. of constraints + for (int flag=0;flag<n;++flag){//no. of variables + iRow[index]=var; + jCol[index]=flag; + index++; + } + } + //It asks for values + else { + int index=0; + for (int var=0;var<m;++var) + for (int flag=0;flag<n;++flag) + values[index++]=conMatrix_[var+flag*m]; + } + return true; +} + +/* +The structure of the Hessain matrix and the values +*/ +bool QuadTMINLP::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) +{ + if (values==NULL){ + Index idx=0; + for (Index row = 0; row < n; row++) { + for (Index col = 0; col <= row; col++) { + iRow[idx] = row; + jCol[idx] = col; + idx++; + } + } + } + else { + Index index=0; + for (Index row=0;row < n;++row){ + for (Index col=0; col <= row; ++col){ + values[index++]=obj_factor*(qMatrix_[n*row+col]); + } + } + } + return true; +} + +void QuadTMINLP::finalize_solution(TMINLP::SolverReturn status, Index n, const Number* x,Number obj_value) +{ + + finalX_ = (double*)malloc(sizeof(double) * numVars_ * 1); + for (Index i=0; i<n; i++) + { + finalX_[i] = x[i]; + } + + finalObjVal_ = obj_value; + status_ = status; +} + +const double * QuadTMINLP::getX() +{ + return finalX_; +} + +double QuadTMINLP::getObjVal() +{ + return finalObjVal_; +} + +int QuadTMINLP::returnStatus() +{ + return status_; +} diff --git a/sci_gateway/cpp/sci_iofunc.cpp b/sci_gateway/cpp/sci_iofunc.cpp index 8168743..6a41aed 100644 --- a/sci_gateway/cpp/sci_iofunc.cpp +++ b/sci_gateway/cpp/sci_iofunc.cpp @@ -1,22 +1,17 @@ -// Copyright (C) 2015 - IIT Bombay - FOSSEE -// -// This file must be used under the terms of the CeCILL. -// This source file is licensed as described in the file COPYING, which -// you should have received as part of this distribution. The terms -// are also available at -// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt -// Author: Harpreet Singh and Keyur Joshi -// 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" #include "sciprint.h" #include "BOOL.h" #include <localization.h> +#include "call_scilab.h" +#include <string.h> -using namespace std; +using namespace std; int getFunctionFromScilab(int argNum, int **dest) { //data declarations @@ -43,7 +38,6 @@ int getFunctionFromScilab(int argNum, int **dest) } - int getDoubleFromScilab(int argNum, double *dest) { //data declarations @@ -240,6 +234,88 @@ int getStringFromScilab(int argNum,char **dest) } +bool getFunctionFromScilab1(int n,char name[], double *x,int posFirstElementOnStackForSF,int nOfRhsOnSF,int nOfLhsOnSF, double **dest) +{ + double check; + createMatrixOfDouble(pvApiCtx, posFirstElementOnStackForSF, 1, n, x); + C2F(scistring)(&posFirstElementOnStackForSF,name,&nOfLhsOnSF,&nOfRhsOnSF,(unsigned long)strlen(name)); + + if(getDoubleFromScilab(posFirstElementOnStackForSF+1,&check)) + { + return true; + } + if (check==1) + { + return true; + } + else + { + int x_rows, x_cols; + if(getDoubleMatrixFromScilab(posFirstElementOnStackForSF, &x_rows, &x_cols, dest)) + { + sciprint("No results "); + return true; + + } + } + return 0; +} + +bool getHessFromScilab(int n,int numConstr_,char name[], double *x,double *obj,double *lambda,int posFirstElementOnStackForSF,int nOfRhsOnSF,int nOfLhsOnSF, double **dest) +{ + double check; + createMatrixOfDouble(pvApiCtx, posFirstElementOnStackForSF, 1, n, x); + createMatrixOfDouble(pvApiCtx, posFirstElementOnStackForSF+1, 1, 1, obj); + createMatrixOfDouble(pvApiCtx, posFirstElementOnStackForSF+2, 1, numConstr_, lambda); + C2F(scistring)(&posFirstElementOnStackForSF,name,&nOfLhsOnSF,&nOfRhsOnSF,(unsigned long)strlen(name)); + + if(getDoubleFromScilab(posFirstElementOnStackForSF+1,&check)) + { + return true; + } + if (check==1) + { + return true; + } + else + { + int x_rows, x_cols; + if(getDoubleMatrixFromScilab(posFirstElementOnStackForSF, &x_rows, &x_cols, dest)) + { + sciprint("No results "); + return 1; + } + } + return 0; +} + +int getIntMatrixFromScilab(int argNum, int *rows, int *cols, int **dest) +{ + int *varAddress; + SciErr sciErr; + const char errMsg[]="Wrong type for input argument #%d: A matrix of integer is expected.\n"; + const int errNum=999; + //same steps as above + sciErr = getVarAddressFromPosition(pvApiCtx, argNum, &varAddress); + if (sciErr.iErr) + { + printError(&sciErr, 0); + return 1; + } + // if ( !isIntegerType(pvApiCtx,varAddress) || isVarComplex(pvApiCtx,varAddress) ) + // { + // Scierror(errNum,errMsg,argNum); + // return 1; + // } + getMatrixOfInteger32(pvApiCtx, varAddress, rows, cols, dest); + if (sciErr.iErr) + { + printError(&sciErr, 0); + return 1; + } + return 0; +} + int return0toScilab() { int iRet; @@ -274,7 +350,7 @@ int returnDoubleToScilab(double retVal) return 0; } -int returnDoubleMatrixToScilab(int itemPos, int rows, int cols, const double *dest) +int returnDoubleMatrixToScilab(int itemPos, int rows, int cols, double *dest) { SciErr sciErr; //same steps as above diff --git a/sci_gateway/cpp/sci_iofunc.hpp b/sci_gateway/cpp/sci_iofunc.hpp index 92f45fe..69ac07f 100644 --- a/sci_gateway/cpp/sci_iofunc.hpp +++ b/sci_gateway/cpp/sci_iofunc.hpp @@ -1,13 +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: Harpreet Singh and Keyur Joshi -// 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 @@ -21,11 +14,14 @@ int getFixedSizeDoubleMatrixFromScilab(int argNum, int rows, int cols, double ** 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); +bool getFunctionFromScilab1(int n,char name[], double *x,int posFirstElementOnStackForSF,int nOfRhsOnSF,int nOfLhsOnSF, double **dest); +bool getHessFromScilab(int n,int numConstr_,char name[], double *x,double *obj,double *lambda,int posFirstElementOnStackForSF,int nOfRhsOnSF,int nOfLhsOnSF, double **dest); +int getIntMatrixFromScilab(int argNum, int *rows, int *cols, int **dest); //output int return0toScilab(); int returnDoubleToScilab(double retVal); -int returnDoubleMatrixToScilab(int itemPos, int rows, int cols,const double *dest); +int returnDoubleMatrixToScilab(int itemPos, int rows, int cols, double *dest); int returnIntegerMatrixToScilab(int itemPos, int rows, int cols, int *dest); #endif //SCI_IOFUNCHEADER diff --git a/sci_gateway/cpp/sci_minbndTMINLP.cpp b/sci_gateway/cpp/sci_minbndTMINLP.cpp new file mode 100644 index 0000000..dae6d2e --- /dev/null +++ b/sci_gateway/cpp/sci_minbndTMINLP.cpp @@ -0,0 +1,218 @@ +// Copyright (C) 2015 - IIT Bombay - FOSSEE +// +// Author: Harpreet Singh +// Organization: FOSSEE, IIT Bombay +// Email: toolbox@scilab.in +// 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 "minbndTMINLP.hpp" +#include "sci_iofunc.hpp" + +extern "C" +{ +#include "call_scilab.h" +#include <api_scilab.h> +#include <Scierror.h> +#include <BOOL.h> +#include <localization.h> +#include <sciprint.h> +#include <string.h> +#include <assert.h> +} + +using namespace Ipopt; +using namespace Bonmin; + +minbndTMINLP::~minbndTMINLP() +{ + if(finalX_) delete[] finalX_; +} + +// Set the type of every variable - CONTINUOUS or INTEGER +bool minbndTMINLP::get_variables_types(Index n, VariableType* var_types) +{ + n = numVars_; + for(int i=0; i < n; i++) + var_types[i] = CONTINUOUS; + for(int i=0 ; i < intconSize_ ; ++i) + var_types[(int)(intcon_[i]-1)] = INTEGER; + return true; +} + +// The linearity of the variables - LINEAR or NON_LINEAR +bool minbndTMINLP::get_variables_linearity(Index n, Ipopt::TNLP::LinearityType* var_types) +{ return true; } + +// The linearity of the constraints - LINEAR or NON_LINEAR +bool minbndTMINLP::get_constraints_linearity(Index m, Ipopt::TNLP::LinearityType* const_types) +{ return true;} + +//get NLP info such as number of variables,constraints,no.of elements in jacobian and hessian to allocate memory +bool minbndTMINLP::get_nlp_info(Index& n, Index& m, Index& nnz_jac_g, Index& nnz_h_lag, TNLP::IndexStyleEnum& index_style) +{ + n=numVars_; // Number of variables + m=0; // 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=TNLP::C_STYLE; // Index style of matrices + return true; +} + +//get variable and constraint bound info +bool minbndTMINLP::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;i<n;i++) + { + x_l[i]=lb_[i]+0.0000001; + x_u[i]=ub_[i]-0.0000001; + } + + g_l=NULL; + g_u=NULL; + return true; +} + +// return the value of the constraints: g(x) +bool minbndTMINLP::eval_g(Index n, const Number* x, bool new_x, Index m, Number* g) +{ + // return the value of the constraints: g(x) + g=NULL; + return true; +} + +// return the structure or values of the jacobian +bool minbndTMINLP::eval_jac_g(Index n, const Number* x, bool new_x,Index m, Index nele_jac, Index* iRow, Index *jCol,Number* values) +{ + if (values == NULL) + { + // return the structure of the jacobian of the constraints + iRow=NULL; + jCol=NULL; + } + else + { + values=NULL; + } + return true; +} + +//get value of objective function at vector x +bool minbndTMINLP::eval_f(Index n, const Number* x, bool new_x, Number& obj_value) +{ + char name[20]="_f"; + Number *obj; + if (getFunctionFromScilab1(n,name,x, 7, 1,2,&obj)) + { + return false; + } + obj_value = *obj; + return true; +} + +//get value of gradient of objective function at vector x. +bool minbndTMINLP::eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f) +{ + char name[20]="_gradf"; + Number *resg; + if (getFunctionFromScilab1(n,name,x, 7, 1,2,&resg)) + { + return false; + } + + Index i; + for(i=0;i<numVars_;i++) + { + grad_f[i]=resg[i]; + } + return true; +} + +// This method sets initial values for required vectors . For now we are assuming 0 to all values. +bool minbndTMINLP::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) +{ + assert(init_x == true); + assert(init_z == false); + assert(init_lambda == false); + if (init_x == true) + { //we need to set initial values for vector x + for (Index var=0;var<n;var++) + {x[var]=0.0;}//initialize with 0. + } + return true; +} + +/* + * Return either the sparsity structure of the Hessian of the Lagrangian, + * or the values of the Hessian of the Lagrangian for the given values for + * x,lambda,obj_factor. +*/ + +bool minbndTMINLP::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) +{ + double check; + if (values==NULL) + { + Index idx=0; + for (Index row = 0; row < numVars_; row++) + { + for (Index col = 0; col <= row; col++) + { iRow[idx] = row; + jCol[idx] = col; + idx++; + } + } + } + + else + { char name[20]="_gradhess"; + Number *resh; + if (getFunctionFromScilab1(n,name,x, 7, 1,2,&resh)) + { + return false; + } + Index index=0; + for (Index row=0;row < numVars_ ;++row) + { + for (Index col=0; col <= row; ++col) + { + values[index++]=obj_factor*(resh[numVars_*row+col]); + } + } + } + return true; +} + +void minbndTMINLP::finalize_solution(SolverReturn status,Index n, const Number* x, Number obj_value) +{ + finalObjVal_ = obj_value; + status_ = status; + if(status==0 ||status== 3) + { + finalX_ = new double[n]; + for (Index i=0; i<numVars_; i++) + { + finalX_[i] = x[i]; + } + } + +} + +const double * minbndTMINLP::getX() +{ + return finalX_; +} + +double minbndTMINLP::getObjVal() +{ + return finalObjVal_; +} + +int minbndTMINLP::returnStatus() +{ + return status_; +} diff --git a/sci_gateway/cpp/sci_minconTMINLP.cpp b/sci_gateway/cpp/sci_minconTMINLP.cpp new file mode 100644 index 0000000..0f9a6dc --- /dev/null +++ b/sci_gateway/cpp/sci_minconTMINLP.cpp @@ -0,0 +1,324 @@ +// Copyright (C) 2015 - IIT Bombay - FOSSEE +// +// Author: Harpreet Singh, Pranav Deshpande and Akshay Miterani +// Organization: FOSSEE, IIT Bombay +// Email: toolbox@scilab.in +// 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 "minconTMINLP.hpp" +#include "sci_iofunc.hpp" + +extern "C" +{ +#include "call_scilab.h" +#include <api_scilab.h> +#include <Scierror.h> +#include <BOOL.h> +#include <localization.h> +#include <sciprint.h> +#include <string.h> +#include <assert.h> +} + +using namespace Ipopt; +using namespace Bonmin; + +//#define DEBUG 0 + +minconTMINLP::~minconTMINLP() +{ + if(finalX_) delete[] finalX_; +} + +// Set the type of every variable - CONTINUOUS or INTEGER +bool minconTMINLP::get_variables_types(Index n, VariableType* var_types) +{ + #ifdef DEBUG + sciprint("Code is in get_variables_types\n"); + #endif + n = numVars_; + for(int i=0; i < n; i++) + var_types[i] = CONTINUOUS; + for(int i=0 ; i < intconSize_ ; ++i) + var_types[(int)(intcon_[i]-1)] = INTEGER; + return true; +} + +// The linearity of the variables - LINEAR or NON_LINEAR +bool minconTMINLP::get_variables_linearity(Index n, Ipopt::TNLP::LinearityType* var_types) +{ + #ifdef DEBUG + sciprint("Code is in get_variables_linearity\n"); + #endif + for(int i=0;i<n;i++) + { + var_types[i] = Ipopt::TNLP::NON_LINEAR; + } + return true; } + +// The linearity of the constraints - LINEAR or NON_LINEAR +bool minconTMINLP::get_constraints_linearity(Index m, Ipopt::TNLP::LinearityType* const_types) +{ + + #ifdef DEBUG + sciprint("Code is in get_constraints_linearity\n"); + #endif + for(int i=0;i<numLC_;i++) + { + const_types[i] = Ipopt::TNLP::LINEAR; + } + + for(int i=numLC_;i<m;i++) + { + const_types[i] = Ipopt::TNLP::NON_LINEAR; + } + return true;} + +//get NLP info such as number of variables,constraints,no.of elements in jacobian and hessian to allocate memory +bool minconTMINLP::get_nlp_info(Index& n, Index& m, Index& nnz_jac_g, Index& nnz_h_lag, TNLP::IndexStyleEnum& index_style) +{ + #ifdef DEBUG + sciprint("Code is in get_nlp_info\n"); + #endif + n=numVars_; // Number of variables + m=numCons_; // Number of constraints + nnz_jac_g = n*m; // No. of elements in Jacobian of constraints + nnz_h_lag = n*n; // No. of elements in Hessian of the Lagrangian. + index_style=TNLP::C_STYLE; // Index style of matrices + return true; +} + +//get variable and constraint bound info +bool minconTMINLP::get_bounds_info(Index n, Number* x_l, Number* x_u, Index m, Number* g_l, Number* g_u) +{ + #ifdef DEBUG + sciprint("Code is in get_bounds_info\n"); + #endif + unsigned int i; + for(i=0;i<n;i++) + { + x_l[i]=lb_[i]; + x_u[i]=ub_[i]; + } + for(i=0;i<m;i++) + { + g_l[i]=conLb_[i]; + g_u[i]=conUb_[i]; + } + return true; +} + +// This method sets initial values for required vectors . For now we are assuming 0 to all values. +bool minconTMINLP::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) +{ + assert(init_x == true); + assert(init_z == false); + assert(init_lambda == false); + if (init_x == true) + { //we need to set initial values for vector x + for (Index var=0;var<n;var++) + {x[var]=x0_[var];}//initialize with 0. + } + return true; +} + +//get value of objective function at vector x +bool minconTMINLP::eval_f(Index n, const Number* x, bool new_x, Number& obj_value) +{ + #ifdef DEBUG + sciprint("Code is eval_f\n"); + #endif + char name[20]="_f"; + Number *obj; + if (getFunctionFromScilab1(n,name,x, 7, 1,2,&obj)) + { + return false; + } + obj_value = *obj; + return true; +} + +//get value of gradient of objective function at vector x. +bool minconTMINLP::eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f) +{ + #ifdef DEBUG + sciprint("Code is in eval_grad_f\n"); + #endif + char name[20]="_gradf"; + Number *resg; + if (getFunctionFromScilab1(n,name,x, 7, 1,2,&resg)) + { + return false; + } + + Index i; + for(i=0;i<numVars_;i++) + { + grad_f[i]=resg[i]; + } + return true; +} + +// return the value of the constraints: g(x) +bool minconTMINLP::eval_g(Index n, const Number* x, bool new_x, Index m, Number* g) +{ + #ifdef DEBUG + sciprint("Code is in eval_g\n"); + #endif + // return the value of the constraints: g(x) + if(m==0) + { + g=NULL; + } + else + { + char name[20]="_addnlc"; + Number *con; + if (getFunctionFromScilab1(n,name,x, 7, 1,2,&con)) + { + return false; + } + + Index i; + for(i=0;i<m;i++) + { + g[i]=con[i]; + } + } + + return true; +} + +// return the structure or values of the jacobian +bool minconTMINLP::eval_jac_g(Index n, const Number* x, bool new_x,Index m, Index nele_jac, Index* iRow, Index *jCol,Number* values) +{ + #ifdef DEBUG + sciprint("Code is in eval_jac_g\n"); + #endif + if (values == NULL) + { + if(m==0)// return the structure of the jacobian of the constraints + { + iRow=NULL; + jCol=NULL; + } + else + { + unsigned int i,j,idx=0; + for(i=0;i<m;i++) + for(j=0;j<n;j++) + { + iRow[idx]=i; + jCol[idx]=j; + idx++; + } + } + } + else + { + if(m==0) + { + values=NULL; + } + else + { + double* resj; + char name[20]="_gradnlc"; + if (getFunctionFromScilab1(n,name,x, 7, 1,2,&resj)) + { + return false; + } + int c = 0; + for(int i=0;i<m;i++) + { + for(int j=0;j<n;j++) + { + values[c] = resj[j*(int)m+i]; + c++; + } + } + } + } + return true; +} + +/* + * Return either the sparsity structure of the Hessian of the Lagrangian, + * or the values of the Hessian of the Lagrangian for the given values for + * x,lambda,obj_factor. +*/ + +bool minconTMINLP::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) +{ + #ifdef DEBUG + sciprint("Code is in eval_h\n"); + #endif + double check; + if (values==NULL) + { + Index idx=0; + for (Index row = 0; row < numVars_; row++) + { + for (Index col = 0; col < numVars_; col++) + { + iRow[idx] = row; + jCol[idx] = col; + idx++; + } + } + } + else + { char name[20]="_gradhess"; + Number *resCh; + if (getHessFromScilab(n,m,name,x, &obj_factor, lambda, 7, 3,2,&resCh)) + { + return false; + } + Index index=0; + for (Index row=0;row < numVars_ ;++row) + { + for (Index col=0; col < numVars_; ++col) + { + values[index++]=resCh[numVars_*row+col]; + } + } + } + return true; +} + +void minconTMINLP::finalize_solution(SolverReturn status,Index n, const Number* x, Number obj_value) +{ + #ifdef DEBUG + sciprint("Code is in finalize_solution\n"); + sciprint("%d",status); + #endif + finalObjVal_ = obj_value; + status_ = status; + if(status==0 ||status== 3) + { + finalX_ = new double[n]; + for (Index i=0; i<numVars_; i++) + { + finalX_[i] = x[i]; + } + } +} + +const double * minconTMINLP::getX() +{ + return finalX_; +} + +double minconTMINLP::getObjVal() +{ + return finalObjVal_; +} + +int minconTMINLP::returnStatus() +{ + return status_; +} diff --git a/sci_gateway/cpp/sci_minuncTMINLP.cpp b/sci_gateway/cpp/sci_minuncTMINLP.cpp new file mode 100644 index 0000000..47e9879 --- /dev/null +++ b/sci_gateway/cpp/sci_minuncTMINLP.cpp @@ -0,0 +1,236 @@ +// Copyright (C) 2015 - IIT Bombay - FOSSEE +// +// Author: Harpreet Singh, Pranav Deshpande and Akshay Miterani +// Organization: FOSSEE, IIT Bombay +// Email: toolbox@scilab.in +// 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 "minuncTMINLP.hpp" +#include "sci_iofunc.hpp" + +extern "C" +{ +#include "call_scilab.h" +#include <api_scilab.h> +#include <Scierror.h> +#include <BOOL.h> +#include <localization.h> +#include <sciprint.h> +#include <string.h> +#include <assert.h> +} + +using namespace Ipopt; +using namespace Bonmin; + +minuncTMINLP::~minuncTMINLP() +{ + if(finalX_) delete[] finalX_; +} + +// Set the type of every variable - CONTINUOUS or INTEGER +bool minuncTMINLP::get_variables_types(Index n, VariableType* var_types) +{ + n = numVars_; + for(int i=0; i < n; i++) + var_types[i] = CONTINUOUS; + for(int i=0 ; i < intconSize_ ; ++i) + var_types[(int)(intcon_[i]-1)] = INTEGER; + return true; +} + +// The linearity of the variables - LINEAR or NON_LINEAR +bool minuncTMINLP::get_variables_linearity(Index n, Ipopt::TNLP::LinearityType* var_types) +{ + /* + n = numVars_; + for(int i = 0; i < n; i++) + var_types[i] = Ipopt::TNLP::LINEAR; + */ + return true; +} + +// The linearity of the constraints - LINEAR or NON_LINEAR +bool minuncTMINLP::get_constraints_linearity(Index m, Ipopt::TNLP::LinearityType* const_types) +{ + /* m = numConstr_; + for(int i = 0; i < m; i++) + const_types[i] = Ipopt::TNLP::LINEAR; + */ + return true; +} + +//get NLP info such as number of variables,constraints,no.of elements in jacobian and hessian to allocate memory +bool minuncTMINLP::get_nlp_info(Index& n, Index& m, Index& nnz_jac_g, Index& nnz_h_lag, TNLP::IndexStyleEnum& index_style) +{ + n=numVars_; // Number of variables + m=0; // 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=TNLP::C_STYLE; // Index style of matrices + return true; +} + +//get variable and constraint bound info +bool minuncTMINLP::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;i<n;i++) + { + x_l[i]=-1.0e19; + x_u[i]=1.0e19; + } + + g_l=NULL; + g_u=NULL; + return true; +} + +// return the value of the constraints: g(x) +bool minuncTMINLP::eval_g(Index n, const Number* x, bool new_x, Index m, Number* g) +{ + // return the value of the constraints: g(x) + g=NULL; + return true; +} + +// return the structure or values of the jacobian +bool minuncTMINLP::eval_jac_g(Index n, const Number* x, bool new_x,Index m, Index nele_jac, Index* iRow, Index *jCol,Number* values) +{ + if (values == NULL) + { + // return the structure of the jacobian of the constraints + iRow=NULL; + jCol=NULL; + } + else + { + values=NULL; + } + + return true; +} + +//get value of objective function at vector x +bool minuncTMINLP::eval_f(Index n, const Number* x, bool new_x, Number& obj_value) +{ + char name[20]="_f"; + Number *obj; + if (getFunctionFromScilab1(n,name,x, 7, 1,2,&obj)) + { + return false; + } + obj_value = *obj; + return true; +} + +//get value of gradient of objective function at vector x. +bool minuncTMINLP::eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f) +{ + char name[20]="_gradf"; + Number *resg; + if (getFunctionFromScilab1(n,name,x, 7, 1, 2, &resg)) + { + return false; + } + + Index i; + for(i=0;i<numVars_;i++) + { + grad_f[i]=resg[i]; + } + return true; +} + +// This method sets initial values for required vectors . For now we are assuming 0 to all values. +bool minuncTMINLP::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) +{ + assert(init_x == true); + assert(init_z == false); + assert(init_lambda == false); + if (init_x == true) + { //we need to set initial values for vector x + for (Index var=0;var<n;var++) + x[var]=varGuess_[var];//initialize with 0 or we can change. + } + + return true; +} + +/* + * Return either the sparsity structure of the Hessian of the Lagrangian, + * or the values of the Hessian of the Lagrangian for the given values for + * x,lambda,obj_factor. +*/ + +bool minuncTMINLP::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) +{ + double check; + if (values==NULL) + { + Index idx=0; + for (Index row = 0; row < numVars_; row++) + { + for (Index col = 0; col <= row; col++) + { + iRow[idx] = row; + jCol[idx] = col; + idx++; + } + } + } + + else + { + char name[20]="_gradhess"; + Number *resh; + if (getFunctionFromScilab1(n,name,x, 7, 1,2,&resh)) + { + return false; + } + Index index=0; + for (Index row=0;row < numVars_ ;++row) + { + for (Index col=0; col <= row; ++col) + { + values[index++]=obj_factor*(resh[numVars_*row+col]); + } + } + return true; + } +} + + +void minuncTMINLP::finalize_solution(SolverReturn status,Index n, const Number* x, Number obj_value) +{ + finalObjVal_ = obj_value; + status_ = status; + if(status==0 ||status== 3) + { + finalX_ = new double[n]; + for (Index i=0; i<numVars_; i++) + { + finalX_[i] = x[i]; + } + } + +} + +const double * minuncTMINLP::getX() +{ + return finalX_; +} + +double minuncTMINLP::getObjVal() +{ + return finalObjVal_; +} + +int minuncTMINLP::returnStatus() +{ + return status_; +} |