Skip to content

Commit f924cb8

Browse files
committed
Clean up
1 parent a33bd14 commit f924cb8

File tree

2 files changed

+60
-112
lines changed

2 files changed

+60
-112
lines changed

highs/util/HighsLinearSumBounds.cpp

Lines changed: 49 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,34 @@
1010
#include <algorithm>
1111

1212
void HighsLinearSumBounds::add(HighsInt sum, HighsInt var, double coefficient) {
13-
addOrRemoveVarUpper(sum, var, coefficient, varUpper[var], HighsInt{1});
14-
addOrRemoveVarLower(sum, var, coefficient, varLower[var], HighsInt{1});
15-
addOrRemoveImplVarUpper(sum, var, coefficient, getImplVarUpper(sum, var),
16-
HighsInt{1});
17-
addOrRemoveImplVarLower(sum, var, coefficient, getImplVarLower(sum, var),
18-
HighsInt{1});
13+
handleVarUpper(sum, coefficient, varUpper[var], HighsInt{1});
14+
handleVarLower(sum, coefficient, varLower[var], HighsInt{1});
15+
handleImplVarUpper(sum, coefficient, getImplVarUpper(sum, var), HighsInt{1});
16+
handleImplVarLower(sum, coefficient, getImplVarLower(sum, var), HighsInt{1});
1917
}
2018

2119
void HighsLinearSumBounds::remove(HighsInt sum, HighsInt var,
2220
double coefficient) {
23-
addOrRemoveVarUpper(sum, var, coefficient, varUpper[var], HighsInt{-1});
24-
addOrRemoveVarLower(sum, var, coefficient, varLower[var], HighsInt{-1});
25-
addOrRemoveImplVarUpper(sum, var, coefficient, getImplVarUpper(sum, var),
26-
HighsInt{-1});
27-
addOrRemoveImplVarLower(sum, var, coefficient, getImplVarLower(sum, var),
28-
HighsInt{-1});
21+
handleVarUpper(sum, coefficient, varUpper[var], HighsInt{-1});
22+
handleVarLower(sum, coefficient, varLower[var], HighsInt{-1});
23+
handleImplVarUpper(sum, coefficient, getImplVarUpper(sum, var), HighsInt{-1});
24+
handleImplVarLower(sum, coefficient, getImplVarLower(sum, var), HighsInt{-1});
2925
}
3026

3127
void HighsLinearSumBounds::updatedVarUpper(HighsInt sum, HighsInt var,
3228
double coefficient,
3329
double oldVarUpper) {
34-
addOrRemoveVarUpper(sum, var, coefficient, oldVarUpper, HighsInt{-1});
35-
addOrRemoveVarUpper(sum, var, coefficient, varUpper[var], HighsInt{1});
30+
handleVarUpper(sum, coefficient, oldVarUpper, HighsInt{-1});
31+
handleVarUpper(sum, coefficient, varUpper[var], HighsInt{1});
3632
updatedImplVarUpper(sum, var, coefficient, oldVarUpper, implVarUpper[var],
3733
implVarUpperSource[var]);
3834
}
3935

4036
void HighsLinearSumBounds::updatedVarLower(HighsInt sum, HighsInt var,
4137
double coefficient,
4238
double oldVarLower) {
43-
addOrRemoveVarLower(sum, var, coefficient, oldVarLower, HighsInt{-1});
44-
addOrRemoveVarLower(sum, var, coefficient, varLower[var], HighsInt{1});
39+
handleVarLower(sum, coefficient, oldVarLower, HighsInt{-1});
40+
handleVarLower(sum, coefficient, varLower[var], HighsInt{1});
4541
updatedImplVarLower(sum, var, coefficient, oldVarLower, implVarLower[var],
4642
implVarLowerSource[var]);
4743
}
@@ -228,96 +224,45 @@ double HighsLinearSumBounds::getImplVarLower(
228224
: std::max(myImplVarLower, myVarLower));
229225
}
230226

