diff options
Diffstat (limited to 'sci_gateway')
-rw-r--r-- | sci_gateway/cpp/QuadNLP.hpp~ | 134 | ||||
-rw-r--r-- | sci_gateway/cpp/README.rst~ | 0 | ||||
-rw-r--r-- | sci_gateway/cpp/builder_gateway_cpp.sce~ | 149 | ||||
-rwxr-xr-x | sci_gateway/cpp/libFAMOS.so | bin | 126944 -> 126944 bytes | |||
-rw-r--r-- | sci_gateway/cpp/sci_QuadNLP.cpp | 5 | ||||
-rw-r--r-- | sci_gateway/cpp/sci_QuadNLP.cpp~ | 253 | ||||
-rw-r--r-- | sci_gateway/cpp/sci_ipopt.cpp | 167 | ||||
-rw-r--r-- | sci_gateway/cpp/sci_ipopt.cpp~ | 409 | ||||
-rw-r--r-- | sci_gateway/cpp/sci_sym_solve.cpp~ | 49 |
9 files changed, 126 insertions, 1040 deletions
diff --git a/sci_gateway/cpp/QuadNLP.hpp~ b/sci_gateway/cpp/QuadNLP.hpp~ deleted file mode 100644 index 5e1047f..0000000 --- a/sci_gateway/cpp/QuadNLP.hpp~ +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Quadratic Programming Toolbox for Scilab using IPOPT library - * Authors : - Sai Kiran - Keyur Joshi - Iswarya - - - * Optimizing (minimizing) the quadratic objective function having any number of variables and linear constraints. - * -*/ - -#ifndef __QuadNLP_HPP__ -#define __QuadNLP_HPP__ - -#include "IpTNLP.hpp" -extern "C"{ -#include <sciprint.h> - -} -using namespace Ipopt; - -class QuadNLP : public TNLP -{ - private: - Index numVars_; // Number of variables. - - Index numConstr_; // Number of 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 *conMatrix_ = NULL;//conMatrix_ is a pointer to matrix of size numConstr 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*numConstr_ - // with upper bounds of all constraints. - - const Number *conLB_ = NULL; //conLB_ is a pointer to a matrix of size of 1*numConstr_ - // 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 iter_; //Number of iteration. - - int status_; //Solver return status - - QuadNLP(const QuadNLP&); - QuadNLP& operator=(const QuadNLP&); - public: - /* - * Constructor - */ - QuadNLP(Index nV, Index nC, Number *qM, Number *lM, Number *cM, Number *cUB, Number *cLB, Number *vUB, Number *vLB): - numVars_(nV),numConstr_(nC),qMatrix_(qM),lMatrix_(lM),conMatrix_(cM),conUB_(cUB),conLB_(cLB),varUB_(vUB),varLB_(vLB),finalX_(0), finalZl_(0), finalZu_(0), finalObjVal_(1e20){ } - - - /* Go to : - - http://www.coin-or.org/Ipopt/documentation/node23.html#SECTION00053130000000000000 - For details about these below methods. - */ - virtual ~QuadNLP(); - virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g, - Index& nnz_h_lag, 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 finalize_solution(SolverReturn status, - Index n, const Number* x, const Number* z_L, const Number* z_U, - Index m, const Number* g, const Number* lambda, Number obj_value, - const IpoptData* ip_data, - IpoptCalculatedQuantities* ip_cq); - - const double * getX(); //Returns a pointer to a matrix of size of 1*numVar - // with final value for the primal variables. - - const double * getZu(); //Returns a pointer to a matrix of size of 1*numVars - // with final values for the upper bound multipliers - - const double * getZl(); //Returns a pointer to a matrix of size of 1*numVars - // with final values for the upper bound multipliers - - const double * getLambda(); //Returns a pointer to a matrix of size of 1*numConstr - // with final values for the constraint multipliers - - - double getObjVal(); //Returns the output of the final value of the objective. - - double iterCount(); //Returns the iteration count - - int returnStatus(); //Returns the status count - - -}; - -#endif __QuadNLP_HPP__ diff --git a/sci_gateway/cpp/README.rst~ b/sci_gateway/cpp/README.rst~ deleted file mode 100644 index e69de29..0000000 --- a/sci_gateway/cpp/README.rst~ +++ /dev/null diff --git a/sci_gateway/cpp/builder_gateway_cpp.sce~ b/sci_gateway/cpp/builder_gateway_cpp.sce~ deleted file mode 100644 index 225edd8..0000000 --- a/sci_gateway/cpp/builder_gateway_cpp.sce~ +++ /dev/null @@ -1,149 +0,0 @@ -// Copyright (C) 2015 - IIT Bombay - FOSSEE -// -// Author: Keyur Joshi, Sai Kiran, Iswarya and Harpreet Singh -// Organization: FOSSEE, IIT Bombay -// Email: harpreet.mertia@gmail.com -// This file must be used under the terms of the CeCILL. -// This source file is licensed as described in the file COPYING, which -// you should have received as part of this distribution. The terms -// are also available at -// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt - -mode(-1) -lines(0) - -toolbox_title = "FAMOS"; - -[a, opt] = getversion(); -Version = opt(2); - -path_builder = get_absolute_file_path('builder_gateway_cpp.sce'); - -tools_path = path_builder + "../../thirdparty/linux/"; - -C_Flags=["-w -fpermissive -I"+tools_path+"include/coin -Wl,-rpath="+tools_path+"lib/"+Version+filesep()+" "] - -Linker_Flag = ["-L"+tools_path+"lib/"+Version+filesep()+"libSym"+" "+"-L"+tools_path+"lib/"+Version+filesep()+"libipopt" ] - - -//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"; - "sym_getConstrSense","sci_sym_get_row_sense"; - - //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"; - - //QP function - "solveqp","sci_solveqp" - ]; - -//Name of all the files to be compiled -Files = [ - "globals.cpp", - "sci_iofunc.hpp", - "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", - "sym_data_query_functions.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", - "QuadNLP.hpp", - "sci_ipopt.cpp" - - ] - -tbx_build_gateway(toolbox_title,Function_Names,Files,get_absolute_file_path("builder_gateway_cpp.sce"), [], Linker_Flag, C_Flags, [], "g++"); - -clear WITHOUT_AUTO_PUTLHSVAR toolbox_title Function_Names Files Linker_Flag C_Flags; diff --git a/sci_gateway/cpp/libFAMOS.so b/sci_gateway/cpp/libFAMOS.so Binary files differindex 4a210ee..ec45532 100755 --- a/sci_gateway/cpp/libFAMOS.so +++ b/sci_gateway/cpp/libFAMOS.so diff --git a/sci_gateway/cpp/sci_QuadNLP.cpp b/sci_gateway/cpp/sci_QuadNLP.cpp index 99987a2..50e8109 100644 --- a/sci_gateway/cpp/sci_QuadNLP.cpp +++ b/sci_gateway/cpp/sci_QuadNLP.cpp @@ -209,10 +209,11 @@ void QuadNLP::finalize_solution(SolverReturn status, finalLambda_[i] = lambda[i]; } - iter_ = ip_data->iter_count(); finalObjVal_ = obj_value; status_ = status; - + if (status_ == 0 | status_ == 1 | status_ == 2){ + iter_ = ip_data->iter_count(); + } } const double * QuadNLP::getX() diff --git a/sci_gateway/cpp/sci_QuadNLP.cpp~ b/sci_gateway/cpp/sci_QuadNLP.cpp~ deleted file mode 100644 index 99987a2..0000000 --- a/sci_gateway/cpp/sci_QuadNLP.cpp~ +++ /dev/null @@ -1,253 +0,0 @@ -/* - * Quadratic Programming Toolbox for Scilab using IPOPT library - * Authors : - Sai Kiran - Keyur Joshi - Iswarya - */ - -#include "QuadNLP.hpp" -#include "IpIpoptData.hpp" - -extern "C"{ -#include <api_scilab.h> -#include <Scierror.h> -#include <BOOL.h> -#include <localization.h> -#include <sciprint.h> - - -double x_static,i, *op_obj_x = NULL,*op_obj_value = NULL; - -using namespace Ipopt; - -QuadNLP::~QuadNLP() - { - free(finalX_); - free(finalZl_); - free(finalZu_);} - -//get NLP info such as number of variables,constraints,no.of elements in jacobian and hessian to allocate memory -bool QuadNLP::get_nlp_info(Index& n, Index& m, Index& nnz_jac_g, Index& nnz_h_lag, IndexStyleEnum& index_style){ - n=numVars_; // Number of variables - m=numConstr_; // Number of constraints - nnz_jac_g = n*m; // No. of elements in Jacobian of constraints - nnz_h_lag = n*(n+1)/2; // No. of elements in lower traingle of Hessian of the Lagrangian. - index_style=C_STYLE; // Index style of matrices - return true; - } - -//get variable and constraint bound info -bool QuadNLP::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; - } - -//get value of objective function at vector x -bool QuadNLP::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 value of gradient of objective function at vector x. -bool QuadNLP::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 QuadNLP::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; -} - -// This method sets initial values for required vectors . For now we are assuming 0 to all values. -bool QuadNLP::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; - } -/* Return either the sparsity structure of the Jacobian of the constraints, or the values for the Jacobian of the constraints at the point x. - -*/ -bool QuadNLP::eval_jac_g(Index n, const Number* x, bool new_x, - Index m, Index nele_jac, Index* iRow, Index *jCol, - Number* values){ - - //It asked for structure of 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 asked 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; - } - -/* - * 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 QuadNLP::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 QuadNLP::finalize_solution(SolverReturn status, - Index n, const Number* x, const Number* z_L, const Number* z_U, - Index m, const Number* g, const Number* lambda, Number obj_value, - const IpoptData* ip_data, - IpoptCalculatedQuantities* ip_cq){ - - finalX_ = (double*)malloc(sizeof(double) * numVars_ * 1); - for (Index i=0; i<n; i++) - { - finalX_[i] = x[i]; - } - - finalZl_ = (double*)malloc(sizeof(double) * numVars_ * 1); - for (Index i=0; i<n; i++) - { - finalZl_[i] = z_L[i]; - } - - finalZu_ = (double*)malloc(sizeof(double) * numVars_ * 1); - for (Index i=0; i<n; i++) - { - finalZu_[i] = z_U[i]; - } - - finalLambda_ = (double*)malloc(sizeof(double) * numConstr_ * 1); - for (Index i=0; i<m; i++) - { - finalLambda_[i] = lambda[i]; - } - - iter_ = ip_data->iter_count(); - finalObjVal_ = obj_value; - status_ = status; - - } - - const double * QuadNLP::getX() - { - return finalX_; - } - - const double * QuadNLP::getZl() - { - return finalZl_; - } - - const double * QuadNLP::getZu() - { - return finalZu_; - } - - const double * QuadNLP::getLambda() - { - return finalLambda_; - } - - double QuadNLP::getObjVal() - { - return finalObjVal_; - } - - double QuadNLP::iterCount() - { - return (double)iter_; - } - - int QuadNLP::returnStatus() - { - return status_; - } - -} diff --git a/sci_gateway/cpp/sci_ipopt.cpp b/sci_gateway/cpp/sci_ipopt.cpp index 4168488..98ed153 100644 --- a/sci_gateway/cpp/sci_ipopt.cpp +++ b/sci_gateway/cpp/sci_ipopt.cpp @@ -324,46 +324,45 @@ int sci_solveqp(char *fname) return 0; } - using namespace Ipopt; - - SmartPtr<QuadNLP> Prob = new QuadNLP(nVars,nCons,QItems,PItems,ConItems,conUB,conLB,varUB,varLB,init_guess); - SmartPtr<IpoptApplication> app = IpoptApplicationFactory(); - app->RethrowNonIpoptException(true); - - // Change some options - // Note: The following choices are only examples, they might not be - // suitable for your optimization problem. - app->Options()->SetNumericValue("tol", 1e-7); - app->Options()->SetIntegerValue("max_iter", (int)*max_iter); - app->Options()->SetNumericValue("max_cpu_time", *cpu_time); - app->Options()->SetStringValue("mu_strategy", "adaptive"); - - // Indicates whether all equality constraints are linear - app->Options()->SetStringValue("jac_c_constant", "yes"); - // Indicates whether all inequality constraints are linear - app->Options()->SetStringValue("jac_d_constant", "yes"); - // Indicates whether the problem is a quadratic problem - app->Options()->SetStringValue("hessian_constant", "yes"); + using namespace Ipopt; + + SmartPtr<QuadNLP> Prob = new QuadNLP(nVars,nCons,QItems,PItems,ConItems,conUB,conLB,varUB,varLB,init_guess); + SmartPtr<IpoptApplication> app = IpoptApplicationFactory(); + app->RethrowNonIpoptException(true); + + // Change some options + // Note: The following choices are only examples, they might not be + // suitable for your optimization problem. + app->Options()->SetNumericValue("tol", 1e-7); + app->Options()->SetIntegerValue("max_iter", (int)*max_iter); + app->Options()->SetNumericValue("max_cpu_time", *cpu_time); + app->Options()->SetStringValue("mu_strategy", "adaptive"); + + // Indicates whether all equality constraints are linear + app->Options()->SetStringValue("jac_c_constant", "yes"); + // Indicates whether all inequality constraints are linear + app->Options()->SetStringValue("jac_d_constant", "yes"); + // Indicates whether the problem is a quadratic problem + app->Options()->SetStringValue("hessian_constant", "yes"); + + // Initialize the IpoptApplication and process the options + ApplicationReturnStatus status; + status = app->Initialize(); + if (status != Solve_Succeeded) { + sciprint("\n*** Error during initialization!\n"); + return (int) status; + } + // Ask Ipopt to solve the problem - // Initialize the IpoptApplication and process the options - ApplicationReturnStatus status; - status = app->Initialize(); - if (status != Solve_Succeeded) { - sciprint("\n*** Error during initialization!\n"); - return0toScilab(); - return (int) status; - } - // Ask Ipopt to solve the problem - - status = app->OptimizeTNLP(Prob); + status = app->OptimizeTNLP(Prob); + + int stats = Prob->returnStatus(); + if (stats == 0 | stats == 1 | stats == 2){ double *fX = Prob->getX(); double ObjVal = Prob->getObjVal(); - double *Zl = Prob->getZl(); - double *Zu = Prob->getZu(); - double *Lambda = Prob->getLambda(); double iteration = Prob->iterCount(); - int stats = Prob->returnStatus(); + sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, nVars, fX); if (sciErr.iErr) { @@ -392,41 +391,121 @@ int sci_solveqp(char *fname) return 0; } - sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 5, 1, nVars, Zl); + AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; + AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2; + AssignOutputVariable(pvApiCtx, 3) = nbInputArgument(pvApiCtx) + 3; + AssignOutputVariable(pvApiCtx, 4) = nbInputArgument(pvApiCtx) + 4; + } + + else + { + double *fX = NULL; + double ObjVal = 0; + int stats = Prob->returnStatus(); + double iteration = 0; + + sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 0, 0, fX); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } - sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 6, 1, nVars, Zu); + sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2,1,1,&ObjVal); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } - - sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 7, 1, nCons, Lambda); + + sciErr = createMatrixOfInteger32(pvApiCtx, nbInputArgument(pvApiCtx) + 3,1,1,&stats); + if (sciErr.iErr) + { + printError(&sciErr, 0); + return 0; + } + + sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 4,1,1,&iteration); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } - AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2; AssignOutputVariable(pvApiCtx, 3) = nbInputArgument(pvApiCtx) + 3; AssignOutputVariable(pvApiCtx, 4) = nbInputArgument(pvApiCtx) + 4; + } + + + if(stats == 0){ + + double *Zl = Prob->getZl(); + double *Zu = Prob->getZu(); + double *Lambda = Prob->getLambda(); + + sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 5, 1, nVars, Zl); + if (sciErr.iErr) + { + printError(&sciErr, 0); + return 0; + } + + sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 6, 1, nVars, Zu); + if (sciErr.iErr) + { + printError(&sciErr, 0); + return 0; + } + + sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 7, 1, nCons, Lambda); + if (sciErr.iErr) + { + printError(&sciErr, 0); + return 0; + } + AssignOutputVariable(pvApiCtx, 5) = nbInputArgument(pvApiCtx) + 5; AssignOutputVariable(pvApiCtx, 6) = nbInputArgument(pvApiCtx) + 6; - AssignOutputVariable(pvApiCtx, 7) = nbInputArgument(pvApiCtx) + 7; + AssignOutputVariable(pvApiCtx, 7) = nbInputArgument(pvApiCtx) + 7; + } - // As the SmartPtrs go out of scope, the reference count - // will be decremented and the objects will automatically - // be deleted. + else{ + double *Zl = NULL; + double *Zu = NULL; + double *Lambda = NULL; + sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 5, 0, 0, Zl); + if (sciErr.iErr) + { + printError(&sciErr, 0); + return 0; + } + + sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 6, 0, 0, Zu); + if (sciErr.iErr) + { + printError(&sciErr, 0); + return 0; + } + + sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 7, 0, 0, Lambda); + if (sciErr.iErr) + { + printError(&sciErr, 0); + return 0; + } + + AssignOutputVariable(pvApiCtx, 5) = nbInputArgument(pvApiCtx) + 5; + AssignOutputVariable(pvApiCtx, 6) = nbInputArgument(pvApiCtx) + 6; + AssignOutputVariable(pvApiCtx, 7) = nbInputArgument(pvApiCtx) + 7; + } + // As the SmartPtrs go out of scope, the reference count + // will be decremented and the objects will automatically + // be deleted. + return 0; } diff --git a/sci_gateway/cpp/sci_ipopt.cpp~ b/sci_gateway/cpp/sci_ipopt.cpp~ deleted file mode 100644 index 8d62b21..0000000 --- a/sci_gateway/cpp/sci_ipopt.cpp~ +++ /dev/null @@ -1,409 +0,0 @@ -/* - * Quadratic Programming Toolbox for Scilab using IPOPT library - * Authors : - Sai Kiran - Keyur Joshi - Iswarya - */ - - -#include "sci_iofunc.hpp" -#include "IpIpoptApplication.hpp" -#include "QuadNLP.hpp" - -extern "C"{ -#include <api_scilab.h> -#include <Scierror.h> -#include <BOOL.h> -#include <localization.h> -#include <sciprint.h> - -int j; -double *op_x, *op_obj,*p; - -bool readSparse(int arg,int *iRows,int *iCols,int *iNbItem,int** piNbItemRow, int** piColPos, double** pdblReal){ - SciErr sciErr; - int* piAddr = NULL; - int iType = 0; - int iRet = 0; - sciErr = getVarAddressFromPosition(pvApiCtx, arg, &piAddr); - if(sciErr.iErr) { - printError(&sciErr, 0); - return false; - } - sciprint("\ndone\n"); - if(isSparseType(pvApiCtx, piAddr)){ - sciprint("done\n"); - sciErr =getSparseMatrix(pvApiCtx, piAddr, iRows, iCols, iNbItem, piNbItemRow, piColPos, pdblReal); - if(sciErr.iErr) { - printError(&sciErr, 0); - return false; - } - } - - else { - sciprint("\nSparse matrix required\n"); - return false; - } - return true; - } - -int sci_solveqp(char *fname) -{ - - CheckInputArgument(pvApiCtx, 10, 10); // We need total 10 input arguments. - CheckOutputArgument(pvApiCtx, 7, 7); - - // Error management variable - SciErr sciErr; - int retVal=0, *piAddressVarQ = NULL,*piAddressVarP = NULL,*piAddressVarCM = NULL,*piAddressVarCUB = NULL,*piAddressVarCLB = NULL, *piAddressVarLB = NULL,*piAddressVarUB = NULL,*piAddressVarG = NULL; - double *QItems=NULL,*PItems=NULL,*ConItems=NULL,*conUB=NULL,*conLB=NULL,*varUB=NULL,*varLB=NULL,*init_guess = NULL,x,f,iter; - static unsigned int nVars = 0,nCons = 0; - unsigned int temp1 = 0,temp2 = 0; - - - ////////// Manage the input argument ////////// - - - //Number of Variables - getIntFromScilab(1,&nVars); - - //Number of Constraints - getIntFromScilab(2,&nCons); - - temp1 = nVars; - temp2 = nCons; - - //Q matrix from scilab - /* get Address of inputs */ - sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarQ); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - /* Check that the first input argument is a real matrix (and not complex) */ - if ( !isDoubleType(pvApiCtx, piAddressVarQ) || isVarComplex(pvApiCtx, piAddressVarQ) ) - { - Scierror(999, "%s: Wrong type for input argument #%d: A real matrix expected.\n", fname, 3); - return 0; - } - - /* get matrix */ - sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarQ, &temp1, &temp1, &QItems); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - //P matrix from scilab - /* get Address of inputs */ - sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddressVarP); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - /* Check that the first input argument is a real matrix (and not complex) */ - if ( !isDoubleType(pvApiCtx, piAddressVarP) || isVarComplex(pvApiCtx, piAddressVarP) ) - { - Scierror(999, "%s: Wrong type for input argument #%d: A real matrix expected.\n", fname, 4); - return 0; - } - - temp1 = 1; - temp2 = nVars; - /* get matrix */ - sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarP, &temp1,&temp2, &PItems); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - if (nCons!=0) - { - //conMatrix matrix from scilab - /* get Address of inputs */ - sciErr = getVarAddressFromPosition(pvApiCtx, 5, &piAddressVarCM); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - /* Check that the first input argument is a real matrix (and not complex) */ - if ( !isDoubleType(pvApiCtx, piAddressVarCM) || isVarComplex(pvApiCtx, piAddressVarCM) ) - { - Scierror(999, "%s: Wrong type for input argument #%d: A real matrix expected.\n", fname, 5); - return 0; - } - temp1 = nCons; - temp2 = nVars; - - /* get matrix */ - sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarCM,&temp1, &temp2, &ConItems); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - - //conLB matrix from scilab - /* get Address of inputs */ - sciErr = getVarAddressFromPosition(pvApiCtx, 6, &piAddressVarCLB); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - /* Check that the first input argument is a real matrix (and not complex) */ - if ( !isDoubleType(pvApiCtx, piAddressVarCLB) || isVarComplex(pvApiCtx, piAddressVarCLB) ) - { - Scierror(999, "%s: Wrong type for input argument #%d: A real matrix expected.\n", fname, 6); - return 0; - } - temp1 = nCons; - temp2 = 1; - - /* get matrix */ - sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarCLB,&temp1, &temp2, &conLB); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - //conUB matrix from scilab - /* get Address of inputs */ - sciErr = getVarAddressFromPosition(pvApiCtx, 7, &piAddressVarCUB); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - /* Check that the first input argument is a real matrix (and not complex) */ - if ( !isDoubleType(pvApiCtx, piAddressVarCUB) || isVarComplex(pvApiCtx, piAddressVarCUB) ) - { - Scierror(999, "%s: Wrong type for input argument #%d: A real matrix expected.\n", fname, 7); - return 0; - } - - temp1 = nCons; - temp2 = 1; - - /* get matrix */ - sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarCUB,&temp1, &temp2, &conUB); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - } - - //varLB matrix from scilab - /* get Address of inputs */ - sciErr = getVarAddressFromPosition(pvApiCtx, 8, &piAddressVarLB); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - /* Check that the first input argument is a real matrix (and not complex) */ - if ( !isDoubleType(pvApiCtx, piAddressVarLB) || isVarComplex(pvApiCtx, piAddressVarLB) ) - { - Scierror(999, "%s: Wrong type for input argument #%d: A real matrix expected.\n", fname, 8); - return 0; - } - temp1 = 1; - temp2 = nVars; - - /* get matrix */ - sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarLB, &temp1,&temp2, &varLB); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - //varUB matrix from scilab - /* get Address of inputs */ - sciErr = getVarAddressFromPosition(pvApiCtx, 9, &piAddressVarUB); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - /* Check that the first input argument is a real matrix (and not complex) */ - if ( !isDoubleType(pvApiCtx, piAddressVarUB) || isVarComplex(pvApiCtx, piAddressVarUB) ) - { - Scierror(999, "%s: Wrong type for input argument #%d: A real matrix expected.\n", fname, 9); - return 0; - } - - temp1 = 1; - temp2 = nVars; - - /* get matrix */ - sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarUB, &temp1,&temp2, &varUB); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - /* get matrix */ - sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarLB, &temp1,&temp2, &varLB); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - //Initial Value of variables from scilab - /* get Address of inputs */ - sciErr = getVarAddressFromPosition(pvApiCtx, 10, &piAddressVarG); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - /* Check that the first input argument is a real matrix (and not complex) */ - if ( !isDoubleType(pvApiCtx, piAddressVarG) || isVarComplex(pvApiCtx, piAddressVarG) ) - { - Scierror(999, "%s: Wrong type for input argument #%d: A real matrix expected.\n", fname, 10); - return 0; - } - - temp1 = 1; - temp2 = nVars; - - /* get matrix */ - sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarG, &temp1,&temp2, &init_guess); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - using namespace Ipopt; - - SmartPtr<QuadNLP> Prob = new QuadNLP(nVars,nCons,QItems,PItems,ConItems,conUB,conLB,varUB,varLB); - SmartPtr<IpoptApplication> app = IpoptApplicationFactory(); - app->RethrowNonIpoptException(true); - - // Change some options - // Note: The following choices are only examples, they might not be - // suitable for your optimization problem. - app->Options()->SetNumericValue("tol", 1e-7); - app->Options()->SetStringValue("mu_strategy", "adaptive"); - - // Indicates whether all equality constraints are linear - app->Options()->SetStringValue("jac_c_constant", "yes"); - // Indicates whether all inequality constraints are linear - app->Options()->SetStringValue("jac_d_constant", "yes"); - // Indicates whether the problem is a quadratic problem - app->Options()->SetStringValue("hessian_constant", "yes"); - - // Initialize the IpoptApplication and process the options - ApplicationReturnStatus status; - status = app->Initialize(); - if (status != Solve_Succeeded) { - sciprint("\n*** Error during initialization!\n"); - return0toScilab(); - return (int) status; - } - // Ask Ipopt to solve the problem - - status = app->OptimizeTNLP(Prob); - - double *fX = Prob->getX(); - double ObjVal = Prob->getObjVal(); - double *Zl = Prob->getZl(); - double *Zu = Prob->getZu(); - double *Lambda = Prob->getLambda(); - double iteration = Prob->iterCount(); - int stats = Prob->returnStatus(); - sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, nVars, fX); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2,1,1,&ObjVal); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - sciErr = createMatrixOfInteger32(pvApiCtx, nbInputArgument(pvApiCtx) + 3,1,1,&stats); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 4,1,1,&iteration); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 5, 1, nVars, Zl); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 6, 1, nVars, Zu); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 7, 1, nCons, Lambda); - if (sciErr.iErr) - { - printError(&sciErr, 0); - return 0; - } - - - AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; - AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2; - AssignOutputVariable(pvApiCtx, 3) = nbInputArgument(pvApiCtx) + 3; - AssignOutputVariable(pvApiCtx, 4) = nbInputArgument(pvApiCtx) + 4; - AssignOutputVariable(pvApiCtx, 5) = nbInputArgument(pvApiCtx) + 5; - AssignOutputVariable(pvApiCtx, 6) = nbInputArgument(pvApiCtx) + 6; - AssignOutputVariable(pvApiCtx, 7) = nbInputArgument(pvApiCtx) + 7; - - // As the SmartPtrs go out of scope, the reference count - // will be decremented and the objects will automatically - // be deleted. - - - return 0; - } - -} - -/* -hessian_constan -jacobian _constant - -j_s_d constant : yes -*/ - diff --git a/sci_gateway/cpp/sci_sym_solve.cpp~ b/sci_gateway/cpp/sci_sym_solve.cpp~ deleted file mode 100644 index 4abb268..0000000 --- a/sci_gateway/cpp/sci_sym_solve.cpp~ +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Implementation Symphony Tool Box for Scilab - * Contains sym_solve function - * Author : Sai Kiran - */ - -#include <symphony.h> -#include <sci_iofunc.hpp> -extern sym_environment* global_sym_env;//defined in globals.cpp - -extern "C" { -#include <api_scilab.h> -#include <Scierror.h> -#include <BOOL.h> -#include <localization.h> -#include <sciprint.h> -#include <stdio.h> -int process_ret_val(int); - -int sci_sym_solve(char *fname, unsigned long fname_len){ - - int status=0; - - //check whether we have no input and one output argument or not - CheckInputArgument(pvApiCtx, 0, 0) ;//no input argument - CheckOutputArgument(pvApiCtx, 1, 1) ;//one output argument - - // Check environment - if(global_sym_env==NULL) - sciprint("Error: Symphony environment is not initialized.\n"); - else {// There is an environment opened - double time_limit = -1.0; - status = sym_get_dbl_param(global_sym_env,"time_limit",&time_limit); - - if (status == FUNCTION_TERMINATED_NORMALLY) { - if ( time_limit < 0.0 ) - sciprint("\nNote: There is no limit on time.\n"); - else sciprint("\nNote: Time limit has been set to %lf.\n",time_limit); - status=process_ret_val(sym_solve(global_sym_env));// Call function - } - else { - sciprint("\nUnable to read time limit.\n"); - status = 1; //Error state - } - } - // Return result to scilab - return returnDoubleToScilab(status); - } -} |