/
UnoptimizedCnecFiller.java
377 lines (341 loc) · 19.6 KB
/
UnoptimizedCnecFiller.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* Copyright (c) 2021, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.openrao.searchtreerao.linearoptimisation.algorithms.fillers;
import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.openrao.data.cracapi.Identifiable;
import com.powsybl.openrao.data.cracapi.State;
import com.powsybl.openrao.data.cracapi.cnec.FlowCnec;
import com.powsybl.openrao.data.cracapi.cnec.Side;
import com.powsybl.openrao.data.cracapi.rangeaction.HvdcRangeAction;
import com.powsybl.openrao.data.cracapi.rangeaction.InjectionRangeAction;
import com.powsybl.openrao.data.cracapi.rangeaction.PstRangeAction;
import com.powsybl.openrao.data.cracapi.rangeaction.RangeAction;
import com.powsybl.openrao.raoapi.parameters.RangeActionsOptimizationParameters;
import com.powsybl.openrao.searchtreerao.commons.RaoUtil;
import com.powsybl.openrao.searchtreerao.commons.optimizationperimeters.OptimizationPerimeter;
import com.powsybl.openrao.searchtreerao.commons.parameters.UnoptimizedCnecParameters;
import com.powsybl.openrao.searchtreerao.linearoptimisation.algorithms.linearproblem.OpenRaoMPConstraint;
import com.powsybl.openrao.searchtreerao.linearoptimisation.algorithms.linearproblem.OpenRaoMPVariable;
import com.powsybl.openrao.searchtreerao.linearoptimisation.algorithms.linearproblem.LinearProblem;
import com.powsybl.openrao.searchtreerao.result.api.FlowResult;
import com.powsybl.openrao.searchtreerao.result.api.RangeActionActivationResult;
import com.powsybl.openrao.searchtreerao.result.api.SensitivityResult;
import java.util.*;
import java.util.stream.Collectors;
import static com.powsybl.openrao.commons.Unit.MEGAWATT;
/**
* This filler adds variables and constraints allowing the RAO to ignore some
* cnecs, if they should not be optimized. This can happen in the following cases :
* * selectedRule = MARGIN_DECREASE : some operators' CNECs' margins will not be taken into account in the objective function,
* unless they are worse than their pre-perimeter margins.
* * selectedRule = PST_LIMITATION : some cnecs parametrized as in series with a pst will not be taken into account in the objective
* function, as long as a there are enough setpoints on the pst left to absorb the margin deficit
*
* @author Peter Mitri {@literal <peter.mitri at rte-france.com>}
* @author Godelaine de Montmorillon {@literal <godelaine.demontmorillon at rte-france.com>}
*/
public class UnoptimizedCnecFiller implements ProblemFiller {
public static final String VARIABLE_NOT_CREATED = "%s variable has not yet been created for Cnec %s (side %s)";
public static final String OPTIMIZE_CNEC_BINARY = "Optimize cnec binary";
private final OptimizationPerimeter optimizationContext;
private final Set<FlowCnec> flowCnecs;
private final FlowResult prePerimeterFlowResult;
private final Set<String> operatorsNotToOptimize;
private final double highestThresholdValue;
private final Map<FlowCnec, RangeAction<?>> flowCnecRangeActionMap;
private final RangeActionsOptimizationParameters rangeActionParameters;
private UnoptimizedCnecFillerRule selectedRule;
public UnoptimizedCnecFiller(OptimizationPerimeter optimizationContext,
Set<FlowCnec> flowCnecs,
FlowResult prePerimeterFlowResult,
UnoptimizedCnecParameters unoptimizedCnecParameters,
RangeActionsOptimizationParameters rangeActionParameters) {
this.optimizationContext = optimizationContext;
this.flowCnecs = new TreeSet<>(Comparator.comparing(Identifiable::getId));
this.flowCnecs.addAll(flowCnecs);
this.prePerimeterFlowResult = prePerimeterFlowResult;
this.operatorsNotToOptimize = unoptimizedCnecParameters.getOperatorsNotToOptimize();
this.highestThresholdValue = RaoUtil.getLargestCnecThreshold(flowCnecs, MEGAWATT);
this.flowCnecRangeActionMap = unoptimizedCnecParameters.getDoNotOptimizeCnecsSecuredByTheirPst();
this.rangeActionParameters = rangeActionParameters;
}
public enum UnoptimizedCnecFillerRule {
MARGIN_DECREASE,
PST_LIMITATION
}
private void selectUnoptimizedCnecFillerRule() {
if (Objects.nonNull(flowCnecRangeActionMap)) {
selectedRule = UnoptimizedCnecFillerRule.PST_LIMITATION;
} else {
selectedRule = UnoptimizedCnecFillerRule.MARGIN_DECREASE;
}
}
@Override
public void fill(LinearProblem linearProblem, FlowResult flowResult, SensitivityResult sensitivityResult) {
// define UnoptimizedCnecFillerRule
selectUnoptimizedCnecFillerRule();
// build variables
buildDontOptimizeCnecVariables(linearProblem);
// build constraints
buildDontOptimizeCnecConstraints(linearProblem, sensitivityResult);
// update minimum margin objective function constraints
updateMinimumMarginConstraints(linearProblem);
}
@Override
public void updateBetweenSensiIteration(LinearProblem linearProblem, FlowResult flowResult, SensitivityResult sensitivityResult, RangeActionActivationResult rangeActionActivationResult) {
updateDontOptimizeCnecConstraints(linearProblem, sensitivityResult);
}
@Override
public void updateBetweenMipIteration(LinearProblem linearProblem, RangeActionActivationResult rangeActionActivationResult) {
// nothing to do
}
/**
* This method defines a binary variable that detects the decrease of the margin on the given CNEC compared to the preperimeter margin
* The variable should be equal to 1 if there is a decrease
*/
private void buildDontOptimizeCnecVariables(LinearProblem linearProblem) {
getFlowCnecs().forEach(cnec -> cnec.getMonitoredSides().forEach(side ->
linearProblem.addOptimizeCnecBinaryVariable(cnec, side)
));
}
/**
* Gathers flow cnecs that can be unoptimized depending on the ongoing UnoptimizedCnecFillerRule.
*/
private Set<FlowCnec> getFlowCnecs() {
if (selectedRule.equals(UnoptimizedCnecFillerRule.MARGIN_DECREASE)) {
return flowCnecs.stream()
.filter(cnec -> operatorsNotToOptimize.contains(cnec.getOperator()))
.collect(Collectors.toSet());
} else {
return optimizationContext.getFlowCnecs().stream()
.filter(flowCnecRangeActionMap::containsKey)
.collect(Collectors.toSet());
}
}
private void buildDontOptimizeCnecConstraints(LinearProblem linearProblem, SensitivityResult sensitivityResult) {
if (selectedRule.equals(UnoptimizedCnecFillerRule.MARGIN_DECREASE)) {
buildDontOptimizeCnecConstraintsForTsosThatDoNotShareRas(linearProblem);
} else {
buildDontOptimizeCnecConstraintsForCnecsInSeriesWithPsts(linearProblem, sensitivityResult);
}
}
private void updateDontOptimizeCnecConstraints(LinearProblem linearProblem, SensitivityResult sensitivityResult) {
if (selectedRule.equals(UnoptimizedCnecFillerRule.PST_LIMITATION)) {
updateDontOptimizeCnecConstraintsForCnecsInSeriesWithPsts(linearProblem, sensitivityResult);
}
}
/**
* This method defines, for each CNEC belonging to a TSO that does not share RAs in the given perimeter, a constraint
* The constraint defines the behaviour of the binary variable optimize cnec
* margin >= margin_preperimeter - optimize_cnec * bigM
* => (1) -flow + optimize_cnec * bigM >= margin_preperimeter - maxFlow
* and (2) flow + optimize_cnec * bigM >= margin_preperimeter + minFlow
* bigM is computed to be equal to the maximum margin decrease possible, which is the amount that decreases the
* cnec's margin to the initial worst margin
*/
private void buildDontOptimizeCnecConstraintsForTsosThatDoNotShareRas(LinearProblem linearProblem) {
double worstMarginDecrease = 20 * highestThresholdValue;
// No margin should be smaller than the worst margin computed above, otherwise it means the linear optimizer or
// the search tree rao is degrading the situation
// So we can use this to estimate the worst decrease possible of the margins on cnecs
getFlowCnecs().forEach(cnec -> cnec.getMonitoredSides().forEach(side -> {
double prePerimeterMargin = prePerimeterFlowResult.getMargin(cnec, side, MEGAWATT);
OpenRaoMPVariable flowVariable = linearProblem.getFlowVariable(cnec, side);
OpenRaoMPVariable optimizeCnecBinaryVariable = linearProblem.getOptimizeCnecBinaryVariable(cnec, side);
Optional<Double> minFlow;
Optional<Double> maxFlow;
minFlow = cnec.getLowerBound(side, MEGAWATT);
maxFlow = cnec.getUpperBound(side, MEGAWATT);
if (minFlow.isPresent()) {
OpenRaoMPConstraint decreaseMinmumThresholdMargin = linearProblem.addDontOptimizeCnecConstraint(
prePerimeterMargin + minFlow.get(),
LinearProblem.infinity(), cnec, side,
LinearProblem.MarginExtension.BELOW_THRESHOLD
);
decreaseMinmumThresholdMargin.setCoefficient(flowVariable, 1);
decreaseMinmumThresholdMargin.setCoefficient(optimizeCnecBinaryVariable, worstMarginDecrease);
}
if (maxFlow.isPresent()) {
OpenRaoMPConstraint decreaseMinmumThresholdMargin = linearProblem.addDontOptimizeCnecConstraint(
prePerimeterMargin - maxFlow.get(),
LinearProblem.infinity(), cnec, side,
LinearProblem.MarginExtension.ABOVE_THRESHOLD
);
decreaseMinmumThresholdMargin.setCoefficient(flowVariable, -1);
decreaseMinmumThresholdMargin.setCoefficient(optimizeCnecBinaryVariable, worstMarginDecrease);
}
}));
}
private void defineDontOptimizeCnecConstraintsForCnecsInSeriesWithPsts(LinearProblem linearProblem, SensitivityResult sensitivityResult, boolean buildConstraint) {
getFlowCnecs().forEach(cnec -> cnec.getMonitoredSides()
.forEach(side -> defineDontOptimizeCnecConstraintsForCnecInSeriesWithPsts(linearProblem, sensitivityResult, buildConstraint, cnec, side)));
}
private void defineDontOptimizeCnecConstraintsForCnecInSeriesWithPsts(LinearProblem linearProblem, SensitivityResult sensitivityResult, boolean buildConstraint, FlowCnec cnec, Side side) {
// Flow variable
OpenRaoMPVariable flowVariable = linearProblem.getFlowVariable(cnec, side);
// Optimize cnec binary variable
OpenRaoMPVariable optimizeCnecBinaryVariable = linearProblem.getOptimizeCnecBinaryVariable(cnec, side);
State state = getLastStateWithRangeActionAvailableForCnec(cnec);
if (Objects.isNull(state)) {
return;
}
OpenRaoMPVariable setPointVariable = linearProblem.getRangeActionSetpointVariable(flowCnecRangeActionMap.get(cnec), state);
double maxSetpoint = setPointVariable.ub();
double minSetpoint = setPointVariable.lb();
double sensitivity = zeroIfSensitivityBelowThreshold(
flowCnecRangeActionMap.get(cnec), sensitivityResult.getSensitivityValue(cnec, side, flowCnecRangeActionMap.get(cnec), MEGAWATT));
Optional<Double> minFlow = cnec.getLowerBound(side, MEGAWATT);
Optional<Double> maxFlow = cnec.getUpperBound(side, MEGAWATT);
double bigM = 20 * highestThresholdValue;
if (minFlow.isPresent()) {
OpenRaoMPConstraint extendSetpointBounds;
if (buildConstraint) {
extendSetpointBounds = linearProblem.addDontOptimizeCnecConstraint(
-LinearProblem.infinity(),
LinearProblem.infinity(), cnec,
side,
LinearProblem.MarginExtension.BELOW_THRESHOLD);
extendSetpointBounds.setCoefficient(flowVariable, 1);
} else {
extendSetpointBounds = linearProblem.getDontOptimizeCnecConstraint(cnec, side, LinearProblem.MarginExtension.BELOW_THRESHOLD);
}
extendSetpointBounds.setCoefficient(setPointVariable, -sensitivity);
extendSetpointBounds.setCoefficient(optimizeCnecBinaryVariable, bigM);
double lb = minFlow.get();
if (sensitivity >= 0) {
lb += -maxSetpoint * sensitivity;
} else {
lb += -minSetpoint * sensitivity;
}
extendSetpointBounds.setLb(lb);
}
if (maxFlow.isPresent()) {
OpenRaoMPConstraint extendSetpointBounds;
if (buildConstraint) {
extendSetpointBounds = linearProblem.addDontOptimizeCnecConstraint(
-LinearProblem.infinity(),
LinearProblem.infinity(), cnec, side,
LinearProblem.MarginExtension.ABOVE_THRESHOLD);
extendSetpointBounds.setCoefficient(flowVariable, -1);
} else {
extendSetpointBounds = linearProblem.getDontOptimizeCnecConstraint(cnec, side, LinearProblem.MarginExtension.ABOVE_THRESHOLD);
}
extendSetpointBounds.setCoefficient(setPointVariable, sensitivity);
extendSetpointBounds.setCoefficient(optimizeCnecBinaryVariable, bigM);
double lb = -maxFlow.get();
if (sensitivity >= 0) {
lb += minSetpoint * sensitivity;
} else {
lb += maxSetpoint * sensitivity;
}
extendSetpointBounds.setLb(lb);
}
}
/**
* This method defines, for each CNEC parameterized as in series with a pst in the given perimeter, a constraint
* The constraint defines the behaviour of the binary variable optimize cnec
* As long as a there are enough setpoints on the pst left to absorb the margin deficit, the cnec should not
* participate in the definition of the minimum margin (i.e. the binary variable optimize cnec is 0)
* When sensitivity >= 0 :
* (1) setpoint <= maxSetpoint - -(maxFlow - flow)/sensitivity + bigM * optimize_cnec
* (2) setpoint >= minSetpoint + -(flow - minFlow)/sensitivity - bigM * optimize_cnec
* * When sensitivity < 0 :
* (1) setpoint <= maxSetpoint - -(flow - minFlow)/-sensitivity + bigM * optimize_cnec
* (2) setpoint >= minSetpoint + -(maxFlow - flow)/-sensitivity - bigM * optimize_cnec
* bigM is computed to allow the PST to reach its bounds with optimize_cnec set to 1 :
* bigM = maxSetpoint - minSetpoint
*/
private void buildDontOptimizeCnecConstraintsForCnecsInSeriesWithPsts(LinearProblem linearProblem, SensitivityResult sensitivityResult) {
defineDontOptimizeCnecConstraintsForCnecsInSeriesWithPsts(linearProblem, sensitivityResult, true);
}
private void updateDontOptimizeCnecConstraintsForCnecsInSeriesWithPsts(LinearProblem linearProblem, SensitivityResult sensitivityResult) {
defineDontOptimizeCnecConstraintsForCnecsInSeriesWithPsts(linearProblem, sensitivityResult, false);
}
/**
* This method finds the most recent state amongst the states in optimizationContext previous to cnec's state
* for which the pst range action in series with cnec was available.
**/
private State getLastStateWithRangeActionAvailableForCnec(FlowCnec cnec) {
List<State> statesBeforeCnec = FillersUtil.getPreviousStates(cnec.getState(), optimizationContext).stream()
.sorted((s1, s2) -> Integer.compare(s2.getInstant().getOrder(), s1.getInstant().getOrder())) // start with curative state
.toList();
Optional<State> lastState = statesBeforeCnec.stream().filter(state ->
optimizationContext.getRangeActionsPerState().get(state).contains(flowCnecRangeActionMap.get(cnec)))
.findFirst();
// Range action (referenced for "cnec" in flowCnecPstRangeActionMap) can be unavailable for cnec
return lastState.orElse(null);
}
/**
* For CNECs with binary variable optimize_cnecs set to 0, deactivate their participation in the definition of the minimum margin
* Do this by adding (1 - optimize_cnecs) * bigM to the right side of the inequality
* bigM is computed as 2 times the largest absolute threshold between all CNECs
* Of course this can be restrictive as CNECs can have hypothetically infinite margins if they are monitored in one direction only
* But we'll suppose for now that the minimum margin can never be greater than 1 * the largest threshold
*/
private void updateMinimumMarginConstraints(LinearProblem linearProblem) {
double bigM = 2 * highestThresholdValue;
getFlowCnecs().forEach(cnec -> cnec.getMonitoredSides().forEach(side -> {
OpenRaoMPVariable optimizeCnecBinaryVariable = linearProblem.getOptimizeCnecBinaryVariable(cnec, side);
try {
updateMinimumMarginConstraint(
linearProblem.getMinimumMarginConstraint(cnec, side, LinearProblem.MarginExtension.BELOW_THRESHOLD),
optimizeCnecBinaryVariable,
bigM
);
} catch (OpenRaoException ignored) {
}
try {
updateMinimumMarginConstraint(
linearProblem.getMinimumMarginConstraint(cnec, side, LinearProblem.MarginExtension.ABOVE_THRESHOLD),
optimizeCnecBinaryVariable,
bigM
);
} catch (OpenRaoException ignored) {
}
try {
updateMinimumMarginConstraint(
linearProblem.getMinimumRelativeMarginConstraint(cnec, side, LinearProblem.MarginExtension.BELOW_THRESHOLD),
optimizeCnecBinaryVariable,
bigM
);
} catch (OpenRaoException ignored) {
}
try {
updateMinimumMarginConstraint(
linearProblem.getMinimumRelativeMarginConstraint(cnec, side, LinearProblem.MarginExtension.ABOVE_THRESHOLD),
optimizeCnecBinaryVariable,
bigM
);
} catch (OpenRaoException ignored) {
}
}));
}
/**
* Add a big coefficient to the minimum margin definition constraint, allowing it to be relaxed if the
* binary variable is equal to 1
*/
private void updateMinimumMarginConstraint(OpenRaoMPConstraint constraint, OpenRaoMPVariable optimizeCnecBinaryVariable, double bigM) {
constraint.setCoefficient(optimizeCnecBinaryVariable, bigM);
constraint.setUb(constraint.ub() + bigM);
}
/**
* Replace small sensitivity values with zero to avoid numerical issues
*/
private double zeroIfSensitivityBelowThreshold(RangeAction<?> rangeAction, double sensitivity) {
double threshold;
if (rangeAction instanceof PstRangeAction) {
threshold = rangeActionParameters.getPstSensitivityThreshold();
} else if (rangeAction instanceof HvdcRangeAction) {
threshold = rangeActionParameters.getHvdcSensitivityThreshold();
} else if (rangeAction instanceof InjectionRangeAction) {
threshold = rangeActionParameters.getInjectionRaSensitivityThreshold();
} else {
throw new OpenRaoException("Type of RangeAction not yet handled by the LinearRao.");
}
return Math.abs(sensitivity) >= threshold ? sensitivity : 0;
}
}