231-
void HighsLinearSumBounds::addOrRemoveVarUpper(HighsInt sum, HighsInt var,
232-
double coefficient,
233-
double myVarUpper,
234-
HighsInt direction) {
235-
if (coefficient > 0) {
236-
// coefficient is positive, therefore variable upper bound contributes to
237-
// sum upper bound
238-
if (myVarUpper == kHighsInf)
239-
numInfSumUpperOrig[sum] += direction;
240-
else
241-
sumUpperOrig[sum] +=
242-
direction * static_cast<HighsCDouble>(myVarUpper) * coefficient;
243-
} else {
244-
// coefficient is negative, therefore variable upper bound contributes to
245-
// sum lower bound
246-
if (myVarUpper == kHighsInf)
247-
numInfSumLowerOrig[sum] += direction;
248-
else
249-
sumLowerOrig[sum] +=
250-
direction * static_cast<HighsCDouble>(myVarUpper) * coefficient;
251-
}
227+
void HighsLinearSumBounds::update(HighsInt& numInf, HighsCDouble& sum,
228+
bool isBoundFinite, HighsInt direction,
229+
double bound, double coefficient) {
230+
if (!isBoundFinite)
231+
numInf += direction;
232+
else
233+
sum += direction * static_cast<HighsCDouble>(bound) * coefficient;
252234
}
253235

254-
void HighsLinearSumBounds::addOrRemoveVarLower(HighsInt sum, HighsInt var,
255-
double coefficient,
256-
double myVarLower,
257-
HighsInt direction) {
258-
if (coefficient > 0) {
259-
// coefficient is positive, therefore variable lower bound contributes to
260-
// sum lower bound
261-
if (myVarLower == -kHighsInf)
262-
numInfSumLowerOrig[sum] += direction;
263-
else
264-
sumLowerOrig[sum] +=
265-
direction * static_cast<HighsCDouble>(myVarLower) * coefficient;
266-
} else {
267-
// coefficient is negative, therefore variable lower bound contributes to
268-
// sum upper bound
269-
if (myVarLower == -kHighsInf)
270-
numInfSumUpperOrig[sum] += direction;
271-
else
272-
sumUpperOrig[sum] +=
273-
direction * static_cast<HighsCDouble>(myVarLower) * coefficient;
274-
}
236+
void HighsLinearSumBounds::handleVarUpper(HighsInt sum, double coefficient,
237+
double myVarUpper,
238+
HighsInt direction) {
239+
update(coefficient > 0 ? numInfSumUpperOrig[sum] : numInfSumLowerOrig[sum],
240+
coefficient > 0 ? sumUpperOrig[sum] : sumLowerOrig[sum],
241+
myVarUpper != kHighsInf, direction, myVarUpper, coefficient);
275242
}
276243

277-
void HighsLinearSumBounds::addOrRemoveImplVarUpper(HighsInt sum, HighsInt var,
278-
double coefficient,
279-
double myImplVarUpper,
280-
HighsInt direction) {
281-
if (coefficient > 0) {
282-
// coefficient is positive, therefore variable upper bound contributes to
283-
// sum upper bound
284-
if (myImplVarUpper == kHighsInf)
285-
numInfSumUpper[sum] += direction;
286-
else
287-
sumUpper[sum] +=
288-
direction * static_cast<HighsCDouble>(myImplVarUpper) * coefficient;
289-
} else {
290-
// coefficient is negative, therefore variable upper bound contributes to
291-
// sum lower bound
292-
if (myImplVarUpper == kHighsInf)
293-
numInfSumLower[sum] += direction;
294-
else
295-
sumLower[sum] +=
296-
direction * static_cast<HighsCDouble>(myImplVarUpper) * coefficient;
297-
}
244+
void HighsLinearSumBounds::handleVarLower(HighsInt sum, double coefficient,
245+
double myVarLower,
246+
HighsInt direction) {
247+
update(coefficient > 0 ? numInfSumLowerOrig[sum] : numInfSumUpperOrig[sum],
248+
coefficient > 0 ? sumLowerOrig[sum] : sumUpperOrig[sum],
249+
myVarLower != -kHighsInf, direction, myVarLower, coefficient);
298250
}
299251

300-
void HighsLinearSumBounds::addOrRemoveImplVarLower(HighsInt sum, HighsInt var,
301-
double coefficient,
302-
double myImplVarLower,
303-
HighsInt direction) {
304-
if (coefficient > 0) {
305-
// coefficient is positive, therefore variable lower bound contributes to
306-
// sum lower bound
307-
if (myImplVarLower == -kHighsInf)
308-
numInfSumLower[sum] += direction;
309-
else
310-
sumLower[sum] +=
311-
direction * static_cast<HighsCDouble>(myImplVarLower) * coefficient;
312-
} else {
313-
// coefficient is negative, therefore variable lower bound contributes to
314-
// sum upper bound
315-
if (myImplVarLower == -kHighsInf)
316-
numInfSumUpper[sum] += direction;
317-
else
318-
sumUpper[sum] +=
319-
direction * static_cast<HighsCDouble>(myImplVarLower) * coefficient;
320-
}
252+
void HighsLinearSumBounds::handleImplVarUpper(HighsInt sum, double coefficient,
253+
double myImplVarUpper,
254+
HighsInt direction) {
255+
update(coefficient > 0 ? numInfSumUpper[sum] : numInfSumLower[sum],
256+
coefficient > 0 ? sumUpper[sum] : sumLower[sum],
257+
myImplVarUpper != kHighsInf, direction, myImplVarUpper, coefficient);
258+
}
259+
260+
void HighsLinearSumBounds::handleImplVarLower(HighsInt sum, double coefficient,
261+
double myImplVarLower,
262+
HighsInt direction) {
263+
update(coefficient > 0 ? numInfSumLower[sum] : numInfSumUpper[sum],
264+
coefficient > 0 ? sumLower[sum] : sumUpper[sum],
265+
myImplVarLower != -kHighsInf, direction, myImplVarLower, coefficient);
321266
}
322267

323268
void HighsLinearSumBounds::updatedImplVarUpper(HighsInt sum, HighsInt var,
@@ -331,8 +276,8 @@ void HighsLinearSumBounds::updatedImplVarUpper(HighsInt sum, HighsInt var,
331276

332277
if (vUpper == oldVUpper) return;
333278

334-
addOrRemoveImplVarUpper(sum, var, coefficient, oldVUpper, HighsInt{-1});
335-
addOrRemoveImplVarUpper(sum, var, coefficient, vUpper, HighsInt{1});
279+
handleImplVarUpper(sum, coefficient, oldVUpper, HighsInt{-1});
280+
handleImplVarUpper(sum, coefficient, vUpper, HighsInt{1});
336281
}
337282

338283
void HighsLinearSumBounds::updatedImplVarLower(HighsInt sum, HighsInt var,
@@ -346,6 +291,6 @@ void HighsLinearSumBounds::updatedImplVarLower(HighsInt sum, HighsInt var,
346291

347292
if (vLower == oldVLower) return;
348293

349-
addOrRemoveImplVarLower(sum, var, coefficient, oldVLower, HighsInt{-1});
350-
addOrRemoveImplVarLower(sum, var, coefficient, vLower, HighsInt{1});
294+
handleImplVarLower(sum, coefficient, oldVLower, HighsInt{-1});
295+
handleImplVarLower(sum, coefficient, vLower, HighsInt{1});
351296
}

highs/util/HighsLinearSumBounds.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,20 @@ class HighsLinearSumBounds {
164164
double getImplVarLower(HighsInt sum, double myVarLower, double myImplVarLower,
165165
HighsInt myImplVarLowerSource) const;
166166

167-
void addOrRemoveVarUpper(HighsInt sum, HighsInt var, double coefficient,
168-
double myVarUpper, HighsInt direction);
167+
void update(HighsInt& numInf, HighsCDouble& sum, bool isBoundFinite,
168+
HighsInt direction, double bound, double coefficient);
169169

170-
void addOrRemoveVarLower(HighsInt sum, HighsInt var, double coefficient,
171-
double myVarLower, HighsInt direction);
170+
void handleVarUpper(HighsInt sum, double coefficient, double myVarUpper,
171+
HighsInt direction);
172172

173-
void addOrRemoveImplVarUpper(HighsInt sum, HighsInt var, double coefficient,
174-
double myImplVarUpper, HighsInt direction);
173+
void handleVarLower(HighsInt sum, double coefficient, double myVarLower,
174+
HighsInt direction);
175175

176-
void addOrRemoveImplVarLower(HighsInt sum, HighsInt var, double coefficient,
177-
double myImplVarLower, HighsInt direction);
176+
void handleImplVarUpper(HighsInt sum, double coefficient,
177+
double myImplVarUpper, HighsInt direction);
178+
179+
void handleImplVarLower(HighsInt sum, double coefficient,
180+
double myImplVarLower, HighsInt direction);
178181

179182
void updatedImplVarUpper(HighsInt sum, HighsInt var, double coefficient,
180183
double oldVarUpper, double oldImplVarUpper,

0 commit comments

Comments
 (0)