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-2025 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 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
88public:
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),
102 {
103 }
104
105 ///
106 /// destructor
107 ///
109 )
110 {
111 if( tightenedVarLbs )
112 {
113 delete [] tightenedVarLbs;
114 tightenedVarLbs = 0;
115 }
116 if( tightenedVarUbs )
117 {
118 delete [] tightenedVarUbs;
119 tightenedVarUbs = 0;
120 }
121 }
122
123#ifdef UG_WITH_ZLIB
124
125 ///
126 /// read a ParaNode from checkpoint file
127 /// @return ParaNode object, 0 in the end of file
128 ///
130 bool onlyBoundChanges ///< indicate if it read only bound changes of not.
131 ///< true: only bound changes, fase: else
132 // bool hasMergingStatus
133 )
134 {
135 BbParaNode *paraNode = dynamic_cast<BbParaNode *>(paraComm->createParaTask());
136 if( paraNode->read(paraComm, checkpointTasksStream, onlyBoundChanges) )
137 {
138 return paraNode;
139 }
140 else
141 {
142 delete paraNode;
143 checkpointTasksStream.close();
144 return 0;
145 }
146 }
147
148#endif
149
150 ///
151 /// check if problem is solved at init or not
152 /// @return true if problem is solved, false otherwise
153 ///
155 )
156 {
157 return solvedAtInit;
158 }
159
160 ///
161 /// check if problem is solved at reInit or not
162 /// @return true if problem is solved, false otherwise
163 ///
165 )
166 {
167 return solvedAtReInit;
168 }
169
170 ///
171 /// set tightened variable lower bound
172 /// TODO: this function should be in inherited class
173 ///
174 virtual bool setTightenedVarLbs(
175 int i, ///< index of variable
176 double v ///< tightened bound
177 )
178 {
179 assert(tightenedVarLbs);
180 tightenedVarLbs[i] = v;
181 // could detect infeasibility
182 // assert( EPSLE(tightenedVarLbs[i],tightenedVarUbs[i], MINEPSILON) );
183 return true;
184 }
185
186 ///
187 /// set tightened variable upper bound
188 /// TODO: this function should be in inherited class
189 ///
190 virtual bool setTightenedVarUbs(
191 int i, ///< index of variable
192 double v ///< tightened bound
193 )
194 {
195 assert(tightenedVarUbs);
196 tightenedVarUbs[i] = v;
197 // could detect infeasibility
198 // assert( EPSLE(tightenedVarLbs[i],tightenedVarUbs[i], MINEPSILON) );
199 return true;
200 }
201
202 ///
203 /// get tightened variable lower bound
204 /// TODO: this function should be in inherited class
205 /// @return lower bound
206 ///
207 virtual double getTightenedVarLbs(
208 int i ///< index of variable
209 )
210 {
211 if( tightenedVarLbs )
212 {
213 return tightenedVarLbs[i];
214 }
215 else
216 {
217 return DBL_MAX;
218 }
219 }
220
221 ///
222 /// get tightened variable upper bound
223 /// TODO: this function should be in inherited class
224 /// @return uppper bound
225 ///
226 virtual double getTightenedVarUbs(
227 int i ///< index of variable
228 )
229 {
230 if( tightenedVarUbs )
231 {
232 return tightenedVarUbs[i];
233 }
234 else
235 {
236 return -DBL_MAX;
237 }
238 }
239
240 ///
241 /// check if there are tightened lower or upper bound
242 /// TODO: this function should be in inherited class
243 /// @return true if there is a tightened lower or upper bound, false otherwise
244 ///
246 )
247 {
248 assert( (tightenedVarLbs && tightenedVarUbs) || ( (!tightenedVarLbs) && (!tightenedVarUbs) ) );
249 return ( tightenedVarLbs != 0 );
250 }
251
252 ///
253 /// make DiffSubproblem object for root node
254 /// @return pointer to the root ParaDiffSubproblem object
255 ///
257 ) = 0;
258
259 ///
260 /// convert objective function value to external value
261 /// TODO: this function may be in inherited class
262 /// @return objective function value as in external value
263 ///
265 double internalValue ///< internal value of the objective function
266 ) = 0;
267
268
269 ///
270 /// get global best incumbent solution
271 /// @return pinter to ParaSolution object
272 ///
274 ) = 0;
275
276 ///
277 /// get the number of incumbent solutions
278 /// @return the number of incumbent solutions
279 ///
280 virtual int getNSolutions(
281 ) = 0;
282
283 ///
284 /// try to set incumbent solution
285 /// @return true if solution is set successfully, false otherwise
286 ///
288 BbParaSolution *sol, ///< pointer to ParaSolution object to be set
289 bool checksol ///< true if the solution feasibility need to be checked
290 ) = 0;
291
292 ///
293 /// get absolute gap of dual bound value
294 /// @return absolute gap
295 ///
296 virtual double getAbsgap(
297 double dualBoundValue ///< dual bound value
298 ) = 0;
299
300 ///
301 /// get relative gap of dual bound value
302 /// @return relative gap
303 ///
304 virtual double getGap(
305 double dualBoundValue ///< dual bound value
306 ) = 0;
307
308 ///
309 /// get absgap value specified
310 /// @return absgap value
311 ///
312 virtual double getAbsgapValue(
313 ) = 0;
314
315 ///
316 /// get gap value specified
317 /// @return gap value
318 ///
319 virtual double getGapValue(
320 ) = 0;
321
322 ///
323 /// set final solver status
324 ///
326 FinalSolverState status ///< solver status
327 ) = 0;
328
329 ///
330 /// set number of nodes solved
331 ///
333 long long n ///< the number of nodes solved
334 ) = 0;
335
336 ///
337 /// set final dual bound
338 ///
339 virtual void setDualBound(
340 double bound ///< dual bound value
341 ) = 0;
342
343
344 ///
345 /// check if feasible solution exists or not
346 /// @return true if a feasible solution exists, false otherwise
347 ///
348 virtual bool isFeasibleSolution(
349 ) = 0;
350
351 ///
352 /// accumulate initial status
353 ///
355 ParaInitialStat *initialStat ///< initial status collected
356 )
357 {
358 }
359
360 ///
361 /// set initial status on DiffSubproblem
362 ///
364 int minDepth, ///< minimum depth
365 int maxDepth, ///< maximum depth
366 BbParaDiffSubproblem *diffSubproblem ///< pointer to ParaDiffSubproblem object
367 )
368 {
369 }
370
371 ///
372 /// check if objective function value is always integral or not
373 /// @return true if it is always integral, false others
374 ///
375 virtual bool isObjIntegral(
376 )
377 {
378 return false;
379 }
380
381 ///
382 /// check if solver can generate special cut off value or not
383 /// @return true if it can be generated, false others
384 ///
386 )
387 {
388 return false;
389 }
390
391};
392
393typedef ParaInitiator *ParaInitiatorPtr;
394
395}
396
397#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
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 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
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
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
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
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
ParaComm * paraComm
communicator used
Definition: paraInitiator.h:66
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