GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
clscons_miplibconstypes.cpp
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the program and library */
4/* GCG --- Generic Column Generation */
5/* a Dantzig-Wolfe decomposition based extension */
6/* of the branch-cut-and-price framework */
7/* SCIP --- Solving Constraint Integer Programs */
8/* */
9/* Copyright (C) 2010-2026 Operations Research, RWTH Aachen University */
10/* Zuse Institute Berlin (ZIB) */
11/* */
12/* Licensed under the Apache License, Version 2.0 (the "License"); */
13/* you may not use this file except in compliance with the License. */
14/* You may obtain a copy of the License at */
15/* */
16/* http://www.apache.org/licenses/LICENSE-2.0 */
17/* */
18/* Unless required by applicable law or agreed to in writing, software */
19/* distributed under the License is distributed on an "AS IS" BASIS, */
20/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
21/* See the License for the specific language governing permissions and */
22/* limitations under the License. */
23/* */
24/* You should have received a copy of the Apache-2.0 license */
25/* along with GCG; see the file LICENSE. If not visit gcg.or.rwth-aachen.de.*/
26/* */
27/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
28
29/**@file clscons_miplibconstypes.cpp
30 *
31 * @brief classifies constraints according to their miplib constypes
32 */
33
34/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35
37#include "gcg/cons_decomp.h"
38#include "gcg/cons_decomp.hpp"
39#include <vector>
40#include <stdio.h>
41#include <sstream>
42
44
46#include "gcg/scip_misc.h"
47
48/* classifier properties */
49#define CLSCONS_NAME "miplibconstype" /**< name of classifier */
50#define CLSCONS_DESC "miplib constypes" /**< short description of classification*/
51#define CLSCONS_PRIORITY 0
52
53#define CLSCONS_ENABLED TRUE
54
55
56/*
57 * Data structures
58 */
59
60/** classifier handler data */
62{
63};
64
65
66/*
67 * Local methods
68 */
69
70/* put your local methods here, and declare them static */
71
72
73/*
74 * classifier callback methods
75 */
76
77/** destructor of classifier to free user data (called when GCG is exiting) */
78#define classifierFree NULL
79
80static
81GCG_DECL_CONSCLASSIFY(classifierClassify)
82{
83 gcg::DETPROBDATA* detprobdata;
85 if( transformed )
86 {
88 }
89 else
90 {
92 }
93
94 std::vector<int> nfoundconstypesrangedsinglecount( (int) SCIP_CONSTYPE_GENERAL + 1, 0 );
95 std::vector<int> nfoundconstypesrangeddoublecount( (int) SCIP_CONSTYPE_GENERAL + 1, 0 );
96
97 std::vector<int> classforcons = std::vector<int>( detprobdata->getNConss(), -1 );
98 gcg::ConsPartition* classifier;
99
100 /* firstly, assign all constraints to classindices */
101 for( int c = 0; c < detprobdata->getNConss(); ++ c )
102 {
103 SCIP_CONS* cons;
104 SCIP_Real lhs;
105 SCIP_Real rhs;
106 SCIP_Real* vals = NULL;
107 SCIP_VAR** vars = NULL;
108 int nvars;
109 int i;
110
111 cons = detprobdata->getCons(c);
112
114
115 lhs = GCGconsGetLhs(origprob, cons);
116 rhs = GCGconsGetRhs(origprob, cons);
117 if( nvars != 0 )
118 {
123 }
124
125 for( i = 0; i < nvars; i++ )
126 {
127 assert(!SCIPisZero(origprob, vals[i]) );
128 }
129
130
131 /* is constraint of type SCIP_CONSTYPE_EMPTY? */
132 if( nvars == 0 )
133 {
134 SCIPdebugMsg(origprob, "classified as EMPTY: ");
136 nfoundconstypesrangedsinglecount[SCIP_CONSTYPE_EMPTY]++;
137 nfoundconstypesrangeddoublecount[SCIP_CONSTYPE_EMPTY]++;
138 classforcons[c] = SCIP_CONSTYPE_EMPTY;
139 continue;
140 }
141
142 /* is constraint of type SCIP_CONSTYPE_FREE? */
143 if( SCIPisInfinity(origprob, rhs) && SCIPisInfinity(origprob, -lhs) )
144 {
145 SCIPdebugMsg(origprob, "classified as FREE: ");
147 nfoundconstypesrangeddoublecount[SCIP_CONSTYPE_FREE]++;
148 nfoundconstypesrangedsinglecount[SCIP_CONSTYPE_FREE]++;
149 classforcons[c] = SCIP_CONSTYPE_FREE;
152 continue;
153 }
154
155 /* is constraint of type SCIP_CONSTYPE_SINGLETON? */
156 if( nvars == 1 )
157 {
158 SCIPdebugMsg(origprob, "classified as SINGLETON: ");
160 nfoundconstypesrangeddoublecount[SCIP_CONSTYPE_SINGLETON] += 2 ;
161 nfoundconstypesrangedsinglecount[SCIP_CONSTYPE_SINGLETON]++;
162 classforcons[c] = SCIP_CONSTYPE_SINGLETON;
165 continue;
166 }
167
168 /* is constraint of type SCIP_CONSTYPE_AGGREGATION? */
169 if( nvars == 2 && SCIPisEQ(origprob, lhs, rhs) )
170 {
171 SCIPdebugMsg(origprob, "classified as AGGREGATION: ");
173 nfoundconstypesrangeddoublecount[SCIP_CONSTYPE_AGGREGATION]++;
174 nfoundconstypesrangedsinglecount[SCIP_CONSTYPE_AGGREGATION]++;
175 classforcons[c] = SCIP_CONSTYPE_AGGREGATION;
178 continue;
179 }
180
181 /* is constraint of type SCIP_CONSTYPE_{VARBOUND}? */
182 if( nvars == 2 )
183 {
184 SCIPdebugMsg(origprob, "classified as VARBOUND: ");
186 nfoundconstypesrangeddoublecount[SCIP_CONSTYPE_VARBOUND] += 2 ;
187 nfoundconstypesrangedsinglecount[SCIP_CONSTYPE_VARBOUND]++;
188 classforcons[c] = SCIP_CONSTYPE_VARBOUND;
191 continue;
192 }
193
194 /* is constraint of type SCIP_CONSTYPE_{SETPARTITION, SETPACKING, SETCOVERING, CARDINALITY, INVKNAPSACK}? */
195 {
196 SCIP_Real scale;
197 SCIP_Real b;
198 SCIP_Bool unmatched;
199 int nnegbinvars;
200
201 unmatched = FALSE;
202 nnegbinvars = 0;
203
204 scale = REALABS(vals[0]);
205 for( i = 0; i < nvars && !unmatched; i++ )
206 {
207 unmatched = unmatched || SCIPvarGetType(vars[i]) == SCIP_VARTYPE_CONTINUOUS;
208 unmatched = unmatched || SCIPisLE(origprob, SCIPvarGetLbGlobal(vars[i]), -1.0);
209 unmatched = unmatched || SCIPisGE(origprob, SCIPvarGetUbGlobal(vars[i]), 2.0);
210 unmatched = unmatched || !SCIPisEQ(origprob, REALABS(vals[i]), scale);
211
212 if( vals[i] < 0.0 )
213 nnegbinvars++;
214 }
215
216 if( !unmatched )
217 {
218 if( SCIPisEQ(origprob, lhs, rhs) )
219 {
220 b = rhs/scale + nnegbinvars;
221 if( SCIPisEQ(origprob, 1.0, b) )
222 {
223 SCIPdebugMsg(origprob, "classified as SETPARTITION: ");
225 nfoundconstypesrangeddoublecount[SCIP_CONSTYPE_SETPARTITION] += 1 ;
226 nfoundconstypesrangedsinglecount[SCIP_CONSTYPE_SETPARTITION]++;
227 classforcons[c] = SCIP_CONSTYPE_SETPARTITION;
230 continue;
231 }
233 {
234 SCIPdebugMsg(origprob, "classified as CARDINALITY: ");
236 nfoundconstypesrangeddoublecount[SCIP_CONSTYPE_CARDINALITY] += 1 ;
237 nfoundconstypesrangedsinglecount[SCIP_CONSTYPE_CARDINALITY]++;
238 classforcons[c] = SCIP_CONSTYPE_CARDINALITY;
241 continue;
242 }
243 }
244
245 b = rhs/scale + nnegbinvars;
246 if( SCIPisEQ(origprob, 1.0, b) )
247 {
248 SCIPdebugMsg(origprob, "classified as SETPACKING: ");
250 nfoundconstypesrangeddoublecount[SCIP_CONSTYPE_SETPACKING] += 1 ;
251 nfoundconstypesrangedsinglecount[SCIP_CONSTYPE_SETPACKING]++;
252 classforcons[c] = SCIP_CONSTYPE_SETPACKING;
253 rhs = SCIPinfinity(origprob);
254 }
256 {
257 SCIPdebugMsg(origprob, "classified as INVKNAPSACK: ");
259 nfoundconstypesrangeddoublecount[SCIP_CONSTYPE_INVKNAPSACK] += 1 ;
260 nfoundconstypesrangedsinglecount[SCIP_CONSTYPE_INVKNAPSACK]++;
261 classforcons[c] = SCIP_CONSTYPE_INVKNAPSACK;
262 rhs = SCIPinfinity(origprob);
263 }
264
265 b = lhs/scale + nnegbinvars;
266 if( SCIPisEQ(origprob, 1.0, b) )
267 {
268 SCIPdebugMsg(origprob, "classified as SETCOVERING: ");
270 nfoundconstypesrangeddoublecount[SCIP_CONSTYPE_SETCOVERING] += 1 ;
271 nfoundconstypesrangedsinglecount[SCIP_CONSTYPE_SETCOVERING]++;
272 classforcons[c] = SCIP_CONSTYPE_SETCOVERING;
273 lhs = -SCIPinfinity(origprob);
274 }
275
276 if( SCIPisInfinity(origprob, -lhs) && SCIPisInfinity(origprob, rhs) )
277 {
280 continue;
281 }
282 }
283 }
284
285 /* is constraint of type SCIP_CONSTYPE_{EQKNAPSACK, BINPACKING, KNAPSACK}? */
286 /* @todo If coefficients or rhs are not integral, we currently do not check
287 * if the constraint could be scaled (finitely), such that they are.
288 */
289 {
290 SCIP_Real b;
291 SCIP_Bool unmatched;
292
293 b = rhs;
294 unmatched = FALSE;
295 for( i = 0; i < nvars && !unmatched; i++ )
296 {
297 unmatched = unmatched || SCIPvarGetType(vars[i]) == SCIP_VARTYPE_CONTINUOUS;
298 unmatched = unmatched || SCIPisLE(origprob, SCIPvarGetLbGlobal(vars[i]), -1.0);
299 unmatched = unmatched || SCIPisGE(origprob, SCIPvarGetUbGlobal(vars[i]), 2.0);
300 unmatched = unmatched || !SCIPisIntegral(origprob, vals[i]);
301
302 if( SCIPisNegative(origprob, vals[i]) )
303 b -= vals[i];
304 }
305 unmatched = unmatched || !detprobdata->isFiniteNonnegativeIntegral(b);
306
307 if( !unmatched )
308 {
309 if( SCIPisEQ(origprob, lhs, rhs) )
310 {
311 SCIPdebugMsg(origprob, "classified as EQKNAPSACK: ");
313 nfoundconstypesrangeddoublecount[SCIP_CONSTYPE_EQKNAPSACK] += 1 ;
314 nfoundconstypesrangedsinglecount[SCIP_CONSTYPE_EQKNAPSACK]++;
315 classforcons[c] = SCIP_CONSTYPE_EQKNAPSACK;
318 continue;
319 }
320 else
321 {
322 SCIP_Bool matched;
323
324 matched = FALSE;
325 for( i = 0; i < nvars && !matched; i++ )
326 {
327 matched = matched || SCIPisEQ(origprob, b, REALABS(vals[i]));
328 }
329
330 SCIPdebugMsg(origprob, "classified as %s: ", matched ? "BINPACKING" : "KNAPSACK");
332 nfoundconstypesrangeddoublecount[matched ? SCIP_CONSTYPE_BINPACKING : SCIP_CONSTYPE_KNAPSACK] += 1 ;
333 nfoundconstypesrangedsinglecount[matched ? SCIP_CONSTYPE_BINPACKING : SCIP_CONSTYPE_KNAPSACK]++;
334 classforcons[c] = matched ? SCIP_CONSTYPE_BINPACKING : SCIP_CONSTYPE_KNAPSACK;
335
336 }
337
338 if( SCIPisInfinity(origprob, -lhs) )
339 {
342 continue;
343 }
344 else
345 rhs = SCIPinfinity(origprob);
346 }
347 }
348
349 /* is constraint of type SCIP_CONSTYPE_{INTKNAPSACK}? */
350 {
351 SCIP_Real b;
352 SCIP_Bool unmatched;
353
354 unmatched = FALSE;
355
356 b = rhs;
357 unmatched = unmatched || !detprobdata->isFiniteNonnegativeIntegral(b);
358
359 for( i = 0; i < nvars && !unmatched; i++ )
360 {
361 unmatched = unmatched || SCIPvarGetType(vars[i]) == SCIP_VARTYPE_CONTINUOUS;
362 unmatched = unmatched || SCIPisNegative(origprob, SCIPvarGetLbGlobal(vars[i]));
363 unmatched = unmatched || !SCIPisIntegral(origprob, vals[i]);
364 unmatched = unmatched || SCIPisNegative(origprob, vals[i]);
365 }
366
367 if( !unmatched )
368 {
369 SCIPdebugMsg(origprob, "classified as INTKNAPSACK: ");
371 nfoundconstypesrangeddoublecount[SCIP_CONSTYPE_INTKNAPSACK] += 1 ;
372 nfoundconstypesrangedsinglecount[SCIP_CONSTYPE_INTKNAPSACK]++;
373 classforcons[c] = SCIP_CONSTYPE_INTKNAPSACK;
374
375 if( SCIPisInfinity(origprob, -lhs) )
376 {
379 continue;
380 }
381 else
382 rhs = SCIPinfinity(origprob);
383 }
384 }
385
386 /* is constraint of type SCIP_CONSTYPE_{MIXEDBINARY}? */
387 {
388 SCIP_Bool unmatched;
389
390 unmatched = FALSE;
391 for( i = 0; i < nvars && !unmatched; i++ )
392 {
396 unmatched = TRUE;
397 }
398
399 if( !unmatched )
400 {
401 SCIPdebugMsg(origprob, "classified as MIXEDBINARY (%d): ", detprobdata->isRangedRow(lhs, rhs) ? 2 : 1);
403 nfoundconstypesrangeddoublecount[SCIP_CONSTYPE_MIXEDBINARY] += 1 ;
404 nfoundconstypesrangedsinglecount[SCIP_CONSTYPE_MIXEDBINARY]++;
405 classforcons[c] = SCIP_CONSTYPE_MIXEDBINARY;
408 continue;
409
410 }
411 }
412
413 /* no special structure detected */
414 SCIPdebugMsg(origprob, "classified as GENERAL: ");
416 nfoundconstypesrangeddoublecount[SCIP_CONSTYPE_GENERAL] += 1 ;
417 nfoundconstypesrangedsinglecount[SCIP_CONSTYPE_GENERAL]++;
418 classforcons[c] = SCIP_CONSTYPE_GENERAL;
421 }
422
423 classifier = new gcg::ConsPartition(gcg, "constypes according to miplib", (int) SCIP_CONSTYPE_GENERAL + 1, detprobdata->getNConss() );
424
425 /* set class names and descriptions of every class */
426 for( int c = 0; c < classifier->getNClasses(); ++ c )
427 {
428 std::string name;
429 std::stringstream text;
430 switch( c )
431 {
432 case (int) SCIP_CONSTYPE_EMPTY:
433 name = "empty";
434 break;
436 name = "free";
437 break;
439 name = "singleton";
440 break;
442 name = "aggregation";
443 break;
445 name = "varbound";
446 break;
448 name = "setpartition";
449 break;
451 name = "setpacking";
452 break;
454 name = "setcovering";
455 break;
457 name = "cardinality";
458 break;
460 name = "invknapsack";
461 break;
463 name = "eqknapsack";
464 break;
466 name = "binpacking";
467 break;
469 name = "knapsack";
470 break;
472 name = "intknapsack";
473 break;
475 name = "mixed binary";
476 break;
478 name = "general";
479 break;
480 default:
481 name = "unknown";
482 break;
483 }
484
485
486#ifdef WRITE_ORIG_CONSTYPES
487 myfile << " " << nfoundconstypesrangeddoublecount[c] << ",";
488#endif
489
490 classifier->setClassName( c, name.c_str() );
491 text << "This class contains all constraints that are of (miplib) constype \"" << name << "\".";
492 classifier->setClassDescription( c, text.str().c_str() );
493 }
494
495#ifdef WRITE_ORIG_CONSTYPES
496 myfile << std::endl;
497 myfile.close();
498#endif
499
500
501
502 for( int i = 0; i < classifier->getNConss(); ++ i )
503 {
504 classifier->assignConsToClass( i, classforcons[i] );
505 }
506
507
508
509 classifier->removeEmptyClasses();
510 SCIPverbMessage(origprob, SCIP_VERBLEVEL_HIGH, NULL, " Consclassifier \"%s\" yields a classification with %d different constraint classes \n", classifier->getName(), classifier->getNClasses() );
511
512 detprobdata->addConsPartition(classifier);
513 return SCIP_OKAY;
514}
515
516/*
517 * classifier specific interface methods
518 */
519
521 GCG* gcg /**< GCG data structure */
522 )
523{
524 GCG_CLASSIFIERDATA* classifierdata = NULL;
525
526 SCIP_CALL(
528 classifierFree, classifierClassify));
529
530 return SCIP_OKAY;
531}
SCIP_VAR ** b
class representing a partition of a set of constraints
class storing partialdecs and the problem matrix
@ SCIP_CONSTYPE_GENERAL
@ SCIP_CONSTYPE_SETPARTITION
@ SCIP_CONSTYPE_EMPTY
@ SCIP_CONSTYPE_AGGREGATION
@ SCIP_CONSTYPE_SETPACKING
@ SCIP_CONSTYPE_EQKNAPSACK
@ SCIP_CONSTYPE_BINPACKING
@ SCIP_CONSTYPE_CARDINALITY
@ SCIP_CONSTYPE_INTKNAPSACK
@ SCIP_CONSTYPE_MIXEDBINARY
@ SCIP_CONSTYPE_KNAPSACK
@ SCIP_CONSTYPE_FREE
@ SCIP_CONSTYPE_SETCOVERING
@ SCIP_CONSTYPE_SINGLETON
@ SCIP_CONSTYPE_VARBOUND
@ SCIP_CONSTYPE_INVKNAPSACK
GCG_EXPORT void assignConsToClass(int consindex, int classindex)
GCG_EXPORT int getNConss()
returns the number of variables considered in the detprobdata
GCG_EXPORT SCIP_Bool isRangedRow(SCIP_Real lhs, SCIP_Real rhs)
is constraint ranged row, i.e., -inf < lhs < rhs < inf?
GCG_EXPORT SCIP_Bool isFiniteNonnegativeIntegral(SCIP_Real x)
is constraint ranged row, i.e., -inf < lhs < rhs < inf?
GCG_EXPORT void addConsPartition(ConsPartition *partition)
adds a constraint partition if it is no duplicate of an existing constraint partition
GCG_EXPORT SCIP_CONS * getCons(int consIndex)
returns the SCIP constraint related to a constraint index
GCG_EXPORT const char * getName()
GCG_EXPORT void setClassDescription(int classindex, const char *desc)
GCG_EXPORT int removeEmptyClasses()
GCG_EXPORT void setClassName(int classindex, const char *name)
#define CLSCONS_DESC
#define classifierFree
#define CLSCONS_NAME
#define CLSCONS_ENABLED
#define CLSCONS_PRIORITY
SCIP_RETCODE GCGincludeConsClassifierMiplibConstypes(GCG *gcg)
classifies constraints according to their miplib constypes
DETPROBDATA * GCGconshdlrDecompGetDetprobdataPresolved(GCG *gcg)
help method to access detprobdata for transformed problem
DETPROBDATA * GCGconshdlrDecompGetDetprobdataOrig(GCG *gcg)
help method to access detprobdata for unpresolved problem
SCIP_RETCODE GCGincludeConsClassifier(GCG *gcg, const char *name, const char *description, int priority, SCIP_Bool enabled, GCG_CLASSIFIERDATA *classifierdata, GCG_DECL_FREECONSCLASSIFIER((*freeClassifier)),)
includes one constraint classifier
constraint handler for structure detection
C++ interface of cons_decomp.
#define SCIP_Bool
#define SCIP_Real
#define SCIP_CALL_ABORT(x)
#define REALABS(x)
#define TRUE
#define FALSE
SCIP * GCGgetOrigprob(GCG *gcg)
Definition gcg.cpp:87
SCIP_Real GCGconsGetRhs(SCIP *scip, SCIP_CONS *cons)
Definition scip_misc.c:109
SCIP_RETCODE GCGconsGetVars(SCIP *scip, SCIP_CONS *cons, SCIP_VAR **vars, int nvars)
Definition scip_misc.c:487
SCIP_Real GCGconsGetLhs(SCIP *scip, SCIP_CONS *cons)
Definition scip_misc.c:207
SCIP_RETCODE GCGconsGetVals(SCIP *scip, SCIP_CONS *cons, SCIP_Real *vals, int nvals)
Definition scip_misc.c:618
int GCGconsGetNVars(SCIP *scip, SCIP_CONS *cons)
Definition scip_misc.c:433
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
#define SCIPdebugMsg
#define SCIPallocBufferArray(scip, ptr, num)
SCIP_Real SCIPinfinity(SCIP *scip)
SCIP_Bool SCIPisGE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisIntegral(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisLE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisInfinity(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisNegative(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisZero(SCIP *scip, SCIP_Real val)
SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
static assert(heur !=NULL)
SCIP_CALL(SCIPgetExternBranchCands(origprob, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL, NULL, NULL))
SCIP * origprob
SCIP_VAR ** vars
int i
int nvars
SCIPfreeBufferArray(scip, &cycles)
#define NULL
#define SCIPdebugPrintCons(x, y, z)
various SCIP helper methods
struct GCG_ClassifierData GCG_CLASSIFIERDATA
struct SCIP_Cons SCIP_CONS
#define GCG_DECL_CONSCLASSIFY(x)
struct Gcg GCG
Definition type_gcg.h:44
SCIP_VERBLEVEL_HIGH
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
struct SCIP_Var SCIP_VAR
SCIP_VARTYPE_CONTINUOUS