Scippy

SCIP

Solving Constraint Integer Programs

EventhdlrNewSol.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-2017 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License. */
12 /* along with SCIP; see the file COPYING. If not email to scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file EventhdlrNewSol.cpp
17  * @brief event handler for new solutions in TSP
18  * @author Timo Berthold
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #include <cassert>
24 #include <fstream>
25 #include <iostream>
26 #ifndef _MSC_VER
27 #include <unistd.h>
28 #else
29 #include <windows.h>
30 #define sleep Sleep
31 #endif
32 
33 #include "objscip/objscip.h"
34 #include "EventhdlrNewSol.h"
35 #include "ProbDataTSP.h"
36 #include "GomoryHuTree.h"
37 
38 using namespace tsp;
39 using namespace std;
40 
41 
42 /** destructor of event handler to free user data (called when SCIP is exiting) */
43 SCIP_DECL_EVENTFREE(EventhdlrNewSol::scip_free)
44 {
45  return SCIP_OKAY;
46 } /*lint !e715*/
47 
48 
49 /** initialization method of event handler (called after problem was transformed) */
50 SCIP_DECL_EVENTINIT(EventhdlrNewSol::scip_init)
51 {
52 
53  int lockwaits = 0;
54  while( SCIPfileExists("temp.tour.lock") && lockwaits < 10 )
55  {
56  /* wait one second and try again */
57  (void) sleep(1);
58  lockwaits++;
59  }
60  if( SCIPfileExists("temp.tour.lock") )
61  {
62  SCIPwarningMessage(scip, "cannot reset, because lockfile <temp.tour.lock> is still existing\n");
63  return SCIP_OKAY;
64  }
65 
66  /* create lock file */
67  ofstream lockfile("temp.tour.lock");
68  lockfile << "lock" << endl;
69  lockfile.close();
70 
71  // create output file which can be read by TSPViewer
72  ofstream filedata("temp.tour");
73  filedata << "RESET" << endl;
74  filedata.close();
75 
76  /* delete lock file */
77  unlink("temp.tour.lock");
78  (void) sleep(1); /* wait for the Java TSPViewer */
79 
80  return SCIP_OKAY;
81 } /*lint !e715*/
82 
83 
84 /** deinitialization method of event handler (called before transformed problem is freed) */
85 SCIP_DECL_EVENTEXIT(EventhdlrNewSol::scip_exit)
86 {
87  return SCIP_OKAY;
88 } /*lint !e715*/
89 
90 
91 /** solving process initialization method of event handler (called when branch and bound process is about to begin)
92  *
93  * This method is called when the presolving was finished and the branch and bound process is about to begin.
94  * The event handler may use this call to initialize its branch and bound specific data.
95  *
96  */
97 SCIP_DECL_EVENTINITSOL(EventhdlrNewSol::scip_initsol)
98 {
100  return SCIP_OKAY;
101 }
102 
103 
104 /** solving process deinitialization method of event handler (called before branch and bound process data is freed)
105  *
106  * This method is called before the branch and bound process is freed.
107  * The event handler should use this call to clean up its branch and bound data.
108  */
109 SCIP_DECL_EVENTEXITSOL(EventhdlrNewSol::scip_exitsol)
110 {
112  return SCIP_OKAY;
113 }
114 
115 
116 /** frees specific constraint data */
117 SCIP_DECL_EVENTDELETE(EventhdlrNewSol::scip_delete)
118 {
119  return SCIP_OKAY;
120 } /*lint !e715*/
121 
122 
123 /** execution method of event handler
124  *
125  * Processes the event. The method is called every time an event occurs, for which the event handler
126  * is responsible. Event handlers may declare themselves resposible for events by calling the
127  * corresponding SCIPcatch...() method. This method creates an event filter object to point to the
128  * given event handler and event data.
129  */
130 SCIP_DECL_EVENTEXEC(EventhdlrNewSol::scip_exec)
131 {
132  SCIP_SOL* sol;
133  sol = SCIPgetBestSol(scip);
134  ProbDataTSP* probdata = dynamic_cast<ProbDataTSP*>(SCIPgetObjProbData(scip));
135  GRAPH* graph = probdata->getGraph(); /*lint !e613*/
136  GRAPHNODE* node = &graph->nodes[0];
137  GRAPHEDGE* lastedge = NULL;
138  int lockwaits = 0;
139 
140  /* wait for lock file to disappear */
141  while( SCIPfileExists("temp.tour.lock") && lockwaits < 10 )
142  {
143  /* wait one second and try again */
144  (void) sleep(1);
145  lockwaits++;
146  }
147 
148  if( SCIPfileExists("temp.tour.lock") )
149  {
150  SCIPwarningMessage(scip, "cannot output tour in file, because lockfile <temp.tour.lock> is still existing\n");
151  return SCIP_OKAY;
152  }
153 
154  /* create lock file */
155  ofstream lockfile("temp.tour.lock");
156  lockfile << "lock" << endl;
157  lockfile.close();
158 
159  // create output file which can be read by TSPViewer
160  ofstream filedata("temp.tour");
161  filedata << graph->nnodes << endl;
162 
163  SCIP_HEUR* heur = SCIPgetSolHeur(scip, sol);
164  if ( heur == NULL)
165  filedata << "relaxation" << endl;
166  else
167  filedata << SCIPheurGetName(heur) << endl;
168 
169  filedata << SCIPgetSolOrigObj(scip,sol) << endl;
170  do
171  {
172  // output the number of nodes
173  filedata << node->id << " " << node->x << " " << node->y << endl;
174  GRAPHEDGE* edge = node->first_edge;
175 
176  // output the id and the coordinates of the nodes in the order they appear in the tour
177 
178  while( edge != NULL)
179  {
180  if( edge->back != lastedge && SCIPgetSolVal(scip, sol, edge->var) > 0.5 )
181  {
182  node = edge->adjac;
183  lastedge = edge;
184  break;
185  }
186  edge = edge->next;
187  }
188  }
189  while ( node != &graph->nodes[0] );
190 
191  filedata.close();
192 
193  /* delete lock file */
194  unlink("temp.tour.lock");
195  (void) sleep(1); /* wait for the Java TSPViewer */
196 
197  return SCIP_OKAY;
198 } /*lint !e715*/
struct GraphEdge * next
Definition: GomoryHuTree.h:59
int nnodes
Definition: GomoryHuTree.h:72
GRAPHNODE * nodes
Definition: GomoryHuTree.h:76
SCIP_DECL_EVENTEXIT(EventhdlrNewSol::scip_exit)
generator for global cuts in undirected graphs
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip.c:1260
SCIP_DECL_EVENTINITSOL(EventhdlrNewSol::scip_initsol)
SCIP_Bool SCIPfileExists(const char *filename)
Definition: misc.c:9513
SCIP_DECL_EVENTINIT(EventhdlrNewSol::scip_init)
SCIP_VAR * var
Definition: GomoryHuTree.h:64
SCIP_DECL_EVENTFREE(EventhdlrNewSol::scip_free)
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1181
GRAPHNODE * adjac
Definition: GomoryHuTree.h:62
struct GraphEdge * back
Definition: GomoryHuTree.h:60
struct GraphEdge * first_edge
Definition: GomoryHuTree.h:43
#define NULL
Definition: lpi_spx1.cpp:137
C++ wrapper classes for SCIP.
#define SCIP_CALL(x)
Definition: def.h:306
event handler for new solutions in TSP
GRAPH * getGraph()
Definition: ProbDataTSP.h:111
C++ problem data for TSP.
SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
Definition: scip.c:40207
SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
Definition: scip.c:40241
SCIP_Real SCIPgetSolOrigObj(SCIP *scip, SCIP_SOL *sol)
Definition: scip.c:38090
scip::ObjProbData * SCIPgetObjProbData(SCIP *scip)
#define SCIP_EVENTTYPE_BESTSOLFOUND
Definition: type_event.h:88
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip.c:38931
double x
Definition: GomoryHuTree.h:35
SCIP_DECL_EVENTEXEC(EventhdlrNewSol::scip_exec)
SCIP_HEUR * SCIPgetSolHeur(SCIP *scip, SCIP_SOL *sol)
Definition: scip.c:38330
SCIP_DECL_EVENTEXITSOL(EventhdlrNewSol::scip_exitsol)
double y
Definition: GomoryHuTree.h:36
SCIP_DECL_EVENTDELETE(EventhdlrNewSol::scip_delete)
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip.c:38007