glpapi13.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /* glpapi13.c (branch-and-bound interface routines) */
  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 Andrew Makhorin, Department for Applied Informatics,
  7. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  8. * 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 "glpios.h"
  24. /***********************************************************************
  25. * NAME
  26. *
  27. * glp_ios_reason - determine reason for calling the callback routine
  28. *
  29. * SYNOPSIS
  30. *
  31. * glp_ios_reason(glp_tree *tree);
  32. *
  33. * RETURNS
  34. *
  35. * The routine glp_ios_reason returns a code, which indicates why the
  36. * user-defined callback routine is being called. */
  37. int glp_ios_reason(glp_tree *tree)
  38. { return
  39. tree->reason;
  40. }
  41. /***********************************************************************
  42. * NAME
  43. *
  44. * glp_ios_get_prob - access the problem object
  45. *
  46. * SYNOPSIS
  47. *
  48. * glp_prob *glp_ios_get_prob(glp_tree *tree);
  49. *
  50. * DESCRIPTION
  51. *
  52. * The routine glp_ios_get_prob can be called from the user-defined
  53. * callback routine to access the problem object, which is used by the
  54. * MIP solver. It is the original problem object passed to the routine
  55. * glp_intopt if the MIP presolver is not used; otherwise it is an
  56. * internal problem object built by the presolver. If the current
  57. * subproblem exists, LP segment of the problem object corresponds to
  58. * its LP relaxation.
  59. *
  60. * RETURNS
  61. *
  62. * The routine glp_ios_get_prob returns a pointer to the problem object
  63. * used by the MIP solver. */
  64. glp_prob *glp_ios_get_prob(glp_tree *tree)
  65. { return
  66. tree->mip;
  67. }
  68. /***********************************************************************
  69. * NAME
  70. *
  71. * glp_ios_tree_size - determine size of the branch-and-bound tree
  72. *
  73. * SYNOPSIS
  74. *
  75. * void glp_ios_tree_size(glp_tree *tree, int *a_cnt, int *n_cnt,
  76. * int *t_cnt);
  77. *
  78. * DESCRIPTION
  79. *
  80. * The routine glp_ios_tree_size stores the following three counts which
  81. * characterize the current size of the branch-and-bound tree:
  82. *
  83. * a_cnt is the current number of active nodes, i.e. the current size of
  84. * the active list;
  85. *
  86. * n_cnt is the current number of all (active and inactive) nodes;
  87. *
  88. * t_cnt is the total number of nodes including those which have been
  89. * already removed from the tree. This count is increased whenever
  90. * a new node appears in the tree and never decreased.
  91. *
  92. * If some of the parameters a_cnt, n_cnt, t_cnt is a null pointer, the
  93. * corresponding count is not stored. */
  94. void glp_ios_tree_size(glp_tree *tree, int *a_cnt, int *n_cnt,
  95. int *t_cnt)
  96. { if (a_cnt != NULL) *a_cnt = tree->a_cnt;
  97. if (n_cnt != NULL) *n_cnt = tree->n_cnt;
  98. if (t_cnt != NULL) *t_cnt = tree->t_cnt;
  99. return;
  100. }
  101. /***********************************************************************
  102. * NAME
  103. *
  104. * glp_ios_curr_node - determine current active subproblem
  105. *
  106. * SYNOPSIS
  107. *
  108. * int glp_ios_curr_node(glp_tree *tree);
  109. *
  110. * RETURNS
  111. *
  112. * The routine glp_ios_curr_node returns the reference number of the
  113. * current active subproblem. However, if the current subproblem does
  114. * not exist, the routine returns zero. */
  115. int glp_ios_curr_node(glp_tree *tree)
  116. { IOSNPD *node;
  117. /* obtain pointer to the current subproblem */
  118. node = tree->curr;
  119. /* return its reference number */
  120. return node == NULL ? 0 : node->p;
  121. }
  122. /***********************************************************************
  123. * NAME
  124. *
  125. * glp_ios_next_node - determine next active subproblem
  126. *
  127. * SYNOPSIS
  128. *
  129. * int glp_ios_next_node(glp_tree *tree, int p);
  130. *
  131. * RETURNS
  132. *
  133. * If the parameter p is zero, the routine glp_ios_next_node returns
  134. * the reference number of the first active subproblem. However, if the
  135. * tree is empty, zero is returned.
  136. *
  137. * If the parameter p is not zero, it must specify the reference number
  138. * of some active subproblem, in which case the routine returns the
  139. * reference number of the next active subproblem. However, if there is
  140. * no next active subproblem in the list, zero is returned.
  141. *
  142. * All subproblems in the active list are ordered chronologically, i.e.
  143. * subproblem A precedes subproblem B if A was created before B. */
  144. int glp_ios_next_node(glp_tree *tree, int p)
  145. { IOSNPD *node;
  146. if (p == 0)
  147. { /* obtain pointer to the first active subproblem */
  148. node = tree->head;
  149. }
  150. else
  151. { /* obtain pointer to the specified subproblem */
  152. if (!(1 <= p && p <= tree->nslots))
  153. err: xerror("glp_ios_next_node: p = %d; invalid subproblem refer"
  154. "ence number\n", p);
  155. node = tree->slot[p].node;
  156. if (node == NULL) goto err;
  157. /* the specified subproblem must be active */
  158. if (node->count != 0)
  159. xerror("glp_ios_next_node: p = %d; subproblem not in the ac"
  160. "tive list\n", p);
  161. /* obtain pointer to the next active subproblem */
  162. node = node->next;
  163. }
  164. /* return the reference number */
  165. return node == NULL ? 0 : node->p;
  166. }
  167. /***********************************************************************
  168. * NAME
  169. *
  170. * glp_ios_prev_node - determine previous active subproblem
  171. *
  172. * SYNOPSIS
  173. *
  174. * int glp_ios_prev_node(glp_tree *tree, int p);
  175. *
  176. * RETURNS
  177. *
  178. * If the parameter p is zero, the routine glp_ios_prev_node returns
  179. * the reference number of the last active subproblem. However, if the
  180. * tree is empty, zero is returned.
  181. *
  182. * If the parameter p is not zero, it must specify the reference number
  183. * of some active subproblem, in which case the routine returns the
  184. * reference number of the previous active subproblem. However, if there
  185. * is no previous active subproblem in the list, zero is returned.
  186. *
  187. * All subproblems in the active list are ordered chronologically, i.e.
  188. * subproblem A precedes subproblem B if A was created before B. */
  189. int glp_ios_prev_node(glp_tree *tree, int p)
  190. { IOSNPD *node;
  191. if (p == 0)
  192. { /* obtain pointer to the last active subproblem */
  193. node = tree->tail;
  194. }
  195. else
  196. { /* obtain pointer to the specified subproblem */
  197. if (!(1 <= p && p <= tree->nslots))
  198. err: xerror("glp_ios_prev_node: p = %d; invalid subproblem refer"
  199. "ence number\n", p);
  200. node = tree->slot[p].node;
  201. if (node == NULL) goto err;
  202. /* the specified subproblem must be active */
  203. if (node->count != 0)
  204. xerror("glp_ios_prev_node: p = %d; subproblem not in the ac"
  205. "tive list\n", p);
  206. /* obtain pointer to the previous active subproblem */
  207. node = node->prev;
  208. }
  209. /* return the reference number */
  210. return node == NULL ? 0 : node->p;
  211. }
  212. /***********************************************************************
  213. * NAME
  214. *
  215. * glp_ios_up_node - determine parent subproblem
  216. *
  217. * SYNOPSIS
  218. *
  219. * int glp_ios_up_node(glp_tree *tree, int p);
  220. *
  221. * RETURNS
  222. *
  223. * The parameter p must specify the reference number of some (active or
  224. * inactive) subproblem, in which case the routine iet_get_up_node
  225. * returns the reference number of its parent subproblem. However, if
  226. * the specified subproblem is the root of the tree and, therefore, has
  227. * no parent, the routine returns zero. */
  228. int glp_ios_up_node(glp_tree *tree, int p)
  229. { IOSNPD *node;
  230. /* obtain pointer to the specified subproblem */
  231. if (!(1 <= p && p <= tree->nslots))
  232. err: xerror("glp_ios_up_node: p = %d; invalid subproblem reference "
  233. "number\n", p);
  234. node = tree->slot[p].node;
  235. if (node == NULL) goto err;
  236. /* obtain pointer to the parent subproblem */
  237. node = node->up;
  238. /* return the reference number */
  239. return node == NULL ? 0 : node->p;
  240. }
  241. /***********************************************************************
  242. * NAME
  243. *
  244. * glp_ios_node_level - determine subproblem level
  245. *
  246. * SYNOPSIS
  247. *
  248. * int glp_ios_node_level(glp_tree *tree, int p);
  249. *
  250. * RETURNS
  251. *
  252. * The routine glp_ios_node_level returns the level of the subproblem,
  253. * whose reference number is p, in the branch-and-bound tree. (The root
  254. * subproblem has level 0, and the level of any other subproblem is the
  255. * level of its parent plus one.) */
  256. int glp_ios_node_level(glp_tree *tree, int p)
  257. { IOSNPD *node;
  258. /* obtain pointer to the specified subproblem */
  259. if (!(1 <= p && p <= tree->nslots))
  260. err: xerror("glp_ios_node_level: p = %d; invalid subproblem referen"
  261. "ce number\n", p);
  262. node = tree->slot[p].node;
  263. if (node == NULL) goto err;
  264. /* return the node level */
  265. return node->level;
  266. }
  267. /***********************************************************************
  268. * NAME
  269. *
  270. * glp_ios_node_bound - determine subproblem local bound
  271. *
  272. * SYNOPSIS
  273. *
  274. * double glp_ios_node_bound(glp_tree *tree, int p);
  275. *
  276. * RETURNS
  277. *
  278. * The routine glp_ios_node_bound returns the local bound for (active or
  279. * inactive) subproblem, whose reference number is p.
  280. *
  281. * COMMENTS
  282. *
  283. * The local bound for subproblem p is an lower (minimization) or upper
  284. * (maximization) bound for integer optimal solution to this subproblem
  285. * (not to the original problem). This bound is local in the sense that
  286. * only subproblems in the subtree rooted at node p cannot have better
  287. * integer feasible solutions.
  288. *
  289. * On creating a subproblem (due to the branching step) its local bound
  290. * is inherited from its parent and then may get only stronger (never
  291. * weaker). For the root subproblem its local bound is initially set to
  292. * -DBL_MAX (minimization) or +DBL_MAX (maximization) and then improved
  293. * as the root LP relaxation has been solved.
  294. *
  295. * Note that the local bound is not necessarily the optimal objective
  296. * value to corresponding LP relaxation; it may be stronger. */
  297. double glp_ios_node_bound(glp_tree *tree, int p)
  298. { IOSNPD *node;
  299. /* obtain pointer to the specified subproblem */
  300. if (!(1 <= p && p <= tree->nslots))
  301. err: xerror("glp_ios_node_bound: p = %d; invalid subproblem referen"
  302. "ce number\n", p);
  303. node = tree->slot[p].node;
  304. if (node == NULL) goto err;
  305. /* return the node local bound */
  306. return node->bound;
  307. }
  308. /***********************************************************************
  309. * NAME
  310. *
  311. * glp_ios_best_node - find active subproblem with best local bound
  312. *
  313. * SYNOPSIS
  314. *
  315. * int glp_ios_best_node(glp_tree *tree);
  316. *
  317. * RETURNS
  318. *
  319. * The routine glp_ios_best_node returns the reference number of the
  320. * active subproblem, whose local bound is best (i.e. smallest in case
  321. * of minimization or largest in case of maximization). However, if the
  322. * tree is empty, the routine returns zero.
  323. *
  324. * COMMENTS
  325. *
  326. * The best local bound is an lower (minimization) or upper
  327. * (maximization) bound for integer optimal solution to the original
  328. * MIP problem. */
  329. int glp_ios_best_node(glp_tree *tree)
  330. { return
  331. ios_best_node(tree);
  332. }
  333. /***********************************************************************
  334. * NAME
  335. *
  336. * glp_ios_mip_gap - compute relative MIP gap
  337. *
  338. * SYNOPSIS
  339. *
  340. * double glp_ios_mip_gap(glp_tree *tree);
  341. *
  342. * DESCRIPTION
  343. *
  344. * The routine glp_ios_mip_gap computes the relative MIP gap with the
  345. * following formula:
  346. *
  347. * gap = |best_mip - best_bnd| / (|best_mip| + DBL_EPSILON),
  348. *
  349. * where best_mip is the best integer feasible solution found so far,
  350. * best_bnd is the best (global) bound. If no integer feasible solution
  351. * has been found yet, gap is set to DBL_MAX.
  352. *
  353. * RETURNS
  354. *
  355. * The routine glp_ios_mip_gap returns the relative MIP gap. */
  356. double glp_ios_mip_gap(glp_tree *tree)
  357. { return
  358. ios_relative_gap(tree);
  359. }
  360. /***********************************************************************
  361. * NAME
  362. *
  363. * glp_ios_node_data - access subproblem application-specific data
  364. *
  365. * SYNOPSIS
  366. *
  367. * void *glp_ios_node_data(glp_tree *tree, int p);
  368. *
  369. * DESCRIPTION
  370. *
  371. * The routine glp_ios_node_data allows the application accessing a
  372. * memory block allocated for the subproblem (which may be active or
  373. * inactive), whose reference number is p.
  374. *
  375. * The size of the block is defined by the control parameter cb_size
  376. * passed to the routine glp_intopt. The block is initialized by binary
  377. * zeros on creating corresponding subproblem, and its contents is kept
  378. * until the subproblem will be removed from the tree.
  379. *
  380. * The application may use these memory blocks to store specific data
  381. * for each subproblem.
  382. *
  383. * RETURNS
  384. *
  385. * The routine glp_ios_node_data returns a pointer to the memory block
  386. * for the specified subproblem. Note that if cb_size = 0, the routine
  387. * returns a null pointer. */
  388. void *glp_ios_node_data(glp_tree *tree, int p)
  389. { IOSNPD *node;
  390. /* obtain pointer to the specified subproblem */
  391. if (!(1 <= p && p <= tree->nslots))
  392. err: xerror("glp_ios_node_level: p = %d; invalid subproblem referen"
  393. "ce number\n", p);
  394. node = tree->slot[p].node;
  395. if (node == NULL) goto err;
  396. /* return pointer to the application-specific data */
  397. return node->data;
  398. }
  399. /***********************************************************************
  400. * NAME
  401. *
  402. * glp_ios_row_attr - retrieve additional row attributes
  403. *
  404. * SYNOPSIS
  405. *
  406. * void glp_ios_row_attr(glp_tree *tree, int i, glp_attr *attr);
  407. *
  408. * DESCRIPTION
  409. *
  410. * The routine glp_ios_row_attr retrieves additional attributes of row
  411. * i and stores them in the structure glp_attr. */
  412. void glp_ios_row_attr(glp_tree *tree, int i, glp_attr *attr)
  413. { GLPROW *row;
  414. if (!(1 <= i && i <= tree->mip->m))
  415. xerror("glp_ios_row_attr: i = %d; row number out of range\n",
  416. i);
  417. row = tree->mip->row[i];
  418. attr->level = row->level;
  419. attr->origin = row->origin;
  420. attr->klass = row->klass;
  421. return;
  422. }
  423. /**********************************************************************/
  424. int glp_ios_pool_size(glp_tree *tree)
  425. { /* determine current size of the cut pool */
  426. if (tree->reason != GLP_ICUTGEN)
  427. xerror("glp_ios_pool_size: operation not allowed\n");
  428. xassert(tree->local != NULL);
  429. return tree->local->size;
  430. }
  431. /**********************************************************************/
  432. int glp_ios_add_row(glp_tree *tree,
  433. const char *name, int klass, int flags, int len, const int ind[],
  434. const double val[], int type, double rhs)
  435. { /* add row (constraint) to the cut pool */
  436. int num;
  437. if (tree->reason != GLP_ICUTGEN)
  438. xerror("glp_ios_add_row: operation not allowed\n");
  439. xassert(tree->local != NULL);
  440. num = ios_add_row(tree, tree->local, name, klass, flags, len,
  441. ind, val, type, rhs);
  442. return num;
  443. }
  444. /**********************************************************************/
  445. void glp_ios_del_row(glp_tree *tree, int i)
  446. { /* remove row (constraint) from the cut pool */
  447. if (tree->reason != GLP_ICUTGEN)
  448. xerror("glp_ios_del_row: operation not allowed\n");
  449. ios_del_row(tree, tree->local, i);
  450. return;
  451. }
  452. /**********************************************************************/
  453. void glp_ios_clear_pool(glp_tree *tree)
  454. { /* remove all rows (constraints) from the cut pool */
  455. if (tree->reason != GLP_ICUTGEN)
  456. xerror("glp_ios_clear_pool: operation not allowed\n");
  457. ios_clear_pool(tree, tree->local);
  458. return;
  459. }
  460. /***********************************************************************
  461. * NAME
  462. *
  463. * glp_ios_can_branch - check if can branch upon specified variable
  464. *
  465. * SYNOPSIS
  466. *
  467. * int glp_ios_can_branch(glp_tree *tree, int j);
  468. *
  469. * RETURNS
  470. *
  471. * If j-th variable (column) can be used to branch upon, the routine
  472. * glp_ios_can_branch returns non-zero, otherwise zero. */
  473. int glp_ios_can_branch(glp_tree *tree, int j)
  474. { if (!(1 <= j && j <= tree->mip->n))
  475. xerror("glp_ios_can_branch: j = %d; column number out of range"
  476. "\n", j);
  477. return tree->non_int[j];
  478. }
  479. /***********************************************************************
  480. * NAME
  481. *
  482. * glp_ios_branch_upon - choose variable to branch upon
  483. *
  484. * SYNOPSIS
  485. *
  486. * void glp_ios_branch_upon(glp_tree *tree, int j, int sel);
  487. *
  488. * DESCRIPTION
  489. *
  490. * The routine glp_ios_branch_upon can be called from the user-defined
  491. * callback routine in response to the reason GLP_IBRANCH to choose a
  492. * branching variable, whose ordinal number is j. Should note that only
  493. * variables, for which the routine glp_ios_can_branch returns non-zero,
  494. * can be used to branch upon.
  495. *
  496. * The parameter sel is a flag that indicates which branch (subproblem)
  497. * should be selected next to continue the search:
  498. *
  499. * GLP_DN_BRNCH - select down-branch;
  500. * GLP_UP_BRNCH - select up-branch;
  501. * GLP_NO_BRNCH - use general selection technique. */
  502. void glp_ios_branch_upon(glp_tree *tree, int j, int sel)
  503. { if (!(1 <= j && j <= tree->mip->n))
  504. xerror("glp_ios_branch_upon: j = %d; column number out of rang"
  505. "e\n", j);
  506. if (!(sel == GLP_DN_BRNCH || sel == GLP_UP_BRNCH ||
  507. sel == GLP_NO_BRNCH))
  508. xerror("glp_ios_branch_upon: sel = %d: invalid branch selectio"
  509. "n flag\n", sel);
  510. if (!(tree->non_int[j]))
  511. xerror("glp_ios_branch_upon: j = %d; variable cannot be used t"
  512. "o branch upon\n", j);
  513. if (tree->br_var != 0)
  514. xerror("glp_ios_branch_upon: branching variable already chosen"
  515. "\n");
  516. tree->br_var = j;
  517. tree->br_sel = sel;
  518. return;
  519. }
  520. /***********************************************************************
  521. * NAME
  522. *
  523. * glp_ios_select_node - select subproblem to continue the search
  524. *
  525. * SYNOPSIS
  526. *
  527. * void glp_ios_select_node(glp_tree *tree, int p);
  528. *
  529. * DESCRIPTION
  530. *
  531. * The routine glp_ios_select_node can be called from the user-defined
  532. * callback routine in response to the reason GLP_ISELECT to select an
  533. * active subproblem, whose reference number is p. The search will be
  534. * continued from the subproblem selected. */
  535. void glp_ios_select_node(glp_tree *tree, int p)
  536. { IOSNPD *node;
  537. /* obtain pointer to the specified subproblem */
  538. if (!(1 <= p && p <= tree->nslots))
  539. err: xerror("glp_ios_select_node: p = %d; invalid subproblem refere"
  540. "nce number\n", p);
  541. node = tree->slot[p].node;
  542. if (node == NULL) goto err;
  543. /* the specified subproblem must be active */
  544. if (node->count != 0)
  545. xerror("glp_ios_select_node: p = %d; subproblem not in the act"
  546. "ive list\n", p);
  547. /* no subproblem must be selected yet */
  548. if (tree->next_p != 0)
  549. xerror("glp_ios_select_node: subproblem already selected\n");
  550. /* select the specified subproblem to continue the search */
  551. tree->next_p = p;
  552. return;
  553. }
  554. /***********************************************************************
  555. * NAME
  556. *
  557. * glp_ios_heur_sol - provide solution found by heuristic
  558. *
  559. * SYNOPSIS
  560. *
  561. * int glp_ios_heur_sol(glp_tree *tree, const double x[]);
  562. *
  563. * DESCRIPTION
  564. *
  565. * The routine glp_ios_heur_sol can be called from the user-defined
  566. * callback routine in response to the reason GLP_IHEUR to provide an
  567. * integer feasible solution found by a primal heuristic.
  568. *
  569. * Primal values of *all* variables (columns) found by the heuristic
  570. * should be placed in locations x[1], ..., x[n], where n is the number
  571. * of columns in the original problem object. Note that the routine
  572. * glp_ios_heur_sol *does not* check primal feasibility of the solution
  573. * provided.
  574. *
  575. * Using the solution passed in the array x the routine computes value
  576. * of the objective function. If the objective value is better than the
  577. * best known integer feasible solution, the routine computes values of
  578. * auxiliary variables (rows) and stores all solution components in the
  579. * problem object.
  580. *
  581. * RETURNS
  582. *
  583. * If the provided solution is accepted, the routine glp_ios_heur_sol
  584. * returns zero. Otherwise, if the provided solution is rejected, the
  585. * routine returns non-zero. */
  586. int glp_ios_heur_sol(glp_tree *tree, const double x[])
  587. { glp_prob *mip = tree->mip;
  588. int m = tree->orig_m;
  589. int n = tree->n;
  590. int i, j;
  591. double obj;
  592. xassert(mip->m >= m);
  593. xassert(mip->n == n);
  594. /* check values of integer variables and compute value of the
  595. objective function */
  596. obj = mip->c0;
  597. for (j = 1; j <= n; j++)
  598. { GLPCOL *col = mip->col[j];
  599. if (col->kind == GLP_IV)
  600. { /* provided value must be integral */
  601. if (x[j] != floor(x[j])) return 1;
  602. }
  603. obj += col->coef * x[j];
  604. }
  605. /* check if the provided solution is better than the best known
  606. integer feasible solution */
  607. if (mip->mip_stat == GLP_FEAS)
  608. { switch (mip->dir)
  609. { case GLP_MIN:
  610. if (obj >= tree->mip->mip_obj) return 1;
  611. break;
  612. case GLP_MAX:
  613. if (obj <= tree->mip->mip_obj) return 1;
  614. break;
  615. default:
  616. xassert(mip != mip);
  617. }
  618. }
  619. /* it is better; store it in the problem object */
  620. if (tree->parm->msg_lev >= GLP_MSG_ON)
  621. xprintf("Solution found by heuristic: %.12g\n", obj);
  622. mip->mip_stat = GLP_FEAS;
  623. mip->mip_obj = obj;
  624. for (j = 1; j <= n; j++)
  625. mip->col[j]->mipx = x[j];
  626. for (i = 1; i <= m; i++)
  627. { GLPROW *row = mip->row[i];
  628. GLPAIJ *aij;
  629. row->mipx = 0.0;
  630. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  631. row->mipx += aij->val * aij->col->mipx;
  632. }
  633. return 0;
  634. }
  635. /***********************************************************************
  636. * NAME
  637. *
  638. * glp_ios_terminate - terminate the solution process.
  639. *
  640. * SYNOPSIS
  641. *
  642. * void glp_ios_terminate(glp_tree *tree);
  643. *
  644. * DESCRIPTION
  645. *
  646. * The routine glp_ios_terminate sets a flag indicating that the MIP
  647. * solver should prematurely terminate the search. */
  648. void glp_ios_terminate(glp_tree *tree)
  649. { if (tree->parm->msg_lev >= GLP_MSG_DBG)
  650. xprintf("The search is prematurely terminated due to applicati"
  651. "on request\n");
  652. tree->stop = 1;
  653. return;
  654. }
  655. /* eof */