Scippy

GCG

Branch-and-Price & Column Generation for Everyone

clscons_nnonzeros.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_nnonzeros.cpp
30 *
31 * @brief classifies constraints according to their nonzero entries
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
44
46#include "gcg/scip_misc.h"
47
48/* classifier properties */
49#define CLSCONS_NAME "nnonzeros" /**< name of classifier */
50#define CLSCONS_DESC "nnonezero entries" /**< short description of classification*/
51#define CLSCONS_PRIORITY 0
52
53#define CLSCONS_ENABLED TRUE
54
55
56/*
57 * Data structures
58 */
59
60/** classifier handler data */
62{
63};
64
65
66/*
67 * Local methods
68 */
69
70/* put your local methods here, and declare them static */
71
72
73/*
74 * classifier callback methods
75 */
76
77/** destructor of classifier to free user data (called when GCG is exiting) */
78#define classifierFree NULL
79
80static
81GCG_DECL_CONSCLASSIFY(classifierClassify)
82{
83 gcg::DETPROBDATA* detprobdata;
84 SCIP* origprob = GCGgetOrigprob(gcg);
85 if( transformed )
86 {
88 }
89 else
90 {
92 }
93
94 std::vector<int> nconssforclass( 0 );
95 std::vector<int> differentNNonzeros( 0 );
96 std::vector<int> classForCons( detprobdata->getNConss(), - 1 );
97 int counterClasses = 0;
98
99 /* firstly, assign all constraints to classindices */
100 for( int i = 0; i < detprobdata->getNConss(); ++ i )
101 {
102 int consnnonzeros = detprobdata->getNVarsForCons( i );
103 bool nzalreadyfound = false;
104
105 /* check if number of nonzeros belongs to an existing class index */
106 for( size_t nzid = 0; nzid < differentNNonzeros.size(); ++ nzid )
107 {
108 if( consnnonzeros == differentNNonzeros[nzid] )
109 {
110 nzalreadyfound = true;
111 classForCons[i] = (int) nzid;
112 ++ nconssforclass[nzid];
113 break;
114 }
115 }
116
117 /* if not, create a new class index */
118 if( ! nzalreadyfound )
119 {
120 classForCons[i] = counterClasses;
121 ++ counterClasses;
122 differentNNonzeros.push_back( consnnonzeros );
123 nconssforclass.push_back( 1 );
124 }
125 }
126
127 /* secondly, use these information to create a ConsPartition */
128 gcg::ConsPartition* classifier = new gcg::ConsPartition(gcg, "nonzeros", (int) differentNNonzeros.size(), detprobdata->getNConss() );
129
130 /* set class names and descriptions of every class */
131 for( int c = 0; c < classifier->getNClasses(); ++ c )
132 {
133 std::stringstream text;
134 text << differentNNonzeros[c];
135 classifier->setClassName( c, text.str().c_str() );
136 text.str( "" );
137 text.clear();
138 text << "This class contains all constraints with " << differentNNonzeros[c] << " nonzero coefficients.";
139 classifier->setClassDescription( c, text.str().c_str() );
140 }
141
142 /* copy the constraint assignment information found in first step */
143 for( int i = 0; i < classifier->getNConss(); ++ i )
144 {
145 classifier->assignConsToClass( i, classForCons[i] );
146 }
147 SCIPverbMessage(origprob, SCIP_VERBLEVEL_HIGH, NULL, " Consclassifier \"%s\" yields a classification with %d different constraint classes \n", classifier->getName(), classifier->getNClasses() );
148
149 detprobdata->addConsPartition(classifier);
150 return SCIP_OKAY;
151}
152
153/*
154 * classifier specific interface methods
155 */
156
157/** creates the handler for classifier for Nnonzeros and includes it in SCIP */
159 GCG* gcg /**< GCG data structure */
160 )
161{
162 GCG_CLASSIFIERDATA* classifierdata = NULL;
163
164 SCIP_CALL(
166 classifierFree, classifierClassify) );
167
168 return SCIP_OKAY;
169}
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()
GCG_EXPORT int getNConss()
returns the number of variables considered in the detprobdata
GCG_EXPORT int getNVarsForCons(int consIndex)
returns the number of variables for a given constraint
GCG_EXPORT void addConsPartition(ConsPartition *partition)
adds a constraint partition if it is no duplicate of an existing constraint partition
GCG_EXPORT int getNClasses()
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
SCIP_RETCODE GCGincludeConsClassifierNNonzeros(GCG *gcg)
#define CLSCONS_ENABLED
#define CLSCONS_PRIORITY
static GCG_DECL_CONSCLASSIFY(classifierClassify)
classifies constraints according to their nonzero entries
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,...)
#define NULL
various SCIP helper methods
Definition: struct_gcg.h:55
SCIP_VERBLEVEL_HIGH
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE