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