Scippy

UG

Ubiquity Generator framework

scipParaInitialStat.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 scipParaInitialStat.cpp
27 * @brief ParaInitialStat 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#include <iostream>
38#include <cassert>
39#include "scipParaComm.h"
40#include "scipParaInitialStat.h"
41
42using namespace UG;
43using namespace ParaSCIP;
44
45/** create clone of this object */
47ScipParaInitialStat::clone(
49 )
50{
51 int newMaxDepth = maxDepth;
52 int newMaxTotalDepth = maxTotalDepth;
53 int newNVarBranchStatsDown = nVarBranchStatsDown;
54 int newNVarBranchStatsUp = nVarBranchStatsUp;
55 int *newIdxLBranchStatsVarsDown = new int[nVarBranchStatsDown];
56 int *newNVarBranchingDown = new int[nVarBranchStatsDown];
57 int *newIdxLBranchStatsVarsUp = new int[nVarBranchStatsUp];
58 int *newNVarBranchingUp = new int[nVarBranchStatsUp];
59 SCIP_Real *newDownpscost = new SCIP_Real[nVarBranchStatsDown];
60 SCIP_Real *newDownvsids = new SCIP_Real[nVarBranchStatsDown];
61 SCIP_Real *newDownconflen = new SCIP_Real[nVarBranchStatsDown];
62 SCIP_Real *newDowninfer = new SCIP_Real[nVarBranchStatsDown];
63 SCIP_Real *newDowncutoff = new SCIP_Real[nVarBranchStatsDown];
64 SCIP_Real *newUppscost = new SCIP_Real[nVarBranchStatsUp];
65 SCIP_Real *newUpvsids = new SCIP_Real[nVarBranchStatsUp];
66 SCIP_Real *newUpconflen = new SCIP_Real[nVarBranchStatsUp];
67 SCIP_Real *newUpinfer = new SCIP_Real[nVarBranchStatsUp];
68 SCIP_Real *newUpcutoff = new SCIP_Real[nVarBranchStatsUp];
69 for( int i = 0; i < nVarBranchStatsDown; i++ )
70 {
71 newIdxLBranchStatsVarsDown[i] = idxLBranchStatsVarsDown[i];
72 newNVarBranchingDown[i] = nVarBranchingDown[i];
73 newDownpscost[i] = downpscost[i];
74 newDownvsids[i] = downvsids[i];
75 newDownconflen[i] = downconflen[i];
76 newDowninfer[i] = downinfer[i];
77 newDowncutoff[i] = downcutoff[i];
78 }
79 for( int i = 0; i < nVarBranchStatsUp; i++ )
80 {
81 newIdxLBranchStatsVarsUp[i] = idxLBranchStatsVarsUp[i];
82 newNVarBranchingUp[i] = nVarBranchingUp[i];
83 newUppscost[i] = uppscost[i];
84 newUpvsids[i] = upvsids[i];
85 newUpconflen[i] = upconflen[i];
86 newUpinfer[i] = upinfer[i];
87 newUpcutoff[i] = upcutoff[i];
88 }
89 DEF_SCIP_PARA_COMM( scipParaComm, comm);
90 return (
91 scipParaComm->createScipParaInitialStat(newMaxDepth, newMaxTotalDepth, newNVarBranchStatsDown, newNVarBranchStatsUp,
92 newIdxLBranchStatsVarsDown, newNVarBranchingDown, newIdxLBranchStatsVarsUp, newNVarBranchingUp,
93 newDownpscost, newDownvsids, newDownconflen, newDowninfer, newDowncutoff,
94 newUppscost, newUpvsids, newUpconflen, newUpinfer, newUpcutoff)
95 );
96}
97
99 SCIP *scip
100 ) :
101 maxDepth(0),
102 maxTotalDepth(0),
103 nVarBranchStatsDown(0),
104 nVarBranchStatsUp(0),
105 idxLBranchStatsVarsDown(0),
106 nVarBranchingDown(0),
107 idxLBranchStatsVarsUp(0),
108 nVarBranchingUp(0),
109 downpscost(0),
110 downvsids(0),
111 downconflen(0),
112 downinfer(0),
113 downcutoff(0),
114 uppscost(0),
115 upvsids(0),
116 upconflen(0),
117 upinfer(0),
118 upcutoff(0)
119{
120 maxDepth = SCIPgetMaxDepth(scip);
121 maxTotalDepth = SCIPgetMaxTotalDepth(scip);
124
125 int nvars; /* number of variables */
126 int nbinvars; /* number of binary variables */
127 int nintvars; /* number of integer variables */
128 SCIP_VAR** vars; /* transformed problem's variables */
129 SCIP_CALL_ABORT( SCIPgetVarsData(scip, &vars, &nvars, &nbinvars, &nintvars, NULL, NULL) );
130 int ngenvars = nbinvars+nintvars;
131
132 /** count downward vars and upward vars */
133 for( int i = 0; i < ngenvars; ++i )
134 {
135 assert( SCIPvarGetType(vars[i]) == SCIP_VARTYPE_BINARY || SCIPvarGetType(vars[i]) == SCIP_VARTYPE_INTEGER );
136 SCIP_VAR *transformVar = vars[i];
137 SCIP_Real scalar = 1.0;
138 SCIP_Real constant = 0.0;
139 SCIP_CALL_ABORT( SCIPvarGetOrigvarSum(&transformVar, &scalar, &constant ) );
140 assert(transformVar != NULL);
141 if( scalar > 0.0 )
142 {
143 if( SCIPvarGetNBranchings(transformVar, SCIP_BRANCHDIR_DOWNWARDS) > 0 )
144 {
146 }
147 if( SCIPvarGetNBranchings(transformVar, SCIP_BRANCHDIR_UPWARDS) > 0 )
148 {
150 }
151 }
152 else
153 {
154 if( SCIPvarGetNBranchings(transformVar, SCIP_BRANCHDIR_DOWNWARDS) > 0 )
155 {
157 }
158 if( SCIPvarGetNBranchings(transformVar, SCIP_BRANCHDIR_UPWARDS) > 0 )
159 {
161 }
162 }
163 }
164
165 /** allocate memory */
170 downpscost = new SCIP_Real[nVarBranchStatsDown];
171 downvsids = new SCIP_Real[nVarBranchStatsDown];
172 downconflen = new SCIP_Real[nVarBranchStatsDown];
173 downinfer = new SCIP_Real[nVarBranchStatsDown];
174 downcutoff = new SCIP_Real[nVarBranchStatsDown];
175 uppscost = new SCIP_Real[nVarBranchStatsUp];
176 upvsids = new SCIP_Real[nVarBranchStatsUp];
177 upconflen = new SCIP_Real[nVarBranchStatsUp];
178 upinfer = new SCIP_Real[nVarBranchStatsUp];
179 upcutoff = new SCIP_Real[nVarBranchStatsUp];
180
181 int nDown = 0;
182 int nUp = 0;
183 for( int i = 0; i < ngenvars; ++i )
184 {
185 assert( SCIPvarGetType(vars[i]) == SCIP_VARTYPE_BINARY || SCIPvarGetType(vars[i]) == SCIP_VARTYPE_INTEGER );
186 SCIP_VAR *transformVar = vars[i];
187 SCIP_Real scalar = 1.0;
188 SCIP_Real constant = 0.0;
189 SCIP_CALL_ABORT( SCIPvarGetOrigvarSum(&transformVar, &scalar, &constant ) );
190 assert(transformVar != NULL);
191 if( scalar > 0.0 )
192 {
193 if( SCIPvarGetNBranchings(transformVar, SCIP_BRANCHDIR_DOWNWARDS) > 0 )
194 {
195 idxLBranchStatsVarsDown[nDown] = SCIPvarGetIndex(transformVar);
196 nVarBranchingDown[nDown] = SCIPvarGetNBranchings(transformVar, SCIP_BRANCHDIR_DOWNWARDS);
197 downpscost[nDown] = SCIPgetVarPseudocost(scip, vars[i], SCIP_BRANCHDIR_DOWNWARDS);
198 downvsids[nDown] = SCIPgetVarVSIDS(scip, vars[i], SCIP_BRANCHDIR_DOWNWARDS);
199 downconflen[nDown] = SCIPgetVarAvgConflictlength(scip, vars[i], SCIP_BRANCHDIR_DOWNWARDS);
200 downinfer[nDown] = SCIPgetVarAvgInferences(scip, vars[i], SCIP_BRANCHDIR_DOWNWARDS);
201 downcutoff[nDown] = SCIPgetVarAvgCutoffs(scip, vars[i], SCIP_BRANCHDIR_DOWNWARDS);
202 nDown++;
203 }
204 if( SCIPvarGetNBranchings(transformVar, SCIP_BRANCHDIR_UPWARDS) > 0 )
205 {
206 idxLBranchStatsVarsUp[nUp] = SCIPvarGetIndex(transformVar);
207 nVarBranchingUp[nUp] = SCIPvarGetNBranchings(transformVar, SCIP_BRANCHDIR_UPWARDS);
208 uppscost[nUp] = SCIPgetVarPseudocost(scip, vars[i], SCIP_BRANCHDIR_UPWARDS);
209 upvsids[nUp] = SCIPgetVarVSIDS(scip, vars[i], SCIP_BRANCHDIR_UPWARDS);
210 upconflen[nUp] = SCIPgetVarAvgConflictlength(scip, vars[i], SCIP_BRANCHDIR_UPWARDS);
211 upinfer[nUp] = SCIPgetVarAvgInferences(scip, vars[i], SCIP_BRANCHDIR_UPWARDS);
212 upcutoff[nUp] = SCIPgetVarAvgCutoffs(scip, vars[i], SCIP_BRANCHDIR_UPWARDS);
213 nUp++;
214 }
215 }
216 else
217 {
218 if( SCIPvarGetNBranchings(transformVar, SCIP_BRANCHDIR_DOWNWARDS) > 0 )
219 {
220 idxLBranchStatsVarsUp[nUp] = SCIPvarGetIndex(transformVar);
221 nVarBranchingUp[nUp] = SCIPvarGetNBranchings(transformVar, SCIP_BRANCHDIR_UPWARDS);
222 uppscost[nUp] = SCIPgetVarPseudocost(scip, vars[i], SCIP_BRANCHDIR_UPWARDS);
223 upvsids[nUp] = SCIPgetVarVSIDS(scip, vars[i], SCIP_BRANCHDIR_UPWARDS);
224 upconflen[nUp] = SCIPgetVarAvgConflictlength(scip, vars[i], SCIP_BRANCHDIR_UPWARDS);
225 upinfer[nUp] = SCIPgetVarAvgInferences(scip, vars[i], SCIP_BRANCHDIR_UPWARDS);
226 upcutoff[nUp] = SCIPgetVarAvgCutoffs(scip, vars[i], SCIP_BRANCHDIR_UPWARDS);
227 nUp++;
228 }
229 if( SCIPvarGetNBranchings(transformVar, SCIP_BRANCHDIR_UPWARDS) > 0 )
230 {
231 idxLBranchStatsVarsDown[nDown] = SCIPvarGetIndex(transformVar);
232 nVarBranchingDown[nDown] = SCIPvarGetNBranchings(transformVar, SCIP_BRANCHDIR_DOWNWARDS);
233 downpscost[nDown] = SCIPgetVarPseudocost(scip, vars[i], SCIP_BRANCHDIR_DOWNWARDS);
234 downvsids[nDown] = SCIPgetVarVSIDS(scip, vars[i], SCIP_BRANCHDIR_DOWNWARDS);
235 downconflen[nDown] = SCIPgetVarAvgConflictlength(scip, vars[i], SCIP_BRANCHDIR_DOWNWARDS);
236 downinfer[nDown] = SCIPgetVarAvgInferences(scip, vars[i], SCIP_BRANCHDIR_DOWNWARDS);
237 downcutoff[nDown] = SCIPgetVarAvgCutoffs(scip, vars[i], SCIP_BRANCHDIR_DOWNWARDS);
238 nDown++;
239 }
240 }
241 }
242 assert( nVarBranchStatsDown == nDown && nVarBranchStatsUp == nUp );
243}
244
245void
247 SCIP *scip
248 )
249{
250 SCIP_VAR **vars = SCIPgetVars(scip);
251 for(int n = 0; n < nVarBranchStatsDown; n++ )
252 {
253 for( int i = 0; i < nVarBranchingDown[n]; i++ )
254 {
255 assert( SCIPvarGetProbindex(vars[idxLBranchStatsVarsDown[n]]) == idxLBranchStatsVarsDown[n]);
256 SCIP_CALL_ABORT( SCIPinitVarBranchStats(scip,vars[idxLBranchStatsVarsDown[n]],downpscost[n],0.0,downvsids[n],0.0,downconflen[n],0.0,downinfer[n],0.0,downcutoff[n],0.0) );
257 }
258 }
259 for(int n = 0; n < nVarBranchStatsUp; n++ )
260 {
261 for( int i = 0; i < nVarBranchingUp[n]; i++ )
262 {
263 assert( SCIPvarGetProbindex(vars[idxLBranchStatsVarsUp[n]]) == idxLBranchStatsVarsUp[n]);
264 SCIP_CALL_ABORT( SCIPinitVarBranchStats(scip,vars[idxLBranchStatsVarsUp[n]],0.0,uppscost[n],0.0,upvsids[n],0.0,upconflen[n],0.0,upinfer[n],0.0,upcutoff[n]) );
265 }
266 }
267}
Base class of communicator object.
Definition: paraComm.h:102
class for initial statistics collecting after racing
static ScipParaCommTh * comm
Definition: fscip.cpp:73
#define DEF_SCIP_PARA_COMM(scip_para_comm, comm)
ParaComm extension for SCIP solver.
ParaInitialStat extension for SCIP solver.