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-2026 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 bbParaInitiator.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"
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
54namespace 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
77protected:
78
79 ParaComm *paraComm; ///< communicator used
80 ParaTimer *timer; ///< timer used
81 char *prefixWarm; ///< prefix of warm start files
82 bool solvedAtInit; ///< solved at init
83 bool solvedAtReInit; ///< solved at reInit
84 double *tightenedVarLbs; ///< array of tightened lower bound of variable
85 double *tightenedVarUbs; ///< array of tightened upper bound of variable
86
87#ifdef UG_WITH_ZLIB
88 gzstream::igzstream checkpointTasksStream; ///< gzstream for checkpoint tasks file
89#endif
90
91public:
92 bool comment; ///< print comment messages
93
94 ///
95 /// constructor
96 ///
98 ParaComm *inComm, ///< communicator used
99 ParaTimer *inTimer ///< timer used
100 )
101 : ParaInitiator(),
102 paraComm(inComm),
103 timer(inTimer),
104 prefixWarm(0),
105 solvedAtInit(false),
106 solvedAtReInit(false),
109 comment(false)
110 {
111 }
112
113 ///
114 /// override of pure virtual getters
115 ///
117 )
118 {
119 return comment;
120 }
121
123 )
124 {
125 return prefixWarm != 0;
126 }
127
128 const char *getPrefixWarm(
129 )
130 {
131 return prefixWarm;
132 }
133
135 )
136 {
137 return paraComm;
138 }
139
140 ///
141 /// destructor
142 ///
144 )
145 {
146 if( tightenedVarLbs )
147 {
148 delete [] tightenedVarLbs;
149 tightenedVarLbs = 0;
150 }
151 if( tightenedVarUbs )
152 {
153 delete [] tightenedVarUbs;
154 tightenedVarUbs = 0;
155 }
156 }
157
158#ifdef UG_WITH_ZLIB
159
160 ///
161 /// read a ParaNode from checkpoint file
162 /// @return ParaNode object, 0 in the end of file
163 ///
165 bool onlyBoundChanges ///< indicate if it read only bound changes of not.
166 ///< true: only bound changes, fase: else
167 // bool hasMergingStatus
168 )
169 {
170 BbParaNode *paraNode = dynamic_cast<BbParaNode *>(paraComm->createParaTask());
171 if( paraNode->read(paraComm, checkpointTasksStream, onlyBoundChanges) )
172 {
173 return paraNode;
174 }
175 else
176 {
177 delete paraNode;
178 checkpointTasksStream.close();
179 return 0;
180 }
181 }
182
183#endif
184
185 ///
186 /// check if problem is solved at init or not
187 /// @return true if problem is solved, false otherwise
188 ///
190 )
191 {
192 return solvedAtInit;
193 }
194
195 ///
196 /// check if problem is solved at reInit or not
197 /// @return true if problem is solved, false otherwise
198 ///
200 )
201 {
202 return solvedAtReInit;
203 }
204
205 ///
206 /// set tightened variable lower bound
207 /// TODO: this function should be in inherited class
208 ///
209 virtual bool setTightenedVarLbs(
210 int i, ///< index of variable
211 double v ///< tightened bound
212 )
213 {
214 assert(tightenedVarLbs);
215 tightenedVarLbs[i] = v;
216 // could detect infeasibility
217 // assert( EPSLE(tightenedVarLbs[i],tightenedVarUbs[i], MINEPSILON) );
218 return true;
219 }
220
221 ///
222 /// set tightened variable upper bound
223 /// TODO: this function should be in inherited class
224 ///
225 virtual bool setTightenedVarUbs(
226 int i, ///< index of variable
227 double v ///< tightened bound
228 )
229 {
230 assert(tightenedVarUbs);
231 tightenedVarUbs[i] = v;
232 // could detect infeasibility
233 // assert( EPSLE(tightenedVarLbs[i],tightenedVarUbs[i], MINEPSILON) );
234 return true;
235 }
236
237 ///
238 /// get tightened variable lower bound
239 /// TODO: this function should be in inherited class
240 /// @return lower bound
241 ///
242 virtual double getTightenedVarLbs(
243 int i ///< index of variable
244 )
245 {
246 if( tightenedVarLbs )
247 {
248 return tightenedVarLbs[i];
249 }
250 else
251 {
252 return DBL_MAX;
253 }
254 }
255
256 ///
257 /// get tightened variable upper bound
258 /// TODO: this function should be in inherited class
259 /// @return uppper bound
260 ///
261 virtual double getTightenedVarUbs(
262 int i ///< index of variable
263 )
264 {
265 if( tightenedVarUbs )
266 {
267 return tightenedVarUbs[i];
268 }
269 else
270 {
271 return -DBL_MAX;
272 }
273 }
274
275 ///
276 /// check if there are tightened lower or upper bound
277 /// TODO: this function should be in inherited class
278 /// @return true if there is a tightened lower or upper bound, false otherwise
279 ///
281 )
282 {
283 assert( (tightenedVarLbs && tightenedVarUbs) || ( (!tightenedVarLbs) && (!tightenedVarUbs) ) );
284 return ( tightenedVarLbs != 0 );
285 }
286
287 ///
288 /// make DiffSubproblem object for root node
289 /// @return pointer to the root ParaDiffSubproblem object
290 ///
292 ) = 0;
293
294 ///
295 /// convert objective function value to external value
296 /// TODO: this function may be in inherited class
297 /// @return objective function value as in external value
298 ///
300 double internalValue ///< internal value of the objective function
301 ) = 0;
302
303
304 ///
305 /// get global best incumbent solution
306 /// @return pinter to ParaSolution object
307 ///
309 ) = 0;
310
311 ///
312 /// get the number of incumbent solutions
313 /// @return the number of incumbent solutions
314 ///
315 virtual int getNSolutions(
316 ) = 0;
317
318 ///
319 /// try to set incumbent solution
320 /// @return true if solution is set successfully, false otherwise
321 ///
323 BbParaSolution *sol, ///< pointer to ParaSolution object to be set
324 bool checksol ///< true if the solution feasibility need to be checked
325 ) = 0;
326
327 ///
328 /// get absolute gap of dual bound value
329 /// @return absolute gap
330 ///
331 virtual double getAbsgap(
332 double dualBoundValue ///< dual bound value
333 ) = 0;
334
335 ///
336 /// get relative gap of dual bound value
337 /// @return relative gap
338 ///
339 virtual double getGap(
340 double dualBoundValue ///< dual bound value
341 ) = 0;
342
343 ///
344 /// get absgap value specified
345 /// @return absgap value
346 ///
347 virtual double getAbsgapValue(
348 ) = 0;
349
350 ///
351 /// get gap value specified
352 /// @return gap value
353 ///
354 virtual double getGapValue(
355 ) = 0;
356
357 ///
358 /// set final solver status
359 ///
361 FinalSolverState status ///< solver status
362 ) = 0;
363
364 ///
365 /// set number of nodes solved
366 ///
368 long long n ///< the number of nodes solved
369 ) = 0;
370
371 ///
372 /// set final dual bound
373 ///
374 virtual void setDualBound(
375 double bound ///< dual bound value
376 ) = 0;
377
378
379 ///
380 /// check if feasible solution exists or not
381 /// @return true if a feasible solution exists, false otherwise
382 ///
383 virtual bool isFeasibleSolution(
384 ) = 0;
385
386 ///
387 /// accumulate initial status
388 ///
390 ParaInitialStat *initialStat ///< initial status collected
391 )
392 {
393 }
394
395 ///
396 /// set initial status on DiffSubproblem
397 ///
399 int minDepth, ///< minimum depth
400 int maxDepth, ///< maximum depth
401 BbParaDiffSubproblem *diffSubproblem ///< pointer to ParaDiffSubproblem object
402 )
403 {
404 }
405
406 ///
407 /// check if objective function value is always integral or not
408 /// @return true if it is always integral, false others
409 ///
410 virtual bool isObjIntegral(
411 )
412 {
413 return false;
414 }
415
416 ///
417 /// check if solver can generate special cut off value or not
418 /// @return true if it can be generated, false others
419 ///
421 )
422 {
423 return false;
424 }
425
426};
427
428typedef ParaInitiator *ParaInitiatorPtr;
429
430}
431
432#endif // __BB_PARA_INITIATOR_HPP__
Base class of communicator for UG Framework.
Base class for a container which has difference between instance and subproblem.
Base class for BbParaNode.
Base class for solution.
Class for the difference between instance and subproblem.
Class for initiator.
virtual bool setTightenedVarUbs(int i, double v)
set tightened variable upper bound TODO: this function should be in inherited class
virtual void setFinalSolverStatus(FinalSolverState status)=0
set final solver status
ParaComm * paraComm
communicator used
virtual double getTightenedVarLbs(int i)
get tightened variable lower bound TODO: this function should be in inherited class
virtual bool setTightenedVarLbs(int i, double v)
set tightened variable lower bound TODO: this function should be in inherited class
bool isPrintComment()
override of pure virtual getters
bool areTightenedVarBounds()
check if there are tightened lower or upper bound TODO: this function should be in inherited class
virtual int getNSolutions()=0
get the number of incumbent solutions
BbParaInitiator(ParaComm *inComm, ParaTimer *inTimer)
constructor
ParaComm * getParaComm()
virtual double getGap(double dualBoundValue)=0
get relative gap of dual bound value
bool solvedAtReInit
solved at reInit
virtual bool tryToSetIncumbentSolution(BbParaSolution *sol, bool checksol)=0
try to set incumbent solution
bool comment
print comment messages
virtual ~BbParaInitiator()
destructor
double * tightenedVarUbs
array of tightened upper bound of variable
virtual double getAbsgap(double dualBoundValue)=0
get absolute gap of dual bound value
double * tightenedVarLbs
array of tightened lower bound of variable
virtual double getAbsgapValue()=0
get absgap value specified
char * prefixWarm
prefix of warm start files
ParaTimer * timer
timer used
virtual void setNumberOfNodesSolved(long long n)=0
set number of nodes solved
virtual double getTightenedVarUbs(int i)
get tightened variable upper bound TODO: this function should be in inherited class
const char * getPrefixWarm()
virtual void setDualBound(double bound)=0
set final dual bound
virtual bool isFeasibleSolution()=0
check if feasible solution exists or not
virtual void accumulateInitialStat(ParaInitialStat *initialStat)
accumulate initial status
virtual bool isObjIntegral()
check if objective function value is always integral or not
virtual double getGapValue()=0
get gap value specified
virtual BbParaDiffSubproblem * makeRootNodeDiffSubproblem()=0
make DiffSubproblem object for root node
bool solvedAtInit
solved at init
bool isSolvedAtReInit()
check if problem is solved at reInit or not
bool isSolvedAtInit()
check if problem is solved at init or not
BbParaNode * readParaNodeFromCheckpointFile(bool onlyBoundChanges)
read a ParaNode from checkpoint file
virtual double convertToExternalValue(double internalValue)=0
convert objective function value to external value TODO: this function may be in inherited class
virtual bool canGenerateSpecialCutOffValue()
check if solver can generate special cut off value or not
virtual void setInitialStatOnDiffSubproblem(int minDepth, int maxDepth, BbParaDiffSubproblem *diffSubproblem)
set initial status on DiffSubproblem
virtual BbParaSolution * getGlobalBestIncumbentSolution()=0
get global best incumbent solution
gzstream::igzstream checkpointTasksStream
gzstream for checkpoint tasks file
class BbParaNode
Definition: bbParaNode.h:62
bool read(ParaComm *comm, gzstream::igzstream &in, bool onlyBoundChanges)
read from checkpoint file
Definition: bbParaNode.cpp:78
class for solution
Base class of communicator object.
Definition: paraComm.h:102
virtual ParaTask * createParaTask()=0
create ParaTask object by default constructor
class for initial statistics collecting after racing
Class for initiator.
Definition: paraInitiator.h:63
class ParaTimer
Definition: paraTimer.h:49
FinalSolverState
Final status of computation.
@ MemoryLimitIsReached
memory limit is reached in a solver
@ ComputingWasInterrupted
computing was interrupted
@ GivenGapIsReached
given gap is reached for the computation
@ ProblemWasSolved
problem was solved
@ HardTimeLimitIsReached
hard time limit is reached
@ Aborted
aborted
@ RequestedSubProblemsWereSolved
requested subproblem was solved
@ InitialNodesGenerated
initial nodes were generated
ParaInitiator * ParaInitiatorPtr
Base class of initiator that maintains original problem and incumbent solution.
returns the current git hash of UG