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