00001
00017
00018 #include "OSConfig.h"
00019 #include "OSCoinSolver.h"
00020 #include "OSIpoptSolver.h"
00021 #include "OSResult.h"
00022 #include "OSiLReader.h"
00023 #include "OSiLWriter.h"
00024 #include "OSrLReader.h"
00025 #include "OSrLWriter.h"
00026 #include "OSoLWriter.h"
00027 #include "OSInstance.h"
00028 #include "OSFileUtil.h"
00029 #include "OSDefaultSolver.h"
00030 #include "OShL.h"
00031 #include "OSErrorClass.h"
00032 #include "OSmps2osil.h"
00033 #include "OSBase64.h"
00034 #include "OSErrorClass.h"
00035 #include "OSMathUtil.h"
00036 #include "CoinFinite.hpp"
00037
00038 #include "ClpSimplex.hpp"
00039 #include "ClpSolve.hpp"
00040 #include "CbcOrClpParam.hpp"
00041 #include "ClpInterior.hpp"
00042 #include "ClpCholeskyBase.hpp"
00043 #include "ClpQuadraticObjective.hpp"
00044
00045 #include<iostream>
00046 #include <ostream>
00047 #include <sstream>
00048 #include <streambuf>
00049
00050 #include<map>
00051 #include<stdio.h>
00052
00053 using std::cout;
00054 using std::endl;
00055
00056
00057
00058
00059 int main(int argC, char* argV[]){
00060 WindowsErrorPopupBlocker();
00061 FileUtil *fileUtil = NULL;
00062 fileUtil = new FileUtil();
00063
00064
00065
00066 ClpSolve solveOptions;
00067 ClpSolve::SolveType method;
00068 method = ClpSolve::useBarrier;
00069
00070 method = ClpSolve::usePrimal;
00071 solveOptions.setSolveType( method);
00072
00073 ClpSimplex* qpClpModel;
00074
00075 qpClpModel = NULL;
00076
00077 std::cout << "Hello World" << std::endl;
00078
00079
00081
00082 CbcOrClpParam parameters[CBCMAXPARAMETERS];
00083 int numberParameters ;
00084 establishParams(numberParameters, parameters) ;
00085 std::cout << "NUMBER OF PARAMETERS = " << numberParameters<< std::endl;
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00120
00121
00122 qpClpModel = new ClpSimplex();
00123
00124
00125
00126
00127
00128
00129 qpClpModel->setIntParam(ClpMaxNumIteration, 100) ;
00130
00131
00132
00133
00134 std::cout << "Parameter number for: CLP_PARAM_INT_SOLVERLOGLEVEL " << whichParam(CLP_PARAM_INT_SOLVERLOGLEVEL, numberParameters, parameters) << std::endl;
00135 std::cout << "Parameter number for: CLP_PARAM_INT_MAXITERATION " << whichParam(CLP_PARAM_INT_MAXITERATION, numberParameters, parameters) << std::endl;
00136 parameters[ whichParam(CLP_PARAM_INT_SOLVERLOGLEVEL, numberParameters, parameters)].setIntParameter(qpClpModel, 0);
00137 parameters[ whichParam(CLP_PARAM_INT_MAXITERATION, numberParameters, parameters)].setIntParameter(qpClpModel, 10);
00138
00139 std::cout << "CLP_PARAM_INT_SOLVERLOGLEVEL = " << parameters[whichParam(CLP_PARAM_INT_SOLVERLOGLEVEL, numberParameters, parameters)].intValue() << std::endl;
00140
00141 try{
00142
00143 int i;
00144
00145
00146 const char dirsep = CoinFindDirSeparator();
00147 std::string osil;
00148
00149 std::string dataDir;
00150 std::string osilFileName;
00151
00152 dataDir = dirsep == '/' ? "../data/" : "..\\data\\";
00153
00154 DefaultSolver *solver = NULL;
00155
00156 OSiLReader *osilreader = NULL;
00157 OSInstance *osinstance = NULL;
00158 OSoLWriter *osolwriter = NULL;
00159 OSOption* osoption = NULL;
00160
00161
00162
00163
00164 std::cout << std::endl << std::endl;
00165 std::cout << "CLP EXAMPLE" << std::endl;
00166
00167
00168
00169
00170 std::string qpFileName;
00171 qpFileName = dataDir + "parincQuadratic2.osil";
00172
00173 osil = fileUtil->getFileAsString( qpFileName.c_str() );
00174 osilreader = new OSiLReader();
00175 osinstance = osilreader->readOSiL( osil);
00176
00177
00178
00179
00180
00181 osoption = new OSOption();
00187
00188 osoption->setAnotherSolverOption("OsiHintTry","","osi","","OsiHintStrength","");
00189 osoption->setAnotherSolverOption("OsiDoReducePrint","false","osi","","OsiHintParam","");
00190 osolwriter = new OSoLWriter();
00191 std::cout << osolwriter-> writeOSoL( osoption);
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 CoinPackedMatrix* matrix;
00203 bool columnMajor = true;
00204 double maxGap = 0;
00205 matrix = new CoinPackedMatrix(
00206 columnMajor,
00207 columnMajor? osinstance->getConstraintNumber() : osinstance->getVariableNumber(),
00208 columnMajor? osinstance->getVariableNumber() : osinstance->getConstraintNumber(),
00209 osinstance->getLinearConstraintCoefficientNumber(),
00210 columnMajor? osinstance->getLinearConstraintCoefficientsInColumnMajor()->values : osinstance->getLinearConstraintCoefficientsInRowMajor()->values,
00211 columnMajor? osinstance->getLinearConstraintCoefficientsInColumnMajor()->indexes : osinstance->getLinearConstraintCoefficientsInRowMajor()->indexes,
00212 columnMajor? osinstance->getLinearConstraintCoefficientsInColumnMajor()->starts : osinstance->getLinearConstraintCoefficientsInRowMajor()->starts,
00213 0, 0, maxGap );
00214
00215 qpClpModel->setOptimizationDirection( -1);
00216 qpClpModel->loadProblem( *matrix, osinstance->getVariableLowerBounds(),
00217 osinstance->getVariableUpperBounds(),
00218 osinstance->getDenseObjectiveCoefficients()[0],
00219 osinstance->getConstraintLowerBounds(), osinstance->getConstraintUpperBounds()
00220 );
00221
00222
00223
00224
00225 std::map<int, std::map<int, double> > varIndexMap;
00226 std::map<int, std::map<int, double> >::iterator mit1;
00227 std::map<int, double> tmpMap;
00228 std::map<int, double>::iterator mit2;
00229
00230 int numNonz;
00231 numNonz = 0;
00232
00233 QuadraticTerms* quadTerms = NULL;
00234 quadTerms = osinstance->getQuadraticTerms();
00235 int i1;
00236 int j1;
00237 int tmpint;
00238
00239 for(i = 0; i < osinstance->getNumberOfQuadraticTerms(); i++){
00240
00241 if( quadTerms->rowIndexes[ i] == -1){
00242
00243 i1 = quadTerms->varOneIndexes[ i] ;
00244 j1 = quadTerms->varTwoIndexes[ i] ;
00245
00246 if(i1 == j1) quadTerms->coefficients[i] = 2*quadTerms->coefficients[i];
00247
00248
00249
00250
00251 if(j1 < i1){
00252
00253 tmpint = j1;
00254 j1 = i1;
00255 i1 = tmpint;
00256
00257
00258 }
00259
00260 mit1 = varIndexMap.find( i1 );
00261
00262 if( mit1 == varIndexMap.end() ){
00263
00264 tmpMap.insert( std::pair<int, double>( j1,
00265 quadTerms->coefficients[i]) );
00266 numNonz++;
00267
00268 varIndexMap[ i1 ] = tmpMap;
00269
00270 tmpMap.clear();
00271 }else{
00272
00273
00274
00275 mit2 = mit1->second.find( j1) ;
00276
00277 if( mit2 == mit1->second.end() ){
00278
00279 mit1->second.insert( std::pair<int, double>( j1,
00280 quadTerms->coefficients[i]) );
00281 numNonz++;
00282
00283 }else{
00284
00285 mit2->second += quadTerms->coefficients[i];
00286
00287 }
00288
00289 }
00290
00291 }
00292
00293 }
00294
00295 std::cout << "numNonz = " << numNonz << std::endl;
00296
00297
00298 int *start = NULL;
00299 int *idx = NULL;
00300 double *nonz = NULL;
00301
00302 start = new int[ varIndexMap.size() + 1];
00303 idx = new int[ numNonz];
00304 nonz = new double[ numNonz];
00305
00306 int kount;
00307 kount = 0;
00308 numNonz = 0;
00309 start[ kount++] = numNonz;
00310
00311 for( mit1 = varIndexMap.begin(); mit1 != varIndexMap.end(); mit1++){
00312
00313 std::cout << std::endl;
00314 std::cout << "FIRST INDEX = " << mit1->first << std::endl;
00315 for( mit2 = mit1->second.begin(); mit2 != mit1->second.end(); mit2++){
00316 std::cout << "SECOND INDEX = " << mit2->first << std::endl;
00317 std::cout << "COEFFICIENT = " << mit2->second << std::endl;
00318 idx[ numNonz] = mit2->first;
00319
00320 nonz[ numNonz++ ] = mit2->second;
00321 }
00322 start[ kount++] = numNonz ;
00323
00324 }
00325
00343 qpClpModel->loadQuadraticObjective( qpClpModel->numberColumns(), start, idx, nonz);
00344
00345 qpClpModel->writeMps("quad.mps");
00346
00347
00348
00349
00350 ClpCholeskyBase * cholesky = new ClpCholeskyBase();
00351 cholesky->setKKT(true);
00352
00353
00354
00355 qpClpModel->initialSolve( solveOptions);
00356
00357 double *primal;
00358 double *dual;
00359 primal = qpClpModel->primalColumnSolution();
00360 dual = qpClpModel->dualRowSolution();
00361
00362
00363 int numberColumns = qpClpModel->numberColumns();
00364 int numberRows = qpClpModel->numberRows();
00365 for (i = 0; i < numberColumns; i++) {
00366 if (fabs(primal[i]) > 1.0e-8)
00367 printf("%d primal %g\n", i, primal[i]);
00368 }
00369 for (i = 0; i < numberRows; i++) {
00370 if (fabs(dual[i]) > 1.0e-8)
00371 printf("%d dual %g\n", i, dual[i]);
00372 }
00373
00374
00375 std::cout << osinstance->printModel();
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396 dfd
00397
00398
00399
00400 delete osilreader;
00401 osilreader = NULL;
00402 delete solver;
00403 solver = NULL;
00404 delete osoption;
00405 osoption = NULL;
00406 delete osolwriter;
00407 osolwriter = NULL;
00408 delete qpClpModel;
00409 qpClpModel = NULL;
00410
00411
00412
00413
00414
00415
00416
00417 delete fileUtil;
00418 fileUtil = NULL;
00419 return 0;
00420
00421 }
00422 catch(const ErrorClass& eclass){
00423 delete fileUtil;
00424 fileUtil = NULL;
00425 delete qpClpModel;
00426 qpClpModel = NULL;
00427 std::cout << eclass.errormsg << std::endl;
00428 return 0;
00429 }
00430
00431 }
00432