Scippy

UG

Ubiquity Generator framework

scipParaInstanceTh.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-2024 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 scipParaInstanceTh.h
27 * @brief ScipParaInstance extension for threads communication.
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_INSTANCE_TH_H__
38#define __SCIP_PARA_INSTANCE_TH_H__
39
40#include <cassert>
41#include <cstring>
42#include "ug_bb/bbParaComm.h"
44#include "scipUserPlugins.h"
45#include "scip/scip.h"
46#include "scip/cons_linear.h"
47#include "scipDiffParamSet.h"
48#include "scipParaSolution.h"
49
50namespace ParaSCIP
51{
52
53class ScipParaInstance : public UG::BbParaInstance
54{
55protected:
56 SCIP *scip; // this pointer should point to the scip environment of LoadCoordinator, in case that
57 // LC does not have two scip environment
58 virtual const char *getFileName() = 0;
59 int nVars;
60 int varIndexRange;
61 int *mapToOriginalIndecies; /**< array of indices to map to original problem's probindices
62 LC does not have this map in general, it is in the transformed prob. in scip */
63 int *mapToSolverLocalIndecies; /**< array of reverse indices mapToOriginalIndecies */
64 // int *mapToProbIndecies; /**< in Solver, one more variable map is need in some cases */
65 SCIP *orgScip; /**< if LC has the above MAP, variables need to be converted to this scip */
66 bool copyIncreasedVariables; /**< indicate if SCIP copy increase the number of veariables or not */
67public:
68 /** constructor */
70 ) : scip(0), nVars(0), varIndexRange(0), mapToOriginalIndecies(0), mapToSolverLocalIndecies(0), // mapToProbIndecies(0),
72 {
73 }
74
76 SCIP *inScip
77 ) : scip(inScip), nVars(0), varIndexRange(0), mapToOriginalIndecies(0), mapToSolverLocalIndecies(0), // mapToProbIndecies(0),
79 {
80 }
81 /** destractor */
83 )
84 {
86 {
87 delete [] mapToOriginalIndecies;
88 }
90 {
92 }
93 /*
94 if( mapToProbIndecies )
95 {
96 delete [] mapToProbIndecies;
97 }
98 */
99 if( orgScip ) // If this instance has orgScip pointer, scip has to be freed
100 {
101 SCIPfree(&scip);
102 }
103 }
104
105 /** convert an internal value to external value */
106 double convertToExternalValue(double internalValue)
107 {
108 if( orgScip )
109 {
110 return SCIPretransformObj(orgScip, SCIPretransformObj(scip, internalValue) );
111 }
112 else
113 {
114 return SCIPretransformObj(scip, internalValue);
115 }
116 }
117
118 /** convert an external value to internal value */
119 double convertToInternalValue(double externalValue)
120 {
121 if( orgScip )
122 {
123 return SCIPtransformObj(scip, SCIPtransformObj(orgScip, externalValue));
124 }
125 else
126 {
127 return SCIPtransformObj(scip, externalValue);
128 }
129 }
130
131 /** get solution values for the original problem */
133 {
134 assert( orgScip );
135 assert( mapToOriginalIndecies );
136 SCIP_SOL* newsol;
137 SCIP_CALL_ABORT( SCIPcreateSol(scip, &newsol, 0) );
138 SCIP_VAR **vars = SCIPgetVars(scip);
139 assert( SCIPgetNVars(scip) == sol->getNVars() );
140 int j = 0;
141 for( int i = 0; i < varIndexRange; i++ )
142 {
143 if( mapToOriginalIndecies[i] >= 0 )
144 {
145 // assert( sol->indexAmongSolvers(j) == mapToOriginalIndecies[i]);
146 if( sol->indexAmongSolvers(j) == mapToOriginalIndecies[i] )
147 {
148 SCIP_CALL_ABORT( SCIPsetSolVal(scip, newsol, vars[sol->indexAmongSolvers(j)], sol->getValues()[j]) );
149 j++;
150 }
151 }
152 // SCIP_CALL_ABORT( SCIPsetSolVal(scip, newsol, vars[sol->indexAmongSolvers(i)], sol->getValues()[i]) );
153 // SCIP_CALL_ABORT( SCIPsetSolVal(scip, newsol, vars[mapToOriginalIndecies[sol->indexAmongSolvers(i)]], sol->getValues()[i]) );
154 /* this is not necessary
155 if( mapToOriginalIndecies )
156 {
157 assert( mapToOriginalIndecies[sol->indexAmongSolvers(i)] < sol->getNVars() );
158 }
159 else
160 {
161 assert( sol->indexAmongSolvers(i) < sol->getNVars() );
162 SCIP_CALL_ABORT( SCIPsetSolVal(scip, newsol, vars[i], sol->getValues()[i]) );
163 }
164 */
165 }
166 SCIP_CALL_ABORT(SCIPgetSolVals(scip, newsol, SCIPgetNVars(scip), SCIPgetVars(scip), vals) );
167 SCIP_Bool success;
168 // SCIP_CALL_ABORT( SCIPaddSolFree(scip, &newsol, &success) );
169#if (SCIP_VERSION < 321 || ( SCIP_VERSION == 321 && SCIP_SUBVERSION < 2) )
170 SCIP_CALL_ABORT( SCIPtrySolFree(scip, &newsol, FALSE, TRUE, TRUE, TRUE, &success) );
171#else
172 SCIP_CALL_ABORT( SCIPtrySolFree(scip, &newsol, FALSE, TRUE, TRUE, TRUE, TRUE, &success) );
173#endif
174 // std::cout << "** 1 ** success = " << success << std::endl;
175 }
176
177 /** create presolved problem instance that is solved by ParaSCIP form scip environment in this object */
179 SCIP **scip
180 );
181
182 SCIP *getScip(
183 )
184 {
185 return scip;
186 }
187
188 /** create presolved problem instance that is solved by ParaSCIP */
190 SCIP *scip,
191 int method, // transferring method
192 bool noPreprocessingInLC, // LC preprocesing settings
193 bool usetRootNodeCuts,
194 ScipDiffParamSet *scipDiffParamSetRoot,
195 ScipDiffParamSet *scipDiffParamSet,
196 char *settingsNameLC, // LC preprocesing settings
197 char *isolname
198 );
199
200 /** stringfy ParaCalculationState */
201 const std::string toString(
202 )
203 {
204 return std::string("Should be written from scip environment.");
205 }
206
207 // int getOrigProbIndex(int index){ return mapToOriginalIndecies[index]; }
208 const char *getProbName()
209 {
210 if( orgScip )
211 {
212 return SCIPgetProbName(orgScip);
213 }
214 else
215 {
216 return SCIPgetProbName(scip);
217 }
218 }
219
220 int getNVars(){ return nVars; }
222 SCIP_Real getVarLb(int i)
223 {
224 SCIP_VAR **vars = SCIPgetVars(scip);
225 return SCIPvarGetLbGlobal(vars[i]);
226 }
227 SCIP_Real getVarUb(int i)
228 {
229 SCIP_VAR **vars = SCIPgetVars(scip);
230 return SCIPvarGetUbGlobal(vars[i]);
231 }
232 int getVarType(int i)
233 {
234 SCIP_VAR **vars = SCIPgetVars(scip);
235 return SCIPvarGetType(vars[i]);
236 }
237 SCIP_Real getObjCoef(int i)
238 {
239 SCIP_VAR **vars = SCIPgetVars(scip);
240 return SCIPvarGetObj(vars[i]);
241 }
242
243 const char *getVarName(int i)
244 {
245 SCIP_VAR **vars = SCIPgetVars(scip);
246 return SCIPvarGetName(vars[i]);
247 }
248
250 {
251 return SCIPgetNConss(scip);
252 }
253
254 SCIP_Real getLhsLinear(int i)
255 {
256 SCIP_CONS **conss = SCIPgetConss(scip);
257 SCIP_CONSHDLR* conshdlr = SCIPconsGetHdlr(conss[i]);
258 assert( conshdlr != NULL );
259 if( strcmp(SCIPconshdlrGetName(conshdlr),"linear") != 0 )
260 {
261 THROW_LOGICAL_ERROR2("invalid constraint type exists; consname = ", SCIPconshdlrGetName(conshdlr));
262 }
263 return SCIPgetLhsLinear(scip, conss[i]);
264 }
265
266 SCIP_Real getRhsLinear(int i)
267 {
268 SCIP_CONS **conss = SCIPgetConss(scip);
269 SCIP_CONSHDLR* conshdlr = SCIPconsGetHdlr(conss[i]);
270 assert( conshdlr != NULL );
271 if( strcmp(SCIPconshdlrGetName(conshdlr),"linear") != 0 )
272 {
273 THROW_LOGICAL_ERROR2("invalid constraint type exists; consname = ", SCIPconshdlrGetName(conshdlr));
274 }
275 return SCIPgetRhsLinear(scip, conss[i]);
276 }
277
278 int getNVarsLinear(int i)
279 {
280 SCIP_CONS **conss = SCIPgetConss(scip);
281 SCIP_CONSHDLR* conshdlr = SCIPconsGetHdlr(conss[i]);
282 assert( conshdlr != NULL );
283 if( strcmp(SCIPconshdlrGetName(conshdlr),"linear") != 0 )
284 {
285 THROW_LOGICAL_ERROR2("invalid constraint type exists; consname = ", SCIPconshdlrGetName(conshdlr));
286 }
287 return SCIPgetNVarsLinear(scip, conss[i]);
288 }
289
290 int getIdxLinearCoefVar(int i, int j) // i-th constraint, j-th variable
291 {
292 SCIP_CONS **conss = SCIPgetConss(scip);
293 SCIP_VAR **vars = SCIPgetVarsLinear(scip, conss[i]);
294 return SCIPvarGetProbindex(vars[j]);
295 }
296
297 SCIP_Real *getLinearCoefs(int i) // i-th constraint, j-th variable
298 {
299 SCIP_CONS **conss = SCIPgetConss(scip);
300 return SCIPgetValsLinear(scip, conss[i]);
301 }
302
303 const char *getConsName(int i)
304 {
305 SCIP_CONS **conss = SCIPgetConss(scip);
306 return SCIPconsGetName(conss[i]);
307 }
308
309 /** set user plugins */
310 void setUserPlugins(ScipUserPlugins *inUi) { /** maybe called, no need to do anything */ }
311
312 /** include user plugins */
313 void includeUserPlugins(SCIP *inScip){/** should not be called **/}
314
315 virtual void setFileName( const char *file ) = 0;
316
318
320
321 int getOrigProbIndex(int index)
322 {
323 assert(mapToOriginalIndecies);
324 return mapToOriginalIndecies[index];
325 }
326
328 {
329 assert(mapToOriginalIndecies);
330 int *extract = mapToOriginalIndecies;
332 return extract;
333 }
334
336 {
338 int *extract = mapToSolverLocalIndecies;
340 return extract;
341 }
342
343 /*
344 int *extractProbIndexMap()
345 {
346 assert(mapToProbIndecies);
347 int *extract = mapToProbIndecies;
348 mapToProbIndecies = 0;
349 return extract;
350 }
351 */
352
354 {
355 assert( orgScip );
356 return scip;
357 }
358
360 {
361 return SCIPgetObjsense(scip);;
362 }
363
365 {
367 }
368
370 {
372 }
373
374};
375
376}
377
378namespace ParaSCIP
379{
380
381/** ScipInstanceTh */
383{
384 const char *getFileName()
385 {
386 std::cout << "This function should name be used for FiberSCIP!" << std::endl;
387 abort();
388 }
389public:
390 /** constructor */
392 )
393 {
394 }
395
396 /** constructor : only called from ScipInitiator */
398 SCIP *inScip,
399 int method
400 );
401
402 /** destractor */
404 )
405 {
406 }
407
408 /** broadcasts instance to all solvers */
409 int bcast(UG::ParaComm *comm, int rank, int method);
410
411 void setFileName( const char *file )
412 {
413 std::cout << "This function should name be used for FiberSCIP!" << std::endl;
414 abort();
415 }
416
417};
418
420
421}
422
423#endif // __SCIP_PARA_INSTANCE_TH_H__
424
Base class of communicator for UG Framework.
Base class for instance data.
void setFileName(const char *file)
int bcast(UG::ParaComm *comm, int rank, int method)
double convertToInternalValue(double externalValue)
const std::string toString()
const char * getVarName(int i)
SCIP_Real * getLinearCoefs(int i)
const char * getConsName(int i)
void getSolValuesForOriginalProblem(ScipParaSolution *sol, SCIP_Real *vals)
virtual const char * getFileName()=0
double convertToExternalValue(double internalValue)
void setUserPlugins(ScipUserPlugins *inUi)
virtual void setFileName(const char *file)=0
void createProblem(SCIP *scip, int method, bool noPreprocessingInLC, bool usetRootNodeCuts, ScipDiffParamSet *scipDiffParamSetRoot, ScipDiffParamSet *scipDiffParamSet, char *settingsNameLC, char *isolname)
void copyScipEnvironment(SCIP **scip)
int getIdxLinearCoefVar(int i, int j)
void includeUserPlugins(SCIP *inScip)
class for instance data
Base class of communicator object.
Definition: paraComm.h:102
static ScipParaCommTh * comm
Definition: fscip.cpp:73
ScipParaInstanceTh * ScipParaInstanceThPtr
#define THROW_LOGICAL_ERROR2(msg1, msg2)
Definition: paraDef.h:69
SCIP parameter set to be transferred ( Only keep difference between default settings ).
ParaSolution extension for SCIP solver.
SCIP user plugins.