~ubuntu-branches/ubuntu/saucy/gfan/saucy-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#include "lp_soplexcdd.h"

#include "printer.h"

#include "spxdefines.h"
#include "spxsolver.h"

#include "timer.h"
#include "spxpricer.h"
//#include "spxdefaultpr.h"
#include "spxparmultpr.h"
#include "spxdevexpr.h"
#include "spxhybridpr.h"
#include "spxsteeppr.h"
#include "spxweightpr.h"
#include "spxratiotester.h"
#include "spxharrisrt.h"
#include "spxdefaultrt.h"
#include "spxfastrt.h"
#include "spxsimplifier.h"
//#include "spxaggregatesm.h"
//#include "spxredundantsm.h"
//#include "spxrem1sm.h"
//#include "spxgeneralsm.h"
#include "spxscaler.h"
#include "spxequilisc.h"
#include "spxsumst.h"
#include "spxweightst.h"
#include "spxvectorst.h"
#include "slufactor.h"
#include "soplex.h"
#include "continuedfractions.h"
#include "matrix.h"

#include "log.h"
using namespace soplex;

/** Here comes a simple derived class from #SoPlex, which uses #terminate() as
 *  callback method for outputting statistics.
 */
class MySoPlex : public SoPlex
{
private:
   SLUFactor m_slu;

public:
   /// default constructor
   MySoPlex(SPxSolver::Type p_type = SPxSolver::LEAVE, SPxSolver::Representation p_rep = SPxSolver::COLUMN)
      : SoPlex(p_type, p_rep)
   {



   bool                   print_solution = false;
   bool                   print_quality  = false;
   NameSet                rownames;
   NameSet                colnames;
   SPxStarter*            starter        = 0;
   SPxSolver::Type           type           = SPxSolver::LEAVE;
   SPxSolver::Representation representation = SPxSolver::COLUMN;
   int                    precision;
   Real                   delta          = DEFAULT_BND_VIOL;
   Real                   epsilon        = DEFAULT_EPS_ZERO;
   int                    verbose        = 0;
   SLUFactor::UpdateType  update         = SLUFactor::FOREST_TOMLIN;
   Real                   timelimit      = 1.0;-1.0;
   SPxPricer*             pricer         = 0;
   SPxRatioTester*        ratiotester    = 0;
   SPxScaler*             scaler         = 0;
   SPxSimplifier*         simplifier     = 0;

   precision = int(-log10(delta)) + 1;

   Param::setEpsilon(epsilon);
   Param::setVerbose(verbose);



   //options -p4 -t2 -g1 -s0 -c0
   setUtype(update);
   setTerminationTime(timelimit);
   setDelta(delta);

   assert(isConsistent());

   pricer = new SPxSteepPR;
   setPricer(pricer);
   assert(isConsistent());

   ratiotester = new SPxFastRT;
   setTester(ratiotester);
   assert(isConsistent());

   /*   scaler = new SPxEquili(representation == SoPlex::COLUMN, true);
   setScaler(scaler);
   assert(isConsistent());
   */
   setSimplifier(simplifier);
   assert(isConsistent());

   setStarter(starter);
   assert(isConsistent());
   }

   virtual bool terminate()
   {
      /*      if (iteration() % 100 == 0)
         std::cout << iteration() << ":\t" << value() << std::endl;
      */
     return SoPlex::terminate();
   }

   void setUtype(SLUFactor::UpdateType tp)
   {
      m_slu.setUtype(tp);
   }

