source: trunk/Cbc/src/unitTestClp.cpp @ 312

Last change on this file since 312 was 312, checked in by andreasw, 14 years ago

finished (for now) conversion of Cbc to autotools

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 75.2 KB
Line 
1// Copyright (C) 2002, International Business Machines
2// Corporation and others.  All Rights Reserved.
3
4#include "CoinPragma.hpp"
5#include <cassert>
6#include <cstdio>
7#include <cmath>
8#include <cfloat>
9#include <string>
10#include <iostream>
11
12#include "CoinMpsIO.hpp"
13#include "CoinPackedMatrix.hpp"
14#include "CoinPackedVector.hpp"
15#include "CoinHelperFunctions.hpp"
16#include "CoinTime.hpp"
17#include "CbcModel.hpp"
18#include "OsiClpSolverInterface.hpp"
19#include "ClpFactorization.hpp"
20#include "ClpSimplex.hpp"
21#include "ClpSimplexOther.hpp"
22#include "ClpInterior.hpp"
23#include "ClpLinearObjective.hpp"
24#include "ClpDualRowSteepest.hpp"
25#include "ClpDualRowDantzig.hpp"
26#include "ClpPrimalColumnSteepest.hpp"
27#include "ClpPrimalColumnDantzig.hpp"
28#include "ClpParameters.hpp"
29#include "ClpNetworkMatrix.hpp"
30#include "ClpPlusMinusOneMatrix.hpp"
31#include "MyMessageHandler.hpp"
32#include "MyEventHandler.hpp"
33
34#include "ClpPresolve.hpp"
35#include "Idiot.hpp"
36
37
38//#############################################################################
39
40#ifdef NDEBUG
41#undef NDEBUG
42#endif
43
44// Function Prototypes. Function definitions is in this file.
45void testingMessage( const char * const msg );
46#if UFL_BARRIER
47static int barrierAvailable=1;
48static std::string nameBarrier="barrier-UFL";
49#elif WSSMP_BARRIER
50static int barrierAvailable=2;
51static std::string nameBarrier="barrier-WSSMP";
52#else
53static int barrierAvailable=0;
54static std::string nameBarrier="barrier-slow";
55#endif
56#define NUMBER_ALGORITHMS 12
57// If you just want a subset then set some to 1
58static int switchOff[NUMBER_ALGORITHMS]={0,0,0,0,0,0,0,0,0,0,0,0};
59// shortName - 0 no , 1 yes
60ClpSolve setupForSolve(int algorithm, std::string & nameAlgorithm,
61                       int shortName)
62{
63  ClpSolve solveOptions;
64  /* algorithms are
65     0 barrier
66     1 dual with volumne crash
67     2,3 dual with and without crash
68     4,5 primal with and without
69     6,7 automatic with and without
70     8,9 primal with idiot 1 and 5
71     10,11 primal with 70, dual with volume
72  */
73  switch (algorithm) {
74  case 0:
75    if (shortName)
76      nameAlgorithm="ba";
77    else
78      nameAlgorithm="nameBarrier";
79    solveOptions.setSolveType(ClpSolve::useBarrier);
80    if (barrierAvailable==1) 
81      solveOptions.setSpecialOption(4,4);
82    else if (barrierAvailable==2) 
83      solveOptions.setSpecialOption(4,2);
84    break;
85  case 1:
86#ifdef COIN_HAS_VOL
87    if (shortName)
88      nameAlgorithm="du-vol-50";
89    else
90      nameAlgorithm="dual-volume-50";
91    solveOptions.setSolveType(ClpSolve::useDual);
92    solveOptions.setSpecialOption(0,2,50); // volume
93#else 
94      solveOptions.setSolveType(ClpSolve::notImplemented);
95#endif
96    break;
97  case 2:
98    if (shortName)
99      nameAlgorithm="du-cr";
100    else
101      nameAlgorithm="dual-crash";
102    solveOptions.setSolveType(ClpSolve::useDual);
103    solveOptions.setSpecialOption(0,1);
104    break;
105  case 3:
106    if (shortName)
107      nameAlgorithm="du";
108    else
109      nameAlgorithm="dual";
110    solveOptions.setSolveType(ClpSolve::useDual);
111    break;
112  case 4:
113    if (shortName)
114      nameAlgorithm="pr-cr";
115    else
116      nameAlgorithm="primal-crash";
117    solveOptions.setSolveType(ClpSolve::usePrimal);
118    solveOptions.setSpecialOption(1,1);
119    break;
120  case 5:
121    if (shortName)
122      nameAlgorithm="pr";
123    else
124      nameAlgorithm="primal";
125    solveOptions.setSolveType(ClpSolve::usePrimal);
126    break;
127  case 6:
128    if (shortName)
129      nameAlgorithm="au-cr";
130    else
131      nameAlgorithm="either-crash";
132    solveOptions.setSolveType(ClpSolve::automatic);
133    solveOptions.setSpecialOption(1,1);
134    break;
135  case 7:
136    if (shortName)
137      nameAlgorithm="au";
138    else
139      nameAlgorithm="either";
140    solveOptions.setSolveType(ClpSolve::automatic);
141    break;
142  case 8:
143    if (shortName)
144      nameAlgorithm="pr-id-1";
145    else
146      nameAlgorithm="primal-idiot-1";
147    solveOptions.setSolveType(ClpSolve::usePrimalorSprint);
148    solveOptions.setSpecialOption(1,2,1); // idiot
149    break;
150  case 9:
151    if (shortName)
152      nameAlgorithm="pr-id-5";
153    else
154      nameAlgorithm="primal-idiot-5";
155    solveOptions.setSolveType(ClpSolve::usePrimalorSprint);
156    solveOptions.setSpecialOption(1,2,5); // idiot
157    break;
158  case 10:
159    if (shortName)
160      nameAlgorithm="pr-id-70";
161    else
162      nameAlgorithm="primal-idiot-70";
163    solveOptions.setSolveType(ClpSolve::usePrimalorSprint);
164    solveOptions.setSpecialOption(1,2,70); // idiot
165    break;
166  case 11:
167#ifdef COIN_HAS_VOL
168    if (shortName)
169      nameAlgorithm="du-vol";
170    else
171      nameAlgorithm="dual-volume";
172    solveOptions.setSolveType(ClpSolve::useDual);
173    solveOptions.setSpecialOption(0,2,3000); // volume
174#else 
175      solveOptions.setSolveType(ClpSolve::notImplemented);
176#endif
177    break;
178  default:
179    abort();
180  }
181  if (shortName) {
182    // can switch off
183    if (switchOff[algorithm])
184      solveOptions.setSolveType(ClpSolve::notImplemented);
185  }
186  return solveOptions;
187}
188//----------------------------------------------------------------
189// unitTest [-mpsDir=V1] [-netlibDir=V2] [-test]
190//
191// where:
192//   -mpsDir: directory containing mps test files
193//       Default value V1="../../Data/Sample"   
194//   -netlibDir: directory containing netlib files
195//       Default value V2="../../Data/Netlib"
196//   -test
197//       If specified, then netlib test set run
198//
199// All parameters are optional.
200//----------------------------------------------------------------
201int mainTest (int argc, const char *argv[],int algorithm,
202              ClpSimplex empty, bool doPresolve, int switchOffValue)
203{
204  int i;
205
206  if (switchOffValue>0) {
207    // switch off some
208    int iTest;
209    for (iTest=0;iTest<NUMBER_ALGORITHMS;iTest++) {
210      int bottom = switchOffValue%10;
211      assert (bottom==0||bottom==1);
212      switchOffValue/= 10;
213      switchOff[iTest]=0;
214    }
215  }
216
217  // define valid parameter keywords
218  std::set<std::string> definedKeyWords;
219  definedKeyWords.insert("-mpsDir");
220  definedKeyWords.insert("-netlibDir");
221  definedKeyWords.insert("-netlib");
222
223  // Create a map of parameter keys and associated data
224  std::map<std::string,std::string> parms;
225  for ( i=1; i<argc; i++ ) {
226    std::string parm(argv[i]);
227    std::string key,value;
228    unsigned int  eqPos = parm.find('=');
229
230    // Does parm contain and '='
231    if ( eqPos==std::string::npos ) {
232      //Parm does not contain '='
233      key = parm;
234    }
235    else {
236      key=parm.substr(0,eqPos);
237      value=parm.substr(eqPos+1);
238    }
239
240    // Is specifed key valid?
241    if ( definedKeyWords.find(key) == definedKeyWords.end() ) {
242      // invalid key word.
243      // Write help text
244      std::cerr <<"Undefined parameter \"" <<key <<"\".\n";
245      std::cerr <<"Correct usage: \n";
246      std::cerr <<"  unitTest [-mpsDir=V1] [-netlibDir=V2] [-test[=V3]]\n";
247      std::cerr <<"  where:\n";
248      std::cerr <<"    -mpsDir: directory containing mps test files\n";
249      std::cerr <<"        Default value V1=\"../../Data/Sample\"\n";
250      std::cerr <<"    -netlibDir: directory containing netlib files\n";
251      std::cerr <<"        Default value V2=\"../../Data/Netlib\"\n";
252      std::cerr <<"    -test\n";
253      std::cerr <<"        If specified, then netlib testset run.\n";
254      std::cerr <<"        If V3 then taken as single file\n";
255      return 1;
256    }
257    parms[key]=value;
258  }
259 
260  const char dirsep =  CoinFindDirSeparator();
261  // Set directory containing mps data files.
262  std::string mpsDir;
263  if (parms.find("-mpsDir") != parms.end())
264    mpsDir=parms["-mpsDir"] + dirsep;
265  else 
266    mpsDir = dirsep == '/' ? "../../Data/Sample/" : "..\\..\\Data\\Sample\\";
267 
268  // Set directory containing netlib data files.
269  std::string netlibDir;
270  if (parms.find("-netlibDir") != parms.end())
271    netlibDir=parms["-netlibDir"] + dirsep;
272  else 
273    netlibDir = dirsep == '/' ? "../../Data/Netlib/" : "..\\..\\Data\\Netlib\\";
274  if (!empty.numberRows()) {
275    testingMessage( "Testing ClpSimplex\n" );
276    ClpSimplexUnitTest(mpsDir,netlibDir);
277  }
278  if (parms.find("-netlib") != parms.end()||empty.numberRows())
279  {
280    unsigned int m;
281   
282    // Define test problems:
283    //   mps names,
284    //   maximization or minimization,
285    //   Number of rows and columns in problem, and
286    //   objective function value
287    std::vector<std::string> mpsName;
288    std::vector<bool> min;
289    std::vector<int> nRows;
290    std::vector<int> nCols;
291    std::vector<double> objValue;
292    std::vector<double> objValueTol;
293    // 100 added means no presolve
294    std::vector<int> bestStrategy;
295    if(empty.numberRows()) {
296      std::string alg;
297      for (int iTest=0;iTest<NUMBER_ALGORITHMS;iTest++) {
298        ClpSolve solveOptions=setupForSolve(iTest,alg,0);
299        printf("%d %s ",iTest,alg.c_str());
300        if (switchOff[iTest]) 
301          printf("skipped by user\n");
302        else if(solveOptions.getSolveType()==ClpSolve::notImplemented)
303          printf("skipped as not available\n");
304        else
305          printf("will be tested\n");
306      }
307    }
308    if (!empty.numberRows()) {
309      mpsName.push_back("25fv47");
310      min.push_back(true);
311      nRows.push_back(822);
312      nCols.push_back(1571);
313      objValueTol.push_back(1.E-10);
314      objValue.push_back(5.5018458883E+03);
315      bestStrategy.push_back(0);
316      mpsName.push_back("80bau3b");min.push_back(true);nRows.push_back(2263);nCols.push_back(9799);objValueTol.push_back(1.e-10);objValue.push_back(9.8722419241E+05);bestStrategy.push_back(3);
317      mpsName.push_back("blend");min.push_back(true);nRows.push_back(75);nCols.push_back(83);objValueTol.push_back(1.e-10);objValue.push_back(-3.0812149846e+01);bestStrategy.push_back(3);
318      mpsName.push_back("pilotnov");min.push_back(true);nRows.push_back(976);nCols.push_back(2172);objValueTol.push_back(1.e-10);objValue.push_back(-4.4972761882e+03);bestStrategy.push_back(3);
319      mpsName.push_back("maros-r7");min.push_back(true);nRows.push_back(3137);nCols.push_back(9408);objValueTol.push_back(1.e-10);objValue.push_back(1.4971851665e+06);bestStrategy.push_back(2);
320      mpsName.push_back("pilot");min.push_back(true);nRows.push_back(1442);nCols.push_back(3652);objValueTol.push_back(1.e-5);objValue.push_back(/*-5.5740430007e+02*/-557.48972927292);bestStrategy.push_back(3);
321      mpsName.push_back("pilot4");min.push_back(true);nRows.push_back(411);nCols.push_back(1000);objValueTol.push_back(5.e-5);objValue.push_back(-2.5811392641e+03);bestStrategy.push_back(3);
322      mpsName.push_back("pilot87");min.push_back(true);nRows.push_back(2031);nCols.push_back(4883);objValueTol.push_back(1.e-4);objValue.push_back(3.0171072827e+02);bestStrategy.push_back(0);
323      mpsName.push_back("adlittle");min.push_back(true);nRows.push_back(57);nCols.push_back(97);objValueTol.push_back(1.e-10);objValue.push_back(2.2549496316e+05);bestStrategy.push_back(3);
324      mpsName.push_back("afiro");min.push_back(true);nRows.push_back(28);nCols.push_back(32);objValueTol.push_back(1.e-10);objValue.push_back(-4.6475314286e+02);bestStrategy.push_back(3);
325      mpsName.push_back("agg");min.push_back(true);nRows.push_back(489);nCols.push_back(163);objValueTol.push_back(1.e-10);objValue.push_back(-3.5991767287e+07);bestStrategy.push_back(3);
326      mpsName.push_back("agg2");min.push_back(true);nRows.push_back(517);nCols.push_back(302);objValueTol.push_back(1.e-10);objValue.push_back(-2.0239252356e+07);bestStrategy.push_back(3);
327      mpsName.push_back("agg3");min.push_back(true);nRows.push_back(517);nCols.push_back(302);objValueTol.push_back(1.e-10);objValue.push_back(1.0312115935e+07);bestStrategy.push_back(4);
328      mpsName.push_back("bandm");min.push_back(true);nRows.push_back(306);nCols.push_back(472);objValueTol.push_back(1.e-10);objValue.push_back(-1.5862801845e+02);bestStrategy.push_back(2);
329      mpsName.push_back("beaconfd");min.push_back(true);nRows.push_back(174);nCols.push_back(262);objValueTol.push_back(1.e-10);objValue.push_back(3.3592485807e+04);bestStrategy.push_back(0);
330      mpsName.push_back("bnl1");min.push_back(true);nRows.push_back(644);nCols.push_back(1175);objValueTol.push_back(1.e-10);objValue.push_back(1.9776295615E+03);bestStrategy.push_back(3);
331      mpsName.push_back("bnl2");min.push_back(true);nRows.push_back(2325);nCols.push_back(3489);objValueTol.push_back(1.e-10);objValue.push_back(1.8112365404e+03);bestStrategy.push_back(3);
332      mpsName.push_back("boeing1");min.push_back(true);nRows.push_back(/*351*/352);nCols.push_back(384);objValueTol.push_back(1.e-10);objValue.push_back(-3.3521356751e+02);bestStrategy.push_back(3);
333      mpsName.push_back("boeing2");min.push_back(true);nRows.push_back(167);nCols.push_back(143);objValueTol.push_back(1.e-10);objValue.push_back(-3.1501872802e+02);bestStrategy.push_back(3);
334      mpsName.push_back("bore3d");min.push_back(true);nRows.push_back(234);nCols.push_back(315);objValueTol.push_back(1.e-10);objValue.push_back(1.3730803942e+03);bestStrategy.push_back(3);
335      mpsName.push_back("brandy");min.push_back(true);nRows.push_back(221);nCols.push_back(249);objValueTol.push_back(1.e-10);objValue.push_back(1.5185098965e+03);bestStrategy.push_back(3);
336      mpsName.push_back("capri");min.push_back(true);nRows.push_back(272);nCols.push_back(353);objValueTol.push_back(1.e-10);objValue.push_back(2.6900129138e+03);bestStrategy.push_back(3);
337      mpsName.push_back("cycle");min.push_back(true);nRows.push_back(1904);nCols.push_back(2857);objValueTol.push_back(1.e-9);objValue.push_back(-5.2263930249e+00);bestStrategy.push_back(3);
338      mpsName.push_back("czprob");min.push_back(true);nRows.push_back(930);nCols.push_back(3523);objValueTol.push_back(1.e-10);objValue.push_back(2.1851966989e+06);bestStrategy.push_back(3);
339      mpsName.push_back("d2q06c");min.push_back(true);nRows.push_back(2172);nCols.push_back(5167);objValueTol.push_back(1.e-7);objValue.push_back(122784.21557456);bestStrategy.push_back(0);
340      mpsName.push_back("d6cube");min.push_back(true);nRows.push_back(416);nCols.push_back(6184);objValueTol.push_back(1.e-7);objValue.push_back(3.1549166667e+02);bestStrategy.push_back(3);
341      mpsName.push_back("degen2");min.push_back(true);nRows.push_back(445);nCols.push_back(534);objValueTol.push_back(1.e-10);objValue.push_back(-1.4351780000e+03);bestStrategy.push_back(3);
342      mpsName.push_back("degen3");min.push_back(true);nRows.push_back(1504);nCols.push_back(1818);objValueTol.push_back(1.e-10);objValue.push_back(-9.8729400000e+02);bestStrategy.push_back(2);
343      mpsName.push_back("dfl001");min.push_back(true);nRows.push_back(6072);nCols.push_back(12230);objValueTol.push_back(1.e-5);objValue.push_back(1.1266396047E+07);bestStrategy.push_back(5);
344      mpsName.push_back("e226");min.push_back(true);nRows.push_back(224);nCols.push_back(282);objValueTol.push_back(1.e-10);objValue.push_back(-1.8751929066e+01+7.113);bestStrategy.push_back(3); // The correct answer includes -7.113 term. This is a constant in the objective function. See line 1683 of the mps file.
345      mpsName.push_back("etamacro");min.push_back(true);nRows.push_back(401);nCols.push_back(688);objValueTol.push_back(1.e-6);objValue.push_back(-7.5571521774e+02 );bestStrategy.push_back(3);
346      mpsName.push_back("fffff800");min.push_back(true);nRows.push_back(525);nCols.push_back(854);objValueTol.push_back(1.e-6);objValue.push_back(5.5567961165e+05);bestStrategy.push_back(3);
347      mpsName.push_back("finnis");min.push_back(true);nRows.push_back(498);nCols.push_back(614);objValueTol.push_back(1.e-6);objValue.push_back(1.7279096547e+05);bestStrategy.push_back(3);
348      mpsName.push_back("fit1d");min.push_back(true);nRows.push_back(25);nCols.push_back(1026);objValueTol.push_back(1.e-10);objValue.push_back(-9.1463780924e+03);bestStrategy.push_back(3+100);
349      mpsName.push_back("fit1p");min.push_back(true);nRows.push_back(628);nCols.push_back(1677);objValueTol.push_back(1.e-10);objValue.push_back(9.1463780924e+03);bestStrategy.push_back(5+100);
350      mpsName.push_back("fit2d");min.push_back(true);nRows.push_back(26);nCols.push_back(10500);objValueTol.push_back(1.e-10);objValue.push_back(-6.8464293294e+04);bestStrategy.push_back(3+100);
351      mpsName.push_back("fit2p");min.push_back(true);nRows.push_back(3001);nCols.push_back(13525);objValueTol.push_back(1.e-9);objValue.push_back(6.8464293232e+04);bestStrategy.push_back(5+100);
352      mpsName.push_back("forplan");min.push_back(true);nRows.push_back(162);nCols.push_back(421);objValueTol.push_back(1.e-6);objValue.push_back(-6.6421873953e+02);bestStrategy.push_back(3);
353      mpsName.push_back("ganges");min.push_back(true);nRows.push_back(1310);nCols.push_back(1681);objValueTol.push_back(1.e-5);objValue.push_back(-1.0958636356e+05);bestStrategy.push_back(3);
354      mpsName.push_back("gfrd-pnc");min.push_back(true);nRows.push_back(617);nCols.push_back(1092);objValueTol.push_back(1.e-10);objValue.push_back(6.9022359995e+06);bestStrategy.push_back(3);
355      mpsName.push_back("greenbea");min.push_back(true);nRows.push_back(2393);nCols.push_back(5405);objValueTol.push_back(1.e-10);objValue.push_back(/*-7.2462405908e+07*/-72555248.129846);bestStrategy.push_back(3);
356      mpsName.push_back("greenbeb");min.push_back(true);nRows.push_back(2393);nCols.push_back(5405);objValueTol.push_back(1.e-10);objValue.push_back(/*-4.3021476065e+06*/-4302260.2612066);bestStrategy.push_back(3);
357      mpsName.push_back("grow15");min.push_back(true);nRows.push_back(301);nCols.push_back(645);objValueTol.push_back(1.e-10);objValue.push_back(-1.0687094129e+08);bestStrategy.push_back(4+100);
358      mpsName.push_back("grow22");min.push_back(true);nRows.push_back(441);nCols.push_back(946);objValueTol.push_back(1.e-10);objValue.push_back(-1.6083433648e+08);bestStrategy.push_back(4+100);
359      mpsName.push_back("grow7");min.push_back(true);nRows.push_back(141);nCols.push_back(301);objValueTol.push_back(1.e-10);objValue.push_back(-4.7787811815e+07);bestStrategy.push_back(4+100);
360      mpsName.push_back("israel");min.push_back(true);nRows.push_back(175);nCols.push_back(142);objValueTol.push_back(1.e-10);objValue.push_back(-8.9664482186e+05);bestStrategy.push_back(2);
361      mpsName.push_back("kb2");min.push_back(true);nRows.push_back(44);nCols.push_back(41);objValueTol.push_back(1.e-10);objValue.push_back(-1.7499001299e+03);bestStrategy.push_back(3);
362      mpsName.push_back("lotfi");min.push_back(true);nRows.push_back(154);nCols.push_back(308);objValueTol.push_back(1.e-10);objValue.push_back(-2.5264706062e+01);bestStrategy.push_back(3);
363      mpsName.push_back("maros");min.push_back(true);nRows.push_back(847);nCols.push_back(1443);objValueTol.push_back(1.e-10);objValue.push_back(-5.8063743701e+04);bestStrategy.push_back(3);
364      mpsName.push_back("modszk1");min.push_back(true);nRows.push_back(688);nCols.push_back(1620);objValueTol.push_back(1.e-10);objValue.push_back(3.2061972906e+02);bestStrategy.push_back(3);
365      mpsName.push_back("nesm");min.push_back(true);nRows.push_back(663);nCols.push_back(2923);objValueTol.push_back(1.e-5);objValue.push_back(1.4076073035e+07);bestStrategy.push_back(2);
366      mpsName.push_back("perold");min.push_back(true);nRows.push_back(626);nCols.push_back(1376);objValueTol.push_back(1.e-6);objValue.push_back(-9.3807580773e+03);bestStrategy.push_back(3);
367      //mpsName.push_back("qap12");min.push_back(true);nRows.push_back(3193);nCols.push_back(8856);objValueTol.push_back(1.e-6);objValue.push_back(5.2289435056e+02);bestStrategy.push_back(3);
368      //mpsName.push_back("qap15");min.push_back(true);nRows.push_back(6331);nCols.push_back(22275);objValueTol.push_back(1.e-10);objValue.push_back(1.0409940410e+03);bestStrategy.push_back(3);
369      mpsName.push_back("recipe");min.push_back(true);nRows.push_back(92);nCols.push_back(180);objValueTol.push_back(1.e-10);objValue.push_back(-2.6661600000e+02);bestStrategy.push_back(3);
370      mpsName.push_back("sc105");min.push_back(true);nRows.push_back(106);nCols.push_back(103);objValueTol.push_back(1.e-10);objValue.push_back(-5.2202061212e+01);bestStrategy.push_back(3);
371      mpsName.push_back("sc205");min.push_back(true);nRows.push_back(206);nCols.push_back(203);objValueTol.push_back(1.e-10);objValue.push_back(-5.2202061212e+01);bestStrategy.push_back(3);
372      mpsName.push_back("sc50a");min.push_back(true);nRows.push_back(51);nCols.push_back(48);objValueTol.push_back(1.e-10);objValue.push_back(-6.4575077059e+01);bestStrategy.push_back(3);
373      mpsName.push_back("sc50b");min.push_back(true);nRows.push_back(51);nCols.push_back(48);objValueTol.push_back(1.e-10);objValue.push_back(-7.0000000000e+01);bestStrategy.push_back(3);
374      mpsName.push_back("scagr25");min.push_back(true);nRows.push_back(472);nCols.push_back(500);objValueTol.push_back(1.e-10);objValue.push_back(-1.4753433061e+07);bestStrategy.push_back(3);
375      mpsName.push_back("scagr7");min.push_back(true);nRows.push_back(130);nCols.push_back(140);objValueTol.push_back(1.e-6);objValue.push_back(-2.3313892548e+06);bestStrategy.push_back(3);
376      mpsName.push_back("scfxm1");min.push_back(true);nRows.push_back(331);nCols.push_back(457);objValueTol.push_back(1.e-10);objValue.push_back(1.8416759028e+04);bestStrategy.push_back(3);
377      mpsName.push_back("scfxm2");min.push_back(true);nRows.push_back(661);nCols.push_back(914);objValueTol.push_back(1.e-10);objValue.push_back(3.6660261565e+04);bestStrategy.push_back(3);
378      mpsName.push_back("scfxm3");min.push_back(true);nRows.push_back(991);nCols.push_back(1371);objValueTol.push_back(1.e-10);objValue.push_back(5.4901254550e+04);bestStrategy.push_back(3);
379      mpsName.push_back("scorpion");min.push_back(true);nRows.push_back(389);nCols.push_back(358);objValueTol.push_back(1.e-10);objValue.push_back(1.8781248227e+03);bestStrategy.push_back(3);
380      mpsName.push_back("scrs8");min.push_back(true);nRows.push_back(491);nCols.push_back(1169);objValueTol.push_back(1.e-5);objValue.push_back(9.0429998619e+02);bestStrategy.push_back(2);
381      mpsName.push_back("scsd1");min.push_back(true);nRows.push_back(78);nCols.push_back(760);objValueTol.push_back(1.e-10);objValue.push_back(8.6666666743e+00);bestStrategy.push_back(3+100);
382      mpsName.push_back("scsd6");min.push_back(true);nRows.push_back(148);nCols.push_back(1350);objValueTol.push_back(1.e-10);objValue.push_back(5.0500000078e+01);bestStrategy.push_back(3+100);
383      mpsName.push_back("scsd8");min.push_back(true);nRows.push_back(398);nCols.push_back(2750);objValueTol.push_back(1.e-10);objValue.push_back(9.0499999993e+02);bestStrategy.push_back(1+100);
384      mpsName.push_back("sctap1");min.push_back(true);nRows.push_back(301);nCols.push_back(480);objValueTol.push_back(1.e-10);objValue.push_back(1.4122500000e+03);bestStrategy.push_back(3);
385      mpsName.push_back("sctap2");min.push_back(true);nRows.push_back(1091);nCols.push_back(1880);objValueTol.push_back(1.e-10);objValue.push_back(1.7248071429e+03);bestStrategy.push_back(3);
386      mpsName.push_back("sctap3");min.push_back(true);nRows.push_back(1481);nCols.push_back(2480);objValueTol.push_back(1.e-10);objValue.push_back(1.4240000000e+03);bestStrategy.push_back(3);
387      mpsName.push_back("seba");min.push_back(true);nRows.push_back(516);nCols.push_back(1028);objValueTol.push_back(1.e-10);objValue.push_back(1.5711600000e+04);bestStrategy.push_back(3);
388      mpsName.push_back("share1b");min.push_back(true);nRows.push_back(118);nCols.push_back(225);objValueTol.push_back(1.e-10);objValue.push_back(-7.6589318579e+04);bestStrategy.push_back(3);
389      mpsName.push_back("share2b");min.push_back(true);nRows.push_back(97);nCols.push_back(79);objValueTol.push_back(1.e-10);objValue.push_back(-4.1573224074e+02);bestStrategy.push_back(3);
390      mpsName.push_back("shell");min.push_back(true);nRows.push_back(537);nCols.push_back(1775);objValueTol.push_back(1.e-10);objValue.push_back(1.2088253460e+09);bestStrategy.push_back(3);
391      mpsName.push_back("ship04l");min.push_back(true);nRows.push_back(403);nCols.push_back(2118);objValueTol.push_back(1.e-10);objValue.push_back(1.7933245380e+06);bestStrategy.push_back(3);
392      mpsName.push_back("ship04s");min.push_back(true);nRows.push_back(403);nCols.push_back(1458);objValueTol.push_back(1.e-10);objValue.push_back(1.7987147004e+06);bestStrategy.push_back(3);
393      mpsName.push_back("ship08l");min.push_back(true);nRows.push_back(779);nCols.push_back(4283);objValueTol.push_back(1.e-10);objValue.push_back(1.9090552114e+06);bestStrategy.push_back(3);
394      mpsName.push_back("ship08s");min.push_back(true);nRows.push_back(779);nCols.push_back(2387);objValueTol.push_back(1.e-10);objValue.push_back(1.9200982105e+06);bestStrategy.push_back(2);
395      mpsName.push_back("ship12l");min.push_back(true);nRows.push_back(1152);nCols.push_back(5427);objValueTol.push_back(1.e-10);objValue.push_back(1.4701879193e+06);bestStrategy.push_back(3);
396      mpsName.push_back("ship12s");min.push_back(true);nRows.push_back(1152);nCols.push_back(2763);objValueTol.push_back(1.e-10);objValue.push_back(1.4892361344e+06);bestStrategy.push_back(2);
397      mpsName.push_back("sierra");min.push_back(true);nRows.push_back(1228);nCols.push_back(2036);objValueTol.push_back(1.e-10);objValue.push_back(1.5394362184e+07);bestStrategy.push_back(3);
398      mpsName.push_back("stair");min.push_back(true);nRows.push_back(357);nCols.push_back(467);objValueTol.push_back(1.e-10);objValue.push_back(-2.5126695119e+02);bestStrategy.push_back(3);
399      mpsName.push_back("standata");min.push_back(true);nRows.push_back(360);nCols.push_back(1075);objValueTol.push_back(1.e-10);objValue.push_back(1.2576995000e+03);bestStrategy.push_back(3);
400      //mpsName.push_back("standgub");min.push_back(true);nRows.push_back(362);nCols.push_back(1184);objValueTol.push_back(1.e-10);objValue.push_back(1257.6995); bestStrategy.push_back(3);
401      mpsName.push_back("standmps");min.push_back(true);nRows.push_back(468);nCols.push_back(1075);objValueTol.push_back(1.e-10);objValue.push_back(1.4060175000E+03); bestStrategy.push_back(3);
402      mpsName.push_back("stocfor1");min.push_back(true);nRows.push_back(118);nCols.push_back(111);objValueTol.push_back(1.e-10);objValue.push_back(-4.1131976219E+04);bestStrategy.push_back(3);
403      mpsName.push_back("stocfor2");min.push_back(true);nRows.push_back(2158);nCols.push_back(2031);objValueTol.push_back(1.e-10);objValue.push_back(-3.9024408538e+04);bestStrategy.push_back(3);
404      //mpsName.push_back("stocfor3");min.push_back(true);nRows.push_back(16676);nCols.push_back(15695);objValueTol.push_back(1.e-10);objValue.push_back(-3.9976661576e+04);bestStrategy.push_back(3);
405      //mpsName.push_back("truss");min.push_back(true);nRows.push_back(1001);nCols.push_back(8806);objValueTol.push_back(1.e-10);objValue.push_back(4.5881584719e+05);bestStrategy.push_back(3);
406      mpsName.push_back("tuff");min.push_back(true);nRows.push_back(334);nCols.push_back(587);objValueTol.push_back(1.e-10);objValue.push_back(2.9214776509e-01);bestStrategy.push_back(3);
407      mpsName.push_back("vtpbase");min.push_back(true);nRows.push_back(199);nCols.push_back(203);objValueTol.push_back(1.e-10);objValue.push_back(1.2983146246e+05);bestStrategy.push_back(3);
408      mpsName.push_back("wood1p");min.push_back(true);nRows.push_back(245);nCols.push_back(2594);objValueTol.push_back(5.e-5);objValue.push_back(1.4429024116e+00);bestStrategy.push_back(3);
409      mpsName.push_back("woodw");min.push_back(true);nRows.push_back(1099);nCols.push_back(8405);objValueTol.push_back(1.e-10);objValue.push_back(1.3044763331E+00);bestStrategy.push_back(3);
410    } else {
411      // Just testing one
412      mpsName.push_back(empty.problemName());min.push_back(true);nRows.push_back(-1);
413      nCols.push_back(-1);objValueTol.push_back(1.e-10);
414      objValue.push_back(0.0);bestStrategy.push_back(0);
415      int iTest;
416      std::string alg;
417      for (iTest=0;iTest<NUMBER_ALGORITHMS;iTest++) {
418        ClpSolve solveOptions=setupForSolve(iTest,alg,0);
419        printf("%d %s ",iTest,alg.c_str());
420        if (switchOff[iTest]) 
421          printf("skipped by user\n");
422        else if(solveOptions.getSolveType()==ClpSolve::notImplemented)
423          printf("skipped as not available\n");
424        else
425          printf("will be tested\n");
426      }
427    }
428
429    double timeTaken =0.0;
430    if( !barrierAvailable)
431      switchOff[0]=1;
432  // Loop once for each Mps File
433    for (m=0; m<mpsName.size(); m++ ) {
434      std::cerr <<"  processing mps file: " <<mpsName[m] 
435                <<" (" <<m+1 <<" out of " <<mpsName.size() <<")" <<std::endl;
436
437      ClpSimplex solutionBase=empty;
438      std::string fn = netlibDir+mpsName[m];
439      if (!empty.numberRows()||algorithm<6) {
440        // Read data mps file,
441        CoinMpsIO mps;
442        mps.readMps(fn.c_str(),"mps");
443        solutionBase.loadProblem(*mps.getMatrixByCol(),mps.getColLower(),
444                                 mps.getColUpper(),
445                                 mps.getObjCoefficients(),
446                                 mps.getRowLower(),mps.getRowUpper());
447       
448        solutionBase.setDblParam(ClpObjOffset,mps.objectiveOffset());
449      } 
450     
451      // Runs through strategies
452      if (algorithm==6||algorithm==7) {
453        // algorithms tested are at top of file
454        double testTime[NUMBER_ALGORITHMS];
455        std::string alg[NUMBER_ALGORITHMS];
456        int iTest;
457        for (iTest=0;iTest<NUMBER_ALGORITHMS;iTest++) {
458          ClpSolve solveOptions=setupForSolve(iTest,alg[iTest],1);
459          if (solveOptions.getSolveType()!=ClpSolve::notImplemented) {
460            double time1 = CoinCpuTime();
461            ClpSimplex solution=solutionBase;
462            if (solution.maximumSeconds()<0.0)
463              solution.setMaximumSeconds(120.0);
464            solution.initialSolve(solveOptions);
465            double time2 = CoinCpuTime()-time1;
466            testTime[iTest]=time2;
467            printf("Took %g seconds - status %d\n",time2,solution.problemStatus());
468            if (solution.problemStatus()) 
469              testTime[iTest]=1.0e20;
470          } else {
471            testTime[iTest]=1.0e30;
472          }
473        }
474        int iBest=-1;
475        double dBest=1.0e10;
476        printf("%s",fn.c_str());
477        for (iTest=0;iTest<NUMBER_ALGORITHMS;iTest++) {
478          if (testTime[iTest]<1.0e30) {
479            printf(" %s %g",
480                   alg[iTest].c_str(),testTime[iTest]);
481            if (testTime[iTest]<dBest) {
482              dBest=testTime[iTest];
483              iBest=iTest;
484            }
485          }
486        }
487        printf("\n");
488        if (iBest>=0)
489          printf("Best strategy for %s is %s (%d) which takes %g seconds\n",
490                 fn.c_str(),alg[iBest].c_str(),iBest,testTime[iBest]);
491        else
492          printf("No strategy finished in time limit\n");
493        continue;
494      }
495      double time1 = CoinCpuTime();
496      ClpSimplex solution=solutionBase;
497#if 0
498      solution.setOptimizationDirection(-1);
499      {
500        int j;
501        double * obj = solution.objective();
502        int n=solution.numberColumns();
503        for (j=0;j<n;j++)
504          obj[j] *= -1.0;
505      }
506#endif
507      ClpSolve::SolveType method;
508      ClpSolve::PresolveType presolveType;
509      ClpSolve solveOptions;
510      if (doPresolve)
511        presolveType=ClpSolve::presolveOn;
512      else
513        presolveType=ClpSolve::presolveOff;
514      solveOptions.setPresolveType(presolveType,5);
515      std::string nameAlgorithm;
516      if (algorithm!=5) {
517        if (algorithm==0) {
518          method=ClpSolve::useDual;
519          nameAlgorithm="dual";
520        } else if (algorithm==1) {
521          method=ClpSolve::usePrimalorSprint;
522          nameAlgorithm="primal";
523        } else if (algorithm==3) {
524          method=ClpSolve::automatic;
525          nameAlgorithm="either";
526        } else {
527          nameAlgorithm="barrier-slow";
528#ifdef UFL_BARRIER
529          solveOptions.setSpecialOption(4,4);
530          nameAlgorithm="barrier-UFL";
531#endif
532#ifdef WSSMP_BARRIER
533          solveOptions.setSpecialOption(4,2);
534          nameAlgorithm="barrier-WSSMP";
535#endif
536          method = ClpSolve::useBarrier;
537        }
538        solveOptions.setSolveType(method);
539      } else {
540        int iAlg = bestStrategy[m];
541        int presolveOff=iAlg/100;
542        iAlg=iAlg % 100;
543        if( !barrierAvailable&&iAlg==0)
544          if (nRows[m]!=2172)
545            iAlg = 5; // try primal
546          else
547            iAlg=3; // d2q06c
548        solveOptions=setupForSolve(iAlg,nameAlgorithm,0);
549        if (presolveOff)
550          solveOptions.setPresolveType(ClpSolve::presolveOff);
551      }
552      solution.initialSolve(solveOptions);
553      double time2 = CoinCpuTime()-time1;
554      timeTaken += time2;
555      printf("%s took %g seconds using algorithm %s\n",fn.c_str(),time2,nameAlgorithm.c_str());
556      // Test objective solution value
557      {
558        double soln = solution.objectiveValue();
559        CoinRelFltEq eq(objValueTol[m]);
560        std::cerr <<soln <<",  " <<objValue[m] <<" diff "<<
561          soln-objValue[m]<<std::endl;
562        if(!eq(soln,objValue[m]))
563          printf("** difference fails\n");
564      }
565    }
566    printf("Total time %g seconds\n",timeTaken);
567  }
568  else {
569    testingMessage( "***Skipped Testing on netlib    ***\n" );
570    testingMessage( "***use -netlib to test class***\n" );
571  }
572 
573  testingMessage( "All tests completed successfully\n" );
574  return 0;
575}
576
577 
578// Display message on stdout and stderr
579void testingMessage( const char * const msg )
580{
581  std::cerr <<msg;
582  //cout <<endl <<"*****************************************"
583  //     <<endl <<msg <<endl;
584}
585
586//--------------------------------------------------------------------------
587// test factorization methods and simplex method and simple barrier
588void
589ClpSimplexUnitTest(const std::string & mpsDir,
590                   const std::string & netlibDir)
591{
592 
593  CoinRelFltEq eq(0.000001);
594
595  {
596    ClpSimplex solution;
597 
598    // matrix data
599    //deliberate hiccup of 2 between 0 and 1
600    CoinBigIndex start[5]={0,4,7,8,9};
601    int length[5]={2,3,1,1,1};
602    int rows[11]={0,2,-1,-1,0,1,2,0,1,2};
603    double elements[11]={7.0,2.0,1.0e10,1.0e10,-2.0,1.0,-2.0,1,1,1};
604    CoinPackedMatrix matrix(true,3,5,8,elements,rows,start,length);
605   
606    // rim data
607    double objective[7]={-4.0,1.0,0.0,0.0,0.0,0.0,0.0};
608    double rowLower[5]={14.0,3.0,3.0,1.0e10,1.0e10};
609    double rowUpper[5]={14.0,3.0,3.0,-1.0e10,-1.0e10};
610    double colLower[7]={0.0,0.0,0.0,0.0,0.0,0.0,0.0};
611    double colUpper[7]={100.0,100.0,100.0,100.0,100.0,100.0,100.0};
612   
613    // basis 1
614    int rowBasis1[3]={-1,-1,-1};
615    int colBasis1[5]={1,1,-1,-1,1};
616    solution.loadProblem(matrix,colLower,colUpper,objective,
617                         rowLower,rowUpper);
618    int i;
619    solution.createStatus();
620    for (i=0;i<3;i++) {
621      if (rowBasis1[i]<0) {
622        solution.setRowStatus(i,ClpSimplex::atLowerBound);
623      } else {
624        solution.setRowStatus(i,ClpSimplex::basic);
625      }
626    }
627    for (i=0;i<5;i++) {
628      if (colBasis1[i]<0) {
629        solution.setColumnStatus(i,ClpSimplex::atLowerBound);
630      } else {
631        solution.setColumnStatus(i,ClpSimplex::basic);
632      }
633    }
634    solution.setLogLevel(3+4+8+16+32);
635    solution.primal();
636    for (i=0;i<3;i++) {
637      if (rowBasis1[i]<0) {
638        solution.setRowStatus(i,ClpSimplex::atLowerBound);
639      } else {
640        solution.setRowStatus(i,ClpSimplex::basic);
641      }
642    }
643    for (i=0;i<5;i++) {
644      if (colBasis1[i]<0) {
645        solution.setColumnStatus(i,ClpSimplex::atLowerBound);
646      } else {
647        solution.setColumnStatus(i,ClpSimplex::basic);
648      }
649    }
650    // intricate stuff does not work with scaling
651    solution.scaling(0);
652    int returnCode = solution.factorize ( );
653    assert(!returnCode);
654    const double * colsol = solution.primalColumnSolution();
655    const double * rowsol = solution.primalRowSolution();
656    solution.getSolution(rowsol,colsol);
657    double colsol1[5]={20.0/7.0,3.0,0.0,0.0,23.0/7.0};
658    for (i=0;i<5;i++) {
659      assert(eq(colsol[i],colsol1[i]));
660    }
661    // now feed in again without actually doing factorization
662    ClpFactorization factorization2 = *solution.factorization();
663    ClpSimplex solution2 = solution;
664    solution2.setFactorization(factorization2);
665    solution2.createStatus();
666    for (i=0;i<3;i++) {
667      if (rowBasis1[i]<0) {
668        solution2.setRowStatus(i,ClpSimplex::atLowerBound);
669      } else {
670        solution2.setRowStatus(i,ClpSimplex::basic);
671      }
672    }
673    for (i=0;i<5;i++) {
674      if (colBasis1[i]<0) {
675        solution2.setColumnStatus(i,ClpSimplex::atLowerBound);
676      } else {
677        solution2.setColumnStatus(i,ClpSimplex::basic);
678      }
679    }
680    // intricate stuff does not work with scaling
681    solution2.scaling(0);
682    solution2.getSolution(rowsol,colsol);
683    colsol = solution2.primalColumnSolution();
684    rowsol = solution2.primalRowSolution();
685    for (i=0;i<5;i++) {
686      assert(eq(colsol[i],colsol1[i]));
687    }
688    solution2.setDualBound(0.1);
689    solution2.dual();
690    objective[2]=-1.0;
691    objective[3]=-0.5;
692    objective[4]=10.0;
693    solution.dual();
694    for (i=0;i<3;i++) {
695      rowLower[i]=-1.0e20;
696      colUpper[i+2]=0.0;
697    }
698    solution.setLogLevel(3);
699    solution.dual();
700    double rowObjective[]={1.0,0.5,-10.0};
701    solution.loadProblem(matrix,colLower,colUpper,objective,
702                         rowLower,rowUpper,rowObjective);
703    solution.dual();
704    solution.loadProblem(matrix,colLower,colUpper,objective,
705                         rowLower,rowUpper,rowObjective);
706    solution.primal();
707  }
708#ifndef COIN_NO_CLP_MESSAGE
709  {   
710    CoinMpsIO m;
711    std::string fn = mpsDir+"exmip1";
712    m.readMps(fn.c_str(),"mps");
713    ClpSimplex solution;
714    solution.loadProblem(*m.getMatrixByCol(),m.getColLower(),m.getColUpper(),
715                         m.getObjCoefficients(),
716                         m.getRowLower(),m.getRowUpper());
717    solution.dual();
718    // Test event handling
719    MyEventHandler handler;
720    solution.passInEventHandler(&handler);
721    int numberRows=solution.numberRows();
722    // make sure values pass has something to do
723    for (int i=0;i<numberRows;i++)
724      solution.setRowStatus(i,ClpSimplex::basic);
725    solution.primal(1);
726    assert (solution.secondaryStatus()==102); // Came out at end of pass
727  }
728  // Test Message handler
729  {   
730    CoinMpsIO m;
731    std::string fn = mpsDir+"exmip1";
732    //fn = "Test/subGams4";
733    m.readMps(fn.c_str(),"mps");
734    ClpSimplex model;
735    model.loadProblem(*m.getMatrixByCol(),m.getColLower(),m.getColUpper(),
736                         m.getObjCoefficients(),
737                         m.getRowLower(),m.getRowUpper());
738    // Message handler
739    MyMessageHandler messageHandler(&model);
740    std::cout<<"Testing derived message handler"<<std::endl;
741    model.passInMessageHandler(&messageHandler);
742    model.messagesPointer()->setDetailMessage(1,102);
743    model.setFactorizationFrequency(10);
744    model.primal();
745
746    // Write saved solutions
747    int nc = model.getNumCols();
748    int s; 
749    std::deque<StdVectorDouble> fep = messageHandler.getFeasibleExtremePoints();
750    int numSavedSolutions = fep.size();
751    for ( s=0; s<numSavedSolutions; ++s ) {
752      const StdVectorDouble & solnVec = fep[s];
753      for ( int c=0; c<nc; ++c ) {
754        if (fabs(solnVec[c])>1.0e-8)
755          std::cout <<"Saved Solution: " <<s <<" ColNum: " <<c <<" Value: " <<solnVec[c] <<std::endl;
756      }
757    }
758    // Solve again without scaling
759    // and maximize then minimize
760    messageHandler.clearFeasibleExtremePoints();
761    model.scaling(0);
762    model.setOptimizationDirection(-1);
763    model.primal();
764    model.setOptimizationDirection(1);
765    model.primal();
766    fep = messageHandler.getFeasibleExtremePoints();
767    numSavedSolutions = fep.size();
768    for ( s=0; s<numSavedSolutions; ++s ) {
769      const StdVectorDouble & solnVec = fep[s];
770      for ( int c=0; c<nc; ++c ) {
771        if (fabs(solnVec[c])>1.0e-8)
772          std::cout <<"Saved Solution: " <<s <<" ColNum: " <<c <<" Value: " <<solnVec[c] <<std::endl;
773      }
774    }
775  }
776#endif
777  // Test dual ranging
778  {   
779    CoinMpsIO m;
780    std::string fn = mpsDir+"exmip1";
781    m.readMps(fn.c_str(),"mps");
782    ClpSimplex model;
783    model.loadProblem(*m.getMatrixByCol(),m.getColLower(),m.getColUpper(),
784                         m.getObjCoefficients(),
785                         m.getRowLower(),m.getRowUpper());
786    model.primal();
787    int which[13] = {0,1,2,3,4,5,6,7,8,9,10,11,12};
788    double costIncrease[13];
789    int sequenceIncrease[13];
790    double costDecrease[13];
791    int sequenceDecrease[13];
792    // ranging
793    model.dualRanging(13,which,costIncrease,sequenceIncrease,
794                      costDecrease,sequenceDecrease);
795    int i;
796    for ( i=0;i<13;i++)
797      printf("%d increase %g %d, decrease %g %d\n",
798             i,costIncrease[i],sequenceIncrease[i],
799             costDecrease[i],sequenceDecrease[i]);
800    assert (fabs(costDecrease[3])<1.0e-4);
801    assert (fabs(costIncrease[7]-1.0)<1.0e-4);
802    model.setOptimizationDirection(-1);
803    {
804      int j;
805      double * obj = model.objective();
806      int n=model.numberColumns();
807      for (j=0;j<n;j++) 
808        obj[j] *= -1.0;
809    }
810    double costIncrease2[13];
811    int sequenceIncrease2[13];
812    double costDecrease2[13];
813    int sequenceDecrease2[13];
814    // ranging
815    model.dualRanging(13,which,costIncrease2,sequenceIncrease2,
816                      costDecrease2,sequenceDecrease2);
817    for (i=0;i<13;i++) {
818      assert (fabs(costIncrease[i]-costDecrease2[i])<1.0e-6);
819      assert (fabs(costDecrease[i]-costIncrease2[i])<1.0e-6);
820      assert (sequenceIncrease[i]==sequenceDecrease2[i]);
821      assert (sequenceDecrease[i]==sequenceIncrease2[i]);
822    }
823    // Now delete all rows and see what happens
824    model.deleteRows(model.numberRows(),which);
825    model.primal();
826    // ranging
827    if (!model.dualRanging(8,which,costIncrease,sequenceIncrease,
828                           costDecrease,sequenceDecrease)) {
829      for (i=0;i<8;i++) {
830        printf("%d increase %g %d, decrease %g %d\n",
831               i,costIncrease[i],sequenceIncrease[i],
832               costDecrease[i],sequenceDecrease[i]);
833      }
834    }
835  }
836  // Test primal ranging
837  {   
838    CoinMpsIO m;
839    std::string fn = mpsDir+"exmip1";
840    m.readMps(fn.c_str(),"mps");
841    ClpSimplex model;
842    model.loadProblem(*m.getMatrixByCol(),m.getColLower(),m.getColUpper(),
843                         m.getObjCoefficients(),
844                         m.getRowLower(),m.getRowUpper());
845    model.primal();
846    int which[13] = {0,1,2,3,4,5,6,7,8,9,10,11,12};
847    double valueIncrease[13];
848    int sequenceIncrease[13];
849    double valueDecrease[13];
850    int sequenceDecrease[13];
851    // ranging
852    model.primalRanging(13,which,valueIncrease,sequenceIncrease,
853                      valueDecrease,sequenceDecrease);
854    int i;
855    for ( i=0;i<13;i++)
856      printf("%d increase %g %d, decrease %g %d\n",
857             i,valueIncrease[i],sequenceIncrease[i],
858             valueDecrease[i],sequenceDecrease[i]);
859    assert (fabs(valueDecrease[3]-0.642857)<1.0e-4);
860    assert (fabs(valueDecrease[8]-2.95113)<1.0e-4);
861    // Test parametrics
862    ClpSimplexOther * model2 = (ClpSimplexOther *) (&model);
863    double rhs[]={ 1.0,2.0,3.0,4.0,5.0};
864    double endingTheta=1.0;
865    model2->scaling(0);
866    model2->setLogLevel(63);
867    model2->parametrics(0.0,endingTheta,0.1,
868                        NULL,NULL,rhs,rhs,NULL);
869  }
870  // Test binv etc
871  {   
872    /*
873       Wolsey : Page 130
874       max 4x1 -  x2
875       7x1 - 2x2    <= 14
876       x2    <= 3
877       2x1 - 2x2    <= 3
878       x1 in Z+, x2 >= 0
879
880       note slacks are -1 in Clp so signs may be different
881    */
882   
883    int n_cols = 2;
884    int n_rows = 3;
885   
886    double obj[2] = {-4.0, 1.0};
887    double collb[2] = {0.0, 0.0};
888    double colub[2] = {COIN_DBL_MAX, COIN_DBL_MAX};
889    double rowlb[3] = {-COIN_DBL_MAX, -COIN_DBL_MAX, -COIN_DBL_MAX};
890    double rowub[3] = {14.0, 3.0, 3.0};
891   
892    int rowIndices[5] =  {0,     2,    0,    1,    2};
893    int colIndices[5] =  {0,     0,    1,    1,    1};
894    double elements[5] = {7.0, 2.0, -2.0,  1.0, -2.0};
895    CoinPackedMatrix M(true, rowIndices, colIndices, elements, 5);
896
897    ClpSimplex model;
898    model.loadProblem(M, collb, colub, obj, rowlb, rowub);
899    model.dual(0,1); // keep factorization
900   
901    //check that the tableau matches wolsey (B-1 A)
902    // slacks in second part of binvA
903    double * binvA = (double*) malloc((n_cols+n_rows) * sizeof(double));
904   
905    printf("B-1 A by row\n");
906    int i;
907    for( i = 0; i < n_rows; i++){
908      model.getBInvARow(i, binvA,binvA+n_cols);
909      printf("row: %d -> ",i);
910      for(int j=0; j < n_cols+n_rows; j++){
911        printf("%g, ", binvA[j]);
912      }
913      printf("\n");
914    }
915    // See if can re-use factorization AND arrays
916    model.primal(0,3+4); // keep factorization
917    // And do by column
918    printf("B-1 A by column\n");
919    for( i = 0; i < n_rows+n_cols; i++){
920      model.getBInvACol(i, binvA);
921      printf("column: %d -> ",i);
922      for(int j=0; j < n_rows; j++){
923        printf("%g, ", binvA[j]);
924      }
925      printf("\n");
926    }
927    free(binvA);
928    model.setColUpper(1,2.0);
929    model.dual(0,2+4); // use factorization and arrays
930    model.dual(0,2); // hopefully will not use factorization
931    model.primal(0,3+4); // keep factorization
932    // but say basis has changed
933    model.setWhatsChanged(model.whatsChanged()&(~512));
934    model.dual(0,2); // hopefully will not use factorization
935  }
936  // test steepest edge
937  {   
938    CoinMpsIO m;
939    std::string fn = netlibDir+"finnis";
940    int returnCode = m.readMps(fn.c_str(),"mps");
941    if (returnCode) {
942      // probable cause is that gz not there
943      fprintf(stderr,"Unable to open finnis.mps in Data/Netlib!\n");
944      fprintf(stderr,"Most probable cause is finnis.mps is gzipped i.e. finnis.mps.gz and libz has not been activated\n");
945      fprintf(stderr,"Either gunzip files or edit Makefiles/Makefile.location to get libz\n");
946      exit(999);
947    }
948    ClpModel model;
949    model.loadProblem(*m.getMatrixByCol(),m.getColLower(),
950                    m.getColUpper(),
951                    m.getObjCoefficients(),
952                    m.getRowLower(),m.getRowUpper());
953    ClpSimplex solution(model);
954
955    solution.scaling(1); 
956    solution.setDualBound(1.0e8);
957    //solution.factorization()->maximumPivots(1);
958    //solution.setLogLevel(3);
959    solution.setDualTolerance(1.0e-7);
960    // set objective sense,
961    ClpDualRowSteepest steep;
962    solution.setDualRowPivotAlgorithm(steep);
963    solution.setDblParam(ClpObjOffset,m.objectiveOffset());
964    solution.dual();
965  }
966  // test normal solution
967  {   
968    CoinMpsIO m;
969    std::string fn = netlibDir+"afiro";
970    m.readMps(fn.c_str(),"mps");
971    ClpSimplex solution;
972    ClpModel model;
973    // do twice - without and with scaling
974    int iPass;
975    for (iPass=0;iPass<2;iPass++) {
976      // explicit row objective for testing
977      int nr = m.getNumRows();
978      double * rowObj = new double[nr];
979      CoinFillN(rowObj,nr,0.0);
980      model.loadProblem(*m.getMatrixByCol(),m.getColLower(),m.getColUpper(),
981                      m.getObjCoefficients(),
982                      m.getRowLower(),m.getRowUpper(),rowObj);
983      delete [] rowObj;
984      solution = ClpSimplex(model);
985      if (iPass) {
986        solution.scaling();
987      }
988      solution.dual();
989      solution.dual();
990      // test optimal
991      assert (solution.status()==0);
992      int numberColumns = solution.numberColumns();
993      int numberRows = solution.numberRows();
994      CoinPackedVector colsol(numberColumns,solution.primalColumnSolution());
995      double * objective = solution.objective();
996      double objValue = colsol.dotProduct(objective);
997      CoinRelFltEq eq(1.0e-8);
998      assert(eq(objValue,-4.6475314286e+02));
999      // Test auxiliary model
1000      //solution.scaling(0);
1001      solution.auxiliaryModel(63-2); // bounds may change
1002      solution.dual();
1003      solution.primal();
1004      solution.allSlackBasis();
1005      solution.dual();
1006      assert(eq(solution.objectiveValue(),-4.6475314286e+02));
1007      solution.auxiliaryModel(-1);
1008      solution.dual();
1009      assert(eq(solution.objectiveValue(),-4.6475314286e+02));
1010      double * lower = solution.columnLower();
1011      double * upper = solution.columnUpper();
1012      double * sol = solution.primalColumnSolution();
1013      double * result = new double[numberColumns];
1014      CoinFillN ( result, numberColumns,0.0);
1015      solution.matrix()->transposeTimes(solution.dualRowSolution(), result);
1016      int iRow , iColumn;
1017      // see if feasible and dual feasible
1018      for (iColumn=0;iColumn<numberColumns;iColumn++) {
1019        double value = sol[iColumn];
1020        assert(value<upper[iColumn]+1.0e-8);
1021        assert(value>lower[iColumn]-1.0e-8);
1022        value = objective[iColumn]-result[iColumn];
1023        assert (value>-1.0e-5);
1024        if (sol[iColumn]>1.0e-5)
1025          assert (value<1.0e-5);
1026      }
1027      delete [] result;
1028      result = new double[numberRows];
1029      CoinFillN ( result, numberRows,0.0);
1030      solution.matrix()->times(colsol, result);
1031      lower = solution.rowLower();
1032      upper = solution.rowUpper();
1033      sol = solution.primalRowSolution();
1034      for (iRow=0;iRow<numberRows;iRow++) {
1035        double value = result[iRow];
1036        assert(eq(value,sol[iRow]));
1037        assert(value<upper[iRow]+1.0e-8);
1038        assert(value>lower[iRow]-1.0e-8);
1039      }
1040      delete [] result;
1041      // test row objective
1042      double * rowObjective = solution.rowObjective();
1043      CoinDisjointCopyN(solution.dualRowSolution(),numberRows,rowObjective);
1044      CoinDisjointCopyN(solution.dualColumnSolution(),numberColumns,objective);
1045      // this sets up all slack basis
1046      solution.createStatus();
1047      solution.dual();
1048      CoinFillN(rowObjective,numberRows,0.0);
1049      CoinDisjointCopyN(m.getObjCoefficients(),numberColumns,objective);
1050      solution.dual();
1051    }
1052  }
1053  // test unbounded
1054  {   
1055    CoinMpsIO m;
1056    std::string fn = netlibDir+"brandy";
1057    m.readMps(fn.c_str(),"mps");
1058    ClpSimplex solution;
1059    // do twice - without and with scaling
1060    int iPass;
1061    for (iPass=0;iPass<2;iPass++) {
1062      solution.loadProblem(*m.getMatrixByCol(),m.getColLower(),m.getColUpper(),
1063                      m.getObjCoefficients(),
1064                      m.getRowLower(),m.getRowUpper());
1065      if (iPass)
1066        solution.scaling();
1067      solution.setOptimizationDirection(-1);
1068      // test unbounded and ray
1069#ifdef DUAL
1070      solution.setDualBound(100.0);
1071      solution.dual();
1072#else
1073      solution.primal();
1074#endif
1075      assert (solution.status()==2);
1076      int numberColumns = solution.numberColumns();
1077      int numberRows = solution.numberRows();
1078      double * lower = solution.columnLower();
1079      double * upper = solution.columnUpper();
1080      double * sol = solution.primalColumnSolution();
1081      double * ray = solution.unboundedRay();
1082      double * objective = solution.objective();
1083      double objChange=0.0;
1084      int iRow , iColumn;
1085      // make sure feasible and columns form ray
1086      for (iColumn=0;iColumn<numberColumns;iColumn++) {
1087        double value = sol[iColumn];
1088        assert(value<upper[iColumn]+1.0e-8);
1089        assert(value>lower[iColumn]-1.0e-8);
1090        value = ray[iColumn];
1091        if (value>0.0)
1092          assert(upper[iColumn]>1.0e30);
1093        else if (value<0.0)
1094          assert(lower[iColumn]<-1.0e30);
1095        objChange += value*objective[iColumn];
1096      }
1097      // make sure increasing objective
1098      assert(objChange>0.0);
1099      double * result = new double[numberRows];
1100      CoinFillN ( result, numberRows,0.0);
1101      solution.matrix()->times(sol, result);
1102      lower = solution.rowLower();
1103      upper = solution.rowUpper();
1104      sol = solution.primalRowSolution();
1105      for (iRow=0;iRow<numberRows;iRow++) {
1106        double value = result[iRow];
1107        assert(eq(value,sol[iRow]));
1108        assert(value<upper[iRow]+2.0e-8);
1109        assert(value>lower[iRow]-2.0e-8);
1110      }
1111      CoinFillN ( result, numberRows,0.0);
1112      solution.matrix()->times(ray, result);
1113      // there may be small differences (especially if scaled)
1114      for (iRow=0;iRow<numberRows;iRow++) {
1115        double value = result[iRow];
1116        if (value>1.0e-8)
1117          assert(upper[iRow]>1.0e30);
1118        else if (value<-1.0e-8)
1119          assert(lower[iRow]<-1.0e30);
1120      }
1121      delete [] result;
1122      delete [] ray;
1123    }
1124  }
1125  // test infeasible
1126  {   
1127    CoinMpsIO m;
1128    std::string fn = netlibDir+"brandy";
1129    m.readMps(fn.c_str(),"mps");
1130    ClpSimplex solution;
1131    // do twice - without and with scaling
1132    int iPass;
1133    for (iPass=0;iPass<2;iPass++) {
1134      solution.loadProblem(*m.getMatrixByCol(),m.getColLower(),m.getColUpper(),
1135                      m.getObjCoefficients(),
1136                      m.getRowLower(),m.getRowUpper());
1137      if (iPass)
1138        solution.scaling();
1139      // test infeasible and ray
1140      solution.columnUpper()[0]=0.0;
1141#ifdef DUAL
1142      solution.setDualBound(100.0);
1143      solution.dual();
1144#else
1145      solution.primal();
1146#endif
1147      assert (solution.status()==1);
1148      int numberColumns = solution.numberColumns();
1149      int numberRows = solution.numberRows();
1150      double * lower = solution.rowLower();
1151      double * upper = solution.rowUpper();
1152      double * ray = solution.infeasibilityRay();
1153      assert(ray);
1154      // construct proof of infeasibility
1155      int iRow , iColumn;
1156      double lo=0.0,up=0.0;
1157      int nl=0,nu=0;
1158      for (iRow=0;iRow<numberRows;iRow++) {
1159        if (lower[iRow]>-1.0e20) {
1160          lo += ray[iRow]*lower[iRow];
1161        } else {
1162          if (ray[iRow]>1.0e-8) 
1163            nl++;
1164        }
1165        if (upper[iRow]<1.0e20) {
1166          up += ray[iRow]*upper[iRow];
1167        } else {
1168          if (ray[iRow]>1.0e-8) 
1169            nu++;
1170        }
1171      }
1172      if (nl)
1173        lo=-1.0e100;
1174      if (nu)
1175        up=1.0e100;
1176      double * result = new double[numberColumns];
1177      double lo2=0.0,up2=0.0;
1178      CoinFillN ( result, numberColumns,0.0);
1179      solution.matrix()->transposeTimes(ray, result);
1180      lower = solution.columnLower();
1181      upper = solution.columnUpper();
1182      nl=nu=0;
1183      for (iColumn=0;iColumn<numberColumns;iColumn++) {
1184        if (result[iColumn]>1.0e-8) {
1185          if (lower[iColumn]>-1.0e20)
1186            lo2 += result[iColumn]*lower[iColumn];
1187          else
1188            nl++;
1189          if (upper[iColumn]<1.0e20)
1190            up2 += result[iColumn]*upper[iColumn];
1191          else
1192            nu++;
1193        } else if (result[iColumn]<-1.0e-8) {
1194          if (lower[iColumn]>-1.0e20)
1195            up2 += result[iColumn]*lower[iColumn];
1196          else
1197            nu++;
1198          if (upper[iColumn]<1.0e20)
1199            lo2 += result[iColumn]*upper[iColumn];
1200          else
1201            nl++;
1202        }
1203      }
1204      if (nl)
1205        lo2=-1.0e100;
1206      if (nu)
1207        up2=1.0e100;
1208      // make sure inconsistency
1209      assert(lo2>up||up2<lo);
1210      delete [] result;
1211      delete [] ray;
1212    }
1213  }
1214  // test delete and add
1215  {   
1216    CoinMpsIO m;
1217    std::string fn = netlibDir+"brandy";
1218    m.readMps(fn.c_str(),"mps");
1219    ClpSimplex solution;
1220    solution.loadProblem(*m.getMatrixByCol(),m.getColLower(),m.getColUpper(),
1221                         m.getObjCoefficients(),
1222                         m.getRowLower(),m.getRowUpper());
1223    solution.dual();
1224    CoinRelFltEq eq(1.0e-8);
1225    assert(eq(solution.objectiveValue(),1.5185098965e+03));
1226
1227    int numberColumns = solution.numberColumns();
1228    int numberRows = solution.numberRows();
1229    double * saveObj = new double [numberColumns];
1230    double * saveLower = new double[numberRows+numberColumns];
1231    double * saveUpper = new double[numberRows+numberColumns];
1232    int * which = new int [numberRows+numberColumns];
1233
1234    int numberElements = m.getMatrixByCol()->getNumElements();
1235    int * starts = new int[numberRows+numberColumns];
1236    int * index = new int[numberElements];
1237    double * element = new double[numberElements];
1238
1239    const CoinBigIndex * startM;
1240    const int * lengthM;
1241    const int * indexM;
1242    const double * elementM;
1243
1244    int n,nel;
1245
1246    // delete non basic columns
1247    n=0;
1248    nel=0;
1249    int iRow , iColumn;
1250    const double * lower = m.getColLower();
1251    const double * upper = m.getColUpper();
1252    const double * objective = m.getObjCoefficients();
1253    startM = m.getMatrixByCol()->getVectorStarts();
1254    lengthM = m.getMatrixByCol()->getVectorLengths();
1255    indexM = m.getMatrixByCol()->getIndices();
1256    elementM = m.getMatrixByCol()->getElements();
1257    starts[0]=0;
1258    for (iColumn=0;iColumn<numberColumns;iColumn++) {
1259      if (solution.getColumnStatus(iColumn)!=ClpSimplex::basic) {
1260        saveObj[n]=objective[iColumn];
1261        saveLower[n]=lower[iColumn];
1262        saveUpper[n]=upper[iColumn];
1263        int j;
1264        for (j=startM[iColumn];j<startM[iColumn]+lengthM[iColumn];j++) {
1265          index[nel]=indexM[j];
1266          element[nel++]=elementM[j];
1267        }
1268        which[n++]=iColumn;
1269        starts[n]=nel;
1270      }
1271    }
1272    solution.deleteColumns(n,which);
1273    solution.dual();
1274    // Put back
1275    solution.addColumns(n,saveLower,saveUpper,saveObj,
1276                        starts,index,element);
1277    solution.dual();
1278    assert(eq(solution.objectiveValue(),1.5185098965e+03));
1279    // Delete all columns and add back
1280    n=0;
1281    nel=0;
1282    starts[0]=0;
1283    lower = m.getColLower();
1284    upper = m.getColUpper();
1285    objective = m.getObjCoefficients();
1286    for (iColumn=0;iColumn<numberColumns;iColumn++) {
1287      saveObj[n]=objective[iColumn];
1288      saveLower[n]=lower[iColumn];
1289      saveUpper[n]=upper[iColumn];
1290      int j;
1291      for (j=startM[iColumn];j<startM[iColumn]+lengthM[iColumn];j++) {
1292        index[nel]=indexM[j];
1293        element[nel++]=elementM[j];
1294      }
1295      which[n++]=iColumn;
1296      starts[n]=nel;
1297    }
1298    solution.deleteColumns(n,which);
1299    solution.dual();
1300    // Put back
1301    solution.addColumns(n,saveLower,saveUpper,saveObj,
1302                        starts,index,element);
1303    solution.dual();
1304    assert(eq(solution.objectiveValue(),1.5185098965e+03));
1305
1306    // reload with original
1307    solution.loadProblem(*m.getMatrixByCol(),m.getColLower(),m.getColUpper(),
1308                         m.getObjCoefficients(),
1309                         m.getRowLower(),m.getRowUpper());
1310    // delete half rows
1311    n=0;
1312    nel=0;
1313    lower = m.getRowLower();
1314    upper = m.getRowUpper();
1315    startM = m.getMatrixByRow()->getVectorStarts();
1316    lengthM = m.getMatrixByRow()->getVectorLengths();
1317    indexM = m.getMatrixByRow()->getIndices();
1318    elementM = m.getMatrixByRow()->getElements();
1319    starts[0]=0;
1320    for (iRow=0;iRow<numberRows;iRow++) {
1321      if ((iRow&1)==0) {
1322        saveLower[n]=lower[iRow];
1323        saveUpper[n]=upper[iRow];
1324        int j;
1325        for (j=startM[iRow];j<startM[iRow]+lengthM[iRow];j++) {
1326          index[nel]=indexM[j];
1327          element[nel++]=elementM[j];
1328        }
1329        which[n++]=iRow;
1330        starts[n]=nel;
1331      }
1332    }
1333    solution.deleteRows(n,which);
1334    solution.dual();
1335    // Put back
1336    solution.addRows(n,saveLower,saveUpper,
1337                        starts,index,element);
1338    solution.dual();
1339    assert(eq(solution.objectiveValue(),1.5185098965e+03));
1340    solution.writeMps("yy.mps");
1341    // Delete all rows
1342    n=0;
1343    nel=0;
1344    lower = m.getRowLower();
1345    upper = m.getRowUpper();
1346    starts[0]=0;
1347    for (iRow=0;iRow<numberRows;iRow++) {
1348      saveLower[n]=lower[iRow];
1349      saveUpper[n]=upper[iRow];
1350      int j;
1351      for (j=startM[iRow];j<startM[iRow]+lengthM[iRow];j++) {
1352        index[nel]=indexM[j];
1353        element[nel++]=elementM[j];
1354      }
1355      which[n++]=iRow;
1356      starts[n]=nel;
1357    }
1358    solution.deleteRows(n,which);
1359    solution.dual();
1360    // Put back
1361    solution.addRows(n,saveLower,saveUpper,
1362                        starts,index,element);
1363    solution.dual();
1364    solution.writeMps("xx.mps");
1365    assert(eq(solution.objectiveValue(),1.5185098965e+03));
1366    // Zero out status array to give some interest
1367    memset(solution.statusArray()+numberColumns,0,numberRows);
1368    solution.primal(1);
1369    assert(eq(solution.objectiveValue(),1.5185098965e+03));
1370    // Delete all columns and rows
1371    n=0;
1372    for (iColumn=0;iColumn<numberColumns;iColumn++) {
1373      which[n++]=iColumn;
1374      starts[n]=nel;
1375    }
1376    solution.deleteColumns(n,which);
1377    n=0;
1378    for (iRow=0;iRow<numberRows;iRow++) {
1379      which[n++]=iRow;
1380      starts[n]=nel;
1381    }
1382    solution.deleteRows(n,which);
1383
1384    delete [] saveObj;
1385    delete [] saveLower;
1386    delete [] saveUpper;
1387    delete [] which;
1388    delete [] starts;
1389    delete [] index;
1390    delete [] element;
1391  }
1392#if 1
1393  // Test barrier
1394  {
1395    CoinMpsIO m;
1396    std::string fn = mpsDir+"exmip1";
1397    m.readMps(fn.c_str(),"mps");
1398    ClpInterior solution;
1399    solution.loadProblem(*m.getMatrixByCol(),m.getColLower(),m.getColUpper(),
1400                         m.getObjCoefficients(),
1401                         m.getRowLower(),m.getRowUpper());
1402    solution.primalDual();
1403  }
1404#endif
1405  // test network
1406#define QUADRATIC
1407  if (1) {   
1408    std::string fn = mpsDir+"input.130";
1409    int numberColumns;
1410    int numberRows;
1411   
1412    FILE * fp = fopen(fn.c_str(),"r");
1413    if (!fp) {
1414      // Try in Samples
1415      fn = "Samples/input.130";
1416      fp = fopen(fn.c_str(),"r");
1417    }
1418    if (!fp) {
1419      fprintf(stderr,"Unable to open file input.130 in mpsDir or Samples directory\n");
1420    } else {
1421      int problem;
1422      char temp[100];
1423      // read and skip
1424      fscanf(fp,"%s",temp);
1425      assert (!strcmp(temp,"BEGIN"));
1426      fscanf(fp,"%*s %*s %d %d %*s %*s %d %*s",&problem, &numberRows, 
1427             &numberColumns);
1428      // scan down to SUPPLY
1429      while (fgets(temp,100,fp)) {
1430        if (!strncmp(temp,"SUPPLY",6))
1431          break;
1432      }
1433      if (strncmp(temp,"SUPPLY",6)) {
1434        fprintf(stderr,"Unable to find SUPPLY\n");
1435        exit(2);
1436      }
1437      // get space for rhs
1438      double * lower = new double[numberRows];
1439      double * upper = new double[numberRows];
1440      int i;
1441      for (i=0;i<numberRows;i++) {
1442        lower[i]=0.0;
1443        upper[i]=0.0;
1444      }
1445      // ***** Remember to convert to C notation
1446      while (fgets(temp,100,fp)) {
1447        int row;
1448        int value;
1449        if (!strncmp(temp,"ARCS",4))
1450          break;
1451        sscanf(temp,"%d %d",&row,&value);
1452        upper[row-1]=-value;
1453        lower[row-1]=-value;
1454      }
1455      if (strncmp(temp,"ARCS",4)) {
1456        fprintf(stderr,"Unable to find ARCS\n");
1457        exit(2);
1458      }
1459      // number of columns may be underestimate
1460      int * head = new int[2*numberColumns];
1461      int * tail = new int[2*numberColumns];
1462      int * ub = new int[2*numberColumns];
1463      int * cost = new int[2*numberColumns];
1464      // ***** Remember to convert to C notation
1465      numberColumns=0;
1466      while (fgets(temp,100,fp)) {
1467        int iHead;
1468        int iTail;
1469        int iUb;
1470        int iCost;
1471        if (!strncmp(temp,"DEMAND",6))
1472          break;
1473        sscanf(temp,"%d %d %d %d",&iHead,&iTail,&iCost,&iUb);
1474        iHead--;
1475        iTail--;
1476        head[numberColumns]=iHead;
1477        tail[numberColumns]=iTail;
1478        ub[numberColumns]=iUb;
1479        cost[numberColumns]=iCost;
1480        numberColumns++;
1481      }
1482      if (strncmp(temp,"DEMAND",6)) {
1483        fprintf(stderr,"Unable to find DEMAND\n");
1484        exit(2);
1485      }
1486      // ***** Remember to convert to C notation
1487      while (fgets(temp,100,fp)) {
1488        int row;
1489        int value;
1490        if (!strncmp(temp,"END",3))
1491          break;
1492        sscanf(temp,"%d %d",&row,&value);
1493        upper[row-1]=value;
1494        lower[row-1]=value;
1495      }
1496      if (strncmp(temp,"END",3)) {
1497        fprintf(stderr,"Unable to find END\n");
1498        exit(2);
1499      }
1500      printf("Problem %d has %d rows and %d columns\n",problem,
1501             numberRows,numberColumns);
1502      fclose(fp);
1503      ClpSimplex  model;
1504      // now build model
1505     
1506      double * objective =new double[numberColumns];
1507      double * lowerColumn = new double[numberColumns];
1508      double * upperColumn = new double[numberColumns];
1509     
1510      double * element = new double [2*numberColumns];
1511      CoinBigIndex * start = new CoinBigIndex [numberColumns+1];
1512      int * row = new int[2*numberColumns];
1513      start[numberColumns]=2*numberColumns;
1514      for (i=0;i<numberColumns;i++) {
1515        start[i]=2*i;
1516        element[2*i]=-1.0;
1517        element[2*i+1]=1.0;
1518        row[2*i]=head[i];
1519        row[2*i+1]=tail[i];
1520        lowerColumn[i]=0.0;
1521        upperColumn[i]=ub[i];
1522        objective[i]=cost[i];
1523      }
1524      // Create Packed Matrix
1525      CoinPackedMatrix matrix;
1526      int * lengths = NULL;
1527      matrix.assignMatrix(true,numberRows,numberColumns,
1528                          2*numberColumns,element,row,start,lengths);
1529      // load model
1530      model.loadProblem(matrix,
1531                        lowerColumn,upperColumn,objective,
1532                        lower,upper);
1533      model.factorization()->maximumPivots(200+model.numberRows()/100);
1534      model.createStatus();
1535      double time1 = CoinCpuTime();
1536      model.dual();
1537      std::cout<<"Network problem, ClpPackedMatrix took "<<CoinCpuTime()-time1<<" seconds"<<std::endl;
1538      ClpPlusMinusOneMatrix * plusMinus = new ClpPlusMinusOneMatrix(matrix);
1539      assert (plusMinus->getIndices()); // would be zero if not +- one
1540      //ClpPlusMinusOneMatrix *plusminus_matrix;
1541
1542      //plusminus_matrix = new ClpPlusMinusOneMatrix;
1543
1544      //plusminus_matrix->passInCopy(numberRows, numberColumns, true, plusMinus->getMutableIndices(),
1545      //                         plusMinus->startPositive(),plusMinus->startNegative());
1546      model.loadProblem(*plusMinus,
1547                        lowerColumn,upperColumn,objective,
1548                        lower,upper);
1549      //model.replaceMatrix( plusminus_matrix , true);
1550      delete plusMinus;
1551      //model.createStatus();
1552      //model.initialSolve();
1553      //model.writeMps("xx.mps");
1554     
1555      model.factorization()->maximumPivots(200+model.numberRows()/100);
1556      model.createStatus();
1557      time1 = CoinCpuTime();
1558      model.dual();
1559      std::cout<<"Network problem, ClpPlusMinusOneMatrix took "<<CoinCpuTime()-time1<<" seconds"<<std::endl;
1560      ClpNetworkMatrix network(numberColumns,head,tail);
1561      model.loadProblem(network,
1562                        lowerColumn,upperColumn,objective,
1563                        lower,upper);
1564     
1565      model.factorization()->maximumPivots(200+model.numberRows()/100);
1566      model.createStatus();
1567      time1 = CoinCpuTime();
1568      model.dual();
1569      std::cout<<"Network problem, ClpNetworkMatrix took "<<CoinCpuTime()-time1<<" seconds"<<std::endl;
1570      delete [] lower;
1571      delete [] upper;
1572      delete [] head;
1573      delete [] tail;
1574      delete [] ub;
1575      delete [] cost;
1576      delete [] objective;
1577      delete [] lowerColumn;
1578      delete [] upperColumn;
1579    }
1580  }
1581#ifdef QUADRATIC
1582  // Test quadratic to solve linear
1583  if (1) {   
1584    CoinMpsIO m;
1585    std::string fn = mpsDir+"exmip1";
1586    m.readMps(fn.c_str(),"mps");
1587    ClpSimplex solution;
1588    solution.loadProblem(*m.getMatrixByCol(),m.getColLower(),m.getColUpper(),
1589                         m.getObjCoefficients(),
1590                         m.getRowLower(),m.getRowUpper());
1591    //solution.dual();
1592    // get quadratic part
1593    int numberColumns=solution.numberColumns();
1594    int * start=new int [numberColumns+1];
1595    int * column = new int[numberColumns];
1596    double * element = new double[numberColumns];
1597    int i;
1598    start[0]=0;
1599    int n=0;
1600    int kk=numberColumns-1;
1601    int kk2=numberColumns-1;
1602    for (i=0;i<numberColumns;i++) {
1603      if (i>=kk) {
1604        column[n]=i;
1605        if (i>=kk2)
1606          element[n]=1.0e-1;
1607        else
1608          element[n]=0.0;
1609        n++;
1610      }
1611      start[i+1]=n;
1612    }
1613    // Load up objective
1614    solution.loadQuadraticObjective(numberColumns,start,column,element);
1615    delete [] start;
1616    delete [] column;
1617    delete [] element;
1618    //solution.quadraticSLP(50,1.0e-4);
1619    double objValue = solution.getObjValue();
1620    CoinRelFltEq eq(1.0e-4);
1621    //assert(eq(objValue,820.0));
1622    //solution.setLogLevel(63);
1623    solution.primal();
1624    int numberRows = solution.numberRows();
1625
1626    double * rowPrimal = solution.primalRowSolution();
1627    double * rowDual = solution.dualRowSolution();
1628    double * rowLower = solution.rowLower();
1629    double * rowUpper = solution.rowUpper();
1630   
1631    int iRow;
1632    printf("Rows\n");
1633    for (iRow=0;iRow<numberRows;iRow++) {
1634      printf("%d primal %g dual %g low %g up %g\n",
1635             iRow,rowPrimal[iRow],rowDual[iRow],
1636             rowLower[iRow],rowUpper[iRow]);
1637    }
1638    double * columnPrimal = solution.primalColumnSolution();
1639    double * columnDual = solution.dualColumnSolution();
1640    double * columnLower = solution.columnLower();
1641    double * columnUpper = solution.columnUpper();
1642    objValue = solution.getObjValue();
1643    int iColumn;
1644    printf("Columns\n");
1645    for (iColumn=0;iColumn<numberColumns;iColumn++) {
1646      printf("%d primal %g dual %g low %g up %g\n",
1647             iColumn,columnPrimal[iColumn],columnDual[iColumn],
1648             columnLower[iColumn],columnUpper[iColumn]);
1649    }
1650    //assert(eq(objValue,3.2368421));
1651    //exit(77);
1652  }
1653  // Test quadratic
1654  if (1) {   
1655    CoinMpsIO m;
1656    std::string fn = mpsDir+"share2qp";
1657    //fn = "share2qpb";
1658    m.readMps(fn.c_str(),"mps");
1659    ClpSimplex model;
1660    model.loadProblem(*m.getMatrixByCol(),m.getColLower(),m.getColUpper(),
1661                         m.getObjCoefficients(),
1662                         m.getRowLower(),m.getRowUpper());
1663    model.dual();
1664    // get quadratic part
1665    int * start=NULL;
1666    int * column = NULL;
1667    double * element = NULL;
1668    m.readQuadraticMps(NULL,start,column,element,2);
1669    int column2[200];
1670    double element2[200];
1671    int start2[80];
1672    int j;
1673    start2[0]=0;
1674    int nel=0;
1675    bool good=false;
1676    for (j=0;j<79;j++) {
1677      if (start[j]==start[j+1]) {
1678        column2[nel]=j;
1679        element2[nel]=0.0;
1680        nel++;
1681      } else {
1682        int i;
1683        for (i=start[j];i<start[j+1];i++) {
1684          column2[nel]=column[i];
1685          element2[nel++]=element[i];
1686        }
1687      }
1688      start2[j+1]=nel;
1689    }
1690    // Load up objective
1691    if (good)
1692      model.loadQuadraticObjective(model.numberColumns(),start2,column2,element2);
1693    else
1694      model.loadQuadraticObjective(model.numberColumns(),start,column,element);
1695    delete [] start;
1696    delete [] column;
1697    delete [] element;
1698    int numberColumns=model.numberColumns();
1699#if 0
1700    model.nonlinearSLP(50,1.0e-4);
1701#else
1702    // Get feasible
1703    ClpObjective * saveObjective = model.objectiveAsObject()->clone();
1704    ClpLinearObjective zeroObjective(NULL,numberColumns);
1705    model.setObjective(&zeroObjective);
1706    model.dual();
1707    model.setObjective(saveObjective);
1708    delete saveObjective;
1709#endif
1710    //model.setLogLevel(63);
1711    //exit(77);
1712    model.setFactorizationFrequency(10);
1713    model.primal();
1714    double objValue = model.getObjValue();
1715    CoinRelFltEq eq(1.0e-4);
1716    assert(eq(objValue,-400.92));
1717  }
1718  if (0) {   
1719    CoinMpsIO m;
1720    std::string fn = "./beale";
1721    //fn = "./jensen";
1722    m.readMps(fn.c_str(),"mps");
1723    ClpSimplex solution;
1724    solution.loadProblem(*m.getMatrixByCol(),m.getColLower(),m.getColUpper(),
1725                         m.getObjCoefficients(),
1726                         m.getRowLower(),m.getRowUpper());
1727    solution.setDblParam(ClpObjOffset,m.objectiveOffset());
1728    solution.dual();
1729    // get quadratic part
1730    int * start=NULL;
1731    int * column = NULL;
1732    double * element = NULL;
1733    m.readQuadraticMps(NULL,start,column,element,2);
1734    // Load up objective
1735    solution.loadQuadraticObjective(solution.numberColumns(),start,column,element);
1736    delete [] start;
1737    delete [] column;
1738    delete [] element;
1739    solution.primal(1);
1740    solution.nonlinearSLP(50,1.0e-4);
1741    double objValue = solution.getObjValue();
1742    CoinRelFltEq eq(1.0e-4);
1743    assert(eq(objValue,0.5));
1744    solution.primal();
1745    objValue = solution.getObjValue();
1746    assert(eq(objValue,0.5));
1747  }
1748#endif 
1749}
1750void CbcClpUnitTest (const CbcModel & saveModel)
1751{
1752  unsigned int m ;
1753  // See if files exist
1754  FILE * fp;
1755  bool doTest=false;
1756  const char dirsep =  CoinFindDirSeparator();
1757 
1758  // Set directory containing miplib data files.
1759  std::string miplibDir;
1760  miplibDir = dirsep == '/' ? "../../Data/miplib3/" : "..\\..\\Data\\miplib3\\";
1761  std::string test1 = miplibDir +"p0033";
1762  fp=fopen(test1.c_str(),"r");
1763  if (fp) {
1764    doTest=true;
1765    fclose(fp);
1766  }
1767#ifdef COIN_USE_ZLIB
1768  test1 += ".gz";
1769  fp=fopen(test1.c_str(),"r");
1770  if (fp) {
1771    doTest=true;
1772    fclose(fp);
1773  }
1774#endif
1775  if (!doTest)
1776    return;
1777  /*
1778    Vectors to hold test problem names and characteristics. The objective value
1779    after optimization (objValue) must agree to the specified tolerance
1780    (objValueTol).
1781  */
1782  std::vector<std::string> mpsName ;
1783  std::vector<int> nRows ;
1784  std::vector<int> nCols ;
1785  std::vector<double> objValueC ;
1786  std::vector<double> objValue ;
1787  std::vector<int> strategy ;
1788  /*
1789    And a macro to make the vector creation marginally readable.
1790  */
1791#define PUSH_MPS(zz_mpsName_zz,\
1792                 zz_nRows_zz,zz_nCols_zz,zz_objValue_zz,zz_objValueC_zz, \
1793                 zz_strategy_zz) \
1794  mpsName.push_back(zz_mpsName_zz) ; \
1795  nRows.push_back(zz_nRows_zz) ; \
1796  nCols.push_back(zz_nCols_zz) ; \
1797  objValueC.push_back(zz_objValueC_zz) ; \
1798  strategy.push_back(zz_strategy_zz) ; \
1799  objValue.push_back(zz_objValue_zz) ;
1800 
1801  /*
1802    Load up the problem vector. Note that the row counts here include the
1803    objective function.
1804   
1805  */
1806  // 0 for no test, 1 for some, 2 for many, 3 for all
1807#define HOWMANY 2
1808#if HOWMANY
1809#if HOWMANY>1
1810  PUSH_MPS("10teams",230,2025,924,917,7);
1811#endif
1812  PUSH_MPS("air03",124,10757,340160,338864.25,7);
1813#if HOWMANY==3
1814  PUSH_MPS("air04",823,8904,56137,55535.436,8);
1815  PUSH_MPS("air05",426,7195,26374,25877.609,8);
1816#endif
1817  //    PUSH_MPS("arki001",1048,1388,7580813.0459,7579599.80787,7);
1818  PUSH_MPS("bell3a",123,133,878430.32,862578.64,7);
1819#if HOWMANY>1
1820  PUSH_MPS("bell5",91,104,8966406.49,8608417.95,7);
1821#endif
1822  PUSH_MPS("blend2",274,353,7.598985,6.9156751140,7);
1823#if HOWMANY>1
1824  PUSH_MPS("cap6000",2176,6000,-2451377,-2451537.325,7);
1825#endif
1826  //    PUSH_MPS("dano3mip",3202,13873,728.1111,576.23162474,7);
1827  //PUSH_MPS("danoint",664,521,65.67,62.637280418,7);
1828  PUSH_MPS("dcmulti",290,548,188182,183975.5397,7);
1829  PUSH_MPS("dsbmip",1182,1886,-305.19817501,-305.19817501,7);
1830  PUSH_MPS("egout",98,141,568.101,149.589,7);
1831  PUSH_MPS("enigma",21,100,0.0,0.0,7);
1832#if HOWMANY==3
1833  PUSH_MPS("fast0507",507,63009,174,172.14556668,7);
1834#endif
1835  PUSH_MPS("fiber",363,1298,405935.18000,156082.51759,7);
1836#if HOWMANY>1
1837  PUSH_MPS("fixnet6",478,878,3983,1200.88,7);
1838#endif
1839  PUSH_MPS("flugpl",18,18,1201500,1167185.7,7);
1840  PUSH_MPS("gen",780,870,112313,112130.0,7);
1841#if HOWMANY>1
1842  PUSH_MPS("gesa2",1392,1224,25779856.372,25476489.678,7);
1843  PUSH_MPS("gesa2_o",1248,1224,25779856.372,25476489.678,7);
1844#endif
1845  PUSH_MPS("gesa3",1368,1152,27991042.648,27833632.451,7);
1846  PUSH_MPS("gesa3_o",1224,1152,27991042.648,27833632.451,7);
1847  PUSH_MPS("gt2",29,188,21166.000,13460.233074,7);
1848#if HOWMANY==3
1849  PUSH_MPS("harp2",112,2993,-73899798.00,-74353341.502,7);
1850#endif
1851  PUSH_MPS("khb05250",101,1350,106940226,95919464.0,7);
1852#if HOWMANY>1
1853  PUSH_MPS("l152lav",97,1989,4722,4656.36,7);
1854#endif
1855  PUSH_MPS("lseu",28,89,1120,834.68,7);
1856  PUSH_MPS("misc03",96,160,3360,1910.,7);
1857  PUSH_MPS("misc06",820,1808,12850.8607,12841.6,7);
1858#if HOWMANY>1
1859  PUSH_MPS("misc07",212,260,2810,1415.0,7);
1860  PUSH_MPS("mitre",2054,10724,115155,114740.5184,7);
1861#endif
1862  PUSH_MPS("mod008",6,319,307,290.9,7);
1863  PUSH_MPS("mod010",146,2655,6548,6532.08,7);
1864#if HOWMANY==3
1865  PUSH_MPS("mod011",4480,10958,-54558535,-62121982.55,7);
1866  PUSH_MPS("modglob",291,422,20740508,20430947.,7);
1867  PUSH_MPS("noswot",182,128,-43,-43.0,7);
1868#endif
1869#if HOWMANY>1
1870  PUSH_MPS("nw04",36,87482,16862,16310.66667,7);
1871#endif
1872  PUSH_MPS("p0033",16,33,3089,2520.57,7);
1873  PUSH_MPS("p0201",133,201,7615,6875.0,7);
1874  PUSH_MPS("p0282",241,282,258411,176867.50,7);
1875  PUSH_MPS("p0548",176,548,8691,315.29,7);
1876  PUSH_MPS("p2756",755,2756,3124,2688.75,7);
1877#if HOWMANY==3
1878  PUSH_MPS("pk1",45,86,11.0,0.0,7);
1879#endif
1880#if HOWMANY>1
1881  PUSH_MPS("pp08a",136,240,7350.0,2748.3452381,7);
1882  PUSH_MPS("pp08aCUTS",246,240,7350.0,5480.6061563,7);
1883#endif
1884#if HOWMANY==3
1885  PUSH_MPS("qiu",1192,840,-132.873137,-931.638857,7);
1886#endif
1887  PUSH_MPS("qnet1",503,1541,16029.692681,14274.102667,7);
1888  PUSH_MPS("qnet1_o",456,1541,16029.692681,12095.571667,7);
1889  PUSH_MPS("rentacar",6803,9557,30356761,28806137.644,7);
1890  PUSH_MPS("rgn",24,180,82.1999,48.7999,7);
1891#if HOWMANY==3
1892  PUSH_MPS("rout",291,556,1077.56,981.86428571,7);
1893  PUSH_MPS("set1ch",492,712,54537.75,32007.73,7);
1894#endif
1895  //    PUSH_MPS("seymour",4944,1372,423,403.84647413,7);
1896  PUSH_MPS("stein27",118,27,18,13.0,7);
1897#if HOWMANY>1
1898  PUSH_MPS("stein45",331,45,30,22.0,7);
1899#endif
1900  PUSH_MPS("vpm1",234,378,20,15.4167,7);
1901  PUSH_MPS("vpm2",234,378,13.75,9.8892645972,7);
1902#endif
1903#undef PUSH_MPS
1904   
1905  int numProbSolved = 0;
1906  double timeTaken=0.0;
1907 
1908  /*
1909    Open the main loop to step through the MPS problems.
1910  */
1911  for (m = 0 ; m < mpsName.size() ; m++) {
1912    std::cerr << "  processing mps file: " << mpsName[m] 
1913              << " (" << m+1 << " out of " << mpsName.size() << ")" << std::endl ;
1914    /*
1915      Stage 1: Read the MPS
1916      and make sure the size of the constraint matrix is correct.
1917    */
1918    CbcModel * model = new CbcModel(saveModel);
1919     
1920    std::string fn = miplibDir+mpsName[m] ;
1921    model->solver()->readMps(fn.c_str(),"") ;
1922    int nr = model->getNumRows() ;
1923    int nc = model->getNumCols() ;
1924    assert(nr == nRows[m]) ;
1925    assert(nc == nCols[m]) ;
1926    /*
1927      Stage 2: Call solver to solve the problem.
1928     
1929      then check the return code and objective.
1930     
1931    */
1932
1933    double startTime = CoinCpuTime();
1934    model->setMaximumNodes(50000);
1935    OsiClpSolverInterface * si =
1936      dynamic_cast<OsiClpSolverInterface *>(model->solver()) ;
1937    assert (si != NULL);
1938    // get clp itself
1939    ClpSimplex * modelC = si->getModelPtr();
1940    modelC->tightenPrimalBounds();
1941    model->initialSolve();
1942    model->setMinimumDrop(min(5.0e-2,
1943                                 fabs(model->getMinimizationObjValue())*1.0e-3+1.0e-4));
1944    if (model->getNumCols()<500)
1945      model->setMaximumCutPassesAtRoot(-100); // always do 100 if possible
1946    else if (model->getNumCols()<5000)
1947      model->setMaximumCutPassesAtRoot(100); // use minimum drop
1948    else
1949      model->setMaximumCutPassesAtRoot(20);
1950    model->branchAndBound();
1951     
1952    double timeOfSolution = CoinCpuTime()-startTime;
1953    if (!model->status()) { 
1954      double soln = model->getObjValue();       
1955      CoinRelFltEq eq(1.0e-3) ;
1956      if (eq(soln,objValue[m])) { 
1957        std::cerr
1958          <<"cbc_clp"<<" "
1959          << soln << " = " << objValue[m] << " ; okay";
1960        numProbSolved++;
1961      } else  { 
1962        std::cerr <<"cbc_clp" <<" " <<soln << " != " <<objValue[m] << "; error=" ;
1963        std::cerr <<fabs(objValue[m] - soln); 
1964      }
1965    } else {
1966      std::cerr << "error; too many nodes" ;
1967    }
1968    std::cerr<<" - took " <<timeOfSolution<<" seconds."<<std::endl; 
1969    timeTaken += timeOfSolution;
1970    delete model;
1971  }
1972  std::cerr
1973    <<"cbc_clp" 
1974    <<" solved " 
1975    <<numProbSolved
1976    <<" out of "
1977    <<objValue.size()
1978    <<" and took "
1979    <<timeTaken
1980    <<" seconds."
1981    <<std::endl;
1982}
Note: See TracBrowser for help on using the repository browser.