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.

1602 lines
52 KiB

  1. /* glpios01.c */
  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. #include "misc.h"
  26. static int lpx_eval_tab_row(glp_prob *lp, int k, int ind[],
  27. double val[])
  28. { /* compute row of the simplex tableau */
  29. return glp_eval_tab_row(lp, k, ind, val);
  30. }
  31. static int lpx_dual_ratio_test(glp_prob *lp, int len, const int ind[],
  32. const double val[], int how, double tol)
  33. { /* perform dual ratio test */
  34. int piv;
  35. piv = glp_dual_rtest(lp, len, ind, val, how, tol);
  36. xassert(0 <= piv && piv <= len);
  37. return piv == 0 ? 0 : ind[piv];
  38. }
  39. /***********************************************************************
  40. * NAME
  41. *
  42. * ios_create_tree - create branch-and-bound tree
  43. *
  44. * SYNOPSIS
  45. *
  46. * #include "glpios.h"
  47. * glp_tree *ios_create_tree(glp_prob *mip, const glp_iocp *parm);
  48. *
  49. * DESCRIPTION
  50. *
  51. * The routine ios_create_tree creates the branch-and-bound tree.
  52. *
  53. * Being created the tree consists of the only root subproblem whose
  54. * reference number is 1. Note that initially the root subproblem is in
  55. * frozen state and therefore needs to be revived.
  56. *
  57. * RETURNS
  58. *
  59. * The routine returns a pointer to the tree created. */
  60. static IOSNPD *new_node(glp_tree *tree, IOSNPD *parent);
  61. glp_tree *ios_create_tree(glp_prob *mip, const glp_iocp *parm)
  62. { int m = mip->m;
  63. int n = mip->n;
  64. glp_tree *tree;
  65. int i, j;
  66. xassert(mip->tree == NULL);
  67. mip->tree = tree = xmalloc(sizeof(glp_tree));
  68. tree->pool = dmp_create_pool();
  69. tree->n = n;
  70. /* save original problem components */
  71. tree->orig_m = m;
  72. tree->orig_type = xcalloc(1+m+n, sizeof(char));
  73. tree->orig_lb = xcalloc(1+m+n, sizeof(double));
  74. tree->orig_ub = xcalloc(1+m+n, sizeof(double));
  75. tree->orig_stat = xcalloc(1+m+n, sizeof(char));
  76. tree->orig_prim = xcalloc(1+m+n, sizeof(double));
  77. tree->orig_dual = xcalloc(1+m+n, sizeof(double));
  78. for (i = 1; i <= m; i++)
  79. { GLPROW *row = mip->row[i];
  80. tree->orig_type[i] = (char)row->type;
  81. tree->orig_lb[i] = row->lb;
  82. tree->orig_ub[i] = row->ub;
  83. tree->orig_stat[i] = (char)row->stat;
  84. tree->orig_prim[i] = row->prim;
  85. tree->orig_dual[i] = row->dual;
  86. }
  87. for (j = 1; j <= n; j++)
  88. { GLPCOL *col = mip->col[j];
  89. tree->orig_type[m+j] = (char)col->type;
  90. tree->orig_lb[m+j] = col->lb;
  91. tree->orig_ub[m+j] = col->ub;
  92. tree->orig_stat[m+j] = (char)col->stat;
  93. tree->orig_prim[m+j] = col->prim;
  94. tree->orig_dual[m+j] = col->dual;
  95. }
  96. tree->orig_obj = mip->obj_val;
  97. /* initialize the branch-and-bound tree */
  98. tree->nslots = 0;
  99. tree->avail = 0;
  100. tree->slot = NULL;
  101. tree->head = tree->tail = NULL;
  102. tree->a_cnt = tree->n_cnt = tree->t_cnt = 0;
  103. /* the root subproblem is not solved yet, so its final components
  104. are unknown so far */
  105. tree->root_m = 0;
  106. tree->root_type = NULL;
  107. tree->root_lb = tree->root_ub = NULL;
  108. tree->root_stat = NULL;
  109. /* the current subproblem does not exist yet */
  110. tree->curr = NULL;
  111. tree->mip = mip;
  112. /*tree->solved = 0;*/
  113. tree->non_int = xcalloc(1+n, sizeof(char));
  114. memset(&tree->non_int[1], 0, n);
  115. /* arrays to save parent subproblem components will be allocated
  116. later */
  117. tree->pred_m = tree->pred_max = 0;
  118. tree->pred_type = NULL;
  119. tree->pred_lb = tree->pred_ub = NULL;
  120. tree->pred_stat = NULL;
  121. /* cut generator */
  122. tree->local = ios_create_pool(tree);
  123. /*tree->first_attempt = 1;*/
  124. /*tree->max_added_cuts = 0;*/
  125. /*tree->min_eff = 0.0;*/
  126. /*tree->miss = 0;*/
  127. /*tree->just_selected = 0;*/
  128. tree->mir_gen = NULL;
  129. tree->clq_gen = NULL;
  130. /*tree->round = 0;*/
  131. #if 0
  132. /* create the conflict graph */
  133. tree->n_ref = xcalloc(1+n, sizeof(int));
  134. memset(&tree->n_ref[1], 0, n * sizeof(int));
  135. tree->c_ref = xcalloc(1+n, sizeof(int));
  136. memset(&tree->c_ref[1], 0, n * sizeof(int));
  137. tree->g = scg_create_graph(0);
  138. tree->j_ref = xcalloc(1+tree->g->n_max, sizeof(int));
  139. #endif
  140. /* pseudocost branching */
  141. tree->pcost = NULL;
  142. tree->iwrk = xcalloc(1+n, sizeof(int));
  143. tree->dwrk = xcalloc(1+n, sizeof(double));
  144. /* initialize control parameters */
  145. tree->parm = parm;
  146. tree->tm_beg = xtime();
  147. #if 0 /* 10/VI-2013 */
  148. tree->tm_lag = xlset(0);
  149. #else
  150. tree->tm_lag = 0.0;
  151. #endif
  152. tree->sol_cnt = 0;
  153. #if 1 /* 11/VII-2013 */
  154. tree->P = NULL;
  155. tree->npp = NULL;
  156. tree->save_sol = parm->save_sol;
  157. tree->save_cnt = 0;
  158. #endif
  159. /* initialize advanced solver interface */
  160. tree->reason = 0;
  161. tree->reopt = 0;
  162. tree->reinv = 0;
  163. tree->br_var = 0;
  164. tree->br_sel = 0;
  165. tree->child = 0;
  166. tree->next_p = 0;
  167. /*tree->btrack = NULL;*/
  168. tree->stop = 0;
  169. /* create the root subproblem, which initially is identical to
  170. the original MIP */
  171. new_node(tree, NULL);
  172. return tree;
  173. }
  174. /***********************************************************************
  175. * NAME
  176. *
  177. * ios_revive_node - revive specified subproblem
  178. *
  179. * SYNOPSIS
  180. *
  181. * #include "glpios.h"
  182. * void ios_revive_node(glp_tree *tree, int p);
  183. *
  184. * DESCRIPTION
  185. *
  186. * The routine ios_revive_node revives the specified subproblem, whose
  187. * reference number is p, and thereby makes it the current subproblem.
  188. * Note that the specified subproblem must be active. Besides, if the
  189. * current subproblem already exists, it must be frozen before reviving
  190. * another subproblem. */
  191. void ios_revive_node(glp_tree *tree, int p)
  192. { glp_prob *mip = tree->mip;
  193. IOSNPD *node, *root;
  194. /* obtain pointer to the specified subproblem */
  195. xassert(1 <= p && p <= tree->nslots);
  196. node = tree->slot[p].node;
  197. xassert(node != NULL);
  198. /* the specified subproblem must be active */
  199. xassert(node->count == 0);
  200. /* the current subproblem must not exist */
  201. xassert(tree->curr == NULL);
  202. /* the specified subproblem becomes current */
  203. tree->curr = node;
  204. /*tree->solved = 0;*/
  205. /* obtain pointer to the root subproblem */
  206. root = tree->slot[1].node;
  207. xassert(root != NULL);
  208. /* at this point problem object components correspond to the root
  209. subproblem, so if the root subproblem should be revived, there
  210. is nothing more to do */
  211. if (node == root) goto done;
  212. xassert(mip->m == tree->root_m);
  213. /* build path from the root to the current node */
  214. node->temp = NULL;
  215. for (node = node; node != NULL; node = node->up)
  216. { if (node->up == NULL)
  217. xassert(node == root);
  218. else
  219. node->up->temp = node;
  220. }
  221. /* go down from the root to the current node and make necessary
  222. changes to restore components of the current subproblem */
  223. for (node = root; node != NULL; node = node->temp)
  224. { int m = mip->m;
  225. int n = mip->n;
  226. /* if the current node is reached, the problem object at this
  227. point corresponds to its parent, so save attributes of rows
  228. and columns for the parent subproblem */
  229. if (node->temp == NULL)
  230. { int i, j;
  231. tree->pred_m = m;
  232. /* allocate/reallocate arrays, if necessary */
  233. if (tree->pred_max < m + n)
  234. { int new_size = m + n + 100;
  235. if (tree->pred_type != NULL) xfree(tree->pred_type);
  236. if (tree->pred_lb != NULL) xfree(tree->pred_lb);
  237. if (tree->pred_ub != NULL) xfree(tree->pred_ub);
  238. if (tree->pred_stat != NULL) xfree(tree->pred_stat);
  239. tree->pred_max = new_size;
  240. tree->pred_type = xcalloc(1+new_size, sizeof(char));
  241. tree->pred_lb = xcalloc(1+new_size, sizeof(double));
  242. tree->pred_ub = xcalloc(1+new_size, sizeof(double));
  243. tree->pred_stat = xcalloc(1+new_size, sizeof(char));
  244. }
  245. /* save row attributes */
  246. for (i = 1; i <= m; i++)
  247. { GLPROW *row = mip->row[i];
  248. tree->pred_type[i] = (char)row->type;
  249. tree->pred_lb[i] = row->lb;
  250. tree->pred_ub[i] = row->ub;
  251. tree->pred_stat[i] = (char)row->stat;
  252. }
  253. /* save column attributes */
  254. for (j = 1; j <= n; j++)
  255. { GLPCOL *col = mip->col[j];
  256. tree->pred_type[mip->m+j] = (char)col->type;
  257. tree->pred_lb[mip->m+j] = col->lb;
  258. tree->pred_ub[mip->m+j] = col->ub;
  259. tree->pred_stat[mip->m+j] = (char)col->stat;
  260. }
  261. }
  262. /* change bounds of rows and columns */
  263. { IOSBND *b;
  264. for (b = node->b_ptr; b != NULL; b = b->next)
  265. { if (b->k <= m)
  266. glp_set_row_bnds(mip, b->k, b->type, b->lb, b->ub);
  267. else
  268. glp_set_col_bnds(mip, b->k-m, b->type, b->lb, b->ub);
  269. }
  270. }
  271. /* change statuses of rows and columns */
  272. { IOSTAT *s;
  273. for (s = node->s_ptr; s != NULL; s = s->next)
  274. { if (s->k <= m)
  275. glp_set_row_stat(mip, s->k, s->stat);
  276. else
  277. glp_set_col_stat(mip, s->k-m, s->stat);
  278. }
  279. }
  280. /* add new rows */
  281. if (node->r_ptr != NULL)
  282. { IOSROW *r;
  283. IOSAIJ *a;
  284. int i, len, *ind;
  285. double *val;
  286. ind = xcalloc(1+n, sizeof(int));
  287. val = xcalloc(1+n, sizeof(double));
  288. for (r = node->r_ptr; r != NULL; r = r->next)
  289. { i = glp_add_rows(mip, 1);
  290. glp_set_row_name(mip, i, r->name);
  291. #if 1 /* 20/IX-2008 */
  292. xassert(mip->row[i]->level == 0);
  293. mip->row[i]->level = node->level;
  294. mip->row[i]->origin = r->origin;
  295. mip->row[i]->klass = r->klass;
  296. #endif
  297. glp_set_row_bnds(mip, i, r->type, r->lb, r->ub);
  298. len = 0;
  299. for (a = r->ptr; a != NULL; a = a->next)
  300. len++, ind[len] = a->j, val[len] = a->val;
  301. glp_set_mat_row(mip, i, len, ind, val);
  302. glp_set_rii(mip, i, r->rii);
  303. glp_set_row_stat(mip, i, r->stat);
  304. }
  305. xfree(ind);
  306. xfree(val);
  307. }
  308. #if 0
  309. /* add new edges to the conflict graph */
  310. /* add new cliques to the conflict graph */
  311. /* (not implemented yet) */
  312. xassert(node->own_nn == 0);
  313. xassert(node->own_nc == 0);
  314. xassert(node->e_ptr == NULL);
  315. #endif
  316. }
  317. /* the specified subproblem has been revived */
  318. node = tree->curr;
  319. /* delete its bound change list */
  320. while (node->b_ptr != NULL)
  321. { IOSBND *b;
  322. b = node->b_ptr;
  323. node->b_ptr = b->next;
  324. dmp_free_atom(tree->pool, b, sizeof(IOSBND));
  325. }
  326. /* delete its status change list */
  327. while (node->s_ptr != NULL)
  328. { IOSTAT *s;
  329. s = node->s_ptr;
  330. node->s_ptr = s->next;
  331. dmp_free_atom(tree->pool, s, sizeof(IOSTAT));
  332. }
  333. #if 1 /* 20/XI-2009 */
  334. /* delete its row addition list (additional rows may appear, for
  335. example, due to branching on GUB constraints */
  336. while (node->r_ptr != NULL)
  337. { IOSROW *r;
  338. r = node->r_ptr;
  339. node->r_ptr = r->next;
  340. xassert(r->name == NULL);
  341. while (r->ptr != NULL)
  342. { IOSAIJ *a;
  343. a = r->ptr;
  344. r->ptr = a->next;
  345. dmp_free_atom(tree->pool, a, sizeof(IOSAIJ));
  346. }
  347. dmp_free_atom(tree->pool, r, sizeof(IOSROW));
  348. }
  349. #endif
  350. done: return;
  351. }
  352. /***********************************************************************
  353. * NAME
  354. *
  355. * ios_freeze_node - freeze current subproblem
  356. *
  357. * SYNOPSIS
  358. *
  359. * #include "glpios.h"
  360. * void ios_freeze_node(glp_tree *tree);
  361. *
  362. * DESCRIPTION
  363. *
  364. * The routine ios_freeze_node freezes the current subproblem. */
  365. void ios_freeze_node(glp_tree *tree)
  366. { glp_prob *mip = tree->mip;
  367. int m = mip->m;
  368. int n = mip->n;
  369. IOSNPD *node;
  370. /* obtain pointer to the current subproblem */
  371. node = tree->curr;
  372. xassert(node != NULL);
  373. if (node->up == NULL)
  374. { /* freeze the root subproblem */
  375. int k;
  376. xassert(node->p == 1);
  377. xassert(tree->root_m == 0);
  378. xassert(tree->root_type == NULL);
  379. xassert(tree->root_lb == NULL);
  380. xassert(tree->root_ub == NULL);
  381. xassert(tree->root_stat == NULL);
  382. tree->root_m = m;
  383. tree->root_type = xcalloc(1+m+n, sizeof(char));
  384. tree->root_lb = xcalloc(1+m+n, sizeof(double));
  385. tree->root_ub = xcalloc(1+m+n, sizeof(double));
  386. tree->root_stat = xcalloc(1+m+n, sizeof(char));
  387. for (k = 1; k <= m+n; k++)
  388. { if (k <= m)
  389. { GLPROW *row = mip->row[k];
  390. tree->root_type[k] = (char)row->type;
  391. tree->root_lb[k] = row->lb;
  392. tree->root_ub[k] = row->ub;
  393. tree->root_stat[k] = (char)row->stat;
  394. }
  395. else
  396. { GLPCOL *col = mip->col[k-m];
  397. tree->root_type[k] = (char)col->type;
  398. tree->root_lb[k] = col->lb;
  399. tree->root_ub[k] = col->ub;
  400. tree->root_stat[k] = (char)col->stat;
  401. }
  402. }
  403. }
  404. else
  405. { /* freeze non-root subproblem */
  406. int root_m = tree->root_m;
  407. int pred_m = tree->pred_m;
  408. int i, j, k;
  409. xassert(pred_m <= m);
  410. /* build change lists for rows and columns which exist in the
  411. parent subproblem */
  412. xassert(node->b_ptr == NULL);
  413. xassert(node->s_ptr == NULL);
  414. for (k = 1; k <= pred_m + n; k++)
  415. { int pred_type, pred_stat, type, stat;
  416. double pred_lb, pred_ub, lb, ub;
  417. /* determine attributes in the parent subproblem */
  418. pred_type = tree->pred_type[k];
  419. pred_lb = tree->pred_lb[k];
  420. pred_ub = tree->pred_ub[k];
  421. pred_stat = tree->pred_stat[k];
  422. /* determine attributes in the current subproblem */
  423. if (k <= pred_m)
  424. { GLPROW *row = mip->row[k];
  425. type = row->type;
  426. lb = row->lb;
  427. ub = row->ub;
  428. stat = row->stat;
  429. }
  430. else
  431. { GLPCOL *col = mip->col[k - pred_m];
  432. type = col->type;
  433. lb = col->lb;
  434. ub = col->ub;
  435. stat = col->stat;
  436. }
  437. /* save type and bounds of a row/column, if changed */
  438. if (!(pred_type == type && pred_lb == lb && pred_ub == ub))
  439. { IOSBND *b;
  440. b = dmp_get_atom(tree->pool, sizeof(IOSBND));
  441. b->k = k;
  442. b->type = (unsigned char)type;
  443. b->lb = lb;
  444. b->ub = ub;
  445. b->next = node->b_ptr;
  446. node->b_ptr = b;
  447. }
  448. /* save status of a row/column, if changed */
  449. if (pred_stat != stat)
  450. { IOSTAT *s;
  451. s = dmp_get_atom(tree->pool, sizeof(IOSTAT));
  452. s->k = k;
  453. s->stat = (unsigned char)stat;
  454. s->next = node->s_ptr;
  455. node->s_ptr = s;
  456. }
  457. }
  458. /* save new rows added to the current subproblem */
  459. xassert(node->r_ptr == NULL);
  460. if (pred_m < m)
  461. { int i, len, *ind;
  462. double *val;
  463. ind = xcalloc(1+n, sizeof(int));
  464. val = xcalloc(1+n, sizeof(double));
  465. for (i = m; i > pred_m; i--)
  466. { GLPROW *row = mip->row[i];
  467. IOSROW *r;
  468. const char *name;
  469. r = dmp_get_atom(tree->pool, sizeof(IOSROW));
  470. name = glp_get_row_name(mip, i);
  471. if (name == NULL)
  472. r->name = NULL;
  473. else
  474. { r->name = dmp_get_atom(tree->pool, strlen(name)+1);
  475. strcpy(r->name, name);
  476. }
  477. #if 1 /* 20/IX-2008 */
  478. r->origin = row->origin;
  479. r->klass = row->klass;
  480. #endif
  481. r->type = (unsigned char)row->type;
  482. r->lb = row->lb;
  483. r->ub = row->ub;
  484. r->ptr = NULL;
  485. len = glp_get_mat_row(mip, i, ind, val);
  486. for (k = 1; k <= len; k++)
  487. { IOSAIJ *a;
  488. a = dmp_get_atom(tree->pool, sizeof(IOSAIJ));
  489. a->j = ind[k];
  490. a->val = val[k];
  491. a->next = r->ptr;
  492. r->ptr = a;
  493. }
  494. r->rii = row->rii;
  495. r->stat = (unsigned char)row->stat;
  496. r->next = node->r_ptr;
  497. node->r_ptr = r;
  498. }
  499. xfree(ind);
  500. xfree(val);
  501. }
  502. /* remove all rows missing in the root subproblem */
  503. if (m != root_m)
  504. { int nrs, *num;
  505. nrs = m - root_m;
  506. xassert(nrs > 0);
  507. num = xcalloc(1+nrs, sizeof(int));
  508. for (i = 1; i <= nrs; i++) num[i] = root_m + i;
  509. glp_del_rows(mip, nrs, num);
  510. xfree(num);
  511. }
  512. m = mip->m;
  513. /* and restore attributes of all rows and columns for the root
  514. subproblem */
  515. xassert(m == root_m);
  516. for (i = 1; i <= m; i++)
  517. { glp_set_row_bnds(mip, i, tree->root_type[i],
  518. tree->root_lb[i], tree->root_ub[i]);
  519. glp_set_row_stat(mip, i, tree->root_stat[i]);
  520. }
  521. for (j = 1; j <= n; j++)
  522. { glp_set_col_bnds(mip, j, tree->root_type[m+j],
  523. tree->root_lb[m+j], tree->root_ub[m+j]);
  524. glp_set_col_stat(mip, j, tree->root_stat[m+j]);
  525. }
  526. #if 1
  527. /* remove all edges and cliques missing in the conflict graph
  528. for the root subproblem */
  529. /* (not implemented yet) */
  530. #endif
  531. }
  532. /* the current subproblem has been frozen */
  533. tree->curr = NULL;
  534. return;
  535. }
  536. /***********************************************************************
  537. * NAME
  538. *
  539. * ios_clone_node - clone specified subproblem
  540. *
  541. * SYNOPSIS
  542. *
  543. * #include "glpios.h"
  544. * void ios_clone_node(glp_tree *tree, int p, int nnn, int ref[]);
  545. *
  546. * DESCRIPTION
  547. *
  548. * The routine ios_clone_node clones the specified subproblem, whose
  549. * reference number is p, creating its nnn exact copies. Note that the
  550. * specified subproblem must be active and must be in the frozen state
  551. * (i.e. it must not be the current subproblem).
  552. *
  553. * Each clone, an exact copy of the specified subproblem, becomes a new
  554. * active subproblem added to the end of the active list. After cloning
  555. * the specified subproblem becomes inactive.
  556. *
  557. * The reference numbers of clone subproblems are stored to locations
  558. * ref[1], ..., ref[nnn]. */
  559. static int get_slot(glp_tree *tree)
  560. { int p;
  561. /* if no free slots are available, increase the room */
  562. if (tree->avail == 0)
  563. { int nslots = tree->nslots;
  564. IOSLOT *save = tree->slot;
  565. if (nslots == 0)
  566. tree->nslots = 20;
  567. else
  568. { tree->nslots = nslots + nslots;
  569. xassert(tree->nslots > nslots);
  570. }
  571. tree->slot = xcalloc(1+tree->nslots, sizeof(IOSLOT));
  572. if (save != NULL)
  573. { memcpy(&tree->slot[1], &save[1], nslots * sizeof(IOSLOT));
  574. xfree(save);
  575. }
  576. /* push more free slots into the stack */
  577. for (p = tree->nslots; p > nslots; p--)
  578. { tree->slot[p].node = NULL;
  579. tree->slot[p].next = tree->avail;
  580. tree->avail = p;
  581. }
  582. }
  583. /* pull a free slot from the stack */
  584. p = tree->avail;
  585. tree->avail = tree->slot[p].next;
  586. xassert(tree->slot[p].node == NULL);
  587. tree->slot[p].next = 0;
  588. return p;
  589. }
  590. static IOSNPD *new_node(glp_tree *tree, IOSNPD *parent)
  591. { IOSNPD *node;
  592. int p;
  593. /* pull a free slot for the new node */
  594. p = get_slot(tree);
  595. /* create descriptor of the new subproblem */
  596. node = dmp_get_atom(tree->pool, sizeof(IOSNPD));
  597. tree->slot[p].node = node;
  598. node->p = p;
  599. node->up = parent;
  600. node->level = (parent == NULL ? 0 : parent->level + 1);
  601. node->count = 0;
  602. node->b_ptr = NULL;
  603. node->s_ptr = NULL;
  604. node->r_ptr = NULL;
  605. node->solved = 0;
  606. #if 0
  607. node->own_nn = node->own_nc = 0;
  608. node->e_ptr = NULL;
  609. #endif
  610. #if 1 /* 04/X-2008 */
  611. node->lp_obj = (parent == NULL ? (tree->mip->dir == GLP_MIN ?
  612. -DBL_MAX : +DBL_MAX) : parent->lp_obj);
  613. #endif
  614. node->bound = (parent == NULL ? (tree->mip->dir == GLP_MIN ?
  615. -DBL_MAX : +DBL_MAX) : parent->bound);
  616. node->br_var = 0;
  617. node->br_val = 0.0;
  618. node->ii_cnt = 0;
  619. node->ii_sum = 0.0;
  620. #if 1 /* 30/XI-2009 */
  621. node->changed = 0;
  622. #endif
  623. if (tree->parm->cb_size == 0)
  624. node->data = NULL;
  625. else
  626. { node->data = dmp_get_atom(tree->pool, tree->parm->cb_size);
  627. memset(node->data, 0, tree->parm->cb_size);
  628. }
  629. node->temp = NULL;
  630. node->prev = tree->tail;
  631. node->next = NULL;
  632. /* add the new subproblem to the end of the active list */
  633. if (tree->head == NULL)
  634. tree->head = node;
  635. else
  636. tree->tail->next = node;
  637. tree->tail = node;
  638. tree->a_cnt++;
  639. tree->n_cnt++;
  640. tree->t_cnt++;
  641. /* increase the number of child subproblems */
  642. if (parent == NULL)
  643. xassert(p == 1);
  644. else
  645. parent->count++;
  646. return node;
  647. }
  648. void ios_clone_node(glp_tree *tree, int p, int nnn, int ref[])
  649. { IOSNPD *node;
  650. int k;
  651. /* obtain pointer to the subproblem to be cloned */
  652. xassert(1 <= p && p <= tree->nslots);
  653. node = tree->slot[p].node;
  654. xassert(node != NULL);
  655. /* the specified subproblem must be active */
  656. xassert(node->count == 0);
  657. /* and must be in the frozen state */
  658. xassert(tree->curr != node);
  659. /* remove the specified subproblem from the active list, because
  660. it becomes inactive */
  661. if (node->prev == NULL)
  662. tree->head = node->next;
  663. else
  664. node->prev->next = node->next;
  665. if (node->next == NULL)
  666. tree->tail = node->prev;
  667. else
  668. node->next->prev = node->prev;
  669. node->prev = node->next = NULL;
  670. tree->a_cnt--;
  671. /* create clone subproblems */
  672. xassert(nnn > 0);
  673. for (k = 1; k <= nnn; k++)
  674. ref[k] = new_node(tree, node)->p;
  675. return;
  676. }
  677. /***********************************************************************
  678. * NAME
  679. *
  680. * ios_delete_node - delete specified subproblem
  681. *
  682. * SYNOPSIS
  683. *
  684. * #include "glpios.h"
  685. * void ios_delete_node(glp_tree *tree, int p);
  686. *
  687. * DESCRIPTION
  688. *
  689. * The routine ios_delete_node deletes the specified subproblem, whose
  690. * reference number is p. The subproblem must be active and must be in
  691. * the frozen state (i.e. it must not be the current subproblem).
  692. *
  693. * Note that deletion is performed recursively, i.e. if a subproblem to
  694. * be deleted is the only child of its parent, the parent subproblem is
  695. * also deleted, etc. */
  696. void ios_delete_node(glp_tree *tree, int p)
  697. { IOSNPD *node, *temp;
  698. /* obtain pointer to the subproblem to be deleted */
  699. xassert(1 <= p && p <= tree->nslots);
  700. node = tree->slot[p].node;
  701. xassert(node != NULL);
  702. /* the specified subproblem must be active */
  703. xassert(node->count == 0);
  704. /* and must be in the frozen state */
  705. xassert(tree->curr != node);
  706. /* remove the specified subproblem from the active list, because
  707. it is gone from the tree */
  708. if (node->prev == NULL)
  709. tree->head = node->next;
  710. else
  711. node->prev->next = node->next;
  712. if (node->next == NULL)
  713. tree->tail = node->prev;
  714. else
  715. node->next->prev = node->prev;
  716. node->prev = node->next = NULL;
  717. tree->a_cnt--;
  718. loop: /* recursive deletion starts here */
  719. /* delete the bound change list */
  720. { IOSBND *b;
  721. while (node->b_ptr != NULL)
  722. { b = node->b_ptr;
  723. node->b_ptr = b->next;
  724. dmp_free_atom(tree->pool, b, sizeof(IOSBND));
  725. }
  726. }
  727. /* delete the status change list */
  728. { IOSTAT *s;
  729. while (node->s_ptr != NULL)
  730. { s = node->s_ptr;
  731. node->s_ptr = s->next;
  732. dmp_free_atom(tree->pool, s, sizeof(IOSTAT));
  733. }
  734. }
  735. /* delete the row addition list */
  736. while (node->r_ptr != NULL)
  737. { IOSROW *r;
  738. r = node->r_ptr;
  739. if (r->name != NULL)
  740. dmp_free_atom(tree->pool, r->name, strlen(r->name)+1);
  741. while (r->ptr != NULL)
  742. { IOSAIJ *a;
  743. a = r->ptr;
  744. r->ptr = a->next;
  745. dmp_free_atom(tree->pool, a, sizeof(IOSAIJ));
  746. }
  747. node->r_ptr = r->next;
  748. dmp_free_atom(tree->pool, r, sizeof(IOSROW));
  749. }
  750. #if 0
  751. /* delete the edge addition list */
  752. /* delete the clique addition list */
  753. /* (not implemented yet) */
  754. xassert(node->own_nn == 0);
  755. xassert(node->own_nc == 0);
  756. xassert(node->e_ptr == NULL);
  757. #endif
  758. /* free application-specific data */
  759. if (tree->parm->cb_size == 0)
  760. xassert(node->data == NULL);
  761. else
  762. dmp_free_atom(tree->pool, node->data, tree->parm->cb_size);
  763. /* free the corresponding node slot */
  764. p = node->p;
  765. xassert(tree->slot[p].node == node);
  766. tree->slot[p].node = NULL;
  767. tree->slot[p].next = tree->avail;
  768. tree->avail = p;
  769. /* save pointer to the parent subproblem */
  770. temp = node->up;
  771. /* delete the subproblem descriptor */
  772. dmp_free_atom(tree->pool, node, sizeof(IOSNPD));
  773. tree->n_cnt--;
  774. /* take pointer to the parent subproblem */
  775. node = temp;
  776. if (node != NULL)
  777. { /* the parent subproblem exists; decrease the number of its
  778. child subproblems */
  779. xassert(node->count > 0);
  780. node->count--;
  781. /* if now the parent subproblem has no childs, it also must be
  782. deleted */
  783. if (node->count == 0) goto loop;
  784. }
  785. return;
  786. }
  787. /***********************************************************************
  788. * NAME
  789. *
  790. * ios_delete_tree - delete branch-and-bound tree
  791. *
  792. * SYNOPSIS
  793. *
  794. * #include "glpios.h"
  795. * void ios_delete_tree(glp_tree *tree);
  796. *
  797. * DESCRIPTION
  798. *
  799. * The routine ios_delete_tree deletes the branch-and-bound tree, which
  800. * the parameter tree points to, and frees all the memory allocated to
  801. * this program object.
  802. *
  803. * On exit components of the problem object are restored to correspond
  804. * to the original MIP passed to the routine ios_create_tree. */
  805. void ios_delete_tree(glp_tree *tree)
  806. { glp_prob *mip = tree->mip;
  807. int i, j;
  808. int m = mip->m;
  809. int n = mip->n;
  810. xassert(mip->tree == tree);
  811. /* remove all additional rows */
  812. if (m != tree->orig_m)
  813. { int nrs, *num;
  814. nrs = m - tree->orig_m;
  815. xassert(nrs > 0);
  816. num = xcalloc(1+nrs, sizeof(int));
  817. for (i = 1; i <= nrs; i++) num[i] = tree->orig_m + i;
  818. glp_del_rows(mip, nrs, num);
  819. xfree(num);
  820. }
  821. m = tree->orig_m;
  822. /* restore original attributes of rows and columns */
  823. xassert(m == tree->orig_m);
  824. xassert(n == tree->n);
  825. for (i = 1; i <= m; i++)
  826. { glp_set_row_bnds(mip, i, tree->orig_type[i],
  827. tree->orig_lb[i], tree->orig_ub[i]);
  828. glp_set_row_stat(mip, i, tree->orig_stat[i]);
  829. mip->row[i]->prim = tree->orig_prim[i];
  830. mip->row[i]->dual = tree->orig_dual[i];
  831. }
  832. for (j = 1; j <= n; j++)
  833. { glp_set_col_bnds(mip, j, tree->orig_type[m+j],
  834. tree->orig_lb[m+j], tree->orig_ub[m+j]);
  835. glp_set_col_stat(mip, j, tree->orig_stat[m+j]);
  836. mip->col[j]->prim = tree->orig_prim[m+j];
  837. mip->col[j]->dual = tree->orig_dual[m+j];
  838. }
  839. mip->pbs_stat = mip->dbs_stat = GLP_FEAS;
  840. mip->obj_val = tree->orig_obj;
  841. /* delete the branch-and-bound tree */
  842. xassert(tree->local != NULL);
  843. ios_delete_pool(tree, tree->local);
  844. dmp_delete_pool(tree->pool);
  845. xfree(tree->orig_type);
  846. xfree(tree->orig_lb);
  847. xfree(tree->orig_ub);
  848. xfree(tree->orig_stat);
  849. xfree(tree->orig_prim);
  850. xfree(tree->orig_dual);
  851. xfree(tree->slot);
  852. if (tree->root_type != NULL) xfree(tree->root_type);
  853. if (tree->root_lb != NULL) xfree(tree->root_lb);
  854. if (tree->root_ub != NULL) xfree(tree->root_ub);
  855. if (tree->root_stat != NULL) xfree(tree->root_stat);
  856. xfree(tree->non_int);
  857. #if 0
  858. xfree(tree->n_ref);
  859. xfree(tree->c_ref);
  860. xfree(tree->j_ref);
  861. #endif
  862. if (tree->pcost != NULL) ios_pcost_free(tree);
  863. xfree(tree->iwrk);
  864. xfree(tree->dwrk);
  865. #if 0
  866. scg_delete_graph(tree->g);
  867. #endif
  868. if (tree->pred_type != NULL) xfree(tree->pred_type);
  869. if (tree->pred_lb != NULL) xfree(tree->pred_lb);
  870. if (tree->pred_ub != NULL) xfree(tree->pred_ub);
  871. if (tree->pred_stat != NULL) xfree(tree->pred_stat);
  872. #if 0
  873. xassert(tree->cut_gen == NULL);
  874. #endif
  875. xassert(tree->mir_gen == NULL);
  876. xassert(tree->clq_gen == NULL);
  877. xfree(tree);
  878. mip->tree = NULL;
  879. return;
  880. }
  881. /***********************************************************************
  882. * NAME
  883. *
  884. * ios_eval_degrad - estimate obj. degrad. for down- and up-branches
  885. *
  886. * SYNOPSIS
  887. *
  888. * #include "glpios.h"
  889. * void ios_eval_degrad(glp_tree *tree, int j, double *dn, double *up);
  890. *
  891. * DESCRIPTION
  892. *
  893. * Given optimal basis to LP relaxation of the current subproblem the
  894. * routine ios_eval_degrad performs the dual ratio test to compute the
  895. * objective values in the adjacent basis for down- and up-branches,
  896. * which are stored in locations *dn and *up, assuming that x[j] is a
  897. * variable chosen to branch upon. */
  898. void ios_eval_degrad(glp_tree *tree, int j, double *dn, double *up)
  899. { glp_prob *mip = tree->mip;
  900. int m = mip->m, n = mip->n;
  901. int len, kase, k, t, stat;
  902. double alfa, beta, gamma, delta, dz;
  903. int *ind = tree->iwrk;
  904. double *val = tree->dwrk;
  905. /* current basis must be optimal */
  906. xassert(glp_get_status(mip) == GLP_OPT);
  907. /* basis factorization must exist */
  908. xassert(glp_bf_exists(mip));
  909. /* obtain (fractional) value of x[j] in optimal basic solution
  910. to LP relaxation of the current subproblem */
  911. xassert(1 <= j && j <= n);
  912. beta = mip->col[j]->prim;
  913. /* since the value of x[j] is fractional, it is basic; compute
  914. corresponding row of the simplex table */
  915. len = lpx_eval_tab_row(mip, m+j, ind, val);
  916. /* kase < 0 means down-branch; kase > 0 means up-branch */
  917. for (kase = -1; kase <= +1; kase += 2)
  918. { /* for down-branch we introduce new upper bound floor(beta)
  919. for x[j]; similarly, for up-branch we introduce new lower
  920. bound ceil(beta) for x[j]; in the current basis this new
  921. upper/lower bound is violated, so in the adjacent basis
  922. x[j] will leave the basis and go to its new upper/lower
  923. bound; we need to know which non-basic variable x[k] should
  924. enter the basis to keep dual feasibility */
  925. #if 0 /* 23/XI-2009 */
  926. k = lpx_dual_ratio_test(mip, len, ind, val, kase, 1e-7);
  927. #else
  928. k = lpx_dual_ratio_test(mip, len, ind, val, kase, 1e-9);
  929. #endif
  930. /* if no variable has been chosen, current basis being primal
  931. infeasible due to the new upper/lower bound of x[j] is dual
  932. unbounded, therefore, LP relaxation to corresponding branch
  933. has no primal feasible solution */
  934. if (k == 0)
  935. { if (mip->dir == GLP_MIN)
  936. { if (kase < 0)
  937. *dn = +DBL_MAX;
  938. else
  939. *up = +DBL_MAX;
  940. }
  941. else if (mip->dir == GLP_MAX)
  942. { if (kase < 0)
  943. *dn = -DBL_MAX;
  944. else
  945. *up = -DBL_MAX;
  946. }
  947. else
  948. xassert(mip != mip);
  949. continue;
  950. }
  951. xassert(1 <= k && k <= m+n);
  952. /* row of the simplex table corresponding to specified basic
  953. variable x[j] is the following:
  954. x[j] = ... + alfa * x[k] + ... ;
  955. we need to know influence coefficient, alfa, at non-basic
  956. variable x[k] chosen with the dual ratio test */
  957. for (t = 1; t <= len; t++)
  958. if (ind[t] == k) break;
  959. xassert(1 <= t && t <= len);
  960. alfa = val[t];
  961. /* determine status and reduced cost of variable x[k] */
  962. if (k <= m)
  963. { stat = mip->row[k]->stat;
  964. gamma = mip->row[k]->dual;
  965. }
  966. else
  967. { stat = mip->col[k-m]->stat;
  968. gamma = mip->col[k-m]->dual;
  969. }
  970. /* x[k] cannot be basic or fixed non-basic */
  971. xassert(stat == GLP_NL || stat == GLP_NU || stat == GLP_NF);
  972. /* if the current basis is dual degenerative, some reduced
  973. costs, which are close to zero, may have wrong sign due to
  974. round-off errors, so correct the sign of gamma */
  975. if (mip->dir == GLP_MIN)
  976. { if (stat == GLP_NL && gamma < 0.0 ||
  977. stat == GLP_NU && gamma > 0.0 ||
  978. stat == GLP_NF) gamma = 0.0;
  979. }
  980. else if (mip->dir == GLP_MAX)
  981. { if (stat == GLP_NL && gamma > 0.0 ||
  982. stat == GLP_NU && gamma < 0.0 ||
  983. stat == GLP_NF) gamma = 0.0;
  984. }
  985. else
  986. xassert(mip != mip);
  987. /* determine the change of x[j] in the adjacent basis:
  988. delta x[j] = new x[j] - old x[j] */
  989. delta = (kase < 0 ? floor(beta) : ceil(beta)) - beta;
  990. /* compute the change of x[k] in the adjacent basis:
  991. delta x[k] = new x[k] - old x[k] = delta x[j] / alfa */
  992. delta /= alfa;
  993. /* compute the change of the objective in the adjacent basis:
  994. delta z = new z - old z = gamma * delta x[k] */
  995. dz = gamma * delta;
  996. if (mip->dir == GLP_MIN)
  997. xassert(dz >= 0.0);
  998. else if (mip->dir == GLP_MAX)
  999. xassert(dz <= 0.0);
  1000. else
  1001. xassert(mip != mip);
  1002. /* compute the new objective value in the adjacent basis:
  1003. new z = old z + delta z */
  1004. if (kase < 0)
  1005. *dn = mip->obj_val + dz;
  1006. else
  1007. *up = mip->obj_val + dz;
  1008. }
  1009. /*xprintf("obj = %g; dn = %g; up = %g\n",
  1010. mip->obj_val, *dn, *up);*/
  1011. return;
  1012. }
  1013. /***********************************************************************
  1014. * NAME
  1015. *
  1016. * ios_round_bound - improve local bound by rounding
  1017. *
  1018. * SYNOPSIS
  1019. *
  1020. * #include "glpios.h"
  1021. * double ios_round_bound(glp_tree *tree, double bound);
  1022. *
  1023. * RETURNS
  1024. *
  1025. * For the given local bound for any integer feasible solution to the
  1026. * current subproblem the routine ios_round_bound returns an improved
  1027. * local bound for the same integer feasible solution.
  1028. *
  1029. * BACKGROUND
  1030. *
  1031. * Let the current subproblem has the following objective function:
  1032. *
  1033. * z = sum c[j] * x[j] + s >= b, (1)
  1034. * j in J
  1035. *
  1036. * where J = {j: c[j] is non-zero and integer, x[j] is integer}, s is
  1037. * the sum of terms corresponding to fixed variables, b is an initial
  1038. * local bound (minimization).
  1039. *
  1040. * From (1) it follows that:
  1041. *
  1042. * d * sum (c[j] / d) * x[j] + s >= b, (2)
  1043. * j in J
  1044. *
  1045. * or, equivalently,
  1046. *
  1047. * sum (c[j] / d) * x[j] >= (b - s) / d = h, (3)
  1048. * j in J
  1049. *
  1050. * where d = gcd(c[j]). Since the left-hand side of (3) is integer,
  1051. * h = (b - s) / d can be rounded up to the nearest integer:
  1052. *
  1053. * h' = ceil(h) = (b' - s) / d, (4)
  1054. *
  1055. * that gives an rounded, improved local bound:
  1056. *
  1057. * b' = d * h' + s. (5)
  1058. *
  1059. * In case of maximization '>=' in (1) should be replaced by '<=' that
  1060. * leads to the following formula:
  1061. *
  1062. * h' = floor(h) = (b' - s) / d, (6)
  1063. *
  1064. * which should used in the same way as (4).
  1065. *
  1066. * NOTE: If b is a valid local bound for a child of the current
  1067. * subproblem, b' is also valid for that child subproblem. */
  1068. double ios_round_bound(glp_tree *tree, double bound)
  1069. { glp_prob *mip = tree->mip;
  1070. int n = mip->n;
  1071. int d, j, nn, *c = tree->iwrk;
  1072. double s, h;
  1073. /* determine c[j] and compute s */
  1074. nn = 0, s = mip->c0, d = 0;
  1075. for (j = 1; j <= n; j++)
  1076. { GLPCOL *col = mip->col[j];
  1077. if (col->coef == 0.0) continue;
  1078. if (col->type == GLP_FX)
  1079. { /* fixed variable */
  1080. s += col->coef * col->prim;
  1081. }
  1082. else
  1083. { /* non-fixed variable */
  1084. if (col->kind != GLP_IV) goto skip;
  1085. if (col->coef != floor(col->coef)) goto skip;
  1086. if (fabs(col->coef) <= (double)INT_MAX)
  1087. c[++nn] = (int)fabs(col->coef);
  1088. else
  1089. d = 1;
  1090. }
  1091. }
  1092. /* compute d = gcd(c[1],...c[nn]) */
  1093. if (d == 0)
  1094. { if (nn == 0) goto skip;
  1095. d = gcdn(nn, c);
  1096. }
  1097. xassert(d > 0);
  1098. /* compute new local bound */
  1099. if (mip->dir == GLP_MIN)
  1100. { if (bound != +DBL_MAX)
  1101. { h = (bound - s) / (double)d;
  1102. if (h >= floor(h) + 0.001)
  1103. { /* round up */
  1104. h = ceil(h);
  1105. /*xprintf("d = %d; old = %g; ", d, bound);*/
  1106. bound = (double)d * h + s;
  1107. /*xprintf("new = %g\n", bound);*/
  1108. }
  1109. }
  1110. }
  1111. else if (mip->dir == GLP_MAX)
  1112. { if (bound != -DBL_MAX)
  1113. { h = (bound - s) / (double)d;
  1114. if (h <= ceil(h) - 0.001)
  1115. { /* round down */
  1116. h = floor(h);
  1117. bound = (double)d * h + s;
  1118. }
  1119. }
  1120. }
  1121. else
  1122. xassert(mip != mip);
  1123. skip: return bound;
  1124. }
  1125. /***********************************************************************
  1126. * NAME
  1127. *
  1128. * ios_is_hopeful - check if subproblem is hopeful
  1129. *
  1130. * SYNOPSIS
  1131. *
  1132. * #include "glpios.h"
  1133. * int ios_is_hopeful(glp_tree *tree, double bound);
  1134. *
  1135. * DESCRIPTION
  1136. *
  1137. * Given the local bound of a subproblem the routine ios_is_hopeful
  1138. * checks if the subproblem can have an integer optimal solution which
  1139. * is better than the best one currently known.
  1140. *
  1141. * RETURNS
  1142. *
  1143. * If the subproblem can have a better integer optimal solution, the
  1144. * routine returns non-zero; otherwise, if the corresponding branch can
  1145. * be pruned, the routine returns zero. */
  1146. int ios_is_hopeful(glp_tree *tree, double bound)
  1147. { glp_prob *mip = tree->mip;
  1148. int ret = 1;
  1149. double eps;
  1150. if (mip->mip_stat == GLP_FEAS)
  1151. { eps = tree->parm->tol_obj * (1.0 + fabs(mip->mip_obj));
  1152. switch (mip->dir)
  1153. { case GLP_MIN:
  1154. if (bound >= mip->mip_obj - eps) ret = 0;
  1155. break;
  1156. case GLP_MAX:
  1157. if (bound <= mip->mip_obj + eps) ret = 0;
  1158. break;
  1159. default:
  1160. xassert(mip != mip);
  1161. }
  1162. }
  1163. else
  1164. { switch (mip->dir)
  1165. { case GLP_MIN:
  1166. if (bound == +DBL_MAX) ret = 0;
  1167. break;
  1168. case GLP_MAX:
  1169. if (bound == -DBL_MAX) ret = 0;
  1170. break;
  1171. default:
  1172. xassert(mip != mip);
  1173. }
  1174. }
  1175. return ret;
  1176. }
  1177. /***********************************************************************
  1178. * NAME
  1179. *
  1180. * ios_best_node - find active node with best local bound
  1181. *
  1182. * SYNOPSIS
  1183. *
  1184. * #include "glpios.h"
  1185. * int ios_best_node(glp_tree *tree);
  1186. *
  1187. * DESCRIPTION
  1188. *
  1189. * The routine ios_best_node finds an active node whose local bound is
  1190. * best among other active nodes.
  1191. *
  1192. * It is understood that the integer optimal solution of the original
  1193. * mip problem cannot be better than the best bound, so the best bound
  1194. * is an lower (minimization) or upper (maximization) global bound for
  1195. * the original problem.
  1196. *
  1197. * RETURNS
  1198. *
  1199. * The routine ios_best_node returns the subproblem reference number
  1200. * for the best node. However, if the tree is empty, it returns zero. */
  1201. int ios_best_node(glp_tree *tree)
  1202. { IOSNPD *node, *best = NULL;
  1203. switch (tree->mip->dir)
  1204. { case GLP_MIN:
  1205. /* minimization */
  1206. for (node = tree->head; node != NULL; node = node->next)
  1207. if (best == NULL || best->bound > node->bound)
  1208. best = node;
  1209. break;
  1210. case GLP_MAX:
  1211. /* maximization */
  1212. for (node = tree->head; node != NULL; node = node->next)
  1213. if (best == NULL || best->bound < node->bound)
  1214. best = node;
  1215. break;
  1216. default:
  1217. xassert(tree != tree);
  1218. }
  1219. return best == NULL ? 0 : best->p;
  1220. }
  1221. /***********************************************************************
  1222. * NAME
  1223. *
  1224. * ios_relative_gap - compute relative mip gap
  1225. *
  1226. * SYNOPSIS
  1227. *
  1228. * #include "glpios.h"
  1229. * double ios_relative_gap(glp_tree *tree);
  1230. *
  1231. * DESCRIPTION
  1232. *
  1233. * The routine ios_relative_gap computes the relative mip gap using the
  1234. * formula:
  1235. *
  1236. * gap = |best_mip - best_bnd| / (|best_mip| + DBL_EPSILON),
  1237. *
  1238. * where best_mip is the best integer feasible solution found so far,
  1239. * best_bnd is the best (global) bound. If no integer feasible solution
  1240. * has been found yet, rel_gap is set to DBL_MAX.
  1241. *
  1242. * RETURNS
  1243. *
  1244. * The routine ios_relative_gap returns the relative mip gap. */
  1245. double ios_relative_gap(glp_tree *tree)
  1246. { glp_prob *mip = tree->mip;
  1247. int p;
  1248. double best_mip, best_bnd, gap;
  1249. if (mip->mip_stat == GLP_FEAS)
  1250. { best_mip = mip->mip_obj;
  1251. p = ios_best_node(tree);
  1252. if (p == 0)
  1253. { /* the tree is empty */
  1254. gap = 0.0;
  1255. }
  1256. else
  1257. { best_bnd = tree->slot[p].node->bound;
  1258. gap = fabs(best_mip - best_bnd) / (fabs(best_mip) +
  1259. DBL_EPSILON);
  1260. }
  1261. }
  1262. else
  1263. { /* no integer feasible solution has been found yet */
  1264. gap = DBL_MAX;
  1265. }
  1266. return gap;
  1267. }
  1268. /***********************************************************************
  1269. * NAME
  1270. *
  1271. * ios_solve_node - solve LP relaxation of current subproblem
  1272. *
  1273. * SYNOPSIS
  1274. *
  1275. * #include "glpios.h"
  1276. * int ios_solve_node(glp_tree *tree);
  1277. *
  1278. * DESCRIPTION
  1279. *
  1280. * The routine ios_solve_node re-optimizes LP relaxation of the current
  1281. * subproblem using the dual simplex method.
  1282. *
  1283. * RETURNS
  1284. *
  1285. * The routine returns the code which is reported by glp_simplex. */
  1286. int ios_solve_node(glp_tree *tree)
  1287. { glp_prob *mip = tree->mip;
  1288. glp_smcp parm;
  1289. int ret;
  1290. /* the current subproblem must exist */
  1291. xassert(tree->curr != NULL);
  1292. /* set some control parameters */
  1293. glp_init_smcp(&parm);
  1294. switch (tree->parm->msg_lev)
  1295. { case GLP_MSG_OFF:
  1296. parm.msg_lev = GLP_MSG_OFF; break;
  1297. case GLP_MSG_ERR:
  1298. parm.msg_lev = GLP_MSG_ERR; break;
  1299. case GLP_MSG_ON:
  1300. case GLP_MSG_ALL:
  1301. parm.msg_lev = GLP_MSG_ON; break;
  1302. case GLP_MSG_DBG:
  1303. parm.msg_lev = GLP_MSG_ALL; break;
  1304. default:
  1305. xassert(tree != tree);
  1306. }
  1307. parm.meth = GLP_DUALP;
  1308. if (tree->parm->msg_lev < GLP_MSG_DBG)
  1309. parm.out_dly = tree->parm->out_dly;
  1310. else
  1311. parm.out_dly = 0;
  1312. /* if the incumbent objective value is already known, use it to
  1313. prematurely terminate the dual simplex search */
  1314. if (mip->mip_stat == GLP_FEAS)
  1315. { switch (tree->mip->dir)
  1316. { case GLP_MIN:
  1317. parm.obj_ul = mip->mip_obj;
  1318. break;
  1319. case GLP_MAX:
  1320. parm.obj_ll = mip->mip_obj;
  1321. break;
  1322. default:
  1323. xassert(mip != mip);
  1324. }
  1325. }
  1326. /* try to solve/re-optimize the LP relaxation */
  1327. ret = glp_simplex(mip, &parm);
  1328. tree->curr->solved++;
  1329. #if 0
  1330. xprintf("ret = %d; status = %d; pbs = %d; dbs = %d; some = %d\n",
  1331. ret, glp_get_status(mip), mip->pbs_stat, mip->dbs_stat,
  1332. mip->some);
  1333. lpx_print_sol(mip, "sol");
  1334. #endif
  1335. return ret;
  1336. }
  1337. /**********************************************************************/
  1338. IOSPOOL *ios_create_pool(glp_tree *tree)
  1339. { /* create cut pool */
  1340. IOSPOOL *pool;
  1341. #if 0
  1342. pool = dmp_get_atom(tree->pool, sizeof(IOSPOOL));
  1343. #else
  1344. xassert(tree == tree);
  1345. pool = xmalloc(sizeof(IOSPOOL));
  1346. #endif
  1347. pool->size = 0;
  1348. pool->head = pool->tail = NULL;
  1349. pool->ord = 0, pool->curr = NULL;
  1350. return pool;
  1351. }
  1352. int ios_add_row(glp_tree *tree, IOSPOOL *pool,
  1353. const char *name, int klass, int flags, int len, const int ind[],
  1354. const double val[], int type, double rhs)
  1355. { /* add row (constraint) to the cut pool */
  1356. IOSCUT *cut;
  1357. IOSAIJ *aij;
  1358. int k;
  1359. xassert(pool != NULL);
  1360. cut = dmp_get_atom(tree->pool, sizeof(IOSCUT));
  1361. if (name == NULL || name[0] == '\0')
  1362. cut->name = NULL;
  1363. else
  1364. { for (k = 0; name[k] != '\0'; k++)
  1365. { if (k == 256)
  1366. xerror("glp_ios_add_row: cut name too long\n");
  1367. if (iscntrl((unsigned char)name[k]))
  1368. xerror("glp_ios_add_row: cut name contains invalid chara"
  1369. "cter(s)\n");
  1370. }
  1371. cut->name = dmp_get_atom(tree->pool, strlen(name)+1);
  1372. strcpy(cut->name, name);
  1373. }
  1374. if (!(0 <= klass && klass <= 255))
  1375. xerror("glp_ios_add_row: klass = %d; invalid cut class\n",
  1376. klass);
  1377. cut->klass = (unsigned char)klass;
  1378. if (flags != 0)
  1379. xerror("glp_ios_add_row: flags = %d; invalid cut flags\n",
  1380. flags);
  1381. cut->ptr = NULL;
  1382. if (!(0 <= len && len <= tree->n))
  1383. xerror("glp_ios_add_row: len = %d; invalid cut length\n",
  1384. len);
  1385. for (k = 1; k <= len; k++)
  1386. { aij = dmp_get_atom(tree->pool, sizeof(IOSAIJ));
  1387. if (!(1 <= ind[k] && ind[k] <= tree->n))
  1388. xerror("glp_ios_add_row: ind[%d] = %d; column index out of "
  1389. "range\n", k, ind[k]);
  1390. aij->j = ind[k];
  1391. aij->val = val[k];
  1392. aij->next = cut->ptr;
  1393. cut->ptr = aij;
  1394. }
  1395. if (!(type == GLP_LO || type == GLP_UP || type == GLP_FX))
  1396. xerror("glp_ios_add_row: type = %d; invalid cut type\n",
  1397. type);
  1398. cut->type = (unsigned char)type;
  1399. cut->rhs = rhs;
  1400. cut->prev = pool->tail;
  1401. cut->next = NULL;
  1402. if (cut->prev == NULL)
  1403. pool->head = cut;
  1404. else
  1405. cut->prev->next = cut;
  1406. pool->tail = cut;
  1407. pool->size++;
  1408. return pool->size;
  1409. }
  1410. IOSCUT *ios_find_row(IOSPOOL *pool, int i)
  1411. { /* find row (constraint) in the cut pool */
  1412. /* (smart linear search) */
  1413. xassert(pool != NULL);
  1414. xassert(1 <= i && i <= pool->size);
  1415. if (pool->ord == 0)
  1416. { xassert(pool->curr == NULL);
  1417. pool->ord = 1;
  1418. pool->curr = pool->head;
  1419. }
  1420. xassert(pool->curr != NULL);
  1421. if (i < pool->ord)
  1422. { if (i < pool->ord - i)
  1423. { pool->ord = 1;
  1424. pool->curr = pool->head;
  1425. while (pool->ord != i)
  1426. { pool->ord++;
  1427. xassert(pool->curr != NULL);
  1428. pool->curr = pool->curr->next;
  1429. }
  1430. }
  1431. else
  1432. { while (pool->ord != i)
  1433. { pool->ord--;
  1434. xassert(pool->curr != NULL);
  1435. pool->curr = pool->curr->prev;
  1436. }
  1437. }
  1438. }
  1439. else if (i > pool->ord)
  1440. { if (i - pool->ord < pool->size - i)
  1441. { while (pool->ord != i)
  1442. { pool->ord++;
  1443. xassert(pool->curr != NULL);
  1444. pool->curr = pool->curr->next;
  1445. }
  1446. }
  1447. else
  1448. { pool->ord = pool->size;
  1449. pool->curr = pool->tail;
  1450. while (pool->ord != i)
  1451. { pool->ord--;
  1452. xassert(pool->curr != NULL);
  1453. pool->curr = pool->curr->prev;
  1454. }
  1455. }
  1456. }
  1457. xassert(pool->ord == i);
  1458. xassert(pool->curr != NULL);
  1459. return pool->curr;
  1460. }
  1461. void ios_del_row(glp_tree *tree, IOSPOOL *pool, int i)
  1462. { /* remove row (constraint) from the cut pool */
  1463. IOSCUT *cut;
  1464. IOSAIJ *aij;
  1465. xassert(pool != NULL);
  1466. if (!(1 <= i && i <= pool->size))
  1467. xerror("glp_ios_del_row: i = %d; cut number out of range\n",
  1468. i);
  1469. cut = ios_find_row(pool, i);
  1470. xassert(pool->curr == cut);
  1471. if (cut->next != NULL)
  1472. pool->curr = cut->next;
  1473. else if (cut->prev != NULL)
  1474. pool->ord--, pool->curr = cut->prev;
  1475. else
  1476. pool->ord = 0, pool->curr = NULL;
  1477. if (cut->name != NULL)
  1478. dmp_free_atom(tree->pool, cut->name, strlen(cut->name)+1);
  1479. if (cut->prev == NULL)
  1480. { xassert(pool->head == cut);
  1481. pool->head = cut->next;
  1482. }
  1483. else
  1484. { xassert(cut->prev->next == cut);
  1485. cut->prev->next = cut->next;
  1486. }
  1487. if (cut->next == NULL)
  1488. { xassert(pool->tail == cut);
  1489. pool->tail = cut->prev;
  1490. }
  1491. else
  1492. { xassert(cut->next->prev == cut);
  1493. cut->next->prev = cut->prev;
  1494. }
  1495. while (cut->ptr != NULL)
  1496. { aij = cut->ptr;
  1497. cut->ptr = aij->next;
  1498. dmp_free_atom(tree->pool, aij, sizeof(IOSAIJ));
  1499. }
  1500. dmp_free_atom(tree->pool, cut, sizeof(IOSCUT));
  1501. pool->size--;
  1502. return;
  1503. }
  1504. void ios_clear_pool(glp_tree *tree, IOSPOOL *pool)
  1505. { /* remove all rows (constraints) from the cut pool */
  1506. xassert(pool != NULL);
  1507. while (pool->head != NULL)
  1508. { IOSCUT *cut = pool->head;
  1509. pool->head = cut->next;
  1510. if (cut->name != NULL)
  1511. dmp_free_atom(tree->pool, cut->name, strlen(cut->name)+1);
  1512. while (cut->ptr != NULL)
  1513. { IOSAIJ *aij = cut->ptr;
  1514. cut->ptr = aij->next;
  1515. dmp_free_atom(tree->pool, aij, sizeof(IOSAIJ));
  1516. }
  1517. dmp_free_atom(tree->pool, cut, sizeof(IOSCUT));
  1518. }
  1519. pool->size = 0;
  1520. pool->head = pool->tail = NULL;
  1521. pool->ord = 0, pool->curr = NULL;
  1522. return;
  1523. }
  1524. void ios_delete_pool(glp_tree *tree, IOSPOOL *pool)
  1525. { /* delete cut pool */
  1526. xassert(pool != NULL);
  1527. ios_clear_pool(tree, pool);
  1528. xfree(pool);
  1529. return;
  1530. }
  1531. #if 1 /* 11/VII-2013 */
  1532. #include "glpnpp.h"
  1533. void ios_process_sol(glp_tree *T)
  1534. { /* process integer feasible solution just found */
  1535. if (T->npp != NULL)
  1536. { /* postprocess solution from transformed mip */
  1537. npp_postprocess(T->npp, T->mip);
  1538. /* store solution to problem passed to glp_intopt */
  1539. npp_unload_sol(T->npp, T->P);
  1540. }
  1541. xassert(T->P != NULL);
  1542. /* save solution to text file, if requested */
  1543. if (T->save_sol != NULL)
  1544. { char *fn, *mark;
  1545. fn = talloc(strlen(T->save_sol) + 50, char);
  1546. mark = strrchr(T->save_sol, '*');
  1547. if (mark == NULL)
  1548. strcpy(fn, T->save_sol);
  1549. else
  1550. { memcpy(fn, T->save_sol, mark - T->save_sol);
  1551. fn[mark - T->save_sol] = '\0';
  1552. sprintf(fn + strlen(fn), "%03d", ++(T->save_cnt));
  1553. strcat(fn, &mark[1]);
  1554. }
  1555. glp_write_mip(T->P, fn);
  1556. tfree(fn);
  1557. }
  1558. return;
  1559. }
  1560. #endif
  1561. /* eof */