  void build(const IntegerVectorList &g, IntegerVectorList::const_iterator i)
   {
     int width=g.size()-1;
     int height=i->v.size();
     
     LPColSet cols(width,width*height);
     DSVector c1(height);
     
     //Build Cols
     for(IntegerVectorList::const_iterator j=g.begin();j!=g.end();j++)
       {
	 c1.clear();
	 for(int t=0;t<height;t++)
	   if(j->v[t]!=0)
	     c1.add(t,(double)(j->v[t]));
	 
	 if(j!=i)
	   {
	     Real obj=0;
	     Real upper=infinity;
	     Real lower=0;
	     
	     cols.add(obj,lower,c1,upper);
	   }
       }
     
     LPRowSet rows(height,width*height);
     DSVector r1(width);
     
     //Change Rows
     for(int t=0;t<height;t++)
       rows.add(i->v[t],r1,i->v[t]);
     
     addRows(rows);
     addCols(cols);
     
     changeSense(SPxLP::MINIMIZE);
     
     assert(isConsistent());
   }
  void buildType2(int n, const IntegerVectorList &inequalities, const IntegerVectorList &equations)
   {
     int width=n;
     int nInequalities=inequalities.size();
     int height=inequalities.size()+equations.size();

     IntegerMatrix m=rowsToIntegerMatrix(inequalities,n);
     m.append(rowsToIntegerMatrix(equations,n));
     
     LPColSet cols(width,width*height);
     DSVector c1(height);
     
     //Build Cols
     for(int i=0;i<width;i++)
       {
	 c1.clear();
	 for(int t=0;t<height;t++)
	   if(m[t][i]!=0)
	     c1.add(t,(double)(m[t][i]));

	 Real obj=0;
	 Real upper=infinity;
	 //Real lower=0;
	 Real lower=-infinity;//is this correct?

	 if(i==0)
	   {
	     upper=1;
	     lower=1;
	   }

	 cols.add(obj,lower,c1,upper);
       }
     
     LPRowSet rows(height,width*height);
     DSVector r1(width);
     
     //Change Rows
     for(int t=0;t<nInequalities;t++)
       rows.add(0,r1,infinity);
     for(int t=nInequalities;t<height;t++)
       rows.add(0,r1,0);
     
     addRows(rows);
     addCols(cols);
     
     changeSense(SPxLP::MINIMIZE);
     
     assert(isConsistent());
   }
};

static int toint(float r)
{
   return *((int*)&r);
}

static void printLP(SPxLP &w)
{
      std::cout << "LP has " 
             << w.nRows() 
             << "\trows and\n       "
             << w.nCols() 
             << "\tcolumns" 
             << std::endl;
      int nr=w.nRows();
      int nc=w.nCols();

      for(int i=0;i<nr;i++)
         {      
            for(int j=0;j<nc;j++)
               {
                  LPRow R;
                  w.getRow(i,R);
                  //  if(j<R.rowVector().size())
                     std::cout<<R.rowVector()[j]<<" ";
                  //  else
                     //                     std::cout<<(0.0)<<" ";
               }
            std::cout<<std::endl;
         }
      std::cout<<std::endl;

      for(int i=0;i<nr;i++)
         {      
            for(int j=0;j<nc;j++)
               {
                  LPCol C;
                  w.getCol(j,C);
                  //                  if(i<C.colVector().size())
                     std::cout<<C.colVector()[i]<<" ";
                     // else
                     // std::cout<<(0.0)<<" ";
               }
            std::cout<<std::endl;
         }

      std::cout<<"cols:"<<std::endl;

      for(int j=0;j<nc;j++)
         {
            LPCol C;
            w.getCol(j,C);
            std::cout<<C.lower()<<" "<<C.upper()<<" "<<C.obj()<<std::endl;
            std::cout<<toint(C.lower())<<" "<<toint(C.upper())<<" "<<toint(C.obj())<<std::endl;
         }

      std::cout<<"rows:"<<std::endl;

      for(int i=0;i<nr;i++)
         {
            LPRow R;
            w.getRow(i,R);
            std::cout<<toint(R.lhs())<<" "<<toint(R.rhs())<<" "<<R.type()<<std::endl;
         }
}


MySoPlex work(SPxSolver::LEAVE, SPxSolver::COLUMN);


static bool isFeasibleSolution(IntegerVector const &solution, int denominator, IntegerVectorList const &g, IntegerVectorList::const_iterator i)
{
  if(denominator<=0)return false;
  // To do: Truncate  
  IntegerVector sum=denominator*(*i);

  int t=0;
  for(IntegerVectorList::const_iterator j=g.begin();j!=g.end();j++)
    if(j!=i)
      {
	if(solution[t]<0)return false;
	sum-=solution[t]*(*j);
	t++;
      }
  return sum.isZero();
}

static bool isInfeasibilityCertificate(IntegerVector const &certificate, IntegerVectorList const &g, IntegerVectorList::const_iterator i)
{
  IntegerVector c=certificate;
  // To do: add truncation on c
  if(dotLong(c,*i)<=0)return false;

  for(IntegerVectorList::const_iterator j=g.begin();j!=g.end();j++)
    if(i!=j)
      if(dotLong(c,*j)>0)return false;
  return true;
}
/*	     for(IntegerVectorList::const_iterator j=g.begin();j!=g.end();j++)
	       {
		 /*		 double prod=0;
		 for(int i=0;i<work.nRows();i++)
		   {prod+=(*j)[i]*certificate[i];
		     //		 fprintf(stderr,"%f \n",prod);
		   }
		 int num,den;doubleToFraction(prod,num,den);
		 */
		 //		 fprintf(stderr,":%f: %i/%i\n",prod,num,den);
