Scippy

UG

Ubiquity Generator framework

scipDiffParamSet.cpp
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 scipDiffParamSet.cpp
27 * @brief SCIP parameter set to be transferred ( Only keep difference between default settings ).
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#include <string.h>
38#include <cassert>
39#include "scipDiffParamSet.h"
40
41using namespace UG;
42using namespace ParaSCIP;
43
44/** allocate memory for names and values */
45void
46ScipDiffParamSet::allocateMemoty(
47 )
48{
49 if( boolParamNamesSize > 0 )
50 {
52 }
53 if (numBoolParams > 0 )
54 {
55 boolParamValues = new unsigned int[numBoolParams];
56 }
57 if (intParamNamesSize > 0 )
58 {
60 }
61 if( numIntParams > 0 )
62 {
64 }
65 if( longintParamNamesSize > 0 )
66 {
68 }
69 if( numLongintParams > 0 )
70 {
71 longintParamValues = new long long[numLongintParams];
72 }
73 if( realParamNamesSize > 0 )
74 {
76 }
77 if( numRealParams )
78 {
79 realParamValues = new double[numRealParams];
80 }
81 if( charParamNamesSize > 0 )
82 {
84 }
85 if( numCharParams > 0 )
86 {
88 }
89 if( stringParamNamesSize > 0 )
90 {
92 }
93 if( stringParamValuesSize > 0 )
94 {
96 }
97}
98
99/** constructor with scip */
101 SCIP *scip
102 )
103 : numBoolParams(0), boolParamNamesSize(0), boolParamNames(0), boolParamValues(0),
104 numIntParams(0), intParamNamesSize(0), intParamNames(0), intParamValues(0),
105 numLongintParams(0), longintParamNamesSize(0), longintParamNames(0), longintParamValues(0),
106 numRealParams(0), realParamNamesSize(0), realParamNames(0), realParamValues(0),
107 numCharParams(0), charParamNamesSize(0), charParamNames(0), charParamValues(0),
108 numStringParams(0), stringParamNamesSize(0), stringParamNames(0), stringParamValuesSize(0), stringParamValues(0)
109{
110 /** get Parameters */
111 int nParams = SCIPgetNParams (scip);
112 SCIP_PARAM **params = SCIPgetParams (scip);
113
114 /** count the number of parameters for each type */
115 for( int i = 0; i < nParams; i++ )
116 {
117 if( !SCIPparamIsDefault (params[i]) )
118 {
119 switch (SCIPparamGetType (params[i]))
120 {
121 case SCIP_PARAMTYPE_BOOL:
123 boolParamNamesSize += ( strlen(SCIPparamGetName(params[i]) ) + 1 );
124 break;
125 case SCIP_PARAMTYPE_INT:
126 numIntParams++;
127 intParamNamesSize += ( strlen(SCIPparamGetName(params[i]) ) + 1 );
128 break;
129 case SCIP_PARAMTYPE_LONGINT:
131 longintParamNamesSize += ( strlen(SCIPparamGetName(params[i]) ) + 1 );
132 break;
133 case SCIP_PARAMTYPE_REAL:
135 realParamNamesSize += ( strlen(SCIPparamGetName(params[i]) ) + 1 );
136 break;
137 case SCIP_PARAMTYPE_CHAR:
139 charParamNamesSize += ( strlen(SCIPparamGetName(params[i]) ) + 1 );
140 break;
141 case SCIP_PARAMTYPE_STRING:
143 stringParamNamesSize += ( strlen(SCIPparamGetName(params[i]) ) + 1 );
144 stringParamValuesSize += ( strlen( SCIPparamGetString(params[i]) ) + 1 );
145 break;
146 default:
147 THROW_LOGICAL_ERROR2("Invalid SCIP parameter type: Type = ", params[i]);
148 }
149 }
150 }
151
152 /** allocate memory */
154
155 /** set names and values for each type */
156 int iBool = 0;
157 int iInt = 0;
158 int iLongint = 0;
159 int iReal = 0;
160 int iChar = 0;
161 int iString = 0;
162 char *boolName = boolParamNames;
163 char *intName = intParamNames;
164 char *longintName = longintParamNames;
165 char *realName = realParamNames;
166 char *charName = charParamNames;
167 char *stringName = stringParamNames;
168 char *stringValue = stringParamValues;
169 for( int i = 0; i < nParams; i++ )
170 {
171 if( !SCIPparamIsDefault (params[i]) )
172 {
173 switch (SCIPparamGetType (params[i]))
174 {
175 case SCIP_PARAMTYPE_BOOL:
176 strcpy(boolName, SCIPparamGetName(params[i]));
177 boolParamValues[iBool] = SCIPparamGetBool(params[i]);
178 boolName += ( strlen(SCIPparamGetName(params[i]) ) + 1 );
179 iBool++;
180 break;
181 case SCIP_PARAMTYPE_INT:
182 strcpy(intName, SCIPparamGetName(params[i]));
183 intParamValues[iInt] = SCIPparamGetInt(params[i]);
184 intName += ( strlen( SCIPparamGetName(params[i]) ) + 1 );
185 iInt++;
186 break;
187 case SCIP_PARAMTYPE_LONGINT:
188 strcpy(longintName, SCIPparamGetName(params[i]));
189 longintParamValues[iLongint] = SCIPparamGetLongint(params[i]);
190 longintName += ( strlen( SCIPparamGetName(params[i]) ) + 1 );
191 iLongint++;
192 break;
193 case SCIP_PARAMTYPE_REAL:
194 strcpy(realName, SCIPparamGetName(params[i]));
195 realParamValues[iReal] = SCIPparamGetReal(params[i]);
196 realName += ( strlen( SCIPparamGetName(params[i]) ) + 1 );
197 iReal++;
198 break;
199 case SCIP_PARAMTYPE_CHAR:
200 strcpy(charName, SCIPparamGetName(params[i]));
201 charParamValues[iChar] = SCIPparamGetChar(params[i]);
202 charName += ( strlen( SCIPparamGetName(params[i]) ) + 1 );
203 iChar++;
204 break;
205 case SCIP_PARAMTYPE_STRING:
206 strcpy( stringName, SCIPparamGetName(params[i]) );
207 strcpy( stringValue, SCIPparamGetString(params[i]) );
208 stringName += ( strlen( SCIPparamGetName(params[i]) ) + 1 );
209 stringValue += ( strlen( SCIPparamGetString(params[i]) ) + 1 );
210 iString++;
211 break;
212 default:
213 THROW_LOGICAL_ERROR2("Invalid SCIP parameter type: Type = ", params[i]);
214 }
215 }
216 }
217
218 assert(
219 iBool == numBoolParams && iInt == numIntParams && iLongint == numLongintParams &&
220 iReal == numRealParams && iChar == numCharParams && iString == numStringParams &&
221 boolParamNamesSize == (unsigned)( boolName - boolParamNames ) &&
222 intParamNamesSize == (unsigned)( intName - intParamNames ) &&
223 longintParamNamesSize == (unsigned)( longintName - longintParamNames ) &&
224 realParamNamesSize == (unsigned)( realName - realParamNames ) &&
225 charParamNamesSize == (unsigned)( charName - charParamNames ) &&
226 stringParamNamesSize == (unsigned)( stringName - stringParamNames ) &&
227 stringParamValuesSize == (unsigned)( stringValue - stringParamValues )
228 );
229}
230
231/** set these parameter values in scip environment */
232void
234 SCIP *scip
235 )
236{
237 char *paramName;
238
239 /** set boolean parameter values in scip environment */
240 paramName = boolParamNames;
241 for( int i = 0; i < numBoolParams; i++ )
242 {
243 SCIP_CALL_ABORT(
244 SCIPsetBoolParam(scip, paramName, boolParamValues[i])
245 );
246 paramName += ( strlen(paramName) + 1 );
247 }
248 assert( boolParamNamesSize == (unsigned)( paramName - boolParamNames ) );
249
250 /** set int parameter values in scip environment */
251 paramName = intParamNames;
252 for( int i = 0; i < numIntParams; i++ )
253 {
254 SCIP_CALL_ABORT(
255 SCIPsetIntParam(scip, paramName, intParamValues[i])
256 );
257 paramName += ( strlen(paramName) + 1 );
258 }
259 assert( intParamNamesSize == (unsigned)( paramName - intParamNames ) );
260
261 /** set longint parameter values in scip environment */
262 paramName = longintParamNames;
263 for( int i = 0; i < numLongintParams; i++ )
264 {
265 SCIP_CALL_ABORT(
266 SCIPsetLongintParam(scip, paramName, longintParamValues[i])
267 );
268 paramName += ( strlen(paramName) + 1 );
269 }
270 assert( longintParamNamesSize == (unsigned)( paramName - longintParamNames ) );
271
272 /** set real parameter values in scip environment */
273 paramName = realParamNames;
274 for( int i = 0; i < numRealParams; i++ )
275 {
276 SCIP_CALL_ABORT(
277 SCIPsetRealParam(scip, paramName, realParamValues[i])
278 );
279 paramName += ( strlen(paramName) + 1 );
280 }
281 assert( realParamNamesSize == (unsigned)( paramName - realParamNames ) );
282
283 /** set char parameter values in scip environment */
284 paramName = charParamNames;
285 for( int i = 0; i < numCharParams; i++ )
286 {
287 SCIP_CALL_ABORT(
288 SCIPsetCharParam(scip, paramName, charParamValues[i])
289 );
290 paramName += ( strlen(paramName) + 1 );
291 }
292 assert( charParamNamesSize == (unsigned)( paramName - charParamNames ) );
293
294 /** set string parameter values in scip environment */
295 paramName = stringParamNames;
296 char *paramValue = stringParamValues;
297 for( int i = 0; i < numStringParams; i++ )
298 {
299 SCIP_CALL_ABORT(
300 SCIPsetStringParam(scip, paramName, paramValue)
301 );
302 paramName += ( strlen(paramName) + 1 );
303 paramValue += ( strlen(paramValue) + 1 );
304 }
305 assert( stringParamNamesSize == (unsigned)( paramName - stringParamNames ) );
306 assert( stringParamValuesSize == (unsigned)( paramValue - stringParamValues ) );
307
308}
309
310/** stringfy DiffParamSet */
311std::string
313 )
314{
315 std::ostringstream s;
316
317 char *paramName;
318
319 /** stringfy boolean parameter values */
320 paramName = boolParamNames;
321 for( int i = 0; i < numBoolParams; i++ )
322 {
323 s << paramName << " = ";
324 if( boolParamValues[i] )
325 {
326 s << "TRUE";
327 }
328 else
329 {
330 s << "FALSE";
331 }
332 s << std::endl;
333 paramName += strlen(paramName) + 1;
334 }
335 assert( boolParamNamesSize == (unsigned)( paramName - boolParamNames ) );
336
337 /** stringfy int parameter values */
338 paramName = intParamNames;
339 for( int i = 0; i < numIntParams; i++ )
340 {
341 s << paramName << " = " << intParamValues[i] << std::endl;
342 paramName += strlen(paramName) + 1;
343 }
344 assert( intParamNamesSize == (unsigned)( paramName - intParamNames ) );
345
346 /** stringfy longint parameter values */
347 paramName = longintParamNames;
348 for( int i = 0; i < numLongintParams; i++ )
349 {
350 s << paramName << " = " << longintParamValues[i] << std::endl;
351 paramName += strlen(paramName) + 1;
352 }
353 assert( longintParamNamesSize == (unsigned)( paramName - longintParamNames ) );
354
355 /** stringfy real parameter values */
356 paramName = realParamNames;
357 for( int i = 0; i < numRealParams; i++ )
358 {
359 s << paramName << " = " << realParamValues[i] << std::endl;
360 paramName += strlen(paramName) + 1;
361 }
362 assert( realParamNamesSize == (unsigned)( paramName - realParamNames ) );
363
364 /** stringfy char parameter values */
365 paramName = charParamNames;
366 for( int i = 0; i < numCharParams; i++ )
367 {
368 s << paramName << " = " << charParamValues[i] << std::endl;
369 paramName += strlen(paramName) + 1;
370 }
371 assert( charParamNamesSize == (unsigned)( paramName - charParamNames ) );
372
373 /** stringfy string parameter values */
374 paramName = stringParamNames;
375 char *paramValue = stringParamValues;
376 for( int i = 0; i < numStringParams; i++ )
377 {
378 s << paramName << " = \"" << paramValue << "\"" << std::endl;
379 paramName += strlen(paramName) + 1;
380 paramValue += strlen(paramValue) + 1;
381 }
382 return s.str();
383
384}
385
386#ifdef UG_WITH_ZLIB
387/** write ScipDiffParamSet */
388void
390 gzstream::ogzstream &out
391 )
392{
393 /** write boolean parameter names and values */
394 out.write((char *)&numBoolParams, sizeof(int));
395 out.write((char *)&boolParamNamesSize, sizeof(int));
396 if( numBoolParams > 0 )
397 {
399 for( int i = 0; i < numBoolParams; i++ )
400 {
401 out.write((char *)&boolParamValues[i], sizeof(unsigned int));
402 }
403 }
404
405
406 /** write int parameter names and values */
407 out.write((char *)&numIntParams, sizeof(int));
408 out.write((char *)&intParamNamesSize, sizeof(int));
409 if( numIntParams > 0 )
410 {
412 for( int i = 0; i < numIntParams; i++ )
413 {
414 out.write((char *)&intParamValues[i], sizeof(int));
415 }
416 }
417
418
419 /** write longint parameter names and values */
420 out.write((char *)&numLongintParams, sizeof(int));
421 out.write((char *)&longintParamNamesSize, sizeof(int));
422 if( numLongintParams > 0 )
423 {
425 for( int i = 0; i < numLongintParams; i++ )
426 {
427 out.write((char *)&longintParamValues[i], sizeof(long long));
428 }
429 }
430
431 /** write real parameter names and values */
432 out.write((char *)&numRealParams, sizeof(int));
433 out.write((char *)&realParamNamesSize, sizeof(int));
434 if( numRealParams > 0 )
435 {
437 for( int i = 0; i < numRealParams; i++ )
438 {
439 out.write((char *)&realParamValues[i], sizeof(double));
440 }
441 }
442
443
444 /** write char parameter values */
445 out.write((char *)&numCharParams, sizeof(int));
446 out.write((char *)&charParamNamesSize, sizeof(int));
447 if( numCharParams > 0 )
448 {
450 for( int i = 0; i < numCharParams; i++ )
451 {
452 out.write((char *)&charParamValues[i], sizeof(char));
453 }
454 }
455
456 /** write string parameter values */
457 out.write((char *)&numStringParams, sizeof(int));
458 out.write((char *)&stringParamNamesSize, sizeof(int));
459 out.write((char *)&stringParamValuesSize, sizeof(int));
460 if( numStringParams > 0 )
461 {
464 }
465}
466
467/** read ScipDiffParamSet */
468bool
470 ParaComm *comm,
471 gzstream::igzstream &in
472 )
473{
474 /** read boolean parameter names and values */
475 in.read((char *)&numBoolParams, sizeof(int));
476 if( in.eof() ) return false;
477 in.read((char *)&boolParamNamesSize, sizeof(int));
478 if( numBoolParams > 0 )
479 {
481 boolParamValues = new unsigned int[numBoolParams];
483 for( int i = 0; i < numBoolParams; i++ )
484 {
485 in.read((char *)&boolParamValues[i], sizeof(unsigned int));
486 }
487 }
488
489 /** read int parameter names and values */
490 in.read((char *)&numIntParams, sizeof(int));
491 in.read((char *)&intParamNamesSize, sizeof(int));
492 if( numIntParams > 0 )
493 {
495 intParamValues = new int[numIntParams];
497 for( int i = 0; i < numIntParams; i++ )
498 {
499 in.read((char *)&intParamValues[i], sizeof(int));
500 }
501 }
502
503 /** read longint parameter names and values */
504 in.read((char *)&numLongintParams, sizeof(int));
505 in.read((char *)&longintParamNamesSize, sizeof(int));
506 if( numLongintParams > 0 )
507 {
509 longintParamValues = new long long[numLongintParams];
511 for( int i = 0; i < numLongintParams; i++ )
512 {
513 in.read((char *)&longintParamValues[i], sizeof(long long));
514 }
515 }
516
517 /** read real parameter names and values */
518 in.read((char *)&numRealParams, sizeof(int));
519 in.read((char *)&realParamNamesSize, sizeof(int));
520 if( numRealParams > 0 )
521 {
523 realParamValues = new double[numRealParams];
525 for( int i = 0; i < numRealParams; i++ )
526 {
527 in.read((char *)&realParamValues[i], sizeof(double));
528 }
529 }
530
531 /** read char parameter values */
532 in.read((char *)&numCharParams, sizeof(int));
533 in.read((char *)&charParamNamesSize, sizeof(int));
534 if( numCharParams > 0 )
535 {
537 charParamValues = new char[numCharParams];
539 for( int i = 0; i < numCharParams; i++ )
540 {
541 in.read((char *)&charParamValues[i], sizeof(char));
542 }
543 }
544
545 /** read string parameter values */
546 in.read((char *)&numStringParams, sizeof(int));
547 in.read((char *)&stringParamNamesSize, sizeof(int));
548 in.read((char *)&stringParamValuesSize, sizeof(int));
549 if( numStringParams > 0 )
550 {
555 }
556
557 return true;
558}
559
560#endif
bool read(UG::ParaComm *comm, gzstream::igzstream &in)
void setParametersInScip(SCIP *scip)
void write(gzstream::ogzstream &out)
Base class of communicator object.
Definition: paraComm.h:102
static ScipParaCommTh * comm
Definition: fscip.cpp:73
#define THROW_LOGICAL_ERROR2(msg1, msg2)
Definition: paraDef.h:69
SCIP parameter set to be transferred ( Only keep difference between default settings ).