GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
clscons_gamsdomain.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_gamsdomain.cpp
30 *
31 * @brief Classifies by domains from which constraints are created TODO: what is together in one class?
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 <set>
44#include <stdio.h>
45#include <sstream>
46
48
50#include "gcg/scip_misc.h"
51
52/* classifier properties */
53#define CLSCONS_NAME "gamsdomain" /**< name of classifier */
54#define CLSCONS_DESC "domain 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, std::set<int>>* constodomain; /**< maps constraint name to the corresponding set of domain indices */
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
85 classifierdata = GCGconsClassifierGetData(classifier);
86 assert(classifierdata != NULL);
87 assert(strcmp(GCGconsClassifierGetName(classifier), CLSCONS_NAME) == 0);
88
89 delete classifierdata->constodomain;
90
91 SCIPfreeMemory(GCGgetOrigprob(gcg), &classifierdata);
92
93 return SCIP_OKAY;
94}
95
96static
97GCG_DECL_CONSCLASSIFY(classifierClassify) {
98 gcg::DETPROBDATA* detprobdata;
99
100 if( transformed )
101 {
103 }
104 else
105 {
107 }
108
109 int ncons = detprobdata->getNConss();
110 std::vector<int> nconssForClass( 0 ); // [i] holds number of constraints for class i
111 std::vector<std::set<int>> domainForClass( 0 ); // [i] holds domain for class i
112 std::vector<int> classForCons( ncons, - 1 ); // [i] holds class index for constraint i -> indexing over detection internal constraint array!
113 int counterClasses = 0;
114
116 assert(classifier != NULL);
117
118 GCG_CLASSIFIERDATA* classdata = GCGconsClassifierGetData(classifier);
119 assert(classdata != NULL);
120
121 /* firstly, assign all constraints to classindices */
122 // iterate over constraints in detection and lookup in classdata->constodomain
123 for( int consid = 0; consid < detprobdata->getNConss(); ++ consid )
124 {
125 SCIP_CONS* cons = detprobdata->getCons(consid);
126 std::string consname = std::string( SCIPconsGetName( cons ) );
127
128 auto domainiter = classdata->constodomain->find(consname);
129 std::set<int> domain;
130 if( domainiter != classdata->constodomain->end() )
131 {
132 domain = domainiter->second;
133 }
134 else
135 {
136 domain = std::set<int>();
137 domain.insert(-1);
138 }
139
140 bool classfound = false;
141
142 /* check if class for domain exists */
143 for( size_t classid = 0; classid < domainForClass.size(); ++classid )
144 {
145 if( domain == domainForClass[classid] )
146 {
147 classfound = true;
148 classForCons[consid] = (int) classid;
149 ++nconssForClass[classid];
150 break;
151 }
152 }
153
154 /* if not, create a new class index */
155 if( !classfound )
156 {
157 classForCons[consid] = counterClasses;
158 ++counterClasses;
159 domainForClass.push_back( domain );
160 nconssForClass.push_back( 1 );
161 }
162 }
163 assert( counterClasses == (int) domainForClass.size() );
164
165 /* secondly, use these information to create a ConsPartition */
166 gcg::ConsPartition* partition = new gcg::ConsPartition(gcg, "gamsdomain", counterClasses, detprobdata->getNConss() );
167
168 /* set class names and descriptions of every class */
169 for( int c = 0; c < partition->getNClasses(); ++ c )
170 {
171 std::stringstream text;
172 text << "{";
173 for( auto iter : domainForClass[c] )
174 {
175 text << iter;
176
177 }
178 text << "}";
179 partition->setClassName( c, text.str().c_str() );
180 text.str( "" );
181 text.clear();
182 //text << "This class contains all constraints with gams domain" << domainForClass[c] << ".";
183 partition->setClassDescription( c, text.str().c_str() );
184 }
185
186 /* copy the constraint assignment information found in first step */
187 for( int i = 0; i < partition->getNConss(); ++ i )
188 {
189 partition->assignConsToClass( i, classForCons[i] );
190 }
191 SCIPverbMessage(GCGgetOrigprob(gcg), SCIP_VERBLEVEL_HIGH, NULL, " Consclassifier \"%s\" yields a classification with %d different constraint classes \n", partition->getName(), partition->getNClasses() );
192
193 detprobdata->addConsPartition(partition);
194
195 return SCIP_OKAY;
196}
197
198/*
199 * classifier specific interface methods
200 */
201
202/** adds an entry to clsdata->constodomain */
204 GCG_CONSCLASSIFIER* classifier,
205 SCIP_CONS* cons,
206 int symDomIdx[],
207 int* symDim
208 )
209{
210 assert(classifier != NULL);
211 GCG_CLASSIFIERDATA* classdata = GCGconsClassifierGetData(classifier);
212 assert(classdata != NULL);
213
214 std::string consname = SCIPconsGetName( cons );
215 std::set<int> domainset;
216 for( int i = 0; i < *symDim; ++i)
217 {
218 domainset.insert(symDomIdx[i]);
219 }
220 classdata->constodomain->insert({consname, domainset});
221
222 return SCIP_OKAY;
223}
224
225/** creates the handler for gamsdomain classifier and includes it in SCIP */
227 GCG* gcg /**< GCG data structure */
228 )
229{
230 GCG_CLASSIFIERDATA* classifierdata = NULL;
231
232 SCIP_CALL( SCIPallocMemory(origprob, &classifierdata) );
233 assert(classifierdata != NULL);
234 classifierdata->constodomain = new std::map<std::string, std::set<int>>();
235
237
238 return SCIP_OKAY;
239}
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 void assignConsToClass(int consindex, int classindex)
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
GCG_EXPORT const char * getName()
GCG_EXPORT void setClassDescription(int classindex, const char *desc)
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 GCGconsClassifierGamsdomainAddEntry(GCG_CONSCLASSIFIER *classifier, SCIP_CONS *cons, int symDomIdx[], int *symDim)
SCIP_RETCODE GCGincludeConsClassifierGamsdomain(GCG *gcg)
Classifies by domains from which constraints are created TODO: what is together in one 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)),)
includes one constraint classifier
constraint handler for structure detection
C++ interface of cons_decomp.
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 SCIPallocMemory(scip, ptr)
SCIPfreeMemory(GCGgetOrigprob(gcg), &divingdata)
static assert(heur !=NULL)
SCIP_CALL(SCIPgetExternBranchCands(origprob, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL, NULL, NULL))
SCIP * origprob
int i
#define NULL
various SCIP helper methods
std::map< std::string, std::set< int > > * constodomain
struct GCG_ClassifierData GCG_CLASSIFIERDATA
struct SCIP_Cons SCIP_CONS
struct GCG_ConsClassifier GCG_CONSCLASSIFIER
#define GCG_DECL_CONSCLASSIFY(x)
#define GCG_DECL_FREECONSCLASSIFIER(x)
struct Gcg GCG
Definition type_gcg.h:44
SCIP_VERBLEVEL_HIGH
enum SCIP_Retcode SCIP_RETCODE