From faa5366a45596eefda814837e4c8ba073df6a5f0 Mon Sep 17 00:00:00 2001 From: Peter Mitri Date: Thu, 10 Nov 2022 16:28:34 +0100 Subject: [PATCH 1/4] SearchTree: prioritize network action combinations --- .../search_tree_rao/search_tree/algorithms/SearchTree.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/SearchTree.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/SearchTree.java index c314ed3565..7be194964e 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/SearchTree.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/SearchTree.java @@ -314,7 +314,11 @@ private void updateOptimalLeafWithNextDepthBestLeaf(AbstractNetworkPool networkP private int arbitraryNetworkActionCombinationComparison(NetworkActionCombination ra1, NetworkActionCombination ra2) { if (ra1.isDetectedDuringRao() == ra2.isDetectedDuringRao()) { - return Hashing.crc32().hashString(ra1.getConcatenatedId(), StandardCharsets.UTF_8).hashCode() - Hashing.crc32().hashString(ra2.getConcatenatedId(), StandardCharsets.UTF_8).hashCode(); + if (ra1.getNetworkActionSet().size() == ra2.getNetworkActionSet().size()) { + return Hashing.crc32().hashString(ra1.getConcatenatedId(), StandardCharsets.UTF_8).hashCode() - Hashing.crc32().hashString(ra2.getConcatenatedId(), StandardCharsets.UTF_8).hashCode(); + } else { + return Integer.compare(ra2.getNetworkActionSet().size(), ra1.getNetworkActionSet().size()); + } } else if (ra1.isDetectedDuringRao()) { return -1; } else { From 6f3976198cca4f49dc98be006c2208c3ca370bb2 Mon Sep 17 00:00:00 2001 From: Peter Mitri Date: Wed, 7 Dec 2022 14:57:39 +0100 Subject: [PATCH 2/4] Fix OnFlowConstraintImpl & OnAngleConstraintImpl's usage methods --- .../data/crac_impl/OnAngleConstraintImpl.java | 6 ++++- .../data/crac_impl/OnFlowConstraintImpl.java | 6 ++++- .../crac_impl/OnAngleConstraintImplTest.java | 22 +++++++++++++++++++ .../crac_impl/OnFlowConstraintImplTest.java | 20 +++++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintImpl.java b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintImpl.java index a400055786..dcc6121bbc 100644 --- a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintImpl.java +++ b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintImpl.java @@ -37,7 +37,11 @@ public Instant getInstant() { @Override public UsageMethod getUsageMethod(State state) { - return state.getInstant().equals(instant) ? UsageMethod.TO_BE_EVALUATED : UsageMethod.UNDEFINED; + if (state.isPreventive()) { + return state.getInstant().equals(instant) ? UsageMethod.TO_BE_EVALUATED : UsageMethod.UNDEFINED; + } else { + return state.getInstant().equals(instant) && state.equals(this.angleCnec.getState()) ? UsageMethod.TO_BE_EVALUATED : UsageMethod.UNDEFINED; + } } @Override diff --git a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnFlowConstraintImpl.java b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnFlowConstraintImpl.java index 7869f6cc6a..bea8c9f687 100644 --- a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnFlowConstraintImpl.java +++ b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnFlowConstraintImpl.java @@ -37,7 +37,11 @@ public Instant getInstant() { @Override public UsageMethod getUsageMethod(State state) { - return state.getInstant().equals(instant) ? UsageMethod.TO_BE_EVALUATED : UsageMethod.UNDEFINED; + if (state.isPreventive()) { + return state.getInstant().equals(instant) ? UsageMethod.TO_BE_EVALUATED : UsageMethod.UNDEFINED; + } else { + return state.getInstant().equals(instant) && state.equals(this.flowCnec.getState()) ? UsageMethod.TO_BE_EVALUATED : UsageMethod.UNDEFINED; + } } @Override diff --git a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintImplTest.java b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintImplTest.java index 3c54b690a4..6b1b704825 100644 --- a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintImplTest.java +++ b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintImplTest.java @@ -9,7 +9,9 @@ import com.farao_community.farao.data.crac_api.Instant; import com.farao_community.farao.data.crac_api.State; import com.farao_community.farao.data.crac_api.cnec.AngleCnec; +import com.farao_community.farao.data.crac_api.cnec.FlowCnec; import com.farao_community.farao.data.crac_api.usage_rule.OnAngleConstraint; +import com.farao_community.farao.data.crac_api.usage_rule.OnFlowConstraint; import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; import org.junit.Before; import org.junit.Test; @@ -30,8 +32,10 @@ public void setUp() { angleCnec = Mockito.mock(AngleCnec.class); preventiveState = Mockito.mock(State.class); Mockito.when(preventiveState.getInstant()).thenReturn(Instant.PREVENTIVE); + Mockito.when(preventiveState.isPreventive()).thenReturn(true); curativeState = Mockito.mock(State.class); Mockito.when(curativeState.getInstant()).thenReturn(Instant.CURATIVE); + Mockito.when(curativeState.isPreventive()).thenReturn(false); } @Test @@ -66,4 +70,22 @@ public void testEquals() { assertNotEquals(onAngleConstraint1, onAngleConstraint2); assertNotEquals(onAngleConstraint1.hashCode(), onAngleConstraint2.hashCode()); } + + @Test + public void testGetUsageMethod() { + State curativeState2 = Mockito.mock(State.class); + Mockito.when(curativeState2.getInstant()).thenReturn(Instant.CURATIVE); + Mockito.when(curativeState2.isPreventive()).thenReturn(false); + + OnAngleConstraint onAngleConstraint = new OnAngleConstraintImpl(Instant.PREVENTIVE, angleCnec); + assertEquals(UsageMethod.TO_BE_EVALUATED, onAngleConstraint.getUsageMethod(preventiveState)); + assertEquals(UsageMethod.UNDEFINED, onAngleConstraint.getUsageMethod(curativeState)); + assertEquals(UsageMethod.UNDEFINED, onAngleConstraint.getUsageMethod(curativeState2)); + + Mockito.when(angleCnec.getState()).thenReturn(curativeState); + onAngleConstraint = new OnAngleConstraintImpl(Instant.CURATIVE, angleCnec); + assertEquals(UsageMethod.UNDEFINED, onAngleConstraint.getUsageMethod(preventiveState)); + assertEquals(UsageMethod.TO_BE_EVALUATED, onAngleConstraint.getUsageMethod(curativeState)); + assertEquals(UsageMethod.UNDEFINED, onAngleConstraint.getUsageMethod(curativeState2)); + } } diff --git a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnFlowConstraintImplTest.java b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnFlowConstraintImplTest.java index f25ea652f4..4655f78424 100644 --- a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnFlowConstraintImplTest.java +++ b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnFlowConstraintImplTest.java @@ -30,8 +30,10 @@ public void setUp() { flowCnec = Mockito.mock(FlowCnec.class); preventiveState = Mockito.mock(State.class); Mockito.when(preventiveState.getInstant()).thenReturn(Instant.PREVENTIVE); + Mockito.when(preventiveState.isPreventive()).thenReturn(true); curativeState = Mockito.mock(State.class); Mockito.when(curativeState.getInstant()).thenReturn(Instant.CURATIVE); + Mockito.when(curativeState.isPreventive()).thenReturn(false); } @Test @@ -66,4 +68,22 @@ public void testEquals() { assertNotEquals(onFlowConstraint1, onFlowConstraint2); assertNotEquals(onFlowConstraint1.hashCode(), onFlowConstraint2.hashCode()); } + + @Test + public void testGetUsageMethod() { + State curativeState2 = Mockito.mock(State.class); + Mockito.when(curativeState2.getInstant()).thenReturn(Instant.CURATIVE); + Mockito.when(curativeState2.isPreventive()).thenReturn(false); + + OnFlowConstraint onFlowConstraint = new OnFlowConstraintImpl(Instant.PREVENTIVE, flowCnec); + assertEquals(UsageMethod.TO_BE_EVALUATED, onFlowConstraint.getUsageMethod(preventiveState)); + assertEquals(UsageMethod.UNDEFINED, onFlowConstraint.getUsageMethod(curativeState)); + assertEquals(UsageMethod.UNDEFINED, onFlowConstraint.getUsageMethod(curativeState2)); + + Mockito.when(flowCnec.getState()).thenReturn(curativeState); + onFlowConstraint = new OnFlowConstraintImpl(Instant.CURATIVE, flowCnec); + assertEquals(UsageMethod.UNDEFINED, onFlowConstraint.getUsageMethod(preventiveState)); + assertEquals(UsageMethod.TO_BE_EVALUATED, onFlowConstraint.getUsageMethod(curativeState)); + assertEquals(UsageMethod.UNDEFINED, onFlowConstraint.getUsageMethod(curativeState2)); + } } From 663bf6e1aab0a662119f90006b4668c1c7e0f43a Mon Sep 17 00:00:00 2001 From: Peter Mitri Date: Wed, 7 Dec 2022 15:00:28 +0100 Subject: [PATCH 3/4] checkstyle --- .../farao/data/crac_impl/OnAngleConstraintImplTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintImplTest.java b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintImplTest.java index 6b1b704825..c64feab7e2 100644 --- a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintImplTest.java +++ b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintImplTest.java @@ -9,9 +9,7 @@ import com.farao_community.farao.data.crac_api.Instant; import com.farao_community.farao.data.crac_api.State; import com.farao_community.farao.data.crac_api.cnec.AngleCnec; -import com.farao_community.farao.data.crac_api.cnec.FlowCnec; import com.farao_community.farao.data.crac_api.usage_rule.OnAngleConstraint; -import com.farao_community.farao.data.crac_api.usage_rule.OnFlowConstraint; import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; import org.junit.Before; import org.junit.Test; From 3528c51b20a97aa95c675a92fdd41d85ec6c96f7 Mon Sep 17 00:00:00 2001 From: Peter Mitri Date: Wed, 7 Dec 2022 16:09:01 +0100 Subject: [PATCH 4/4] fix unit tests --- .../castor/algorithm/CastorFullOptimizationTest.java | 4 ++-- .../farao/search_tree_rao/commons/RaoUtilTest.java | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorFullOptimizationTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorFullOptimizationTest.java index 076c8e64b3..126d40ea23 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorFullOptimizationTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorFullOptimizationTest.java @@ -374,10 +374,10 @@ public void testIsRangeActionAvailableInState() { assertFalse(CastorFullOptimization.isRangeActionAvailableInState(ra5, state1, crac)); assertTrue(CastorFullOptimization.isRangeActionAvailableInState(ra5, state2, crac)); - // ra6 is available in preventive and in state1 and in state2 + // ra6 is available in preventive and in state1 assertTrue(CastorFullOptimization.isRangeActionAvailableInState(ra6, crac.getPreventiveState(), crac)); assertTrue(CastorFullOptimization.isRangeActionAvailableInState(ra6, state1, crac)); - assertTrue(CastorFullOptimization.isRangeActionAvailableInState(ra6, state2, crac)); + assertFalse(CastorFullOptimization.isRangeActionAvailableInState(ra6, state2, crac)); } @Test 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 246a9ffa33..45d22642f1 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 @@ -187,8 +187,7 @@ public void testGetLargestCnecThreshold() { @Test public void testIsOnFlowConstraintAvailable() { - State optimizedState = Mockito.mock(State.class); - when(optimizedState.getInstant()).thenReturn(Instant.CURATIVE); + State optimizedState = crac.getState("Contingency FR1 FR3", Instant.CURATIVE); FlowCnec flowCnec = crac.getFlowCnec("cnec1stateCurativeContingency1"); FlowResult flowResult = mock(FlowResult.class); @@ -217,7 +216,7 @@ public void testIsOnFlowConstraintAvailable() { assertTrue(RaoUtil.isOnFlowConstraintAvailable(onFlowConstraint, optimizedState, flowResult)); assertTrue(RaoUtil.isRemedialActionAvailable(na2, optimizedState, flowResult, crac.getFlowCnecs(), network)); - when(optimizedState.getInstant()).thenReturn(Instant.PREVENTIVE); + optimizedState = crac.getPreventiveState(); assertFalse(RaoUtil.isRemedialActionAvailable(na1, optimizedState, flowResult, crac.getFlowCnecs(), network)); assertFalse(RaoUtil.isOnFlowConstraintAvailable(onFlowConstraint, optimizedState, flowResult)); assertFalse(RaoUtil.isRemedialActionAvailable(na2, optimizedState, flowResult, crac.getFlowCnecs(), network));