You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1438 lines
50 KiB

  1. /* glpios03.c (branch-and-cut driver) */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  6. * 2009, 2010, 2011, 2013 Andrew Makhorin, Department for Applied
  7. * Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
  8. * reserved. E-mail: <mao@gnu.org>.
  9. *
  10. * GLPK is free software: you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  18. * License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************/
  23. #include "env.h"
  24. #include "glpios.h"
  25. /***********************************************************************
  26. * show_progress - display current progress of the search
  27. *
  28. * This routine displays some information about current progress of the
  29. * search.
  30. *
  31. * The information includes:
  32. *
  33. * the current number of iterations performed by the simplex solver;
  34. *
  35. * the objective value for the best known integer feasible solution,
  36. * which is upper (minimization) or lower (maximization) global bound
  37. * for optimal solution of the original mip problem;
  38. *
  39. * the best local bound for active nodes, which is lower (minimization)
  40. * or upper (maximization) global bound for optimal solution of the
  41. * original mip problem;
  42. *
  43. * the relative mip gap, in percents;
  44. *
  45. * the number of open (active) subproblems;
  46. *
  47. * the number of completely explored subproblems, i.e. whose nodes have
  48. * been removed from the tree. */
  49. static void show_progress(glp_tree *T, int bingo)
  50. { int p;
  51. double temp;
  52. char best_mip[50], best_bound[50], *rho, rel_gap[50];
  53. /* format the best known integer feasible solution */
  54. if (T->mip->mip_stat == GLP_FEAS)
  55. sprintf(best_mip, "%17.9e", T->mip->mip_obj);
  56. else
  57. sprintf(best_mip, "%17s", "not found yet");
  58. /* determine reference number of an active subproblem whose local
  59. bound is best */
  60. p = ios_best_node(T);
  61. /* format the best bound */
  62. if (p == 0)
  63. sprintf(best_bound, "%17s", "tree is empty");
  64. else
  65. { temp = T->slot[p].node->bound;
  66. if (temp == -DBL_MAX)
  67. sprintf(best_bound, "%17s", "-inf");
  68. else if (temp == +DBL_MAX)
  69. sprintf(best_bound, "%17s", "+inf");
  70. else
  71. sprintf(best_bound, "%17.9e", temp);
  72. }
  73. /* choose the relation sign between global bounds */
  74. if (T->mip->dir == GLP_MIN)
  75. rho = ">=";
  76. else if (T->mip->dir == GLP_MAX)
  77. rho = "<=";
  78. else
  79. xassert(T != T);
  80. /* format the relative mip gap */
  81. temp = ios_relative_gap(T);
  82. if (temp == 0.0)
  83. sprintf(rel_gap, " 0.0%%");
  84. else if (temp < 0.001)
  85. sprintf(rel_gap, "< 0.1%%");
  86. else if (temp <= 9.999)
  87. sprintf(rel_gap, "%5.1f%%", 100.0 * temp);
  88. else
  89. sprintf(rel_gap, "%6s", "");
  90. /* display progress of the search */
  91. xprintf("+%6d: %s %s %s %s %s (%d; %d)\n",
  92. T->mip->it_cnt, bingo ? ">>>>>" : "mip =", best_mip, rho,
  93. best_bound, rel_gap, T->a_cnt, T->t_cnt - T->n_cnt);
  94. T->tm_lag = xtime();
  95. return;
  96. }
  97. /***********************************************************************
  98. * is_branch_hopeful - check if specified branch is hopeful
  99. *
  100. * This routine checks if the specified subproblem can have an integer
  101. * optimal solution which is better than the best known one.
  102. *
  103. * The check is based on comparison of the local objective bound stored
  104. * in the subproblem descriptor and the incumbent objective value which
  105. * is the global objective bound.
  106. *
  107. * If there is a chance that the specified subproblem can have a better
  108. * integer optimal solution, the routine returns non-zero. Otherwise, if
  109. * the corresponding branch can pruned, zero is returned. */
  110. static int is_branch_hopeful(glp_tree *T, int p)
  111. { xassert(1 <= p && p <= T->nslots);
  112. xassert(T->slot[p].node != NULL);
  113. return ios_is_hopeful(T, T->slot[p].node->bound);
  114. }
  115. /***********************************************************************
  116. * check_integrality - check integrality of basic solution
  117. *
  118. * This routine checks if the basic solution of LP relaxation of the
  119. * current subproblem satisfies to integrality conditions, i.e. that all
  120. * variables of integer kind have integral primal values. (The solution
  121. * is assumed to be optimal.)
  122. *
  123. * For each variable of integer kind the routine computes the following
  124. * quantity:
  125. *
  126. * ii(x[j]) = min(x[j] - floor(x[j]), ceil(x[j]) - x[j]), (1)
  127. *
  128. * which is a measure of the integer infeasibility (non-integrality) of
  129. * x[j] (for example, ii(2.1) = 0.1, ii(3.7) = 0.3, ii(5.0) = 0). It is
  130. * understood that 0 <= ii(x[j]) <= 0.5, and variable x[j] is integer
  131. * feasible if ii(x[j]) = 0. However, due to floating-point arithmetic
  132. * the routine checks less restrictive condition:
  133. *
  134. * ii(x[j]) <= tol_int, (2)
  135. *
  136. * where tol_int is a given tolerance (small positive number) and marks
  137. * each variable which does not satisfy to (2) as integer infeasible by
  138. * setting its fractionality flag.
  139. *
  140. * In order to characterize integer infeasibility of the basic solution
  141. * in the whole the routine computes two parameters: ii_cnt, which is
  142. * the number of variables with the fractionality flag set, and ii_sum,
  143. * which is the sum of integer infeasibilities (1). */
  144. static void check_integrality(glp_tree *T)
  145. { glp_prob *mip = T->mip;
  146. int j, type, ii_cnt = 0;
  147. double lb, ub, x, temp1, temp2, ii_sum = 0.0;
  148. /* walk through the set of columns (structural variables) */
  149. for (j = 1; j <= mip->n; j++)
  150. { GLPCOL *col = mip->col[j];
  151. T->non_int[j] = 0;
  152. /* if the column is not integer, skip it */
  153. if (col->kind != GLP_IV) continue;
  154. /* if the column is non-basic, it is integer feasible */
  155. if (col->stat != GLP_BS) continue;
  156. /* obtain the type and bounds of the column */
  157. type = col->type, lb = col->lb, ub = col->ub;
  158. /* obtain value of the column in optimal basic solution */
  159. x = col->prim;
  160. /* if the column's primal value is close to the lower bound,
  161. the column is integer feasible within given tolerance */
  162. if (type == GLP_LO || type == GLP_DB || type == GLP_FX)
  163. { temp1 = lb - T->parm->tol_int;
  164. temp2 = lb + T->parm->tol_int;
  165. if (temp1 <= x && x <= temp2) continue;
  166. #if 0
  167. /* the lower bound must not be violated */
  168. xassert(x >= lb);
  169. #else
  170. if (x < lb) continue;
  171. #endif
  172. }
  173. /* if the column's primal value is close to the upper bound,
  174. the column is integer feasible within given tolerance */
  175. if (type == GLP_UP || type == GLP_DB || type == GLP_FX)
  176. { temp1 = ub - T->parm->tol_int;
  177. temp2 = ub + T->parm->tol_int;
  178. if (temp1 <= x && x <= temp2) continue;
  179. #if 0
  180. /* the upper bound must not be violated */
  181. xassert(x <= ub);
  182. #else
  183. if (x > ub) continue;
  184. #endif
  185. }
  186. /* if the column's primal value is close to nearest integer,
  187. the column is integer feasible within given tolerance */
  188. temp1 = floor(x + 0.5) - T->parm->tol_int;
  189. temp2 = floor(x + 0.5) + T->parm->tol_int;
  190. if (temp1 <= x && x <= temp2) continue;
  191. /* otherwise the column is integer infeasible */
  192. T->non_int[j] = 1;
  193. /* increase the number of fractional-valued columns */
  194. ii_cnt++;
  195. /* compute the sum of integer infeasibilities */
  196. temp1 = x - floor(x);
  197. temp2 = ceil(x) - x;
  198. xassert(temp1 > 0.0 && temp2 > 0.0);
  199. ii_sum += (temp1 <= temp2 ? temp1 : temp2);
  200. }
  201. /* store ii_cnt and ii_sum to the current problem descriptor */
  202. xassert(T->curr != NULL);
  203. T->curr->ii_cnt = ii_cnt;
  204. T->curr->ii_sum = ii_sum;
  205. /* and also display these parameters */
  206. if (T->parm->msg_lev >= GLP_MSG_DBG)
  207. { if (ii_cnt == 0)
  208. xprintf("There are no fractional columns\n");
  209. else if (ii_cnt == 1)
  210. xprintf("There is one fractional column, integer infeasibil"
  211. "ity is %.3e\n", ii_sum);
  212. else
  213. xprintf("There are %d fractional columns, integer infeasibi"
  214. "lity is %.3e\n", ii_cnt, ii_sum);
  215. }
  216. return;
  217. }
  218. /***********************************************************************
  219. * record_solution - record better integer feasible solution
  220. *
  221. * This routine records optimal basic solution of LP relaxation of the
  222. * current subproblem, which being integer feasible is better than the
  223. * best known integer feasible solution. */
  224. static void record_solution(glp_tree *T)
  225. { glp_prob *mip = T->mip;
  226. int i, j;
  227. mip->mip_stat = GLP_FEAS;
  228. mip->mip_obj = mip->obj_val;
  229. for (i = 1; i <= mip->m; i++)
  230. { GLPROW *row = mip->row[i];
  231. row->mipx = row->prim;
  232. }
  233. for (j = 1; j <= mip->n; j++)
  234. { GLPCOL *col = mip->col[j];
  235. if (col->kind == GLP_CV)
  236. col->mipx = col->prim;
  237. else if (col->kind == GLP_IV)
  238. { /* value of the integer column must be integral */
  239. col->mipx = floor(col->prim + 0.5);
  240. }
  241. else
  242. xassert(col != col);
  243. }
  244. T->sol_cnt++;
  245. return;
  246. }
  247. /***********************************************************************
  248. * fix_by_red_cost - fix non-basic integer columns by reduced costs
  249. *
  250. * This routine fixes some non-basic integer columns if their reduced
  251. * costs indicate that increasing (decreasing) the column at least by
  252. * one involves the objective value becoming worse than the incumbent
  253. * objective value. */
  254. static void fix_by_red_cost(glp_tree *T)
  255. { glp_prob *mip = T->mip;
  256. int j, stat, fixed = 0;
  257. double obj, lb, ub, dj;
  258. /* the global bound must exist */
  259. xassert(T->mip->mip_stat == GLP_FEAS);
  260. /* basic solution of LP relaxation must be optimal */
  261. xassert(mip->pbs_stat == GLP_FEAS && mip->dbs_stat == GLP_FEAS);
  262. /* determine the objective function value */
  263. obj = mip->obj_val;
  264. /* walk through the column list */
  265. for (j = 1; j <= mip->n; j++)
  266. { GLPCOL *col = mip->col[j];
  267. /* if the column is not integer, skip it */
  268. if (col->kind != GLP_IV) continue;
  269. /* obtain bounds of j-th column */
  270. lb = col->lb, ub = col->ub;
  271. /* and determine its status and reduced cost */
  272. stat = col->stat, dj = col->dual;
  273. /* analyze the reduced cost */
  274. switch (mip->dir)
  275. { case GLP_MIN:
  276. /* minimization */
  277. if (stat == GLP_NL)
  278. { /* j-th column is non-basic on its lower bound */
  279. if (dj < 0.0) dj = 0.0;
  280. if (obj + dj >= mip->mip_obj)
  281. glp_set_col_bnds(mip, j, GLP_FX, lb, lb), fixed++;
  282. }
  283. else if (stat == GLP_NU)
  284. { /* j-th column is non-basic on its upper bound */
  285. if (dj > 0.0) dj = 0.0;
  286. if (obj - dj >= mip->mip_obj)
  287. glp_set_col_bnds(mip, j, GLP_FX, ub, ub), fixed++;
  288. }
  289. break;
  290. case GLP_MAX:
  291. /* maximization */
  292. if (stat == GLP_NL)
  293. { /* j-th column is non-basic on its lower bound */
  294. if (dj > 0.0) dj = 0.0;
  295. if (obj + dj <= mip->mip_obj)
  296. glp_set_col_bnds(mip, j, GLP_FX, lb, lb), fixed++;
  297. }
  298. else if (stat == GLP_NU)
  299. { /* j-th column is non-basic on its upper bound */
  300. if (dj < 0.0) dj = 0.0;
  301. if (obj - dj <= mip->mip_obj)
  302. glp_set_col_bnds(mip, j, GLP_FX, ub, ub), fixed++;
  303. }
  304. break;
  305. default:
  306. xassert(T != T);
  307. }
  308. }
  309. if (T->parm->msg_lev >= GLP_MSG_DBG)
  310. { if (fixed == 0)
  311. /* nothing to say */;
  312. else if (fixed == 1)
  313. xprintf("One column has been fixed by reduced cost\n");
  314. else
  315. xprintf("%d columns have been fixed by reduced costs\n",
  316. fixed);
  317. }
  318. /* fixing non-basic columns on their current bounds does not
  319. change the basic solution */
  320. xassert(mip->pbs_stat == GLP_FEAS && mip->dbs_stat == GLP_FEAS);
  321. return;
  322. }
  323. /***********************************************************************
  324. * branch_on - perform branching on specified variable
  325. *
  326. * This routine performs branching on j-th column (structural variable)
  327. * of the current subproblem. The specified column must be of integer
  328. * kind and must have a fractional value in optimal basic solution of
  329. * LP relaxation of the current subproblem (i.e. only columns for which
  330. * the flag non_int[j] is set are valid candidates to branch on).
  331. *
  332. * Let x be j-th structural variable, and beta be its primal fractional
  333. * value in the current basic solution. Branching on j-th variable is
  334. * dividing the current subproblem into two new subproblems, which are
  335. * identical to the current subproblem with the following exception: in
  336. * the first subproblem that begins the down-branch x has a new upper
  337. * bound x <= floor(beta), and in the second subproblem that begins the
  338. * up-branch x has a new lower bound x >= ceil(beta).
  339. *
  340. * Depending on estimation of local bounds for down- and up-branches
  341. * this routine returns the following:
  342. *
  343. * 0 - both branches have been created;
  344. * 1 - one branch is hopeless and has been pruned, so now the current
  345. * subproblem is other branch;
  346. * 2 - both branches are hopeless and have been pruned; new subproblem
  347. * selection is needed to continue the search. */
  348. static int branch_on(glp_tree *T, int j, int next)
  349. { glp_prob *mip = T->mip;
  350. IOSNPD *node;
  351. int m = mip->m;
  352. int n = mip->n;
  353. int type, dn_type, up_type, dn_bad, up_bad, p, ret, clone[1+2];
  354. double lb, ub, beta, new_ub, new_lb, dn_lp, up_lp, dn_bnd, up_bnd;
  355. /* determine bounds and value of x[j] in optimal solution to LP
  356. relaxation of the current subproblem */
  357. xassert(1 <= j && j <= n);
  358. type = mip->col[j]->type;
  359. lb = mip->col[j]->lb;
  360. ub = mip->col[j]->ub;
  361. beta = mip->col[j]->prim;
  362. /* determine new bounds of x[j] for down- and up-branches */
  363. new_ub = floor(beta);
  364. new_lb = ceil(beta);
  365. switch (type)
  366. { case GLP_FR:
  367. dn_type = GLP_UP;
  368. up_type = GLP_LO;
  369. break;
  370. case GLP_LO:
  371. xassert(lb <= new_ub);
  372. dn_type = (lb == new_ub ? GLP_FX : GLP_DB);
  373. xassert(lb + 1.0 <= new_lb);
  374. up_type = GLP_LO;
  375. break;
  376. case GLP_UP:
  377. xassert(new_ub <= ub - 1.0);
  378. dn_type = GLP_UP;
  379. xassert(new_lb <= ub);
  380. up_type = (new_lb == ub ? GLP_FX : GLP_DB);
  381. break;
  382. case GLP_DB:
  383. xassert(lb <= new_ub && new_ub <= ub - 1.0);
  384. dn_type = (lb == new_ub ? GLP_FX : GLP_DB);
  385. xassert(lb + 1.0 <= new_lb && new_lb <= ub);
  386. up_type = (new_lb == ub ? GLP_FX : GLP_DB);
  387. break;
  388. default:
  389. xassert(type != type);
  390. }
  391. /* compute local bounds to LP relaxation for both branches */
  392. ios_eval_degrad(T, j, &dn_lp, &up_lp);
  393. /* and improve them by rounding */
  394. dn_bnd = ios_round_bound(T, dn_lp);
  395. up_bnd = ios_round_bound(T, up_lp);
  396. /* check local bounds for down- and up-branches */
  397. dn_bad = !ios_is_hopeful(T, dn_bnd);
  398. up_bad = !ios_is_hopeful(T, up_bnd);
  399. if (dn_bad && up_bad)
  400. { if (T->parm->msg_lev >= GLP_MSG_DBG)
  401. xprintf("Both down- and up-branches are hopeless\n");
  402. ret = 2;
  403. goto done;
  404. }
  405. else if (up_bad)
  406. { if (T->parm->msg_lev >= GLP_MSG_DBG)
  407. xprintf("Up-branch is hopeless\n");
  408. glp_set_col_bnds(mip, j, dn_type, lb, new_ub);
  409. T->curr->lp_obj = dn_lp;
  410. if (mip->dir == GLP_MIN)
  411. { if (T->curr->bound < dn_bnd)
  412. T->curr->bound = dn_bnd;
  413. }
  414. else if (mip->dir == GLP_MAX)
  415. { if (T->curr->bound > dn_bnd)
  416. T->curr->bound = dn_bnd;
  417. }
  418. else
  419. xassert(mip != mip);
  420. ret = 1;
  421. goto done;
  422. }
  423. else if (dn_bad)
  424. { if (T->parm->msg_lev >= GLP_MSG_DBG)
  425. xprintf("Down-branch is hopeless\n");
  426. glp_set_col_bnds(mip, j, up_type, new_lb, ub);
  427. T->curr->lp_obj = up_lp;
  428. if (mip->dir == GLP_MIN)
  429. { if (T->curr->bound < up_bnd)
  430. T->curr->bound = up_bnd;
  431. }
  432. else if (mip->dir == GLP_MAX)
  433. { if (T->curr->bound > up_bnd)
  434. T->curr->bound = up_bnd;
  435. }
  436. else
  437. xassert(mip != mip);
  438. ret = 1;
  439. goto done;
  440. }
  441. /* both down- and up-branches seem to be hopeful */
  442. if (T->parm->msg_lev >= GLP_MSG_DBG)
  443. xprintf("Branching on column %d, primal value is %.9e\n",
  444. j, beta);
  445. /* determine the reference number of the current subproblem */
  446. xassert(T->curr != NULL);
  447. p = T->curr->p;
  448. T->curr->br_var = j;
  449. T->curr->br_val = beta;
  450. /* freeze the current subproblem */
  451. ios_freeze_node(T);
  452. /* create two clones of the current subproblem; the first clone
  453. begins the down-branch, the second one begins the up-branch */
  454. ios_clone_node(T, p, 2, clone);
  455. if (T->parm->msg_lev >= GLP_MSG_DBG)
  456. xprintf("Node %d begins down branch, node %d begins up branch "
  457. "\n", clone[1], clone[2]);
  458. /* set new upper bound of j-th column in the down-branch */
  459. node = T->slot[clone[1]].node;
  460. xassert(node != NULL);
  461. xassert(node->up != NULL);
  462. xassert(node->b_ptr == NULL);
  463. node->b_ptr = dmp_get_atom(T->pool, sizeof(IOSBND));
  464. node->b_ptr->k = m + j;
  465. node->b_ptr->type = (unsigned char)dn_type;
  466. node->b_ptr->lb = lb;
  467. node->b_ptr->ub = new_ub;
  468. node->b_ptr->next = NULL;
  469. node->lp_obj = dn_lp;
  470. if (mip->dir == GLP_MIN)
  471. { if (node->bound < dn_bnd)
  472. node->bound = dn_bnd;
  473. }
  474. else if (mip->dir == GLP_MAX)
  475. { if (node->bound > dn_bnd)
  476. node->bound = dn_bnd;
  477. }
  478. else
  479. xassert(mip != mip);
  480. /* set new lower bound of j-th column in the up-branch */
  481. node = T->slot[clone[2]].node;
  482. xassert(node != NULL);
  483. xassert(node->up != NULL);
  484. xassert(node->b_ptr == NULL);
  485. node->b_ptr = dmp_get_atom(T->pool, sizeof(IOSBND));
  486. node->b_ptr->k = m + j;
  487. node->b_ptr->type = (unsigned char)up_type;
  488. node->b_ptr->lb = new_lb;
  489. node->b_ptr->ub = ub;
  490. node->b_ptr->next = NULL;
  491. node->lp_obj = up_lp;
  492. if (mip->dir == GLP_MIN)
  493. { if (node->bound < up_bnd)
  494. node->bound = up_bnd;
  495. }
  496. else if (mip->dir == GLP_MAX)
  497. { if (node->bound > up_bnd)
  498. node->bound = up_bnd;
  499. }
  500. else
  501. xassert(mip != mip);
  502. /* suggest the subproblem to be solved next */
  503. xassert(T->child == 0);
  504. if (next == GLP_NO_BRNCH)
  505. T->child = 0;
  506. else if (next == GLP_DN_BRNCH)
  507. T->child = clone[1];
  508. else if (next == GLP_UP_BRNCH)
  509. T->child = clone[2];
  510. else
  511. xassert(next != next);
  512. ret = 0;
  513. done: return ret;
  514. }
  515. /***********************************************************************
  516. * cleanup_the_tree - prune hopeless branches from the tree
  517. *
  518. * This routine walks through the active list and checks the local
  519. * bound for every active subproblem. If the local bound indicates that
  520. * the subproblem cannot have integer optimal solution better than the
  521. * incumbent objective value, the routine deletes such subproblem that,
  522. * in turn, involves pruning the corresponding branch of the tree. */
  523. static void cleanup_the_tree(glp_tree *T)
  524. { IOSNPD *node, *next_node;
  525. int count = 0;
  526. /* the global bound must exist */
  527. xassert(T->mip->mip_stat == GLP_FEAS);
  528. /* walk through the list of active subproblems */
  529. for (node = T->head; node != NULL; node = next_node)
  530. { /* deleting some active problem node may involve deleting its
  531. parents recursively; however, all its parents being created
  532. *before* it are always *precede* it in the node list, so
  533. the next problem node is never affected by such deletion */
  534. next_node = node->next;
  535. /* if the branch is hopeless, prune it */
  536. if (!is_branch_hopeful(T, node->p))
  537. ios_delete_node(T, node->p), count++;
  538. }
  539. if (T->parm->msg_lev >= GLP_MSG_DBG)
  540. { if (count == 1)
  541. xprintf("One hopeless branch has been pruned\n");
  542. else if (count > 1)
  543. xprintf("%d hopeless branches have been pruned\n", count);
  544. }
  545. return;
  546. }
  547. #if 0 /* 09/VII-2013 */
  548. /***********************************************************************
  549. * round_heur - simple rounding heuristic
  550. *
  551. * This routine attempts to guess an integer feasible solution by
  552. * simple rounding values of all integer variables in basic solution to
  553. * nearest integers. */
  554. static int round_heur(glp_tree *T)
  555. { glp_prob *P = T->mip;
  556. int m = P->m;
  557. int n = P->n;
  558. int i, j, ret;
  559. double *x;
  560. /* compute rounded values of variables */
  561. x = talloc(1+n, double);
  562. for (j = 1; j <= n; j++)
  563. { GLPCOL *col = P->col[j];
  564. if (col->kind == GLP_IV)
  565. { /* integer variable */
  566. x[j] = floor(col->prim + 0.5);
  567. }
  568. else if (col->type == GLP_FX)
  569. { /* fixed variable */
  570. x[j] = col->prim;
  571. }
  572. else
  573. { /* non-integer non-fixed variable */
  574. ret = 3;
  575. goto done;
  576. }
  577. }
  578. /* check that no constraints are violated */
  579. for (i = 1; i <= m; i++)
  580. { GLPROW *row = P->row[i];
  581. int type = row->type;
  582. GLPAIJ *aij;
  583. double sum;
  584. if (type == GLP_FR)
  585. continue;
  586. /* compute value of linear form */
  587. sum = 0.0;
  588. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  589. sum += aij->val * x[aij->col->j];
  590. /* check lower bound */
  591. if (type == GLP_LO || type == GLP_DB || type == GLP_FX)
  592. { if (sum < row->lb - 1e-9)
  593. { /* lower bound is violated */
  594. ret = 2;
  595. goto done;
  596. }
  597. }
  598. /* check upper bound */
  599. if (type == GLP_UP || type == GLP_DB || type == GLP_FX)
  600. { if (sum > row->ub + 1e-9)
  601. { /* upper bound is violated */
  602. ret = 2;
  603. goto done;
  604. }
  605. }
  606. }
  607. /* rounded solution is integer feasible */
  608. if (glp_ios_heur_sol(T, x) == 0)
  609. { /* solution is accepted */
  610. ret = 0;
  611. }
  612. else
  613. { /* solution is rejected */
  614. ret = 1;
  615. }
  616. done: tfree(x);
  617. return ret;
  618. }
  619. #else
  620. /***********************************************************************
  621. * round_heur - simple rounding heuristic
  622. *
  623. * This routine attempts to guess an integer feasible solution by
  624. * simple rounding values of all integer variables in basic solution to
  625. * nearest integers. */
  626. static int round_heur(glp_tree *T)
  627. { glp_prob *P = T->mip;
  628. /*int m = P->m;*/
  629. int n = P->n;
  630. int i, j, ret;
  631. double *x;
  632. /* compute rounded values of variables */
  633. x = talloc(1+n, double);
  634. for (j = 1; j <= n; j++)
  635. { GLPCOL *col = P->col[j];
  636. if (col->kind == GLP_IV)
  637. { /* integer variable */
  638. x[j] = floor(col->prim + 0.5);
  639. }
  640. else if (col->type == GLP_FX)
  641. { /* fixed variable */
  642. x[j] = col->prim;
  643. }
  644. else
  645. { /* non-integer non-fixed variable */
  646. ret = 3;
  647. goto done;
  648. }
  649. }
  650. /* check that no constraints are violated */
  651. for (i = 1; i <= T->orig_m; i++)
  652. { int type = T->orig_type[i];
  653. GLPAIJ *aij;
  654. double sum;
  655. if (type == GLP_FR)
  656. continue;
  657. /* compute value of linear form */
  658. sum = 0.0;
  659. for (aij = P->row[i]->ptr; aij != NULL; aij = aij->r_next)
  660. sum += aij->val * x[aij->col->j];
  661. /* check lower bound */
  662. if (type == GLP_LO || type == GLP_DB || type == GLP_FX)
  663. { if (sum < T->orig_lb[i] - 1e-9)
  664. { /* lower bound is violated */
  665. ret = 2;
  666. goto done;
  667. }
  668. }
  669. /* check upper bound */
  670. if (type == GLP_UP || type == GLP_DB || type == GLP_FX)
  671. { if (sum > T->orig_ub[i] + 1e-9)
  672. { /* upper bound is violated */
  673. ret = 2;
  674. goto done;
  675. }
  676. }
  677. }
  678. /* rounded solution is integer feasible */
  679. if (glp_ios_heur_sol(T, x) == 0)
  680. { /* solution is accepted */
  681. ret = 0;
  682. }
  683. else
  684. { /* solution is rejected */
  685. ret = 1;
  686. }
  687. done: tfree(x);
  688. return ret;
  689. }
  690. #endif
  691. #if 0
  692. #define round_heur round_heur2
  693. static int round_heur(glp_tree *T)
  694. { glp_prob *lp;
  695. int *ind, ret, i, j, len;
  696. double *val;
  697. lp = glp_create_prob();
  698. ind = talloc(1+T->mip->n, int);
  699. val = talloc(1+T->mip->n, double);
  700. glp_add_rows(lp, T->orig_m);
  701. glp_add_cols(lp, T->n);
  702. for (i = 1; i <= T->orig_m; i++)
  703. { glp_set_row_bnds(lp, i,
  704. T->orig_type[i], T->orig_lb[i], T->orig_ub[i]);
  705. len = glp_get_mat_row(T->mip, i, ind, val);
  706. glp_set_mat_row(lp, i, len, ind, val);
  707. }
  708. for (j = 1; j <= T->n; j++)
  709. { GLPCOL *col = T->mip->col[j];
  710. glp_set_obj_coef(lp, j, col->coef);
  711. if (col->kind == GLP_IV)
  712. { /* integer variable */
  713. glp_set_col_bnds(lp, j, GLP_FX, floor(col->prim + .5), 0);
  714. }
  715. else
  716. { glp_set_col_bnds(lp, j, T->orig_type[T->orig_m+j],
  717. T->orig_lb[T->orig_m+j], T->orig_ub[T->orig_m+j]);
  718. }
  719. }
  720. glp_term_out(GLP_OFF);
  721. glp_adv_basis(lp, 0);
  722. ret = glp_simplex(lp, NULL);
  723. glp_term_out(GLP_ON);
  724. if (ret != 0)
  725. { ret = 1;
  726. goto done;
  727. }
  728. if (glp_get_status(lp) != GLP_OPT)
  729. { ret = 2;
  730. goto done;
  731. }
  732. for (j = 1; j <= lp->n; j++)
  733. val[j] = lp->col[j]->prim;
  734. if (glp_ios_heur_sol(T, val) == 0)
  735. ret = 0;
  736. else
  737. ret = 3;
  738. done: glp_delete_prob(lp);
  739. tfree(ind);
  740. tfree(val);
  741. return ret;
  742. }
  743. #endif
  744. /**********************************************************************/
  745. static void generate_cuts(glp_tree *T)
  746. { /* generate generic cuts with built-in generators */
  747. if (!(T->parm->mir_cuts == GLP_ON ||
  748. T->parm->gmi_cuts == GLP_ON ||
  749. T->parm->cov_cuts == GLP_ON ||
  750. T->parm->clq_cuts == GLP_ON)) goto done;
  751. #if 1 /* 20/IX-2008 */
  752. { int i, max_cuts, added_cuts;
  753. max_cuts = T->n;
  754. if (max_cuts < 1000) max_cuts = 1000;
  755. added_cuts = 0;
  756. for (i = T->orig_m+1; i <= T->mip->m; i++)
  757. { if (T->mip->row[i]->origin == GLP_RF_CUT)
  758. added_cuts++;
  759. }
  760. /* xprintf("added_cuts = %d\n", added_cuts); */
  761. if (added_cuts >= max_cuts) goto done;
  762. }
  763. #endif
  764. /* generate and add to POOL all cuts violated by x* */
  765. if (T->parm->gmi_cuts == GLP_ON)
  766. { if (T->curr->changed < 7)
  767. ios_gmi_gen(T);
  768. }
  769. if (T->parm->mir_cuts == GLP_ON)
  770. { xassert(T->mir_gen != NULL);
  771. ios_mir_gen(T, T->mir_gen);
  772. }
  773. if (T->parm->cov_cuts == GLP_ON)
  774. { /* cover cuts works well along with mir cuts */
  775. /*if (T->round <= 5)*/
  776. ios_cov_gen(T);
  777. }
  778. if (T->parm->clq_cuts == GLP_ON)
  779. { if (T->clq_gen != NULL)
  780. #if 0 /* 29/VI-2013 */
  781. { if (T->curr->level == 0 && T->curr->changed < 50 ||
  782. T->curr->level > 0 && T->curr->changed < 5)
  783. #else /* FIXME */
  784. { if (T->curr->level == 0 && T->curr->changed < 500 ||
  785. T->curr->level > 0 && T->curr->changed < 50)
  786. #endif
  787. ios_clq_gen(T, T->clq_gen);
  788. }
  789. }
  790. done: return;
  791. }
  792. /**********************************************************************/
  793. static void remove_cuts(glp_tree *T)
  794. { /* remove inactive cuts (some valueable globally valid cut might
  795. be saved in the global cut pool) */
  796. int i, cnt = 0, *num = NULL;
  797. xassert(T->curr != NULL);
  798. for (i = T->orig_m+1; i <= T->mip->m; i++)
  799. { if (T->mip->row[i]->origin == GLP_RF_CUT &&
  800. T->mip->row[i]->level == T->curr->level &&
  801. T->mip->row[i]->stat == GLP_BS)
  802. { if (num == NULL)
  803. num = xcalloc(1+T->mip->m, sizeof(int));
  804. num[++cnt] = i;
  805. }
  806. }
  807. if (cnt > 0)
  808. { glp_del_rows(T->mip, cnt, num);
  809. #if 0
  810. xprintf("%d inactive cut(s) removed\n", cnt);
  811. #endif
  812. xfree(num);
  813. xassert(glp_factorize(T->mip) == 0);
  814. }
  815. return;
  816. }
  817. /**********************************************************************/
  818. static void display_cut_info(glp_tree *T)
  819. { glp_prob *mip = T->mip;
  820. int i, gmi = 0, mir = 0, cov = 0, clq = 0, app = 0;
  821. for (i = mip->m; i > 0; i--)
  822. { GLPROW *row;
  823. row = mip->row[i];
  824. /* if (row->level < T->curr->level) break; */
  825. if (row->origin == GLP_RF_CUT)
  826. { if (row->klass == GLP_RF_GMI)
  827. gmi++;
  828. else if (row->klass == GLP_RF_MIR)
  829. mir++;
  830. else if (row->klass == GLP_RF_COV)
  831. cov++;
  832. else if (row->klass == GLP_RF_CLQ)
  833. clq++;
  834. else
  835. app++;
  836. }
  837. }
  838. xassert(T->curr != NULL);
  839. if (gmi + mir + cov + clq + app > 0)
  840. { xprintf("Cuts on level %d:", T->curr->level);
  841. if (gmi > 0) xprintf(" gmi = %d;", gmi);
  842. if (mir > 0) xprintf(" mir = %d;", mir);
  843. if (cov > 0) xprintf(" cov = %d;", cov);
  844. if (clq > 0) xprintf(" clq = %d;", clq);
  845. if (app > 0) xprintf(" app = %d;", app);
  846. xprintf("\n");
  847. }
  848. return;
  849. }
  850. /***********************************************************************
  851. * NAME
  852. *
  853. * ios_driver - branch-and-cut driver
  854. *
  855. * SYNOPSIS
  856. *
  857. * #include "glpios.h"
  858. * int ios_driver(glp_tree *T);
  859. *
  860. * DESCRIPTION
  861. *
  862. * The routine ios_driver is a branch-and-cut driver. It controls the
  863. * MIP solution process.
  864. *
  865. * RETURNS
  866. *
  867. * 0 The MIP problem instance has been successfully solved. This code
  868. * does not necessarily mean that the solver has found optimal
  869. * solution. It only means that the solution process was successful.
  870. *
  871. * GLP_EFAIL
  872. * The search was prematurely terminated due to the solver failure.
  873. *
  874. * GLP_EMIPGAP
  875. * The search was prematurely terminated, because the relative mip
  876. * gap tolerance has been reached.
  877. *
  878. * GLP_ETMLIM
  879. * The search was prematurely terminated, because the time limit has
  880. * been exceeded.
  881. *
  882. * GLP_ESTOP
  883. * The search was prematurely terminated by application. */
  884. int ios_driver(glp_tree *T)
  885. { int p, curr_p, p_stat, d_stat, ret;
  886. #if 1 /* carry out to glp_tree */
  887. int pred_p = 0;
  888. /* if the current subproblem has been just created due to
  889. branching, pred_p is the reference number of its parent
  890. subproblem, otherwise pred_p is zero */
  891. #endif
  892. #if 1 /* 18/VII-2013 */
  893. int bad_cut;
  894. double old_obj;
  895. #endif
  896. #if 0 /* 10/VI-2013 */
  897. glp_long ttt = T->tm_beg;
  898. #else
  899. double ttt = T->tm_beg;
  900. #endif
  901. #if 0
  902. ((glp_iocp *)T->parm)->msg_lev = GLP_MSG_DBG;
  903. #endif
  904. /* on entry to the B&B driver it is assumed that the active list
  905. contains the only active (i.e. root) subproblem, which is the
  906. original MIP problem to be solved */
  907. loop: /* main loop starts here */
  908. /* at this point the current subproblem does not exist */
  909. xassert(T->curr == NULL);
  910. /* if the active list is empty, the search is finished */
  911. if (T->head == NULL)
  912. { if (T->parm->msg_lev >= GLP_MSG_DBG)
  913. xprintf("Active list is empty!\n");
  914. #if 0 /* 10/VI-2013 */
  915. xassert(dmp_in_use(T->pool).lo == 0);
  916. #else
  917. xassert(dmp_in_use(T->pool) == 0);
  918. #endif
  919. ret = 0;
  920. goto done;
  921. }
  922. /* select some active subproblem to continue the search */
  923. xassert(T->next_p == 0);
  924. /* let the application program select subproblem */
  925. if (T->parm->cb_func != NULL)
  926. { xassert(T->reason == 0);
  927. T->reason = GLP_ISELECT;
  928. T->parm->cb_func(T, T->parm->cb_info);
  929. T->reason = 0;
  930. if (T->stop)
  931. { ret = GLP_ESTOP;
  932. goto done;
  933. }
  934. }
  935. if (T->next_p != 0)
  936. { /* the application program has selected something */
  937. ;
  938. }
  939. else if (T->a_cnt == 1)
  940. { /* the only active subproblem exists, so select it */
  941. xassert(T->head->next == NULL);
  942. T->next_p = T->head->p;
  943. }
  944. else if (T->child != 0)
  945. { /* select one of branching childs suggested by the branching
  946. heuristic */
  947. T->next_p = T->child;
  948. }
  949. else
  950. { /* select active subproblem as specified by the backtracking
  951. technique option */
  952. T->next_p = ios_choose_node(T);
  953. }
  954. /* the active subproblem just selected becomes current */
  955. ios_revive_node(T, T->next_p);
  956. T->next_p = T->child = 0;
  957. /* invalidate pred_p, if it is not the reference number of the
  958. parent of the current subproblem */
  959. if (T->curr->up != NULL && T->curr->up->p != pred_p) pred_p = 0;
  960. /* determine the reference number of the current subproblem */
  961. p = T->curr->p;
  962. if (T->parm->msg_lev >= GLP_MSG_DBG)
  963. { xprintf("-----------------------------------------------------"
  964. "-------------------\n");
  965. xprintf("Processing node %d at level %d\n", p, T->curr->level);
  966. }
  967. #if 0
  968. if (p == 1)
  969. glp_write_lp(T->mip, NULL, "root.lp");
  970. #endif
  971. /* if it is the root subproblem, initialize cut generators */
  972. if (p == 1)
  973. { if (T->parm->gmi_cuts == GLP_ON)
  974. { if (T->parm->msg_lev >= GLP_MSG_ALL)
  975. xprintf("Gomory's cuts enabled\n");
  976. }
  977. if (T->parm->mir_cuts == GLP_ON)
  978. { if (T->parm->msg_lev >= GLP_MSG_ALL)
  979. xprintf("MIR cuts enabled\n");
  980. xassert(T->mir_gen == NULL);
  981. T->mir_gen = ios_mir_init(T);
  982. }
  983. if (T->parm->cov_cuts == GLP_ON)
  984. { if (T->parm->msg_lev >= GLP_MSG_ALL)
  985. xprintf("Cover cuts enabled\n");
  986. }
  987. if (T->parm->clq_cuts == GLP_ON)
  988. { xassert(T->clq_gen == NULL);
  989. if (T->parm->msg_lev >= GLP_MSG_ALL)
  990. xprintf("Clique cuts enabled\n");
  991. T->clq_gen = ios_clq_init(T);
  992. }
  993. }
  994. #if 1 /* 18/VII-2013 */
  995. bad_cut = 0;
  996. #endif
  997. more: /* minor loop starts here */
  998. /* at this point the current subproblem needs either to be solved
  999. for the first time or re-optimized due to reformulation */
  1000. /* display current progress of the search */
  1001. if (T->parm->msg_lev >= GLP_MSG_DBG ||
  1002. T->parm->msg_lev >= GLP_MSG_ON &&
  1003. (double)(T->parm->out_frq - 1) <=
  1004. 1000.0 * xdifftime(xtime(), T->tm_lag))
  1005. show_progress(T, 0);
  1006. if (T->parm->msg_lev >= GLP_MSG_ALL &&
  1007. xdifftime(xtime(), ttt) >= 60.0)
  1008. #if 0 /* 16/II-2012 */
  1009. { glp_long total;
  1010. glp_mem_usage(NULL, NULL, &total, NULL);
  1011. xprintf("Time used: %.1f secs. Memory used: %.1f Mb.\n",
  1012. xdifftime(xtime(), T->tm_beg), xltod(total) / 1048576.0);
  1013. ttt = xtime();
  1014. }
  1015. #else
  1016. { size_t total;
  1017. glp_mem_usage(NULL, NULL, &total, NULL);
  1018. xprintf("Time used: %.1f secs. Memory used: %.1f Mb.\n",
  1019. xdifftime(xtime(), T->tm_beg), (double)total / 1048576.0);
  1020. ttt = xtime();
  1021. }
  1022. #endif
  1023. /* check the mip gap */
  1024. if (T->parm->mip_gap > 0.0 &&
  1025. ios_relative_gap(T) <= T->parm->mip_gap)
  1026. { if (T->parm->msg_lev >= GLP_MSG_DBG)
  1027. xprintf("Relative gap tolerance reached; search terminated "
  1028. "\n");
  1029. ret = GLP_EMIPGAP;
  1030. goto done;
  1031. }
  1032. /* check if the time limit has been exhausted */
  1033. if (T->parm->tm_lim < INT_MAX &&
  1034. (double)(T->parm->tm_lim - 1) <=
  1035. 1000.0 * xdifftime(xtime(), T->tm_beg))
  1036. { if (T->parm->msg_lev >= GLP_MSG_DBG)
  1037. xprintf("Time limit exhausted; search terminated\n");
  1038. ret = GLP_ETMLIM;
  1039. goto done;
  1040. }
  1041. /* let the application program preprocess the subproblem */
  1042. if (T->parm->cb_func != NULL)
  1043. { xassert(T->reason == 0);
  1044. T->reason = GLP_IPREPRO;
  1045. T->parm->cb_func(T, T->parm->cb_info);
  1046. T->reason = 0;
  1047. if (T->stop)
  1048. { ret = GLP_ESTOP;
  1049. goto done;
  1050. }
  1051. }
  1052. /* perform basic preprocessing */
  1053. if (T->parm->pp_tech == GLP_PP_NONE)
  1054. ;
  1055. else if (T->parm->pp_tech == GLP_PP_ROOT)
  1056. { if (T->curr->level == 0)
  1057. { if (ios_preprocess_node(T, 100))
  1058. goto fath;
  1059. }
  1060. }
  1061. else if (T->parm->pp_tech == GLP_PP_ALL)
  1062. { if (ios_preprocess_node(T, T->curr->level == 0 ? 100 : 10))
  1063. goto fath;
  1064. }
  1065. else
  1066. xassert(T != T);
  1067. /* preprocessing may improve the global bound */
  1068. if (!is_branch_hopeful(T, p))
  1069. { xprintf("*** not tested yet ***\n");
  1070. goto fath;
  1071. }
  1072. /* solve LP relaxation of the current subproblem */
  1073. if (T->parm->msg_lev >= GLP_MSG_DBG)
  1074. xprintf("Solving LP relaxation...\n");
  1075. ret = ios_solve_node(T);
  1076. if (!(ret == 0 || ret == GLP_EOBJLL || ret == GLP_EOBJUL))
  1077. { if (T->parm->msg_lev >= GLP_MSG_ERR)
  1078. xprintf("ios_driver: unable to solve current LP relaxation;"
  1079. " glp_simplex returned %d\n", ret);
  1080. ret = GLP_EFAIL;
  1081. goto done;
  1082. }
  1083. /* analyze status of the basic solution to LP relaxation found */
  1084. p_stat = T->mip->pbs_stat;
  1085. d_stat = T->mip->dbs_stat;
  1086. if (p_stat == GLP_FEAS && d_stat == GLP_FEAS)
  1087. { /* LP relaxation has optimal solution */
  1088. if (T->parm->msg_lev >= GLP_MSG_DBG)
  1089. xprintf("Found optimal solution to LP relaxation\n");
  1090. }
  1091. else if (d_stat == GLP_NOFEAS)
  1092. { /* LP relaxation has no dual feasible solution */
  1093. /* since the current subproblem cannot have a larger feasible
  1094. region than its parent, there is something wrong */
  1095. if (T->parm->msg_lev >= GLP_MSG_ERR)
  1096. xprintf("ios_driver: current LP relaxation has no dual feas"
  1097. "ible solution\n");
  1098. ret = GLP_EFAIL;
  1099. goto done;
  1100. }
  1101. else if (p_stat == GLP_INFEAS && d_stat == GLP_FEAS)
  1102. { /* LP relaxation has no primal solution which is better than
  1103. the incumbent objective value */
  1104. xassert(T->mip->mip_stat == GLP_FEAS);
  1105. if (T->parm->msg_lev >= GLP_MSG_DBG)
  1106. xprintf("LP relaxation has no solution better than incumben"
  1107. "t objective value\n");
  1108. /* prune the branch */
  1109. goto fath;
  1110. }
  1111. else if (p_stat == GLP_NOFEAS)
  1112. { /* LP relaxation has no primal feasible solution */
  1113. if (T->parm->msg_lev >= GLP_MSG_DBG)
  1114. xprintf("LP relaxation has no feasible solution\n");
  1115. /* prune the branch */
  1116. goto fath;
  1117. }
  1118. else
  1119. { /* other cases cannot appear */
  1120. xassert(T->mip != T->mip);
  1121. }
  1122. /* at this point basic solution to LP relaxation of the current
  1123. subproblem is optimal */
  1124. xassert(p_stat == GLP_FEAS && d_stat == GLP_FEAS);
  1125. xassert(T->curr != NULL);
  1126. T->curr->lp_obj = T->mip->obj_val;
  1127. /* thus, it defines a local bound to integer optimal solution of
  1128. the current subproblem */
  1129. { double bound = T->mip->obj_val;
  1130. /* some local bound to the current subproblem could be already
  1131. set before, so we should only improve it */
  1132. bound = ios_round_bound(T, bound);
  1133. if (T->mip->dir == GLP_MIN)
  1134. { if (T->curr->bound < bound)
  1135. T->curr->bound = bound;
  1136. }
  1137. else if (T->mip->dir == GLP_MAX)
  1138. { if (T->curr->bound > bound)
  1139. T->curr->bound = bound;
  1140. }
  1141. else
  1142. xassert(T->mip != T->mip);
  1143. if (T->parm->msg_lev >= GLP_MSG_DBG)
  1144. xprintf("Local bound is %.9e\n", bound);
  1145. }
  1146. /* if the local bound indicates that integer optimal solution of
  1147. the current subproblem cannot be better than the global bound,
  1148. prune the branch */
  1149. if (!is_branch_hopeful(T, p))
  1150. { if (T->parm->msg_lev >= GLP_MSG_DBG)
  1151. xprintf("Current branch is hopeless and can be pruned\n");
  1152. goto fath;
  1153. }
  1154. /* let the application program generate additional rows ("lazy"
  1155. constraints) */
  1156. xassert(T->reopt == 0);
  1157. xassert(T->reinv == 0);
  1158. if (T->parm->cb_func != NULL)
  1159. { xassert(T->reason == 0);
  1160. T->reason = GLP_IROWGEN;
  1161. T->parm->cb_func(T, T->parm->cb_info);
  1162. T->reason = 0;
  1163. if (T->stop)
  1164. { ret = GLP_ESTOP;
  1165. goto done;
  1166. }
  1167. if (T->reopt)
  1168. { /* some rows were added; re-optimization is needed */
  1169. T->reopt = T->reinv = 0;
  1170. goto more;
  1171. }
  1172. if (T->reinv)
  1173. { /* no rows were added, however, some inactive rows were
  1174. removed */
  1175. T->reinv = 0;
  1176. xassert(glp_factorize(T->mip) == 0);
  1177. }
  1178. }
  1179. /* check if the basic solution is integer feasible */
  1180. check_integrality(T);
  1181. /* if the basic solution satisfies to all integrality conditions,
  1182. it is a new, better integer feasible solution */
  1183. if (T->curr->ii_cnt == 0)
  1184. { if (T->parm->msg_lev >= GLP_MSG_DBG)
  1185. xprintf("New integer feasible solution found\n");
  1186. if (T->parm->msg_lev >= GLP_MSG_ALL)
  1187. display_cut_info(T);
  1188. record_solution(T);
  1189. if (T->parm->msg_lev >= GLP_MSG_ON)
  1190. show_progress(T, 1);
  1191. #if 1 /* 11/VII-2013 */
  1192. ios_process_sol(T);
  1193. #endif
  1194. /* make the application program happy */
  1195. if (T->parm->cb_func != NULL)
  1196. { xassert(T->reason == 0);
  1197. T->reason = GLP_IBINGO;
  1198. T->parm->cb_func(T, T->parm->cb_info);
  1199. T->reason = 0;
  1200. if (T->stop)
  1201. { ret = GLP_ESTOP;
  1202. goto done;
  1203. }
  1204. }
  1205. /* since the current subproblem has been fathomed, prune its
  1206. branch */
  1207. goto fath;
  1208. }
  1209. /* at this point basic solution to LP relaxation of the current
  1210. subproblem is optimal, but integer infeasible */
  1211. /* try to fix some non-basic structural variables of integer kind
  1212. on their current bounds due to reduced costs */
  1213. if (T->mip->mip_stat == GLP_FEAS)
  1214. fix_by_red_cost(T);
  1215. /* let the application program try to find some solution to the
  1216. original MIP with a primal heuristic */
  1217. if (T->parm->cb_func != NULL)
  1218. { xassert(T->reason == 0);
  1219. T->reason = GLP_IHEUR;
  1220. T->parm->cb_func(T, T->parm->cb_info);
  1221. T->reason = 0;
  1222. if (T->stop)
  1223. { ret = GLP_ESTOP;
  1224. goto done;
  1225. }
  1226. /* check if the current branch became hopeless */
  1227. if (!is_branch_hopeful(T, p))
  1228. { if (T->parm->msg_lev >= GLP_MSG_DBG)
  1229. xprintf("Current branch became hopeless and can be prune"
  1230. "d\n");
  1231. goto fath;
  1232. }
  1233. }
  1234. /* try to find solution with the feasibility pump heuristic */
  1235. if (T->parm->fp_heur)
  1236. { xassert(T->reason == 0);
  1237. T->reason = GLP_IHEUR;
  1238. ios_feas_pump(T);
  1239. T->reason = 0;
  1240. /* check if the current branch became hopeless */
  1241. if (!is_branch_hopeful(T, p))
  1242. { if (T->parm->msg_lev >= GLP_MSG_DBG)
  1243. xprintf("Current branch became hopeless and can be prune"
  1244. "d\n");
  1245. goto fath;
  1246. }
  1247. }
  1248. #if 1 /* 25/V-2013 */
  1249. /* try to find solution with the proximity search heuristic */
  1250. if (T->parm->ps_heur)
  1251. { xassert(T->reason == 0);
  1252. T->reason = GLP_IHEUR;
  1253. ios_proxy_heur(T);
  1254. T->reason = 0;
  1255. /* check if the current branch became hopeless */
  1256. if (!is_branch_hopeful(T, p))
  1257. { if (T->parm->msg_lev >= GLP_MSG_DBG)
  1258. xprintf("Current branch became hopeless and can be prune"
  1259. "d\n");
  1260. goto fath;
  1261. }
  1262. }
  1263. #endif
  1264. #if 1 /* 09/VII-2013 */
  1265. /* try to find solution with a simple rounding heuristic */
  1266. { xassert(T->reason == 0);
  1267. T->reason = GLP_IHEUR;
  1268. round_heur(T);
  1269. T->reason = 0;
  1270. /* check if the current branch became hopeless */
  1271. if (!is_branch_hopeful(T, p))
  1272. { if (T->parm->msg_lev >= GLP_MSG_DBG)
  1273. xprintf("Current branch became hopeless and can be prune"
  1274. "d\n");
  1275. goto fath;
  1276. }
  1277. }
  1278. #endif
  1279. /* it's time to generate cutting planes */
  1280. xassert(T->local != NULL);
  1281. xassert(T->local->size == 0);
  1282. /* let the application program generate some cuts; note that it
  1283. can add cuts either to the local cut pool or directly to the
  1284. current subproblem */
  1285. if (T->parm->cb_func != NULL)
  1286. { xassert(T->reason == 0);
  1287. T->reason = GLP_ICUTGEN;
  1288. T->parm->cb_func(T, T->parm->cb_info);
  1289. T->reason = 0;
  1290. if (T->stop)
  1291. { ret = GLP_ESTOP;
  1292. goto done;
  1293. }
  1294. }
  1295. #if 1 /* 18/VII-2013 */
  1296. if (T->curr->changed > 0)
  1297. { double degrad = fabs(T->curr->lp_obj - old_obj);
  1298. if (degrad < 1e-4 * (1.0 + fabs(old_obj)))
  1299. bad_cut++;
  1300. else
  1301. bad_cut = 0;
  1302. }
  1303. old_obj = T->curr->lp_obj;
  1304. if (bad_cut == 0 || (T->curr->level == 0 && bad_cut <= 3))
  1305. #endif
  1306. /* try to generate generic cuts with built-in generators
  1307. (as suggested by Prof. Fischetti et al. the built-in cuts are
  1308. not generated at each branching node; an intense attempt of
  1309. generating new cuts is only made at the root node, and then
  1310. a moderate effort is spent after each backtracking step) */
  1311. if (T->curr->level == 0 || pred_p == 0)
  1312. { xassert(T->reason == 0);
  1313. T->reason = GLP_ICUTGEN;
  1314. generate_cuts(T);
  1315. T->reason = 0;
  1316. }
  1317. /* if the local cut pool is not empty, select useful cuts and add
  1318. them to the current subproblem */
  1319. if (T->local->size > 0)
  1320. { xassert(T->reason == 0);
  1321. T->reason = GLP_ICUTGEN;
  1322. ios_process_cuts(T);
  1323. T->reason = 0;
  1324. }
  1325. /* clear the local cut pool */
  1326. ios_clear_pool(T, T->local);
  1327. /* perform re-optimization, if necessary */
  1328. if (T->reopt)
  1329. { T->reopt = 0;
  1330. T->curr->changed++;
  1331. goto more;
  1332. }
  1333. /* no cuts were generated; remove inactive cuts */
  1334. remove_cuts(T);
  1335. if (T->parm->msg_lev >= GLP_MSG_ALL && T->curr->level == 0)
  1336. display_cut_info(T);
  1337. /* update history information used on pseudocost branching */
  1338. if (T->pcost != NULL) ios_pcost_update(T);
  1339. /* it's time to perform branching */
  1340. xassert(T->br_var == 0);
  1341. xassert(T->br_sel == 0);
  1342. /* let the application program choose variable to branch on */
  1343. if (T->parm->cb_func != NULL)
  1344. { xassert(T->reason == 0);
  1345. xassert(T->br_var == 0);
  1346. xassert(T->br_sel == 0);
  1347. T->reason = GLP_IBRANCH;
  1348. T->parm->cb_func(T, T->parm->cb_info);
  1349. T->reason = 0;
  1350. if (T->stop)
  1351. { ret = GLP_ESTOP;
  1352. goto done;
  1353. }
  1354. }
  1355. /* if nothing has been chosen, choose some variable as specified
  1356. by the branching technique option */
  1357. if (T->br_var == 0)
  1358. T->br_var = ios_choose_var(T, &T->br_sel);
  1359. /* perform actual branching */
  1360. curr_p = T->curr->p;
  1361. ret = branch_on(T, T->br_var, T->br_sel);
  1362. T->br_var = T->br_sel = 0;
  1363. if (ret == 0)
  1364. { /* both branches have been created */
  1365. pred_p = curr_p;
  1366. goto loop;
  1367. }
  1368. else if (ret == 1)
  1369. { /* one branch is hopeless and has been pruned, so now the
  1370. current subproblem is other branch */
  1371. /* the current subproblem should be considered as a new one,
  1372. since one bound of the branching variable was changed */
  1373. T->curr->solved = T->curr->changed = 0;
  1374. #if 1 /* 18/VII-2013 */
  1375. /* bad_cut = 0; */
  1376. #endif
  1377. goto more;
  1378. }
  1379. else if (ret == 2)
  1380. { /* both branches are hopeless and have been pruned; new
  1381. subproblem selection is needed to continue the search */
  1382. goto fath;
  1383. }
  1384. else
  1385. xassert(ret != ret);
  1386. fath: /* the current subproblem has been fathomed */
  1387. if (T->parm->msg_lev >= GLP_MSG_DBG)
  1388. xprintf("Node %d fathomed\n", p);
  1389. /* freeze the current subproblem */
  1390. ios_freeze_node(T);
  1391. /* and prune the corresponding branch of the tree */
  1392. ios_delete_node(T, p);
  1393. /* if a new integer feasible solution has just been found, other
  1394. branches may become hopeless and therefore must be pruned */
  1395. if (T->mip->mip_stat == GLP_FEAS) cleanup_the_tree(T);
  1396. /* new subproblem selection is needed due to backtracking */
  1397. pred_p = 0;
  1398. goto loop;
  1399. done: /* display progress of the search on exit from the solver */
  1400. if (T->parm->msg_lev >= GLP_MSG_ON)
  1401. show_progress(T, 0);
  1402. if (T->mir_gen != NULL)
  1403. ios_mir_term(T->mir_gen), T->mir_gen = NULL;
  1404. if (T->clq_gen != NULL)
  1405. ios_clq_term(T->clq_gen), T->clq_gen = NULL;
  1406. /* return to the calling program */
  1407. return ret;
  1408. }
  1409. /* eof */