Debian Codegraph @

Osi/test/OsiTestSolver.hpp

raw
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 // This file is licensed under the terms of Eclipse Public License (EPL).
4
5 // this is a copy of VolVolume (stable/1.1 rev. 233)
6
7 #ifndef __OSITESTSOLVER_HPP__
8 #define __OSITESTSOLVER_HPP__
9
10 #include <cfloat>
11 #include <algorithm>
12 #include <cstdio>
13 #include <cmath>
14 #include <cstdlib>
15
16 #include "CoinFinite.hpp"
17
18 #ifndef VOL_DEBUG
19 // When VOL_DEBUG is 1, we check vector indices
20 #define VOL_DEBUG 0
21 #endif
22
23 template <class T> static inline T
24 VolMax(const T x, const T y) {
25 return ((x) > (y)) ? (x) : (y);
26 }
27
28 template <class T> static inline T
29 VolAbs(const T x) {
30 return ((x) > 0) ? (x) : -(x);
31 }
32
33 //############################################################################
34
35 #if defined(VOL_DEBUG) && (VOL_DEBUG != 0)
36 #define VOL_TEST_INDEX(i, size) \
37 { \
38 if ((i) < 0 || (i) >= (size)) { \
39 printf("bad VOL_?vector index\n"); \
40 abort(); \
41 } \
42 }
43 #define VOL_TEST_SIZE(size) \
44 { \
45 if (s <= 0) { \
46 printf("bad VOL_?vector size\n"); \
47 abort(); \
48 } \
49 }
50 #else
51 #define VOL_TEST_INDEX(i, size)
52 #define VOL_TEST_SIZE(size)
53 #endif
54
55 //############################################################################
56
57 class VOL_dvector;
58 class VOL_ivector;
59 class VOL_primal;
60 class VOL_dual;
61 class VOL_swing;
62 class VOL_alpha_factor;
63 class VOL_vh;
64 class VOL_indc;
65 class VOL_user_hooks;
66 class VOL_problem;
67
68 //############################################################################
69
70 /**
71 This class contains the parameters controlling the Volume Algorithm
72 */
73 struct VOL_parms {
74 /** initial value of lambda */
75 double lambdainit;
76 /** initial value of alpha */
77 double alphainit;
78 /** minimum value for alpha */
79 double alphamin;
80 /** when little progress is being done, we multiply alpha by alphafactor */
81 double alphafactor;
82
83 /** initial upper bound of the value of an integer solution */
84 double ubinit;
85
86 /** accept if max abs viol is less than this */
87 double primal_abs_precision;
88 /** accept if abs gap is less than this */
89 double gap_abs_precision;
90 /** accept if rel gap is less than this */
91 double gap_rel_precision;
92 /** terminate if best_ub - lcost < granularity */
93 double granularity;
94
95 /** terminate if the relative increase in lcost through
96 <code>ascent_check_invl</code> steps is less than this */
97 double minimum_rel_ascent;
98 /** when to check for sufficient relative ascent the first time */
99 int ascent_first_check;
100 /** through how many iterations does the relative ascent have to reach a
101 minimum */
102 int ascent_check_invl;
103
104 /** maximum number of iterations */
105 int maxsgriters;
106
107 /** controls the level of printing.
108 The flag should the the 'OR'-d value of the following options:
109 <ul>
110 <li> 0 - print nothing
111 <li> 1 - print iteration information
112 <li> 2 - add lambda information
113 <li> 4 - add number of Red, Yellow, Green iterations
114 </ul>
115 Default: 3
116 */
117 int printflag;
118 /** controls how often do we print */
119 int printinvl;
120 /** controls how often we run the primal heuristic */
121 int heurinvl;
122
123 /** how many consecutive green iterations are allowed before changing
124 lambda */
125 int greentestinvl;
126 /** how many consecutive yellow iterations are allowed before changing
127 lambda */
128 int yellowtestinvl;
129 /** how many consecutive red iterations are allowed before changing
130 lambda */
131 int redtestinvl;
132
133 /** number of iterations before we check if alpha should be decreased */
134 int alphaint;
135
136 /** name of file for saving dual solution */
137 char* temp_dualfile;
138 };
139
140 //############################################################################
141
142 /** vector of doubles. It is used for most vector operations.
143
144 Note: If <code>VOL_DEBUG</code> is <code>#defined</code> to be 1 then each
145 time an entry is accessed in the vector the index of the entry is tested
146 for nonnegativity and for being less than the size of the vector. It's
147 good to turn this on while debugging, but in final runs it should be
148 turned off (beause of the performance hit).
149 */
150 class VOL_dvector {
151 public:
152 /** The array holding the vector */
153 double* v;
154 /** The size of the vector */
155 int sz;
156
157 public:
158 /** Construct a vector of size s. The content of the vector is undefined. */
159 VOL_dvector(const int s) {
160 VOL_TEST_SIZE(s);
161 v = new double[sz = s];
162 }
163 /** Default constructor creates a vector of size 0. */
164 VOL_dvector() : v(0), sz(0) {}
165 /** Copy constructor makes a replica of x. */
166 VOL_dvector(const VOL_dvector& x) : v(0), sz(0) {
167 sz = x.sz;
168 if (sz > 0) {
169 v = new double[sz];
170 std::copy(x.v, x.v + sz, v);
171 }
172 }
173 /** The destructor deletes the data array. */
174 ~VOL_dvector() { delete[] v; }
175
176 /** Return the size of the vector. */
177 inline int size() const {return sz;}
178
179 /** Return a reference to the <code>i</code>-th entry. */
180 inline double& operator[](const int i) {
181 VOL_TEST_INDEX(i, sz);
182 return v[i];
183 }
184
185 /** Return the <code>i</code>-th entry. */
186 inline double operator[](const int i) const {
187 VOL_TEST_INDEX(i, sz);
188 return v[i];
189 }
190
191 /** Delete the content of the vector and replace it with a vector of length
192 0. */
193 inline void clear() {
194 delete[] v;
195 v = 0;
196 sz = 0;
197 }
198 /** Convex combination. Replace the current vector <code>v</code> with
199 <code>v = (1-gamma) v + gamma w</code>. */
200 inline void cc(const double gamma, const VOL_dvector& w) {
201 if (sz != w.sz) {
202 printf("bad VOL_dvector sizes\n");
203 abort();
204 }
205 double * p_v = v - 1;
206 const double * p_w = w.v - 1;
207 const double * const p_e = v + sz;
208 const double one_gamma = 1.0 - gamma;
209 while ( ++p_v != p_e ){
210 *p_v = one_gamma * (*p_v) + gamma * (*++p_w);
211 }
212 }
213
214 /** delete the current vector and allocate space for a vector of size
215 <code>s</code>. */
216 inline void allocate(const int s) {
217 VOL_TEST_SIZE(s);
218 delete[] v;
219 v = new double[sz = s];
220 }
221
222 /** swaps the vector with <code>w</code>. */
223 inline void swap(VOL_dvector& w) {
224 std::swap(v, w.v);
225 std::swap(sz, w.sz);
226 }
227
228 /** Copy <code>w</code> into the vector. */
229 VOL_dvector& operator=(const VOL_dvector& w);
230 /** Replace every entry in the vector with <code>w</code>. */
231 VOL_dvector& operator=(const double w);
232 };
233
234 //-----------------------------------------------------------------------------
235 /** vector of ints. It's used to store indices, it has similar
236 functions as VOL_dvector.
237
238 Note: If <code>VOL_DEBUG</code> is <code>#defined</code> to be 1 then each
239 time an entry is accessed in the vector the index of the entry is tested
240 for nonnegativity and for being less than the size of the vector. It's
241 good to turn this on while debugging, but in final runs it should be
242 turned off (beause of the performance hit).
243 */
244 class VOL_ivector {
245 public:
246 /** The array holding the vector. */
247 int* v;
248 /** The size of the vector. */
249 int sz;
250 public:
251 /** Construct a vector of size s. The content of the vector is undefined. */
252 VOL_ivector(const int s) {
253 VOL_TEST_SIZE(s);
254 v = new int[sz = s];
255 }
256 /** Default constructor creates a vector of size 0. */
257 VOL_ivector() : v(0), sz(0) {}
258 /** Copy constructor makes a replica of x. */
259 VOL_ivector(const VOL_ivector& x) {
260 sz = x.sz;
261 if (sz > 0) {
262 v = new int[sz];
263 std::copy(x.v, x.v + sz, v);
264 }
265 }
266 /** The destructor deletes the data array. */
267 ~VOL_ivector(){
268 delete [] v;
269 }
270
271 /** Return the size of the vector. */
272 inline int size() const { return sz; }
273 /** Return a reference to the <code>i</code>-th entry. */
274 inline int& operator[](const int i) {
275 VOL_TEST_INDEX(i, sz);
276 return v[i];
277 }
278
279 /** Return the <code>i</code>-th entry. */
280 inline int operator[](const int i) const {
281 VOL_TEST_INDEX(i, sz);
282 return v[i];
283 }
284
285 /** Delete the content of the vector and replace it with a vector of length
286 0. */
287 inline void clear() {
288 delete[] v;
289 v = 0;
290 sz = 0;
291 }
292
293 /** delete the current vector and allocate space for a vector of size
294 <code>s</code>. */
295 inline void allocate(const int s) {
296 VOL_TEST_SIZE(s);
297 delete[] v;
298 v = new int[sz = s];
299 }
300
301 /** swaps the vector with <code>w</code>. */
302 inline void swap(VOL_ivector& w) {
303 std::swap(v, w.v);
304 std::swap(sz, w.sz);
305 }
306
307 /** Copy <code>w</code> into the vector. */
308 VOL_ivector& operator=(const VOL_ivector& v);
309 /** Replace every entry in the vector with <code>w</code>. */
310 VOL_ivector& operator=(const int w);
311 };
312
313 //############################################################################
314 // A class describing a primal solution. This class is used only internally
315 class VOL_primal {
316 public:
317 // objective value of this primal solution
318 double value;
319 // the largest of the v[i]'s
320 double viol;
321 // primal solution
322 VOL_dvector x;
323 // v=b-Ax, for the relaxed constraints
324 VOL_dvector v;
325
326 VOL_primal(const int psize, const int dsize) : x(psize), v(dsize) {}
327 VOL_primal(const VOL_primal& primal) :
328 value(primal.value), viol(primal.viol), x(primal.x), v(primal.v) {}
329 ~VOL_primal() {}
330 inline VOL_primal& operator=(const VOL_primal& p) {
331 if (this == &p)
332 return *this;
333 value = p.value;
334 viol = p.viol;
335 x = p.x;
336 v = p.v;
337 return *this;
338 }
339
340 // convex combination. data members in this will be overwritten
341 // convex combination between two primal solutions
342 // x <-- alpha x + (1 - alpha) p.x
343 // v <-- alpha v + (1 - alpha) p.v
344 inline void cc(const double alpha, const VOL_primal& p) {
345 value = alpha * p.value + (1.0 - alpha) * value;
346 x.cc(alpha, p.x);
347 v.cc(alpha, p.v);
348 }
349 // find maximum of v[i]
350 void find_max_viol(const VOL_dvector& dual_lb,
351 const VOL_dvector& dual_ub);
352 };
353
354 //-----------------------------------------------------------------------------
355 // A class describing a dual solution. This class is used only internally
356 class VOL_dual {
357 public:
358 // lagrangian value
359 double lcost;
360 // reduced costs * (pstar-primal)
361 double xrc;
362 // this information is only printed
363 // dual vector
364 VOL_dvector u;
365
366 VOL_dual(const int dsize) : u(dsize) { u = 0.0;}
367 VOL_dual(const VOL_dual& dual) :
368 lcost(dual.lcost), xrc(dual.xrc), u(dual.u) {}
369 ~VOL_dual() {}
370 inline VOL_dual& operator=(const VOL_dual& p) {
371 if (this == &p)
372 return *this;
373 lcost = p.lcost;
374 xrc = p.xrc;
375 u = p.u;
376 return *this;
377 }
378 // dual step
379 void step(const double target, const double lambda,
380 const VOL_dvector& dual_lb, const VOL_dvector& dual_ub,
381 const VOL_dvector& v);
382 double ascent(const VOL_dvector& v, const VOL_dvector& last_u) const;
383 void compute_xrc(const VOL_dvector& pstarx, const VOL_dvector& primalx,
384 const VOL_dvector& rc);
385
386 };
387
388
389 //############################################################################
390 /* here we check whether an iteration is green, yellow or red. Also according
391 to this information we decide whether lambda should be changed */
392 class VOL_swing {
393 private:
394 VOL_swing(const VOL_swing&);
395 VOL_swing& operator=(const VOL_swing&);
396 public:
397 enum condition {green, yellow, red} lastswing;
398 int lastgreeniter, lastyellowiter, lastrediter;
399 int ngs, nrs, nys;
400 int rd;
401
402 VOL_swing() {
403 lastgreeniter = lastyellowiter = lastrediter = 0;
404 ngs = nrs = nys = 0;
405 }
406 ~VOL_swing(){}
407
408 inline void cond(const VOL_dual& dual,
409 const double lcost, const double ascent, const int iter) {
410 double eps = 1.e-3;
411
412 if (ascent > 0.0 && lcost > dual.lcost + eps) {
413 lastswing = green;
414 lastgreeniter = iter;
415 ++ngs;
416 rd = 0;
417 } else {
418 if (ascent <= 0 && lcost > dual.lcost) {
419 lastswing = yellow;
420 lastyellowiter = iter;
421 ++nys;
422 rd = 0;
423 } else {
424 lastswing = red;
425 lastrediter = iter;
426 ++nrs;
427 rd = 1;
428 }
429 }
430 }
431
432 inline double
433 lfactor(const VOL_parms& parm, const double lambda, const int iter) {
434 double lambdafactor = 1.0;
435 double eps = 5.e-4;
436 int cons;
437
438 switch (lastswing) {
439 case green:
440 cons = iter - VolMax(lastyellowiter, lastrediter);
441 if (parm.printflag & 4)
442 printf(" G: Consecutive Gs = %3d\n\n", cons);
443 if (cons >= parm.greentestinvl && lambda < 2.0) {
444 lastgreeniter = lastyellowiter = lastrediter = iter;
445 lambdafactor = 2.0;
446 if (parm.printflag & 2)
447 printf("\n ---- increasing lamda to %g ----\n\n",
448 lambda * lambdafactor);
449 }
450 break;
451
452 case yellow:
453 cons = iter - VolMax(lastgreeniter, lastrediter);
454 if (parm.printflag & 4)
455 printf(" Y: Consecutive Ys = %3d\n\n", cons);
456 if (cons >= parm.yellowtestinvl) {
457 lastgreeniter = lastyellowiter = lastrediter = iter;
458 lambdafactor = 1.1;
459 if (parm.printflag & 2)
460 printf("\n **** increasing lamda to %g *****\n\n",
461 lambda * lambdafactor);
462 }
463 break;
464
465 case red:
466 cons = iter - VolMax(lastgreeniter, lastyellowiter);
467 if (parm.printflag & 4)
468 printf(" R: Consecutive Rs = %3d\n\n", cons);
469 if (cons >= parm.redtestinvl && lambda > eps) {
470 lastgreeniter = lastyellowiter = lastrediter = iter;
471 lambdafactor = 0.67;
472 if (parm.printflag & 2)
473 printf("\n **** decreasing lamda to %g *****\n\n",
474 lambda * lambdafactor);
475 }
476 break;
477 }
478 return lambdafactor;
479 }
480
481 inline void
482 print() {
483 printf("**** G= %i, Y= %i, R= %i ****\n", ngs, nys, nrs);
484 ngs = nrs = nys = 0;
485 }
486 };
487
488 //############################################################################
489 /* alpha should be decreased if after some number of iterations the objective
490 has increased less that 1% */
491 class VOL_alpha_factor {
492 private:
493 VOL_alpha_factor(const VOL_alpha_factor&);
494 VOL_alpha_factor& operator=(const VOL_alpha_factor&);
495 public:
496 double lastvalue;
497
498 VOL_alpha_factor() {lastvalue = -COIN_DBL_MAX;}
499 ~VOL_alpha_factor() {}
500
501 inline double factor(const VOL_parms& parm, const double lcost,
502 const double alpha) {
503 if (alpha < parm.alphamin)
504 return 1.0;
505 const double ll = VolAbs(lcost);
506 const double x = ll > 10 ? (lcost-lastvalue)/ll : (lcost-lastvalue);
507 lastvalue = lcost;
508 return (x <= 0.01) ? parm.alphafactor : 1.0;
509 }
510 };
511
512 //############################################################################
513 /* here we compute the norm of the conjugate direction -hh-, the norm of the
514 subgradient -norm-, the inner product between the subgradient and the
515 last conjugate direction -vh-, and the inner product between the new
516 conjugate direction and the subgradient */
517 class VOL_vh {
518 private:
519 VOL_vh(const VOL_vh&);
520 VOL_vh& operator=(const VOL_vh&);
521 public:
522 double hh;
523 double norm;
524 double vh;
525 double asc;
526
527 VOL_vh(const double alpha,
528 const VOL_dvector& dual_lb, const VOL_dvector& dual_ub,
529 const VOL_dvector& v, const VOL_dvector& vstar,
530 const VOL_dvector& u);
531 ~VOL_vh(){}
532 };
533
534 //############################################################################
535 /* here we compute different parameter to be printed. v2 is the square of
536 the norm of the subgradient. vu is the inner product between the dual
537 variables and the subgradient. vabs is the maximum absolute value of
538 the violations of pstar. asc is the inner product between the conjugate
539 direction and the subgradient */
540 class VOL_indc {
541 private:
542 VOL_indc(const VOL_indc&);
543 VOL_indc& operator=(const VOL_indc&);
544 public:
545 double v2;
546 double vu;
547 double vabs;
548 double asc;
549
550 public:
551 VOL_indc(const VOL_dvector& dual_lb, const VOL_dvector& dual_ub,
552 const VOL_primal& primal, const VOL_primal& pstar,
553 const VOL_dual& dual);
554 ~VOL_indc() {}
555 };
556
557 //#############################################################################
558
559 /** The user hooks should be overridden by the user to provide the
560 problem specific routines for the volume algorithm. The user
561 should derive a class ...
562
563 for all hooks: return value of -1 means that volume should quit
564 */
565 class VOL_user_hooks {
566 public:
567 virtual ~VOL_user_hooks() {}
568 public:
569 // for all hooks: return value of -1 means that volume should quit
570 /** compute reduced costs
571 @param u (IN) the dual variables
572 @param rc (OUT) the reduced cost with respect to the dual values
573 */
574 virtual int compute_rc(const VOL_dvector& u, VOL_dvector& rc) = 0;
575
576 /** Solve the subproblem for the subgradient step.
577 @param dual (IN) the dual variables
578 @param rc (IN) the reduced cost with respect to the dual values
579 @param lcost (OUT) the lagrangean cost with respect to the dual values
580 @param x (OUT) the primal result of solving the subproblem
581 @param v (OUT) b-Ax for the relaxed constraints
582 @param pcost (OUT) the primal objective value of <code>x</code>
583 */
584 virtual int solve_subproblem(const VOL_dvector& dual, const VOL_dvector& rc,
585 double& lcost, VOL_dvector& x, VOL_dvector& v,
586 double& pcost) = 0;
587 /** Starting from the primal vector x, run a heuristic to produce
588 an integer solution
589 @param x (IN) the primal vector
590 @param heur_val (OUT) the value of the integer solution (return
591 <code>COIN_DBL_MAX</code> here if no feas sol was found
592 */
593 virtual int heuristics(const VOL_problem& p,
594 const VOL_dvector& x, double& heur_val) = 0;
595 };
596
597 //#############################################################################
598
599 /** This class holds every data for the Volume Algorithm and its
600 <code>solve</code> method must be invoked to solve the problem.
601
602 The INPUT fields must be filled out completely before <code>solve</code>
603 is invoked. <code>dsol</code> have to be filled out if and only if the
604 last argument to <code>solve</code> is <code>true</code>.
605 */
606
607 class VOL_problem {
608 private:
609 VOL_problem(const VOL_problem&);
610 VOL_problem& operator=(const VOL_problem&);
611 void set_default_parm();
612 // ############ INPUT fields ########################
613 public:
614 /**@name Constructors and destructor */
615 //@{
616 /** Default constructor. */
617 VOL_problem();
618 /** Create a a <code>VOL_problem</code> object and read in the parameters
619 from <code>filename</code>. */
620 VOL_problem(const char *filename);
621 /** Destruct the object. */
622 ~VOL_problem();
623 //@}
624
625 /**@name Method to solve the problem. */
626 //@{
627 /** Solve the problem using the <code>hooks</code>. Any information needed
628 in the hooks must be stored in the structure <code>user_data</code>
629 points to. */
630 int solve(VOL_user_hooks& hooks, const bool use_preset_dual = false);
631 //@}
632
633 private:
634 /**@name Internal data (may be inquired for) */
635 //@{
636 /** value of alpha */
637 double alpha_;
638 /** value of lambda */
639 double lambda_;
640 // This union is here for padding (so that data members would be
641 // double-aligned on x86 CPU
642 union {
643 /** iteration number */
644 int iter_;
645 double __pad0;
646 };
647 //@}
648
649 public:
650
651 /**@name External data (containing the result after solve) */
652 //@{
653 /** final lagrangian value (OUTPUT) */
654 double value;
655 /** final dual solution (INPUT/OUTPUT) */
656 VOL_dvector dsol;
657 /** final primal solution (OUTPUT) */
658 VOL_dvector psol;
659 /** violations (b-Ax) for the relaxed constraints */
660 VOL_dvector viol;
661 //@}
662
663 /**@name External data (may be changed by the user before calling solve) */
664 //@{
665 /** The parameters controlling the Volume Algorithm (INPUT) */
666 VOL_parms parm;
667 /** length of primal solution (INPUT) */
668 int psize;
669 /** length of dual solution (INPUT) */
670 int dsize;
671 /** lower bounds for the duals (if 0 length, then filled with -inf) (INPUT)
672 */
673 VOL_dvector dual_lb;
674 /** upper bounds for the duals (if 0 length, then filled with +inf) (INPUT)
675 */
676 VOL_dvector dual_ub;
677 //@}
678
679 public:
680 /**@name Methods returning final data */
681 //@{
682 /** returns the iteration number */
683 int iter() const { return iter_; }
684 /** returns the value of alpha */
685 double alpha() const { return alpha_; }
686 /** returns the value of lambda */
687 double lambda() const { return lambda_; }
688 //@}
689
690 private:
691 /**@name Private methods used internally */
692 //@{
693 /** Read in the parameters from the file <code>filename</code>. */
694 void read_params(const char* filename);
695
696 /** initializes duals, bounds for the duals, alpha, lambda */
697 int initialize(const bool use_preset_dual);
698
699 /** print volume info every parm.printinvl iterations */
700 void print_info(const int iter,
701 const VOL_primal& primal, const VOL_primal& pstar,
702 const VOL_dual& dual);
703
704 /** Checks if lcost is close to the target, if so it increases the target.
705 Close means that we got within 5% of the target. */
706 double readjust_target(const double oldtarget, const double lcost) const;
707
708 /** Here we decide the value of alpha1 to be used in the convex
709 combination. The new pstar will be computed as <br>
710 pstar = alpha1 * pstar + (1 - alpha1) * primal <br>
711 More details of this are in doc.ps. <br>
712 IN: alpha, primal, pstar, dual <br>
713 @return alpha1
714 */
715 double power_heur(const VOL_primal& primal, const VOL_primal& pstar,
716 const VOL_dual& dual) const;
717 //@}
718 };
719
720 #endif