Debian Codegraph @

Osi/test/OsiTestSolver.cpp

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 #include <cstdlib>
6 #include <cstdio>
7 #include <cstring>
8 #include <string>
9 #include <cmath>
10
11 #include "OsiTestSolver.hpp"
12
13 //#############################################################################
14 /// Usage: v=w; where w is a VOL_dvector
15 VOL_dvector&
16 VOL_dvector::operator=(const VOL_dvector& w) {
17 if (this == &w)
18 return *this;
19 delete[] v;
20 const int wsz = w.size();
21 if (wsz == 0) {
22 v = 0;
23 sz = 0;
24 } else {
25 v = new double[sz = wsz];
26 for (int i = sz - 1; i >= 0; --i)
27 v[i] = w[i];
28 }
29 return *this;
30 }
31 /// Usage v=w; where w is a double. It copies w in every entry of v
32 VOL_dvector&
33 VOL_dvector::operator=(const double w) {
34 for (int i = sz - 1; i >= 0; --i)
35 v[i] = w;
36 return *this;
37 }
38
39 //#############################################################################
40 /// Usage: v=w; where w is a VOL_ivector
41 VOL_ivector&
42 VOL_ivector::operator=(const VOL_ivector& w) {
43 if (this == &w) return *this;
44 delete[] v;
45 const int wsz = w.size();
46 if (wsz == 0) {
47 v = 0;
48 sz = 0;
49 } else {
50 v = new int[sz = wsz];
51 for (int i = sz - 1; i >= 0; --i)
52 v[i] = w[i];
53 }
54 return *this;
55 }
56 /// Usage v=w; where w is an int. It copies w in every entry of v
57 VOL_ivector&
58 VOL_ivector::operator=(const int w) {
59 for (int i = sz - 1; i >= 0; --i)
60 v[i] = w;
61 return *this;
62 }
63
64 //############################################################################
65 /// find maximum absolute value of the primal violations
66 void
67 VOL_primal::find_max_viol(const VOL_dvector& dual_lb,
68 const VOL_dvector& dual_ub)
69 {
70 const int nc = v.size();
71 viol = 0;
72 for ( int i = 0; i < nc; ++i ) {
73 if ( (v[i] > 0.0 && dual_ub[i] != 0.0) ||
74 (v[i] < 0.0 && dual_lb[i] != 0.0) )
75 viol = VolMax(viol, VolAbs(v[i]));
76 }
77 }
78
79 //############################################################################
80 /// Dual step. It takes a step in the direction v
81 // lcost is a member of VOL_dual
82 void
83 VOL_dual::step(const double target, const double lambda,
84 const VOL_dvector& dual_lb, const VOL_dvector& dual_ub,
85 const VOL_dvector& v) {
86 const int nc = u.size();
87 int i;
88
89 double viol = 0.0;
90 for (i = 0; i < nc; ++i) {
91 if (( v[i] > 0.0 && u[i] < dual_ub[i] ) ||
92 ( v[i] < 0.0 && u[i] > dual_lb[i] )) {
93 viol += v[i] * v[i];
94 }
95 }
96
97 const double stp = viol == 0.0 ? 0.0 : (target - lcost) / viol * lambda;
98
99 for (i = 0; i < nc; ++i) {
100 if (( v[i] > 0.0 && u[i] < dual_ub[i] ) ||
101 ( v[i] < 0.0 && u[i] > dual_lb[i] )) {
102 u[i] += stp * v[i];
103 if (u[i] < dual_lb[i])
104 u[i] = dual_lb[i];
105 else if (u[i] > dual_ub[i])
106 u[i] = dual_ub[i];
107 }
108 }
109 }
110
111 /// ascent = inner product(v, u - last_u)
112 double
113 VOL_dual::ascent(const VOL_dvector& v, const VOL_dvector& last_u) const
114 {
115 const int nc = u.size();
116 int i;
117 double asc = 0.0;
118 for (i = 0; i < nc; ++i)
119 asc += v[i] * (u[i] - last_u[i]);
120 return asc;
121 }
122 /** compute xrc. This is (c - u A) * ( xstar - x ). This is just
123 miscellaneous information, it is not used in the algorithm. */
124 void
125 VOL_dual::compute_xrc(const VOL_dvector& xstar, const VOL_dvector& x,
126 const VOL_dvector& rc)
127 {
128 const int nc = x.size();
129 xrc = 0;
130 for (int i = 0; i < nc; ++i) {
131 xrc += rc[i] * (xstar[i] - x[i]);
132 }
133 }
134
135 //############################################################################
136 /** Computing inner products. It computes v * ( alpha v + (1-alpha) h),
137 v * h, v * v, h * h. Here v is the subgradient direction, and h is
138 the conjugate direction. */
139 VOL_vh::VOL_vh(const double alpha,
140 const VOL_dvector& dual_lb, const VOL_dvector& dual_ub,
141 const VOL_dvector& v, const VOL_dvector& vstar,
142 const VOL_dvector& u) :
143 hh(0), norm(0), vh(0), asc(0)
144 {
145 int i;
146 const int nc = vstar.size();
147 double vv;
148
149 for (i = 0; i < nc; ++i) {
150 const double vi = v[i];
151 const double vsi = vstar[i];
152 vv = alpha * vi + (1.0 - alpha) * vsi;
153 if (u[i] == 0.0 && dual_lb[i] == 0.0 && vv <= 0.0)
154 continue;
155 if (u[i] == 0.0 && dual_ub[i] == 0.0 && vv >= 0.0)
156 continue;
157 asc += vi * vv;
158 vh += vi * vsi;
159 norm += vi * vi;
160 hh += vsi * vsi;
161 }
162 }
163
164 //############################################################################
165 /** Computes indicators for printing. They are v2=vstar * vstar, asc= v*v,
166 vu= vstar * u, vabs = sum( abs(vstar[i]))/m, v2= sum( vstar[i]^2) / m.
167 */
168 VOL_indc::VOL_indc(const VOL_dvector& dual_lb, const VOL_dvector& dual_ub,
169 const VOL_primal& primal, const VOL_primal& pstar,
170 const VOL_dual& dual) {
171
172 v2 = vu = vabs = asc = 0.0;
173 const VOL_dvector v = primal.v;
174 const VOL_dvector vstar = pstar.v;
175 const VOL_dvector u = dual.u;
176 int i;
177 const int nc = vstar.size();
178
179 for (i = 0; i < nc; ++i) {
180 if (u[i] == 0.0 && dual_lb[i] == 0.0 && vstar[i] <= 0.0)
181 continue;
182 if (u[i] == 0.0 && dual_ub[i] == 0.0 && vstar[i] >= 0.0)
183 continue;
184 v2 += vstar[i] * vstar[i];
185 asc += v[i] * v[i];
186 vu -= vstar[i] * u[i];
187 vabs += VolAbs(vstar[i]);
188 }
189
190 v2 = sqrt(v2) / nc;
191 vabs /= nc;
192 }
193
194 //############################################################################
195
196 // reading parameters that control the algorithm
197 void
198 VOL_problem::read_params(const char* filename)
199 {
200 char s[100];
201 FILE* infile = fopen(filename, "r");
202 if (!infile) {
203 printf("Failure to open file: %s\n", filename);
204 abort();
205 }
206 while (fgets(s, 100, infile)) {
207 const size_t len = strlen(s) - 1;
208 if (s[len] == '\n')
209 s[len] = 0;
210 std::string ss(s);
211
212 if (ss.find("temp_dualfile") == 0) {
213 size_t i = ss.find("=");
214 size_t i1 = ss.length()-i-1;
215 std::string sss = ss.substr(i+1,i1);
216 parm.temp_dualfile = new char[sss.length() + 1];
217 memcpy(parm.temp_dualfile, sss.c_str(), sss.length());
218 parm.temp_dualfile[sss.length()] = 0;
219 } else if (ss.find("ubinit") == 0) {
220 size_t i = ss.find("=");
221 parm.ubinit = atof(&s[i+1]);
222
223 } else if (ss.find("printflag") == 0) {
224 size_t i = ss.find("=");
225 parm.printflag = atoi(&s[i+1]);
226
227 } else if (ss.find("printinvl") == 0) {
228 size_t i = ss.find("=");
229 parm.printinvl = atoi(&s[i+1]);
230
231 } else if (ss.find("maxsgriters") == 0) {
232 size_t i = ss.find("=");
233 parm.maxsgriters = atoi(&s[i+1]);
234
235 } else if (ss.find("heurinvl") == 0) {
236 size_t i = ss.find("=");
237 parm.heurinvl = atoi(&s[i+1]);
238
239 } else if (ss.find("greentestinvl") == 0) {
240 size_t i = ss.find("=");
241 parm.greentestinvl = atoi(&s[i+1]);
242
243 } else if (ss.find("yellowtestinvl") == 0) {
244 size_t i = ss.find("=");
245 parm.yellowtestinvl = atoi(&s[i+1]);
246
247 } else if (ss.find("redtestinvl") == 0) {
248 size_t i = ss.find("=");
249 parm.redtestinvl = atoi(&s[i+1]);
250
251 } else if (ss.find("lambdainit") == 0) {
252 size_t i = ss.find("=");
253 parm.lambdainit = atof(&s[i+1]);
254
255 } else if (ss.find("alphainit") == 0) {
256 size_t i = ss.find("=");
257 parm.alphainit = atof(&s[i+1]);
258
259 } else if (ss.find("alphamin") == 0) {
260 size_t i = ss.find("=");
261 parm.alphamin = atof(&s[i+1]);
262
263 } else if (ss.find("alphafactor") == 0) {
264 size_t i = ss.find("=");
265 parm.alphafactor = atof(&s[i+1]);
266
267 } else if (ss.find("alphaint") == 0) {
268 size_t i = ss.find("=");
269 parm.alphaint = atoi(&s[i+1]);
270
271 } else if (ss.find("primal_abs_precision") == 0) {
272 size_t i = ss.find("=");
273 parm.primal_abs_precision = atof(&s[i+1]);
274
275 // } else if (ss.find("primal_rel_precision") == 0) {
276 // size_t i = ss.find("=");
277 // parm.primal_rel_precision = atof(&s[i+1]);
278
279 } else if (ss.find("gap_abs_precision") == 0) {
280 size_t i = ss.find("=");
281 parm.gap_abs_precision = atof(&s[i+1]);
282
283 } else if (ss.find("gap_rel_precision") == 0) {
284 size_t i = ss.find("=");
285 parm.gap_rel_precision = atof(&s[i+1]);
286
287
288 } else if (ss.find("ascent_check_invl") == 0) {
289 size_t i = ss.find("=");
290 parm.ascent_check_invl = atoi(&s[i+1]);
291
292 } else if (ss.find("minimum_rel_ascent") == 0) {
293 size_t i = ss.find("=");
294 parm.minimum_rel_ascent = atoi(&s[i+1]);
295
296 } else if (ss.find("granularity") == 0) {
297 size_t i = ss.find("=");
298 parm.granularity = atof(&s[i+1]);
299 }
300 }
301 fclose(infile);
302 }
303
304 //#############################################################################
305
306 void
307 VOL_problem::set_default_parm()
308 {
309 parm.lambdainit = 0.1;
310 parm.alphainit = 0.01;
311 parm.alphamin = 0.001;
312 parm.alphafactor = 0.5;
313 parm.ubinit = COIN_DBL_MAX;
314 parm.primal_abs_precision = 0.02;
315 // parm.primal_rel_precision = 0.01;
316 parm.gap_abs_precision = 0.0;
317 parm.gap_rel_precision = 0.001;
318 parm.granularity = 0.0;
319 parm.minimum_rel_ascent = 0.0001;
320 parm.ascent_first_check = 500;
321 parm.ascent_check_invl = 100;
322 parm.maxsgriters = 2000;
323 parm.printflag = 3;
324 parm.printinvl = 50;
325 parm.heurinvl = 100000000;
326 parm.greentestinvl = 1;
327 parm.yellowtestinvl = 2;
328 parm.redtestinvl = 10;
329 parm.alphaint = 80;
330 parm.temp_dualfile = 0;
331 }
332
333 //#############################################################################
334
335 VOL_problem::VOL_problem() :
336 alpha_(-1),
337 lambda_(-1),
338 iter_(0),
339 value(-1),
340 psize(-1),
341 dsize(-1)
342 {
343 set_default_parm();
344 }
345
346 //
347 VOL_problem::VOL_problem(const char *filename) :
348 alpha_(-1),
349 lambda_(-1),
350 iter_(0),
351 value(-1),
352 psize(-1),
353 dsize(-1)
354 {
355 set_default_parm();
356 read_params(filename);
357 }
358
359 //######################################################################
360
361 VOL_problem::~VOL_problem()
362 {
363 delete[] parm.temp_dualfile;
364 }
365
366 //######################################################################
367 /// print information about the current iteration
368 void
369 VOL_problem::print_info(const int iter,
370 const VOL_primal& primal, const VOL_primal& pstar,
371 const VOL_dual& dual)
372 {
373 VOL_indc indc(dual_lb, dual_ub, primal, pstar, dual);
374 printf("%i. L=%f P=%f vu=%f infeas=%f\n asc=%f vmax=%f P-vu=%f xrc =%f\n",
375 iter, dual.lcost, pstar.value, indc.vu, indc.v2, indc.asc,
376 pstar.viol, pstar.value - indc.vu, dual.xrc);
377 }
378
379 //######################################################################
380 /// this is the Volume Algorithm
381 int
382 VOL_problem::solve(VOL_user_hooks& hooks, const bool use_preset_dual)
383 {
384
385 if (initialize(use_preset_dual) < 0) // initialize several parameters
386 return -1;
387
388 double best_ub = parm.ubinit; // upper bound
389 int retval = 0;
390
391 VOL_dvector rc(psize); // reduced costs
392
393 VOL_dual dual(dsize); // dual vector
394 dual.u = dsol;
395
396 VOL_primal primal(psize, dsize); // primal vector
397
398 retval = hooks.compute_rc(dual.u, rc); // compute reduced costs
399 if (retval < 0) return -1;
400 // solve relaxed problem
401 retval = hooks.solve_subproblem(dual.u, rc, dual.lcost,
402 primal.x, primal.v, primal.value);
403 if (retval < 0) return -1;
404 // set target for the lagrangian value
405 double target = readjust_target(-COIN_DBL_MAX/2, dual.lcost);
406 // find primal violation
407 primal.find_max_viol(dual_lb, dual_ub); // this may be left out for speed
408
409 VOL_primal pstar(primal); // set pstar=primal
410 pstar.find_max_viol(dual_lb, dual_ub); // set violation of pstar
411
412 dual.compute_xrc(pstar.x, primal.x, rc); // compute xrc
413
414 //
415 VOL_dual dstar(dual); // dstar is the best dual solution so far
416 VOL_dual dlast(dual); // set dlast=dual
417
418 iter_ = 0;
419 if (parm.printflag)
420 print_info(iter_, primal, pstar, dual);
421
422 VOL_swing swing;
423 VOL_alpha_factor alpha_factor;
424
425 double * lcost_sequence = new double[parm.ascent_check_invl];
426
427 const int ascent_first_check = VolMax(parm.ascent_first_check,
428 parm.ascent_check_invl);
429
430 for (iter_ = 1; iter_ <= parm.maxsgriters; ++iter_) { // main iteration
431 dlast = dual;
432 // take a dual step
433 dual.step(target, lambda_, dual_lb, dual_ub, pstar.v);
434 // compute reduced costs
435 retval = hooks.compute_rc(dual.u, rc);
436 if (retval < 0) break;
437 // solve relaxed problem
438 retval = hooks.solve_subproblem(dual.u, rc, dual.lcost,
439 primal.x, primal.v, primal.value);
440 if (retval < 0) break;
441 // set the violation of primal
442 primal.find_max_viol(dual_lb, dual_ub); // this may be left out for speed
443 dual.compute_xrc(pstar.x, primal.x, rc); // compute xrc
444
445 if (dual.lcost > dstar.lcost) {
446 dstar = dual; // update dstar
447 }
448 // check if target should be updated
449 target = readjust_target(target, dstar.lcost);
450 // compute inner product between the new subgradient and the
451 // last direction. This to decide among green, yellow, red
452 const double ascent = dual.ascent(primal.v, dlast.u);
453 // green, yellow, red
454 swing.cond(dlast, dual.lcost, ascent, iter_);
455 // change lambda if needed
456 lambda_ *= swing.lfactor(parm, lambda_, iter_);
457
458 if (iter_ % parm.alphaint == 0) { // change alpha if needed
459 const double fact = alpha_factor.factor(parm, dstar.lcost, alpha_);
460 if (fact != 1.0 && (parm.printflag & 2)) {
461 printf(" ------------decreasing alpha to %f\n", alpha_*fact);
462 }
463 alpha_ *= fact;
464 }
465 // convex combination with new primal vector
466 pstar.cc(power_heur(primal, pstar, dual), primal);
467 pstar.find_max_viol(dual_lb, dual_ub); // find maximum violation of pstar
468
469 if (swing.rd)
470 dual = dstar; // if there is no improvement reset dual=dstar
471
472 if ((iter_ % parm.printinvl == 0) && parm.printflag) { // printing iteration information
473 print_info(iter_, primal, pstar, dual);
474 swing.print();
475 }
476
477 if (iter_ % parm.heurinvl == 0) { // run primal heuristic
478 double ub = COIN_DBL_MAX;
479 retval = hooks.heuristics(*this, pstar.x, ub);
480 if (retval < 0) break;
481 if (ub < best_ub)
482 best_ub = ub;
483 }
484 // save dual solution every 500 iterations
485 if (iter_ % 500 == 0 && parm.temp_dualfile != 0) {
486 FILE* outfile = fopen(parm.temp_dualfile, "w");
487 const VOL_dvector& u = dstar.u;
488 const int m = u.size();
489 for (int i = 0; i < m; ++i) {
490 fprintf(outfile, "%i %f\n", i+1, u[i]);
491 }
492 fclose(outfile);
493 }
494
495 // test terminating criteria
496 const bool primal_feas =
497 (pstar.viol < parm.primal_abs_precision);
498 //const double gap = VolAbs(pstar.value - dstar.lcost);
499 const double gap = pstar.value - dstar.lcost;
500 const bool small_gap = VolAbs(dstar.lcost) < 0.0001 ?
501 (gap < parm.gap_abs_precision) :
502 ( (gap < parm.gap_abs_precision) ||
503 (gap/VolAbs(dstar.lcost) < parm.gap_rel_precision) );
504
505 // test optimality
506 if (primal_feas && small_gap){
507 if (parm.printflag) printf(" small lp gap \n");
508 break;
509 }
510
511 // test proving integer optimality
512 if (best_ub - dstar.lcost < parm.granularity){
513 if (parm.printflag) printf(" small ip gap \n");
514 break;
515 }
516
517 // test for non-improvement
518 const int k = iter_ % parm.ascent_check_invl;
519 if (iter_ > ascent_first_check) {
520 if (dstar.lcost - lcost_sequence[k] <
521 VolAbs(lcost_sequence[k]) * parm.minimum_rel_ascent){
522 if (parm.printflag) printf(" small improvement \n");
523 break;
524 }
525 }
526 lcost_sequence[k] = dstar.lcost;
527 }
528 delete[] lcost_sequence;
529
530 if (parm.printflag)
531 print_info(iter_, primal, pstar, dual);
532 // set solution to return
533 value = dstar.lcost;
534 psol = pstar.x;
535 dsol = dstar.u;
536 viol = pstar.v;
537
538 return retval;
539 }
540
541
542 /// A function to initialize a few variables
543 int
544 VOL_problem::initialize(const bool use_preset_dual) {
545 // setting bounds for dual variables
546 if (dual_lb.size() > 0) {
547 if (dual_lb.size() != dsize) {
548 printf("size inconsistent (dual_lb)\n");
549 return -1;
550 }
551 } else {
552 // fill it with -infinity
553 dual_lb.allocate(dsize);
554 dual_lb = - COIN_DBL_MAX;
555 }
556
557 if (dual_ub.size() > 0) {
558 if (dual_ub.size() != dsize) {
559 printf("size inconsistent (dual_ub)\n");
560 return -1;
561 }
562 } else {
563 // fill it with infinity
564 dual_ub.allocate(dsize);
565 dual_ub = COIN_DBL_MAX;
566 }
567 // setting initial values for parameters
568 alpha_ = parm.alphainit;
569 lambda_ = parm.lambdainit;
570 // check if there is an initial dual solution
571 if (use_preset_dual) {
572 if (dsol.size() != dsize) {
573 printf("size inconsistent (dsol)\n");
574 return -1;
575 }
576 } else {
577 dsol.clear();
578 dsol.allocate(dsize);
579 dsol = 0.0;
580 }
581
582 return 0;
583 }
584
585
586 /// Here we increase the target once we get within 5% of it
587 double
588 VOL_problem::readjust_target(const double oldtarget, const double lcost) const
589 {
590 double target = oldtarget;
591 if (lcost >= target - VolAbs(target) * 0.05) {
592 if (VolAbs(lcost) < 10.0) {
593 target = 10.0;
594 } else {
595 target += 0.025 * VolAbs(target);
596 target = VolMax(target, lcost + 0.05 * VolAbs(lcost));
597 }
598 if (target != oldtarget && (parm.printflag & 2)) {
599 printf(" **** readjusting target!!! new target = %f *****\n",
600 target);
601 }
602 }
603 return target;
604 }
605
606 /** Here we decide the value of alpha_fb to be used in the convex
607 combination. More details of this are in doc.ps
608 IN: alpha, primal, pstar, dual
609 OUT: pstar = alpha_fb * pstar + (1 - alpha_fb) * primal
610 */
611 double
612 VOL_problem::power_heur(const VOL_primal& primal, const VOL_primal& pstar,
613 const VOL_dual& dual) const
614 {
615 const double alpha = alpha_;
616
617 VOL_vh prod(alpha, dual_lb, dual_ub, primal.v, pstar.v, dual.u);
618
619 double a_asc = (alpha * prod.norm - prod.vh) / (prod.norm - prod.vh);
620 double alpha_fb;
621
622 if (prod.norm + prod.hh - 2.0 * prod.vh > 0.0)
623 alpha_fb = (prod.hh - prod.vh) / (prod.norm + prod.hh - 2.0 * prod.vh);
624 else
625 alpha_fb = alpha;
626
627 if (alpha_fb > alpha)
628 alpha_fb = alpha;
629 if (alpha_fb < a_asc)
630 alpha_fb = a_asc;
631 if (alpha_fb > 1.0)
632 alpha_fb = alpha;
633 if (alpha_fb < 0.0)
634 alpha_fb = alpha / 10.0;
635
636 return alpha_fb;
637 }
638