Scippy

UG

Ubiquity Generator framework

scipParaInstance.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 scipParaInstance.h
27 * @brief ParaInstance extenstion 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_INSTANCE_H__
38#define __SCIP_PARA_INSTANCE_H__
39
40#if defined(_COMM_MPI_WORLD)
41#include "ug/paraDef.h"
43#include "scipUserPlugins.h"
44#include "scip/scip.h"
45#include "scipDiffParamSet.h"
46#include "scipParaSolution.h"
47
48namespace ParaSCIP
49{
50/** ScipInstance */
52{
53protected:
54 SCIP *orgScip; /**< original scip enverionemt */
55 SCIP *paraInstanceScip; /**< scip shared by all solvers */
56 int lProbName; /**< length of problem name */
57 char *probName; /**< problem name */
58 int nCopies; /**< the number of copies in Solver side */
59 int origObjSense; /**< objective sense : SCIP_OBJSENSE_MAXIMIZE = -1, SCIP_OBJSENSE_MINIMIZE = +1 */
60 /** Do not set objScale and objOffset in SCIP */
61 SCIP_Real objScale; /**< scalar applied to objective function; original objective value is extobj = objsense * objscale * (intobj + objoffset) */
62 SCIP_Real objOffset; /**< objective offset from bound shifting and fixing */
63 int nVars; /**< number of variables */
64 int varIndexRange; /**< index ragne of variables */
65 SCIP_Real *varLbs; /**< array of current lower bound of variable */
66 SCIP_Real *varUbs; /**< array of current upper bound of variable */
67 SCIP_Real *objCoefs; /**< coefficient values: allocation size = nVars */
68 int *varTypes; /**< array of variable type */
69 size_t lVarNames; /**< length of varNames area */
70 char *varNames; /**< variable names : names are concatenated */
71 int *posVarNames; /**< positions of varNames */
72 int *mapToOriginalIndecies; /**< array of indices to map to original problem's probindices */
73 /**< NOTE: these indices are not transferred. Only valid for Initiator object */
74 int *mapToSolverLocalIndecies; /**< array of reverse indices mapToOriginalIndecies */
75 // int *mapToProbIndecies; /**< in Solver, one more variable map is need in some cases */
76 int nConss; /**< number of constraints */
77 int lConsNames; /**< length of consNames area */
78 char *consNames; /**< constraint names : names are concatenated */
79 int *posConsNames; /**< positions of consNames */
80
81 bool copyIncreasedVariables; /**< indicate if SCIP copy increase the number of veariables or not */
82
83
84 /*************************
85 * for linear constrains *
86 * ***********************/
87 int nLinearConss; /**< number of linear constrains */
88 int *idxLinearConsNames; /**< array of indices to consName */
89 SCIP_Real *linearLhss; /**< array of lhs */
90 SCIP_Real *linearRhss; /**< array of rhs */
91 int *nLinearCoefs; /**< array of number of coefficient values for linear constrains */
92 SCIP_Real **linearCoefs; /**< array of non-zero coefficient values of linear constrains */
93 int **idxLinearCoefsVars; /**< array of indices of on-zero coefficient values of linear constrains */
94
95 /********************************************************
96 * for setppc constrains (NOTE:: should be normalized ) *
97 * *****************************************************/
98 int nSetppcConss; /**< number of setppc constrains */
99 int *idxSetppcConsNames; /**< array of indices to consName */
100 int *nIdxSetppcVars; /**< array of numbers of indices of variables for setppc constrains */
101 int *setppcTypes; /**< setppc Types */
102 int **idxSetppcVars; /**< array of indices of variables for setppc constrains */
103
104 /*********************************
105 * for logicor constrains (NOTE:: should be normalized ) *
106 *********************************/
107 int nLogicorConss; /**< number of logical constrains */
108 int *idxLogicorConsNames; /**< array of indices to consName */
109 int *nIdxLogicorVars; /**< array of number of indices of variables for logicor constrains */
110 int **idxLogicorVars; /**< array of indices of of variables for logicor constrains */
111
112 /*********************************
113 * for knapsack constrains (NOTE:: should be normalized ) *
114 *********************************/
115 int nKnapsackConss; /**< number of knapsack constrains */
116 int *idxKnapsackConsNames; /**< array of indices to consName */
117 SCIP_Longint *capacities; /**< array of capacities(rhs) */
118 int *nLKnapsackCoefs; /**< array of number of coefficient values for knapsack constrains */
119 SCIP_Longint **knapsackCoefs; /**< array of non-zero coefficient values of knapsack constrains */
120 int **idxKnapsackCoefsVars; /**< array of indices of on-zero coefficient values of knapsack constrains */
121
122 /**********************************
123 * for varbound constrains (NOTE:: should be normalized ) *
124 *********************************/
125 int nVarboundConss; /**< number of varbound constrains */
126 int *idxVarboundConsNames; /**< array of indices to consName */
127 SCIP_Real *varboundLhss; /**< array of lhs */
128 SCIP_Real *varboundRhss; /**< array of rhs */
129 int *idxVarboundCoefVar1s; /**< array of indices of variable 1 */
130 SCIP_Real *varboundCoef2s; /**< array of coefficient of variable 2 */
131 int *idxVarboundCoefVar2s; /**< array of indices of variable 2 */
132
133 /**********************************
134 * for bounddisjunction constrains (NOTE:: should be normalized ) *
135 *********************************/
136 int nVarBoundDisjunctionConss; /**< number of bounddisjunction constrains */
137 int *idxBoundDisjunctionConsNames; /**< array of indices to consName */
138 int *nVarsBoundDisjunction; /** array of the number of variables */
139 int **idxVarBoundDisjunction; /**< array of indices of variables */
140 SCIP_BOUNDTYPE **boundTypesBoundDisjunction; /**< array of bound types */
141 SCIP_Real **boundsBoundDisjunction; /**< array of bounds */
142
143 /*********************************
144 * for SOS1 constraints (NOTE:: should be normalized ) *
145 *********************************/
146 int nSos1Conss; /**< number of SOS1 constraints */
147 int *idxSos1ConsNames; /**< array of indices to consName */
148 int *nSos1Coefs; /**< array of number of coefficient values for SOS1 constrains */
149 SCIP_Real **sos1Coefs; /**< array of non-zero coefficient values of SOS1 constrains */
150 int **idxSos1CoefsVars; /**< array of indices of on-zero coefficient values of SOS1 constrains */
151
152 /*********************************
153 * for SOS2 constraints (NOTE:: should be normalized ) *
154 *********************************/
155 int nSos2Conss; /**< number of SOS1 constraints */
156 int *idxSos2ConsNames; /**< array of indices to consName */
157 int *nSos2Coefs; /**< array of number of coefficient values for SOS2 constrains */
158 SCIP_Real **sos2Coefs; /**< array of non-zero coefficient values of SOS2 constrains */
159 int **idxSos2CoefsVars; /**< array of indices of on-zero coefficient values of SOS2 constrains */
160
161 /**********************************
162 * for aggregated constrains *
163 *********************************/
164 int nAggregatedConss; /**< number of aggregated constrains = number of aggregated vars */
165 int lAggregatedVarNames; /**< length of aggregatedVarNames area */
166 char *aggregatedVarNames; /**< aggregated var names: names are concatenated */
167 int *posAggregatedVarNames; /**< positions of aggregatedVarNames */
168 /** no need lower bound and upper bound: always free */
169 int lAggregatedConsNames; /**< length of aggregatedConsNames area */
170 char *aggregatedConsNames; /**< aggregated cons names: names are concatenated */
171 int *posAggregatedConsNames; /**< positions of aggregatedConsNames */
172 SCIP_Real *aggregatedLhsAndLhss; /**< array of lhs and rhs (lhs = rhs) */
173 int *nAggregatedCoefs; /**< array of number of coefficient values for aggregated constrains */
174 SCIP_Real **aggregatedCoefs; /**< array of non-zero coefficient values of aggregated constrains */
175 int **idxAggregatedCoefsVars; /**< array of indices of on-zero coefficient values of aggregated constrains */
176
177 ScipUserPlugins *userPlugins; /**< user plugins */
178
179 /**********************************
180 * private functions *
181 **********************************/
183 void addOrdinaryConstraintName(int c, SCIP_CONS *cons );
184 void setLinearConstraint( SCIP *scip, int c, SCIP_CONS *cons );
186 void setSetppcConstraint( SCIP *scip, int c, SCIP_CONS *cons );
188 void setLogicorConstraint( SCIP *scip, int c, SCIP_CONS *cons );
190 void setKnapsackConstraint( SCIP *scip, int c, SCIP_CONS *cons );
192 void setVarboundConstraint( SCIP *scip, int c, SCIP_CONS *cons );
194 void setBoundDisjunctionConstraint( SCIP *scip, int c, SCIP_CONS *cons );
196 void setSos1Constraint( SCIP *scip, int c, SCIP_CONS *cons, SCIP_CONS** consSOS1 );
197 void createSos1ConstraintsInSCIP( SCIP *scip );
198 void setSos2Constraint( SCIP *scip, int c, SCIP_CONS *cons, SCIP_CONS** consSOS2 );
199 void createSos2ConstraintsInSCIP( SCIP *scip );
200 void getActiveVariables( SCIP *scip, SCIP_VAR **vars, SCIP_Real *scalars, int *nvars,
201 SCIP_Real *constant, SCIP_Bool transformed );
202 void collectAggregatedVars(SCIP *scip, int nvars, SCIP_VAR** vars, int* nAggregatedVars,
203 SCIP_VAR*** aggregatedVars, SCIP_HASHTABLE** varAggregated );
205 SCIP* scip, /**< SCIP data structure */
206 int c, /**< aggregated constraint number */
207 const char* constName, /**< constraint name */
208 SCIP_VAR** vars, /**< array of variables */
209 SCIP_Real* vals, /**< array of values */
210 int nvars, /**< number of variables */
211 SCIP_Real lhsAndrhs /**< right hand side = left hand side */
212 );
214 SCIP* scip, /**< SCIP data structure */
215 int nvars, /**< number of mutable variables in the problem */
216 int nAggregatedVars, /**< number of aggregated variables */
217 SCIP_VAR** aggregatedVars /**< array storing the aggregated variables */
218 );
220 virtual const char *getFileName() = 0;
221 bool addRootNodeCuts(SCIP *scip, ScipDiffParamSet *scipDiffParamSetRoot);
222public:
223 /** constructor */
225 ) : orgScip(0), paraInstanceScip(0), lProbName(0), probName(0), nCopies(0), origObjSense(0), objScale(0.0),
226 objOffset(0.0), nVars(0), varIndexRange(0),
227 varLbs(0), varUbs(0), objCoefs(0), varTypes(0), lVarNames(0), varNames(0),
228 posVarNames(0), mapToOriginalIndecies(0), mapToSolverLocalIndecies(0), // mapToProbIndecies(0),
229 nConss(0), lConsNames(0), consNames(0),
246 userPlugins(0)
247 {
248 }
249
250 /** constractor : only called from ScipInitiator */
252 SCIP *scip,
253 int method // transferring method
254 );
255
256 /** destractor */
257 virtual ~ScipParaInstance(
258 );
259
260 /** convert an internal value to external value
261 double convertToExternalValue(double internalValue)
262 {
263 return ( (internalValue + objOffset) * objScale * origObjSense );
264 }
265
266 // convert an external value to internal value
267 double convertToInternalValue(double exteranlValue)
268 {
269 return ( ( exteranlValue / ( objScale * origObjSense ) ) - objOffset );
270 }
271 */
272
273 /** convert an internal value to external value */
274 double convertToExternalValue(double internalValue)
275 {
276 if( orgScip )
277 {
278 return SCIPretransformObj(orgScip, SCIPretransformObj(paraInstanceScip, internalValue) );
279 }
280 else
281 {
282 return SCIPretransformObj(paraInstanceScip, internalValue);
283 }
284 }
285
286 /** convert an external value to internal value */
287 double convertToInternalValue(double externalValue)
288 {
289 if( orgScip )
290 {
291 return SCIPtransformObj(paraInstanceScip, SCIPtransformObj(orgScip, externalValue));
292 }
293 else
294 {
295 return SCIPtransformObj(paraInstanceScip, externalValue);
296 }
297 }
298
299
300 /** get solution values for the original problem */
302 {
303 assert( orgScip );
304 assert( mapToOriginalIndecies );
305 SCIP_SOL* newsol;
306 SCIP_CALL_ABORT( SCIPcreateSol(paraInstanceScip, &newsol, 0) );
307 SCIP_VAR **vars = SCIPgetVars(paraInstanceScip);
308 assert( SCIPgetNVars(paraInstanceScip) == sol->getNVars() );
309 int j = 0;
310 for( int i = 0; i < sol->getNVars(); i++ )
311 {
312 if( mapToOriginalIndecies[i] >= 0 )
313 {
314 // assert( sol->indexAmongSolvers(j) == mapToOriginalIndecies[i]);
315 if( sol->indexAmongSolvers(j) == mapToOriginalIndecies[i] )
316 {
317 SCIP_CALL_ABORT( SCIPsetSolVal(paraInstanceScip, newsol, vars[sol->indexAmongSolvers(j)], sol->getValues()[j]) );
318 j++;
319 }
320 }
321 // SCIP_CALL_ABORT( SCIPsetSolVal(paraInstanceScip, newsol, vars[sol->indexAmongSolvers(i)], sol->getValues()[i]) );
322 /* This is not necessry
323 if( mapToOriginalIndecies )
324 {
325 assert( mapToOriginalIndecies[sol->indexAmongSolvers(i)] < sol->getNVars() );
326 SCIP_CALL_ABORT( SCIPsetSolVal(paraInstanceScip, newsol, vars[sol->indexAmongSolvers(i)], sol->getValues()[i]) );
327 // SCIP_CALL_ABORT( SCIPsetSolVal(paraInstanceScip, newsol, vars[mapToOriginalIndecies[sol->indexAmongSolvers(i)]], sol->getValues()[i]) );
328 }
329 else
330 {
331 assert( sol->indexAmongSolvers(i) < sol->getNVars() );
332 SCIP_CALL_ABORT( SCIPsetSolVal(paraInstanceScip, newsol, vars[sol->indexAmongSolvers(i)], sol->getValues()[i]) );
333 }
334 */
335 }
336 SCIP_CALL_ABORT(SCIPgetSolVals(paraInstanceScip, newsol, SCIPgetNVars(paraInstanceScip), SCIPgetVars(paraInstanceScip), vals) );
337 SCIP_Bool success;
338 SCIP_CALL_ABORT( SCIPaddSolFree(paraInstanceScip, &newsol, &success) );
339 }
340
341 /** create presolved problem instance that is solved by ParaSCIP */
342 void createProblem(
343 SCIP *scip,
344 int method, // transferring method
345 bool noPreprocessingInLC, // LC preprocesing settings
346 bool usetRootNodeCuts,
347 ScipDiffParamSet *scipDiffParamSetRoot,
348 ScipDiffParamSet *scipDiffParamSet,
349 char *settingsNameLC, // LC preprocesing settings
350 char *isolname
351 );
352
353 /** stringfy ParaCalculationState */
354 const std::string toString(
355 );
356
358
360
361 int getOrigProbIndex(int index)
362 {
363 assert(mapToOriginalIndecies);
364 return mapToOriginalIndecies[index];
365 }
366
367 const char *getProbName(){ return probName; }
368
369 void freeMemory();
370
371 int getNVars(){return nVars;}
373 SCIP_Real getVarLb(int i){ return varLbs[i]; }
374 SCIP_Real getVarUb(int i){ return varUbs[i]; }
375 SCIP_Real getObjCoef(int i){ return objCoefs[i]; }
376 int getVarType(int i){ return varTypes[i]; }
377 const char* getVarName(int i){ return &varNames[posVarNames[i]]; }
378
379 int getNConss(){ return nLinearConss; }
380 SCIP_Real getLhsLinear(int i){ return linearLhss[i]; }
381 SCIP_Real getRhsLinear(int i){ return linearRhss[i]; }
382 int getNVarsLinear(int i){ return nLinearCoefs[i]; }
383 int getIdxLinearCoefVar(int i, int j) // i-th constraint, j-th variable
384 {
385 return idxLinearCoefsVars[i][j];
386 }
387 SCIP_Real *getLinearCoefs(int i) // i-th constraint, j-th variable
388 {
389 return linearCoefs[i];
390 }
391 const char *getConsName(int i)
392 {
393 return const_cast<const char*>(&consNames[posConsNames[i]]);
394 }
395
396 SCIP *getScip(){ THROW_LOGICAL_ERROR1("This function is only for FiberSCIP!!"); return 0; }
397
398 /** set user plugins */
400
401 /** include user plugins */
402 void includeUserPlugins(SCIP *inScip)
403 {
404 if( userPlugins )
405 {
406 (*userPlugins)(inScip);
407 }
408 }
409
410 virtual void setFileName(const char *fileName) = 0;
411
413 {
414 assert(mapToOriginalIndecies);
415 int *extract = mapToOriginalIndecies;
417 return extract;
418 }
419
421 {
423 int *extract = mapToSolverLocalIndecies;
425 return extract;
426 }
427
429 {
430 assert( orgScip );
431 return paraInstanceScip;
432 }
433
435 {
436 return origObjSense;
437 }
438
439 virtual bool isCopyIncreasedVariables() = 0;
440 virtual void copyIncrasedVariables() = 0;
441
442};
443
444}
445
446#include "scipParaInstanceMpi.h"
447#endif
448
449#if defined(_COMM_PTH)
450#include "scipParaInstanceTh.h"
451#endif
452
453#endif // __SCIP_PARA_INSTANCE_H__
Base class for instance data.
double convertToInternalValue(double externalValue)
void setLinearConstraint(SCIP *scip, int c, SCIP_CONS *cons)
virtual void setFileName(const char *fileName)=0
bool addRootNodeCuts(SCIP *scip, ScipDiffParamSet *scipDiffParamSetRoot)
void setSos2Constraint(SCIP *scip, int c, SCIP_CONS *cons, SCIP_CONS **consSOS2)
const char * getVarName(int i)
void setAggregatedConstrains(SCIP *scip, int nvars, int nAggregatedVars, SCIP_VAR **aggregatedVars)
SCIP_Real * getLinearCoefs(int i)
void createVarboundConstraintsInSCIP(SCIP *scip)
const char * getConsName(int i)
void setAggregatedConstraint(SCIP *scip, int c, const char *constName, SCIP_VAR **vars, SCIP_Real *vals, int nvars, SCIP_Real lhsAndrhs)
void setSos1Constraint(SCIP *scip, int c, SCIP_CONS *cons, SCIP_CONS **consSOS1)
void getSolValuesForOriginalProblem(ScipParaSolution *sol, SCIP_Real *vals)
virtual const char * getFileName()=0
void createSetppcConstraintsInSCIP(SCIP *scip)
virtual bool isCopyIncreasedVariables()=0
void createLinearConstraintsInSCIP(SCIP *scip)
void addOrdinaryConstraintName(int c, SCIP_CONS *cons)
SCIP_BOUNDTYPE ** boundTypesBoundDisjunction
double convertToExternalValue(double internalValue)
void setSetppcConstraint(SCIP *scip, int c, SCIP_CONS *cons)
ScipUserPlugins * userPlugins
void getActiveVariables(SCIP *scip, SCIP_VAR **vars, SCIP_Real *scalars, int *nvars, SCIP_Real *constant, SCIP_Bool transformed)
void collectAggregatedVars(SCIP *scip, int nvars, SCIP_VAR **vars, int *nAggregatedVars, SCIP_VAR ***aggregatedVars, SCIP_HASHTABLE **varAggregated)
void createSos1ConstraintsInSCIP(SCIP *scip)
void setKnapsackConstraint(SCIP *scip, int c, SCIP_CONS *cons)
void setBoundDisjunctionConstraint(SCIP *scip, int c, SCIP_CONS *cons)
void createSos2ConstraintsInSCIP(SCIP *scip)
void setUserPlugins(ScipUserPlugins *inUi)
void createBoundDisjunctionConstraintInSCIP(SCIP *scip)
void setVarboundConstraint(SCIP *scip, int c, SCIP_CONS *cons)
virtual void copyIncrasedVariables()=0
void setLogicorConstraint(SCIP *scip, int c, SCIP_CONS *cons)
void createProblem(SCIP *scip, int method, bool noPreprocessingInLC, bool usetRootNodeCuts, ScipDiffParamSet *scipDiffParamSetRoot, ScipDiffParamSet *scipDiffParamSet, char *settingsNameLC, char *isolname)
void createLogicorConstraintsInSCIP(SCIP *scip)
void createKnapsackConstraintsInSCIP(SCIP *scip)
void createAggregatedVarsAndConstrainsInSCIP(SCIP *scip)
int getIdxLinearCoefVar(int i, int j)
void includeUserPlugins(SCIP *inScip)
class for instance data
Defines for UG Framework.
#define THROW_LOGICAL_ERROR1(msg1)
Definition: paraDef.h:52
SCIP parameter set to be transferred ( Only keep difference between default settings ).
ScipParaInstance extension for MPI communication.
ScipParaInstance extension for threads communication.
ParaSolution extension for SCIP solver.
SCIP user plugins.