Scippy

GCG

Branch-and-Price & Column Generation for Everyone

score_classic.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 score_classic.cpp
30 * @brief classic score
31 * @author Jurgen Lentz
32 */
33
34/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35
38#include "gcg/cons_decomp.h"
39#include "gcg/cons_decomp.hpp"
40#include "gcg/score_classic.h"
41
42/* score properties */
43#define SCORE_NAME "classic"
44#define SCORE_SHORTNAME "classi"
45#define SCORE_DESC "classical score"
46
47
48/*
49 * Data structures
50 */
51struct GCG_ScoreData
52{
53};
54
55/*
56 * Local methods
57 */
58
59/* put your local methods here, and declare them static */
60
61
62
63/*
64 * score callback methods
65 */
66
67/** destructor of score to free user data (called when GCG is exiting) */
68#define scoreFreeClassic NULL
69
70static
71GCG_DECL_SCORECALC(scoreCalcClassic)
72{
73 int i;
74 int j;
75 int k;
76
77 unsigned long matrixarea;
78 unsigned long borderarea;
79 SCIP_Real borderscore; /* score of the border */
80 SCIP_Real densityscore; /* score of block densities */
81 SCIP_Real linkingscore; /* score related to interlinking blocks */
82 SCIP_Real totalscore; /* accumulated score */
83
84 SCIP_Real varratio;
85 int* nzblocks = NULL;
86 int* nlinkvarsblocks = NULL;
87 int* nvarsblocks = NULL;
88 SCIP_Real* blockdensities = NULL;
89 int* blocksizes = NULL;
90 SCIP_Real density;
91
92 SCIP_Real alphaborderarea;
93 SCIP_Real alphalinking;
94 SCIP_Real alphadensity;
95
96 alphaborderarea = 0.6;
97 alphalinking = 0.2;
98 alphadensity = 0.2;
99
102 gcg::DETPROBDATA* detprobdata = partialdec->getDetprobdata();
103
104 SCIP_CALL( SCIPallocBufferArray(scip, &nzblocks, partialdec->getNBlocks()) );
105 SCIP_CALL( SCIPallocBufferArray(scip, &nlinkvarsblocks, partialdec->getNBlocks()) );
106 SCIP_CALL( SCIPallocBufferArray(scip, &blockdensities, partialdec->getNBlocks()) );
107 SCIP_CALL( SCIPallocBufferArray(scip, &blocksizes, partialdec->getNBlocks()) );
108 SCIP_CALL( SCIPallocBufferArray(scip, &nvarsblocks, partialdec->getNBlocks()) );
109
110 /*
111 * 3 Scores
112 *
113 * - Area percentage (min)
114 * - block density (max)
115 * - \pi_b {v_b|v_b is linking}/#vb (min)
116 */
117
118 /* calculate slave sizes, nonzeros and linkingvars */
119 for( i = 0; i < partialdec->getNBlocks(); ++ i )
120 {
121 int ncurconss;
122 int nvarsblock;
123 SCIP_Bool *ishandled = NULL;
124
125 SCIP_CALL( SCIPallocBufferArray(scip, &ishandled, partialdec->getNVars()) );
126 nvarsblock = 0;
127 nzblocks[i] = 0;
128 nlinkvarsblocks[i] = 0;
129
130 for( j = 0; j < partialdec->getNVars(); ++ j )
131 {
132 ishandled[j] = FALSE;
133 }
134 ncurconss = partialdec->getNConssForBlock(i);
135
136 for( j = 0; j < ncurconss; ++ j )
137 {
138 int cons = partialdec->getConssForBlock(i)[j];
139 int ncurvars;
140 ncurvars = detprobdata->getNVarsForCons(cons);
141 for( k = 0; k < ncurvars; ++ k )
142 {
143 int var = detprobdata->getVarsForCons(cons)[k];
144 int block = -3;
145 if( partialdec->isVarBlockvarOfBlock(var, i) )
146 block = i + 1;
147 else if( partialdec->isVarLinkingvar(var) || partialdec->isVarStairlinkingvar(var) )
148 block = partialdec->getNBlocks() + 2;
149 else if( partialdec->isVarMastervar(var) )
150 block = partialdec->getNBlocks() + 1;
151
152 ++(nzblocks[i]);
153
154 if( block == partialdec->getNBlocks() + 1 && ishandled[var] == FALSE )
155 {
156 ++(nlinkvarsblocks[i]);
157 }
158 ishandled[var] = TRUE;
159 }
160 }
161
162 for( j = 0; j < partialdec->getNVars(); ++ j )
163 {
164 if( ishandled[j] )
165 {
166 ++nvarsblock;
167 }
168 }
169
170 blocksizes[i] = nvarsblock * ncurconss;
171 nvarsblocks[i] = nvarsblock;
172 if( blocksizes[i] > 0 )
173 {
174 blockdensities[i] = 1.0 * nzblocks[i] / blocksizes[i];
175 }
176 else
177 {
178 blockdensities[i] = 0.0;
179 }
180
181 assert( blockdensities[i] >= 0 && blockdensities[i] <= 1.0 );
182 SCIPfreeBufferArray( scip, &ishandled );
183 }
184
185 borderarea = ((unsigned long) partialdec->getNMasterconss() * partialdec->getNVars() ) + (((unsigned long) partialdec->getNLinkingvars() + partialdec->getNMastervars() + partialdec->getNTotalStairlinkingvars())) * (partialdec->getNConss() - partialdec->getNMasterconss());
186
187 matrixarea = ((unsigned long) partialdec->getNVars()) * ((unsigned long) partialdec->getNConss());
188
189 density = 1E20;
190 varratio = 1.0;
191 linkingscore = 1.;
192 borderscore = 1.;
193 densityscore = 1.;
194
195 for( i = 0; i < partialdec->getNBlocks(); ++ i )
196 {
197 density = MIN(density, blockdensities[i]);
198
199 if( (partialdec->getNLinkingvars() + partialdec->getNMastervars() + partialdec->getNTotalStairlinkingvars()) > 0 )
200 {
201 varratio *= 1.0 * nlinkvarsblocks[i] / (partialdec->getNLinkingvars() + partialdec->getNMastervars() + partialdec->getNTotalStairlinkingvars());
202 }
203 else
204 {
205 varratio = 0.;
206 }
207 }
208 linkingscore = 0.5 + 0.5 * varratio;
209
210 densityscore = 1. - density;
211
212 borderscore = 1.0 * ( borderarea ) / matrixarea;
213
214 totalscore = 1. - (alphaborderarea * borderscore + alphalinking * linkingscore + alphadensity * densityscore);
215
216 if(totalscore > 1)
217 totalscore = 1;
218 if(totalscore < 0)
219 totalscore = 0;
220
221 *scorevalue = totalscore;
222
223 SCIPfreeBufferArray( scip, & nzblocks );
224 SCIPfreeBufferArray( scip, & nlinkvarsblocks) ;
225 SCIPfreeBufferArray( scip, & blockdensities);
226 SCIPfreeBufferArray( scip, & blocksizes);
227 SCIPfreeBufferArray( scip, & nvarsblocks);
228
229 return SCIP_OKAY;
230}
231
232
233
234
235/*
236 * score specific interface methods
237 */
238
239/** creates the classic score and includes it in SCIP */
241 GCG* gcg /**< GCG data structure */
242 )
243{
244 GCG_SCOREDATA* scoredata = NULL;
245
246 SCIP_CALL(
248 scoreFreeClassic, scoreCalcClassic) );
249
250 return SCIP_OKAY;
251}
class storing partialdecs and the problem matrix
class storing (potentially incomplete) decompositions
GCG_EXPORT int getNVarsForCons(int consIndex)
returns the number of variables for a given constraint
GCG_EXPORT std::vector< int > & getVarsForCons(int consIndex)
returns the variable indices of the coefficient matrix for a constraint
class to manage partial decompositions
GCG_EXPORT int getNConss()
Gets the number of constraints.
GCG_EXPORT int getNMasterconss()
Gets size of the vector containing master conss.
GCG_EXPORT bool isVarLinkingvar(int var)
Checks whether the var is a linking var.
GCG_EXPORT std::vector< int > & getConssForBlock(int block)
returns array containing constraints assigned to a block
GCG_EXPORT DETPROBDATA * getDetprobdata()
Gets the corresponding detprobdata.
GCG_EXPORT bool isVarMastervar(int var)
Checks whether the var is a master var.
GCG_EXPORT int getNVars()
Gets number of vars.
GCG_EXPORT int getNLinkingvars()
Gets size of the vector containing linking vars.
GCG_EXPORT int getNTotalStairlinkingvars()
Gets total number of stairlinking vars.
GCG_EXPORT int getNMastervars()
Gets size of the vector containing master vars.
GCG_EXPORT int getNConssForBlock(int block)
Gets size of the vector containing conss assigned to a block.
GCG_EXPORT bool isVarBlockvarOfBlock(int var, int block)
Checks whether the var is assigned to the block.
GCG_EXPORT bool isVarStairlinkingvar(int var)
Checks whether the var is a stairlinking var.
GCG_EXPORT int getNBlocks()
Gets the number of blocks.
PARTIALDECOMP * GCGconshdlrDecompGetPartialdecFromID(GCG *gcg, int partialdecid)
local method to find a partialdec for a given id or NULL if no partialdec with such id is found
constraint handler for structure detection
C++ interface of cons_decomp.
#define SCIP_Bool
#define MIN(x, y)
#define SCIP_Real
#define SCIP_CALL(x)
#define TRUE
#define FALSE
SCIP * GCGgetOrigprob(GCG *gcg)
Definition: gcg.cpp:87
#define SCIPallocBufferArray(scip, ptr, num)
#define SCIPfreeBufferArray(scip, ptr)
GCG_EXPORT SCIP_RETCODE GCGincludeScore(GCG *gcg, const char *name, const char *shortname, const char *description, GCG_SCOREDATA *scoredata, GCG_DECL_SCOREFREE((*scorefree)), GCG_DECL_SCORECALC((*scorecalc)))
creates a score and includes it in GCG
Definition: score.c:50
#define NULL
static GCG_DECL_SCORECALC(scoreCalcClassic)
#define SCORE_SHORTNAME
#define SCORE_NAME
#define SCORE_DESC
#define scoreFreeClassic
SCIP_RETCODE GCGincludeScoreClassic(GCG *gcg)
classic score
Definition: struct_gcg.h:55
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE