GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
clscons_consnamelevenshtein.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_consnamelevenshtein.cpp
30 *
31 * @brief classifies constraints according to levenshtein distance graph of their names
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#include <queue>
43
45
47#include "gcg/scip_misc.h"
48
49/* classifier properties */
50#define CLSCONS_NAME "consnamelevenshtein" /**< name of classifier */
51#define CLSCONS_DESC "constraint names (according to levenshtein distance graph)" /**< short description of classification*/
52#define CLSCONS_PRIORITY 0
53
54#define CLSCONS_ENABLED FALSE
55
56
57/*
58 * Data structures
59 */
60
61/** classifier handler data */
63{
64};
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) */
79#define classifierFree NULL
80
81/** returns levenshtein distance between two strings */
83 std::string s,
84 std::string t
85 )
86{
87 /* easy cases */
88 if( s.compare( t ) == 0 )
89 return 0;
90 if( s.length() == 0 )
91 return (int) t.length();
92 if( t.length() == 0 )
93 return (int) s.length();
94
95 /* vectors to store integer distances */
96 std::vector<int> prev( t.length() + 1 );
97 std::vector<int> curr( t.length() + 1 );
98
99 /* initialize prev (previous row of distances) */
100 for( size_t i = 0; i < prev.size(); ++i )
101 {
102 prev[i] = (int) i;
103 }
104 for( size_t i = 0; i < s.length(); ++i )
105 {
106 /* calculate curr (row distances) from the previous one */
107
108 curr[0] = (int) i + 1;
109
110 /* fill remaining of row using 'Bellman' equality */
111 for( size_t j = 0; j < t.length(); ++j )
112 {
113 int cost = ( s[i] == t[j] ) ? 0 : 1;
114 curr[j + 1] = std::min( curr[j] + 1, std::min( prev[j + 1] + 1, prev[j] + cost ) );
115 }
116
117 /* copy curr to prev for next iteration */
118 for( size_t j = 0; j < prev.size(); ++j )
119 prev[j] = curr[j];
120 }
121
122 return curr[t.length()];
123}
124
125
126
127static
128GCG_DECL_CONSCLASSIFY(classifierClassify) {
129 gcg::DETPROBDATA* detprobdata;
131 if( transformed )
132 {
134 }
135 else
136 {
138 }
139
140 std::vector < std::string > consnamesToCompare(detprobdata->getNConss(), "");
141 std::vector<int> nConssConstype;
142 std::vector<int> classForCons(detprobdata->getNConss(), - 1);
143 std::vector<bool> alreadyReached(detprobdata->getNConss(), false);
144 std::queue<int> helpqueue;
145 int nUnreachedConss = detprobdata->getNConss();
146 int currentClass = - 1;
147 int nmaxconss = 5000;
148 int connectivity = 1;
149
150 std::stringstream classifierName;
151 classifierName << "lev-dist-" << connectivity;
152 gcg::ConsPartition* classifier = new gcg::ConsPartition(gcg, classifierName.str().c_str(), 0, detprobdata->getNConss());
153
154 /* if number of conss exceeds this number, skip calculating such a classifier */
155 if( detprobdata->getNConss() > nmaxconss )
156 {
157
158 SCIPverbMessage(origprob, SCIP_VERBLEVEL_HIGH, NULL, " skipped levenshtein distance based constraint classes calculating since number of constraints %d exceeds limit %d \n", detprobdata->getNConss(), nmaxconss );
159 delete classifier;
160 return SCIP_ERROR;
161 }
162
163 std::vector<std::vector<int>> levenshteindistances(detprobdata->getNConss(), std::vector<int>( detprobdata->getNConss(), - 1 ));
164
165 /* read consnames */
166 for( int i = 0; i < detprobdata->getNConss(); ++ i )
167 {
168 consnamesToCompare[i] = std::string(SCIPconsGetName(detprobdata->getCons(i)));
169 }
170
171 /* calculate levenshtein distances pairwise */
172 for( int i = 0; i < detprobdata->getNConss(); ++ i )
173 {
174 for( int j = i + 1; j < detprobdata->getNConss(); ++ j )
175 {
176 levenshteindistances[i][j] = calcLevenshteinDistance(consnamesToCompare[i], consnamesToCompare[j]);
177 levenshteindistances[j][i] = levenshteindistances[i][j];
178 }
179 }
180
181 /* repeat doing breadth first search until every constraint is assigned to a class */
182 while( nUnreachedConss > 0 )
183 {
184 int firstUnreached = - 1;
185 currentClass ++;
186 assert( helpqueue.empty() );
187 for( int i = 0; i < detprobdata->getNConss(); ++ i )
188 {
189 if( classForCons[i] == - 1 )
190 {
191 firstUnreached = i;
192 break;
193 }
194 }
195
196 helpqueue.push(firstUnreached);
197 alreadyReached[firstUnreached] = true;
198 classForCons[firstUnreached] = currentClass;
199 -- nUnreachedConss;
200
201 /* consider all constraints which are connected to the current constraint by means of levenshtein distance */
202 while( ! helpqueue.empty() )
203 {
204 int nodecons = helpqueue.front();
205 helpqueue.pop();
206 for( int j = 0; j < detprobdata->getNConss(); ++ j )
207 {
208
209 if( alreadyReached[j] )
210 continue;
211
212 if( j == nodecons )
213 continue;
214
215 if( levenshteindistances[j][nodecons] > connectivity )
216 continue;
217
218 alreadyReached[j] = true;
219 classForCons[j] = currentClass;
220 -- nUnreachedConss;
221 helpqueue.push(j);
222 }
223 }
224
225 /* create a new class with found constraints in ConsPartition*/
226 std::stringstream text;
227 text << "This class contains all constraints with a name similar to \"" << consnamesToCompare[firstUnreached] << "\".";
228 classifier->addClass(consnamesToCompare[firstUnreached].c_str(), text.str().c_str(), gcg::BOTH);
229 }
230
231 /* assign constraint indices to classes */
232 for( int i = 0; i < detprobdata->getNConss(); ++ i )
233 {
234 classifier->assignConsToClass(i, classForCons[i]);
235 }
236
237 SCIPverbMessage(origprob, SCIP_VERBLEVEL_HIGH, NULL, " Consclassifier levenshtein: connectivity of %d yields a classification with %d different constraint classes. \n", connectivity, currentClass + 1);
238
239 detprobdata->addConsPartition(classifier);
240 return SCIP_OKAY;
241}
242
243/*
244 * classifier specific interface methods
245 */
246
248 GCG* gcg /**< GCG data structure */
249 )
250{
251 GCG_CLASSIFIERDATA* classifierdata = NULL;
252
253 SCIP_CALL(
255 classifierFree, classifierClassify));
256
257 return SCIP_OKAY;
258}
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 addClass(const char *name, const char *desc, CONS_DECOMPINFO decompInfo)
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 CLSCONS_DESC
#define classifierFree
#define CLSCONS_NAME
#define CLSCONS_ENABLED
#define CLSCONS_PRIORITY
SCIP_RETCODE GCGincludeConsClassifierConsnameLevenshtein(GCG *gcg)
int calcLevenshteinDistance(std::string s, std::string t)
classifies constraints according to levenshtein distance graph of their names
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,...)
const char * SCIPconsGetName(SCIP_CONS *cons)
static assert(heur !=NULL)
SCIP_CALL(SCIPgetExternBranchCands(origprob, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL, NULL, NULL))
SCIP * origprob
int j
int i
#define NULL
various SCIP helper methods
struct GCG_ClassifierData GCG_CLASSIFIERDATA
#define GCG_DECL_CONSCLASSIFY(x)
struct Gcg GCG
Definition type_gcg.h:44
SCIP_VERBLEVEL_HIGH
SCIP_ERROR
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP