-
Notifications
You must be signed in to change notification settings - Fork 117
/
modk.c
286 lines (224 loc) · 6.4 KB
/
modk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (C) 2005, International Business Machines
// Corporation and others. All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).
/*! \file modk.c
\brief Example of cut generator using callbacks in the CBC C API
Tries to generate mod-k cuts, i.e. for each constraint
rounds it down after multiplying it by {1 ... k-1} / k
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <assert.h>
#include <float.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <Cbc_C_Interface.h>
#define MIN(a,b) ( (a<b) ? (a) : (b) )
static int dblcmp( const void *pd1, const void *pd2 );
static char dbl_is_integer( const double value );
static int intcmp( const void *pd1, const void *pd2 );
static int mcd( int n, const int values[] );
static int dbl_as_int( double value );
static double try_cut( void *osiSolver, int row, const double mult, int num, int den,
int *nzCut, int *idx, double *coef, double *rhs, int *icoef );
void cutcallback( void *osiSolver, void *osiCuts, void *appData, int level, int pass )
{
printf("entered callback\n");
int m = Osi_getNumRows( osiSolver );
int n = Osi_getNumCols( osiSolver );
// sorted coefs and rhs values
int *scoef = (int *) malloc( sizeof(int)*(n+1) );
// different integer positive coeffs (values for k)
int *dcoef = (int *) malloc( sizeof(int)*(n+1) );
// integer coefficients for computing the cut
int *icoef = (int*) malloc( sizeof(int)*n );
// double coefficients for storing the cut
double *coef = (double*) malloc(sizeof(double)*n);
// variables in this cut
int *cidx = (int*) malloc(sizeof(int)*n);
for ( int i=0 ; i<m ; ++i )
{
// checking different coefficients
int nz = Osi_getRowNz( osiSolver, i );
double rhs = Osi_getRowRHS( osiSolver, i );
if (!dbl_is_integer(rhs))
continue;
int irhs = dbl_as_int( rhs );
char allInt = 1;
const double *coeffs = Osi_getRowCoeffs( osiSolver, i );
for ( int j=0 ; (j<nz) ; ++j )
{
if (!dbl_is_integer(coeffs[j]))
{
allInt = 0;
break;
}
scoef[j] = abs(dbl_as_int(coeffs[j]));
}
if (!allInt)
continue;
scoef[nz++] = abs(irhs);
qsort( scoef, nz, sizeof(int), intcmp );
int prev = INT_MAX;
int ndiff = 0;
// checking for different values for k
for ( int j=0 ; (j<nz) ; ++j )
{
if (prev!=scoef[j])
{
prev = scoef[j];
dcoef[ndiff++] = scoef[j];
} // different coefs
} // all columns
if (ndiff<=1)
continue;
int nRem = 0;
// removing those which are divisors of larger values
for ( int j=0 ; (j<ndiff-1) ; ++j )
{
for ( int jl=j+1 ; (jl<ndiff) ; ++jl )
{
if (!dcoef[j])
continue;
if ( dcoef[jl] % dcoef[j] == 0 )
{
dcoef[j] = INT_MAX;
++nRem;
break;
}
} // larger
} // all non-zeros
qsort( dcoef, ndiff, sizeof(int), intcmp );
ndiff -= nRem;
for ( int ik=0 ; ik<ndiff ; ++ ik )
{
int k = dcoef[ik];
for ( int num=1 ; (num<k) ; ++num )
{
int nzCut = 0;
double rhsCut = 0.0;
int nMult, startMult = 0;
double mults[] = { 1.0, -1.0 };
char sense = toupper(Osi_getRowSense( osiSolver, i ));
switch (sense)
{
case 'E':
{
nMult = 2;
break;
}
case 'G':
{
nMult = 1;
startMult = 1;
break;
}
case 'L':
{
nMult = 1;
startMult = 0;
break;
}
default:
{
fprintf( stderr, "unknown sense!");
abort();
}
}
for ( int iMult=startMult ; (iMult<nMult) ; ++iMult )
{
const double mult = mults[iMult];
const double viol = try_cut( osiSolver, i, mult, num, k, &nzCut,
cidx, coef, &rhsCut, icoef );
if ( viol<0.001 )
continue;
OsiCuts_addRowCut( osiCuts, nzCut, cidx, coef, 'L', rhsCut );
} // constraint in the form <=
} // every numerator < k
} // every denominator k
} // every row
free( scoef );
free( dcoef );
free( cidx );
free( coef );
free( icoef );
}
static int dblcmp( const void *pd1, const void *pd2 )
{
const double *d1 = (const double *) pd1;
const double *d2 = (const double *) pd2;
if ((*d1)<(*d2))
return -1;
if ((*d1)>(*d2))
return 1;
return 0;
}
static char dbl_is_integer( const double value )
{
double rv = floor( value+0.5 );
return fabs(rv-value) < 1e-6;
}
static int intcmp( const void *pi1, const void *pi2 )
{
const int *i1 = (const int *) pi1;
const int *i2 = (const int *) pi2;
return ( (*i1) - (*i2) );
}
static int mcd( int n, const int values[] )
{
//for ( int j=0 ; (j<n) ; ++j )
//
return 0;
}
static int dbl_as_int( double value )
{
return (int) floor(value+0.5);
}
static double try_cut( void *osiSolver, int row, const double mult, int num, int den,
int *nzCut, int *idx, double *coef, double *rhs, int *icoef )
{
int nz = Osi_getRowNz( osiSolver, row );
const int *rIdx = Osi_getRowIndices( osiSolver, row );
const double *rCoef = Osi_getRowCoeffs( osiSolver, row );
int imult = dbl_as_int(mult);
int irhs = dbl_as_int( imult*Osi_getRowRHS( osiSolver, row ) );
for ( int j=0 ; j<nz ; ++j )
icoef[j] = dbl_as_int( imult*rCoef[j] );
const double *x = Osi_getColSolution( osiSolver );
double sumLHS = 0.0;
(*nzCut) = 0;
for ( int j=0 ; j<nz ; ++j )
{
int col = rIdx[j];
assert( col >= 0 && col<Osi_getNumCols(osiSolver) );
int cc = (icoef[j]*num)/den;
if (!cc)
continue;
idx[(*nzCut)] = col;
coef[(*nzCut)] = cc;
sumLHS += ((double)cc) * x[col];
(*nzCut)++;
}
*rhs = (irhs*num)/den;
return (*rhs) - sumLHS;
}
int main( int argc, char **argv )
{
if (argc<2)
{
fprintf( stderr, "enter mps instance name.\n");
exit(1);
}
Cbc_Model *mip = Cbc_newModel();
Cbc_readMps( mip, argv[1] );
Cbc_setParameter( mip, "cuts", "off" );
Cbc_setParameter( mip, "gomory", "ifmove" );
Cbc_setParameter( mip, "preprocess", "off" );
Cbc_addCutCallback( mip, cutcallback, "modk", NULL, -1, 0 );
Cbc_solve( mip );
Cbc_deleteModel( mip );
}
/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
*/