3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7 * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
9 * E-mail: <mao@gnu.org>.
11 * GLPK is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
23 ***********************************************************************/
27 /***********************************************************************
30 * ios_create_tree - create branch-and-bound tree
35 * glp_tree *ios_create_tree(glp_prob *mip, const glp_iocp *parm);
39 * The routine ios_create_tree creates the branch-and-bound tree.
41 * Being created the tree consists of the only root subproblem whose
42 * reference number is 1. Note that initially the root subproblem is in
43 * frozen state and therefore needs to be revived.
47 * The routine returns a pointer to the tree created. */
49 static IOSNPD *new_node(glp_tree *tree, IOSNPD *parent);
51 glp_tree *ios_create_tree(glp_prob *mip, const glp_iocp *parm)
56 xassert(mip->tree == NULL);
57 mip->tree = tree = xmalloc(sizeof(glp_tree));
58 tree->pool = dmp_create_pool();
60 /* save original problem components */
62 tree->orig_type = xcalloc(1+m+n, sizeof(char));
63 tree->orig_lb = xcalloc(1+m+n, sizeof(double));
64 tree->orig_ub = xcalloc(1+m+n, sizeof(double));
65 tree->orig_stat = xcalloc(1+m+n, sizeof(char));
66 tree->orig_prim = xcalloc(1+m+n, sizeof(double));
67 tree->orig_dual = xcalloc(1+m+n, sizeof(double));
68 for (i = 1; i <= m; i++)
69 { GLPROW *row = mip->row[i];
70 tree->orig_type[i] = (char)row->type;
71 tree->orig_lb[i] = row->lb;
72 tree->orig_ub[i] = row->ub;
73 tree->orig_stat[i] = (char)row->stat;
74 tree->orig_prim[i] = row->prim;
75 tree->orig_dual[i] = row->dual;
77 for (j = 1; j <= n; j++)
78 { GLPCOL *col = mip->col[j];
79 tree->orig_type[m+j] = (char)col->type;
80 tree->orig_lb[m+j] = col->lb;
81 tree->orig_ub[m+j] = col->ub;
82 tree->orig_stat[m+j] = (char)col->stat;
83 tree->orig_prim[m+j] = col->prim;
84 tree->orig_dual[m+j] = col->dual;
86 tree->orig_obj = mip->obj_val;
87 /* initialize the branch-and-bound tree */
91 tree->head = tree->tail = NULL;
92 tree->a_cnt = tree->n_cnt = tree->t_cnt = 0;
93 /* the root subproblem is not solved yet, so its final components
96 tree->root_type = NULL;
97 tree->root_lb = tree->root_ub = NULL;
98 tree->root_stat = NULL;
99 /* the current subproblem does not exist yet */
102 /*tree->solved = 0;*/
103 tree->non_int = xcalloc(1+n, sizeof(char));
104 memset(&tree->non_int[1], 0, n);
105 /* arrays to save parent subproblem components will be allocated
107 tree->pred_m = tree->pred_max = 0;
108 tree->pred_type = NULL;
109 tree->pred_lb = tree->pred_ub = NULL;
110 tree->pred_stat = NULL;
112 tree->local = ios_create_pool(tree);
113 /*tree->first_attempt = 1;*/
114 /*tree->max_added_cuts = 0;*/
115 /*tree->min_eff = 0.0;*/
117 /*tree->just_selected = 0;*/
118 tree->mir_gen = NULL;
119 tree->clq_gen = NULL;
122 /* create the conflict graph */
123 tree->n_ref = xcalloc(1+n, sizeof(int));
124 memset(&tree->n_ref[1], 0, n * sizeof(int));
125 tree->c_ref = xcalloc(1+n, sizeof(int));
126 memset(&tree->c_ref[1], 0, n * sizeof(int));
127 tree->g = scg_create_graph(0);
128 tree->j_ref = xcalloc(1+tree->g->n_max, sizeof(int));
130 /* pseudocost branching */
132 tree->iwrk = xcalloc(1+n, sizeof(int));
133 tree->dwrk = xcalloc(1+n, sizeof(double));
134 /* initialize control parameters */
136 tree->tm_beg = xtime();
137 tree->tm_lag = xlset(0);
139 /* initialize advanced solver interface */
147 /*tree->btrack = NULL;*/
149 /* create the root subproblem, which initially is identical to
151 new_node(tree, NULL);
155 /***********************************************************************
158 * ios_revive_node - revive specified subproblem
162 * #include "glpios.h"
163 * void ios_revive_node(glp_tree *tree, int p);
167 * The routine ios_revive_node revives the specified subproblem, whose
168 * reference number is p, and thereby makes it the current subproblem.
169 * Note that the specified subproblem must be active. Besides, if the
170 * current subproblem already exists, it must be frozen before reviving
171 * another subproblem. */
173 void ios_revive_node(glp_tree *tree, int p)
174 { glp_prob *mip = tree->mip;
176 /* obtain pointer to the specified subproblem */
177 xassert(1 <= p && p <= tree->nslots);
178 node = tree->slot[p].node;
179 xassert(node != NULL);
180 /* the specified subproblem must be active */
181 xassert(node->count == 0);
182 /* the current subproblem must not exist */
183 xassert(tree->curr == NULL);
184 /* the specified subproblem becomes current */
186 /*tree->solved = 0;*/
187 /* obtain pointer to the root subproblem */
188 root = tree->slot[1].node;
189 xassert(root != NULL);
190 /* at this point problem object components correspond to the root
191 subproblem, so if the root subproblem should be revived, there
192 is nothing more to do */
193 if (node == root) goto done;
194 xassert(mip->m == tree->root_m);
195 /* build path from the root to the current node */
197 for (node = node; node != NULL; node = node->up)
198 { if (node->up == NULL)
199 xassert(node == root);
201 node->up->temp = node;
203 /* go down from the root to the current node and make necessary
204 changes to restore components of the current subproblem */
205 for (node = root; node != NULL; node = node->temp)
208 /* if the current node is reached, the problem object at this
209 point corresponds to its parent, so save attributes of rows
210 and columns for the parent subproblem */
211 if (node->temp == NULL)
214 /* allocate/reallocate arrays, if necessary */
215 if (tree->pred_max < m + n)
216 { int new_size = m + n + 100;
217 if (tree->pred_type != NULL) xfree(tree->pred_type);
218 if (tree->pred_lb != NULL) xfree(tree->pred_lb);
219 if (tree->pred_ub != NULL) xfree(tree->pred_ub);
220 if (tree->pred_stat != NULL) xfree(tree->pred_stat);
221 tree->pred_max = new_size;
222 tree->pred_type = xcalloc(1+new_size, sizeof(char));
223 tree->pred_lb = xcalloc(1+new_size, sizeof(double));
224 tree->pred_ub = xcalloc(1+new_size, sizeof(double));
225 tree->pred_stat = xcalloc(1+new_size, sizeof(char));
227 /* save row attributes */
228 for (i = 1; i <= m; i++)
229 { GLPROW *row = mip->row[i];
230 tree->pred_type[i] = (char)row->type;
231 tree->pred_lb[i] = row->lb;
232 tree->pred_ub[i] = row->ub;
233 tree->pred_stat[i] = (char)row->stat;
235 /* save column attributes */
236 for (j = 1; j <= n; j++)
237 { GLPCOL *col = mip->col[j];
238 tree->pred_type[mip->m+j] = (char)col->type;
239 tree->pred_lb[mip->m+j] = col->lb;
240 tree->pred_ub[mip->m+j] = col->ub;
241 tree->pred_stat[mip->m+j] = (char)col->stat;
244 /* change bounds of rows and columns */
246 for (b = node->b_ptr; b != NULL; b = b->next)
248 glp_set_row_bnds(mip, b->k, b->type, b->lb, b->ub);
250 glp_set_col_bnds(mip, b->k-m, b->type, b->lb, b->ub);
253 /* change statuses of rows and columns */
255 for (s = node->s_ptr; s != NULL; s = s->next)
257 glp_set_row_stat(mip, s->k, s->stat);
259 glp_set_col_stat(mip, s->k-m, s->stat);
263 if (node->r_ptr != NULL)
268 ind = xcalloc(1+n, sizeof(int));
269 val = xcalloc(1+n, sizeof(double));
270 for (r = node->r_ptr; r != NULL; r = r->next)
271 { i = glp_add_rows(mip, 1);
272 glp_set_row_name(mip, i, r->name);
273 #if 1 /* 20/IX-2008 */
274 xassert(mip->row[i]->level == 0);
275 mip->row[i]->level = node->level;
276 mip->row[i]->origin = r->origin;
277 mip->row[i]->klass = r->klass;
279 glp_set_row_bnds(mip, i, r->type, r->lb, r->ub);
281 for (a = r->ptr; a != NULL; a = a->next)
282 len++, ind[len] = a->j, val[len] = a->val;
283 glp_set_mat_row(mip, i, len, ind, val);
284 glp_set_rii(mip, i, r->rii);
285 glp_set_row_stat(mip, i, r->stat);
291 /* add new edges to the conflict graph */
292 /* add new cliques to the conflict graph */
293 /* (not implemented yet) */
294 xassert(node->own_nn == 0);
295 xassert(node->own_nc == 0);
296 xassert(node->e_ptr == NULL);
299 /* the specified subproblem has been revived */
301 /* delete its bound change list */
302 while (node->b_ptr != NULL)
305 node->b_ptr = b->next;
306 dmp_free_atom(tree->pool, b, sizeof(IOSBND));
308 /* delete its status change list */
309 while (node->s_ptr != NULL)
312 node->s_ptr = s->next;
313 dmp_free_atom(tree->pool, s, sizeof(IOSTAT));
315 #if 1 /* 20/XI-2009 */
316 /* delete its row addition list (additional rows may appear, for
317 example, due to branching on GUB constraints */
318 while (node->r_ptr != NULL)
321 node->r_ptr = r->next;
322 xassert(r->name == NULL);
323 while (r->ptr != NULL)
327 dmp_free_atom(tree->pool, a, sizeof(IOSAIJ));
329 dmp_free_atom(tree->pool, r, sizeof(IOSROW));
335 /***********************************************************************
338 * ios_freeze_node - freeze current subproblem
342 * #include "glpios.h"
343 * void ios_freeze_node(glp_tree *tree);
347 * The routine ios_freeze_node freezes the current subproblem. */
349 void ios_freeze_node(glp_tree *tree)
350 { glp_prob *mip = tree->mip;
354 /* obtain pointer to the current subproblem */
356 xassert(node != NULL);
357 if (node->up == NULL)
358 { /* freeze the root subproblem */
360 xassert(node->p == 1);
361 xassert(tree->root_m == 0);
362 xassert(tree->root_type == NULL);
363 xassert(tree->root_lb == NULL);
364 xassert(tree->root_ub == NULL);
365 xassert(tree->root_stat == NULL);
367 tree->root_type = xcalloc(1+m+n, sizeof(char));
368 tree->root_lb = xcalloc(1+m+n, sizeof(double));
369 tree->root_ub = xcalloc(1+m+n, sizeof(double));
370 tree->root_stat = xcalloc(1+m+n, sizeof(char));
371 for (k = 1; k <= m+n; k++)
373 { GLPROW *row = mip->row[k];
374 tree->root_type[k] = (char)row->type;
375 tree->root_lb[k] = row->lb;
376 tree->root_ub[k] = row->ub;
377 tree->root_stat[k] = (char)row->stat;
380 { GLPCOL *col = mip->col[k-m];
381 tree->root_type[k] = (char)col->type;
382 tree->root_lb[k] = col->lb;
383 tree->root_ub[k] = col->ub;
384 tree->root_stat[k] = (char)col->stat;
389 { /* freeze non-root subproblem */
390 int root_m = tree->root_m;
391 int pred_m = tree->pred_m;
393 xassert(pred_m <= m);
394 /* build change lists for rows and columns which exist in the
396 xassert(node->b_ptr == NULL);
397 xassert(node->s_ptr == NULL);
398 for (k = 1; k <= pred_m + n; k++)
399 { int pred_type, pred_stat, type, stat;
400 double pred_lb, pred_ub, lb, ub;
401 /* determine attributes in the parent subproblem */
402 pred_type = tree->pred_type[k];
403 pred_lb = tree->pred_lb[k];
404 pred_ub = tree->pred_ub[k];
405 pred_stat = tree->pred_stat[k];
406 /* determine attributes in the current subproblem */
408 { GLPROW *row = mip->row[k];
415 { GLPCOL *col = mip->col[k - pred_m];
421 /* save type and bounds of a row/column, if changed */
422 if (!(pred_type == type && pred_lb == lb && pred_ub == ub))
424 b = dmp_get_atom(tree->pool, sizeof(IOSBND));
426 b->type = (unsigned char)type;
429 b->next = node->b_ptr;
432 /* save status of a row/column, if changed */
433 if (pred_stat != stat)
435 s = dmp_get_atom(tree->pool, sizeof(IOSTAT));
437 s->stat = (unsigned char)stat;
438 s->next = node->s_ptr;
442 /* save new rows added to the current subproblem */
443 xassert(node->r_ptr == NULL);
447 ind = xcalloc(1+n, sizeof(int));
448 val = xcalloc(1+n, sizeof(double));
449 for (i = m; i > pred_m; i--)
450 { GLPROW *row = mip->row[i];
453 r = dmp_get_atom(tree->pool, sizeof(IOSROW));
454 name = glp_get_row_name(mip, i);
458 { r->name = dmp_get_atom(tree->pool, strlen(name)+1);
459 strcpy(r->name, name);
461 #if 1 /* 20/IX-2008 */
462 r->origin = row->origin;
463 r->klass = row->klass;
465 r->type = (unsigned char)row->type;
469 len = glp_get_mat_row(mip, i, ind, val);
470 for (k = 1; k <= len; k++)
472 a = dmp_get_atom(tree->pool, sizeof(IOSAIJ));
479 r->stat = (unsigned char)row->stat;
480 r->next = node->r_ptr;
486 /* remove all rows missing in the root subproblem */
491 num = xcalloc(1+nrs, sizeof(int));
492 for (i = 1; i <= nrs; i++) num[i] = root_m + i;
493 glp_del_rows(mip, nrs, num);
497 /* and restore attributes of all rows and columns for the root
499 xassert(m == root_m);
500 for (i = 1; i <= m; i++)
501 { glp_set_row_bnds(mip, i, tree->root_type[i],
502 tree->root_lb[i], tree->root_ub[i]);
503 glp_set_row_stat(mip, i, tree->root_stat[i]);
505 for (j = 1; j <= n; j++)
506 { glp_set_col_bnds(mip, j, tree->root_type[m+j],
507 tree->root_lb[m+j], tree->root_ub[m+j]);
508 glp_set_col_stat(mip, j, tree->root_stat[m+j]);
511 /* remove all edges and cliques missing in the conflict graph
512 for the root subproblem */
513 /* (not implemented yet) */
516 /* the current subproblem has been frozen */
521 /***********************************************************************
524 * ios_clone_node - clone specified subproblem
528 * #include "glpios.h"
529 * void ios_clone_node(glp_tree *tree, int p, int nnn, int ref[]);
533 * The routine ios_clone_node clones the specified subproblem, whose
534 * reference number is p, creating its nnn exact copies. Note that the
535 * specified subproblem must be active and must be in the frozen state
536 * (i.e. it must not be the current subproblem).
538 * Each clone, an exact copy of the specified subproblem, becomes a new
539 * active subproblem added to the end of the active list. After cloning
540 * the specified subproblem becomes inactive.
542 * The reference numbers of clone subproblems are stored to locations
543 * ref[1], ..., ref[nnn]. */
545 static int get_slot(glp_tree *tree)
547 /* if no free slots are available, increase the room */
548 if (tree->avail == 0)
549 { int nslots = tree->nslots;
550 IOSLOT *save = tree->slot;
554 { tree->nslots = nslots + nslots;
555 xassert(tree->nslots > nslots);
557 tree->slot = xcalloc(1+tree->nslots, sizeof(IOSLOT));
559 { memcpy(&tree->slot[1], &save[1], nslots * sizeof(IOSLOT));
562 /* push more free slots into the stack */
563 for (p = tree->nslots; p > nslots; p--)
564 { tree->slot[p].node = NULL;
565 tree->slot[p].next = tree->avail;
569 /* pull a free slot from the stack */
571 tree->avail = tree->slot[p].next;
572 xassert(tree->slot[p].node == NULL);
573 tree->slot[p].next = 0;
577 static IOSNPD *new_node(glp_tree *tree, IOSNPD *parent)
580 /* pull a free slot for the new node */
582 /* create descriptor of the new subproblem */
583 node = dmp_get_atom(tree->pool, sizeof(IOSNPD));
584 tree->slot[p].node = node;
587 node->level = (parent == NULL ? 0 : parent->level + 1);
594 node->own_nn = node->own_nc = 0;
597 #if 1 /* 04/X-2008 */
598 node->lp_obj = (parent == NULL ? (tree->mip->dir == GLP_MIN ?
599 -DBL_MAX : +DBL_MAX) : parent->lp_obj);
601 node->bound = (parent == NULL ? (tree->mip->dir == GLP_MIN ?
602 -DBL_MAX : +DBL_MAX) : parent->bound);
607 #if 1 /* 30/XI-2009 */
610 if (tree->parm->cb_size == 0)
613 { node->data = dmp_get_atom(tree->pool, tree->parm->cb_size);
614 memset(node->data, 0, tree->parm->cb_size);
617 node->prev = tree->tail;
619 /* add the new subproblem to the end of the active list */
620 if (tree->head == NULL)
623 tree->tail->next = node;
628 /* increase the number of child subproblems */
636 void ios_clone_node(glp_tree *tree, int p, int nnn, int ref[])
639 /* obtain pointer to the subproblem to be cloned */
640 xassert(1 <= p && p <= tree->nslots);
641 node = tree->slot[p].node;
642 xassert(node != NULL);
643 /* the specified subproblem must be active */
644 xassert(node->count == 0);
645 /* and must be in the frozen state */
646 xassert(tree->curr != node);
647 /* remove the specified subproblem from the active list, because
648 it becomes inactive */
649 if (node->prev == NULL)
650 tree->head = node->next;
652 node->prev->next = node->next;
653 if (node->next == NULL)
654 tree->tail = node->prev;
656 node->next->prev = node->prev;
657 node->prev = node->next = NULL;
659 /* create clone subproblems */
661 for (k = 1; k <= nnn; k++)
662 ref[k] = new_node(tree, node)->p;
666 /***********************************************************************
669 * ios_delete_node - delete specified subproblem
673 * #include "glpios.h"
674 * void ios_delete_node(glp_tree *tree, int p);
678 * The routine ios_delete_node deletes the specified subproblem, whose
679 * reference number is p. The subproblem must be active and must be in
680 * the frozen state (i.e. it must not be the current subproblem).
682 * Note that deletion is performed recursively, i.e. if a subproblem to
683 * be deleted is the only child of its parent, the parent subproblem is
684 * also deleted, etc. */
686 void ios_delete_node(glp_tree *tree, int p)
687 { IOSNPD *node, *temp;
688 /* obtain pointer to the subproblem to be deleted */
689 xassert(1 <= p && p <= tree->nslots);
690 node = tree->slot[p].node;
691 xassert(node != NULL);
692 /* the specified subproblem must be active */
693 xassert(node->count == 0);
694 /* and must be in the frozen state */
695 xassert(tree->curr != node);
696 /* remove the specified subproblem from the active list, because
697 it is gone from the tree */
698 if (node->prev == NULL)
699 tree->head = node->next;
701 node->prev->next = node->next;
702 if (node->next == NULL)
703 tree->tail = node->prev;
705 node->next->prev = node->prev;
706 node->prev = node->next = NULL;
708 loop: /* recursive deletion starts here */
709 /* delete the bound change list */
711 while (node->b_ptr != NULL)
713 node->b_ptr = b->next;
714 dmp_free_atom(tree->pool, b, sizeof(IOSBND));
717 /* delete the status change list */
719 while (node->s_ptr != NULL)
721 node->s_ptr = s->next;
722 dmp_free_atom(tree->pool, s, sizeof(IOSTAT));
725 /* delete the row addition list */
726 while (node->r_ptr != NULL)
730 dmp_free_atom(tree->pool, r->name, strlen(r->name)+1);
731 while (r->ptr != NULL)
735 dmp_free_atom(tree->pool, a, sizeof(IOSAIJ));
737 node->r_ptr = r->next;
738 dmp_free_atom(tree->pool, r, sizeof(IOSROW));
741 /* delete the edge addition list */
742 /* delete the clique addition list */
743 /* (not implemented yet) */
744 xassert(node->own_nn == 0);
745 xassert(node->own_nc == 0);
746 xassert(node->e_ptr == NULL);
748 /* free application-specific data */
749 if (tree->parm->cb_size == 0)
750 xassert(node->data == NULL);
752 dmp_free_atom(tree->pool, node->data, tree->parm->cb_size);
753 /* free the corresponding node slot */
755 xassert(tree->slot[p].node == node);
756 tree->slot[p].node = NULL;
757 tree->slot[p].next = tree->avail;
759 /* save pointer to the parent subproblem */
761 /* delete the subproblem descriptor */
762 dmp_free_atom(tree->pool, node, sizeof(IOSNPD));
764 /* take pointer to the parent subproblem */
767 { /* the parent subproblem exists; decrease the number of its
769 xassert(node->count > 0);
771 /* if now the parent subproblem has no childs, it also must be
773 if (node->count == 0) goto loop;
778 /***********************************************************************
781 * ios_delete_tree - delete branch-and-bound tree
785 * #include "glpios.h"
786 * void ios_delete_tree(glp_tree *tree);
790 * The routine ios_delete_tree deletes the branch-and-bound tree, which
791 * the parameter tree points to, and frees all the memory allocated to
792 * this program object.
794 * On exit components of the problem object are restored to correspond
795 * to the original MIP passed to the routine ios_create_tree. */
797 void ios_delete_tree(glp_tree *tree)
798 { glp_prob *mip = tree->mip;
802 xassert(mip->tree == tree);
803 /* remove all additional rows */
804 if (m != tree->orig_m)
806 nrs = m - tree->orig_m;
808 num = xcalloc(1+nrs, sizeof(int));
809 for (i = 1; i <= nrs; i++) num[i] = tree->orig_m + i;
810 glp_del_rows(mip, nrs, num);
814 /* restore original attributes of rows and columns */
815 xassert(m == tree->orig_m);
816 xassert(n == tree->n);
817 for (i = 1; i <= m; i++)
818 { glp_set_row_bnds(mip, i, tree->orig_type[i],
819 tree->orig_lb[i], tree->orig_ub[i]);
820 glp_set_row_stat(mip, i, tree->orig_stat[i]);
821 mip->row[i]->prim = tree->orig_prim[i];
822 mip->row[i]->dual = tree->orig_dual[i];
824 for (j = 1; j <= n; j++)
825 { glp_set_col_bnds(mip, j, tree->orig_type[m+j],
826 tree->orig_lb[m+j], tree->orig_ub[m+j]);
827 glp_set_col_stat(mip, j, tree->orig_stat[m+j]);
828 mip->col[j]->prim = tree->orig_prim[m+j];
829 mip->col[j]->dual = tree->orig_dual[m+j];
831 mip->pbs_stat = mip->dbs_stat = GLP_FEAS;
832 mip->obj_val = tree->orig_obj;
833 /* delete the branch-and-bound tree */
834 xassert(tree->local != NULL);
835 ios_delete_pool(tree, tree->local);
836 dmp_delete_pool(tree->pool);
837 xfree(tree->orig_type);
838 xfree(tree->orig_lb);
839 xfree(tree->orig_ub);
840 xfree(tree->orig_stat);
841 xfree(tree->orig_prim);
842 xfree(tree->orig_dual);
844 if (tree->root_type != NULL) xfree(tree->root_type);
845 if (tree->root_lb != NULL) xfree(tree->root_lb);
846 if (tree->root_ub != NULL) xfree(tree->root_ub);
847 if (tree->root_stat != NULL) xfree(tree->root_stat);
848 xfree(tree->non_int);
854 if (tree->pcost != NULL) ios_pcost_free(tree);
858 scg_delete_graph(tree->g);
860 if (tree->pred_type != NULL) xfree(tree->pred_type);
861 if (tree->pred_lb != NULL) xfree(tree->pred_lb);
862 if (tree->pred_ub != NULL) xfree(tree->pred_ub);
863 if (tree->pred_stat != NULL) xfree(tree->pred_stat);
865 xassert(tree->cut_gen == NULL);
867 xassert(tree->mir_gen == NULL);
868 xassert(tree->clq_gen == NULL);
874 /***********************************************************************
877 * ios_eval_degrad - estimate obj. degrad. for down- and up-branches
881 * #include "glpios.h"
882 * void ios_eval_degrad(glp_tree *tree, int j, double *dn, double *up);
886 * Given optimal basis to LP relaxation of the current subproblem the
887 * routine ios_eval_degrad performs the dual ratio test to compute the
888 * objective values in the adjacent basis for down- and up-branches,
889 * which are stored in locations *dn and *up, assuming that x[j] is a
890 * variable chosen to branch upon. */
892 void ios_eval_degrad(glp_tree *tree, int j, double *dn, double *up)
893 { glp_prob *mip = tree->mip;
894 int m = mip->m, n = mip->n;
895 int len, kase, k, t, stat;
896 double alfa, beta, gamma, delta, dz;
897 int *ind = tree->iwrk;
898 double *val = tree->dwrk;
899 /* current basis must be optimal */
900 xassert(glp_get_status(mip) == GLP_OPT);
901 /* basis factorization must exist */
902 xassert(glp_bf_exists(mip));
903 /* obtain (fractional) value of x[j] in optimal basic solution
904 to LP relaxation of the current subproblem */
905 xassert(1 <= j && j <= n);
906 beta = mip->col[j]->prim;
907 /* since the value of x[j] is fractional, it is basic; compute
908 corresponding row of the simplex table */
909 len = lpx_eval_tab_row(mip, m+j, ind, val);
910 /* kase < 0 means down-branch; kase > 0 means up-branch */
911 for (kase = -1; kase <= +1; kase += 2)
912 { /* for down-branch we introduce new upper bound floor(beta)
913 for x[j]; similarly, for up-branch we introduce new lower
914 bound ceil(beta) for x[j]; in the current basis this new
915 upper/lower bound is violated, so in the adjacent basis
916 x[j] will leave the basis and go to its new upper/lower
917 bound; we need to know which non-basic variable x[k] should
918 enter the basis to keep dual feasibility */
919 #if 0 /* 23/XI-2009 */
920 k = lpx_dual_ratio_test(mip, len, ind, val, kase, 1e-7);
922 k = lpx_dual_ratio_test(mip, len, ind, val, kase, 1e-9);
924 /* if no variable has been chosen, current basis being primal
925 infeasible due to the new upper/lower bound of x[j] is dual
926 unbounded, therefore, LP relaxation to corresponding branch
927 has no primal feasible solution */
929 { if (mip->dir == GLP_MIN)
935 else if (mip->dir == GLP_MAX)
945 xassert(1 <= k && k <= m+n);
946 /* row of the simplex table corresponding to specified basic
947 variable x[j] is the following:
948 x[j] = ... + alfa * x[k] + ... ;
949 we need to know influence coefficient, alfa, at non-basic
950 variable x[k] chosen with the dual ratio test */
951 for (t = 1; t <= len; t++)
952 if (ind[t] == k) break;
953 xassert(1 <= t && t <= len);
955 /* determine status and reduced cost of variable x[k] */
957 { stat = mip->row[k]->stat;
958 gamma = mip->row[k]->dual;
961 { stat = mip->col[k-m]->stat;
962 gamma = mip->col[k-m]->dual;
964 /* x[k] cannot be basic or fixed non-basic */
965 xassert(stat == GLP_NL || stat == GLP_NU || stat == GLP_NF);
966 /* if the current basis is dual degenerative, some reduced
967 costs, which are close to zero, may have wrong sign due to
968 round-off errors, so correct the sign of gamma */
969 if (mip->dir == GLP_MIN)
970 { if (stat == GLP_NL && gamma < 0.0 ||
971 stat == GLP_NU && gamma > 0.0 ||
972 stat == GLP_NF) gamma = 0.0;
974 else if (mip->dir == GLP_MAX)
975 { if (stat == GLP_NL && gamma > 0.0 ||
976 stat == GLP_NU && gamma < 0.0 ||
977 stat == GLP_NF) gamma = 0.0;
981 /* determine the change of x[j] in the adjacent basis:
982 delta x[j] = new x[j] - old x[j] */
983 delta = (kase < 0 ? floor(beta) : ceil(beta)) - beta;
984 /* compute the change of x[k] in the adjacent basis:
985 delta x[k] = new x[k] - old x[k] = delta x[j] / alfa */
987 /* compute the change of the objective in the adjacent basis:
988 delta z = new z - old z = gamma * delta x[k] */
990 if (mip->dir == GLP_MIN)
992 else if (mip->dir == GLP_MAX)
996 /* compute the new objective value in the adjacent basis:
997 new z = old z + delta z */
999 *dn = mip->obj_val + dz;
1001 *up = mip->obj_val + dz;
1003 /*xprintf("obj = %g; dn = %g; up = %g\n",
1004 mip->obj_val, *dn, *up);*/
1008 /***********************************************************************
1011 * ios_round_bound - improve local bound by rounding
1015 * #include "glpios.h"
1016 * double ios_round_bound(glp_tree *tree, double bound);
1020 * For the given local bound for any integer feasible solution to the
1021 * current subproblem the routine ios_round_bound returns an improved
1022 * local bound for the same integer feasible solution.
1026 * Let the current subproblem has the following objective function:
1028 * z = sum c[j] * x[j] + s >= b, (1)
1031 * where J = {j: c[j] is non-zero and integer, x[j] is integer}, s is
1032 * the sum of terms corresponding to fixed variables, b is an initial
1033 * local bound (minimization).
1035 * From (1) it follows that:
1037 * d * sum (c[j] / d) * x[j] + s >= b, (2)
1042 * sum (c[j] / d) * x[j] >= (b - s) / d = h, (3)
1045 * where d = gcd(c[j]). Since the left-hand side of (3) is integer,
1046 * h = (b - s) / d can be rounded up to the nearest integer:
1048 * h' = ceil(h) = (b' - s) / d, (4)
1050 * that gives an rounded, improved local bound:
1052 * b' = d * h' + s. (5)
1054 * In case of maximization '>=' in (1) should be replaced by '<=' that
1055 * leads to the following formula:
1057 * h' = floor(h) = (b' - s) / d, (6)
1059 * which should used in the same way as (4).
1061 * NOTE: If b is a valid local bound for a child of the current
1062 * subproblem, b' is also valid for that child subproblem. */
1064 double ios_round_bound(glp_tree *tree, double bound)
1065 { glp_prob *mip = tree->mip;
1067 int d, j, nn, *c = tree->iwrk;
1069 /* determine c[j] and compute s */
1070 nn = 0, s = mip->c0, d = 0;
1071 for (j = 1; j <= n; j++)
1072 { GLPCOL *col = mip->col[j];
1073 if (col->coef == 0.0) continue;
1074 if (col->type == GLP_FX)
1075 { /* fixed variable */
1076 s += col->coef * col->prim;
1079 { /* non-fixed variable */
1080 if (col->kind != GLP_IV) goto skip;
1081 if (col->coef != floor(col->coef)) goto skip;
1082 if (fabs(col->coef) <= (double)INT_MAX)
1083 c[++nn] = (int)fabs(col->coef);
1088 /* compute d = gcd(c[1],...c[nn]) */
1090 { if (nn == 0) goto skip;
1094 /* compute new local bound */
1095 if (mip->dir == GLP_MIN)
1096 { if (bound != +DBL_MAX)
1097 { h = (bound - s) / (double)d;
1098 if (h >= floor(h) + 0.001)
1101 /*xprintf("d = %d; old = %g; ", d, bound);*/
1102 bound = (double)d * h + s;
1103 /*xprintf("new = %g\n", bound);*/
1107 else if (mip->dir == GLP_MAX)
1108 { if (bound != -DBL_MAX)
1109 { h = (bound - s) / (double)d;
1110 if (h <= ceil(h) - 0.001)
1113 bound = (double)d * h + s;
1118 xassert(mip != mip);
1122 /***********************************************************************
1125 * ios_is_hopeful - check if subproblem is hopeful
1129 * #include "glpios.h"
1130 * int ios_is_hopeful(glp_tree *tree, double bound);
1134 * Given the local bound of a subproblem the routine ios_is_hopeful
1135 * checks if the subproblem can have an integer optimal solution which
1136 * is better than the best one currently known.
1140 * If the subproblem can have a better integer optimal solution, the
1141 * routine returns non-zero; otherwise, if the corresponding branch can
1142 * be pruned, the routine returns zero. */
1144 int ios_is_hopeful(glp_tree *tree, double bound)
1145 { glp_prob *mip = tree->mip;
1148 if (mip->mip_stat == GLP_FEAS)
1149 { eps = tree->parm->tol_obj * (1.0 + fabs(mip->mip_obj));
1152 if (bound >= mip->mip_obj - eps) ret = 0;
1155 if (bound <= mip->mip_obj + eps) ret = 0;
1158 xassert(mip != mip);
1164 if (bound == +DBL_MAX) ret = 0;
1167 if (bound == -DBL_MAX) ret = 0;
1170 xassert(mip != mip);
1176 /***********************************************************************
1179 * ios_best_node - find active node with best local bound
1183 * #include "glpios.h"
1184 * int ios_best_node(glp_tree *tree);
1188 * The routine ios_best_node finds an active node whose local bound is
1189 * best among other active nodes.
1191 * It is understood that the integer optimal solution of the original
1192 * mip problem cannot be better than the best bound, so the best bound
1193 * is an lower (minimization) or upper (maximization) global bound for
1194 * the original problem.
1198 * The routine ios_best_node returns the subproblem reference number
1199 * 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)
1206 for (node = tree->head; node != NULL; node = node->next)
1207 if (best == NULL || best->bound > node->bound)
1212 for (node = tree->head; node != NULL; node = node->next)
1213 if (best == NULL || best->bound < node->bound)
1217 xassert(tree != tree);
1219 return best == NULL ? 0 : best->p;
1222 /***********************************************************************
1225 * ios_relative_gap - compute relative mip gap
1229 * #include "glpios.h"
1230 * double ios_relative_gap(glp_tree *tree);
1234 * The routine ios_relative_gap computes the relative mip gap using the
1237 * gap = |best_mip - best_bnd| / (|best_mip| + DBL_EPSILON),
1239 * where best_mip is the best integer feasible solution found so far,
1240 * best_bnd is the best (global) bound. If no integer feasible solution
1241 * has been found yet, rel_gap is set to DBL_MAX.
1245 * The routine ios_relative_gap returns the relative mip gap. */
1247 double ios_relative_gap(glp_tree *tree)
1248 { glp_prob *mip = tree->mip;
1250 double best_mip, best_bnd, gap;
1251 if (mip->mip_stat == GLP_FEAS)
1252 { best_mip = mip->mip_obj;
1253 p = ios_best_node(tree);
1255 { /* the tree is empty */
1259 { best_bnd = tree->slot[p].node->bound;
1260 gap = fabs(best_mip - best_bnd) / (fabs(best_mip) +
1265 { /* no integer feasible solution has been found yet */
1271 /***********************************************************************
1274 * ios_solve_node - solve LP relaxation of current subproblem
1278 * #include "glpios.h"
1279 * int ios_solve_node(glp_tree *tree);
1283 * The routine ios_solve_node re-optimizes LP relaxation of the current
1284 * subproblem using the dual simplex method.
1288 * The routine returns the code which is reported by glp_simplex. */
1290 int ios_solve_node(glp_tree *tree)
1291 { glp_prob *mip = tree->mip;
1294 /* the current subproblem must exist */
1295 xassert(tree->curr != NULL);
1296 /* set some control parameters */
1297 glp_init_smcp(&parm);
1298 switch (tree->parm->msg_lev)
1300 parm.msg_lev = GLP_MSG_OFF; break;
1302 parm.msg_lev = GLP_MSG_ERR; break;
1305 parm.msg_lev = GLP_MSG_ON; break;
1307 parm.msg_lev = GLP_MSG_ALL; break;
1309 xassert(tree != tree);
1311 parm.meth = GLP_DUALP;
1312 if (tree->parm->msg_lev < GLP_MSG_DBG)
1313 parm.out_dly = tree->parm->out_dly;
1316 /* if the incumbent objective value is already known, use it to
1317 prematurely terminate the dual simplex search */
1318 if (mip->mip_stat == GLP_FEAS)
1319 { switch (tree->mip->dir)
1321 parm.obj_ul = mip->mip_obj;
1324 parm.obj_ll = mip->mip_obj;
1327 xassert(mip != mip);
1330 /* try to solve/re-optimize the LP relaxation */
1331 ret = glp_simplex(mip, &parm);
1332 tree->curr->solved++;
1334 xprintf("ret = %d; status = %d; pbs = %d; dbs = %d; some = %d\n",
1335 ret, glp_get_status(mip), mip->pbs_stat, mip->dbs_stat,
1337 lpx_print_sol(mip, "sol");
1342 /**********************************************************************/
1344 IOSPOOL *ios_create_pool(glp_tree *tree)
1345 { /* create cut pool */
1348 pool = dmp_get_atom(tree->pool, sizeof(IOSPOOL));
1350 xassert(tree == tree);
1351 pool = xmalloc(sizeof(IOSPOOL));
1354 pool->head = pool->tail = NULL;
1355 pool->ord = 0, pool->curr = NULL;
1359 int ios_add_row(glp_tree *tree, IOSPOOL *pool,
1360 const char *name, int klass, int flags, int len, const int ind[],
1361 const double val[], int type, double rhs)
1362 { /* add row (constraint) to the cut pool */
1366 xassert(pool != NULL);
1367 cut = dmp_get_atom(tree->pool, sizeof(IOSCUT));
1368 if (name == NULL || name[0] == '\0')
1371 { for (k = 0; name[k] != '\0'; k++)
1373 xerror("glp_ios_add_row: cut name too long\n");
1374 if (iscntrl((unsigned char)name[k]))
1375 xerror("glp_ios_add_row: cut name contains invalid chara"
1378 cut->name = dmp_get_atom(tree->pool, strlen(name)+1);
1379 strcpy(cut->name, name);
1381 if (!(0 <= klass && klass <= 255))
1382 xerror("glp_ios_add_row: klass = %d; invalid cut class\n",
1384 cut->klass = (unsigned char)klass;
1386 xerror("glp_ios_add_row: flags = %d; invalid cut flags\n",
1389 if (!(0 <= len && len <= tree->n))
1390 xerror("glp_ios_add_row: len = %d; invalid cut length\n",
1392 for (k = 1; k <= len; k++)
1393 { aij = dmp_get_atom(tree->pool, sizeof(IOSAIJ));
1394 if (!(1 <= ind[k] && ind[k] <= tree->n))
1395 xerror("glp_ios_add_row: ind[%d] = %d; column index out of "
1396 "range\n", k, ind[k]);
1399 aij->next = cut->ptr;
1402 if (!(type == GLP_LO || type == GLP_UP || type == GLP_FX))
1403 xerror("glp_ios_add_row: type = %d; invalid cut type\n",
1405 cut->type = (unsigned char)type;
1407 cut->prev = pool->tail;
1409 if (cut->prev == NULL)
1412 cut->prev->next = cut;
1418 IOSCUT *ios_find_row(IOSPOOL *pool, int i)
1419 { /* find row (constraint) in the cut pool */
1420 /* (smart linear search) */
1421 xassert(pool != NULL);
1422 xassert(1 <= i && i <= pool->size);
1424 { xassert(pool->curr == NULL);
1426 pool->curr = pool->head;
1428 xassert(pool->curr != NULL);
1430 { if (i < pool->ord - i)
1432 pool->curr = pool->head;
1433 while (pool->ord != i)
1435 xassert(pool->curr != NULL);
1436 pool->curr = pool->curr->next;
1440 { while (pool->ord != i)
1442 xassert(pool->curr != NULL);
1443 pool->curr = pool->curr->prev;
1447 else if (i > pool->ord)
1448 { if (i - pool->ord < pool->size - i)
1449 { while (pool->ord != i)
1451 xassert(pool->curr != NULL);
1452 pool->curr = pool->curr->next;
1456 { pool->ord = pool->size;
1457 pool->curr = pool->tail;
1458 while (pool->ord != i)
1460 xassert(pool->curr != NULL);
1461 pool->curr = pool->curr->prev;
1465 xassert(pool->ord == i);
1466 xassert(pool->curr != NULL);
1470 void ios_del_row(glp_tree *tree, IOSPOOL *pool, int i)
1471 { /* remove row (constraint) from the cut pool */
1474 xassert(pool != NULL);
1475 if (!(1 <= i && i <= pool->size))
1476 xerror("glp_ios_del_row: i = %d; cut number out of range\n",
1478 cut = ios_find_row(pool, i);
1479 xassert(pool->curr == cut);
1480 if (cut->next != NULL)
1481 pool->curr = cut->next;
1482 else if (cut->prev != NULL)
1483 pool->ord--, pool->curr = cut->prev;
1485 pool->ord = 0, pool->curr = NULL;
1486 if (cut->name != NULL)
1487 dmp_free_atom(tree->pool, cut->name, strlen(cut->name)+1);
1488 if (cut->prev == NULL)
1489 { xassert(pool->head == cut);
1490 pool->head = cut->next;
1493 { xassert(cut->prev->next == cut);
1494 cut->prev->next = cut->next;
1496 if (cut->next == NULL)
1497 { xassert(pool->tail == cut);
1498 pool->tail = cut->prev;
1501 { xassert(cut->next->prev == cut);
1502 cut->next->prev = cut->prev;
1504 while (cut->ptr != NULL)
1506 cut->ptr = aij->next;
1507 dmp_free_atom(tree->pool, aij, sizeof(IOSAIJ));
1509 dmp_free_atom(tree->pool, cut, sizeof(IOSCUT));
1514 void ios_clear_pool(glp_tree *tree, IOSPOOL *pool)
1515 { /* remove all rows (constraints) from the cut pool */
1516 xassert(pool != NULL);
1517 while (pool->head != NULL)
1518 { IOSCUT *cut = pool->head;
1519 pool->head = cut->next;
1520 if (cut->name != NULL)
1521 dmp_free_atom(tree->pool, cut->name, strlen(cut->name)+1);
1522 while (cut->ptr != NULL)
1523 { IOSAIJ *aij = cut->ptr;
1524 cut->ptr = aij->next;
1525 dmp_free_atom(tree->pool, aij, sizeof(IOSAIJ));
1527 dmp_free_atom(tree->pool, cut, sizeof(IOSCUT));
1530 pool->head = pool->tail = NULL;
1531 pool->ord = 0, pool->curr = NULL;
1535 void ios_delete_pool(glp_tree *tree, IOSPOOL *pool)
1536 { /* delete cut pool */
1537 xassert(pool != NULL);
1538 ios_clear_pool(tree, pool);
1543 /**********************************************************************/
1546 static int refer_to_node(glp_tree *tree, int j)
1547 { /* determine node number corresponding to binary variable x[j] or
1549 glp_prob *mip = tree->mip;
1555 ref = tree->c_ref, j = - j;
1556 xassert(1 <= j && j <= n);
1558 { /* new node is needed */
1560 int n_max = g->n_max;
1561 ref[j] = scg_add_nodes(g, 1);
1562 if (g->n_max > n_max)
1563 { int *save = tree->j_ref;
1564 tree->j_ref = xcalloc(1+g->n_max, sizeof(int));
1565 memcpy(&tree->j_ref[1], &save[1], g->n * sizeof(int));
1568 xassert(ref[j] == g->n);
1569 tree->j_ref[ref[j]] = j;
1570 xassert(tree->curr != NULL);
1571 if (tree->curr->level > 0) tree->curr->own_nn++;
1578 void ios_add_edge(glp_tree *tree, int j1, int j2)
1579 { /* add new edge to the conflict graph */
1580 glp_prob *mip = tree->mip;
1584 xassert(-n <= j1 && j1 <= +n && j1 != 0);
1585 xassert(-n <= j2 && j2 <= +n && j2 != 0);
1587 /* determine number of the first node, which was added for the
1588 current subproblem */
1589 xassert(tree->curr != NULL);
1590 first = tree->g->n - tree->curr->own_nn + 1;
1591 /* determine node numbers for both endpoints */
1592 i1 = refer_to_node(tree, j1);
1593 i2 = refer_to_node(tree, j2);
1594 /* add edge (i1,i2) to the conflict graph */
1595 e = scg_add_edge(tree->g, i1, i2);
1596 /* if the current subproblem is not the root and both endpoints
1597 were created on some previous levels, save the edge */
1598 if (tree->curr->level > 0 && i1 < first && i2 < first)
1600 rib = dmp_get_atom(tree->pool, sizeof(IOSRIB));
1604 rib->next = tree->curr->e_ptr;
1605 tree->curr->e_ptr = rib;