GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
clsvar_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 clsvar_gamsdomain.cpp
30 *
31 * @brief Classifies by domains from which variables 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
38#include "gcg/cons_decomp.h"
39#include "gcg/cons_decomp.hpp"
40#include <map>
41#include <set>
42#include <stdio.h>
43#include <sstream>
44#include <vector>
45
47
49#include "gcg/scip_misc.h"
50
51/* classifier properties */
52#define CLSVAR_NAME "gamsdomain" /**< name of classifier */
53#define CLSVAR_DESC "domain in gams file" /**< short description of classification */
54#define CLSVAR_PRIORITY 0 /**< priority of this classifier */
55
56#define CLSVAR_ENABLED TRUE
57
58
59/*
60 * Data structures
61 */
62
63/** classifier handler data */
65{
66 std::map<std::string, std::set<int>>* vartodomain; /**< maps variable name to the corresponding set of domain indices */
67};
68
69/*
70 * Local methods
71 */
72
73/* put your local methods here, and declare them static */
74
75
76/*
77 * classifier callback methods
78 */
79
80/** destructor of classifier to free user data (called when GCG is exiting) */
81static
83{
84 GCG_CLASSIFIERDATA* classifierdata = GCGvarClassifierGetData(classifier);
85 assert(classifierdata != NULL);
86 assert(strcmp(GCGvarClassifierGetName(classifier), CLSVAR_NAME) == 0);
87
88 delete classifierdata->vartodomain;
89
90 SCIPfreeMemory(GCGgetOrigprob(gcg), &classifierdata);
91
92 return SCIP_OKAY;
93}
94
95static
96GCG_DECL_VARCLASSIFY(classifierClassify)
97{
98 gcg::DETPROBDATA* detprobdata;
99
100 if( transformed )
101 {
103 }
104 else
105 {
107 }
108
109 int nvar = detprobdata->getNVars();
110 std::vector<int> nvarsForClass( 0 ); // [i] holds number of variables for class i
111 std::vector<std::set<int>> domainForClass( 0 ); // [i] holds domain for class i
112 std::vector<int> classForVar( nvar, - 1 ); // [i] holds class index for variable i -> indexing over detection internal variable array!
113 int counterClasses = 0;
114
116 assert(classifier != NULL);
117
118 GCG_CLASSIFIERDATA* classdata = GCGvarClassifierGetData(classifier);
119 assert(classdata != NULL);
120
121 /* firstly, assign all variables to classindices */
122 // iterate over variables in detection and lookup in classdata->vartodomain
123 // iterating over classdata->vartosymbol and lookup variables with getIndexForVar fails with assertion if variable is not found -> should return error value?
124 for( int varid = 0; varid < detprobdata->getNVars(); ++ varid )
125 {
126 SCIP_VAR* var = detprobdata->getVar(varid);
127 std::string varname = std::string( SCIPvarGetName( var ) );
128 auto domainiter = classdata->vartodomain->find(varname);
129 std::set<int> domain;
130 if( domainiter != classdata->vartodomain->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 classForVar[varid] = (int) classid;
149 ++nvarsForClass[classid];
150 break;
151 }
152 }
153
154 /* if not, create a new class index */
155 if( !classfound )
156 {
157 classForVar[varid] = counterClasses;
158 ++counterClasses;
159 domainForClass.push_back( domain );
160 nvarsForClass.push_back( 1 );
161 }
162 }
163 assert( counterClasses == (int) domainForClass.size() );
164
165 /* secondly, use these information to create a ConsPartition */
166 gcg::VarPartition* partition = new gcg::VarPartition(gcg, "gamsdomain", counterClasses, detprobdata->getNVars() );
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 variables 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->getNVars(); ++ i )
188 {
189 partition->assignVarToClass( i, classForVar[i] );
190 }
191 SCIPverbMessage(GCGgetOrigprob(gcg), SCIP_VERBLEVEL_HIGH, NULL, " Varclassifier \"%s\" yields a classification with %d different variable classes \n", partition->getName(), partition->getNClasses() );
192
193 detprobdata->addVarPartition(partition);
194 return SCIP_OKAY;
195}
196
197/*
198 * classifier specific interface methods
199 */
200
201/** adds an entry to clsdata->vartodomain */
203 GCG_VARCLASSIFIER* classifier,
204 SCIP_VAR* var,
205 int symDomIdx[],
206 int* symDim
207 )
208{
209 assert(classifier != NULL);
210 GCG_CLASSIFIERDATA* classdata = GCGvarClassifierGetData(classifier);
211 assert(classdata != NULL);
212
213 std::string varname = SCIPvarGetName( var );
214 char varnametrans[SCIP_MAXSTRLEN];
215 (void) SCIPsnprintf(varnametrans, SCIP_MAXSTRLEN, "t_%s", varname.c_str());
216 std::string nametrans(varnametrans);
217 std::set<int> domainset;
218 for( int i = 0; i < *symDim; ++i)
219 {
220 domainset.insert(symDomIdx[i]);
221 }
222 classdata->vartodomain->insert({varname, domainset});
223 classdata->vartodomain->insert({varnametrans, domainset});
224
225 return SCIP_OKAY;
226}
227
228/** creates the handler for gamsdomain classifier and includes it in SCIP */
230 GCG* gcg /**< GCG data structure */
231 )
232{
233 GCG_CLASSIFIERDATA* classifierdata = NULL;
234
235 SCIP_CALL( SCIPallocMemory(GCGgetOrigprob(gcg), &classifierdata) );
236 assert(classifierdata != NULL);
237 classifierdata->vartodomain = new std::map<std::string, std::set<int>>();
238
240
241 return SCIP_OKAY;
242}
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 storing partialdecs and the problem matrix
class representing a partition of a set of variables
GCG_EXPORT int getNVars()
return the number of variables considered in the detprobdata
GCG_EXPORT void addVarPartition(VarPartition *partition)
adds a variable partition if it is no duplicate of an existing variable partition
GCG_EXPORT SCIP_VAR * getVar(int varIndex)
returns SCIP variable related to a variable index
GCG_EXPORT const char * getName()
GCG_EXPORT void setClassDescription(int classindex, const char *desc)
GCG_EXPORT void setClassName(int classindex, const char *name)
GCG_EXPORT void assignVarToClass(int varindex, int classindex)
GCG_EXPORT int getNVars()
#define classifierFree
SCIP_RETCODE GCGvarClassifierGamsdomainAddEntry(GCG_VARCLASSIFIER *classifier, SCIP_VAR *var, int symDomIdx[], int *symDim)
#define CLSVAR_ENABLED
#define CLSVAR_NAME
SCIP_RETCODE GCGincludeVarClassifierGamsdomain(GCG *gcg)
#define CLSVAR_PRIORITY
#define CLSVAR_DESC
Classifies by domains from which variables are created TODO: what is together in one class?
GCG_VARCLASSIFIER * GCGfindVarClassifier(GCG *gcg, const char *name)
searches for the varclassifier 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
SCIP_RETCODE GCGincludeVarClassifier(GCG *gcg, const char *name, const char *description, int priority, SCIP_Bool enabled, GCG_CLASSIFIERDATA *classifierdata, GCG_DECL_FREEVARCLASSIFIER((*freeClassifier)),)
includes one variable classifier
DETPROBDATA * GCGconshdlrDecompGetDetprobdataOrig(GCG *gcg)
help method to access detprobdata for unpresolved problem
constraint handler for structure detection
C++ interface of cons_decomp.
#define SCIP_MAXSTRLEN
SCIP * GCGgetOrigprob(GCG *gcg)
Definition gcg.cpp:87
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
#define SCIPallocMemory(scip, ptr)
GCG_CLASSIFIERDATA * GCGvarClassifierGetData(GCG_VARCLASSIFIER *varClassifier)
Definition clsvar.c:46
const char * GCGvarClassifierGetName(GCG_VARCLASSIFIER *varClassifier)
Definition clsvar.c:67
const char * SCIPvarGetName(SCIP_VAR *var)
int SCIPsnprintf(char *t, int len, const char *s,...)
SCIPfreeMemory(GCGgetOrigprob(gcg), &divingdata)
static assert(heur !=NULL)
SCIP_CALL(SCIPgetExternBranchCands(origprob, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL, NULL, NULL))
SCIP_VAR * var
int i
#define NULL
various SCIP helper methods
std::map< std::string, std::set< int > > * vartodomain
struct GCG_ClassifierData GCG_CLASSIFIERDATA
struct Gcg GCG
Definition type_gcg.h:44
SCIP_VERBLEVEL_HIGH
enum SCIP_Retcode SCIP_RETCODE
struct SCIP_Var SCIP_VAR
#define GCG_DECL_FREEVARCLASSIFIER(x)
#define GCG_DECL_VARCLASSIFY(x)
struct GCG_VarClassifier GCG_VARCLASSIFIER