alpar@9: /* glpios08.c (clique cut generator) */ 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: static double get_row_lb(LPX *lp, int i) alpar@9: { /* this routine returns lower bound of row i or -DBL_MAX if the alpar@9: row has no lower bound */ alpar@9: double lb; alpar@9: switch (lpx_get_row_type(lp, i)) alpar@9: { case LPX_FR: alpar@9: case LPX_UP: alpar@9: lb = -DBL_MAX; alpar@9: break; alpar@9: case LPX_LO: alpar@9: case LPX_DB: alpar@9: case LPX_FX: alpar@9: lb = lpx_get_row_lb(lp, i); alpar@9: break; alpar@9: default: alpar@9: xassert(lp != lp); alpar@9: } alpar@9: return lb; alpar@9: } alpar@9: alpar@9: static double get_row_ub(LPX *lp, int i) alpar@9: { /* this routine returns upper bound of row i or +DBL_MAX if the alpar@9: row has no upper bound */ alpar@9: double ub; alpar@9: switch (lpx_get_row_type(lp, i)) alpar@9: { case LPX_FR: alpar@9: case LPX_LO: alpar@9: ub = +DBL_MAX; alpar@9: break; alpar@9: case LPX_UP: alpar@9: case LPX_DB: alpar@9: case LPX_FX: alpar@9: ub = lpx_get_row_ub(lp, i); alpar@9: break; alpar@9: default: alpar@9: xassert(lp != lp); alpar@9: } alpar@9: return ub; alpar@9: } alpar@9: alpar@9: static double get_col_lb(LPX *lp, int j) alpar@9: { /* this routine returns lower bound of column j or -DBL_MAX if alpar@9: the column has no lower bound */ alpar@9: double lb; alpar@9: switch (lpx_get_col_type(lp, j)) alpar@9: { case LPX_FR: alpar@9: case LPX_UP: alpar@9: lb = -DBL_MAX; alpar@9: break; alpar@9: case LPX_LO: alpar@9: case LPX_DB: alpar@9: case LPX_FX: alpar@9: lb = lpx_get_col_lb(lp, j); alpar@9: break; alpar@9: default: alpar@9: xassert(lp != lp); alpar@9: } alpar@9: return lb; alpar@9: } alpar@9: alpar@9: static double get_col_ub(LPX *lp, int j) alpar@9: { /* this routine returns upper bound of column j or +DBL_MAX if alpar@9: the column has no upper bound */ alpar@9: double ub; alpar@9: switch (lpx_get_col_type(lp, j)) alpar@9: { case LPX_FR: alpar@9: case LPX_LO: alpar@9: ub = +DBL_MAX; alpar@9: break; alpar@9: case LPX_UP: alpar@9: case LPX_DB: alpar@9: case LPX_FX: alpar@9: ub = lpx_get_col_ub(lp, j); alpar@9: break; alpar@9: default: alpar@9: xassert(lp != lp); alpar@9: } alpar@9: return ub; alpar@9: } alpar@9: alpar@9: static int is_binary(LPX *lp, int j) alpar@9: { /* this routine checks if variable x[j] is binary */ alpar@9: return alpar@9: lpx_get_col_kind(lp, j) == LPX_IV && alpar@9: lpx_get_col_type(lp, j) == LPX_DB && alpar@9: lpx_get_col_lb(lp, j) == 0.0 && lpx_get_col_ub(lp, j) == 1.0; alpar@9: } alpar@9: alpar@9: static double eval_lf_min(LPX *lp, int len, int ind[], double val[]) alpar@9: { /* this routine computes the minimum of a specified linear form alpar@9: alpar@9: sum a[j]*x[j] alpar@9: j alpar@9: alpar@9: using the formula: alpar@9: alpar@9: min = sum a[j]*lb[j] + sum a[j]*ub[j], alpar@9: j in J+ j in J- alpar@9: alpar@9: where J+ = {j: a[j] > 0}, J- = {j: a[j] < 0}, lb[j] and ub[j] alpar@9: are lower and upper bound of variable x[j], resp. */ alpar@9: int j, t; alpar@9: double lb, ub, sum; alpar@9: sum = 0.0; alpar@9: for (t = 1; t <= len; t++) alpar@9: { j = ind[t]; alpar@9: if (val[t] > 0.0) alpar@9: { lb = get_col_lb(lp, j); alpar@9: if (lb == -DBL_MAX) alpar@9: { sum = -DBL_MAX; alpar@9: break; alpar@9: } alpar@9: sum += val[t] * lb; alpar@9: } alpar@9: else if (val[t] < 0.0) alpar@9: { ub = get_col_ub(lp, j); alpar@9: if (ub == +DBL_MAX) alpar@9: { sum = -DBL_MAX; alpar@9: break; alpar@9: } alpar@9: sum += val[t] * ub; alpar@9: } alpar@9: else alpar@9: xassert(val != val); alpar@9: } alpar@9: return sum; alpar@9: } alpar@9: alpar@9: static double eval_lf_max(LPX *lp, int len, int ind[], double val[]) alpar@9: { /* this routine computes the maximum of a specified linear form alpar@9: alpar@9: sum a[j]*x[j] alpar@9: j alpar@9: alpar@9: using the formula: alpar@9: alpar@9: max = sum a[j]*ub[j] + sum a[j]*lb[j], alpar@9: j in J+ j in J- alpar@9: alpar@9: where J+ = {j: a[j] > 0}, J- = {j: a[j] < 0}, lb[j] and ub[j] alpar@9: are lower and upper bound of variable x[j], resp. */ alpar@9: int j, t; alpar@9: double lb, ub, sum; alpar@9: sum = 0.0; alpar@9: for (t = 1; t <= len; t++) alpar@9: { j = ind[t]; alpar@9: if (val[t] > 0.0) alpar@9: { ub = get_col_ub(lp, j); alpar@9: if (ub == +DBL_MAX) alpar@9: { sum = +DBL_MAX; alpar@9: break; alpar@9: } alpar@9: sum += val[t] * ub; alpar@9: } alpar@9: else if (val[t] < 0.0) alpar@9: { lb = get_col_lb(lp, j); alpar@9: if (lb == -DBL_MAX) alpar@9: { sum = +DBL_MAX; alpar@9: break; alpar@9: } alpar@9: sum += val[t] * lb; alpar@9: } alpar@9: else alpar@9: xassert(val != val); alpar@9: } alpar@9: return sum; alpar@9: } alpar@9: alpar@9: /*---------------------------------------------------------------------- alpar@9: -- probing - determine logical relation between binary variables. alpar@9: -- alpar@9: -- This routine tentatively sets a binary variable to 0 and then to 1 alpar@9: -- and examines whether another binary variable is caused to be fixed. alpar@9: -- alpar@9: -- The examination is based only on one row (constraint), which is the alpar@9: -- following: alpar@9: -- alpar@9: -- L <= sum a[j]*x[j] <= U. (1) alpar@9: -- j alpar@9: -- alpar@9: -- Let x[p] be a probing variable, x[q] be an examined variable. Then alpar@9: -- (1) can be written as: alpar@9: -- alpar@9: -- L <= sum a[j]*x[j] + a[p]*x[p] + a[q]*x[q] <= U, (2) alpar@9: -- j in J' alpar@9: -- alpar@9: -- where J' = {j: j != p and j != q}. alpar@9: -- alpar@9: -- Let alpar@9: -- alpar@9: -- L' = L - a[p]*x[p], (3) alpar@9: -- alpar@9: -- U' = U - a[p]*x[p], (4) alpar@9: -- alpar@9: -- where x[p] is assumed to be fixed at 0 or 1. So (2) can be rewritten alpar@9: -- as follows: alpar@9: -- alpar@9: -- L' <= sum a[j]*x[j] + a[q]*x[q] <= U', (5) alpar@9: -- j in J' alpar@9: -- alpar@9: -- from where we have: alpar@9: -- alpar@9: -- L' - sum a[j]*x[j] <= a[q]*x[q] <= U' - sum a[j]*x[j]. (6) alpar@9: -- j in J' j in J' alpar@9: -- alpar@9: -- Thus, alpar@9: -- alpar@9: -- min a[q]*x[q] = L' - MAX, (7) alpar@9: -- alpar@9: -- max a[q]*x[q] = U' - MIN, (8) alpar@9: -- alpar@9: -- where alpar@9: -- alpar@9: -- MIN = min sum a[j]*x[j], (9) alpar@9: -- j in J' alpar@9: -- alpar@9: -- MAX = max sum a[j]*x[j]. (10) alpar@9: -- j in J' alpar@9: -- alpar@9: -- Formulae (7) and (8) allows determining implied lower and upper alpar@9: -- bounds of x[q]. alpar@9: -- alpar@9: -- Parameters len, val, L and U specify the constraint (1). alpar@9: -- alpar@9: -- Parameters lf_min and lf_max specify implied lower and upper bounds alpar@9: -- of the linear form (1). It is assumed that these bounds are computed alpar@9: -- with the routines eval_lf_min and eval_lf_max (see above). alpar@9: -- alpar@9: -- Parameter p specifies the probing variable x[p], which is set to 0 alpar@9: -- (if set is 0) or to 1 (if set is 1). alpar@9: -- alpar@9: -- Parameter q specifies the examined variable x[q]. alpar@9: -- alpar@9: -- On exit the routine returns one of the following codes: alpar@9: -- alpar@9: -- 0 - there is no logical relation between x[p] and x[q]; alpar@9: -- 1 - x[q] can take only on value 0; alpar@9: -- 2 - x[q] can take only on value 1. */ alpar@9: alpar@9: static int probing(int len, double val[], double L, double U, alpar@9: double lf_min, double lf_max, int p, int set, int q) alpar@9: { double temp; alpar@9: xassert(1 <= p && p < q && q <= len); alpar@9: /* compute L' (3) */ alpar@9: if (L != -DBL_MAX && set) L -= val[p]; alpar@9: /* compute U' (4) */ alpar@9: if (U != +DBL_MAX && set) U -= val[p]; alpar@9: /* compute MIN (9) */ alpar@9: if (lf_min != -DBL_MAX) alpar@9: { if (val[p] < 0.0) lf_min -= val[p]; alpar@9: if (val[q] < 0.0) lf_min -= val[q]; alpar@9: } alpar@9: /* compute MAX (10) */ alpar@9: if (lf_max != +DBL_MAX) alpar@9: { if (val[p] > 0.0) lf_max -= val[p]; alpar@9: if (val[q] > 0.0) lf_max -= val[q]; alpar@9: } alpar@9: /* compute implied lower bound of x[q]; see (7), (8) */ alpar@9: if (val[q] > 0.0) alpar@9: { if (L == -DBL_MAX || lf_max == +DBL_MAX) alpar@9: temp = -DBL_MAX; alpar@9: else alpar@9: temp = (L - lf_max) / val[q]; alpar@9: } alpar@9: else alpar@9: { if (U == +DBL_MAX || lf_min == -DBL_MAX) alpar@9: temp = -DBL_MAX; alpar@9: else alpar@9: temp = (U - lf_min) / val[q]; alpar@9: } alpar@9: if (temp > 0.001) return 2; alpar@9: /* compute implied upper bound of x[q]; see (7), (8) */ alpar@9: if (val[q] > 0.0) alpar@9: { if (U == +DBL_MAX || lf_min == -DBL_MAX) alpar@9: temp = +DBL_MAX; alpar@9: else alpar@9: temp = (U - lf_min) / val[q]; alpar@9: } alpar@9: else alpar@9: { if (L == -DBL_MAX || lf_max == +DBL_MAX) alpar@9: temp = +DBL_MAX; alpar@9: else alpar@9: temp = (L - lf_max) / val[q]; alpar@9: } alpar@9: if (temp < 0.999) return 1; alpar@9: /* there is no logical relation between x[p] and x[q] */ alpar@9: return 0; alpar@9: } alpar@9: alpar@9: struct COG alpar@9: { /* conflict graph; it represents logical relations between binary alpar@9: variables and has a vertex for each binary variable and its alpar@9: complement, and an edge between two vertices when at most one alpar@9: of the variables represented by the vertices can equal one in alpar@9: an optimal solution */ alpar@9: int n; alpar@9: /* number of variables */ alpar@9: int nb; alpar@9: /* number of binary variables represented in the graph (note that alpar@9: not all binary variables can be represented); vertices which alpar@9: correspond to binary variables have numbers 1, ..., nb while alpar@9: vertices which correspond to complements of binary variables alpar@9: have numbers nb+1, ..., nb+nb */ alpar@9: int ne; alpar@9: /* number of edges in the graph */ alpar@9: int *vert; /* int vert[1+n]; */ alpar@9: /* if x[j] is a binary variable represented in the graph, vert[j] alpar@9: is the vertex number corresponding to x[j]; otherwise vert[j] alpar@9: is zero */ alpar@9: int *orig; /* int list[1:nb]; */ alpar@9: /* if vert[j] = k > 0, then orig[k] = j */ alpar@9: unsigned char *a; alpar@9: /* adjacency matrix of the graph having 2*nb rows and columns; alpar@9: only strict lower triangle is stored in dense packed form */ alpar@9: }; alpar@9: alpar@9: /*---------------------------------------------------------------------- alpar@9: -- lpx_create_cog - create the conflict graph. alpar@9: -- alpar@9: -- SYNOPSIS alpar@9: -- alpar@9: -- #include "glplpx.h" alpar@9: -- void *lpx_create_cog(LPX *lp); alpar@9: -- alpar@9: -- DESCRIPTION alpar@9: -- alpar@9: -- The routine lpx_create_cog creates the conflict graph for a given alpar@9: -- problem instance. alpar@9: -- alpar@9: -- RETURNS alpar@9: -- alpar@9: -- If the graph has been created, the routine returns a pointer to it. alpar@9: -- Otherwise the routine returns NULL. */ alpar@9: alpar@9: #define MAX_NB 4000 alpar@9: #define MAX_ROW_LEN 500 alpar@9: alpar@9: static void lpx_add_cog_edge(void *_cog, int i, int j); alpar@9: alpar@9: static void *lpx_create_cog(LPX *lp) alpar@9: { struct COG *cog = NULL; alpar@9: int m, n, nb, i, j, p, q, len, *ind, *vert, *orig; alpar@9: double L, U, lf_min, lf_max, *val; alpar@9: xprintf("Creating the conflict graph...\n"); alpar@9: m = lpx_get_num_rows(lp); alpar@9: n = lpx_get_num_cols(lp); alpar@9: /* determine which binary variables should be included in the alpar@9: conflict graph */ alpar@9: nb = 0; alpar@9: vert = xcalloc(1+n, sizeof(int)); alpar@9: for (j = 1; j <= n; j++) vert[j] = 0; alpar@9: orig = xcalloc(1+n, sizeof(int)); alpar@9: ind = xcalloc(1+n, sizeof(int)); alpar@9: val = xcalloc(1+n, sizeof(double)); alpar@9: for (i = 1; i <= m; i++) alpar@9: { L = get_row_lb(lp, i); alpar@9: U = get_row_ub(lp, i); alpar@9: if (L == -DBL_MAX && U == +DBL_MAX) continue; alpar@9: len = lpx_get_mat_row(lp, i, ind, val); alpar@9: if (len > MAX_ROW_LEN) continue; alpar@9: lf_min = eval_lf_min(lp, len, ind, val); alpar@9: lf_max = eval_lf_max(lp, len, ind, val); alpar@9: for (p = 1; p <= len; p++) alpar@9: { if (!is_binary(lp, ind[p])) continue; alpar@9: for (q = p+1; q <= len; q++) alpar@9: { if (!is_binary(lp, ind[q])) continue; alpar@9: if (probing(len, val, L, U, lf_min, lf_max, p, 0, q) || alpar@9: probing(len, val, L, U, lf_min, lf_max, p, 1, q)) alpar@9: { /* there is a logical relation */ alpar@9: /* include the first variable in the graph */ alpar@9: j = ind[p]; alpar@9: if (vert[j] == 0) nb++, vert[j] = nb, orig[nb] = j; alpar@9: /* incude the second variable in the graph */ alpar@9: j = ind[q]; alpar@9: if (vert[j] == 0) nb++, vert[j] = nb, orig[nb] = j; alpar@9: } alpar@9: } alpar@9: } alpar@9: } alpar@9: /* if the graph is either empty or has too many vertices, do not alpar@9: create it */ alpar@9: if (nb == 0 || nb > MAX_NB) alpar@9: { xprintf("The conflict graph is either empty or too big\n"); alpar@9: xfree(vert); alpar@9: xfree(orig); alpar@9: goto done; alpar@9: } alpar@9: /* create the conflict graph */ alpar@9: cog = xmalloc(sizeof(struct COG)); alpar@9: cog->n = n; alpar@9: cog->nb = nb; alpar@9: cog->ne = 0; alpar@9: cog->vert = vert; alpar@9: cog->orig = orig; alpar@9: len = nb + nb; /* number of vertices */ alpar@9: len = (len * (len - 1)) / 2; /* number of entries in triangle */ alpar@9: len = (len + (CHAR_BIT - 1)) / CHAR_BIT; /* bytes needed */ alpar@9: cog->a = xmalloc(len); alpar@9: memset(cog->a, 0, len); alpar@9: for (j = 1; j <= nb; j++) alpar@9: { /* add edge between variable and its complement */ alpar@9: lpx_add_cog_edge(cog, +orig[j], -orig[j]); alpar@9: } alpar@9: for (i = 1; i <= m; i++) alpar@9: { L = get_row_lb(lp, i); alpar@9: U = get_row_ub(lp, i); alpar@9: if (L == -DBL_MAX && U == +DBL_MAX) continue; alpar@9: len = lpx_get_mat_row(lp, i, ind, val); alpar@9: if (len > MAX_ROW_LEN) continue; alpar@9: lf_min = eval_lf_min(lp, len, ind, val); alpar@9: lf_max = eval_lf_max(lp, len, ind, val); alpar@9: for (p = 1; p <= len; p++) alpar@9: { if (!is_binary(lp, ind[p])) continue; alpar@9: for (q = p+1; q <= len; q++) alpar@9: { if (!is_binary(lp, ind[q])) continue; alpar@9: /* set x[p] to 0 and examine x[q] */ alpar@9: switch (probing(len, val, L, U, lf_min, lf_max, p, 0, q)) alpar@9: { case 0: alpar@9: /* no logical relation */ alpar@9: break; alpar@9: case 1: alpar@9: /* x[p] = 0 implies x[q] = 0 */ alpar@9: lpx_add_cog_edge(cog, -ind[p], +ind[q]); alpar@9: break; alpar@9: case 2: alpar@9: /* x[p] = 0 implies x[q] = 1 */ alpar@9: lpx_add_cog_edge(cog, -ind[p], -ind[q]); alpar@9: break; alpar@9: default: alpar@9: xassert(lp != lp); alpar@9: } alpar@9: /* set x[p] to 1 and examine x[q] */ alpar@9: switch (probing(len, val, L, U, lf_min, lf_max, p, 1, q)) alpar@9: { case 0: alpar@9: /* no logical relation */ alpar@9: break; alpar@9: case 1: alpar@9: /* x[p] = 1 implies x[q] = 0 */ alpar@9: lpx_add_cog_edge(cog, +ind[p], +ind[q]); alpar@9: break; alpar@9: case 2: alpar@9: /* x[p] = 1 implies x[q] = 1 */ alpar@9: lpx_add_cog_edge(cog, +ind[p], -ind[q]); alpar@9: break; alpar@9: default: alpar@9: xassert(lp != lp); alpar@9: } alpar@9: } alpar@9: } alpar@9: } alpar@9: xprintf("The conflict graph has 2*%d vertices and %d edges\n", alpar@9: cog->nb, cog->ne); alpar@9: done: xfree(ind); alpar@9: xfree(val); alpar@9: return cog; alpar@9: } alpar@9: alpar@9: /*---------------------------------------------------------------------- alpar@9: -- lpx_add_cog_edge - add edge to the conflict graph. alpar@9: -- alpar@9: -- SYNOPSIS alpar@9: -- alpar@9: -- #include "glplpx.h" alpar@9: -- void lpx_add_cog_edge(void *cog, int i, int j); alpar@9: -- alpar@9: -- DESCRIPTION alpar@9: -- alpar@9: -- The routine lpx_add_cog_edge adds an edge to the conflict graph. alpar@9: -- The edge connects x[i] (if i > 0) or its complement (if i < 0) and alpar@9: -- x[j] (if j > 0) or its complement (if j < 0), where i and j are alpar@9: -- original ordinal numbers of corresponding variables. */ alpar@9: alpar@9: static void lpx_add_cog_edge(void *_cog, int i, int j) alpar@9: { struct COG *cog = _cog; alpar@9: int k; alpar@9: xassert(i != j); alpar@9: /* determine indices of corresponding vertices */ alpar@9: if (i > 0) alpar@9: { xassert(1 <= i && i <= cog->n); alpar@9: i = cog->vert[i]; alpar@9: xassert(i != 0); alpar@9: } alpar@9: else alpar@9: { i = -i; alpar@9: xassert(1 <= i && i <= cog->n); alpar@9: i = cog->vert[i]; alpar@9: xassert(i != 0); alpar@9: i += cog->nb; alpar@9: } alpar@9: if (j > 0) alpar@9: { xassert(1 <= j && j <= cog->n); alpar@9: j = cog->vert[j]; alpar@9: xassert(j != 0); alpar@9: } alpar@9: else alpar@9: { j = -j; alpar@9: xassert(1 <= j && j <= cog->n); alpar@9: j = cog->vert[j]; alpar@9: xassert(j != 0); alpar@9: j += cog->nb; alpar@9: } alpar@9: /* only lower triangle is stored, so we need i > j */ alpar@9: if (i < j) k = i, i = j, j = k; alpar@9: k = ((i - 1) * (i - 2)) / 2 + (j - 1); alpar@9: cog->a[k / CHAR_BIT] |= alpar@9: (unsigned char)(1 << ((CHAR_BIT - 1) - k % CHAR_BIT)); alpar@9: cog->ne++; alpar@9: return; alpar@9: } alpar@9: alpar@9: /*---------------------------------------------------------------------- alpar@9: -- MAXIMUM WEIGHT CLIQUE alpar@9: -- alpar@9: -- Two subroutines sub() and wclique() below are intended to find a alpar@9: -- maximum weight clique in a given undirected graph. These subroutines alpar@9: -- are slightly modified version of the program WCLIQUE developed by alpar@9: -- Patric Ostergard and based alpar@9: -- on ideas from the article "P. R. J. Ostergard, A new algorithm for alpar@9: -- the maximum-weight clique problem, submitted for publication", which alpar@9: -- in turn is a generalization of the algorithm for unweighted graphs alpar@9: -- presented in "P. R. J. Ostergard, A fast algorithm for the maximum alpar@9: -- clique problem, submitted for publication". alpar@9: -- alpar@9: -- USED WITH PERMISSION OF THE AUTHOR OF THE ORIGINAL CODE. */ alpar@9: alpar@9: struct dsa alpar@9: { /* dynamic storage area */ alpar@9: int n; alpar@9: /* number of vertices */ alpar@9: int *wt; /* int wt[0:n-1]; */ alpar@9: /* weights */ alpar@9: unsigned char *a; alpar@9: /* adjacency matrix (packed lower triangle without main diag.) */ alpar@9: int record; alpar@9: /* weight of best clique */ alpar@9: int rec_level; alpar@9: /* number of vertices in best clique */ alpar@9: int *rec; /* int rec[0:n-1]; */ alpar@9: /* best clique so far */ alpar@9: int *clique; /* int clique[0:n-1]; */ alpar@9: /* table for pruning */ alpar@9: int *set; /* int set[0:n-1]; */ alpar@9: /* current clique */ alpar@9: }; alpar@9: alpar@9: #define n (dsa->n) alpar@9: #define wt (dsa->wt) alpar@9: #define a (dsa->a) alpar@9: #define record (dsa->record) alpar@9: #define rec_level (dsa->rec_level) alpar@9: #define rec (dsa->rec) alpar@9: #define clique (dsa->clique) alpar@9: #define set (dsa->set) alpar@9: alpar@9: #if 0 alpar@9: static int is_edge(struct dsa *dsa, int i, int j) alpar@9: { /* if there is arc (i,j), the routine returns true; otherwise alpar@9: false; 0 <= i, j < n */ alpar@9: int k; alpar@9: xassert(0 <= i && i < n); alpar@9: xassert(0 <= j && j < n); alpar@9: if (i == j) return 0; alpar@9: if (i < j) k = i, i = j, j = k; alpar@9: k = (i * (i - 1)) / 2 + j; alpar@9: return a[k / CHAR_BIT] & alpar@9: (unsigned char)(1 << ((CHAR_BIT - 1) - k % CHAR_BIT)); alpar@9: } alpar@9: #else alpar@9: #define is_edge(dsa, i, j) ((i) == (j) ? 0 : \ alpar@9: (i) > (j) ? is_edge1(i, j) : is_edge1(j, i)) alpar@9: #define is_edge1(i, j) is_edge2(((i) * ((i) - 1)) / 2 + (j)) alpar@9: #define is_edge2(k) (a[(k) / CHAR_BIT] & \ alpar@9: (unsigned char)(1 << ((CHAR_BIT - 1) - (k) % CHAR_BIT))) alpar@9: #endif alpar@9: alpar@9: static void sub(struct dsa *dsa, int ct, int table[], int level, alpar@9: int weight, int l_weight) alpar@9: { int i, j, k, curr_weight, left_weight, *p1, *p2, *newtable; alpar@9: newtable = xcalloc(n, sizeof(int)); alpar@9: if (ct <= 0) alpar@9: { /* 0 or 1 elements left; include these */ alpar@9: if (ct == 0) alpar@9: { set[level++] = table[0]; alpar@9: weight += l_weight; alpar@9: } alpar@9: if (weight > record) alpar@9: { record = weight; alpar@9: rec_level = level; alpar@9: for (i = 0; i < level; i++) rec[i] = set[i]; alpar@9: } alpar@9: goto done; alpar@9: } alpar@9: for (i = ct; i >= 0; i--) alpar@9: { if ((level == 0) && (i < ct)) goto done; alpar@9: k = table[i]; alpar@9: if ((level > 0) && (clique[k] <= (record - weight))) alpar@9: goto done; /* prune */ alpar@9: set[level] = k; alpar@9: curr_weight = weight + wt[k]; alpar@9: l_weight -= wt[k]; alpar@9: if (l_weight <= (record - curr_weight)) alpar@9: goto done; /* prune */ alpar@9: p1 = newtable; alpar@9: p2 = table; alpar@9: left_weight = 0; alpar@9: while (p2 < table + i) alpar@9: { j = *p2++; alpar@9: if (is_edge(dsa, j, k)) alpar@9: { *p1++ = j; alpar@9: left_weight += wt[j]; alpar@9: } alpar@9: } alpar@9: if (left_weight <= (record - curr_weight)) continue; alpar@9: sub(dsa, p1 - newtable - 1, newtable, level + 1, curr_weight, alpar@9: left_weight); alpar@9: } alpar@9: done: xfree(newtable); alpar@9: return; alpar@9: } alpar@9: alpar@9: static int wclique(int _n, int w[], unsigned char _a[], int sol[]) alpar@9: { struct dsa _dsa, *dsa = &_dsa; alpar@9: int i, j, p, max_wt, max_nwt, wth, *used, *nwt, *pos; alpar@9: glp_long timer; alpar@9: n = _n; alpar@9: wt = &w[1]; alpar@9: a = _a; alpar@9: record = 0; alpar@9: rec_level = 0; alpar@9: rec = &sol[1]; alpar@9: clique = xcalloc(n, sizeof(int)); alpar@9: set = xcalloc(n, sizeof(int)); alpar@9: used = xcalloc(n, sizeof(int)); alpar@9: nwt = xcalloc(n, sizeof(int)); alpar@9: pos = xcalloc(n, sizeof(int)); alpar@9: /* start timer */ alpar@9: timer = xtime(); alpar@9: /* order vertices */ alpar@9: for (i = 0; i < n; i++) alpar@9: { nwt[i] = 0; alpar@9: for (j = 0; j < n; j++) alpar@9: if (is_edge(dsa, i, j)) nwt[i] += wt[j]; alpar@9: } alpar@9: for (i = 0; i < n; i++) alpar@9: used[i] = 0; alpar@9: for (i = n-1; i >= 0; i--) alpar@9: { max_wt = -1; alpar@9: max_nwt = -1; alpar@9: for (j = 0; j < n; j++) alpar@9: { if ((!used[j]) && ((wt[j] > max_wt) || (wt[j] == max_wt alpar@9: && nwt[j] > max_nwt))) alpar@9: { max_wt = wt[j]; alpar@9: max_nwt = nwt[j]; alpar@9: p = j; alpar@9: } alpar@9: } alpar@9: pos[i] = p; alpar@9: used[p] = 1; alpar@9: for (j = 0; j < n; j++) alpar@9: if ((!used[j]) && (j != p) && (is_edge(dsa, p, j))) alpar@9: nwt[j] -= wt[p]; alpar@9: } alpar@9: /* main routine */ alpar@9: wth = 0; alpar@9: for (i = 0; i < n; i++) alpar@9: { wth += wt[pos[i]]; alpar@9: sub(dsa, i, pos, 0, 0, wth); alpar@9: clique[pos[i]] = record; alpar@9: #if 0 alpar@9: if (utime() >= timer + 5.0) alpar@9: #else alpar@9: if (xdifftime(xtime(), timer) >= 5.0 - 0.001) alpar@9: #endif alpar@9: { /* print current record and reset timer */ alpar@9: xprintf("level = %d (%d); best = %d\n", i+1, n, record); alpar@9: #if 0 alpar@9: timer = utime(); alpar@9: #else alpar@9: timer = xtime(); alpar@9: #endif alpar@9: } alpar@9: } alpar@9: xfree(clique); alpar@9: xfree(set); alpar@9: xfree(used); alpar@9: xfree(nwt); alpar@9: xfree(pos); alpar@9: /* return the solution found */ alpar@9: for (i = 1; i <= rec_level; i++) sol[i]++; alpar@9: return rec_level; alpar@9: } alpar@9: alpar@9: #undef n alpar@9: #undef wt alpar@9: #undef a alpar@9: #undef record alpar@9: #undef rec_level alpar@9: #undef rec alpar@9: #undef clique alpar@9: #undef set alpar@9: alpar@9: /*---------------------------------------------------------------------- alpar@9: -- lpx_clique_cut - generate cluque cut. alpar@9: -- alpar@9: -- SYNOPSIS alpar@9: -- alpar@9: -- #include "glplpx.h" alpar@9: -- int lpx_clique_cut(LPX *lp, void *cog, int ind[], double val[]); alpar@9: -- alpar@9: -- DESCRIPTION alpar@9: -- alpar@9: -- The routine lpx_clique_cut generates a clique cut using the conflict alpar@9: -- graph specified by the parameter cog. alpar@9: -- alpar@9: -- If a violated clique cut has been found, it has the following form: alpar@9: -- alpar@9: -- sum{j in J} a[j]*x[j] <= b. alpar@9: -- alpar@9: -- Variable indices j in J are stored in elements ind[1], ..., ind[len] alpar@9: -- while corresponding constraint coefficients are stored in elements alpar@9: -- val[1], ..., val[len], where len is returned on exit. The right-hand alpar@9: -- side b is stored in element val[0]. alpar@9: -- alpar@9: -- RETURNS alpar@9: -- alpar@9: -- If the cutting plane has been successfully generated, the routine alpar@9: -- returns 1 <= len <= n, which is the number of non-zero coefficients alpar@9: -- in the inequality constraint. Otherwise, the routine returns zero. */ alpar@9: alpar@9: static int lpx_clique_cut(LPX *lp, void *_cog, int ind[], double val[]) alpar@9: { struct COG *cog = _cog; alpar@9: int n = lpx_get_num_cols(lp); alpar@9: int j, t, v, card, temp, len = 0, *w, *sol; alpar@9: double x, sum, b, *vec; alpar@9: /* allocate working arrays */ alpar@9: w = xcalloc(1 + 2 * cog->nb, sizeof(int)); alpar@9: sol = xcalloc(1 + 2 * cog->nb, sizeof(int)); alpar@9: vec = xcalloc(1+n, sizeof(double)); alpar@9: /* assign weights to vertices of the conflict graph */ alpar@9: for (t = 1; t <= cog->nb; t++) alpar@9: { j = cog->orig[t]; alpar@9: x = lpx_get_col_prim(lp, j); alpar@9: temp = (int)(100.0 * x + 0.5); alpar@9: if (temp < 0) temp = 0; alpar@9: if (temp > 100) temp = 100; alpar@9: w[t] = temp; alpar@9: w[cog->nb + t] = 100 - temp; alpar@9: } alpar@9: /* find a clique of maximum weight */ alpar@9: card = wclique(2 * cog->nb, w, cog->a, sol); alpar@9: /* compute the clique weight for unscaled values */ alpar@9: sum = 0.0; alpar@9: for ( t = 1; t <= card; t++) alpar@9: { v = sol[t]; alpar@9: xassert(1 <= v && v <= 2 * cog->nb); alpar@9: if (v <= cog->nb) alpar@9: { /* vertex v corresponds to binary variable x[j] */ alpar@9: j = cog->orig[v]; alpar@9: x = lpx_get_col_prim(lp, j); alpar@9: sum += x; alpar@9: } alpar@9: else alpar@9: { /* vertex v corresponds to the complement of x[j] */ alpar@9: j = cog->orig[v - cog->nb]; alpar@9: x = lpx_get_col_prim(lp, j); alpar@9: sum += 1.0 - x; alpar@9: } alpar@9: } alpar@9: /* if the sum of binary variables and their complements in the alpar@9: clique greater than 1, the clique cut is violated */ alpar@9: if (sum >= 1.01) alpar@9: { /* construct the inquality */ alpar@9: for (j = 1; j <= n; j++) vec[j] = 0; alpar@9: b = 1.0; alpar@9: for (t = 1; t <= card; t++) alpar@9: { v = sol[t]; alpar@9: if (v <= cog->nb) alpar@9: { /* vertex v corresponds to binary variable x[j] */ alpar@9: j = cog->orig[v]; alpar@9: xassert(1 <= j && j <= n); alpar@9: vec[j] += 1.0; alpar@9: } alpar@9: else alpar@9: { /* vertex v corresponds to the complement of x[j] */ alpar@9: j = cog->orig[v - cog->nb]; alpar@9: xassert(1 <= j && j <= n); alpar@9: vec[j] -= 1.0; alpar@9: b -= 1.0; alpar@9: } alpar@9: } alpar@9: xassert(len == 0); alpar@9: for (j = 1; j <= n; j++) alpar@9: { if (vec[j] != 0.0) alpar@9: { len++; alpar@9: ind[len] = j, val[len] = vec[j]; alpar@9: } alpar@9: } alpar@9: ind[0] = 0, val[0] = b; alpar@9: } alpar@9: /* free working arrays */ alpar@9: xfree(w); alpar@9: xfree(sol); alpar@9: xfree(vec); alpar@9: /* return to the calling program */ alpar@9: return len; alpar@9: } alpar@9: alpar@9: /*---------------------------------------------------------------------- alpar@9: -- lpx_delete_cog - delete the conflict graph. alpar@9: -- alpar@9: -- SYNOPSIS alpar@9: -- alpar@9: -- #include "glplpx.h" alpar@9: -- void lpx_delete_cog(void *cog); alpar@9: -- alpar@9: -- DESCRIPTION alpar@9: -- alpar@9: -- The routine lpx_delete_cog deletes the conflict graph, which the alpar@9: -- parameter cog points to, freeing all the memory allocated to this alpar@9: -- object. */ alpar@9: alpar@9: static void lpx_delete_cog(void *_cog) alpar@9: { struct COG *cog = _cog; alpar@9: xfree(cog->vert); alpar@9: xfree(cog->orig); alpar@9: xfree(cog->a); alpar@9: xfree(cog); alpar@9: } alpar@9: alpar@9: /**********************************************************************/ alpar@9: alpar@9: void *ios_clq_init(glp_tree *tree) alpar@9: { /* initialize clique cut generator */ alpar@9: glp_prob *mip = tree->mip; alpar@9: xassert(mip != NULL); alpar@9: return lpx_create_cog(mip); alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * ios_clq_gen - generate clique cuts alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * #include "glpios.h" alpar@9: * void ios_clq_gen(glp_tree *tree, void *gen); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine ios_clq_gen generates clique cuts for the current point alpar@9: * and adds them to the clique pool. */ alpar@9: alpar@9: void ios_clq_gen(glp_tree *tree, void *gen) alpar@9: { int n = lpx_get_num_cols(tree->mip); alpar@9: int len, *ind; alpar@9: double *val; alpar@9: xassert(gen != NULL); alpar@9: ind = xcalloc(1+n, sizeof(int)); alpar@9: val = xcalloc(1+n, sizeof(double)); alpar@9: len = lpx_clique_cut(tree->mip, gen, ind, val); alpar@9: if (len > 0) alpar@9: { /* xprintf("len = %d\n", len); */ alpar@9: glp_ios_add_row(tree, NULL, GLP_RF_CLQ, 0, len, ind, val, alpar@9: GLP_UP, val[0]); alpar@9: } alpar@9: xfree(ind); alpar@9: xfree(val); alpar@9: return; alpar@9: } alpar@9: alpar@9: /**********************************************************************/ alpar@9: alpar@9: void ios_clq_term(void *gen) alpar@9: { /* terminate clique cut generator */ alpar@9: xassert(gen != NULL); alpar@9: lpx_delete_cog(gen); alpar@9: return; alpar@9: } alpar@9: alpar@9: /* eof */