Scippy

UG

Ubiquity Generator framework

scipParaSolution.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 scipParaSolution.h
27  * @brief ParaSolution extension 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_SOLUTION_H__
38 #define __SCIP_PARA_SOLUTION_H__
39 
40 #include <sstream>
41 #include "ug_bb/bbParaSolution.h"
42 #include "scip/scip.h"
43 #include "scipParaSolver.h"
44 
45 namespace ParaSCIP
46 {
47 
48 /** ScipParaSolution class */
50 {
51 protected:
53  int nVars; /**< number of variables */
54  int *indicesAmongSolvers; /**< array of variable indices, mapping SCIPvarGetProbindex to SCIPvarGetIndex */
55  SCIP_Real *values; /**< array of bounds which the branchings */
56 public:
57  /** constructor */
59  )
60  : objectiveFunctionValue(0.0), nVars(0), indicesAmongSolvers(0), values(0)
61  {
62  }
63 
65  ScipParaSolver *solver,
66  SCIP_Real objval,
67  int inNvars,
68  SCIP_VAR ** vars,
69  SCIP_Real * vals
70  )
71  : indicesAmongSolvers(0), values(0)
72  {
73  objectiveFunctionValue = objval;
74  nVars = inNvars;
75  if( nVars > 0 )
76  {
77  indicesAmongSolvers = new int[nVars];
78  values = new SCIP_Real[nVars];
79  if( solver && solver->isOriginalIndeciesMap() )
80  {
81  for(int i = 0; i < nVars; i++ )
82  {
83  indicesAmongSolvers[i] = solver->getOriginalIndex(SCIPvarGetIndex(vars[i]));
84  // indicesAmongSolvers[i] = SCIPvarGetIndex(vars[i]);
85  values[i] = vals[i];
86  // std::cout << i << ": " << indicesAmongSolvers[i] << std::endl;
87  // std::cout << i << ": " << SCIPvarGetName(vars[indicesAmongSolvers[i]]) << ": " << values[i] << std::endl;
88  // std::cout << i << ": " << SCIPvarGetName(vars[i]) << ": " << values[i] << std::endl;
89  }
90  }
91  else
92  {
93  // std::cout << "*** index: name: value ***" << std::endl;
94  for(int i = 0; i < nVars; i++ )
95  {
96  indicesAmongSolvers[i] = SCIPvarGetIndex(vars[i]);
97  values[i] = vals[i];
98  // std::cout << i << ": " << SCIPvarGetName(vars[indicesAmongSolvers[i]]) << ": " << values[i] << std::endl;
99  // std::cout << i << ": " << SCIPvarGetName(vars[i]) << ": " << values[i] << std::endl;
100  }
101  }
102 
103  }
104  }
105 
107  double inObjectiveFunctionValue,
108  int inNVars, /**< number of variables */
109  int *inIndicesAmongSolvers, /**< array of variable indices ( probindex ) */
110  SCIP_Real *inValues /**< array of bounds which the branchings */
111  ) : objectiveFunctionValue(inObjectiveFunctionValue), nVars(inNVars), indicesAmongSolvers(0), values(0)
112  {
113  if( nVars > 0 )
114  {
115  indicesAmongSolvers = new int[inNVars];
116  values = new SCIP_Real[inNVars];
117  for( int i = 0; i < inNVars; i++ )
118  {
119  indicesAmongSolvers[i] = inIndicesAmongSolvers[i];
120  values[i] = inValues[i];
121  }
122  }
123  }
124 
125  /** destructor */
127  )
128  {
129  if( indicesAmongSolvers ) delete [] indicesAmongSolvers;
130  if( values ) delete [] values;
131  }
132 
133  /** get objective function value */
135 
136  /** set objective function value */
137  void setObjectiveFuntionValue(SCIP_Real val){ objectiveFunctionValue = val; }
138 
139  /** get number of variables */
140  int getNVars(){ return nVars; }
141 
142  int indexAmongSolvers(int index){ return indicesAmongSolvers[index]; }
143 
144  SCIP_Real *getValues(){ return values; }
145  void setValue(int i, SCIP_Real val){ assert(i < nVars && i >= 0 ); values[i] = val; }
146 
147 #ifdef UG_WITH_ZLIB
148  /** user should implement write method */
149  void write(gzstream::ogzstream &out);
150 
151  /** user should implement read method */
152  bool read(UG::ParaComm *comm, gzstream::igzstream &in);
153 #endif
154 
155  /** stringfy solution */
156  const std::string toString(
157  )
158  {
159  std::ostringstream s;
160  s << "Obj = " << objectiveFunctionValue << std::endl;
161  for( int i = 0; i < nVars; i++ )
162  {
163  s << "i = " << i;
164  s << ", idx = " << indicesAmongSolvers[i];
165  s << ", val = " << values[i] << std::endl;
166  }
167  return s.str();
168  }
169 
170 };
171 
172 }
173 
174 #endif // __SCIP_PARA_SOLUTION_H__
175 
int getOriginalIndex(int index)
static ScipParaCommTh * comm
Definition: fscip.cpp:73
void setObjectiveFuntionValue(SCIP_Real val)
class for solution
void setValue(int i, SCIP_Real val)
ParaSolver extension for SCIP: Parallelized solver implementation for SCIP.
ScipParaSolution(ScipParaSolver *solver, SCIP_Real objval, int inNvars, SCIP_VAR **vars, SCIP_Real *vals)
void read(ParaComm *comm, const char *filename)
read ParaParams from file
ScipParaSolution(double inObjectiveFunctionValue, int inNVars, int *inIndicesAmongSolvers, SCIP_Real *inValues)
const std::string toString()
Base class of communicator object.
Definition: paraComm.h:101