From 9876627ad946422170a9fb9c4aa16fa39ba1f193 Mon Sep 17 00:00:00 2001 From: Peter <47452063+pet-mit@users.noreply.github.com> Date: Fri, 21 Apr 2023 15:12:37 +0200 Subject: [PATCH] AUTO instant: ignore overloaded CNECs if they can be secured by their PST (#754) * Not optimized CNEC-PST at AUTO instant * refactor * correction and unit tests * refacto * checkstyle * sensi only available in MW * fix after merge * update psbl * update psbl * rollback update psbl --- .../castor/algorithm/AutomatonSimulator.java | 28 ++++--- .../search_tree_rao/commons/RaoUtil.java | 44 ++++++++++ ...atorWithPstLimitationUnoptimizedCnecs.java | 37 ++------ .../ObjectiveFunction.java | 4 +- .../parameters/UnoptimizedCnecParameters.java | 17 ++-- .../fillers/UnoptimizedCnecFiller.java | 18 ++-- .../algorithm/AutomatonSimulatorTest.java | 59 ++++++++++++- .../search_tree_rao/commons/RaoUtilTest.java | 84 +++++++++++++++++-- ...WithPstLimitationUnoptimizedCnecsTest.java | 17 ++-- .../UnoptimizedCnecParametersTest.java | 6 +- ...imizedCnecFillerPstLimitationRuleTest.java | 13 +-- 11 files changed, 243 insertions(+), 84 deletions(-) diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/AutomatonSimulator.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/AutomatonSimulator.java index 14b31bfa6e..f6a81bc654 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/AutomatonSimulator.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/AutomatonSimulator.java @@ -8,6 +8,7 @@ import com.farao_community.farao.commons.FaraoException; import com.farao_community.farao.commons.RandomizedString; +import com.farao_community.farao.commons.Unit; import com.farao_community.farao.data.crac_api.Crac; import com.farao_community.farao.data.crac_api.Identifiable; import com.farao_community.farao.data.crac_api.State; @@ -25,6 +26,7 @@ import com.farao_community.farao.search_tree_rao.commons.ToolProvider; import com.farao_community.farao.search_tree_rao.commons.objective_function_evaluator.ObjectiveFunction; import com.farao_community.farao.search_tree_rao.commons.objective_function_evaluator.ObjectiveFunctionResultImpl; +import com.farao_community.farao.search_tree_rao.commons.parameters.UnoptimizedCnecParameters; import com.farao_community.farao.search_tree_rao.result.api.*; import com.farao_community.farao.search_tree_rao.result.impl.AutomatonPerimeterResultImpl; import com.farao_community.farao.search_tree_rao.result.impl.PrePerimeterSensitivityResultImpl; @@ -57,22 +59,26 @@ public final class AutomatonSimulator { private final Crac crac; private final RaoParameters raoParameters; + private final Unit flowUnit; private final ToolProvider toolProvider; private final FlowResult initialFlowResult; private final RangeActionSetpointResult prePerimeterRangeActionSetpointResult; private final PrePerimeterResult prePerimeterSensitivityOutput; private final Set operatorsNotSharingCras; private final int numberLoggedElementsDuringRao; + private final Map> flowCnecRangeActionMap; public AutomatonSimulator(Crac crac, RaoParameters raoParameters, ToolProvider toolProvider, FlowResult initialFlowResult, RangeActionSetpointResult prePerimeterRangeActionSetpointResult, PrePerimeterResult prePerimeterSensitivityOutput, Set operatorsNotSharingCras, int numberLoggedElementsDuringRao) { this.crac = crac; this.raoParameters = raoParameters; + this.flowUnit = raoParameters.getObjectiveFunctionParameters().getType().getUnit(); this.toolProvider = toolProvider; this.initialFlowResult = initialFlowResult; this.prePerimeterRangeActionSetpointResult = prePerimeterRangeActionSetpointResult; this.prePerimeterSensitivityOutput = prePerimeterSensitivityOutput; this.operatorsNotSharingCras = operatorsNotSharingCras; this.numberLoggedElementsDuringRao = numberLoggedElementsDuringRao; + this.flowCnecRangeActionMap = UnoptimizedCnecParameters.getDoNotOptimizeCnecsSecuredByTheirPst(raoParameters.getNotOptimizedCnecsParameters(), crac); } /** @@ -556,7 +562,7 @@ RangeAutomatonSimulationResult shiftRangeActionsUntilFlowCnecsSecure(List> flowCnecsToBeExcluded = new HashSet<>(); PrePerimeterResult automatonRangeActionOptimizationSensitivityAnalysisOutput = prePerimeterSensitivityOutput; Map, Double> activatedRangeActionsWithSetpoint = new HashMap<>(); - List> flowCnecsWithNegativeMargin = getCnecsWithNegativeMarginWithoutExcludedCnecs(flowCnecs, flowCnecsToBeExcluded, automatonRangeActionOptimizationSensitivityAnalysisOutput); + List> flowCnecsWithNegativeMargin = getCnecsWithNegativeMarginWithoutExcludedCnecs(flowCnecs, flowCnecsToBeExcluded, automatonRangeActionOptimizationSensitivityAnalysisOutput, activatedRangeActionsWithSetpoint); if (alignedRangeActions.stream().allMatch(HvdcRangeAction.class::isInstance) && !flowCnecsWithNegativeMargin.isEmpty()) { // Disable HvdcAngleDroopActivePowerControl for HVDC lines, fetch their set-point, re-run sensitivity analysis and fetch new negative margins @@ -567,7 +573,7 @@ RangeAutomatonSimulationResult shiftRangeActionsUntilFlowCnecsSecure(List> getCnecsWithNegativeMarginWithoutExcludedCnecs(Set flowCnecs, Set> cnecsToBeExcluded, - PrePerimeterResult prePerimeterSensitivityOutput) { + PrePerimeterResult prePerimeterSensitivityOutput, + Map, Double> activatedRangeActionsWithSetpoint) { Map, Double> cnecsAndMargins = new HashMap<>(); flowCnecs.forEach(flowCnec -> flowCnec.getMonitoredSides().forEach(side -> { - double margin = prePerimeterSensitivityOutput.getMargin(flowCnec, side, raoParameters.getObjectiveFunctionParameters().getType().getUnit()); - if (!cnecsToBeExcluded.contains(Pair.of(flowCnec, side)) && margin < 0) { + double margin = prePerimeterSensitivityOutput.getMargin(flowCnec, side, flowUnit); + boolean cnecShouldBeOptimized = RaoUtil.cnecShouldBeOptimized(flowCnecRangeActionMap, prePerimeterSensitivityOutput, flowCnec, side, activatedRangeActionsWithSetpoint, prePerimeterRangeActionSetpointResult, prePerimeterSensitivityOutput, flowUnit); + if (cnecShouldBeOptimized && !cnecsToBeExcluded.contains(Pair.of(flowCnec, side)) && margin < 0) { cnecsAndMargins.put(Pair.of(flowCnec, side), margin); } })); diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoUtil.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoUtil.java index ef9dafa03e..1cc94cbac3 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoUtil.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoUtil.java @@ -23,9 +23,13 @@ import com.farao_community.farao.rao_api.parameters.extensions.RelativeMarginsParametersExtension; import com.farao_community.farao.search_tree_rao.commons.optimization_perimeters.OptimizationPerimeter; import com.farao_community.farao.search_tree_rao.result.api.FlowResult; +import com.farao_community.farao.search_tree_rao.result.api.RangeActionActivationResult; +import com.farao_community.farao.search_tree_rao.result.api.RangeActionSetpointResult; +import com.farao_community.farao.search_tree_rao.result.api.SensitivityResult; import com.powsybl.iidm.network.Network; import org.apache.commons.lang3.tuple.Pair; +import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -161,4 +165,44 @@ public static double getLargestCnecThreshold(Set flowCnecs, Unit unit) .max(Double::compare) .orElse(0.); } + + public static boolean cnecShouldBeOptimized(Map> flowCnecPstRangeActionMap, + FlowResult flowResult, + FlowCnec flowCnec, + Side side, + RangeActionActivationResult rangeActionActivationResult, + RangeActionSetpointResult prePerimeterRangeActionSetpointResult, + SensitivityResult sensitivityResult, + Unit unit) { + return cnecShouldBeOptimized(flowCnecPstRangeActionMap, flowResult, flowCnec, side, rangeActionActivationResult.getOptimizedSetpointsOnState(flowCnec.getState()), prePerimeterRangeActionSetpointResult, sensitivityResult, unit); + } + + public static boolean cnecShouldBeOptimized(Map> flowCnecPstRangeActionMap, + FlowResult flowResult, + FlowCnec flowCnec, + Side side, + Map, Double> activatedRangeActionsWithSetpoint, + RangeActionSetpointResult prePerimeterRangeActionSetpointResult, + SensitivityResult sensitivityResult, + Unit unit) { + if (!flowCnecPstRangeActionMap.containsKey(flowCnec)) { + return true; + } + + RangeAction ra = flowCnecPstRangeActionMap.get(flowCnec); + double cnecMarginToUpperBound = flowCnec.getUpperBound(side, unit).orElse(Double.POSITIVE_INFINITY) - flowResult.getFlow(flowCnec, side, unit); + double cnecMarginToLowerBound = flowResult.getFlow(flowCnec, side, unit) - flowCnec.getLowerBound(side, unit).orElse(Double.NEGATIVE_INFINITY); + if (cnecMarginToUpperBound >= 0 && cnecMarginToLowerBound >= 0) { + return false; + } + + double sensitivity = sensitivityResult.getSensitivityValue(flowCnec, side, ra, Unit.MEGAWATT) * getFlowUnitMultiplier(flowCnec, side, Unit.MEGAWATT, unit); + double raCurrentSetpoint = activatedRangeActionsWithSetpoint.getOrDefault(ra, prePerimeterRangeActionSetpointResult.getSetpoint(ra)); + double raMaxDecrease = raCurrentSetpoint - ra.getMinAdmissibleSetpoint(prePerimeterRangeActionSetpointResult.getSetpoint(ra)); + double raMaxIncrease = ra.getMaxAdmissibleSetpoint(prePerimeterRangeActionSetpointResult.getSetpoint(ra)) - raCurrentSetpoint; + double maxFlowDecrease = sensitivity >= 0 ? sensitivity * raMaxDecrease : -sensitivity * raMaxIncrease; + double maxFlowIncrease = sensitivity >= 0 ? sensitivity * raMaxIncrease : -sensitivity * raMaxDecrease; + + return cnecMarginToUpperBound + maxFlowDecrease < 0 || cnecMarginToLowerBound + maxFlowIncrease < 0; + } } diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/objective_function_evaluator/MarginEvaluatorWithPstLimitationUnoptimizedCnecs.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/objective_function_evaluator/MarginEvaluatorWithPstLimitationUnoptimizedCnecs.java index e7e8c37306..20a9144385 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/objective_function_evaluator/MarginEvaluatorWithPstLimitationUnoptimizedCnecs.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/objective_function_evaluator/MarginEvaluatorWithPstLimitationUnoptimizedCnecs.java @@ -10,7 +10,8 @@ import com.farao_community.farao.commons.Unit; import com.farao_community.farao.data.crac_api.cnec.FlowCnec; import com.farao_community.farao.data.crac_api.cnec.Side; -import com.farao_community.farao.data.crac_api.range_action.PstRangeAction; +import com.farao_community.farao.data.crac_api.range_action.RangeAction; +import com.farao_community.farao.search_tree_rao.commons.RaoUtil; import com.farao_community.farao.search_tree_rao.result.api.FlowResult; import com.farao_community.farao.search_tree_rao.result.api.RangeActionActivationResult; import com.farao_community.farao.search_tree_rao.result.api.RangeActionSetpointResult; @@ -27,14 +28,14 @@ */ public class MarginEvaluatorWithPstLimitationUnoptimizedCnecs implements MarginEvaluator { private final MarginEvaluator marginEvaluator; - private final Map flowCnecPstRangeActionMap; + private final Map> flowCnecRangeActionMap; private final RangeActionSetpointResult prePerimeterRangeActionSetpointResult; public MarginEvaluatorWithPstLimitationUnoptimizedCnecs(MarginEvaluator marginEvaluator, - Map flowCnecPstRangeActionMap, + Map> flowCnecRangeActionMap, RangeActionSetpointResult rangeActionActivationResult) { this.marginEvaluator = marginEvaluator; - this.flowCnecPstRangeActionMap = flowCnecPstRangeActionMap; + this.flowCnecRangeActionMap = flowCnecRangeActionMap; this.prePerimeterRangeActionSetpointResult = rangeActionActivationResult; } @@ -47,36 +48,10 @@ public double getMargin(FlowResult flowResult, FlowCnec flowCnec, RangeActionAct @Override public double getMargin(FlowResult flowResult, FlowCnec flowCnec, Side side, RangeActionActivationResult rangeActionActivationResult, SensitivityResult sensitivityResult, Unit unit) { - if (cnecShouldBeConsidered(flowResult, flowCnec, side, rangeActionActivationResult, sensitivityResult, unit)) { + if (RaoUtil.cnecShouldBeOptimized(flowCnecRangeActionMap, flowResult, flowCnec, side, rangeActionActivationResult, prePerimeterRangeActionSetpointResult, sensitivityResult, unit)) { return marginEvaluator.getMargin(flowResult, flowCnec, side, rangeActionActivationResult, sensitivityResult, unit); } else { return Double.MAX_VALUE; } } - - private boolean cnecShouldBeConsidered(FlowResult flowResult, FlowCnec flowCnec, Side side, RangeActionActivationResult rangeActionActivationResult, SensitivityResult sensitivityResult, Unit unit) { - if (!flowCnecPstRangeActionMap.containsKey(flowCnec)) { - return true; - } - PstRangeAction pstRangeAction = flowCnecPstRangeActionMap.get(flowCnec); - double sensitivity = sensitivityResult.getSensitivityValue(flowCnec, side, pstRangeAction, Unit.MEGAWATT); - double minSetpoint = pstRangeAction.getMinAdmissibleSetpoint(prePerimeterRangeActionSetpointResult.getSetpoint(pstRangeAction)); - double maxSetpoint = pstRangeAction.getMaxAdmissibleSetpoint(prePerimeterRangeActionSetpointResult.getSetpoint(pstRangeAction)); - // GetOptimizedSetpoint retrieves the latest activated range action's setpoint - double currentSetpoint = rangeActionActivationResult.getOptimizedSetpoint(pstRangeAction, flowCnec.getState()); - double aboveThresholdMargin = flowCnec.getUpperBound(side, unit).orElse(Double.POSITIVE_INFINITY) - flowResult.getFlow(flowCnec, side, unit); - double belowThresholdMargin = flowResult.getFlow(flowCnec, side, unit) - flowCnec.getLowerBound(side, unit).orElse(Double.NEGATIVE_INFINITY); - - double aboveThresholdConstraint; - double belowThresholdConstraint; - if (sensitivity >= 0) { - aboveThresholdConstraint = sensitivity * (currentSetpoint - minSetpoint) + aboveThresholdMargin; - belowThresholdConstraint = sensitivity * (maxSetpoint - currentSetpoint) + belowThresholdMargin; - } else { - aboveThresholdConstraint = Math.abs(sensitivity) * (maxSetpoint - currentSetpoint) + aboveThresholdMargin; - belowThresholdConstraint = Math.abs(sensitivity) * (currentSetpoint - minSetpoint) + belowThresholdMargin; - } - - return aboveThresholdConstraint <= 0 || belowThresholdConstraint <= 0; - } } diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/objective_function_evaluator/ObjectiveFunction.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/objective_function_evaluator/ObjectiveFunction.java index f183753946..94d3b32bcc 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/objective_function_evaluator/ObjectiveFunction.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/objective_function_evaluator/ObjectiveFunction.java @@ -90,7 +90,7 @@ public ObjectiveFunction buildForInitialSensitivityComputation(Set flo // Unoptimized cnecs in series with psts if (!raoParameters.getNotOptimizedCnecsParameters().getDoNotOptimizeCnecsSecuredByTheirPst().isEmpty()) { this.withFunctionalCostEvaluator(new MinMarginEvaluator(flowCnecs, raoParameters.getObjectiveFunctionParameters().getType().getUnit(), - new MarginEvaluatorWithPstLimitationUnoptimizedCnecs(marginEvaluator, UnoptimizedCnecParameters.getUnoptimizedCnecsInSeriesWithPsts(raoParameters.getNotOptimizedCnecsParameters(), crac), prePerimeterRangeActionSetpointResult))); + new MarginEvaluatorWithPstLimitationUnoptimizedCnecs(marginEvaluator, UnoptimizedCnecParameters.getDoNotOptimizeCnecsSecuredByTheirPst(raoParameters.getNotOptimizedCnecsParameters(), crac), prePerimeterRangeActionSetpointResult))); } else { this.withFunctionalCostEvaluator(new MinMarginEvaluator(flowCnecs, raoParameters.getObjectiveFunctionParameters().getType().getUnit(), marginEvaluator)); } @@ -123,7 +123,7 @@ public ObjectiveFunction build(Set flowCnecs, // Unoptimized cnecs in series with psts } else if (!raoParameters.getNotOptimizedCnecsParameters().getDoNotOptimizeCnecsSecuredByTheirPst().isEmpty()) { this.withFunctionalCostEvaluator(new MinMarginEvaluator(flowCnecs, raoParameters.getObjectiveFunctionParameters().getType().getUnit(), - new MarginEvaluatorWithPstLimitationUnoptimizedCnecs(marginEvaluator, UnoptimizedCnecParameters.getUnoptimizedCnecsInSeriesWithPsts(raoParameters.getNotOptimizedCnecsParameters(), crac), prePerimeterRangeActionSetpointResult))); + new MarginEvaluatorWithPstLimitationUnoptimizedCnecs(marginEvaluator, UnoptimizedCnecParameters.getDoNotOptimizeCnecsSecuredByTheirPst(raoParameters.getNotOptimizedCnecsParameters(), crac), prePerimeterRangeActionSetpointResult))); } else { this.withFunctionalCostEvaluator(new MinMarginEvaluator(flowCnecs, raoParameters.getObjectiveFunctionParameters().getType().getUnit(), marginEvaluator)); } diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/parameters/UnoptimizedCnecParameters.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/parameters/UnoptimizedCnecParameters.java index 7e6dc4fdaf..e5afb8ae9f 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/parameters/UnoptimizedCnecParameters.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/parameters/UnoptimizedCnecParameters.java @@ -11,6 +11,7 @@ import com.farao_community.farao.data.crac_api.Crac; import com.farao_community.farao.data.crac_api.cnec.FlowCnec; import com.farao_community.farao.data.crac_api.range_action.PstRangeAction; +import com.farao_community.farao.data.crac_api.range_action.RangeAction; import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; import com.farao_community.farao.rao_api.parameters.NotOptimizedCnecsParameters; @@ -28,14 +29,14 @@ */ public class UnoptimizedCnecParameters { private final Set operatorNotToOptimize; - private final Map unoptimizedCnecsInSeriesWithPsts; + private final Map> unoptimizedCnecsInSeriesWithPsts; - public UnoptimizedCnecParameters(Set operatorNotToOptimize, Map unoptimizedCnecsInSeriesWithPsts) { + public UnoptimizedCnecParameters(Set operatorNotToOptimize, Map> unoptimizedCnecsInSeriesWithPsts) { this.operatorNotToOptimize = operatorNotToOptimize; this.unoptimizedCnecsInSeriesWithPsts = unoptimizedCnecsInSeriesWithPsts; } - public Map getUnoptimizedCnecsInSeriesWithPsts() { + public Map> getDoNotOptimizeCnecsSecuredByTheirPst() { return unoptimizedCnecsInSeriesWithPsts; } @@ -55,22 +56,22 @@ public static UnoptimizedCnecParameters build(NotOptimizedCnecsParameters parame } else if (!parameters.getDoNotOptimizeCnecsSecuredByTheirPst().isEmpty()) { return new UnoptimizedCnecParameters( null, - getUnoptimizedCnecsInSeriesWithPstsFromIds(parameters.getDoNotOptimizeCnecsSecuredByTheirPst(), crac)); + getDoNotOptimizeCnecsSecuredByTheirPstFromIds(parameters.getDoNotOptimizeCnecsSecuredByTheirPst(), crac)); } else { return null; } } - public static Map getUnoptimizedCnecsInSeriesWithPsts(NotOptimizedCnecsParameters parameters, Crac crac) { + public static Map> getDoNotOptimizeCnecsSecuredByTheirPst(NotOptimizedCnecsParameters parameters, Crac crac) { if (!parameters.getDoNotOptimizeCnecsSecuredByTheirPst().isEmpty()) { - return getUnoptimizedCnecsInSeriesWithPstsFromIds(parameters.getDoNotOptimizeCnecsSecuredByTheirPst(), crac); + return getDoNotOptimizeCnecsSecuredByTheirPstFromIds(parameters.getDoNotOptimizeCnecsSecuredByTheirPst(), crac); } else { return Collections.emptyMap(); } } - private static Map getUnoptimizedCnecsInSeriesWithPstsFromIds(Map ids, Crac crac) { - Map mapOfUnoptimizedCnecsAndPsts = new HashMap<>(); + private static Map> getDoNotOptimizeCnecsSecuredByTheirPstFromIds(Map ids, Crac crac) { + Map> mapOfUnoptimizedCnecsAndPsts = new HashMap<>(); // Create map elements for all cnecs with network element id in ids.keySet() for (Map.Entry entrySet : ids.entrySet()) { String cnecId = entrySet.getKey(); diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/fillers/UnoptimizedCnecFiller.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/fillers/UnoptimizedCnecFiller.java index e240e4a7b9..35eddec6a3 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/fillers/UnoptimizedCnecFiller.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/fillers/UnoptimizedCnecFiller.java @@ -11,7 +11,7 @@ import com.farao_community.farao.data.crac_api.Identifiable; import com.farao_community.farao.data.crac_api.State; import com.farao_community.farao.data.crac_api.cnec.FlowCnec; -import com.farao_community.farao.data.crac_api.range_action.PstRangeAction; +import com.farao_community.farao.data.crac_api.range_action.RangeAction; import com.farao_community.farao.search_tree_rao.commons.RaoUtil; import com.farao_community.farao.search_tree_rao.commons.optimization_perimeters.OptimizationPerimeter; import com.farao_community.farao.search_tree_rao.commons.parameters.UnoptimizedCnecParameters; @@ -47,7 +47,7 @@ public class UnoptimizedCnecFiller implements ProblemFiller { private final FlowResult prePerimeterFlowResult; private final Set operatorsNotToOptimize; private final double highestThresholdValue; - private final Map flowCnecPstRangeActionMap; + private final Map> flowCnecRangeActionMap; private UnoptimizedCnecFillerRule selectedRule; public UnoptimizedCnecFiller(OptimizationPerimeter optimizationContext, @@ -60,7 +60,7 @@ public UnoptimizedCnecFiller(OptimizationPerimeter optimizationContext, this.prePerimeterFlowResult = prePerimeterFlowResult; this.operatorsNotToOptimize = unoptimizedCnecParameters.getOperatorsNotToOptimize(); this.highestThresholdValue = RaoUtil.getLargestCnecThreshold(flowCnecs, MEGAWATT); - this.flowCnecPstRangeActionMap = unoptimizedCnecParameters.getUnoptimizedCnecsInSeriesWithPsts(); + this.flowCnecRangeActionMap = unoptimizedCnecParameters.getDoNotOptimizeCnecsSecuredByTheirPst(); } public enum UnoptimizedCnecFillerRule { @@ -69,7 +69,7 @@ public enum UnoptimizedCnecFillerRule { } private void selectUnoptimizedCnecFillerRule() { - if (Objects.nonNull(flowCnecPstRangeActionMap)) { + if (Objects.nonNull(flowCnecRangeActionMap)) { selectedRule = UnoptimizedCnecFillerRule.PST_LIMITATION; } else { selectedRule = UnoptimizedCnecFillerRule.MARGIN_DECREASE; @@ -121,7 +121,7 @@ private Set getFlowCnecs() { .collect(Collectors.toSet()); } else { return optimizationContext.getFlowCnecs().stream() - .filter(flowCnecPstRangeActionMap::containsKey) + .filter(flowCnecRangeActionMap::containsKey) .collect(Collectors.toSet()); } } @@ -207,12 +207,12 @@ private void defineDontOptimizeCnecConstraintsForCnecsInSeriesWithPsts(LinearPro if (Objects.isNull(state)) { return; } - FaraoMPVariable setPointVariable = linearProblem.getRangeActionSetpointVariable(flowCnecPstRangeActionMap.get(cnec), state); - checkVariableCreation(setPointVariable, String.format("Range action variable for PST %s has not been defined yet.", flowCnecPstRangeActionMap.get(cnec).getId())); + FaraoMPVariable setPointVariable = linearProblem.getRangeActionSetpointVariable(flowCnecRangeActionMap.get(cnec), state); + checkVariableCreation(setPointVariable, String.format("Range action variable for PST %s has not been defined yet.", flowCnecRangeActionMap.get(cnec).getId())); double maxSetpoint = setPointVariable.ub(); double minSetpoint = setPointVariable.lb(); - double sensitivity = sensitivityResult.getSensitivityValue(cnec, side, flowCnecPstRangeActionMap.get(cnec), MEGAWATT); + double sensitivity = sensitivityResult.getSensitivityValue(cnec, side, flowCnecRangeActionMap.get(cnec), MEGAWATT); Optional minFlow = cnec.getLowerBound(side, MEGAWATT); Optional maxFlow = cnec.getUpperBound(side, MEGAWATT); double bigM = maxSetpoint - minSetpoint; @@ -307,7 +307,7 @@ private State getLastStateWithRangeActionAvailableForCnec(FlowCnec cnec) { .collect(Collectors.toList()); Optional lastState = statesBeforeCnec.stream().filter(state -> - optimizationContext.getRangeActionsPerState().get(state).contains(flowCnecPstRangeActionMap.get(cnec))) + optimizationContext.getRangeActionsPerState().get(state).contains(flowCnecRangeActionMap.get(cnec))) .findFirst(); if (lastState.isEmpty()) { // Range action (referenced for "cnec" in flowCnecPstRangeActionMap) is unavailable for cnec diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/castor/algorithm/AutomatonSimulatorTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/castor/algorithm/AutomatonSimulatorTest.java index 6638176bca..917a5b3fa9 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/castor/algorithm/AutomatonSimulatorTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/castor/algorithm/AutomatonSimulatorTest.java @@ -19,6 +19,7 @@ import com.farao_community.farao.data.crac_api.range_action.RangeAction; import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; import com.farao_community.farao.data.rao_result_api.ComputationStatus; +import com.farao_community.farao.rao_api.parameters.NotOptimizedCnecsParameters; import com.farao_community.farao.rao_api.parameters.ObjectiveFunctionParameters; import com.farao_community.farao.rao_api.parameters.RaoParameters; import com.farao_community.farao.search_tree_rao.commons.ToolProvider; @@ -27,6 +28,7 @@ import com.farao_community.farao.search_tree_rao.result.api.RangeActionSetpointResult; import com.farao_community.farao.search_tree_rao.result.api.SensitivityResult; import com.farao_community.farao.search_tree_rao.result.impl.AutomatonPerimeterResultImpl; +import com.farao_community.farao.search_tree_rao.result.impl.RangeActionSetpointResultImpl; import com.powsybl.iidm.network.HvdcLine; import com.powsybl.iidm.network.Network; import com.powsybl.iidm.network.extensions.HvdcAngleDroopActivePowerControl; @@ -66,6 +68,8 @@ class AutomatonSimulatorTest { private FlowCnec cnec2; private PrePerimeterSensitivityAnalysis mockedPreAutoPerimeterSensitivityAnalysis; private PrePerimeterResult mockedPrePerimeterResult; + private RaoParameters raoParameters; + private ToolProvider toolProvider; private static final double DOUBLE_TOLERANCE = 0.01; @@ -217,7 +221,7 @@ public void setup() { autoState = crac.getState(contingency1, Instant.AUTO); - RaoParameters raoParameters = new RaoParameters(); + raoParameters = new RaoParameters(); raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT); raoParameters.getLoadFlowAndSensitivityParameters().setSensitivityProvider("OpenLoadFlow"); @@ -225,7 +229,7 @@ public void setup() { mockedPrePerimeterResult = mock(PrePerimeterResult.class); when(mockedPreAutoPerimeterSensitivityAnalysis.runBasedOnInitialResults(any(), any(), any(), any(), any(), any())).thenReturn(mockedPrePerimeterResult); - ToolProvider toolProvider = Mockito.mock(ToolProvider.class); + toolProvider = Mockito.mock(ToolProvider.class); when(toolProvider.getLoopFlowCnecs(any())).thenReturn(Collections.emptySet()); automatonSimulator = new AutomatonSimulator(crac, raoParameters, toolProvider, null, null, mockedPrePerimeterResult, null, 0); } @@ -664,4 +668,55 @@ void testDisableHvdcAngleDroopControlBeforeShifting() { assertEquals(HvdcLine.ConvertersMode.SIDE_1_INVERTER_SIDE_2_RECTIFIER, network.getHvdcLine("BBE2AA12 FFR3AA12 1").getConvertersMode()); } + @Test + void testIgnoreNotOptimizedCnec() { + when(mockedPreAutoPerimeterSensitivityAnalysis.runBasedOnInitialResults(any(), any(), any(), any(), any(), any())).thenReturn(mockedPrePerimeterResult); + + NotOptimizedCnecsParameters notOptimizedCnecsParameters = new NotOptimizedCnecsParameters(); + notOptimizedCnecsParameters.setDoNotOptimizeCnecsSecuredByTheirPst(Map.of("cnec-ne", "BBE2AA11 BBE3AA11 1")); // cnec is coupled with ara1 + raoParameters.setNotOptimizedCnecsParameters(notOptimizedCnecsParameters); + + RangeActionSetpointResultImpl prePerimeterRangeActionSetpointResult = new RangeActionSetpointResultImpl(Map.of(ara1, 0.1, ara2, 0.1)); + automatonSimulator = new AutomatonSimulator(crac, raoParameters, toolProvider, null, prePerimeterRangeActionSetpointResult, mockedPrePerimeterResult, null, 0); + + // only one CNEC monitored, with a flow of 481 MW, 100 MW over its upper threshold (381 MW) + // associated ara1 is initially at tap position 0.1 with a sensitivity of +50MW/° and a min position of -3.1 + // So it has enough set-points left to remove the overload => no RA should be used + when(mockedPrePerimeterResult.getFlow(eq(cnec1), any(), eq(Unit.MEGAWATT))).thenReturn(481.1); + when(mockedPrePerimeterResult.getMargin(cnec1, Unit.MEGAWATT)).thenReturn(-100.); + when(mockedPrePerimeterResult.getMargin(eq(cnec1), any(), eq(Unit.MEGAWATT))).thenReturn(-100.); + when(mockedPrePerimeterResult.getSensitivityValue(eq(cnec1), any(), eq(ara1), eq(Unit.MEGAWATT))).thenReturn(50.); + when(mockedPrePerimeterResult.getSensitivityValue(eq(cnec1), any(), eq(ara2), eq(Unit.MEGAWATT))).thenReturn(50.); + + AutomatonSimulator.RangeAutomatonSimulationResult shiftResult = + automatonSimulator.shiftRangeActionsUntilFlowCnecsSecure(List.of(ara1, ara2), Set.of(cnec1), network, mockedPreAutoPerimeterSensitivityAnalysis, mockedPrePerimeterResult, autoState); + assertTrue(shiftResult.getActivatedRangeActions().isEmpty()); + } + + @Test + void testDontIgnoreNotOptimizedCnec() { + when(mockedPreAutoPerimeterSensitivityAnalysis.runBasedOnInitialResults(any(), any(), any(), any(), any(), any())).thenReturn(mockedPrePerimeterResult); + + NotOptimizedCnecsParameters notOptimizedCnecsParameters = new NotOptimizedCnecsParameters(); + notOptimizedCnecsParameters.setDoNotOptimizeCnecsSecuredByTheirPst(Map.of("cnec-ne", "BBE2AA11 BBE3AA11 1")); // cnec is coupled with ara1 + raoParameters.setNotOptimizedCnecsParameters(notOptimizedCnecsParameters); + + // only one CNEC monitored, with a flow of 481 MW, 100 MW over its upper threshold (381 MW) + // associated ara1 is initially at tap position -2.1 with a sensitivity of +50MW/° and a min position of -3.1 + // So it doesn't have enough set-points left to remove the overload => ara1 and aligned ara2 should be used to reduce the flow + when(mockedPrePerimeterResult.getFlow(eq(cnec1), any(), eq(Unit.MEGAWATT))).thenReturn(481.1); + when(mockedPrePerimeterResult.getMargin(cnec1, Unit.MEGAWATT)).thenReturn(-100.); + when(mockedPrePerimeterResult.getMargin(eq(cnec1), any(), eq(Unit.MEGAWATT))).thenReturn(-100.); + when(mockedPrePerimeterResult.getSensitivityValue(eq(cnec1), any(), eq(ara1), eq(Unit.MEGAWATT))).thenReturn(50.); + when(mockedPrePerimeterResult.getSensitivityValue(eq(cnec1), any(), eq(ara2), eq(Unit.MEGAWATT))).thenReturn(50.); + + RangeActionSetpointResultImpl prePerimeterRangeActionSetpointResult = new RangeActionSetpointResultImpl(Map.of(ara1, -2.1, ara2, 0.1)); + automatonSimulator = new AutomatonSimulator(crac, raoParameters, toolProvider, null, prePerimeterRangeActionSetpointResult, mockedPrePerimeterResult, null, 0); + + AutomatonSimulator.RangeAutomatonSimulationResult shiftResult = + automatonSimulator.shiftRangeActionsUntilFlowCnecsSecure(List.of(ara1, ara2), Set.of(cnec1), network, mockedPreAutoPerimeterSensitivityAnalysis, mockedPrePerimeterResult, autoState); + assertEquals(-3.1, shiftResult.getRangeActionsWithSetpoint().get(ara1), DOUBLE_TOLERANCE); + assertEquals(-3.1, shiftResult.getRangeActionsWithSetpoint().get(ara2), DOUBLE_TOLERANCE); + } + } diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoUtilTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoUtilTest.java index 4bb575ac5a..db5e9ec34c 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoUtilTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoUtilTest.java @@ -17,6 +17,8 @@ import com.farao_community.farao.data.crac_api.cnec.Side; import com.farao_community.farao.data.crac_api.network_action.ActionType; import com.farao_community.farao.data.crac_api.network_action.NetworkAction; +import com.farao_community.farao.data.crac_api.range_action.PstRangeAction; +import com.farao_community.farao.data.crac_api.range_action.RangeAction; import com.farao_community.farao.data.crac_api.usage_rule.OnFlowConstraint; import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; import com.farao_community.farao.data.crac_impl.utils.CommonCracCreation; @@ -26,6 +28,9 @@ import com.farao_community.farao.rao_api.parameters.RaoParameters; import com.farao_community.farao.rao_api.parameters.extensions.RelativeMarginsParametersExtension; import com.farao_community.farao.search_tree_rao.result.api.FlowResult; +import com.farao_community.farao.search_tree_rao.result.api.PrePerimeterResult; +import com.farao_community.farao.search_tree_rao.result.api.RangeActionSetpointResult; +import com.farao_community.farao.search_tree_rao.result.api.SensitivityResult; import com.powsybl.glsk.commons.ZonalData; import com.powsybl.glsk.ucte.UcteGlskDocument; import com.powsybl.iidm.network.Country; @@ -35,10 +40,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Optional; -import java.util.Set; +import java.util.*; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; @@ -61,7 +63,7 @@ class RaoUtilTest { @BeforeEach public void setUp() { network = NetworkImportsUtil.import12NodesNetwork(); - crac = CommonCracCreation.create(); + crac = CommonCracCreation.createWithPreventivePstRange(); variantId = network.getVariantManager().getWorkingVariantId(); raoInput = RaoInput.buildWithPreventiveState(network, crac) .withNetworkVariantId(variantId) @@ -276,4 +278,76 @@ void testIsOnFlowConstraintInCountryAvailable() { private void assertIsOnFlowInCountryAvailable(RemedialAction ra, State optimizedState, FlowResult flowResult, boolean available) { assertEquals(available, ra.isRemedialActionAvailable(optimizedState, RaoUtil.isAnyMarginNegative(flowResult, ra.getFlowCnecsConstrainingUsageRules(crac.getFlowCnecs(), network, optimizedState), raoParameters.getObjectiveFunctionParameters().getType().getUnit()))); } + + @Test + void testCnecShouldBeOptimizedBasic() { + FlowCnec cnec = crac.getFlowCnec("cnec1basecase"); + PstRangeAction pst = crac.getPstRangeAction("pst"); + FlowResult flowResult = mock(FlowResult.class); + RangeActionSetpointResult prePerimeterRangeActionSetpointResult = mock(PrePerimeterResult.class); + SensitivityResult sensitivityResult = mock(SensitivityResult.class); + + // Cnec not in map + assertTrue(RaoUtil.cnecShouldBeOptimized(Map.of(), flowResult, cnec, Side.LEFT, Map.of(), prePerimeterRangeActionSetpointResult, sensitivityResult, Unit.MEGAWATT)); + + // Margins > 0 + when(flowResult.getFlow(cnec, Side.LEFT, Unit.MEGAWATT)).thenReturn(0.); + assertFalse(RaoUtil.cnecShouldBeOptimized(Map.of(cnec, pst), flowResult, cnec, Side.LEFT, Map.of(), prePerimeterRangeActionSetpointResult, sensitivityResult, Unit.MEGAWATT)); + } + + @Test + void testCnecShouldBeOptimizedUpper() { + FlowCnec cnec = crac.getFlowCnec("cnec1basecase"); + PstRangeAction pst = crac.getPstRangeAction("pst"); + FlowResult flowResult = mock(FlowResult.class); + RangeActionSetpointResult prePerimeterRangeActionSetpointResult = mock(PrePerimeterResult.class); + SensitivityResult sensitivityResult = mock(SensitivityResult.class); + Map> map = Map.of(cnec, pst); + + // Upper margin < 0 (max threshold is 2279 A) + when(flowResult.getFlow(cnec, Side.LEFT, Unit.AMPERE)).thenReturn(2379.); + + // Sensi > 0 + when(sensitivityResult.getSensitivityValue(cnec, Side.LEFT, pst, Unit.MEGAWATT)).thenReturn(33.); // = 50 A + // Some taps left (PST at set-point -4.22, can go down to -6.2) + assertFalse(RaoUtil.cnecShouldBeOptimized(map, flowResult, cnec, Side.LEFT, Map.of(pst, -4.22), prePerimeterRangeActionSetpointResult, sensitivityResult, Unit.AMPERE)); + // Not enough taps left + assertTrue(RaoUtil.cnecShouldBeOptimized(map, flowResult, cnec, Side.LEFT, Map.of(pst, -5.22), prePerimeterRangeActionSetpointResult, sensitivityResult, Unit.AMPERE)); + // Sensi < 0 + when(sensitivityResult.getSensitivityValue(cnec, Side.LEFT, pst, Unit.MEGAWATT)).thenReturn(-33.); // = -50 A + // Some taps left (PST at set-point 4.22, can go up to 6.2) + assertFalse(RaoUtil.cnecShouldBeOptimized(map, flowResult, cnec, Side.LEFT, Map.of(pst, 4.22), prePerimeterRangeActionSetpointResult, sensitivityResult, Unit.AMPERE)); + // Not enough taps left + assertTrue(RaoUtil.cnecShouldBeOptimized(map, flowResult, cnec, Side.LEFT, Map.of(pst, 5.22), prePerimeterRangeActionSetpointResult, sensitivityResult, Unit.AMPERE)); + } + + @Test + void testCnecShouldBeOptimizedLower() { + FlowCnec cnec = crac.getFlowCnec("cnec1basecase"); + PstRangeAction pst = crac.getPstRangeAction("pst"); + FlowResult flowResult = mock(FlowResult.class); + RangeActionSetpointResult prePerimeterRangeActionSetpointResult = mock(PrePerimeterResult.class); + SensitivityResult sensitivityResult = mock(SensitivityResult.class); + Map> map = Map.of(cnec, pst); + + // Lower margin < 0 (min threshold is -1500 MW) + when(flowResult.getFlow(cnec, Side.LEFT, Unit.MEGAWATT)).thenReturn(-1700.); + + // Sensi > 0 + when(sensitivityResult.getSensitivityValue(cnec, Side.LEFT, pst, Unit.MEGAWATT)).thenReturn(50.); + // Some taps left (PST at set-point 2.22, can go up to 6.2) + when(prePerimeterRangeActionSetpointResult.getSetpoint(pst)).thenReturn(2.22); + assertFalse(RaoUtil.cnecShouldBeOptimized(map, flowResult, cnec, Side.LEFT, Map.of(), prePerimeterRangeActionSetpointResult, sensitivityResult, Unit.MEGAWATT)); + // Not enough taps left + when(prePerimeterRangeActionSetpointResult.getSetpoint(pst)).thenReturn(3.22); + assertTrue(RaoUtil.cnecShouldBeOptimized(map, flowResult, cnec, Side.LEFT, Map.of(), prePerimeterRangeActionSetpointResult, sensitivityResult, Unit.MEGAWATT)); + // Sensi < 0 + when(sensitivityResult.getSensitivityValue(cnec, Side.LEFT, pst, Unit.MEGAWATT)).thenReturn(-50.); + // Some taps left (PST at set-point -2.22, can go down to -6.2) + when(prePerimeterRangeActionSetpointResult.getSetpoint(pst)).thenReturn(-2.22); + assertFalse(RaoUtil.cnecShouldBeOptimized(map, flowResult, cnec, Side.LEFT, Map.of(), prePerimeterRangeActionSetpointResult, sensitivityResult, Unit.MEGAWATT)); + // Not enough taps left + when(prePerimeterRangeActionSetpointResult.getSetpoint(pst)).thenReturn(-3.22); + assertTrue(RaoUtil.cnecShouldBeOptimized(map, flowResult, cnec, Side.LEFT, Map.of(), prePerimeterRangeActionSetpointResult, sensitivityResult, Unit.MEGAWATT)); + } } diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/objective_function_evaluator/MarginEvaluatorWithPstLimitationUnoptimizedCnecsTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/objective_function_evaluator/MarginEvaluatorWithPstLimitationUnoptimizedCnecsTest.java index 036e3ae381..ab8290a8a4 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/objective_function_evaluator/MarginEvaluatorWithPstLimitationUnoptimizedCnecsTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/objective_function_evaluator/MarginEvaluatorWithPstLimitationUnoptimizedCnecsTest.java @@ -11,6 +11,7 @@ import com.farao_community.farao.data.crac_api.cnec.FlowCnec; import com.farao_community.farao.data.crac_api.cnec.Side; import com.farao_community.farao.data.crac_api.range_action.PstRangeAction; +import com.farao_community.farao.data.crac_api.range_action.RangeAction; import com.farao_community.farao.search_tree_rao.result.api.FlowResult; import com.farao_community.farao.search_tree_rao.result.api.RangeActionActivationResult; import com.farao_community.farao.search_tree_rao.result.api.RangeActionSetpointResult; @@ -39,9 +40,9 @@ class MarginEvaluatorWithPstLimitationUnoptimizedCnecsTest { private final FlowCnec flowCnecInSeries = Mockito.mock(FlowCnec.class); private final PstRangeAction pstRangeActionInSeries = Mockito.mock(PstRangeAction.class); - private Map flowCnecPstRangeActionMap = Map.of(flowCnecInSeries, pstRangeActionInSeries); + private final Map> flowCnecPstRangeActionMap = Map.of(flowCnecInSeries, pstRangeActionInSeries); private final RangeActionSetpointResult rangeActionSetpointResult = Mockito.mock(RangeActionSetpointResult.class); - private MarginEvaluatorWithPstLimitationUnoptimizedCnecs marginEvaluatorWithPstLimitationUnoptimizedCnecs = + private final MarginEvaluatorWithPstLimitationUnoptimizedCnecs marginEvaluatorWithPstLimitationUnoptimizedCnecs = new MarginEvaluatorWithPstLimitationUnoptimizedCnecs( new BasicMarginEvaluator(), flowCnecPstRangeActionMap, @@ -68,7 +69,7 @@ void unoptimizedCnecTest1() { when(sensitivityResult.getSensitivityValue(flowCnecInSeries, Side.LEFT, pstRangeActionInSeries, Unit.MEGAWATT)).thenReturn(50.); when(pstRangeActionInSeries.getMinAdmissibleSetpoint(rangeActionSetpointResult.getSetpoint(pstRangeActionInSeries))).thenReturn(2.); when(pstRangeActionInSeries.getMaxAdmissibleSetpoint(rangeActionSetpointResult.getSetpoint(pstRangeActionInSeries))).thenReturn(10.); - when(rangeActionActivationResult.getOptimizedSetpoint(pstRangeActionInSeries, flowCnecInSeries.getState())).thenReturn(5.); + when(rangeActionActivationResult.getOptimizedSetpointsOnState(flowCnecInSeries.getState())).thenReturn(Map.of(pstRangeActionInSeries, 5.)); when(flowCnecInSeries.getLowerBound(Side.LEFT, Unit.MEGAWATT)).thenReturn(Optional.of(40.)); when(flowCnecInSeries.getUpperBound(Side.LEFT, Unit.MEGAWATT)).thenReturn(Optional.of(100.)); when(currentFlowResult.getFlow(flowCnecInSeries, Side.LEFT, Unit.MEGAWATT)).thenReturn(50.); @@ -83,7 +84,7 @@ void unoptimizedCnecTest2() { when(sensitivityResult.getSensitivityValue(flowCnecInSeries, Side.LEFT, pstRangeActionInSeries, Unit.MEGAWATT)).thenReturn(3.); when(pstRangeActionInSeries.getMinAdmissibleSetpoint(rangeActionSetpointResult.getSetpoint(pstRangeActionInSeries))).thenReturn(2.); when(pstRangeActionInSeries.getMaxAdmissibleSetpoint(rangeActionSetpointResult.getSetpoint(pstRangeActionInSeries))).thenReturn(10.); - when(rangeActionActivationResult.getOptimizedSetpoint(pstRangeActionInSeries, flowCnecInSeries.getState())).thenReturn(5.); + when(rangeActionActivationResult.getOptimizedSetpointsOnState(flowCnecInSeries.getState())).thenReturn(Map.of(pstRangeActionInSeries, 5.)); when(flowCnecInSeries.getLowerBound(Side.LEFT, Unit.MEGAWATT)).thenReturn(Optional.of(40.)); when(flowCnecInSeries.getUpperBound(Side.LEFT, Unit.MEGAWATT)).thenReturn(Optional.of(100.)); when(currentFlowResult.getFlow(flowCnecInSeries, Side.LEFT, Unit.MEGAWATT)).thenReturn(20.); @@ -98,7 +99,7 @@ void unoptimizedCnecTest3() { when(sensitivityResult.getSensitivityValue(flowCnecInSeries, Side.LEFT, pstRangeActionInSeries, Unit.MEGAWATT)).thenReturn(3.); when(pstRangeActionInSeries.getMinAdmissibleSetpoint(rangeActionSetpointResult.getSetpoint(pstRangeActionInSeries))).thenReturn(2.); when(pstRangeActionInSeries.getMaxAdmissibleSetpoint(rangeActionSetpointResult.getSetpoint(pstRangeActionInSeries))).thenReturn(10.); - when(rangeActionActivationResult.getOptimizedSetpoint(pstRangeActionInSeries, flowCnecInSeries.getState())).thenReturn(5.); + when(rangeActionActivationResult.getOptimizedSetpointsOnState(flowCnecInSeries.getState())).thenReturn(Map.of(pstRangeActionInSeries, 5.)); when(flowCnecInSeries.getLowerBound(Side.LEFT, Unit.MEGAWATT)).thenReturn(Optional.of(40.)); when(flowCnecInSeries.getUpperBound(Side.LEFT, Unit.MEGAWATT)).thenReturn(Optional.of(100.)); when(currentFlowResult.getFlow(flowCnecInSeries, Side.LEFT, Unit.MEGAWATT)).thenReturn(120.); @@ -113,7 +114,7 @@ void unoptimizedCnecTest4() { when(sensitivityResult.getSensitivityValue(flowCnecInSeries, Side.LEFT, pstRangeActionInSeries, Unit.MEGAWATT)).thenReturn(-1.); when(pstRangeActionInSeries.getMinAdmissibleSetpoint(rangeActionSetpointResult.getSetpoint(pstRangeActionInSeries))).thenReturn(2.); when(pstRangeActionInSeries.getMaxAdmissibleSetpoint(rangeActionSetpointResult.getSetpoint(pstRangeActionInSeries))).thenReturn(10.); - when(rangeActionActivationResult.getOptimizedSetpoint(pstRangeActionInSeries, flowCnecInSeries.getState())).thenReturn(5.); + when(rangeActionActivationResult.getOptimizedSetpointsOnState(flowCnecInSeries.getState())).thenReturn(Map.of(pstRangeActionInSeries, 5.)); when(flowCnecInSeries.getLowerBound(Side.LEFT, Unit.MEGAWATT)).thenReturn(Optional.of(40.)); when(flowCnecInSeries.getUpperBound(Side.LEFT, Unit.MEGAWATT)).thenReturn(Optional.of(100.)); when(currentFlowResult.getFlow(flowCnecInSeries, Side.LEFT, Unit.MEGAWATT)).thenReturn(50.); @@ -128,7 +129,7 @@ void unoptimizedCnecTest5() { when(sensitivityResult.getSensitivityValue(flowCnecInSeries, Side.LEFT, pstRangeActionInSeries, Unit.MEGAWATT)).thenReturn(-3.); when(pstRangeActionInSeries.getMinAdmissibleSetpoint(rangeActionSetpointResult.getSetpoint(pstRangeActionInSeries))).thenReturn(2.); when(pstRangeActionInSeries.getMaxAdmissibleSetpoint(rangeActionSetpointResult.getSetpoint(pstRangeActionInSeries))).thenReturn(10.); - when(rangeActionActivationResult.getOptimizedSetpoint(pstRangeActionInSeries, flowCnecInSeries.getState())).thenReturn(5.); + when(rangeActionActivationResult.getOptimizedSetpointsOnState(flowCnecInSeries.getState())).thenReturn(Map.of(pstRangeActionInSeries, 5.)); when(flowCnecInSeries.getLowerBound(Side.LEFT, Unit.MEGAWATT)).thenReturn(Optional.of(40.)); when(flowCnecInSeries.getUpperBound(Side.LEFT, Unit.MEGAWATT)).thenReturn(Optional.of(100.)); when(currentFlowResult.getFlow(flowCnecInSeries, Side.LEFT, Unit.MEGAWATT)).thenReturn(120.); @@ -142,7 +143,7 @@ void unoptimizedCnecTest6() { when(sensitivityResult.getSensitivityValue(flowCnecInSeries, Side.LEFT, pstRangeActionInSeries, Unit.MEGAWATT)).thenReturn(-3.); when(pstRangeActionInSeries.getMinAdmissibleSetpoint(rangeActionSetpointResult.getSetpoint(pstRangeActionInSeries))).thenReturn(2.); when(pstRangeActionInSeries.getMaxAdmissibleSetpoint(rangeActionSetpointResult.getSetpoint(pstRangeActionInSeries))).thenReturn(10.); - when(rangeActionActivationResult.getOptimizedSetpoint(pstRangeActionInSeries, flowCnecInSeries.getState())).thenReturn(5.); + when(rangeActionActivationResult.getOptimizedSetpointsOnState(flowCnecInSeries.getState())).thenReturn(Map.of(pstRangeActionInSeries, 5.)); when(flowCnecInSeries.getLowerBound(Side.LEFT, Unit.MEGAWATT)).thenReturn(Optional.of(40.)); when(flowCnecInSeries.getUpperBound(Side.LEFT, Unit.MEGAWATT)).thenReturn(Optional.of(100.)); when(currentFlowResult.getFlow(flowCnecInSeries, Side.LEFT, Unit.MEGAWATT)).thenReturn(10.); diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/parameters/UnoptimizedCnecParametersTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/parameters/UnoptimizedCnecParametersTest.java index 8df2d0f3cd..92e721efff 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/parameters/UnoptimizedCnecParametersTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/parameters/UnoptimizedCnecParametersTest.java @@ -13,7 +13,7 @@ import com.farao_community.farao.data.crac_api.cnec.FlowCnec; import com.farao_community.farao.data.crac_api.cnec.Side; import com.farao_community.farao.data.crac_api.range.RangeType; -import com.farao_community.farao.data.crac_api.range_action.PstRangeAction; +import com.farao_community.farao.data.crac_api.range_action.RangeAction; import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; import com.farao_community.farao.data.crac_impl.CracImpl; import com.farao_community.farao.rao_api.parameters.RaoParameters; @@ -114,7 +114,7 @@ void testUnoptimizedCnecsInSeriesWithPsts() { "fakeId", "pst2")); UnoptimizedCnecParameters.build(raoParameters.getNotOptimizedCnecsParameters(), Set.of("BE"), crac); - Map map = UnoptimizedCnecParameters.getUnoptimizedCnecsInSeriesWithPsts(raoParameters.getNotOptimizedCnecsParameters(), crac); + Map> map = UnoptimizedCnecParameters.getDoNotOptimizeCnecsSecuredByTheirPst(raoParameters.getNotOptimizedCnecsParameters(), crac); assertEquals(3, raoParameters.getNotOptimizedCnecsParameters().getDoNotOptimizeCnecsSecuredByTheirPst().size()); assertEquals(1, map.size()); assertTrue(map.containsKey(crac.getFlowCnec("flowCnec-1"))); @@ -134,7 +134,7 @@ void testUnoptimizedCnecsInSeriesWithPsts() { .add(); RaoParameters newRaoParameters = new RaoParameters(); newRaoParameters.getNotOptimizedCnecsParameters().setDoNotOptimizeCnecsSecuredByTheirPst(Map.of("ne1Id", "pst2")); - Map newMap = UnoptimizedCnecParameters.getUnoptimizedCnecsInSeriesWithPsts(newRaoParameters.getNotOptimizedCnecsParameters(), crac); + Map> newMap = UnoptimizedCnecParameters.getDoNotOptimizeCnecsSecuredByTheirPst(newRaoParameters.getNotOptimizedCnecsParameters(), crac); assertEquals(0, newMap.size()); } diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/fillers/UnoptimizedCnecFillerPstLimitationRuleTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/fillers/UnoptimizedCnecFillerPstLimitationRuleTest.java index f1c55d63a6..463b79836c 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/fillers/UnoptimizedCnecFillerPstLimitationRuleTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/fillers/UnoptimizedCnecFillerPstLimitationRuleTest.java @@ -13,6 +13,7 @@ import com.farao_community.farao.data.crac_api.cnec.Side; import com.farao_community.farao.data.crac_api.range.RangeType; import com.farao_community.farao.data.crac_api.range_action.PstRangeAction; +import com.farao_community.farao.data.crac_api.range_action.RangeAction; import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; import com.farao_community.farao.rao_api.parameters.RangeActionsOptimizationParameters; import com.farao_community.farao.rao_api.parameters.RaoParameters; @@ -53,7 +54,7 @@ class UnoptimizedCnecFillerPstLimitationRuleTest extends AbstractFillerTest { private FlowCnec classicCnec; private PstRangeAction pstRangeActionInSeries; private OptimizationPerimeter optimizationPerimeter; - private Map flowCnecPstRangeActionMap = new HashMap<>(); + private Map> flowCnecRangeActionMap = new HashMap<>(); private double constraintCoeff; @BeforeEach @@ -84,7 +85,7 @@ public void setUp() { classicCnec = crac.getFlowCnec("Tieline BE FR - N - preventive"); pstRangeActionInSeries = crac.getPstRangeAction("pstRangeActionInSeries"); - flowCnecPstRangeActionMap.put(cnecInSeries, pstRangeActionInSeries); + flowCnecRangeActionMap.put(cnecInSeries, pstRangeActionInSeries); RangeActionSetpointResult initialRangeActionSetpointResult = new RangeActionSetpointResultImpl(Map.of(pstRangeActionInSeries, 0.5)); @@ -107,7 +108,7 @@ public void setUp() { } private void buildLinearProblemWithMaxMinMarginAndPositiveSensitivityValue() { - UnoptimizedCnecParameters unoptimizedCnecParameters = new UnoptimizedCnecParameters(null, flowCnecPstRangeActionMap); + UnoptimizedCnecParameters unoptimizedCnecParameters = new UnoptimizedCnecParameters(null, flowCnecRangeActionMap); MaxMinMarginFiller maxMinMarginFiller = new MaxMinMarginFiller(Set.of(classicCnec, cnecInSeries), Unit.MEGAWATT); FlowResult initialFlowResult = Mockito.mock(FlowResult.class); when(initialFlowResult.getMargin(classicCnec, Unit.MEGAWATT)).thenReturn(600.); @@ -129,7 +130,7 @@ private void buildLinearProblemWithMaxMinMarginAndPositiveSensitivityValue() { } private void buildLinearProblemWithMaxMinMarginAndNegativeSensitivityValue() { - UnoptimizedCnecParameters unoptimizedCnecParameters = new UnoptimizedCnecParameters(null, flowCnecPstRangeActionMap); + UnoptimizedCnecParameters unoptimizedCnecParameters = new UnoptimizedCnecParameters(null, flowCnecRangeActionMap); MaxMinMarginFiller maxMinMarginFiller = new MaxMinMarginFiller(Set.of(classicCnec, cnecInSeries), Unit.MEGAWATT); FlowResult initialFlowResult = Mockito.mock(FlowResult.class); when(initialFlowResult.getMargin(classicCnec, Unit.MEGAWATT)).thenReturn(400.); @@ -152,7 +153,7 @@ private void buildLinearProblemWithMaxMinMarginAndNegativeSensitivityValue() { private void buildLinearProblemWithMaxMinRelativeMarginAndPositiveSensi() { RelativeMarginsParametersExtension maxMinRelativeMarginParameters = new RelativeMarginsParametersExtension(); - UnoptimizedCnecParameters unoptimizedCnecParameters = new UnoptimizedCnecParameters(null, flowCnecPstRangeActionMap); + UnoptimizedCnecParameters unoptimizedCnecParameters = new UnoptimizedCnecParameters(null, flowCnecRangeActionMap); FlowResult initialFlowResult = Mockito.mock(FlowResult.class); when(initialFlowResult.getMargin(classicCnec, Unit.MEGAWATT)).thenReturn(400.); when(initialFlowResult.getMargin(cnecInSeries, Unit.MEGAWATT)).thenReturn(600.); @@ -186,7 +187,7 @@ private void buildLinearProblemWithMaxMinRelativeMarginAndPositiveSensi() { private void buildLinearProblemWithMaxMinRelativeMarginAndNegativeSensi() { RelativeMarginsParametersExtension maxMinRelativeMarginParameters = new RelativeMarginsParametersExtension(); - UnoptimizedCnecParameters unoptimizedCnecParameters = new UnoptimizedCnecParameters(null, flowCnecPstRangeActionMap); + UnoptimizedCnecParameters unoptimizedCnecParameters = new UnoptimizedCnecParameters(null, flowCnecRangeActionMap); FlowResult initialFlowResult = Mockito.mock(FlowResult.class); when(initialFlowResult.getMargin(classicCnec, Unit.MEGAWATT)).thenReturn(400.); when(initialFlowResult.getMargin(cnecInSeries, Unit.MEGAWATT)).thenReturn(600.);