alpar@9: /* glpapi13.c (branch-and-bound interface routines) */ alpar@9: alpar@9: /*********************************************************************** alpar@9: * This code is part of GLPK (GNU Linear Programming Kit). alpar@9: * alpar@9: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@9: * 2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics, alpar@9: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@9: * E-mail: . alpar@9: * alpar@9: * GLPK is free software: you can redistribute it and/or modify it alpar@9: * under the terms of the GNU General Public License as published by alpar@9: * the Free Software Foundation, either version 3 of the License, or alpar@9: * (at your option) any later version. alpar@9: * alpar@9: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@9: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@9: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@9: * License for more details. alpar@9: * alpar@9: * You should have received a copy of the GNU General Public License alpar@9: * along with GLPK. If not, see . alpar@9: ***********************************************************************/ alpar@9: alpar@9: #include "glpios.h" alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_reason - determine reason for calling the callback routine alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * glp_ios_reason(glp_tree *tree); alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The routine glp_ios_reason returns a code, which indicates why the alpar@9: * user-defined callback routine is being called. */ alpar@9: alpar@9: int glp_ios_reason(glp_tree *tree) alpar@9: { return alpar@9: tree->reason; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_get_prob - access the problem object alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * glp_prob *glp_ios_get_prob(glp_tree *tree); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_ios_get_prob can be called from the user-defined alpar@9: * callback routine to access the problem object, which is used by the alpar@9: * MIP solver. It is the original problem object passed to the routine alpar@9: * glp_intopt if the MIP presolver is not used; otherwise it is an alpar@9: * internal problem object built by the presolver. If the current alpar@9: * subproblem exists, LP segment of the problem object corresponds to alpar@9: * its LP relaxation. alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The routine glp_ios_get_prob returns a pointer to the problem object alpar@9: * used by the MIP solver. */ alpar@9: alpar@9: glp_prob *glp_ios_get_prob(glp_tree *tree) alpar@9: { return alpar@9: tree->mip; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_tree_size - determine size of the branch-and-bound tree alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_ios_tree_size(glp_tree *tree, int *a_cnt, int *n_cnt, alpar@9: * int *t_cnt); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_ios_tree_size stores the following three counts which alpar@9: * characterize the current size of the branch-and-bound tree: alpar@9: * alpar@9: * a_cnt is the current number of active nodes, i.e. the current size of alpar@9: * the active list; alpar@9: * alpar@9: * n_cnt is the current number of all (active and inactive) nodes; alpar@9: * alpar@9: * t_cnt is the total number of nodes including those which have been alpar@9: * already removed from the tree. This count is increased whenever alpar@9: * a new node appears in the tree and never decreased. alpar@9: * alpar@9: * If some of the parameters a_cnt, n_cnt, t_cnt is a null pointer, the alpar@9: * corresponding count is not stored. */ alpar@9: alpar@9: void glp_ios_tree_size(glp_tree *tree, int *a_cnt, int *n_cnt, alpar@9: int *t_cnt) alpar@9: { if (a_cnt != NULL) *a_cnt = tree->a_cnt; alpar@9: if (n_cnt != NULL) *n_cnt = tree->n_cnt; alpar@9: if (t_cnt != NULL) *t_cnt = tree->t_cnt; alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_curr_node - determine current active subproblem alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_ios_curr_node(glp_tree *tree); alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The routine glp_ios_curr_node returns the reference number of the alpar@9: * current active subproblem. However, if the current subproblem does alpar@9: * not exist, the routine returns zero. */ alpar@9: alpar@9: int glp_ios_curr_node(glp_tree *tree) alpar@9: { IOSNPD *node; alpar@9: /* obtain pointer to the current subproblem */ alpar@9: node = tree->curr; alpar@9: /* return its reference number */ alpar@9: return node == NULL ? 0 : node->p; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_next_node - determine next active subproblem alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_ios_next_node(glp_tree *tree, int p); alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * If the parameter p is zero, the routine glp_ios_next_node returns alpar@9: * the reference number of the first active subproblem. However, if the alpar@9: * tree is empty, zero is returned. alpar@9: * alpar@9: * If the parameter p is not zero, it must specify the reference number alpar@9: * of some active subproblem, in which case the routine returns the alpar@9: * reference number of the next active subproblem. However, if there is alpar@9: * no next active subproblem in the list, zero is returned. alpar@9: * alpar@9: * All subproblems in the active list are ordered chronologically, i.e. alpar@9: * subproblem A precedes subproblem B if A was created before B. */ alpar@9: alpar@9: int glp_ios_next_node(glp_tree *tree, int p) alpar@9: { IOSNPD *node; alpar@9: if (p == 0) alpar@9: { /* obtain pointer to the first active subproblem */ alpar@9: node = tree->head; alpar@9: } alpar@9: else alpar@9: { /* obtain pointer to the specified subproblem */ alpar@9: if (!(1 <= p && p <= tree->nslots)) alpar@9: err: xerror("glp_ios_next_node: p = %d; invalid subproblem refer" alpar@9: "ence number\n", p); alpar@9: node = tree->slot[p].node; alpar@9: if (node == NULL) goto err; alpar@9: /* the specified subproblem must be active */ alpar@9: if (node->count != 0) alpar@9: xerror("glp_ios_next_node: p = %d; subproblem not in the ac" alpar@9: "tive list\n", p); alpar@9: /* obtain pointer to the next active subproblem */ alpar@9: node = node->next; alpar@9: } alpar@9: /* return the reference number */ alpar@9: return node == NULL ? 0 : node->p; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_prev_node - determine previous active subproblem alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_ios_prev_node(glp_tree *tree, int p); alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * If the parameter p is zero, the routine glp_ios_prev_node returns alpar@9: * the reference number of the last active subproblem. However, if the alpar@9: * tree is empty, zero is returned. alpar@9: * alpar@9: * If the parameter p is not zero, it must specify the reference number alpar@9: * of some active subproblem, in which case the routine returns the alpar@9: * reference number of the previous active subproblem. However, if there alpar@9: * is no previous active subproblem in the list, zero is returned. alpar@9: * alpar@9: * All subproblems in the active list are ordered chronologically, i.e. alpar@9: * subproblem A precedes subproblem B if A was created before B. */ alpar@9: alpar@9: int glp_ios_prev_node(glp_tree *tree, int p) alpar@9: { IOSNPD *node; alpar@9: if (p == 0) alpar@9: { /* obtain pointer to the last active subproblem */ alpar@9: node = tree->tail; alpar@9: } alpar@9: else alpar@9: { /* obtain pointer to the specified subproblem */ alpar@9: if (!(1 <= p && p <= tree->nslots)) alpar@9: err: xerror("glp_ios_prev_node: p = %d; invalid subproblem refer" alpar@9: "ence number\n", p); alpar@9: node = tree->slot[p].node; alpar@9: if (node == NULL) goto err; alpar@9: /* the specified subproblem must be active */ alpar@9: if (node->count != 0) alpar@9: xerror("glp_ios_prev_node: p = %d; subproblem not in the ac" alpar@9: "tive list\n", p); alpar@9: /* obtain pointer to the previous active subproblem */ alpar@9: node = node->prev; alpar@9: } alpar@9: /* return the reference number */ alpar@9: return node == NULL ? 0 : node->p; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_up_node - determine parent subproblem alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_ios_up_node(glp_tree *tree, int p); alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The parameter p must specify the reference number of some (active or alpar@9: * inactive) subproblem, in which case the routine iet_get_up_node alpar@9: * returns the reference number of its parent subproblem. However, if alpar@9: * the specified subproblem is the root of the tree and, therefore, has alpar@9: * no parent, the routine returns zero. */ alpar@9: alpar@9: int glp_ios_up_node(glp_tree *tree, int p) alpar@9: { IOSNPD *node; alpar@9: /* obtain pointer to the specified subproblem */ alpar@9: if (!(1 <= p && p <= tree->nslots)) alpar@9: err: xerror("glp_ios_up_node: p = %d; invalid subproblem reference " alpar@9: "number\n", p); alpar@9: node = tree->slot[p].node; alpar@9: if (node == NULL) goto err; alpar@9: /* obtain pointer to the parent subproblem */ alpar@9: node = node->up; alpar@9: /* return the reference number */ alpar@9: return node == NULL ? 0 : node->p; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_node_level - determine subproblem level alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_ios_node_level(glp_tree *tree, int p); alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The routine glp_ios_node_level returns the level of the subproblem, alpar@9: * whose reference number is p, in the branch-and-bound tree. (The root alpar@9: * subproblem has level 0, and the level of any other subproblem is the alpar@9: * level of its parent plus one.) */ alpar@9: alpar@9: int glp_ios_node_level(glp_tree *tree, int p) alpar@9: { IOSNPD *node; alpar@9: /* obtain pointer to the specified subproblem */ alpar@9: if (!(1 <= p && p <= tree->nslots)) alpar@9: err: xerror("glp_ios_node_level: p = %d; invalid subproblem referen" alpar@9: "ce number\n", p); alpar@9: node = tree->slot[p].node; alpar@9: if (node == NULL) goto err; alpar@9: /* return the node level */ alpar@9: return node->level; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_node_bound - determine subproblem local bound alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * double glp_ios_node_bound(glp_tree *tree, int p); alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The routine glp_ios_node_bound returns the local bound for (active or alpar@9: * inactive) subproblem, whose reference number is p. alpar@9: * alpar@9: * COMMENTS alpar@9: * alpar@9: * The local bound for subproblem p is an lower (minimization) or upper alpar@9: * (maximization) bound for integer optimal solution to this subproblem alpar@9: * (not to the original problem). This bound is local in the sense that alpar@9: * only subproblems in the subtree rooted at node p cannot have better alpar@9: * integer feasible solutions. alpar@9: * alpar@9: * On creating a subproblem (due to the branching step) its local bound alpar@9: * is inherited from its parent and then may get only stronger (never alpar@9: * weaker). For the root subproblem its local bound is initially set to alpar@9: * -DBL_MAX (minimization) or +DBL_MAX (maximization) and then improved alpar@9: * as the root LP relaxation has been solved. alpar@9: * alpar@9: * Note that the local bound is not necessarily the optimal objective alpar@9: * value to corresponding LP relaxation; it may be stronger. */ alpar@9: alpar@9: double glp_ios_node_bound(glp_tree *tree, int p) alpar@9: { IOSNPD *node; alpar@9: /* obtain pointer to the specified subproblem */ alpar@9: if (!(1 <= p && p <= tree->nslots)) alpar@9: err: xerror("glp_ios_node_bound: p = %d; invalid subproblem referen" alpar@9: "ce number\n", p); alpar@9: node = tree->slot[p].node; alpar@9: if (node == NULL) goto err; alpar@9: /* return the node local bound */ alpar@9: return node->bound; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_best_node - find active subproblem with best local bound alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_ios_best_node(glp_tree *tree); alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The routine glp_ios_best_node returns the reference number of the alpar@9: * active subproblem, whose local bound is best (i.e. smallest in case alpar@9: * of minimization or largest in case of maximization). However, if the alpar@9: * tree is empty, the routine returns zero. alpar@9: * alpar@9: * COMMENTS alpar@9: * alpar@9: * The best local bound is an lower (minimization) or upper alpar@9: * (maximization) bound for integer optimal solution to the original alpar@9: * MIP problem. */ alpar@9: alpar@9: int glp_ios_best_node(glp_tree *tree) alpar@9: { return alpar@9: ios_best_node(tree); alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_mip_gap - compute relative MIP gap alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * double glp_ios_mip_gap(glp_tree *tree); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_ios_mip_gap computes the relative MIP gap with the alpar@9: * following formula: alpar@9: * alpar@9: * gap = |best_mip - best_bnd| / (|best_mip| + DBL_EPSILON), alpar@9: * alpar@9: * where best_mip is the best integer feasible solution found so far, alpar@9: * best_bnd is the best (global) bound. If no integer feasible solution alpar@9: * has been found yet, gap is set to DBL_MAX. alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The routine glp_ios_mip_gap returns the relative MIP gap. */ alpar@9: alpar@9: double glp_ios_mip_gap(glp_tree *tree) alpar@9: { return alpar@9: ios_relative_gap(tree); alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_node_data - access subproblem application-specific data alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void *glp_ios_node_data(glp_tree *tree, int p); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_ios_node_data allows the application accessing a alpar@9: * memory block allocated for the subproblem (which may be active or alpar@9: * inactive), whose reference number is p. alpar@9: * alpar@9: * The size of the block is defined by the control parameter cb_size alpar@9: * passed to the routine glp_intopt. The block is initialized by binary alpar@9: * zeros on creating corresponding subproblem, and its contents is kept alpar@9: * until the subproblem will be removed from the tree. alpar@9: * alpar@9: * The application may use these memory blocks to store specific data alpar@9: * for each subproblem. alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The routine glp_ios_node_data returns a pointer to the memory block alpar@9: * for the specified subproblem. Note that if cb_size = 0, the routine alpar@9: * returns a null pointer. */ alpar@9: alpar@9: void *glp_ios_node_data(glp_tree *tree, int p) alpar@9: { IOSNPD *node; alpar@9: /* obtain pointer to the specified subproblem */ alpar@9: if (!(1 <= p && p <= tree->nslots)) alpar@9: err: xerror("glp_ios_node_level: p = %d; invalid subproblem referen" alpar@9: "ce number\n", p); alpar@9: node = tree->slot[p].node; alpar@9: if (node == NULL) goto err; alpar@9: /* return pointer to the application-specific data */ alpar@9: return node->data; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_row_attr - retrieve additional row attributes alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_ios_row_attr(glp_tree *tree, int i, glp_attr *attr); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_ios_row_attr retrieves additional attributes of row alpar@9: * i and stores them in the structure glp_attr. */ alpar@9: alpar@9: void glp_ios_row_attr(glp_tree *tree, int i, glp_attr *attr) alpar@9: { GLPROW *row; alpar@9: if (!(1 <= i && i <= tree->mip->m)) alpar@9: xerror("glp_ios_row_attr: i = %d; row number out of range\n", alpar@9: i); alpar@9: row = tree->mip->row[i]; alpar@9: attr->level = row->level; alpar@9: attr->origin = row->origin; alpar@9: attr->klass = row->klass; alpar@9: return; alpar@9: } alpar@9: alpar@9: /**********************************************************************/ alpar@9: alpar@9: int glp_ios_pool_size(glp_tree *tree) alpar@9: { /* determine current size of the cut pool */ alpar@9: if (tree->reason != GLP_ICUTGEN) alpar@9: xerror("glp_ios_pool_size: operation not allowed\n"); alpar@9: xassert(tree->local != NULL); alpar@9: return tree->local->size; alpar@9: } alpar@9: alpar@9: /**********************************************************************/ alpar@9: alpar@9: int glp_ios_add_row(glp_tree *tree, alpar@9: const char *name, int klass, int flags, int len, const int ind[], alpar@9: const double val[], int type, double rhs) alpar@9: { /* add row (constraint) to the cut pool */ alpar@9: int num; alpar@9: if (tree->reason != GLP_ICUTGEN) alpar@9: xerror("glp_ios_add_row: operation not allowed\n"); alpar@9: xassert(tree->local != NULL); alpar@9: num = ios_add_row(tree, tree->local, name, klass, flags, len, alpar@9: ind, val, type, rhs); alpar@9: return num; alpar@9: } alpar@9: alpar@9: /**********************************************************************/ alpar@9: alpar@9: void glp_ios_del_row(glp_tree *tree, int i) alpar@9: { /* remove row (constraint) from the cut pool */ alpar@9: if (tree->reason != GLP_ICUTGEN) alpar@9: xerror("glp_ios_del_row: operation not allowed\n"); alpar@9: ios_del_row(tree, tree->local, i); alpar@9: return; alpar@9: } alpar@9: alpar@9: /**********************************************************************/ alpar@9: alpar@9: void glp_ios_clear_pool(glp_tree *tree) alpar@9: { /* remove all rows (constraints) from the cut pool */ alpar@9: if (tree->reason != GLP_ICUTGEN) alpar@9: xerror("glp_ios_clear_pool: operation not allowed\n"); alpar@9: ios_clear_pool(tree, tree->local); alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_can_branch - check if can branch upon specified variable alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_ios_can_branch(glp_tree *tree, int j); alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * If j-th variable (column) can be used to branch upon, the routine alpar@9: * glp_ios_can_branch returns non-zero, otherwise zero. */ alpar@9: alpar@9: int glp_ios_can_branch(glp_tree *tree, int j) alpar@9: { if (!(1 <= j && j <= tree->mip->n)) alpar@9: xerror("glp_ios_can_branch: j = %d; column number out of range" alpar@9: "\n", j); alpar@9: return tree->non_int[j]; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_branch_upon - choose variable to branch upon alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_ios_branch_upon(glp_tree *tree, int j, int sel); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_ios_branch_upon can be called from the user-defined alpar@9: * callback routine in response to the reason GLP_IBRANCH to choose a alpar@9: * branching variable, whose ordinal number is j. Should note that only alpar@9: * variables, for which the routine glp_ios_can_branch returns non-zero, alpar@9: * can be used to branch upon. alpar@9: * alpar@9: * The parameter sel is a flag that indicates which branch (subproblem) alpar@9: * should be selected next to continue the search: alpar@9: * alpar@9: * GLP_DN_BRNCH - select down-branch; alpar@9: * GLP_UP_BRNCH - select up-branch; alpar@9: * GLP_NO_BRNCH - use general selection technique. */ alpar@9: alpar@9: void glp_ios_branch_upon(glp_tree *tree, int j, int sel) alpar@9: { if (!(1 <= j && j <= tree->mip->n)) alpar@9: xerror("glp_ios_branch_upon: j = %d; column number out of rang" alpar@9: "e\n", j); alpar@9: if (!(sel == GLP_DN_BRNCH || sel == GLP_UP_BRNCH || alpar@9: sel == GLP_NO_BRNCH)) alpar@9: xerror("glp_ios_branch_upon: sel = %d: invalid branch selectio" alpar@9: "n flag\n", sel); alpar@9: if (!(tree->non_int[j])) alpar@9: xerror("glp_ios_branch_upon: j = %d; variable cannot be used t" alpar@9: "o branch upon\n", j); alpar@9: if (tree->br_var != 0) alpar@9: xerror("glp_ios_branch_upon: branching variable already chosen" alpar@9: "\n"); alpar@9: tree->br_var = j; alpar@9: tree->br_sel = sel; alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_select_node - select subproblem to continue the search alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_ios_select_node(glp_tree *tree, int p); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_ios_select_node can be called from the user-defined alpar@9: * callback routine in response to the reason GLP_ISELECT to select an alpar@9: * active subproblem, whose reference number is p. The search will be alpar@9: * continued from the subproblem selected. */ alpar@9: alpar@9: void glp_ios_select_node(glp_tree *tree, int p) alpar@9: { IOSNPD *node; alpar@9: /* obtain pointer to the specified subproblem */ alpar@9: if (!(1 <= p && p <= tree->nslots)) alpar@9: err: xerror("glp_ios_select_node: p = %d; invalid subproblem refere" alpar@9: "nce number\n", p); alpar@9: node = tree->slot[p].node; alpar@9: if (node == NULL) goto err; alpar@9: /* the specified subproblem must be active */ alpar@9: if (node->count != 0) alpar@9: xerror("glp_ios_select_node: p = %d; subproblem not in the act" alpar@9: "ive list\n", p); alpar@9: /* no subproblem must be selected yet */ alpar@9: if (tree->next_p != 0) alpar@9: xerror("glp_ios_select_node: subproblem already selected\n"); alpar@9: /* select the specified subproblem to continue the search */ alpar@9: tree->next_p = p; alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_heur_sol - provide solution found by heuristic alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_ios_heur_sol(glp_tree *tree, const double x[]); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_ios_heur_sol can be called from the user-defined alpar@9: * callback routine in response to the reason GLP_IHEUR to provide an alpar@9: * integer feasible solution found by a primal heuristic. alpar@9: * alpar@9: * Primal values of *all* variables (columns) found by the heuristic alpar@9: * should be placed in locations x[1], ..., x[n], where n is the number alpar@9: * of columns in the original problem object. Note that the routine alpar@9: * glp_ios_heur_sol *does not* check primal feasibility of the solution alpar@9: * provided. alpar@9: * alpar@9: * Using the solution passed in the array x the routine computes value alpar@9: * of the objective function. If the objective value is better than the alpar@9: * best known integer feasible solution, the routine computes values of alpar@9: * auxiliary variables (rows) and stores all solution components in the alpar@9: * problem object. alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * If the provided solution is accepted, the routine glp_ios_heur_sol alpar@9: * returns zero. Otherwise, if the provided solution is rejected, the alpar@9: * routine returns non-zero. */ alpar@9: alpar@9: int glp_ios_heur_sol(glp_tree *tree, const double x[]) alpar@9: { glp_prob *mip = tree->mip; alpar@9: int m = tree->orig_m; alpar@9: int n = tree->n; alpar@9: int i, j; alpar@9: double obj; alpar@9: xassert(mip->m >= m); alpar@9: xassert(mip->n == n); alpar@9: /* check values of integer variables and compute value of the alpar@9: objective function */ alpar@9: obj = mip->c0; alpar@9: for (j = 1; j <= n; j++) alpar@9: { GLPCOL *col = mip->col[j]; alpar@9: if (col->kind == GLP_IV) alpar@9: { /* provided value must be integral */ alpar@9: if (x[j] != floor(x[j])) return 1; alpar@9: } alpar@9: obj += col->coef * x[j]; alpar@9: } alpar@9: /* check if the provided solution is better than the best known alpar@9: integer feasible solution */ alpar@9: if (mip->mip_stat == GLP_FEAS) alpar@9: { switch (mip->dir) alpar@9: { case GLP_MIN: alpar@9: if (obj >= tree->mip->mip_obj) return 1; alpar@9: break; alpar@9: case GLP_MAX: alpar@9: if (obj <= tree->mip->mip_obj) return 1; alpar@9: break; alpar@9: default: alpar@9: xassert(mip != mip); alpar@9: } alpar@9: } alpar@9: /* it is better; store it in the problem object */ alpar@9: if (tree->parm->msg_lev >= GLP_MSG_ON) alpar@9: xprintf("Solution found by heuristic: %.12g\n", obj); alpar@9: mip->mip_stat = GLP_FEAS; alpar@9: mip->mip_obj = obj; alpar@9: for (j = 1; j <= n; j++) alpar@9: mip->col[j]->mipx = x[j]; alpar@9: for (i = 1; i <= m; i++) alpar@9: { GLPROW *row = mip->row[i]; alpar@9: GLPAIJ *aij; alpar@9: row->mipx = 0.0; alpar@9: for (aij = row->ptr; aij != NULL; aij = aij->r_next) alpar@9: row->mipx += aij->val * aij->col->mipx; alpar@9: } alpar@9: return 0; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_ios_terminate - terminate the solution process. alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_ios_terminate(glp_tree *tree); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_ios_terminate sets a flag indicating that the MIP alpar@9: * solver should prematurely terminate the search. */ alpar@9: alpar@9: void glp_ios_terminate(glp_tree *tree) alpar@9: { if (tree->parm->msg_lev >= GLP_MSG_DBG) alpar@9: xprintf("The search is prematurely terminated due to applicati" alpar@9: "on request\n"); alpar@9: tree->stop = 1; alpar@9: return; alpar@9: } alpar@9: alpar@9: /* eof */