/*		 fprintf(stderr,":%i\n",dotLong(c,*j));
	       }
	*/       


static bool isFeasibleSolutionType2(IntegerVector const &s, IntegerVectorList const &inequalities, IntegerVectorList const &equations)
{
  // To do: do truncation
  if(s[0]<=0)return false;
  for(IntegerVectorList::const_iterator i=inequalities.begin();i!=inequalities.end();i++)
    if(dotLong(s,*i)<0)return false;
  for(IntegerVectorList::const_iterator i=equations.begin();i!=equations.end();i++)
    if(dotLong(s,*i)!=0)return false;
  return true;
}


static bool isInfeasibilityCertificateType2(IntegerVector const &c, int n, IntegerVectorList const &inequalities, IntegerVectorList const &equations)
{
  // To do: truncation
  int nInequalities=inequalities.size();
  if(!c.subvector(0,nInequalities).isNonNegative())return false;

  IntegerVector sum(n);
  int j=0;
  for(IntegerVectorList::const_iterator i=inequalities.begin();i!=inequalities.end();i++,j++)
    sum+=c[j]* *i;
  for(IntegerVectorList::const_iterator i=equations.begin();i!=equations.end();i++,j++)
    sum+=c[j]* *i;

  if(sum[0]>=0)return false;

  for(int i=1;i<n;i++)
    if(sum[i]<0)
      return false;

  return true;
}


bool LpSolverSoPlexCddGmp::isFacet(const IntegerVectorList &g, IntegerVectorList::const_iterator I)
{
   SPxSolver::Type           type           = SPxSolver::LEAVE;
   SPxSolver::Representation representation = SPxSolver::COLUMN;

   int lp_status=0;

   work.clear();

   work.build(g,I);

 retry:

   //   std::cerr<< work;
   work.solve();

   SPxSolver::Status stat = work.status();

   switch (stat)
     {
     case SPxSolver::OPTIMAL:
       {
         DVector objx(work.nCols());
         
         if( work.getPrimal(objx) != SPxSolver::ERROR )
	   {
	     vector<double> solution(work.nCols());
	     for(int i=0;i<work.nCols();i++)
	       solution[i]=objx[i];
	     
	     vector<int> solutionNum;
	     int denominator;
	     doubleVectorToFractions(solution,solutionNum,denominator);
	     IntegerVector s(solution.size());
	     for(int i=0;i<s.size();i++)s[i]=solutionNum[i];
	     
	     if(isFeasibleSolution(s,denominator,g,I))
	       {
		 log3 fprintf(Stderr,"Solution OK.\n");
		 return false;
	       }
	     log3 fprintf(Stderr,"Solution failed .\n");
	     goto fallBack;
	   }
       }
       break;
     case SPxSolver::UNBOUNDED:
       std::cerr << "LP is unbounded";
       lp_status=1;
       break;
     case SPxSolver::INFEASIBLE:
       {
	 DVector farkasx(work.nRows());
	 
	 if( work.getDualfarkas(farkasx) != SPxSolver::ERROR )
	   {
	     vector<double> certificate(work.nRows());
	     for(int i=0;i<work.nRows();i++)
	       certificate[i]=farkasx[i];
	     
	     vector<int> certificateNum;
	     int denominator;
	     doubleVectorToFractions(certificate,certificateNum,denominator);
	     IntegerVector c(certificate.size());
	     for(int i=0;i<c.size();i++)c[i]=certificateNum[i];
	     
	     if(isInfeasibilityCertificate(c,g,I))
	       {
		 log3 fprintf(Stderr,"Certificate for infeasibility OK.\n");
		 return true;
	       }
	     log3 fprintf(Stderr,"Certificate failed.\n");
	   }
	 else
	   {
	     log3 fprintf(Stderr,"Error while producing certificate\n");
	   }
	 goto fallBack;
       }
       break;
     case SPxSolver::ABORT_TIME:
       std::cout << "aborted due to time limit";
       lp_status=1;
       break;
     case SPxSolver::ABORT_ITER:
       std::cout << "aborted due to iteration limit";
       lp_status=1;
       break;
     case SPxSolver::ABORT_VALUE:
       std::cout << "aborted due to objective value limit";
       lp_status=1;
       break;
     default:
       std::cout << "An error occurred during the solution process";
       lp_status=1;
       break;
     }

 fallBack:
   log0 fprintf(Stderr,"Falling back on CddLib\n");
   return LpSolverCddGmp::isFacet(g,I);
}



