Scippy

GCG

Branch-and-Price & Column Generation for Everyone

clscons_gamssymbol.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_gamssymbol.cpp
30 *
31 * @brief gamssymbol constraint classifier (classifies by corresponding GAMS symbol)
32 * @author Stefanie Koß
33 */
34
35/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
36#define SCIP_DEBUG
37
39#include "gcg/cons_decomp.h"
40#include "gcg/cons_decomp.hpp"
41#include <vector>
42#include <string>
43#include <map>
44#include <stdio.h>
45#include <sstream>
46
48
50#include "gcg/scip_misc.h"
51
52/* classifier properties */
53#define CLSCONS_NAME "gamssymbol" /**< name of classifier */
54#define CLSCONS_DESC "symbol in GAMS file" /**< short description of classification*/
55#define CLSCONS_PRIORITY 0
56
57#define CLSCONS_ENABLED TRUE
58
59
60/*
61 * Data structures
62 */
64{
65 std::map<std::string, int>* constosymbol; /**< maps constraint name to the corresponding symbol index */
66};
67
68/*
69 * Local methods
70 */
71
72/* put your local methods here, and declare them static */
73
74
75/*
76 * classifier callback methods
77 */
78
79/** destructor of classifier to free user data (called when GCG is exiting) */
80static
82{
83 GCG_CLASSIFIERDATA* classifierdata;
84 SCIP* origprob = GCGgetOrigprob(gcg);
85
86 assert(origprob != NULL);
87
88 classifierdata = GCGconsClassifierGetData(classifier);
89 assert(classifierdata != NULL);
90 assert(strcmp(GCGconsClassifierGetName(classifier), CLSCONS_NAME) == 0);
91
92 delete classifierdata->constosymbol;
93
94 SCIPfreeBlockMemory(origprob, &classifierdata);
95
96 return SCIP_OKAY;
97}
98
99static
100GCG_DECL_CONSCLASSIFY(classifierClassify) {
101 gcg::DETPROBDATA* detprobdata;
102 SCIP* origprob = GCGgetOrigprob(gcg);
103 if( transformed )
104 {
106 }
107 else
108 {
110 }
111
112 int ncons = detprobdata->getNConss();
113 std::vector<int> nconssForClass( 0 ); // [i] holds number of constraints for class i
114 std::vector<int> symbolidxForClass( 0 ); // [i] holds symbol index for class i
115 std::vector<int> classForCons( ncons, - 1 ); // [i] holds class index for constraint i -> indexing over detection internal constraint array!
116 int counterClasses = 0;
117
119 assert(classifier != NULL);
120
121 GCG_CLASSIFIERDATA* classdata = GCGconsClassifierGetData(classifier);
122 assert(classdata != NULL);
123
124 /* firstly, assign all constraints to classindices */
125 // iterate over constraints in detection and lookup in classdata->constosymbol
126 // iterating over classdata->constosymbol and lookup constraints with getIndexForCons fails with assertion if constraint is not found -> should return error value?
127 for( int consid = 0; consid < detprobdata->getNConss(); ++ consid )
128 {
129 // int consid = detprobdata->getIndexForCons(iter.second);
130 SCIP_CONS* cons = detprobdata->getCons(consid);
131 std::string consname = std::string( SCIPconsGetName( cons ) );
132
133 auto symbolidxiter = classdata->constosymbol->find(consname);
134 int symbolidx;
135 if( symbolidxiter != classdata->constosymbol->end() )
136 {
137 symbolidx = symbolidxiter->second;
138 }
139 else
140 {
141 symbolidx = -1;
142 }
143
144 bool classfound = false;
145
146 /* check if class for symbol index exists */
147 for( size_t classid = 0; classid < symbolidxForClass.size(); ++classid )
148 {
149 if( symbolidx == symbolidxForClass[classid] )
150 {
151 classfound = true;
152 classForCons[consid] = (int) classid;
153 ++nconssForClass[classid];
154 break;
155 }
156 }
157
158 /* if not, create a new class index */
159 if( !classfound )
160 {
161 classForCons[consid] = counterClasses;
162 ++counterClasses;
163 symbolidxForClass.push_back( symbolidx );
164 nconssForClass.push_back( 1 );
165 }
166 }
167 assert( counterClasses == (int) symbolidxForClass.size() );
168
169 /* secondly, use these information to create a ConsPartition */
170 gcg::ConsPartition* partition = new gcg::ConsPartition(gcg, "gamssymbols", counterClasses, detprobdata->getNConss() );
171
172 /* set class names and descriptions of every class */
173 for( int c = 0; c < partition->getNClasses(); ++ c )
174 {
175 std::stringstream text;
176 text << symbolidxForClass[c];
177 partition->setClassName( c, text.str().c_str() );
178 text.str( "" );
179 text.clear();
180 text << "This class contains all constraints with gams symbol index" << symbolidxForClass[c] << ".";
181 partition->setClassDescription( c, text.str().c_str() );
182 }
183
184 /* copy the constraint assignment information found in first step */
185 for( int i = 0; i < partition->getNConss(); ++ i )
186 {
187 partition->assignConsToClass( i, classForCons[i] );
188 }
189 SCIPverbMessage(origprob, SCIP_VERBLEVEL_HIGH, NULL, " Consclassifier \"%s\" yields a classification with %d different constraint classes \n", partition->getName(), partition->getNClasses() );
190
191 detprobdata->addConsPartition(partition);
192 return SCIP_OKAY;
193}
194
195/*
196 * classifier specific interface methods
197 */
198
199/** adds an entry to clsdata->constosymbol */
201 GCG_CONSCLASSIFIER* classifier,
202 SCIP_CONS* cons,
203 int symbolIdx
204 )
205{
206 assert(classifier != NULL);
207 GCG_CLASSIFIERDATA* classdata = GCGconsClassifierGetData(classifier);
208 assert(classdata != NULL);
209
210 std::string consname = SCIPconsGetName( cons );
211 classdata->constosymbol->insert({consname, symbolIdx});
212
213 return SCIP_OKAY;
214}
215
216/** creates the handler for gamssymbol classifier and includes it in SCIP */
218 GCG* gcg /**< GCG data structure */
219 )
220{
221 GCG_CLASSIFIERDATA* classifierdata = NULL;
222 SCIP* origprob = GCGgetOrigprob(gcg);
223
224 SCIP_CALL( SCIPallocBlockMemory(origprob, &classifierdata) );
225 assert(classifierdata != NULL);
226 classifierdata->constosymbol = new std::map<std::string, int>();
227
229
230 return SCIP_OKAY;
231}
static SCIP_RETCODE partition(SCIP *scip, SCIP_VAR **J, int *Jsize, SCIP_Longint *priority, SCIP_VAR **F, int Fsize, SCIP_VAR **origvar, SCIP_Real *median)
class representing a partition of a set of constraints
class storing partialdecs and the problem matrix
GCG_EXPORT int getNConss()
returns the number of variables considered in the detprobdata
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
#define classifierFree
#define CLSCONS_DESC
#define CLSCONS_NAME
#define CLSCONS_ENABLED
#define CLSCONS_PRIORITY
static GCG_DECL_CONSCLASSIFY(classifierClassify)
static GCG_DECL_FREECONSCLASSIFIER(classifierFree)
SCIP_RETCODE GCGconsClassifierGamssymbolAddEntry(GCG_CONSCLASSIFIER *classifier, SCIP_CONS *cons, int symbolIdx)
SCIP_RETCODE GCGincludeConsClassifierGamssymbol(GCG *gcg)
Constraints which have the same symbol are put into same class.
GCG_CONSCLASSIFIER * GCGfindConsClassifier(GCG *gcg, const char *name)
searches for the consclassifier with the given name and returns it or NULL if classifier is not found
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)),)
constraint handler for structure detection
C++ interface of cons_decomp.
#define SCIP_CALL(x)
SCIP * GCGgetOrigprob(GCG *gcg)
Definition: gcg.cpp:87
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
GCG_CLASSIFIERDATA * GCGconsClassifierGetData(GCG_CONSCLASSIFIER *consClassifier)
Definition: clscons.c:46
const char * GCGconsClassifierGetName(GCG_CONSCLASSIFIER *consClassifier)
Definition: clscons.c:67
const char * SCIPconsGetName(SCIP_CONS *cons)
#define SCIPfreeBlockMemory(scip, ptr)
#define SCIPallocBlockMemory(scip, ptr)
#define NULL
various SCIP helper methods
std::map< std::string, int > * constosymbol
Definition: struct_gcg.h:55
SCIP_VERBLEVEL_HIGH
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE