Scippy

GCG

Branch-and-Price & Column Generation for Everyone

dialog_graph.cpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program */
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-2021 Operations Research, RWTH Aachen University */
10 /* Zuse Institute Berlin (ZIB) */
11 /* */
12 /* This program is free software; you can redistribute it and/or */
13 /* modify it under the terms of the GNU Lesser General Public License */
14 /* as published by the Free Software Foundation; either version 3 */
15 /* of the License, or (at your option) any later version. */
16 /* */
17 /* This program is distributed in the hope that it will be useful, */
18 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
19 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
20 /* GNU Lesser General Public License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with this program; if not, write to the Free Software */
24 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.*/
25 /* */
26 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
27 
28 /**@file dialog_graph.cpp
29  * @brief A dialog to write graph representations of the matrix and read partitions as decompositions.
30  * @author Martin Bergner
31  */
32 
33 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
34 
35 #include "dialog_graph.h"
36 #include "scip/dialog_default.h"
37 #include "graph/bipartitegraph.h"
38 #include "graph/hyperrowcolgraph.h"
39 #include "graph/hyperrowgraph.h"
40 #include "graph/hypercolgraph.h"
41 #include "graph/columngraph.h"
42 #include "graph/rowgraph.h"
43 #include "scip_misc.h"
44 #include "cons_decomp.h"
45 #include "graph/graph_tclique.h"
46 
47 namespace gcg
48 {
49 
51  SCIP* scip /**< SCIP data structure */
52 ) : ObjDialog(scip, "write", "write graph to file", TRUE)
53 {
54 
55 }
56 
57 SCIP_DECL_DIALOGEXEC(DialogWriteGraph::scip_exec) {
58  SCIP_CALL(SCIPdialogExecMenu(scip, dialog, dialoghdlr, nextdialog));
59  return SCIP_OKAY;
60 }
61 
63  SCIP* scip /**< SCIP data structure */
64 ) : ObjDialog(scip, "graph", "graph submenu to read and write graph", TRUE)
65 {
66 
67 }
68 
69 SCIP_DECL_DIALOGEXEC(DialogGraph::scip_exec) {
70  SCIP_CALL(SCIPdialogExecMenu(scip, dialog, dialoghdlr, nextdialog));
71  return SCIP_OKAY;
72 }
74  SCIP* scip /**< SCIP data structure */
75 ) : ObjDialog(scip, "read", "read partition from file", TRUE)
76 {
77 
78 }
79 
80 SCIP_DECL_DIALOGEXEC(DialogReadPartition::scip_exec) {
81 
82  SCIP_CALL(SCIPdialogExecMenu(scip, dialog, dialoghdlr, nextdialog));
83  return SCIP_OKAY;
84 }
85 
86 template<class T, template <class T1> class G>
88  SCIP* scip /**< SCIP data structure */
89 ): ObjDialog(scip, G<T>(scip, Weights()).name.c_str(), "writes graph of given type", FALSE)
90 {
91  (void)static_cast<MatrixGraph<T>*>((G<T>*)0); /* assure we only get descendants of type Graph */
92 }
93 
94 
95 template<class T,template <class T1> class G>
96 SCIP_RETCODE DialogWriteGraphs<T, G>::scip_exec(SCIP* scip, SCIP_DIALOG* dialog, SCIP_DIALOGHDLR* dialoghdlr, SCIP_DIALOG** nextdialog)
97 {
98 
99  if( SCIPgetStage(scip) < SCIP_STAGE_PROBLEM )
100  {
101  *nextdialog = SCIPdialoghdlrGetRoot(dialoghdlr);
102  SCIPdialogMessage(scip, NULL, "No problem exists, read in a problem first.\n");
103  return SCIP_OKAY;
104  }
105 
106  char* filename;
107  SCIP_Bool endoffile;
108 
109  SCIP_CALL( SCIPdialoghdlrGetWord(dialoghdlr, dialog, "enter filename: ", &filename, &endoffile) );
110  if( endoffile )
111  {
112  *nextdialog = NULL;
113  return SCIP_OKAY;
114  }
115  if( filename[0] != '\0' )
116  {
117 
118  char* extension;
119  int fd;
120  FILE* file;
121 
122  extension = filename;
123 
124  file = fopen(filename, "wx");
125  if( file == NULL )
126  return SCIP_FILECREATEERROR;
127 
128  fd = fileno(file);
129  if( fd == -1 )
130  return SCIP_FILECREATEERROR;
131 
132  MatrixGraph<T>* graph = new G<T>(scip, Weights());
133  SCIP_CALL( SCIPdialoghdlrAddHistory(dialoghdlr, dialog, extension, TRUE) );
134  SCIP_CALL( graph->createFromMatrix(SCIPgetConss(scip), SCIPgetVars(scip), SCIPgetNConss(scip), SCIPgetNVars(scip)) );
135  SCIP_CALL( graph->writeToFile(fd, FALSE) );
136  delete graph;
137  SCIPdialogMessage(scip, NULL, "graph written to <%s>\n", extension);
138  }
139  *nextdialog = SCIPdialoghdlrGetRoot(dialoghdlr);
140  return SCIP_OKAY;
141 }
142 
143 template<class T, template <class T1> class G>
145  SCIP* scip /**< SCIP data structure */
146 ): ObjDialog(scip, G<T>(scip, Weights()).name.c_str(), "reads graph of given type", FALSE)
147 {
148  (void)static_cast<MatrixGraph<T>*>((G<T>*)0); /* assure we only get descendants of type Graph */
149 }
150 
151 template<class T, template <class T1> class G>
152 SCIP_RETCODE DialogReadGraphs<T, G>::scip_exec(SCIP* scip, SCIP_DIALOG* dialog, SCIP_DIALOGHDLR* dialoghdlr, SCIP_DIALOG** nextdialog)
153 {
154 
155  if( SCIPgetStage(scip) < SCIP_STAGE_PROBLEM )
156  {
157  *nextdialog = SCIPdialoghdlrGetRoot(dialoghdlr);
158  SCIPdialogMessage(scip, NULL, "No problem exists, read in a problem first.\n");
159  return SCIP_OKAY;
160  }
161 
162  char* filename;
163  SCIP_Bool endoffile;
164 
165  SCIP_CALL( SCIPdialoghdlrGetWord(dialoghdlr, dialog, "enter filename: ", &filename, &endoffile) );
166  if( endoffile )
167  {
168  *nextdialog = NULL;
169  return SCIP_OKAY;
170  }
171  if( filename[0] != '\0' )
172  {
173  MatrixGraph<T>* graph = new G<T>(scip, Weights());
174  char* extension;
175  extension = filename;
176  DEC_DECOMP* decomp;
177  SCIP_CALL( SCIPdialoghdlrAddHistory(dialoghdlr, dialog, extension, TRUE) );
178  SCIP_CALL( graph->createFromMatrix(SCIPgetConss(scip), SCIPgetVars(scip), SCIPgetNConss(scip), SCIPgetNVars(scip)) );
179  SCIP_CALL( graph->readPartition(extension) );
180  SCIP_CALL( graph->createDecompFromPartition(&decomp) );
181  delete graph;
182 
183  SCIP_CALL( GCGconshdlrDecompAddPreexistingDecomp(scip, decomp) );
184  DECdecompFree(scip, &decomp);
185  SCIPdialogMessage(scip, NULL, "decomposition read from <%s>\n", extension);
186  }
187  *nextdialog = SCIPdialoghdlrGetRoot(dialoghdlr);
188  return SCIP_OKAY;
189 }
190 
191 } /* namespace gcg */
192 
193 /** include the graph entries for both writing the graph and reading in the partition */
194 template<class T, template <class T1> class G>
196  SCIP* scip /**< SCIP data structure */
197 )
198 {
199  SCIP_DIALOG* graphdialog;
200  SCIP_DIALOG* subdialog;
201 
202  (void)static_cast<gcg::MatrixGraph<T>*>((G<T>*)0); /* assure we only get descendants of type Graph */
203 
204  (void) SCIPdialogFindEntry(SCIPgetRootDialog(scip), "graph", &graphdialog);
205  assert(graphdialog != NULL);
206 
207  (void) SCIPdialogFindEntry(graphdialog, "write", &subdialog);
208  assert(subdialog != NULL);
209  SCIP_CALL( SCIPincludeObjDialog(scip, subdialog, new gcg::DialogWriteGraphs<T,G>(scip), true) );
210 
211  (void) SCIPdialogFindEntry(graphdialog, "read", &subdialog);
212  assert(subdialog != NULL);
213  SCIP_CALL( SCIPincludeObjDialog(scip, subdialog, new gcg::DialogReadGraphs<T,G >(scip), true) );
214 
215  return SCIP_OKAY;
216 }
217 
218 /** inludes all graph submenu entries */
219 extern "C"
221  SCIP* scip /**< SCIP data structure */
222  )
223 {
224  SCIP_DIALOG* dialog;
225  SCIP_DIALOG* subdialog;
226  dialog = SCIPgetRootDialog(scip);
227  SCIP_CALL( SCIPincludeObjDialog(scip, dialog, new gcg::DialogGraph(scip), TRUE) );
228  (void) SCIPdialogFindEntry(dialog, "graph", &subdialog);
229  assert(subdialog != NULL);
230  SCIP_CALL( SCIPincludeObjDialog(scip, subdialog, new gcg::DialogWriteGraph(scip), TRUE) );
231  SCIP_CALL( SCIPincludeObjDialog(scip, subdialog, new gcg::DialogReadPartition(scip), TRUE) );
232 
233  SCIP_CALL( (GCGincludeGraphEntries<gcg::GraphTclique,gcg::RowGraph>(scip)) );
234 #ifdef SCIP_DISABLED_CODE
235  /*SCIP_CALL*/( GCGincludeGraphEntries<gcg::GraphTclique,gcg::BipartiteGraph>(scip) );
236  /*SCIP_CALL*/( GCGincludeGraphEntries<gcg::GraphTclique,gcg::ColumnGraph>(scip) );
237  /*SCIP_CALL*/( GCGincludeGraphEntries<gcg::GraphTclique,gcg::HyperrowcolGraph>(scip) );
238  /*SCIP_CALL*/( GCGincludeGraphEntries<gcg::GraphTclique,gcg::HyperrowGraph>(scip) );
239  /*SCIP_CALL*/( GCGincludeGraphEntries<gcg::GraphTclique,gcg::HypercolGraph>(scip) );
240 #endif
241  return SCIP_OKAY;
242 }
DialogReadGraphs(SCIP *scip)
DialogReadPartition(SCIP *scip)
C++ wrapper for dialogs.
Definition: objdialog.h:53
DialogGraph(SCIP *scip)
A row graph where each row is a node and rows are adjacent if they share a variable.
A hypergraph with row and column nodes.
DialogWriteGraphs(SCIP *scip)
constraint handler for structure detection
SCIP_RETCODE GCGconshdlrDecompAddPreexistingDecomp(SCIP *scip, DEC_DECOMP *decomp)
adds a decomp that exists before the detection is called
various SCIP helper methods
DialogWriteGraph(SCIP *scip)
Column hypergraph.
SCIP_RETCODE GCGincludeDialogsGraph(SCIP *scip)
SCIP_DECL_DIALOGEXEC(DialogWriteGraph::scip_exec)
A row graph where each column is a node and columns are adjacent if they appear in a row.
interface to the SCIP tclique graph library
SCIP_RETCODE DECdecompFree(SCIP *scip, DEC_DECOMP **decdecomp)
Definition: decomp.c:530
Column hypergraph.
SCIP_RETCODE GCGincludeGraphEntries(SCIP *scip)
A dialog to write graph representations of the matrix and read partitions as decompositions.
SCIP_RETCODE SCIPincludeObjDialog(SCIP *scip, SCIP_DIALOG *parentdialog, gcg::ObjDialog *objdialog, SCIP_Bool deleteobject)
Definition: objdialog.cpp:127