Scippy

SCIP

Solving Constraint Integer Programs

compute_symmetry_sassy_nauty.cpp
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the program and library */
4/* SCIP --- Solving Constraint Integer Programs */
5/* */
6/* Copyright (c) 2002-2024 Zuse Institute Berlin (ZIB) */
7/* */
8/* Licensed under the Apache License, Version 2.0 (the "License"); */
9/* you may not use this file except in compliance with the License. */
10/* You may obtain a copy of the License at */
11/* */
12/* http://www.apache.org/licenses/LICENSE-2.0 */
13/* */
14/* Unless required by applicable law or agreed to in writing, software */
15/* distributed under the License is distributed on an "AS IS" BASIS, */
16/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17/* See the License for the specific language governing permissions and */
18/* limitations under the License. */
19/* */
20/* You should have received a copy of the Apache-2.0 license */
21/* along with SCIP; see the file LICENSE. If not visit scipopt.org. */
22/* */
23/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25/**@file compute_symmetry_sassy_nauty.cpp
26 * @brief interface for symmetry computations to sassy as a preprocessor to nauty
27 * @author Marc Pfetsch
28 * @author Gioni Mexi
29 * @author Christopher Hojny
30 */
31
32/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
33
34#include "build_sassy_graph.h"
35#include "compute_symmetry.h"
36
37/* the following determines whether nauty or traces is used: */
38#define NAUTY
39
40#ifdef NAUTY
41#include "nauty/nauty.h"
42#include "nauty/nausparse.h"
43#else
44#include "nauty/traces.h"
45#endif
46
47/* include sassy */
48#ifdef __GNUC__
49#pragma GCC diagnostic ignored "-Wshadow"
50#pragma GCC diagnostic ignored "-Wunused-variable"
51#pragma GCC diagnostic ignored "-Wsign-compare"
52#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
53#endif
54
55#ifdef _MSC_VER
56# pragma warning(push)
57# pragma warning(disable: 4189) // local variable is initialized but not referenced
58# pragma warning(disable: 4388) // compare signed and unsigned expression
59# pragma warning(disable: 4456) // shadowed variable
60# pragma warning(disable: 4430) // missing type specifier
61#endif
62
63#include <sassy/preprocessor.h>
64#ifdef NAUTY
65#include "sassy/tools/nauty_converter.h"
66#else
67#include "sassy/tools/traces_converter.h"
68#endif
69
70#ifdef __GNUC__
71#pragma GCC diagnostic warning "-Wunused-but-set-variable"
72#pragma GCC diagnostic warning "-Wsign-compare"
73#pragma GCC diagnostic warning "-Wunused-variable"
74#pragma GCC diagnostic warning "-Wshadow"
75#endif
76
77#ifdef _MSC_VER
78# pragma warning(pop)
79#endif
80
81#include "build_sassy_graph.h"
82
83#include "scip/expr_var.h"
84#include "scip/expr_sum.h"
85#include "scip/expr_pow.h"
86#include "scip/expr.h"
87#include "scip/cons_nonlinear.h"
88#include "scip/cons_linear.h"
89#include "scip/scip_mem.h"
90#include "scip/symmetry_graph.h"
91
92
93/** struct for symmetry callback */
94struct SYMMETRY_Data
95{
96 SCIP* scip; /**< SCIP pointer */
97 SYM_SYMTYPE symtype; /**< type of symmetries that need to be computed */
98 int npermvars; /**< number of variables for permutations */
99 int nperms; /**< number of permutations */
100 int** perms; /**< permutation generators as (nperms x npermvars) matrix */
101 int nmaxperms; /**< maximal number of permutations */
102 int maxgenerators; /**< maximal number of generators constructed (= 0 if unlimited) */
103 SCIP_Bool restricttovars; /**< whether permutations shall be restricted to variables */
104};
105
106#ifdef NAUTY
107/** struct for nauty callback */
108struct NAUTY_Data
109{
110 SCIP* scip; /**< SCIP pointer */
111 int ntreenodes; /**< number of nodes visitied in nauty's search tree */
112 int maxncells; /**< maximum number of cells in nauty's search tree */
113 int maxnnodes; /**< maximum number of nodes in nauty's search tree */
114};
115
116/** static data for nauty callback */
117static struct NAUTY_Data nautydata_;
118#endif
119
120/* ------------------- hook functions ------------------- */
121
122/** callback function for sassy */ /*lint -e{715}*/
123static
125 void* user_param, /**< parameter supplied at call to sassy */
126 int n, /**< dimension of permutations */
127 const int* aut, /**< permutation */
128 int nsupp, /**< support size */
129 const int* suppa /**< support list */
130 )
131{
132 assert( aut != NULL );
133 assert( user_param != NULL );
134
135 SYMMETRY_Data* data = static_cast<SYMMETRY_Data*>(user_param);
136 assert( data->scip != NULL );
137 assert( data->maxgenerators >= 0 );
138
139 /* make sure we do not generate more that maxgenerators many permutations */
140 if ( data->maxgenerators != 0 && data->nperms >= data->maxgenerators )
141 return;
142
143 /* copy first part of automorphism */
144 bool isIdentity = true;
145 int* p = 0;
146 int permlen;
147 if ( data->restricttovars )
148 {
149 switch ( data->symtype )
150 {
151 case SYM_SYMTYPE_PERM:
152 permlen = data->npermvars;
153 break;
154 default:
155 assert( data->symtype == SYM_SYMTYPE_SIGNPERM );
156 permlen = 2 * data->npermvars;
157 }
158 }
159 else
160 permlen = n;
161
162 /* check whether permutation is identity */
163 for (int j = 0; j < permlen; ++j)
164 {
165 if ( (int) aut[j] != j )
166 isIdentity = false;
167 }
168
169 /* don't store identity permutations */
170 if ( isIdentity )
171 return;
172
173 if ( SCIPallocBlockMemoryArray(data->scip, &p, permlen) != SCIP_OKAY )
174 return;
175
176 /* store symmetry */
177 for (int j = 0; j < permlen; ++j)
178 p[j] = (int) aut[j];
179
180 /* check whether we should allocate space for perms */
181 if ( data->nmaxperms <= 0 )
182 {
183 if ( data->maxgenerators == 0 )
184 data->nmaxperms = 100; /* seems to cover many cases */
185 else
186 data->nmaxperms = data->maxgenerators;
187
188 if ( SCIPallocBlockMemoryArray(data->scip, &data->perms, data->nmaxperms) != SCIP_OKAY )
189 return;
190 }
191 else if ( data->nperms >= data->nmaxperms ) /* check whether we need to resize */
192 {
193 int newsize = SCIPcalcMemGrowSize(data->scip, data->nperms + 1);
194 assert( newsize >= data->nperms );
195 assert( data->maxgenerators == 0 );
196
197 if ( SCIPreallocBlockMemoryArray(data->scip, &data->perms, data->nmaxperms, newsize) != SCIP_OKAY )
198 return;
199
200 data->nmaxperms = newsize;
201 }
202
203 data->perms[data->nperms++] = p;
204}
205
206#ifdef NAUTY
207
208/** callback function for nauty to terminate early */ /*lint -e{715}*/
209static
211 graph* g, /**< sparse graph for nauty */
212 int* lab, /**< labels of node */
213 int* ptn, /**< array indicating change of set in node parition of graph */
214 int level, /**< level of current node in nauty's tree */
215 int numcells, /**< number of cells in current partition */
216 int tc, /**< index of target cells in lab if children need to be explored */
217 int code, /**< code produced by refinement and vertex-invariant procedures */
218 int m, /**< number of edges in the graph */
219 int n /**< number of nodes in the graph */
220 )
221{ /* lint --e{715} */
222 SCIP_Bool terminate = FALSE;
224
225 /* add some iteration limit to avoid spending too much time in nauty */
226 if ( numcells >= nautydata_.maxncells )
227 {
228 terminate = TRUE;
230 "symmetry computation terminated early, because number of cells %d in Nauty exceeds limit of %d\n",
231 numcells, nautydata_.maxncells);
233 "for running full symmetry detection, increase value of parameter propagating/symmetry/nautymaxncells\n");
234 }
236 {
237 terminate = TRUE;
239 "symmetry computation terminated early, because number of"
240 " nodes %d in Nauty's search tree exceeds limit of %d\n", nautydata_.ntreenodes, nautydata_.maxnnodes);
242 "for running full symmetry detection, increase value of"
243 " parameter propagating/symmetry/nautymaxnnodes\n");
244 }
245
246 if ( terminate )
247 {
248 /* request a kill from nauty */
249 nauty_kill_request = 1;
250 return;
251 }
252}
253
254#endif
255
256/** return whether symmetry can be computed */
258{
259 return TRUE;
260}
261
262/** static variable for holding the name of nauty */
263static TLS_ATTR char nautyname[20];
264
265/** return name of external program used to compute generators */
266const char* SYMsymmetryGetName(void)
267{
268 /* 28080+HAVE_TLS -> 2.8.(0)8 */
269#ifdef NAUTY
270 (void) SCIPsnprintf(nautyname, (int)sizeof(nautyname), "Nauty %d.%d.%d", NAUTYVERSIONID/10000, (NAUTYVERSIONID%10000)/1000, (NAUTYVERSIONID%1000)/10);
271#else
272 (void) SCIPsnprintf(nautyname, (int)sizeof(nautyname), "Traces %d.%d.%d", NAUTYVERSIONID/10000, (NAUTYVERSIONID%10000)/1000, (NAUTYVERSIONID%1000)/10);
273#endif
274 return nautyname;
275}
276
277/** return description of external program used to compute generators */
278const char* SYMsymmetryGetDesc(void)
279{
280#ifdef NAUTY
281 return "Computing Graph Automorphism Groups by Brendan D. McKay (users.cecs.anu.edu.au/~bdm/nauty)";
282#else
283 return "Computing Graph Automorphism Groups by Adolfo Piperno (pallini.di.uniroma1.it)";
284#endif
285}
286
287#define STR(x) #x
288#define XSTR(x) STR(x)
289
290/** return name of additional external program used for computing symmetries */
291const char* SYMsymmetryGetAddName(void)
292{
293 return "sassy " XSTR(SASSY_VERSION_MAJOR) "." XSTR(SASSY_VERSION_MINOR);
294}
295
296/** return description of additional external program used to compute symmetries */
297const char* SYMsymmetryGetAddDesc(void)
298{
299 return "Symmetry preprocessor by Markus Anders (github.com/markusa4/sassy)";
300}
301
302/** computes autormorphisms of a graph */
303static
305 SCIP* scip, /**< SCIP pointer */
306 SYM_SYMTYPE symtype, /**< type of symmetries that need to be computed */
307 sassy::static_graph* G, /**< pointer to graph for that automorphisms are computed */
308 int nsymvars, /**< number of variables encoded in graph */
309 int maxgenerators, /**< maximum number of generators to be constructed (=0 if unlimited) */
310 int*** perms, /**< pointer to store generators as (nperms x npermvars) matrix */
311 int* nperms, /**< pointer to store number of permutations */
312 int* nmaxperms, /**< pointer to store maximal number of permutations
313 * (needed for freeing storage) */
314 SCIP_Real* log10groupsize, /**< pointer to store log10 of size of group */
315 SCIP_Bool restricttovars, /**< whether permutations shall be restricted to variables */
316 SCIP_Real* symcodetime, /**< pointer to store the time for symmetry code */
317 SCIP_Bool canterminateearly /**< whether we allow to interrupt symmetry detection early
318 * (e.g., because of iteration limits) */
319 )
320{
321 SCIP_Real oldtime;
322
323 assert( scip != NULL );
324 assert( G != NULL );
325 assert( maxgenerators >= 0 );
326 assert( perms != NULL );
327 assert( nperms != NULL );
328 assert( nmaxperms != NULL );
329 assert( log10groupsize != NULL );
330 assert( symcodetime != NULL );
331
332 /* init */
333 *nperms = 0;
334 *nmaxperms = 0;
335 *perms = NULL;
336 *log10groupsize = 0;
337 *symcodetime = 0.0;
338
339 /* init data */
340 struct SYMMETRY_Data data;
341 data.scip = scip;
342 data.symtype = symtype;
343 data.npermvars = nsymvars;
344 data.nperms = 0;
345 data.nmaxperms = 0;
347 data.perms = NULL;
349
350#ifdef NAUTY
353 SCIP_CALL( SCIPgetIntParam(scip, "propagating/symmetry/nautymaxncells", &nautydata_.maxncells) );
354 SCIP_CALL( SCIPgetIntParam(scip, "propagating/symmetry/nautymaxnnodes", &nautydata_.maxnnodes) );
355#endif
356
357 oldtime = SCIPgetSolvingTime(scip);
358
359 /* set up sassy preprocessor */
360 sassy::preprocessor sassy;
361
362 /* turn off some preprocessing that generates redudant permutations */
363 sassy::configstruct sconfig;
364 sconfig.CONFIG_PREP_DEACT_PROBE = true;
365 sassy.configure(&sconfig);
366
367 /* lambda function to have access to data and pass it to sassyhook above */
368 sassy::sassy_hook sassyglue = [&](int n, const int* p, int nsupp, const int* suppa) {
369 sassyhook((void*)&data, n, p, nsupp, suppa);
370 };
371
372 /* call sassy to reduce graph */
373 sassy.reduce(G, &sassyglue);
374
375 /* first, convert the graph */
376 sparsegraph sg;
377 DYNALLSTAT(int, lab, lab_sz);
378 DYNALLSTAT(int, ptn, ptn_sz);
379
380#ifdef NAUTY
381 convert_sassy_to_nauty(G, &sg, &lab, &lab_sz, &ptn, &ptn_sz);
382 statsblk stats;
383 DYNALLSTAT(int, orbits, orbits_sz);
384 DYNALLOC1(int, orbits, orbits_sz, sg.nv, "malloc");
385 DEFAULTOPTIONS_SPARSEGRAPH(options);
386 /* init callback functions for nauty (accumulate the group generators found by nauty) */
387 options.writeautoms = FALSE;
388 options.userautomproc = sassy::preprocessor::nauty_hook;
389 options.defaultptn = FALSE; /* use color classes */
390 if ( canterminateearly )
391 options.usernodeproc = nautyterminationhook;
392 *log10groupsize = 0.0;
393 if(sg.nv > 0) {
394 sparsenauty(&sg, lab, ptn, orbits, &options, &stats, NULL);
395 *log10groupsize = (SCIP_Real) stats.grpsize2;
396 }
397#else
398 convert_sassy_to_traces(&sassygraph, &sg, &lab, &lab_sz, &ptn, &ptn_sz);
399 TracesStats stats;
400 DYNALLSTAT(int, orbits, orbits_sz);
401 DYNALLOC1(int, orbits, orbits_sz, sg.nv, "malloc");
402 DEFAULTOPTIONS_TRACES(options);
403 /* init callback functions for traces (accumulate the group generators found by traces) */
404 options.writeautoms = FALSE;
405 options.userautomproc = sassy::preprocessor::traces_hook;
406 options.defaultptn = FALSE; /* use color classes */
407 if(sg.nv > 0) {
408 Traces(&sg, lab, ptn, orbits, &options, &stats, NULL);
409 }
410#endif
411
412 /* clean up */
413 DYNFREE(lab, lab_sz);
414 DYNFREE(ptn, ptn_sz);
415 SG_FREE(sg);
416
417 *symcodetime = SCIPgetSolvingTime(scip) - oldtime;
418
419 /* prepare return values */
420 if ( data.nperms > 0 )
421 {
422 *perms = data.perms;
423 *nperms = data.nperms;
424 *nmaxperms = data.nmaxperms;
425 }
426 else
427 {
428 assert( data.perms == NULL );
429 assert( data.nmaxperms == 0 );
430
431 *perms = NULL;
432 *nperms = 0;
433 *nmaxperms = 0;
434 }
435
436 return SCIP_OKAY;
437}
438
439/** compute generators of symmetry group */
441 SCIP* scip, /**< SCIP pointer */
442 int maxgenerators, /**< maximal number of generators constructed (= 0 if unlimited) */
443 SYM_GRAPH* symgraph, /**< symmetry detection graph */
444 int* nperms, /**< pointer to store number of permutations */
445 int* nmaxperms, /**< pointer to store maximal number of permutations (needed for freeing storage) */
446 int*** perms, /**< pointer to store permutation generators as (nperms x npermvars) matrix */
447 SCIP_Real* log10groupsize, /**< pointer to store log10 of size of group */
448 SCIP_Real* symcodetime /**< pointer to store the time for symmetry code */
449 )
450{
451 SCIP_Bool success = FALSE;
452
453 assert( scip != NULL );
454 assert( maxgenerators >= 0 );
455 assert( symgraph != NULL );
456 assert( nperms != NULL );
457 assert( nmaxperms != NULL );
458 assert( perms != NULL );
459 assert( log10groupsize != NULL );
460 assert( symcodetime != NULL );
461
462 /* init */
463 *nperms = 0;
464 *nmaxperms = 0;
465 *perms = NULL;
466 *log10groupsize = 0;
467 *symcodetime = 0.0;
468
469 /* create sassy graph */
470 sassy::static_graph sassygraph;
471
472 SCIP_CALL( SYMbuildSassyGraph(scip, &sassygraph, symgraph, &success) );
473
474 /* compute symmetries */
476 maxgenerators, perms, nperms, nmaxperms, log10groupsize, TRUE, symcodetime, TRUE) );
477
478 return SCIP_OKAY;
479}
480
481/** returns whether two given graphs are identical */
483 SCIP* scip, /**< SCIP pointer */
484 SYM_SYMTYPE symtype, /**< type of symmetries to be checked */
485 SYM_GRAPH* G1, /**< first graph */
486 SYM_GRAPH* G2 /**< second graph */
487 )
488{
489 int** perms;
490 int nnodes;
491 int nperms;
492 int nmaxperms;
493 int nnodesfromG1;
494 SCIP_Real symcodetime = 0.0;
495 SCIP_Real log10groupsize;
496 SCIP_Bool success;
497
498 /* create sassy graph */
499 sassy::static_graph sassygraph;
500
501 SCIP_CALL( SYMbuildSassyGraphCheck(scip, &sassygraph, G1, G2, &nnodes, &nnodesfromG1, &success) );
502
503 if ( ! success )
504 return FALSE;
505
506 /* compute symmetries */
508 &perms, &nperms, &nmaxperms, &log10groupsize, FALSE, &symcodetime, FALSE) );
509
510 /* since G1 and G2 are connected and disjoint, they are isomorphic iff there is a permutation
511 * mapping a node from G1 to a node of G2
512 */
513 success = FALSE;
514 for (int p = 0; p < nperms && ! success; ++p)
515 {
516 for (int i = 0; i < nnodesfromG1; ++i)
517 {
518 if ( perms[p][i] >= nnodesfromG1 )
519 {
520 success = TRUE;
521 break;
522 }
523 }
524 }
525
526 for (int p = 0; p < nperms; ++p)
527 {
529 }
531
532 return success;
533}
SCIP_RETCODE SYMbuildSassyGraph(SCIP *scip, sassy::static_graph *sassygraph, SYM_GRAPH *graph, SCIP_Bool *success)
SCIP_RETCODE SYMbuildSassyGraphCheck(SCIP *scip, sassy::static_graph *sassygraph, SYM_GRAPH *G1, SYM_GRAPH *G2, int *nnodes, int *nnodesfromG1, SCIP_Bool *success)
methods to build sassy graph for symmetry detection
interface for symmetry computations
SCIP_Bool SYMcheckGraphsAreIdentical(SCIP *scip, SYM_SYMTYPE symtype, SYM_GRAPH *G1, SYM_GRAPH *G2)
const char * SYMsymmetryGetName(void)
const char * SYMsymmetryGetAddName(void)
SCIP_Bool SYMcanComputeSymmetry(void)
const char * SYMsymmetryGetDesc(void)
static TLS_ATTR char nautyname[20]
static struct NAUTY_Data nautydata_
static void sassyhook(void *user_param, int n, const int *aut, int nsupp, const int *suppa)
#define XSTR(x)
static void nautyterminationhook(graph *g, int *lab, int *ptn, int level, int numcells, int tc, int code, int m, int n)
static SCIP_RETCODE computeAutomorphisms(SCIP *scip, SYM_SYMTYPE symtype, sassy::static_graph *G, int nsymvars, int maxgenerators, int ***perms, int *nperms, int *nmaxperms, SCIP_Real *log10groupsize, SCIP_Bool restricttovars, SCIP_Real *symcodetime, SCIP_Bool canterminateearly)
const char * SYMsymmetryGetAddDesc(void)
SCIP_RETCODE SYMcomputeSymmetryGenerators(SCIP *scip, int maxgenerators, SYM_GRAPH *symgraph, int *nperms, int *nmaxperms, int ***perms, SCIP_Real *log10groupsize, SCIP_Real *symcodetime)
Constraint handler for linear constraints in their most general form, .
constraint handler for nonlinear constraints specified by algebraic expressions
#define NULL
Definition: def.h:266
#define SCIP_Bool
Definition: def.h:91
#define SCIP_Real
Definition: def.h:172
#define TRUE
Definition: def.h:93
#define FALSE
Definition: def.h:94
#define SCIP_CALL_ABORT(x)
Definition: def.h:352
#define SCIP_CALL(x)
Definition: def.h:373
private functions to work with algebraic expressions
power and signed power expression handlers
sum expression handler
variable expression handler
#define nnodes
Definition: gastrans.c:74
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
Definition: scip_message.c:225
SCIP_RETCODE SCIPgetIntParam(SCIP *scip, const char *name, int *value)
Definition: scip_param.c:269
#define SCIPfreeBlockMemoryArray(scip, ptr, num)
Definition: scip_mem.h:110
int SCIPcalcMemGrowSize(SCIP *scip, int num)
Definition: scip_mem.c:139
#define SCIPallocBlockMemoryArray(scip, ptr, num)
Definition: scip_mem.h:93
#define SCIPreallocBlockMemoryArray(scip, ptr, oldnum, newnum)
Definition: scip_mem.h:99
#define SCIPfreeBlockMemoryArrayNull(scip, ptr, num)
Definition: scip_mem.h:111
SCIP_Real SCIPgetSolvingTime(SCIP *scip)
Definition: scip_timing.c:378
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10880
SYM_SYMTYPE SCIPgetSymgraphSymtype(SYM_GRAPH *graph)
int SCIPgetSymgraphNVars(SYM_GRAPH *graph)
public methods for memory management
SYM_SYMTYPE symtype
SCIP_Bool restricttovars
methods for dealing with symmetry detection graphs
@ SCIP_VERBLEVEL_MINIMAL
Definition: type_message.h:54
@ SCIP_OKAY
Definition: type_retcode.h:42
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:63
enum SYM_Symtype SYM_SYMTYPE
Definition: type_symmetry.h:64
@ SYM_SYMTYPE_SIGNPERM
Definition: type_symmetry.h:62
@ SYM_SYMTYPE_PERM
Definition: type_symmetry.h:61