GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
clsvar_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 clsvar_gamssymbol.cpp
30 *
31 * @brief variables which have the same symbol are put into same 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 <vector>
41#include <stdio.h>
42#include <sstream>
43
45
47#include "gcg/scip_misc.h"
48
49/* classifier properties */
50#define CLSVAR_NAME "gamssymbol" /**< name of classifier */
51#define CLSVAR_DESC "symbol in gams file" /**< short description of classification */
52#define CLSVAR_PRIORITY 0 /**< priority of this classifier */
53
54#define CLSVAR_ENABLED TRUE
55
56
57/*
58 * Data structures
59 */
60
61/** classifier handler data */
63{
64 std::map<std::string, int>* vartosymbol; /**< maps variable name to the corresponding symbol index */
65};
66
67/*
68 * Local methods
69 */
70
71/* put your local methods here, and declare them static */
72
73
74/*
75 * classifier callback methods
76 */
77
78/** destructor of classifier to free user data (called when GCG is exiting) */
79static
81{
82 GCG_CLASSIFIERDATA* classifierdata = GCGvarClassifierGetData(classifier);
83 assert(classifierdata != NULL);
84 assert(strcmp(GCGvarClassifierGetName(classifier), CLSVAR_NAME) == 0);
85
86 delete classifierdata->vartosymbol;
87
88 SCIPfreeMemory(GCGgetOrigprob(gcg), &classifierdata);
89
90 return SCIP_OKAY;
91}
92
93static
94GCG_DECL_VARCLASSIFY(classifierClassify)
95{
96 gcg::DETPROBDATA* detprobdata;
97
98 if( transformed )
99 {
101 }
102 else
103 {
105 }
106
107 int nvar = detprobdata->getNVars();
108 std::vector<int> nvarsForClass( 0 ); // [i] holds number of variables for class i
109 std::vector<int> symbolidxForClass( 0 ); // [i] holds symbol index for class i
110 std::vector<int> classForVar( nvar, - 1 ); // [i] holds class index for variable i -> indexing over detection internal variable array!
111 int counterClasses = 0;
112
114 assert(classifier != NULL);
115
116 GCG_CLASSIFIERDATA* classdata = GCGvarClassifierGetData(classifier);
117 assert(classdata != NULL);
118
119 /* firstly, assign all variables to classindices */
120 // iterate over variables in detection and lookup in classdata->vartosymbol
121 // iterating over classdata->vartosymbol and lookup variables with getIndexForVar fails with assertion if variable is not found -> should return error value?
122 for( int varid = 0; varid < detprobdata->getNVars(); ++ varid )
123 {
124 SCIP_VAR* var = detprobdata->getVar(varid);
125 std::string varname = std::string( SCIPvarGetName( var ) );
126 auto symbolidxiter = classdata->vartosymbol->find(varname);
127 int symbolidx;
128 if( symbolidxiter != classdata->vartosymbol->end() )
129 {
130 symbolidx = symbolidxiter->second;
131 }
132 else
133 {
134 symbolidx = -1;
135 }
136
137 bool classfound = false;
138
139 /* check if class for symbol index exists */
140 for( size_t classid = 0; classid < symbolidxForClass.size(); ++classid )
141 {
142 if( symbolidx == symbolidxForClass[classid] )
143 {
144 classfound = true;
145 classForVar[varid] = (int) classid;
146 ++nvarsForClass[classid];
147 break;
148 }
149 }
150
151 /* if not, create a new class index */
152 if( !classfound )
153 {
154 classForVar[varid] = counterClasses;
155 ++counterClasses;
156 symbolidxForClass.push_back( symbolidx );
157 nvarsForClass.push_back( 1 );
158 }
159 }
160 assert( counterClasses == (int) symbolidxForClass.size() );
161
162 /* secondly, use these information to create a ConsPartition */
163 gcg::VarPartition* partition = new gcg::VarPartition(gcg, "gamssymbols", counterClasses, detprobdata->getNVars() );
164
165 /* set class names and descriptions of every class */
166 for( int c = 0; c < partition->getNClasses(); ++ c )
167 {
168 std::stringstream text;
169 text << symbolidxForClass[c];
170 partition->setClassName( c, text.str().c_str() );
171 text.str( "" );
172 text.clear();
173 text << "This class contains all variables with gams symbol index" << symbolidxForClass[c] << ".";
174 partition->setClassDescription( c, text.str().c_str() );
175 }
176
177 /* copy the constraint assignment information found in first step */
178 for( int i = 0; i < partition->getNVars(); ++ i )
179 {
180 partition->assignVarToClass( i, classForVar[i] );
181 }
182 SCIPverbMessage(GCGgetOrigprob(gcg), SCIP_VERBLEVEL_HIGH, NULL, " Varclassifier \"%s\" yields a classification with %d different variable classes \n", partition->getName(), partition->getNClasses() );
183
184 detprobdata->addVarPartition(partition);
185 return SCIP_OKAY;
186}
187
188/*
189 * classifier specific interface methods
190 */
191
192// SHOW
193/** adds an entry to clsdata->vartosymbol */
195 GCG_VARCLASSIFIER* classifier,
196 SCIP_VAR* var,
197 int symbolIdx
198 )
199{
200 assert(classifier != NULL);
201 GCG_CLASSIFIERDATA* classdata = GCGvarClassifierGetData(classifier);
202 assert(classdata != NULL);
203
204 std::string varname = SCIPvarGetName(var);
205 char varnametrans[SCIP_MAXSTRLEN];
206 (void) SCIPsnprintf(varnametrans, SCIP_MAXSTRLEN, "t_%s", varname.c_str());
207 std::string nametrans(varnametrans);
208 classdata->vartosymbol->insert({varname, symbolIdx});
209 classdata->vartosymbol->insert({varnametrans, symbolIdx});
210
211 return SCIP_OKAY;
212}
213
214/** creates the handler for gamssymbol classifier and includes it in SCIP */
216 GCG* gcg /**< GCG data structure */
217 )
218{
219 GCG_CLASSIFIERDATA* classifierdata = NULL;
220
221 SCIP_CALL( SCIPallocMemory(GCGgetOrigprob(gcg), &classifierdata) );
222 assert(classifierdata != NULL);
223 classifierdata->vartosymbol = new std::map<std::string, int>();
224
226
227 return SCIP_OKAY;
228}
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
#define CLSVAR_ENABLED
#define CLSVAR_NAME
#define CLSVAR_PRIORITY
#define CLSVAR_DESC
SCIP_RETCODE GCGincludeVarClassifierGamssymbol(GCG *gcg)
SCIP_RETCODE GCGvarClassifierGamssymbolAddEntry(GCG_VARCLASSIFIER *classifier, SCIP_VAR *var, int symbolIdx)
variables which have the same symbol are put into same 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, int > * vartosymbol
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