Skip to content

Commit

Permalink
Merge branch 'main' into fix/issue-when-open-branch-as-cnec-post-cont…
Browse files Browse the repository at this point in the history
…ingency
  • Loading branch information
Godelaine committed Feb 7, 2025
2 parents de34301 + fa751f9 commit 227d190
Show file tree
Hide file tree
Showing 5 changed files with 344 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ public static Pair<RangeAction<?>, State> getLastAvailableRangeActionOnSameNetwo
} else if (state.getInstant().isCurative()) {

// look if a preventive range action acts on the same network elements
State preventiveState = optimizationContext.getMainOptimizationState();
State previousUsageState = optimizationContext.getMainOptimizationState();

if (preventiveState.isPreventive()) {
Optional<RangeAction<?>> correspondingRa = optimizationContext.getRangeActionsPerState().get(preventiveState).stream()
if (previousUsageState.getInstant().comesBefore(state.getInstant())) {
Optional<RangeAction<?>> correspondingRa = optimizationContext.getRangeActionsPerState().get(previousUsageState).stream()
.filter(ra -> ra.getId().equals(rangeAction.getId()) || ra.getNetworkElements().equals(rangeAction.getNetworkElements()))
.findAny();

if (correspondingRa.isPresent()) {
return Pair.of(correspondingRa.get(), preventiveState);
return Pair.of(correspondingRa.get(), previousUsageState);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@

package com.powsybl.openrao.searchtreerao.commons;

import com.powsybl.contingency.Contingency;
import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.openrao.commons.Unit;
import com.powsybl.openrao.data.crac.api.Crac;
import com.powsybl.openrao.data.crac.api.Instant;
import com.powsybl.openrao.data.crac.api.InstantKind;
import com.powsybl.openrao.data.crac.api.NetworkElement;
import com.powsybl.openrao.data.crac.api.RemedialAction;
import com.powsybl.openrao.data.crac.api.State;
import com.powsybl.openrao.data.crac.api.cnec.FlowCnec;
import com.powsybl.iidm.network.TwoSides;
import com.powsybl.openrao.data.crac.api.networkaction.ActionType;
import com.powsybl.openrao.data.crac.api.networkaction.NetworkAction;
import com.powsybl.openrao.data.crac.api.rangeaction.RangeAction;
import com.powsybl.openrao.data.crac.api.usagerule.OnConstraint;
import com.powsybl.openrao.data.crac.api.usagerule.OnInstant;
import com.powsybl.openrao.data.crac.api.usagerule.UsageMethod;
Expand All @@ -25,16 +29,18 @@
import com.powsybl.openrao.raoapi.RaoInput;
import com.powsybl.openrao.raoapi.parameters.ObjectiveFunctionParameters;
import com.powsybl.openrao.raoapi.parameters.RaoParameters;
import com.powsybl.openrao.raoapi.parameters.RelativeMarginsParameters;
import com.powsybl.openrao.raoapi.parameters.extensions.OpenRaoSearchTreeParameters;
import com.powsybl.openrao.raoapi.parameters.extensions.SearchTreeRaoRangeActionsOptimizationParameters;
import com.powsybl.openrao.raoapi.parameters.RelativeMarginsParameters;
import com.powsybl.openrao.searchtreerao.commons.optimizationperimeters.OptimizationPerimeter;
import com.powsybl.openrao.searchtreerao.result.api.FlowResult;
import com.powsybl.openrao.searchtreerao.result.api.PrePerimeterResult;
import com.powsybl.glsk.commons.ZonalData;
import com.powsybl.glsk.ucte.UcteGlskDocument;
import com.powsybl.iidm.network.Country;
import com.powsybl.iidm.network.Network;
import com.powsybl.sensitivity.SensitivityVariableSet;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -357,4 +363,54 @@ void testElementaryActionsLimitWithNonDiscretePsts() {
OpenRaoException exception = assertThrows(OpenRaoException.class, () -> RaoUtil.checkParameters(raoParameters, raoInput));
assertEquals("The PSTs must be approximated as integers to use the limitations of elementary actions as a constraint in the RAO.", exception.getMessage());
}

@Test
void testGetLastAvailableRangeActionOnSameNetworkElementWithOutageState() {
OptimizationPerimeter optimizationContext = Mockito.mock(OptimizationPerimeter.class);
Mockito.when(optimizationContext.getMainOptimizationState()).thenReturn(crac.getPreventiveState());
State outageState = Mockito.mock(State.class);
Mockito.when(outageState.getContingency()).thenReturn(Optional.of(crac.getContingency("Contingency FR1 FR3")));
Mockito.when(outageState.getInstant()).thenReturn(crac.getInstant(InstantKind.OUTAGE));
OpenRaoException exception = assertThrows(OpenRaoException.class, () -> RaoUtil.getLastAvailableRangeActionOnSameNetworkElement(optimizationContext, crac.getRangeActions().iterator().next(), outageState));
assertEquals("Linear optimization does not handle range actions which are neither PREVENTIVE nor CURATIVE.", exception.getMessage());
}

@Test
void testGetLastAvailableRangeActionOnSameNetworkElementMultiCurative() {
Contingency contingency = crac.getContingency("Contingency FR1 FR3");

Instant curative1Instant = Mockito.mock(Instant.class);
Mockito.when(curative1Instant.getKind()).thenReturn(InstantKind.CURATIVE);

State curativeState1 = Mockito.mock(State.class);
Mockito.when(curativeState1.getInstant()).thenReturn(curative1Instant);
Mockito.when(curativeState1.getContingency()).thenReturn(Optional.of(contingency));

Instant curative3Instant = Mockito.mock(Instant.class);
Mockito.when(curative3Instant.getKind()).thenReturn(InstantKind.CURATIVE);
Mockito.when(curative3Instant.isCurative()).thenReturn(true);

State curativeState3 = Mockito.mock(State.class);
Mockito.when(curativeState3.getInstant()).thenReturn(curative3Instant);
Mockito.when(curativeState3.getContingency()).thenReturn(Optional.of(contingency));

Mockito.when(curative1Instant.comesBefore(curative3Instant)).thenReturn(true);

NetworkElement pst = Mockito.mock(NetworkElement.class);
Mockito.when(pst.getId()).thenReturn("pst");

RangeAction<?> rangeAction1 = Mockito.mock(RangeAction.class);
Mockito.when(rangeAction1.getNetworkElements()).thenReturn(Set.of(pst));
Mockito.when(rangeAction1.getId()).thenReturn("range-action-1");

RangeAction<?> rangeAction2 = Mockito.mock(RangeAction.class);
Mockito.when(rangeAction2.getNetworkElements()).thenReturn(Set.of(pst));
Mockito.when(rangeAction2.getId()).thenReturn("range-action-2");

OptimizationPerimeter optimizationContext = Mockito.mock(OptimizationPerimeter.class);
Mockito.when(optimizationContext.getMainOptimizationState()).thenReturn(curativeState1);
Mockito.when(optimizationContext.getRangeActionsPerState()).thenReturn(Map.of(curativeState1, Set.of(rangeAction1)));

assertEquals(Pair.of(rangeAction1, curativeState1), RaoUtil.getLastAvailableRangeActionOnSameNetworkElement(optimizationContext, rangeAction2, curativeState3));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public void objectiveFunctionValueAfterAraShouldBe(double expectedValue) {

@Then("the value of the objective function after CRA should be {double}")
public void objectiveFunctionValueAfterCraShouldBe(double expectedValue) {
assertEquals(expectedValue, raoResult.getCost(crac.getInstant(InstantKind.CURATIVE)), flowAmpereTolerance(expectedValue));
assertEquals(expectedValue, raoResult.getCost(crac.getLastInstant()), flowAmpereTolerance(expectedValue));
}

@Then("the value of the objective function before optimisation should be {double}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,51 @@ Feature: US 92.2: Costly range actions optimization - APPROXIMATED_INTEGERS PSTs
# Activation of pstBeFr3 (20) + 9 taps moved (9 * 7.5)
And the value of the objective function after PRA should be 87.5
And 0 remedial actions are used after "coBeFr2" at "curative"
And the value of the objective function after CRA should be 87.5
And the value of the objective function after CRA should be 87.5

@fast @costly @rao
Scenario: US 92.2.6: Multi-curative costly optimization
The same PST is moved in preventive optimization and at all curative states
Given network file is "epic92/2Nodes3ParallelLinesPST.uct"
Given crac file is "epic92/crac-92-2-6.json"
Given configuration file is "epic92/RaoParameters_dc_minObjective_discretePst.json"
When I launch search_tree_rao
Then the worst margin is 32.13 MW
And the value of the objective function initially should be 4500000.0
And 1 remedial actions are used in preventive
And the remedial action "pstBeFr3" is used in preventive
And the tap of PstRangeAction "pstBeFr3" should be -2 in preventive
# Activation of pstBeFr3 (20) + 2 taps moved (2 * 5.0)
And the value of the objective function after PRA should be 3518125.13
And 1 remedial actions are used after "coBeFr2" at "curative-1"
And the remedial action "pstBeFr3" is used after "coBeFr2" at "curative-1"
And the tap of PstRangeAction "pstBeFr3" should be -7 after "coBeFr2" at "curative-1"
And 1 remedial actions are used after "coBeFr2" at "curative-2"
And the remedial action "pstBeFr3" is used after "coBeFr2" at "curative-2"
And the tap of PstRangeAction "pstBeFr3" should be -9 after "coBeFr2" at "curative-2"
And 1 remedial actions are used after "coBeFr2" at "curative-3"
And the remedial action "pstBeFr3" is used after "coBeFr2" at "curative-3"
And the tap of PstRangeAction "pstBeFr3" should be -10 after "coBeFr2" at "curative-3"
# Activation of pstBeFr3 4 times (4 * 20) + 10 taps moved (10 * 5.0)
And the value of the objective function after CRA should be 130.0

@fast @costly @rao @second-preventive
Scenario: US 92.2.6: Multi-curative costly optimization with 2P
Same case as US 92.2.6 but with second preventive optimization.
The PST is moved to tap -10 straight from preventive optimization to cut activation expenses.
Given network file is "epic92/2Nodes3ParallelLinesPST.uct"
Given crac file is "epic92/crac-92-2-6.json"
Given configuration file is "epic92/RaoParameters_dc_minObjective_discretePst_2P.json"
When I launch search_tree_rao
Then the worst margin is 40.77 MW
And the value of the objective function initially should be 4500000.0
And 1 remedial actions are used in preventive
And the remedial action "pstBeFr3" is used in preventive
And the tap of PstRangeAction "pstBeFr3" should be -10 in preventive
# Activation of pstBeFr3 (20) + 10 taps moved (10 * 5.0)
And the value of the objective function after PRA should be 70.0
And 0 remedial actions are used after "coBeFr2" at "curative-1"
And 0 remedial actions are used after "coBeFr2" at "curative-2"
And 0 remedial actions are used after "coBeFr2" at "curative-3"
# Activation of pstBeFr3 (20) + 10 taps moved (10 * 5.0)
And the value of the objective function after CRA should be 70.0
Loading

0 comments on commit 227d190

Please sign in to comment.