From cef67a7827b31d9a0e3ab0f03374ac3890f1978f Mon Sep 17 00:00:00 2001 From: Peter Mitri Date: Tue, 10 Jan 2023 17:26:13 +0100 Subject: [PATCH 1/6] Allow running RAO on an auto state only --- .../castor/algorithm/CastorOneStateOnly.java | 70 +++++++++++-------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorOneStateOnly.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorOneStateOnly.java index c556ab048b..b8580735d3 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorOneStateOnly.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorOneStateOnly.java @@ -6,6 +6,9 @@ */ package com.farao_community.farao.search_tree_rao.castor.algorithm; +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.FlowCnec; import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; import com.farao_community.farao.data.rao_result_api.RaoResult; import com.farao_community.farao.rao_api.RaoInput; @@ -80,37 +83,46 @@ public CompletableFuture run() { OptimizationPerimeter optPerimeter; TreeParameters treeParameters; Set operatorsNotToOptimize = new HashSet<>(); - if (raoInput.getOptimizedState().equals(raoInput.getCrac().getPreventiveState())) { - optPerimeter = PreventiveOptimizationPerimeter.buildWithPreventiveCnecsOnly(raoInput.getCrac(), raoInput.getNetwork(), raoParameters, initialResults); - treeParameters = TreeParameters.buildForPreventivePerimeter(raoParameters.getExtension(SearchTreeRaoParameters.class)); - } else { - optPerimeter = CurativeOptimizationPerimeter.build(raoInput.getOptimizedState(), raoInput.getCrac(), raoInput.getNetwork(), raoParameters, initialResults); - treeParameters = TreeParameters.buildForCurativePerimeter(raoParameters.getExtension(SearchTreeRaoParameters.class), -Double.MAX_VALUE); - operatorsNotToOptimize.addAll(stateTree.getOperatorsNotSharingCras()); - } - - SearchTreeParameters searchTreeParameters = SearchTreeParameters.create() - .withConstantParametersOverAllRao(raoParameters, raoInput.getCrac()) - .withTreeParameters(treeParameters) - .withUnoptimizedCnecParameters(UnoptimizedCnecParameters.build(raoParameters, stateTree.getOperatorsNotSharingCras(), raoInput.getCrac())) - .build(); - SearchTreeInput searchTreeInput = SearchTreeInput.create() - .withNetwork(raoInput.getNetwork()) - .withOptimizationPerimeter(optPerimeter) - .withInitialFlowResult(initialResults) - .withPrePerimeterResult(initialResults) - .withPreOptimizationAppliedNetworkActions(new AppliedRemedialActions()) //no remedial Action applied - .withObjectiveFunction(ObjectiveFunction.create().build(optPerimeter.getFlowCnecs(), optPerimeter.getLoopFlowCnecs(), initialResults, initialResults, initialResults, raoInput.getCrac(), operatorsNotToOptimize, raoParameters)) - .withToolProvider(toolProvider) - .build(); + OptimizationResult optimizationResult; + Set perimeterFlowCnecs; - OptimizationResult optimizationResult = new SearchTree(searchTreeInput, searchTreeParameters, true).run().join(); - - // apply RAs and return results - optimizationResult.getRangeActions().forEach(rangeAction -> rangeAction.apply(raoInput.getNetwork(), optimizationResult.getOptimizedSetpoint(rangeAction, raoInput.getOptimizedState()))); - optimizationResult.getActivatedNetworkActions().forEach(networkAction -> networkAction.apply(raoInput.getNetwork())); + if (raoInput.getOptimizedState().getInstant().equals(Instant.AUTO)) { + perimeterFlowCnecs = raoInput.getCrac().getFlowCnecs(raoInput.getOptimizedState()); + State curativeState = raoInput.getCrac().getState(raoInput.getOptimizedState().getContingency().orElseThrow().getId(), Instant.CURATIVE); + AutomatonSimulator automatonSimulator = new AutomatonSimulator(raoInput.getCrac(), raoParameters, toolProvider, initialResults, initialResults, initialResults, stateTree.getOperatorsNotSharingCras(), 2); + optimizationResult = automatonSimulator.simulateAutomatonState(raoInput.getOptimizedState(), curativeState, raoInput.getNetwork()); + } else { + if (raoInput.getOptimizedState().equals(raoInput.getCrac().getPreventiveState())) { + optPerimeter = PreventiveOptimizationPerimeter.buildWithPreventiveCnecsOnly(raoInput.getCrac(), raoInput.getNetwork(), raoParameters, initialResults); + treeParameters = TreeParameters.buildForPreventivePerimeter(raoParameters.getExtension(SearchTreeRaoParameters.class)); + } else { + optPerimeter = CurativeOptimizationPerimeter.build(raoInput.getOptimizedState(), raoInput.getCrac(), raoInput.getNetwork(), raoParameters, initialResults); + treeParameters = TreeParameters.buildForCurativePerimeter(raoParameters.getExtension(SearchTreeRaoParameters.class), -Double.MAX_VALUE); + operatorsNotToOptimize.addAll(stateTree.getOperatorsNotSharingCras()); + } + perimeterFlowCnecs = optPerimeter.getFlowCnecs(); + SearchTreeParameters searchTreeParameters = SearchTreeParameters.create() + .withConstantParametersOverAllRao(raoParameters, raoInput.getCrac()) + .withTreeParameters(treeParameters) + .withUnoptimizedCnecParameters(UnoptimizedCnecParameters.build(raoParameters, stateTree.getOperatorsNotSharingCras(), raoInput.getCrac())) + .build(); + SearchTreeInput searchTreeInput = SearchTreeInput.create() + .withNetwork(raoInput.getNetwork()) + .withOptimizationPerimeter(optPerimeter) + .withInitialFlowResult(initialResults) + .withPrePerimeterResult(initialResults) + .withPreOptimizationAppliedNetworkActions(new AppliedRemedialActions()) //no remedial Action applied + .withObjectiveFunction(ObjectiveFunction.create().build(optPerimeter.getFlowCnecs(), optPerimeter.getLoopFlowCnecs(), initialResults, initialResults, initialResults, raoInput.getCrac(), operatorsNotToOptimize, raoParameters)) + .withToolProvider(toolProvider) + .build(); + optimizationResult = new SearchTree(searchTreeInput, searchTreeParameters, true).run().join(); + + // apply RAs and return results + optimizationResult.getRangeActions().forEach(rangeAction -> rangeAction.apply(raoInput.getNetwork(), optimizationResult.getOptimizedSetpoint(rangeAction, raoInput.getOptimizedState()))); + optimizationResult.getActivatedNetworkActions().forEach(networkAction -> networkAction.apply(raoInput.getNetwork())); + } - return CompletableFuture.completedFuture(new OneStateOnlyRaoResultImpl(raoInput.getOptimizedState(), initialResults, optimizationResult, optPerimeter.getFlowCnecs())); + return CompletableFuture.completedFuture(new OneStateOnlyRaoResultImpl(raoInput.getOptimizedState(), initialResults, optimizationResult, perimeterFlowCnecs)); } } From 2068c6d2aea162b38297d1641d9fa0601cfc8fb3 Mon Sep 17 00:00:00 2001 From: Peter Mitri Date: Wed, 11 Jan 2023 14:28:30 +0100 Subject: [PATCH 2/6] Automaton simulation fixes --- .../castor/algorithm/AutomatonSimulator.java | 36 +++++++++++-------- .../algorithm/AutomatonSimulatorTest.java | 18 +++++----- 2 files changed, 30 insertions(+), 24 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 17d788378d..b993f3941a 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 @@ -84,7 +84,7 @@ AutomatonPerimeterResultImpl simulateAutomatonState(State automatonState, State BUSINESS_WARNS.warn("CRAC has network action automatons with usage method AVAILABLE. These are not supported."); } TECHNICAL_LOGS.info("Initial situation:"); - RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, prePerimeterSensitivityOutput, Set.of(automatonState, curativeState), raoParameters.getObjectiveFunction(), numberLoggedElementsDuringRao); + RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, prePerimeterSensitivityOutput, Set.of(automatonState), raoParameters.getObjectiveFunction(), numberLoggedElementsDuringRao); PrePerimeterSensitivityAnalysis preAutoPerimeterSensitivityAnalysis = getPreAutoPerimeterSensitivityAnalysis(automatonState, curativeState); @@ -169,7 +169,7 @@ TopoAutomatonSimulationResult simulateTopologicalAutomatons(State automatonState if (!appliedNetworkActions.isEmpty()) { TECHNICAL_LOGS.info("Running sensi post application of auto network actions for automaton state {}.", automatonState.getId()); automatonRangeActionOptimizationSensitivityAnalysisOutput = preAutoPerimeterSensitivityAnalysis.runBasedOnInitialResults(network, crac, initialFlowResult, prePerimeterRangeActionSetpointResult, operatorsNotSharingCras, null); - RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, automatonRangeActionOptimizationSensitivityAnalysisOutput, raoParameters.getObjectiveFunction(), numberLoggedElementsDuringRao); + RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, automatonRangeActionOptimizationSensitivityAnalysisOutput, Set.of(automatonState), raoParameters.getObjectiveFunction(), numberLoggedElementsDuringRao); } return new TopoAutomatonSimulationResult(automatonRangeActionOptimizationSensitivityAnalysisOutput, appliedNetworkActions); @@ -202,8 +202,8 @@ Map, Double> getRangeActionsWithSetpoint() { } } - RangeAutomatonSimulationResult simulateRangeAutomatons(State automatonState, State curativeState, Network network, PrePerimeterSensitivityAnalysis preAutoPerimeterSensitivityAnalysis, PrePerimeterResult postAutoResult) { - PrePerimeterResult finalPostAutoResult = postAutoResult; + RangeAutomatonSimulationResult simulateRangeAutomatons(State automatonState, State curativeState, Network network, PrePerimeterSensitivityAnalysis preAutoPerimeterSensitivityAnalysis, PrePerimeterResult postAutoTopoResult) { + PrePerimeterResult finalPostAutoResult = postAutoTopoResult; // -- Create groups of aligned range actions List>> rangeActionsOnAutomatonState = buildRangeActionsGroupsOrderedBySpeed(finalPostAutoResult, automatonState, network); // -- Build AutomatonPerimeterResultImpl objects @@ -219,10 +219,6 @@ RangeAutomatonSimulationResult simulateRangeAutomatons(State automatonState, Sta // -- Optimize range-action automatons for (List> alignedRa : rangeActionsOnAutomatonState) { RangeAction availableRa = alignedRa.get(0); - // Disable AC emulation for HVDC lines - if (alignedRa.stream().allMatch(HvdcRangeAction.class::isInstance)) { - finalPostAutoResult = disableACEmulation(alignedRa, network, preAutoPerimeterSensitivityAnalysis, finalPostAutoResult); - } // Define flowCnecs depending on UsageMethod Set flowCnecs = gatherFlowCnecsForAutoRangeAction(availableRa, automatonState, network); // Shift @@ -231,7 +227,8 @@ RangeAutomatonSimulationResult simulateRangeAutomatons(State automatonState, Sta flowCnecs, network, preAutoPerimeterSensitivityAnalysis, - finalPostAutoResult); + finalPostAutoResult, + automatonState); finalPostAutoResult = postShiftResult.getLeft(); activatedRangeActions.addAll(postShiftResult.getRight().keySet()); rangeActionsWithSetpoint.putAll(postShiftResult.getRight()); @@ -381,7 +378,7 @@ private PrePerimeterResult runPreCurativeSensitivityComputation(State automatonS // Run computation TECHNICAL_LOGS.info("Running pre curative sensi after auto state {}.", automatonState.getId()); PrePerimeterResult postAutomatonSensitivityAnalysisOutput = prePerimeterSensitivityAnalysis.runBasedOnInitialResults(network, crac, initialFlowResult, prePerimeterRangeActionSetpointResult, operatorsNotSharingCras, null); - RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, postAutomatonSensitivityAnalysisOutput, raoParameters.getObjectiveFunction(), numberLoggedElementsDuringRao); + RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, postAutomatonSensitivityAnalysisOutput, Set.of(automatonState), raoParameters.getObjectiveFunction(), numberLoggedElementsDuringRao); return postAutomatonSensitivityAnalysisOutput; } @@ -392,7 +389,8 @@ private PrePerimeterResult runPreCurativeSensitivityComputation(State automatonS PrePerimeterResult disableACEmulation(List> alignedRa, Network network, PrePerimeterSensitivityAnalysis preAutoPerimeterSensitivityAnalysis, - PrePerimeterResult prePerimeterSensitivityOutput) { + PrePerimeterResult prePerimeterSensitivityOutput, + State automatonState) { boolean runSensi = false; for (RangeAction alignedAvailableRa : alignedRa) { if (alignedAvailableRa instanceof HvdcRangeAction) { @@ -408,7 +406,7 @@ PrePerimeterResult disableACEmulation(List> alignedRa, if (runSensi) { TECHNICAL_LOGS.info("Running sensi after disabling AC emulation."); PrePerimeterResult result = preAutoPerimeterSensitivityAnalysis.runBasedOnInitialResults(network, crac, initialFlowResult, prePerimeterRangeActionSetpointResult, operatorsNotSharingCras, null); - RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, result, raoParameters.getObjectiveFunction(), numberLoggedElementsDuringRao); + RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, result, Set.of(automatonState), raoParameters.getObjectiveFunction(), numberLoggedElementsDuringRao); return result; } else { return prePerimeterSensitivityOutput; @@ -429,12 +427,20 @@ Pair, Double>> shiftRangeActionsUntilFlow Set flowCnecs, Network network, PrePerimeterSensitivityAnalysis preAutoPerimeterSensitivityAnalysis, - PrePerimeterResult prePerimeterSensitivityOutput) { + PrePerimeterResult prePerimeterSensitivityOutput, + State automatonState) { + Set> flowCnecsToBeExcluded = new HashSet<>(); PrePerimeterResult automatonRangeActionOptimizationSensitivityAnalysisOutput = prePerimeterSensitivityOutput; Map, Double> activatedRangeActionsWithSetpoint = new HashMap<>(); List> flowCnecsWithNegativeMargin = getCnecsWithNegativeMarginWithoutExcludedCnecs(flowCnecs, flowCnecsToBeExcluded, automatonRangeActionOptimizationSensitivityAnalysisOutput); + if (alignedRangeActions.stream().allMatch(HvdcRangeAction.class::isInstance) && !flowCnecsWithNegativeMargin.isEmpty()) { + // Disable AC emulation for HVDC lines, re-run sensitivity analysis and fetch new negative margins + automatonRangeActionOptimizationSensitivityAnalysisOutput = disableACEmulation(alignedRangeActions, network, preAutoPerimeterSensitivityAnalysis, automatonRangeActionOptimizationSensitivityAnalysisOutput, automatonState); + flowCnecsWithNegativeMargin = getCnecsWithNegativeMarginWithoutExcludedCnecs(flowCnecs, flowCnecsToBeExcluded, automatonRangeActionOptimizationSensitivityAnalysisOutput); + } + // -- Define setpoint bounds // Aligned range actions have the same setpoint : double initialSetpoint = alignedRangeActions.get(0).getCurrentSetpoint(network); @@ -484,9 +490,9 @@ Pair, Double>> shiftRangeActionsUntilFlow rangeAction.apply(network, optimalSetpoint); activatedRangeActionsWithSetpoint.put(rangeAction, optimalSetpoint); } - TECHNICAL_LOGS.debug("Shifting setpoint from {} to {} on range action(s) {} to improve margin on cnec {} on side {}} (initial margin : {} MW).", initialSetpoint, optimalSetpoint, alignedRangeActions.stream().map(Identifiable::getId).collect(Collectors.joining(" ,")), toBeShiftedCnec.getId(), side, initialMargin); + TECHNICAL_LOGS.debug("Shifting setpoint from {} to {} on range action(s) {} to improve margin on cnec {} on side {}} (initial margin : {} MW).", initialSetpoint, optimalSetpoint, alignedRangeActions.stream().map(Identifiable::getId).collect(Collectors.joining(", ")), toBeShiftedCnec.getId(), side, initialMargin); automatonRangeActionOptimizationSensitivityAnalysisOutput = preAutoPerimeterSensitivityAnalysis.runBasedOnInitialResults(network, crac, initialFlowResult, prePerimeterRangeActionSetpointResult, operatorsNotSharingCras, null); - RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, automatonRangeActionOptimizationSensitivityAnalysisOutput, raoParameters.getObjectiveFunction(), numberLoggedElementsDuringRao); + RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, automatonRangeActionOptimizationSensitivityAnalysisOutput, Set.of(automatonState), raoParameters.getObjectiveFunction(), numberLoggedElementsDuringRao); flowCnecsWithNegativeMargin = getCnecsWithNegativeMarginWithoutExcludedCnecs(flowCnecs, flowCnecsToBeExcluded, automatonRangeActionOptimizationSensitivityAnalysisOutput); iteration++; 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 b891d2e923..d78fbdbb37 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 @@ -250,7 +250,7 @@ public void testDisableAcEmulation() { PrePerimeterResult prePerimeterResult = mock(PrePerimeterResult.class); when(mockedPreAutoPerimeterSensitivityAnalysis.runBasedOnInitialResults(any(), any(), any(), any(), any(), any())).thenReturn(mockedPrePerimeterResult); - PrePerimeterResult result = automatonSimulator.disableACEmulation(List.of(hvdcRa), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult); + PrePerimeterResult result = automatonSimulator.disableACEmulation(List.of(hvdcRa), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult, autoState); // check that AC emulation was disabled on HVDC assertFalse(network.getHvdcLine("BBE2AA11 FFR3AA11 1").getExtension(HvdcAngleDroopActivePowerControl.class).isEnabled()); // check that other HVDC was not touched @@ -259,24 +259,24 @@ public void testDisableAcEmulation() { assertEquals(mockedPrePerimeterResult, result); // run a second time => no influence + sensi not run - result = automatonSimulator.disableACEmulation(List.of(hvdcRa), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult); + result = automatonSimulator.disableACEmulation(List.of(hvdcRa), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult, autoState); assertFalse(network.getHvdcLine("BBE2AA11 FFR3AA11 1").getExtension(HvdcAngleDroopActivePowerControl.class).isEnabled()); assertTrue(network.getHvdcLine("BBE2AA12 FFR3AA12 1").getExtension(HvdcAngleDroopActivePowerControl.class).isEnabled()); assertEquals(prePerimeterResult, result); // Test on 2 aligned HVDC RAs - result = automatonSimulator.disableACEmulation(List.of(hvdcRa, hvdcRa2), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult); + result = automatonSimulator.disableACEmulation(List.of(hvdcRa, hvdcRa2), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult, autoState); assertFalse(network.getHvdcLine("BBE2AA11 FFR3AA11 1").getExtension(HvdcAngleDroopActivePowerControl.class).isEnabled()); assertFalse(network.getHvdcLine("BBE2AA12 FFR3AA12 1").getExtension(HvdcAngleDroopActivePowerControl.class).isEnabled()); assertEquals(mockedPrePerimeterResult, result); // Test on an HVDC with no HvdcAngleDroopActivePowerControl network.getHvdcLine("BBE2AA11 FFR3AA11 1").removeExtension(HvdcAngleDroopActivePowerControl.class); - result = automatonSimulator.disableACEmulation(List.of(hvdcRa), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult); + result = automatonSimulator.disableACEmulation(List.of(hvdcRa), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult, autoState); assertEquals(prePerimeterResult, result); // Test on non-HVDC : nothing should happen - result = automatonSimulator.disableACEmulation(List.of(ra2), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult); + result = automatonSimulator.disableACEmulation(List.of(ra2), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult, autoState); assertEquals(prePerimeterResult, result); } @@ -338,7 +338,7 @@ public void testShiftRangeActionsUntilFlowCnecsSecureCase1() { // so PSTs should be shifted to setpoint +1.1 on first iteration, then +2.1 on second Pair, Double>> shiftResult = - automatonSimulator.shiftRangeActionsUntilFlowCnecsSecure(List.of(ara1, ara2), Set.of(cnec), network, mockedPreAutoPerimeterSensitivityAnalysis, mockedPrePerimeterResult); + automatonSimulator.shiftRangeActionsUntilFlowCnecsSecure(List.of(ara1, ara2), Set.of(cnec), network, mockedPreAutoPerimeterSensitivityAnalysis, mockedPrePerimeterResult, autoState); assertEquals(2.1, shiftResult.getRight().get(ara1), DOUBLE_TOLERANCE); assertEquals(2.1, shiftResult.getRight().get(ara2), DOUBLE_TOLERANCE); } @@ -365,7 +365,7 @@ public void testShiftRangeActionsUntilFlowCnecsSecureCase2() { when(mockedPrePerimeterResult.getSensitivityValue(cnec, Side.RIGHT, ara2, Unit.MEGAWATT)).thenReturn(-50., -5.); Pair, Double>> shiftResult = - automatonSimulator.shiftRangeActionsUntilFlowCnecsSecure(List.of(ara1, ara2), Set.of(cnec), network, mockedPreAutoPerimeterSensitivityAnalysis, mockedPrePerimeterResult); + automatonSimulator.shiftRangeActionsUntilFlowCnecsSecure(List.of(ara1, ara2), Set.of(cnec), network, mockedPreAutoPerimeterSensitivityAnalysis, mockedPrePerimeterResult, autoState); assertEquals(2.1, shiftResult.getRight().get(ara1), DOUBLE_TOLERANCE); assertEquals(2.1, shiftResult.getRight().get(ara2), DOUBLE_TOLERANCE); } @@ -388,7 +388,7 @@ public void testShiftRangeActionsUntilFlowCnecsSecureCase3() { when(mockedPrePerimeterResult.getSensitivityValue(cnec, Side.LEFT, ara2, Unit.MEGAWATT)).thenReturn(50., 5.); Pair, Double>> shiftResult = - automatonSimulator.shiftRangeActionsUntilFlowCnecsSecure(List.of(ara1, ara2), Set.of(cnec), network, mockedPreAutoPerimeterSensitivityAnalysis, mockedPrePerimeterResult); + automatonSimulator.shiftRangeActionsUntilFlowCnecsSecure(List.of(ara1, ara2), Set.of(cnec), network, mockedPreAutoPerimeterSensitivityAnalysis, mockedPrePerimeterResult, autoState); assertEquals(-2.1, shiftResult.getRight().get(ara1), DOUBLE_TOLERANCE); assertEquals(-2.1, shiftResult.getRight().get(ara2), DOUBLE_TOLERANCE); } @@ -421,7 +421,7 @@ public void testShiftRangeActionsUntilFlowCnecsSecureCase4() { when(mockedPrePerimeterResult.getSensitivityValue(cnec2, Side.RIGHT, ara2, Unit.MEGAWATT)).thenReturn(0.); Pair, Double>> shiftResult = - automatonSimulator.shiftRangeActionsUntilFlowCnecsSecure(List.of(ara1, ara2), Set.of(cnec, cnec2), network, mockedPreAutoPerimeterSensitivityAnalysis, mockedPrePerimeterResult); + automatonSimulator.shiftRangeActionsUntilFlowCnecsSecure(List.of(ara1, ara2), Set.of(cnec, cnec2), network, mockedPreAutoPerimeterSensitivityAnalysis, mockedPrePerimeterResult, autoState); assertEquals(-2.1, shiftResult.getRight().get(ara1), DOUBLE_TOLERANCE); assertEquals(-2.1, shiftResult.getRight().get(ara2), DOUBLE_TOLERANCE); } From 7ab5dba8c14c33ca13a2b527f27ee0dad57b20fb Mon Sep 17 00:00:00 2001 From: Peter Mitri Date: Thu, 12 Jan 2023 11:00:01 +0100 Subject: [PATCH 3/6] rollback master --- .../castor/algorithm/CastorOneStateOnly.java | 78 ++++++++----------- 1 file changed, 33 insertions(+), 45 deletions(-) diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorOneStateOnly.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorOneStateOnly.java index b8580735d3..7550035ccb 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorOneStateOnly.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorOneStateOnly.java @@ -6,9 +6,6 @@ */ package com.farao_community.farao.search_tree_rao.castor.algorithm; -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.FlowCnec; import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; import com.farao_community.farao.data.rao_result_api.RaoResult; import com.farao_community.farao.rao_api.RaoInput; @@ -66,10 +63,10 @@ public CompletableFuture run() { // compute initial sensitivity on CNECs of the only optimized state PrePerimeterSensitivityAnalysis prePerimeterSensitivityAnalysis = new PrePerimeterSensitivityAnalysis( - raoInput.getCrac().getFlowCnecs(raoInput.getOptimizedState()), - raoInput.getCrac().getRangeActions(raoInput.getOptimizedState(), UsageMethod.AVAILABLE, UsageMethod.TO_BE_EVALUATED), - raoParameters, - toolProvider); + raoInput.getCrac().getFlowCnecs(raoInput.getOptimizedState()), + raoInput.getCrac().getRangeActions(raoInput.getOptimizedState(), UsageMethod.AVAILABLE, UsageMethod.TO_BE_EVALUATED), + raoParameters, + toolProvider); PrePerimeterResult initialResults; try { @@ -83,46 +80,37 @@ public CompletableFuture run() { OptimizationPerimeter optPerimeter; TreeParameters treeParameters; Set operatorsNotToOptimize = new HashSet<>(); - - OptimizationResult optimizationResult; - Set perimeterFlowCnecs; - - if (raoInput.getOptimizedState().getInstant().equals(Instant.AUTO)) { - perimeterFlowCnecs = raoInput.getCrac().getFlowCnecs(raoInput.getOptimizedState()); - State curativeState = raoInput.getCrac().getState(raoInput.getOptimizedState().getContingency().orElseThrow().getId(), Instant.CURATIVE); - AutomatonSimulator automatonSimulator = new AutomatonSimulator(raoInput.getCrac(), raoParameters, toolProvider, initialResults, initialResults, initialResults, stateTree.getOperatorsNotSharingCras(), 2); - optimizationResult = automatonSimulator.simulateAutomatonState(raoInput.getOptimizedState(), curativeState, raoInput.getNetwork()); + if (raoInput.getOptimizedState().equals(raoInput.getCrac().getPreventiveState())) { + optPerimeter = PreventiveOptimizationPerimeter.buildWithPreventiveCnecsOnly(raoInput.getCrac(), raoInput.getNetwork(), raoParameters, initialResults); + treeParameters = TreeParameters.buildForPreventivePerimeter(raoParameters.getExtension(SearchTreeRaoParameters.class)); } else { - if (raoInput.getOptimizedState().equals(raoInput.getCrac().getPreventiveState())) { - optPerimeter = PreventiveOptimizationPerimeter.buildWithPreventiveCnecsOnly(raoInput.getCrac(), raoInput.getNetwork(), raoParameters, initialResults); - treeParameters = TreeParameters.buildForPreventivePerimeter(raoParameters.getExtension(SearchTreeRaoParameters.class)); - } else { - optPerimeter = CurativeOptimizationPerimeter.build(raoInput.getOptimizedState(), raoInput.getCrac(), raoInput.getNetwork(), raoParameters, initialResults); - treeParameters = TreeParameters.buildForCurativePerimeter(raoParameters.getExtension(SearchTreeRaoParameters.class), -Double.MAX_VALUE); - operatorsNotToOptimize.addAll(stateTree.getOperatorsNotSharingCras()); - } - perimeterFlowCnecs = optPerimeter.getFlowCnecs(); - SearchTreeParameters searchTreeParameters = SearchTreeParameters.create() - .withConstantParametersOverAllRao(raoParameters, raoInput.getCrac()) - .withTreeParameters(treeParameters) - .withUnoptimizedCnecParameters(UnoptimizedCnecParameters.build(raoParameters, stateTree.getOperatorsNotSharingCras(), raoInput.getCrac())) - .build(); - SearchTreeInput searchTreeInput = SearchTreeInput.create() - .withNetwork(raoInput.getNetwork()) - .withOptimizationPerimeter(optPerimeter) - .withInitialFlowResult(initialResults) - .withPrePerimeterResult(initialResults) - .withPreOptimizationAppliedNetworkActions(new AppliedRemedialActions()) //no remedial Action applied - .withObjectiveFunction(ObjectiveFunction.create().build(optPerimeter.getFlowCnecs(), optPerimeter.getLoopFlowCnecs(), initialResults, initialResults, initialResults, raoInput.getCrac(), operatorsNotToOptimize, raoParameters)) - .withToolProvider(toolProvider) - .build(); - optimizationResult = new SearchTree(searchTreeInput, searchTreeParameters, true).run().join(); - - // apply RAs and return results - optimizationResult.getRangeActions().forEach(rangeAction -> rangeAction.apply(raoInput.getNetwork(), optimizationResult.getOptimizedSetpoint(rangeAction, raoInput.getOptimizedState()))); - optimizationResult.getActivatedNetworkActions().forEach(networkAction -> networkAction.apply(raoInput.getNetwork())); + optPerimeter = CurativeOptimizationPerimeter.build(raoInput.getOptimizedState(), raoInput.getCrac(), raoInput.getNetwork(), raoParameters, initialResults); + treeParameters = TreeParameters.buildForCurativePerimeter(raoParameters.getExtension(SearchTreeRaoParameters.class), -Double.MAX_VALUE); + operatorsNotToOptimize.addAll(stateTree.getOperatorsNotSharingCras()); } - return CompletableFuture.completedFuture(new OneStateOnlyRaoResultImpl(raoInput.getOptimizedState(), initialResults, optimizationResult, perimeterFlowCnecs)); + SearchTreeParameters searchTreeParameters = SearchTreeParameters.create() + .withConstantParametersOverAllRao(raoParameters, raoInput.getCrac()) + .withTreeParameters(treeParameters) + .withUnoptimizedCnecParameters(UnoptimizedCnecParameters.build(raoParameters, stateTree.getOperatorsNotSharingCras(), raoInput.getCrac())) + .build(); + + SearchTreeInput searchTreeInput = SearchTreeInput.create() + .withNetwork(raoInput.getNetwork()) + .withOptimizationPerimeter(optPerimeter) + .withInitialFlowResult(initialResults) + .withPrePerimeterResult(initialResults) + .withPreOptimizationAppliedNetworkActions(new AppliedRemedialActions()) //no remedial Action applied + .withObjectiveFunction(ObjectiveFunction.create().build(optPerimeter.getFlowCnecs(), optPerimeter.getLoopFlowCnecs(), initialResults, initialResults, initialResults, raoInput.getCrac(), operatorsNotToOptimize, raoParameters)) + .withToolProvider(toolProvider) + .build(); + + OptimizationResult optimizationResult = new SearchTree(searchTreeInput, searchTreeParameters, true).run().join(); + + // apply RAs and return results + optimizationResult.getRangeActions().forEach(rangeAction -> rangeAction.apply(raoInput.getNetwork(), optimizationResult.getOptimizedSetpoint(rangeAction, raoInput.getOptimizedState()))); + optimizationResult.getActivatedNetworkActions().forEach(networkAction -> networkAction.apply(raoInput.getNetwork())); + + return CompletableFuture.completedFuture(new OneStateOnlyRaoResultImpl(raoInput.getOptimizedState(), initialResults, optimizationResult, optPerimeter.getFlowCnecs())); } } From 71856512e6ca2630e90e8bf977b77ed0877371d1 Mon Sep 17 00:00:00 2001 From: Peter Mitri Date: Thu, 12 Jan 2023 11:29:34 +0100 Subject: [PATCH 4/6] Add unit test --- .../algorithm/AutomatonSimulatorTest.java | 67 +++++++++++++------ 1 file changed, 47 insertions(+), 20 deletions(-) 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 d78fbdbb37..9129bd38fa 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 @@ -58,6 +58,8 @@ public class AutomatonSimulatorTest { private RangeAction ara5; private RangeAction ara6; private NetworkAction na; + private HvdcRangeAction hvdcRa1; + private HvdcRangeAction hvdcRa2; private FlowCnec cnec1; private FlowCnec cnec2; private PrePerimeterSensitivityAnalysis mockedPreAutoPerimeterSensitivityAnalysis; @@ -179,6 +181,24 @@ public void setup() { .newOnFlowConstraintUsageRule().withInstant(Instant.AUTO).withFlowCnec("cnec2").add() .add(); + // Add HVDC range actions + hvdcRa1 = crac.newHvdcRangeAction() + .withId("hvdc-ra1") + .withGroupId("hvdcGroup") + .withNetworkElement("BBE2AA11 FFR3AA11 1") + .withSpeed(1) + .newRange().withMax(1).withMin(-1).add() + .newFreeToUseUsageRule().withInstant(Instant.AUTO).withUsageMethod(UsageMethod.FORCED).add() + .add(); + hvdcRa2 = crac.newHvdcRangeAction() + .withId("hvdc-ra2") + .withGroupId("hvdcGroup") + .withNetworkElement("BBE2AA12 FFR3AA12 1") + .withSpeed(1) + .newRange().withMax(1).withMin(-1).add() + .newFreeToUseUsageRule().withInstant(Instant.AUTO).withUsageMethod(UsageMethod.FORCED).add() + .add(); + autoState = crac.getState(contingency1, Instant.AUTO); RaoParameters raoParameters = new RaoParameters(); @@ -227,30 +247,15 @@ public void testCheckAlignedRangeActions1() { public void testBuildRangeActionsGroupsOrderedBySpeed() { PrePerimeterResult rangeActionSensitivity = Mockito.mock(PrePerimeterResult.class); List>> result = automatonSimulator.buildRangeActionsGroupsOrderedBySpeed(rangeActionSensitivity, autoState, network); - assertEquals(List.of(List.of(ra2), List.of(ara1, ara2), List.of(ra3)), result); + assertEquals(List.of(List.of(hvdcRa1, hvdcRa2), List.of(ra2), List.of(ara1, ara2), List.of(ra3)), result); } @Test public void testDisableAcEmulation() { - HvdcRangeAction hvdcRa = crac.newHvdcRangeAction() - .withId("hvdc-ra") - .withNetworkElement("BBE2AA11 FFR3AA11 1") - .withSpeed(1) - .newRange().withMax(1).withMin(-1).add() - .newFreeToUseUsageRule().withInstant(Instant.AUTO).withUsageMethod(UsageMethod.FORCED).add() - .add(); - HvdcRangeAction hvdcRa2 = crac.newHvdcRangeAction() - .withId("hvdc-ra2") - .withNetworkElement("BBE2AA12 FFR3AA12 1") - .withSpeed(1) - .newRange().withMax(1).withMin(-1).add() - .newFreeToUseUsageRule().withInstant(Instant.AUTO).withUsageMethod(UsageMethod.FORCED).add() - .add(); - PrePerimeterResult prePerimeterResult = mock(PrePerimeterResult.class); when(mockedPreAutoPerimeterSensitivityAnalysis.runBasedOnInitialResults(any(), any(), any(), any(), any(), any())).thenReturn(mockedPrePerimeterResult); - PrePerimeterResult result = automatonSimulator.disableACEmulation(List.of(hvdcRa), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult, autoState); + PrePerimeterResult result = automatonSimulator.disableACEmulation(List.of(hvdcRa1), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult, autoState); // check that AC emulation was disabled on HVDC assertFalse(network.getHvdcLine("BBE2AA11 FFR3AA11 1").getExtension(HvdcAngleDroopActivePowerControl.class).isEnabled()); // check that other HVDC was not touched @@ -259,20 +264,20 @@ public void testDisableAcEmulation() { assertEquals(mockedPrePerimeterResult, result); // run a second time => no influence + sensi not run - result = automatonSimulator.disableACEmulation(List.of(hvdcRa), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult, autoState); + result = automatonSimulator.disableACEmulation(List.of(hvdcRa1), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult, autoState); assertFalse(network.getHvdcLine("BBE2AA11 FFR3AA11 1").getExtension(HvdcAngleDroopActivePowerControl.class).isEnabled()); assertTrue(network.getHvdcLine("BBE2AA12 FFR3AA12 1").getExtension(HvdcAngleDroopActivePowerControl.class).isEnabled()); assertEquals(prePerimeterResult, result); // Test on 2 aligned HVDC RAs - result = automatonSimulator.disableACEmulation(List.of(hvdcRa, hvdcRa2), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult, autoState); + result = automatonSimulator.disableACEmulation(List.of(hvdcRa1, hvdcRa2), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult, autoState); assertFalse(network.getHvdcLine("BBE2AA11 FFR3AA11 1").getExtension(HvdcAngleDroopActivePowerControl.class).isEnabled()); assertFalse(network.getHvdcLine("BBE2AA12 FFR3AA12 1").getExtension(HvdcAngleDroopActivePowerControl.class).isEnabled()); assertEquals(mockedPrePerimeterResult, result); // Test on an HVDC with no HvdcAngleDroopActivePowerControl network.getHvdcLine("BBE2AA11 FFR3AA11 1").removeExtension(HvdcAngleDroopActivePowerControl.class); - result = automatonSimulator.disableACEmulation(List.of(hvdcRa), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult, autoState); + result = automatonSimulator.disableACEmulation(List.of(hvdcRa1), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult, autoState); assertEquals(prePerimeterResult, result); // Test on non-HVDC : nothing should happen @@ -513,4 +518,26 @@ public void testSimulateAutomatonState() { assertEquals(Set.of(), result.getActivatedRangeActions(autoState)); assertEquals(Map.of(ara1, 0.1, ara2, 0.1), result.getOptimizedSetpointsOnState(autoState)); } + + + @Test + public void testDisableAcEmulationBeforeShifting() { + PrePerimeterResult prePerimeterResult = mock(PrePerimeterResult.class); + when(mockedPreAutoPerimeterSensitivityAnalysis.runBasedOnInitialResults(any(), any(), any(), any(), any(), any())).thenReturn(mockedPrePerimeterResult); + + // check that AC emulation was not disabled when margins are positive + when(prePerimeterResult.getMargin(cnec1, Side.RIGHT, Unit.MEGAWATT)).thenReturn(0.); + when(prePerimeterResult.getMargin(cnec2, Side.RIGHT, Unit.MEGAWATT)).thenReturn(100.); + automatonSimulator.shiftRangeActionsUntilFlowCnecsSecure(List.of(hvdcRa1, hvdcRa2), Set.of(cnec1, cnec2), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult, autoState); + assertTrue(network.getHvdcLine("BBE2AA11 FFR3AA11 1").getExtension(HvdcAngleDroopActivePowerControl.class).isEnabled()); + assertTrue(network.getHvdcLine("BBE2AA12 FFR3AA12 1").getExtension(HvdcAngleDroopActivePowerControl.class).isEnabled()); + + // check that AC emulation is disabled when one margin is negative + when(prePerimeterResult.getMargin(cnec1, Side.RIGHT, Unit.MEGAWATT)).thenReturn(-1.); + when(prePerimeterResult.getMargin(cnec2, Side.RIGHT, Unit.MEGAWATT)).thenReturn(100.); + automatonSimulator.shiftRangeActionsUntilFlowCnecsSecure(List.of(hvdcRa1, hvdcRa2), Set.of(cnec1, cnec2), network, mockedPreAutoPerimeterSensitivityAnalysis, prePerimeterResult, autoState); + assertFalse(network.getHvdcLine("BBE2AA11 FFR3AA11 1").getExtension(HvdcAngleDroopActivePowerControl.class).isEnabled()); + assertFalse(network.getHvdcLine("BBE2AA12 FFR3AA12 1").getExtension(HvdcAngleDroopActivePowerControl.class).isEnabled()); + } + } From ebee77d84fd39a2dd8adff8fbc4ffd95e6b45761 Mon Sep 17 00:00:00 2001 From: Peter Mitri Date: Thu, 12 Jan 2023 11:40:33 +0100 Subject: [PATCH 5/6] checkstyle --- .../search_tree_rao/castor/algorithm/AutomatonSimulatorTest.java | 1 - 1 file changed, 1 deletion(-) 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 9129bd38fa..0ae49941f0 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 @@ -519,7 +519,6 @@ public void testSimulateAutomatonState() { assertEquals(Map.of(ara1, 0.1, ara2, 0.1), result.getOptimizedSetpointsOnState(autoState)); } - @Test public void testDisableAcEmulationBeforeShifting() { PrePerimeterResult prePerimeterResult = mock(PrePerimeterResult.class); From 8f33dfb6ad49862b216d8a313021b2e1135cc101 Mon Sep 17 00:00:00 2001 From: Peter Mitri Date: Thu, 12 Jan 2023 12:05:05 +0100 Subject: [PATCH 6/6] merge master --- .../castor/algorithm/CastorOneStateOnly.java | 70 +++++++++++-------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorOneStateOnly.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorOneStateOnly.java index 7550035ccb..66fa42e8c1 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorOneStateOnly.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/castor/algorithm/CastorOneStateOnly.java @@ -6,6 +6,9 @@ */ package com.farao_community.farao.search_tree_rao.castor.algorithm; +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.FlowCnec; import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; import com.farao_community.farao.data.rao_result_api.RaoResult; import com.farao_community.farao.rao_api.RaoInput; @@ -80,37 +83,46 @@ public CompletableFuture run() { OptimizationPerimeter optPerimeter; TreeParameters treeParameters; Set operatorsNotToOptimize = new HashSet<>(); - if (raoInput.getOptimizedState().equals(raoInput.getCrac().getPreventiveState())) { - optPerimeter = PreventiveOptimizationPerimeter.buildWithPreventiveCnecsOnly(raoInput.getCrac(), raoInput.getNetwork(), raoParameters, initialResults); - treeParameters = TreeParameters.buildForPreventivePerimeter(raoParameters.getExtension(SearchTreeRaoParameters.class)); - } else { - optPerimeter = CurativeOptimizationPerimeter.build(raoInput.getOptimizedState(), raoInput.getCrac(), raoInput.getNetwork(), raoParameters, initialResults); - treeParameters = TreeParameters.buildForCurativePerimeter(raoParameters.getExtension(SearchTreeRaoParameters.class), -Double.MAX_VALUE); - operatorsNotToOptimize.addAll(stateTree.getOperatorsNotSharingCras()); - } - - SearchTreeParameters searchTreeParameters = SearchTreeParameters.create() - .withConstantParametersOverAllRao(raoParameters, raoInput.getCrac()) - .withTreeParameters(treeParameters) - .withUnoptimizedCnecParameters(UnoptimizedCnecParameters.build(raoParameters, stateTree.getOperatorsNotSharingCras(), raoInput.getCrac())) - .build(); - SearchTreeInput searchTreeInput = SearchTreeInput.create() - .withNetwork(raoInput.getNetwork()) - .withOptimizationPerimeter(optPerimeter) - .withInitialFlowResult(initialResults) - .withPrePerimeterResult(initialResults) - .withPreOptimizationAppliedNetworkActions(new AppliedRemedialActions()) //no remedial Action applied - .withObjectiveFunction(ObjectiveFunction.create().build(optPerimeter.getFlowCnecs(), optPerimeter.getLoopFlowCnecs(), initialResults, initialResults, initialResults, raoInput.getCrac(), operatorsNotToOptimize, raoParameters)) - .withToolProvider(toolProvider) - .build(); + OptimizationResult optimizationResult; + Set perimeterFlowCnecs; - OptimizationResult optimizationResult = new SearchTree(searchTreeInput, searchTreeParameters, true).run().join(); - - // apply RAs and return results - optimizationResult.getRangeActions().forEach(rangeAction -> rangeAction.apply(raoInput.getNetwork(), optimizationResult.getOptimizedSetpoint(rangeAction, raoInput.getOptimizedState()))); - optimizationResult.getActivatedNetworkActions().forEach(networkAction -> networkAction.apply(raoInput.getNetwork())); + if (raoInput.getOptimizedState().getInstant().equals(Instant.AUTO)) { + perimeterFlowCnecs = raoInput.getCrac().getFlowCnecs(raoInput.getOptimizedState()); + State curativeState = raoInput.getCrac().getState(raoInput.getOptimizedState().getContingency().orElseThrow().getId(), Instant.CURATIVE); + AutomatonSimulator automatonSimulator = new AutomatonSimulator(raoInput.getCrac(), raoParameters, toolProvider, initialResults, initialResults, initialResults, stateTree.getOperatorsNotSharingCras(), 2); + optimizationResult = automatonSimulator.simulateAutomatonState(raoInput.getOptimizedState(), curativeState, raoInput.getNetwork()); + } else { + if (raoInput.getOptimizedState().equals(raoInput.getCrac().getPreventiveState())) { + optPerimeter = PreventiveOptimizationPerimeter.buildWithPreventiveCnecsOnly(raoInput.getCrac(), raoInput.getNetwork(), raoParameters, initialResults); + treeParameters = TreeParameters.buildForPreventivePerimeter(raoParameters.getExtension(SearchTreeRaoParameters.class)); + } else { + optPerimeter = CurativeOptimizationPerimeter.build(raoInput.getOptimizedState(), raoInput.getCrac(), raoInput.getNetwork(), raoParameters, initialResults); + treeParameters = TreeParameters.buildForCurativePerimeter(raoParameters.getExtension(SearchTreeRaoParameters.class), -Double.MAX_VALUE); + operatorsNotToOptimize.addAll(stateTree.getOperatorsNotSharingCras()); + } + perimeterFlowCnecs = optPerimeter.getFlowCnecs(); + SearchTreeParameters searchTreeParameters = SearchTreeParameters.create() + .withConstantParametersOverAllRao(raoParameters, raoInput.getCrac()) + .withTreeParameters(treeParameters) + .withUnoptimizedCnecParameters(UnoptimizedCnecParameters.build(raoParameters, stateTree.getOperatorsNotSharingCras(), raoInput.getCrac())) + .build(); + SearchTreeInput searchTreeInput = SearchTreeInput.create() + .withNetwork(raoInput.getNetwork()) + .withOptimizationPerimeter(optPerimeter) + .withInitialFlowResult(initialResults) + .withPrePerimeterResult(initialResults) + .withPreOptimizationAppliedNetworkActions(new AppliedRemedialActions()) //no remedial Action applied + .withObjectiveFunction(ObjectiveFunction.create().build(optPerimeter.getFlowCnecs(), optPerimeter.getLoopFlowCnecs(), initialResults, initialResults, initialResults, raoInput.getCrac(), operatorsNotToOptimize, raoParameters)) + .withToolProvider(toolProvider) + .build(); + optimizationResult = new SearchTree(searchTreeInput, searchTreeParameters, true).run().join(); + + // apply RAs and return results + optimizationResult.getRangeActions().forEach(rangeAction -> rangeAction.apply(raoInput.getNetwork(), optimizationResult.getOptimizedSetpoint(rangeAction, raoInput.getOptimizedState()))); + optimizationResult.getActivatedNetworkActions().forEach(networkAction -> networkAction.apply(raoInput.getNetwork())); + } - return CompletableFuture.completedFuture(new OneStateOnlyRaoResultImpl(raoInput.getOptimizedState(), initialResults, optimizationResult, optPerimeter.getFlowCnecs())); + return CompletableFuture.completedFuture(new OneStateOnlyRaoResultImpl(raoInput.getOptimizedState(), initialResults, optimizationResult, perimeterFlowCnecs)); } }