Scippy

UG

Ubiquity Generator framework

bbParaInitiator.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 paraInitiator.h
27  * @brief Base class of initiator that maintains original problem and incumbent solution.
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 __BB_PARA_INITIATOR_H__
38 #define __BB_PARA_INITIATOR_H__
39 
40 #include <string>
41 
42 #include "ug/uggithash.h"
43 #include "ug/paraInitiator.h"
44 #include "bbParaDiffSubproblem.h"
45 #include "bbParaSolution.h"
46 #include "bbParaComm.h"
47 #include "bbParaNode.h"
48 
49 #ifdef UG_WITH_UGS
50 #include "ugs/ugsDef.h"
51 #include "ugs/ugsParaCommMpi.h"
52 #endif
53 
54 namespace UG
55 {
56 
57 ///
58 /// Final status of computation
59 ///
61  InitialNodesGenerated, ///< initial nodes were generated
62  Aborted, ///< aborted
63  HardTimeLimitIsReached, ///< hard time limit is reached
64  MemoryLimitIsReached, ///< memory limit is reached in a solver
65  GivenGapIsReached, ///< given gap is reached for the computation
66  ComputingWasInterrupted, ///< computing was interrupted
67  ProblemWasSolved, ///< problem was solved
68  RequestedSubProblemsWereSolved ///< requested subproblem was solved
69 };
70 
71 ///
72 /// Class for initiator
73 ///
75 {
76 
77 protected:
78 
79  bool solvedAtInit; ///< solved at init
80  bool solvedAtReInit; ///< solved at reInit
81  double *tightenedVarLbs; ///< array of tightened lower bound of variable
82  double *tightenedVarUbs; ///< array of tightened upper bound of variable
83 
84 #ifdef UG_WITH_ZLIB
85  gzstream::igzstream checkpointTasksStream; ///< gzstream for checkpoint tasks file
86 #endif
87 
88 public:
89 
90  ///
91  /// constructor
92  ///
94  ParaComm *inComm, ///< communicator used
95  ParaTimer *inTimer ///< timer used
96  )
97  : ParaInitiator(inComm, inTimer),
98  solvedAtInit(false),
99  solvedAtReInit(false),
100  tightenedVarLbs(0),
101  tightenedVarUbs(0)
102  {
103  }
104 
105  ///
106  /// destructor
107  ///
109  )
110  {
111  if( tightenedVarLbs ) delete [] tightenedVarLbs;
112  if( tightenedVarUbs ) delete [] tightenedVarUbs;
113  }
114 
115 #ifdef UG_WITH_ZLIB
116 
117  ///
118  /// read a ParaNode from checkpoint file
119  /// @return ParaNode object, 0 in the end of file
120  ///
121  BbParaNode *readParaNodeFromCheckpointFile(
122  bool onlyBoundChanges ///< indicate if it read only bound changes of not.
123  ///< true: only bound changes, fase: else
124  // bool hasMergingStatus
125  )
126  {
127  BbParaNode *paraNode = dynamic_cast<BbParaNode *>(paraComm->createParaTask());
128  if( paraNode->read(paraComm, checkpointTasksStream, onlyBoundChanges) )
129  {
130  return paraNode;
131  }
132  else
133  {
134  delete paraNode;
135  checkpointTasksStream.close();
136  return 0;
137  }
138  }
139 
140 #endif
141 
142  ///
143  /// check if problem is solved at init or not
144  /// @return true if problem is solved, false otherwise
145  ///
147  )
148  {
149  return solvedAtInit;
150  }
151 
152  ///
153  /// check if problem is solved at reInit or not
154  /// @return true if problem is solved, false otherwise
155  ///
157  )
158  {
159  return solvedAtReInit;
160  }
161 
162  ///
163  /// set tightened variable lower bound
164  /// TODO: this function should be in inherited class
165  ///
167  int i, ///< index of variable
168  double v ///< tightened bound
169  )
170  {
171  assert(tightenedVarLbs);
172  tightenedVarLbs[i] = v;
173  // could detect infeasibility
174  // assert( EPSLE(tightenedVarLbs[i],tightenedVarUbs[i], MINEPSILON) );
175  }
176 
177  ///
178  /// set tightened variable upper bound
179  /// TODO: this function should be in inherited class
180  ///
182  int i, ///< index of variable
183  double v ///< tightened bound
184  )
185  {
186  assert(tightenedVarUbs);
187  tightenedVarUbs[i] = v;
188  // could detect infeasibility
189  // assert( EPSLE(tightenedVarLbs[i],tightenedVarUbs[i], MINEPSILON) );
190  }
191 
192  ///
193  /// get tightened variable lower bound
194  /// TODO: this function should be in inherited class
195  /// @return lower bound
196  ///
198  int i ///< index of variable
199  )
200  {
201  if( tightenedVarLbs )
202  {
203  return tightenedVarLbs[i];
204  }
205  else
206  {
207  return DBL_MAX;
208  }
209  }
210 
211  ///
212  /// get tightened variable upper bound
213  /// TODO: this function should be in inherited class
214  /// @return uppper bound
215  ///
217  int i ///< index of variable
218  )
219  {
220  if( tightenedVarUbs )
221  {
222  return tightenedVarUbs[i];
223  }
224  else
225  {
226  return -DBL_MAX;
227  }
228  }
229 
230  ///
231  /// check if there are tightened lower or upper bound
232  /// TODO: this function should be in inherited class
233  /// @return true if there is a tightened lower or upper bound, false otherwise
234  ///
236  )
237  {
238  assert( (tightenedVarLbs && tightenedVarUbs) || ( (!tightenedVarLbs) && (!tightenedVarUbs) ) );
239  return ( tightenedVarLbs != 0 );
240  }
241 
242  ///
243  /// make DiffSubproblem object for root node
244  /// @return pointer to the root ParaDiffSubproblem object
245  ///
247  ) = 0;
248 
249  ///
250  /// convert objective function value to external value
251  /// TODO: this function may be in inherited class
252  /// @return objective function value as in external value
253  ///
254  virtual double convertToExternalValue(
255  double internalValue ///< internal value of the objective function
256  ) = 0;
257 
258 
259  ///
260  /// get global best incumbent solution
261  /// @return pinter to ParaSolution object
262  ///
264  ) = 0;
265 
266  ///
267  /// get the number of incumbent solutions
268  /// @return the number of incumbent solutions
269  ///
270  virtual int getNSolutions(
271  ) = 0;
272 
273  ///
274  /// try to set incumbent solution
275  /// @return true if solution is set successfully, false otherwise
276  ///
277  virtual bool tryToSetIncumbentSolution(
278  BbParaSolution *sol, ///< pointer to ParaSolution object to be set
279  bool checksol ///< true if the solution feasibility need to be checked
280  ) = 0;
281 
282  ///
283  /// get absolute gap of dual bound value
284  /// @return absolute gap
285  ///
286  virtual double getAbsgap(
287  double dualBoundValue ///< dual bound value
288  ) = 0;
289 
290  ///
291  /// get relative gap of dual bound value
292  /// @return relative gap
293  ///
294  virtual double getGap(
295  double dualBoundValue ///< dual bound value
296  ) = 0;
297 
298  ///
299  /// get absgap value specified
300  /// @return absgap value
301  ///
302  virtual double getAbsgapValue(
303  ) = 0;
304 
305  ///
306  /// get gap value specified
307  /// @return gap value
308  ///
309  virtual double getGapValue(
310  ) = 0;
311 
312  ///
313  /// set final solver status
314  ///
315  virtual void setFinalSolverStatus(
316  FinalSolverState status ///< solver status
317  ) = 0;
318 
319  ///
320  /// set number of nodes solved
321  ///
322  virtual void setNumberOfNodesSolved(
323  long long n ///< the number of nodes solved
324  ) = 0;
325 
326  ///
327  /// set final dual bound
328  ///
329  virtual void setDualBound(
330  double bound ///< dual bound value
331  ) = 0;
332 
333 
334  ///
335  /// check if feasible solution exists or not
336  /// @return true if a feasible solution exists, false otherwise
337  ///
338  virtual bool isFeasibleSolution(
339  ) = 0;
340 
341  ///
342  /// accumulate initial status
343  ///
344  virtual void accumulateInitialStat(
345  ParaInitialStat *initialStat ///< initial status collected
346  )
347  {
348  }
349 
350  ///
351  /// set initial status on DiffSubproblem
352  ///
354  int minDepth, ///< minimum depth
355  int maxDepth, ///< maximum depth
356  BbParaDiffSubproblem *diffSubproblem ///< pointer to ParaDiffSubproblem object
357  )
358  {
359  }
360 
361  ///
362  /// check if objective function value is always integral or not
363  /// @return true if it is always integral, false others
364  ///
365  virtual bool isObjIntegral(
366  )
367  {
368  return false;
369  }
370 
371  ///
372  /// check if solver can generate special cut off value or not
373  /// @return true if it can be generated, false others
374  ///
376  )
377  {
378  return false;
379  }
380 
381 };
382 
384 
385 }
386 
387 #endif // __BB_PARA_INITIATOR_HPP__
ParaComm * paraComm
communicator used
Definition: paraInitiator.h:66
virtual BbParaDiffSubproblem * makeRootNodeDiffSubproblem()=0
make DiffSubproblem object for root node
initial nodes were generated
returns the current git hash of UG
virtual BbParaSolution * getGlobalBestIncumbentSolution()=0
get global best incumbent solution
virtual void setInitialStatOnDiffSubproblem(int minDepth, int maxDepth, BbParaDiffSubproblem *diffSubproblem)
set initial status on DiffSubproblem
double * tightenedVarUbs
array of tightened upper bound of variable
virtual bool tryToSetIncumbentSolution(BbParaSolution *sol, bool checksol)=0
try to set incumbent solution
class for solution
virtual double getAbsgap(double dualBoundValue)=0
get absolute gap of dual bound value
void setTightenedVarUbs(int i, double v)
set tightened variable upper bound TODO: this function should be in inherited class ...
double getTightenedVarLbs(int i)
get tightened variable lower bound TODO: this function should be in inherited class ...
virtual void setNumberOfNodesSolved(long long n)=0
set number of nodes solved
bool solvedAtInit
solved at init
Class for initiator.
Definition: paraInitiator.h:62
requested subproblem was solved
double getTightenedVarUbs(int i)
get tightened variable upper bound TODO: this function should be in inherited class ...
virtual void setDualBound(double bound)=0
set final dual bound
bool isSolvedAtReInit()
check if problem is solved at reInit or not
Class for initiator.
virtual double convertToExternalValue(double internalValue)=0
convert objective function value to external value TODO: this function may be in inherited class ...
double * tightenedVarLbs
array of tightened lower bound of variable
FinalSolverState
Final status of computation.
problem was solved
virtual int getNSolutions()=0
get the number of incumbent solutions
virtual double getAbsgapValue()=0
get absgap value specified
void setTightenedVarLbs(int i, double v)
set tightened variable lower bound TODO: this function should be in inherited class ...
hard time limit is reached
given gap is reached for the computation
virtual void setFinalSolverStatus(FinalSolverState status)=0
set final solver status
Class for the difference between instance and subproblem.
bool isSolvedAtInit()
check if problem is solved at init or not
memory limit is reached in a solver
virtual double getGapValue()=0
get gap value specified
computing was interrupted
virtual bool isFeasibleSolution()=0
check if feasible solution exists or not
virtual ParaTask * createParaTask()=0
create ParaTask object by default constructor
BbParaInitiator(ParaComm *inComm, ParaTimer *inTimer)
constructor
class BbParaNode
Definition: bbParaNode.h:61
Base class of initiator that maintains original problem and incumbent solution.
virtual void accumulateInitialStat(ParaInitialStat *initialStat)
accumulate initial status
class for initial statistics collecting after racing
ParaInitiator * ParaInitiatorPtr
virtual ~BbParaInitiator()
destructor
class ParaTimer
Definition: paraTimer.h:48
virtual bool canGenerateSpecialCutOffValue()
check if solver can generate special cut off value or not
bool solvedAtReInit
solved at reInit
virtual double getGap(double dualBoundValue)=0
get relative gap of dual bound value
Base class of communicator object.
Definition: paraComm.h:101
virtual bool isObjIntegral()
check if objective function value is always integral or not
bool areTightenedVarBounds()
check if there are tightened lower or upper bound TODO: this function should be in inherited class ...