bool LpSolverSoPlexCddGmp::hasHomogeneousSolution(int n, const IntegerVectorList &inequalities, const IntegerVectorList &equations)
{
   SPxSolver::Type           type           = SPxSolver::LEAVE;
   SPxSolver::Representation representation = SPxSolver::COLUMN;

   int lp_status=0;

   work.clear();

   work.buildType2(n,inequalities,equations);

 retry:

   //   std::cerr<< work;

   //   assert(0);
   work.solve();

   SPxSolver::Status stat = work.status();

   switch (stat)
     {
     case SPxSolver::OPTIMAL:
       {
         DVector objx(work.nCols());
         
         if( work.getPrimal(objx) != SPxSolver::ERROR )
	   {
	     vector<double> solution(work.nCols());
	     for(int i=0;i<work.nCols();i++)
	       solution[i]=objx[i];
	     
	     vector<int> solutionNum;
	     int denominator;
	     doubleVectorToFractions(solution,solutionNum,denominator);
	     IntegerVector s(solution.size());
	     for(int i=0;i<s.size();i++)s[i]=solutionNum[i];
	     
	     //  AsciiPrinter(Stderr).printVector(s);
	     if(isFeasibleSolutionType2(s,inequalities,equations))
	       {
		 log3 fprintf(Stderr,"Solution OK.\n");
		 return true;
	       }
	     log2 fprintf(Stderr,"Solution failed (Type2).\n");

	     /*	     for(int i=0;i<work.nCols();i++)
	       {
		 std::cerr<<solution[i]<<',';
	       }
	     std::cerr<< work;
	     AsciiPrinter(Stderr).printVector(s);
	     AsciiPrinter(Stderr).printVectorList(inequalities);
	     AsciiPrinter(Stderr).printVectorList(equations);
	     assert(0);
	     */
	     goto fallBack;
	   }
       }
       break;
     case SPxSolver::UNBOUNDED:
       std::cerr << "LP is unbounded";
       lp_status=1;
       break;
     case SPxSolver::INFEASIBLE:
       {
	 DVector farkasx(work.nRows());
	 
	 if( work.getDualfarkas(farkasx) != SPxSolver::ERROR )
	   {
	     vector<double> certificate(work.nRows());
	     for(int i=0;i<work.nRows();i++)
	       certificate[i]=farkasx[i];
	     
	     vector<int> certificateNum;
	     int denominator;
	     doubleVectorToFractions(certificate,certificateNum,denominator);
	     IntegerVector c(certificate.size());
	     for(int i=0;i<c.size();i++)c[i]=certificateNum[i];
	     
	     if(isInfeasibilityCertificateType2(c,n,inequalities,equations))
	       {
		 log3 fprintf(Stderr,"Certificate for infeasibility OK.\n");
		 return false;
	       }
	     
	       log2 fprintf(Stderr,"Certificate failed (Type2).\n");
	       /* std::cerr<< work;
	      std::cerr<< farkasx;
	     AsciiPrinter(Stderr).printVector(c);
	     AsciiPrinter(Stderr).printVectorList(inequalities);
	     AsciiPrinter(Stderr).printVectorList(equations);
	     assert(0);
	     */
	   }
	 else
	   {
	     log3 fprintf(Stderr,"Error while producing certificate\n");
	   }
	 goto fallBack;
	 }
       goto fallBack;
       break;
     case SPxSolver::ABORT_TIME:
       std::cout << "aborted due to time limit";
       lp_status=1;
       break;
     case SPxSolver::ABORT_ITER:
       std::cout << "aborted due to iteration limit";
       lp_status=1;
       break;
     case SPxSolver::ABORT_VALUE:
       std::cout << "aborted due to objective value limit";
       lp_status=1;
       break;
     default:
       std::cout << "An error occurred during the solution process";
       lp_status=1;
       break;
     }

 fallBack:
   log0 fprintf(Stderr,"Falling back on CddLib\n");
   return LpSolverCddGmp::hasHomogeneousSolution(n, inequalities,equations);
}


static LpSolverSoPlexCddGmp theLpSolverSoPlexCdd;