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-2018 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 
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
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
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
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
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
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( SCIPconshdlrDecompAddDecdecomp(scip, decomp) );
184  SCIPdialogMessage(scip, NULL, "decomposition read from <%s>\n", extension);
185  }
186  *nextdialog = SCIPdialoghdlrGetRoot(dialoghdlr);
187  return SCIP_OKAY;
188 }
189 
190 } /* namespace gcg */
191 
193 template<class T, template <class T1> class G>
195  SCIP* scip
196 )
197 {
198  SCIP_DIALOG* graphdialog;
199  SCIP_DIALOG* subdialog;
200 
201  (void)static_cast<gcg::MatrixGraph<T>*>((G<T>*)0); /* assure we only get descendants of type Graph */
202 
203  (void) SCIPdialogFindEntry(SCIPgetRootDialog(scip), "graph", &graphdialog);
204  assert(graphdialog != NULL);
205 
206  (void) SCIPdialogFindEntry(graphdialog, "write", &subdialog);
207  assert(subdialog != NULL);
208  SCIP_CALL( SCIPincludeObjDialog(scip, subdialog, new gcg::DialogWriteGraphs<T,G>(scip), true) );
209 
210  (void) SCIPdialogFindEntry(graphdialog, "read", &subdialog);
211  assert(subdialog != NULL);
212  SCIP_CALL( SCIPincludeObjDialog(scip, subdialog, new gcg::DialogReadGraphs<T,G >(scip), true) );
213 
214  return SCIP_OKAY;
215 }
216 
218 extern "C"
220  SCIP* scip
221  )
222 {
223  SCIP_DIALOG* dialog;
224  SCIP_DIALOG* subdialog;
225  dialog = SCIPgetRootDialog(scip);
226  SCIP_CALL( SCIPincludeObjDialog(scip, dialog, new gcg::DialogGraph(scip), TRUE) );
227  (void) SCIPdialogFindEntry(dialog, "graph", &subdialog);
228  assert(subdialog != NULL);
229  SCIP_CALL( SCIPincludeObjDialog(scip, subdialog, new gcg::DialogWriteGraph(scip), TRUE) );
230  SCIP_CALL( SCIPincludeObjDialog(scip, subdialog, new gcg::DialogReadPartition(scip), TRUE) );
231 
232  SCIP_CALL( (GCGincludeGraphEntries<gcg::GraphTclique,gcg::RowGraph>(scip)) );
233 #if 0
234  /*SCIP_CALL*/( GCGincludeGraphEntries<gcg::GraphTclique,gcg::BipartiteGraph>(scip) );
235  /*SCIP_CALL*/( GCGincludeGraphEntries<gcg::GraphTclique,gcg::ColumnGraph>(scip) );
236  /*SCIP_CALL*/( GCGincludeGraphEntries<gcg::GraphTclique,gcg::HyperrowcolGraph>(scip) );
237  /*SCIP_CALL*/( GCGincludeGraphEntries<gcg::GraphTclique,gcg::HyperrowGraph>(scip) );
238  /*SCIP_CALL*/( GCGincludeGraphEntries<gcg::GraphTclique,gcg::HypercolGraph>(scip) );
239 #endif
240  return SCIP_OKAY;
241 }
SCIP_RETCODE SCIPincludeObjDialog(SCIP *scip, SCIP_DIALOG *parentdialog, gcg::ObjDialog *objdialog, SCIP_Bool deleteobject)
Definition: objdialog.cpp:127
DialogReadGraphs(SCIP *scip)
SCIP_RETCODE SCIPconshdlrDecompAddDecdecomp(SCIP *scip, DEC_DECOMP *decdecomp)
sets (and adds) the decomposition structure
virtual SCIP_RETCODE writeToFile(int fd, SCIP_Bool writeweights)
Definition: matrixgraph.h:79
DialogGraph(SCIP *scip)
DialogWriteGraphs(SCIP *scip)
virtual SCIP_DECL_DIALOGEXEC(scip_exec)
A dialog to write graph representations of the matrix and read partitions as decompositions.
SCIP_RETCODE GCGincludeGraphEntries(SCIP *scip)
A row graph where each column is a node and columns are adjacent if they appear in a row...
A row graph where each row is a node and rows are adjacent if they share a variable.
DialogWriteGraph(SCIP *scip)
various SCIP helper methods
Column hypergraph.
virtual SCIP_RETCODE createDecompFromPartition(DEC_DECOMP **decomp)
Definition: matrixgraph.h:89
A hypergraph with row and column nodes.
virtual SCIP_RETCODE createFromMatrix(SCIP_CONS **conss, SCIP_VAR **vars, int nconss_, int nvars_)
Definition: matrixgraph.h:138
virtual SCIP_RETCODE readPartition(const char *filename)
Definition: matrixgraph.h:113
virtual SCIP_DECL_DIALOGEXEC(scip_exec)
Column hypergraph.
C++ wrapper for dialogs.
Definition: objdialog.h:53
interface to the SCIP tclique graph library
SCIP_RETCODE GCGincludeDialogsGraph(SCIP *scip)
virtual SCIP_DECL_DIALOGEXEC(scip_exec)
constraint handler for structure detection
struct DecDecomp DEC_DECOMP
Definition: type_decomp.h:43
DialogReadPartition(SCIP *scip)