546 ostringstream outStr;
548 bool isNonlinear =
false;
551 std::string description =
"";
553 if(
osresult->setSolverInvoked(
"LINDO Systems, Inc. Lindo API") !=
true)
554 throw ErrorClass(
"OSResult error: setSolverInvoked");
556 throw ErrorClass(
"OSResult error: setServiceName");
559 throw ErrorClass(
"OSResult error: setInstanceName");
564 if(
osinstance->getNumberOfNonlinearObjectives() > 0 ||
osinstance->getNumberOfNonlinearConstraints() > 0) isNonlinear =
true;
567 if(
osinstance->getObjectiveNumber() <= 0)
throw ErrorClass(
"LINDO NEEDS AN OBJECTIVE FUNCTION");
569 if( isNonlinear ==
true )
573 "We are using the LINDO Global Optimizer\n");
576 LSgetInfo (
pModel_, LS_IINFO_GOP_STATUS, &nSolStatus);
581 "We are using the LINDO LSsolveMIP Optimizer\n");
587 throw ErrorClass(
"OSResult error: setVariableNumer");
588 if(
osresult->setObjectiveNumber( 1) !=
true)
589 throw ErrorClass(
"OSResult error: setObjectiveNumber");
591 throw ErrorClass(
"OSResult error: setConstraintNumber");
592 if(
osresult->setSolutionNumber( 1) !=
true)
593 throw ErrorClass(
"OSResult error: setSolutionNumer");
596 outStr <<
"Solution Status = " << nSolStatus << endl;
599 osresult->setGeneralStatusType(
"normal");
600 osresult->setSolutionStatus(solIdx,
"optimal", description);
609 y =
new double[
osinstance->getConstraintNumber() ];
624 || (isNonlinear ==
false) )
627 lindoAPIErrorCheck(
"Error trying to obtain primal solution with integer variables present");
633 lindoAPIErrorCheck(
"Error trying to obtain primal solution with NO integer variables present");
635 osresult->setPrimalVariableValuesDense(solIdx, x);
638 || (isNonlinear ==
false) )
641 lindoAPIErrorCheck(
"Error trying to obtain dual solution with integer variables present");
646 lindoAPIErrorCheck(
"Error trying to obtain dual solution with NO integer variables present");
648 osresult->setDualVariableValuesDense(solIdx, y);
651 || (isNonlinear ==
false ) )
659 lindoAPIErrorCheck(
"Error trying to obtain the reduced costs with NO integer variables present");;
663 int numberOfOtherVariableResult = 1;
666 osresult->setNumberOfOtherVariableResults(solIdx, numberOfOtherVariableResult);
670 srcost[ i] = outStr.str();
673 osresult->setAnOtherVariableResultDense(solIdx, otherIdx,
"reduced costs",
"",
"the variable reduced costs", srcost);
676 || (isNonlinear ==
false ) )
679 lindoAPIErrorCheck(
"Error trying to obtain optimal objective value with integer variables present");
683 LSgetInfo(
pModel_, LS_DINFO_GOP_OBJ, &z[0]);
684 lindoAPIErrorCheck(
"Error trying to obtain optimal objective value with NO integer variables present");
686 osresult->setObjectiveValuesDense(solIdx, z);
690 osresult->setSolutionStatus(solIdx,
"infeasible", description);
693 osresult->setSolutionStatus(solIdx,
"unbounded", description);
696 osresult->setSolutionStatus(solIdx,
"other", description);
710 osresult->setGeneralStatusType(
"error");
719 int nQCnnz =
osinstance->getNumberOfQuadraticTerms();
721 "WE ARE PROCESSING QUADRATIC TERMS\n");
724 if(nQCnnz <= 0)
return false;
725 std::map<std::string, double> mapQuadraticAdjustMap;
726 std::map<std::string, double>::iterator mapPointer;
729 int iVarOneIndex, iVarTwoIndex;
734 int iStringPostionOne, iStringPostionTwo;
736 int* paiQCrows =
osinstance->getQuadraticTerms()->rowIndexes;
737 int* paiQCcols1 =
osinstance->getQuadraticTerms()->varOneIndexes;
738 int* paiQCcols2 =
osinstance->getQuadraticTerms()->varTwoIndexes;
739 double* padQCcoef =
osinstance->getQuadraticTerms()->coefficients;
740 std::ostringstream ostrRow, ostr1, ostr2;
741 std::string sIndexRow, sIndex1, sIndex2;
743 for ( i = 0; i < nQCnnz; i++)
745 iRowIndex = paiQCrows[i];
749 iVarOneIndex = (paiQCcols1[i] >= paiQCcols2[i])?paiQCcols2[i]:paiQCcols1[i];
750 iVarTwoIndex = (paiQCcols1[i] <= paiQCcols2[i])?paiQCcols2[i]:paiQCcols1[i];
751 ostrRow << iRowIndex;
754 sIndexRow = ostrRow.str();
755 sIndex2 = ostr2.str();
756 sIndex1 = ostr1.str();
762 sKey = sIndexRow +
"," + sIndex1 +
"," + sIndex2;
764 mapPointer = mapQuadraticAdjustMap.find(sKey);
765 if (mapPointer != mapQuadraticAdjustMap.end())
767 dValue = mapPointer->second;
768 dValue += padQCcoef[i];
769 mapQuadraticAdjustMap[sKey] = dValue;
773 mapQuadraticAdjustMap[sKey] = padQCcoef[i];
778 int iNumberOfQuadraticTerms = 0;
779 for (mapPointer = mapQuadraticAdjustMap.begin(); mapPointer != mapQuadraticAdjustMap.end(); ++mapPointer)
781 sKey = mapPointer->first;
782 dValue = mapPointer->second;
783 iStringPostionOne = sKey.find_first_of(
',');
784 iStringPostionTwo = sKey.find_last_of(
',');
786 iRowIndex = atoi(sKey.substr(0, iStringPostionOne).c_str());
787 iVarOneIndex = atoi(sKey.substr(iStringPostionOne + 1, iStringPostionTwo).c_str());
788 iVarTwoIndex = atoi(sKey.substr(iStringPostionTwo + 1).c_str());
790 if ( iVarOneIndex == iVarTwoIndex)
795 paiQCrows[iNumberOfQuadraticTerms] = iRowIndex;
796 paiQCcols1[iNumberOfQuadraticTerms] = iVarOneIndex;
797 paiQCcols2[iNumberOfQuadraticTerms] = iVarTwoIndex;
799 padQCcoef[iNumberOfQuadraticTerms] = dValue ;
800 iNumberOfQuadraticTerms ++;
803 if(!LSloadQCData(
pModel_, nQCnnz, paiQCrows, paiQCcols1,
804 paiQCcols2, padQCcoef))
return true;
810 osresult->setGeneralStatusType(
"error");
820 std::ostringstream outStr;
823 "PROCESS NONLINEAR TERMS\n");
828 outStr <<
"The number of objectives with nonlinear terms is: "
829 <<
osinstance->getNumberOfNonlinearObjectives() << endl;
830 outStr <<
"The number of constraints with nonlinear terms is: "
831 <<
osinstance->getNumberOfNonlinearConstraints() << endl << endl << endl;
842 std::map<double, int> mapNewNumber;
845 std::map<double, int>::iterator pos;
852 std::vector<int> insList;
860 std::map<int, OSExpressionTree*> allExpTrees;
867 std::vector<OSnLNode*> postFixVec;
870 std::map<int, OSExpressionTree*>::iterator posTree;
881 int *piObjSense = NULL;
886 double *padVarLowerBounds = NULL;
891 double *padVarUpperBounds = NULL;
894 double *padVarval = NULL;
899 char *pachVarType = NULL;
903 char *pachConType = NULL;
906 int iNumNonlinearNonz = 0;
909 double *padNonlinearNonz = NULL;
912 int iNumberOfNonlinearConstraints =
osinstance->getNumberOfNonlinearConstraints();
917 int *paiConsBegin = NULL;
918 if(iNumberOfNonlinearConstraints > 0) paiConsBegin =
new int[ iNumberOfNonlinearConstraints];
923 int *paiConsLength = NULL;
924 if(iNumberOfNonlinearConstraints > 0) paiConsLength =
new int[ iNumberOfNonlinearConstraints];
927 int iNumberOfNonlinearObjectives =
osinstance->getNumberOfNonlinearObjectives();
932 int *paiObjsBegin = NULL;
933 if(iNumberOfNonlinearObjectives > 0) paiObjsBegin =
new int[ iNumberOfNonlinearObjectives];
938 int *paiObjsLength = NULL;
939 if(iNumberOfNonlinearObjectives > 0) paiObjsLength =
new int[ iNumberOfNonlinearObjectives];
947 int iNumberOfNewVariables = 0;
953 int iInstListLength = 0;
956 int *paiNonlinearConIndex = NULL;
957 if(iNumberOfNonlinearConstraints > 0) paiNonlinearConIndex =
new int[ iNumberOfNonlinearConstraints];
967 allExpTrees =
osinstance->getAllNonlinearExpressionTreesMod();
969 for(posTree = allExpTrees.begin(); posTree != allExpTrees.end(); ++posTree)
973 outStr <<
"HERE IS EXPRESSION TREE " << posTree->first << endl;
976 postFixVec = posTree->second->m_treeRoot->getPostfixFromExpressionTree();
977 int iVecSize = postFixVec.size();
981 for(i = 0; i < iVecSize; i++)
983 iNodeID = postFixVec[i]->inodeInt;
987 insList.push_back( nlNodeIdxLindo[
OS_SUM] );
988 insList.push_back( postFixVec[i]->inumberOfChildren);
992 insList.push_back( nlNodeIdxLindo[
OS_MAX] );
993 insList.push_back( postFixVec[i]->inumberOfChildren);
997 throw ErrorClass(
"Error: OS_PRODUCT operator not supported by Lindo");
1001 insList.push_back( EP_PUSH_NUM );
1003 pos = mapNewNumber.find( numNode->
value);
1004 if(pos == mapNewNumber.end() )
1008 outStr <<
"FOUND A NEW NUMBER " << numNode->
value << endl;
1010 insList.push_back( iNumNonlinearNonz);
1011 mapNewNumber[ numNode->
value] = iNumNonlinearNonz++;
1013 else insList.push_back( pos->second);
1017 insList.push_back( EP_PUSH_VAR );
1019 insList.push_back( varNode->
idx );
1022 if(varNode->
coef != 1)
1025 insList.push_back( EP_PUSH_NUM );
1026 pos = mapNewNumber.find( varNode->
coef);
1027 if(pos == mapNewNumber.end() )
1031 outStr <<
"FOUND A NEW NUMBER " << varNode->
coef << endl;
1033 insList.push_back( iNumNonlinearNonz);
1034 mapNewNumber[ varNode->
coef] = iNumNonlinearNonz++;
1036 else insList.push_back( pos->second);
1037 insList.push_back( EP_MULTIPLY);
1041 insList.push_back( nlNodeIdxLindo[iNodeID] );
1046 if(posTree->first < 0)
1050 paiObjsBegin[ iCountObjs] = iInstListLength;
1051 paiObjsLength[ iCountObjs] = insList.size() - iInstListLength;
1052 iInstListLength = insList.size();
1058 paiConsBegin[ iCountCons] = iInstListLength;
1059 paiConsLength[ iCountCons] = insList.size() - iInstListLength;
1060 paiNonlinearConIndex[ iCountCons] = posTree->first;
1063 outStr <<
"CONSTRAINT ILIST LENGTH = " << iInstListLength << endl;
1065 iInstListLength = insList.size();
1072 padNonlinearNonz =
new double[ iNumNonlinearNonz];
1075 for(pos = mapNewNumber.begin(); pos != mapNewNumber.end(); ++pos)
1077 padNonlinearNonz[ pos->second] = pos->first;
1078 outStr <<
"INDEX = " << pos->second <<
" NUMBER = " << pos->first << endl;
1082 paiInsList =
new int[ iInstListLength];
1083 copy(insList.begin(), insList.end(), paiInsList);
1087 int nLinearz, nAutoDeriv;
1092 LS_IPARAM_NLP_LINEARZ, nLinearz);
1100 LS_IPARAM_NLP_AUTODERIV, nAutoDeriv);
1105 outStr <<
"iNumberOfNonlinearConstraints= " << iNumberOfNonlinearConstraints << std::endl;
1106 outStr <<
"iNumberOfNonlinearObjectives= " << iNumberOfNonlinearObjectives << std::endl;
1107 outStr <<
"iNumberOfNewVariables = " << iNumberOfNewVariables << std::endl;
1108 outStr <<
"iNumNonlinearNonz = " << iNumNonlinearNonz << std::endl;
1109 outStr <<
"piObjSense = " <<
"NULL" << std::endl ;
1110 outStr <<
"pachConType = " <<
"NULL" << std::endl ;
1111 outStr <<
"pachVarType = " <<
"NULL" << std::endl ;
1113 outStr <<
"Here is the instruction list" << std::endl;
1114 for(kl = 0; kl < iInstListLength; kl++)
1116 outStr <<
"instruction list num " << paiInsList[ kl] << endl;
1118 outStr <<
"Number of terms in instruction list " << iInstListLength << std::endl;
1119 outStr <<
"Here are the constraint indices " << std::endl;
1120 for(kl = 0; kl < iNumberOfNonlinearConstraints; kl++)
1122 outStr <<
"con idx " << paiNonlinearConIndex[ kl] << endl;
1124 outStr <<
"Here come the nonlinear nonzeros " << std::endl;
1125 for(kl = 0; kl < iNumNonlinearNonz; kl++)
1127 outStr <<
"nonz value = " << padNonlinearNonz[ kl] << endl;
1129 outStr <<
"padVarval = " <<
"NULL" << std::endl ;
1130 for(kl = 0; kl < iNumberOfNonlinearObjectives; kl++)
1132 outStr <<
"obj inst begin = " << paiObjsBegin[ kl] << endl;
1134 for(kl = 0; kl < iNumberOfNonlinearObjectives; kl++)
1136 outStr <<
"obj inst list length = " << paiObjsLength[ kl] << endl;
1138 for(kl = 0; kl < iNumberOfNonlinearConstraints; kl++)
1140 outStr <<
"constraint inst begin = " << paiConsBegin[ kl] << endl;
1142 for(kl = 0; kl < iNumberOfNonlinearConstraints; kl++)
1144 outStr <<
"constraints inst list length = " << paiConsLength[ kl] << endl;
1146 outStr <<
"padVarLowerBounds = " <<
"NULL" << std::endl ;
1147 outStr <<
"padUpperBounds = " <<
"NULL" << std::endl ;
1151 iNumberOfNonlinearObjectives, iNumberOfNewVariables, iNumNonlinearNonz,
1152 piObjSense, pachConType, pachVarType, paiInsList, iInstListLength, paiNonlinearConIndex,
1153 padNonlinearNonz, padVarval, paiObjsBegin, paiObjsLength, paiConsBegin,
1154 paiConsLength, padVarLowerBounds, padVarUpperBounds);
1159 if( iNumberOfNonlinearConstraints > 0)
delete[] paiConsBegin;
1160 if( iNumberOfNonlinearConstraints > 0)
delete[] paiConsLength;
1161 if( iNumberOfNonlinearObjectives > 0)
delete[] paiObjsBegin;
1162 if( iNumberOfNonlinearObjectives > 0)
delete[] paiObjsLength;
1163 if( iNumberOfNonlinearConstraints > 0)
delete[] paiNonlinearConIndex;
1164 if( iNumNonlinearNonz > 0)
delete[] padNonlinearNonz;
1165 if( iInstListLength > 0)
delete[] paiInsList;
1166 mapNewNumber.clear();
1168 allExpTrees.clear();
1179 osresult->setGeneralStatusType(
"error");
1190 std::ostringstream outStr;
1193 outStr <<
"This is problem: " <<
osinstance->getInstanceName() << endl;
1194 outStr <<
"The problem source is: " <<
osinstance->getInstanceSource() << endl;
1195 outStr <<
"The problem description is: " <<
osinstance->getInstanceDescription() << endl;
1196 outStr <<
"number of variables = " <<
osinstance->getVariableNumber() << endl;
1197 outStr <<
"number of Rows = " <<
osinstance->getConstraintNumber() << endl;
1202 for(i = 0; i <
osinstance->getVariableNumber(); i++)
1204 if(
osinstance->getVariableNames() != NULL) outStr <<
"variable Names " <<
osinstance->getVariableNames()[ i] << endl;
1205 if(
osinstance->getVariableTypes() != NULL) outStr <<
"variable Types " <<
osinstance->getVariableTypes()[ i] << endl;
1206 if(
osinstance->getVariableLowerBounds() != NULL) outStr <<
"variable Lower Bounds " <<
osinstance->getVariableLowerBounds()[ i] << endl;
1207 if(
osinstance->getVariableUpperBounds() != NULL) outStr <<
"variable Upper Bounds " <<
osinstance->getVariableUpperBounds()[i] << endl;
1212 if(
osinstance->getVariableNumber() > 0 ||
osinstance->instanceData->objectives->obj != NULL ||
osinstance->instanceData->objectives->numberOfObjectives > 0)
1214 if(
osinstance->getObjectiveMaxOrMins()[0] ==
"min") outStr <<
"problem is a minimization" << endl;
1215 else outStr <<
"problem is a maximization" << endl;
1216 for(i = 0; i <
osinstance->getVariableNumber(); i++)
1218 outStr <<
"OBJ COEFFICIENT = " <<
osinstance->getDenseObjectiveCoefficients()[0][i] << endl;
1224 for(i = 0; i <
osinstance->getConstraintNumber(); i++)
1226 if(
osinstance->getConstraintNames() != NULL) outStr <<
"row name = " <<
osinstance->getConstraintNames()[i] << endl;
1227 if(
osinstance->getConstraintLowerBounds() != NULL) outStr <<
"row lower bound = " <<
osinstance->getConstraintLowerBounds()[i] << endl;
1228 if(
osinstance->getConstraintUpperBounds() != NULL) outStr <<
"row upper bound = " <<
osinstance->getConstraintUpperBounds()[i] << endl;
1234 outStr <<
"number of nonzeros = " <<
osinstance->getLinearConstraintCoefficientNumber() << endl;
1235 if(
osinstance->getLinearConstraintCoefficientNumber() > 0)
1237 for(i = 0; i <=
osinstance->getVariableNumber(); i++)
1239 outStr <<
"Start Value = " <<
osinstance->getLinearConstraintCoefficientsInColumnMajor()->starts[ i] << endl;
1242 for(i = 0; i <
osinstance->getLinearConstraintCoefficientNumber(); i++)
1244 outStr <<
"Index Value = " <<
osinstance->getLinearConstraintCoefficientsInColumnMajor()->indexes[i] << endl;
1245 outStr <<
"Nonzero Value = " <<
osinstance->getLinearConstraintCoefficientsInColumnMajor()->values[i] << endl;
1249 outStr <<
"number of qterms = " <<
osinstance->getNumberOfQuadraticTerms() << endl;
1250 for(
int i = 0; i <
osinstance->getNumberOfQuadraticTerms(); i++)
1252 outStr <<
"Row Index = " <<
osinstance->getQuadraticTerms()->rowIndexes[i] << endl;
1253 outStr <<
"Var Index 1 = " <<
osinstance->getQuadraticTerms()->varOneIndexes[ i] << endl;
1254 outStr <<
"Var Index 2 = " <<
osinstance->getQuadraticTerms()->varTwoIndexes[ i] << endl;
1255 outStr <<
"Coefficient = " <<
osinstance->getQuadraticTerms()->coefficients[ i] << endl;