Scippy

UG

Ubiquity Generator framework

scipParaInitiator.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and software framework */
4 /* UG --- Ubquity Generator Framework */
5 /* */
6 /* Copyright Written by Yuji Shinano <shinano@zib.de>, */
7 /* Copyright (C) 2021 by Zuse Institute Berlin, */
8 /* licensed under LGPL version 3 or later. */
9 /* Commercial licenses are available through <licenses@zib.de> */
10 /* */
11 /* This code is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public License */
13 /* as published by the Free Software Foundation; either version 3 */
14 /* of the License, or (at your option) any later version. */
15 /* */
16 /* This program is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
19 /* GNU Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* */
24 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
25 
26 /**@file scipParaInitiator.h
27  * @brief ParaInitiator extension for SCIP solver.
28  * @author Yuji Shinano
29  *
30  *
31  *
32  */
33 
34 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35 
36 
37 #ifndef __SCIP_PARA_INITIATOR_H__
38 #define __SCIP_PARA_INITIATOR_H__
39 
40 #include <string>
41 #include "ug/paraDef.h"
42 #include "ug_bb/bbParaInitiator.h"
43 #include "scipParaComm.h"
44 #include "scipUserPlugins.h"
45 #include "scipDiffParamSet.h"
46 #include "scipParaInstance.h"
47 #include "scipParaSolution.h"
48 #include "scipParaDiffSubproblem.h"
49 #include "objscip/objscip.h"
50 #include "scip/scip.h"
51 #include "scip/scipdefplugins.h"
52 #ifdef UG_WITH_UGS
53 #include "ugs/ugsParaCommMpi.h"
54 #endif
55 
56 namespace ParaSCIP
57 {
58 
59 /** Initiator class */
61 {
62 protected:
68  SCIP_MESSAGEHDLR *messagehdlr;
69  FILE *logfile;
70  FILE *solutionFile;
72  SCIP *scip;
73  char *probname;
76  char *settingsName;
78  char *logname;
79  char *isolname;
83  SCIP_Real finalDualBound;
85  long long nSolved;
86  double absgap;
87  double gap;
88  double objlimit;
89 #ifdef UG_WITH_UGS
90  int seqNumber;
91 #endif
92 
93  bool addRootNodeCuts();
94  void outputProblemInfo(int *nNonLinearConsHdlrs);
95  bool onlyLinearConsHandler();
96 
97 public:
98  /** constructor */
100  UG::ParaComm *inComm,
101  UG::ParaTimer *inTimer
102  )
103  : UG::BbParaInitiator(inComm, inTimer), paraParams(0), instance(0), solution(0), scipDiffParamSetRoot(0), scipDiffParamSet(0), messagehdlr(0), logfile(0),
104  solutionFile(0), transSolutionFile(0), scip(0), probname(0), settingsNameLC(0), settingsNameRoot(0), settingsName(0), racingSettingsName(0),
105  logname(0), isolname(0), generatedIsolname(0), solutionFileName(0), userPlugins(0), finalDualBound(-DBL_MAX), finalState(UG::Aborted), nSolved(0),
106  absgap(-1.0), gap(-1.0), objlimit(DBL_MAX)
107 #ifdef UG_WITH_UGS
108  , seqNumber(0)
109 #endif
110  {
111  }
112 
113  /** destructor */
115  )
116  {
117  if( instance ) delete instance;
118  if( solution ) delete solution;
119  if( scipDiffParamSetRoot ) delete scipDiffParamSetRoot;
120  if( scipDiffParamSet ) delete scipDiffParamSet;
121  if( userPlugins ) delete userPlugins;
122  if( generatedIsolname ) delete [] generatedIsolname;
123 
124  // message handler is mangaed within scip. It is freed at SCIPfree
125 #ifndef SCIP_THREADSAFE_MESSAGEHDLRS
126  if( messagehdlr )
127  {
128  SCIP_CALL_ABORT( SCIPsetDefaultMessagehdlr() );
129  SCIP_CALL_ABORT( SCIPfreeObjMessagehdlr(&messagehdlr) );
130  }
131 #endif
132  /******************
133  * Close files *
134  ******************/
135  if( solutionFile )
136  {
137  fclose(solutionFile);
138  }
139  if( transSolutionFile )
140  {
141  fclose(transSolutionFile);
142  }
143 
144  /********************
145  * Deinitialization *
146  ********************/
147  if( !paraParams->getBoolParamValue(UG::Quiet) )
148  {
149  SCIP_CALL_ABORT( SCIPprintStatistics(scip, NULL) ); // output statistics (only for problem info)
150  }
151  if( scip )
152  {
153  SCIP_CALL_ABORT( SCIPfree(&scip) );
154  }
155 
156  if( logfile != NULL )
157  fclose(logfile);
158 
159  BMScheckEmptyMemory();
160  }
161 
162  /** init function */
163  virtual int init(
164  UG::ParaParamSet *paraParams,
165  int argc,
166  char** argv
167  );
168 
169 
170  int reInit(
171  int nRestartedRacing
172  );
173 
174  /** get instance */
176  )
177  {
178  return instance;
179  }
180 
181  /** make DiffSubproblem object for root node */
183  )
184  {
185  return 0;
186  }
187 
188  /** try to set incumbent solution */
189  bool tryToSetIncumbentSolution(UG::BbParaSolution *sol, bool checksol);
190 
191  /** send solver initialization message */
193 
194  /** generate racing ramp-up parameter sets */
195  void generateRacingRampUpParameterSets(int nParamSets, UG::ParaRacingRampUpParamSet **racingRampUpParamSets);
196 
198  {
199  return solution;
200  }
201 
203  {
204  return SCIPgetNSols(scip);
205  }
206 
207  /** convert an internal value to external value */
209  double internalValue
210  )
211  {
212  return instance->convertToExternalValue(internalValue);
213  }
214 
215  /** convert an external value to internal value */
217  double externalValue
218  )
219  {
220  return instance->convertToInternalValue(externalValue);
221  }
222 
223  /** get solution file name */
225  )
226  {
227  return solutionFileName;
228  }
229 
230  /** get absgap */
231  double getAbsgap(double dualBoundValue);
232 
233  /** get gap */
234  double getGap(double dualBoundValue);
235 
236  /** get absgap value specified */
237  double getAbsgapValue()
238  {
239  if( absgap < 0.0 )
240  {
241  SCIP_CALL_ABORT( SCIPgetRealParam(scip, "limits/absgap",&absgap ) );
242  }
243  return absgap;
244  }
245 
246  /** get gap value specified */
247  double getGapValue()
248  {
249  if( gap < 0.0 )
250  {
251  SCIP_CALL_ABORT( SCIPgetRealParam(scip, "limits/gap",&gap ) );
252  }
253  return gap;
254  }
255 
256  /** get epsilon */
257  double getEpsilon();
258 
259  /** write solution */
260  void writeSolution(const std::string& message);
261 
262  /** write ParaInstance */
263  void writeParaInstance(const std::string& filename);
264 
265  /** write solver runtime parameters */
266  void writeSolverParameters(std::ostream *os);
267 
268 #ifdef UG_WITH_ZLIB
269  /** write checkpoint solution */
270  void writeCheckpointSolution(const std::string& filename);
271 
272  /** read solution from checkpoint file */
273  double readSolutionFromCheckpointFile(char *afterCheckpointingSolutionFileName);
274 #endif
275 
276  /** get solving status string */
277  std::string getStatus();
278 
279  /** print solver version **/
280  void printSolverVersion(std::ostream *os); /**< output file (or NULL for standard output) */
281 
282  /** check if feasilbe soltuion exists or not */
284  {
285  return ( SCIPgetBestSol(scip) != NULL );
286  }
287 
288  /** set initial stat on initiator */
289  void accumulateInitialStat(UG::ParaInitialStat *initialStat);
290 
291  /** set initial stat on DiffSubproblem */
292  void setInitialStatOnDiffSubproblem(int minDepth, int maxDepth, UG::BbParaDiffSubproblem *diffSubproblem);
293 
294  /** set final solver status */
296 
297  /** set number of nodes solved */
298  void setNumberOfNodesSolved(long long n);
299 
300  /** set final dual bound */
301  void setDualBound(double bound);
302 
303  /** output solution status */
304  void outputFinalSolverStatistics(std::ostream *os, double time);
305 
306  /** set user plugins */
307  void setUserPlugins(ScipUserPlugins *inUi);
308  /*
309  {
310  userPlugins = inUi;
311  assert(userPlugins != 0);
312  }
313  */
314 
315  /** include user plugins */
316  void includeUserPlugins(SCIP *inScip)
317  {
318  if( userPlugins )
319  {
320  (*userPlugins)(inScip);
321  }
322  }
323 
324  /** returns whether the objective value is known to be integral in every feasible solution */
325  bool isObjIntegral(){ return ( SCIPisObjIntegral(scip) == TRUE );; }
326 
327  void interrupt()
328  {
329  SCIP_STAGE stage = SCIPgetStage(scip);
330 
331  if(stage == SCIP_STAGE_PRESOLVING || stage == SCIP_STAGE_SOLVING)
332  SCIP_CALL_ABORT( SCIPinterruptSolve(scip) );
333  }
334 
335 #ifdef UG_WITH_UGS
336  /** read ugs incumbent solution **/
337  bool readUgsIncumbentSolution(UGS::UgsParaCommMpi *ugsComm, int source);
338 
339  /** write ugs incumbent solution **/
340  void writeUgsIncumbentSolution(UGS::UgsParaCommMpi *ugsComm);
341 #endif
342 
343 };
344 
346 
347 }
348 
349 #endif // __SCIP_PARA_INITIATOR_H__
350 
void printSolverVersion(std::ostream *os)
double getGap(double dualBoundValue)
void setInitialStatOnDiffSubproblem(int minDepth, int maxDepth, UG::BbParaDiffSubproblem *diffSubproblem)
void writeSolution(const std::string &message)
ParaInstance extenstion for SCIP solver.
ScipDiffParamSet * scipDiffParamSetRoot
UG::BbParaSolution * getGlobalBestIncumbentSolution()
UG::BbParaDiffSubproblem * makeRootNodeDiffSubproblem()
void accumulateInitialStat(UG::ParaInitialStat *initialStat)
ParaInitialStat extension for SCIP solver.
SCIP user plugins.
class for solution
ScipParaInitiator * ScipParaInitiatorPtr
ParaSolution extension for SCIP solver.
UG::ParaInstance * getParaInstance()
Defines for UG Framework.
int reInit(int nRestartedRacing)
void writeParaInstance(const std::string &filename)
Class for initiator.
void outputProblemInfo(int *nNonLinearConsHdlrs)
void setNumberOfNodesSolved(long long n)
FinalSolverState
Final status of computation.
void outputFinalSolverStatistics(std::ostream *os, double time)
ScipDiffParamSet * scipDiffParamSet
bool tryToSetIncumbentSolution(UG::BbParaSolution *sol, bool checksol)
ScipParaInitiator(UG::ParaComm *inComm, UG::ParaTimer *inTimer)
Class for the difference between instance and subproblem.
void setFinalSolverStatus(UG::FinalSolverState status)
void writeSolverParameters(std::ostream *os)
class ParaParamSet
Definition: paraParamSet.h:850
SCIP parameter set to be transferred ( Only keep difference between default settings )...
class for instance data
Definition: paraInstance.h:50
double convertToInternalValue(double externalValue)
BbParaInitiator(ParaComm *inComm, ParaTimer *inTimer)
constructor
ParaComm extension for SCIP solver.
void setUserPlugins(ScipUserPlugins *inUi)
virtual int init(UG::ParaParamSet *paraParams, int argc, char **argv)
static const int Quiet
Definition: paraParamSet.h:71
double convertToExternalValue(double internalValue)
double getAbsgap(double dualBoundValue)
class for initial statistics collecting after racing
UG::ParaParamSet * paraParams
SCIP_MESSAGEHDLR * messagehdlr
void includeUserPlugins(SCIP *inScip)
class ParaTimer
Definition: paraTimer.h:48
class ParaRacingRampUpParamSet (parameter set for racing ramp-up)
void generateRacingRampUpParameterSets(int nParamSets, UG::ParaRacingRampUpParamSet **racingRampUpParamSets)
double convertToInternalValue(double externalValue)
Base class of communicator object.
Definition: paraComm.h:101
UG::FinalSolverState finalState
double convertToExternalValue(double internalValue)
bool getBoolParamValue(int param)
for bool parameters