From 7c8a717b068a5098d6af1a3e79b15b034865657e Mon Sep 17 00:00:00 2001 From: belthlemar Date: Mon, 13 Feb 2023 18:43:10 +0100 Subject: [PATCH 01/14] filter out pst with unmodified taps --- .../search_tree_rao/commons/RaoLogger.java | 36 ++++++++----------- .../search_tree/algorithms/SearchTree.java | 2 +- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java index 32a58854c2..df7a129799 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java @@ -14,6 +14,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.data.rao_result_api.OptimizationState; import com.farao_community.farao.rao_api.parameters.RaoParameters; import com.farao_community.farao.search_tree_rao.commons.objective_function_evaluator.ObjectiveFunction; @@ -62,12 +63,6 @@ public static void logSensitivityAnalysisResults(String prefix, numberOfLoggedLimitingElements); } - public static void logRangeActions(FaraoLogger logger, - Leaf leaf, - OptimizationPerimeter optimizationContext) { - logRangeActions(logger, leaf, optimizationContext, null); - } - public static void logRangeActions(FaraoLogger logger, Leaf leaf, OptimizationPerimeter @@ -75,23 +70,22 @@ public static void logRangeActions(FaraoLogger logger, boolean globalPstOptimization = optimizationContext instanceof GlobalOptimizationPerimeter; - String rangeActionSetpoints = optimizationContext.getRangeActionsPerState().entrySet().stream() + List rangeActionSetpoints = optimizationContext.getRangeActionsPerState().entrySet().stream() .flatMap(eState -> eState.getValue().stream().map(rangeAction -> { - double rangeActionValue; - if (rangeAction instanceof PstRangeAction) { - rangeActionValue = leaf.getOptimizedTap((PstRangeAction) rangeAction, eState.getKey()); - } else { - rangeActionValue = leaf.getOptimizedSetpoint(rangeAction, eState.getKey()); - } - if (globalPstOptimization) { - return format("%s@%s: %.0f", rangeAction.getName(), eState.getKey().getId(), rangeActionValue); - } else { - return format("%s: %.0f", rangeAction.getName(), rangeActionValue); + Set> rangeActionSet = leaf.getActivatedRangeActions(eState.getKey()); + if (rangeActionSet.contains(rangeAction)) { + double rangeActionValue; + rangeActionValue = rangeAction instanceof PstRangeAction ? leaf.getOptimizedTap((PstRangeAction) rangeAction, eState.getKey()) : + leaf.getOptimizedSetpoint(rangeAction, eState.getKey()); + return globalPstOptimization ? format("%s@%s: %.0f", rangeAction.getName(), eState.getKey().getId(), rangeActionValue) : + format("%s: %.0f", rangeAction.getName(), rangeActionValue); } - })) - .collect(Collectors.joining(", ")); - - logger.info("{}range action(s): {}", prefix == null ? "" : prefix, rangeActionSetpoints); + return ""; + })).collect(Collectors.toList()); + rangeActionSetpoints.removeAll(List.of("")); + boolean isRangeActionSetPointEmpty = rangeActionSetpoints.isEmpty(); + logger.info(isRangeActionSetPointEmpty ? "{}No range actions activated" : "{}range action(s): {}", + prefix == null ? "" : prefix, isRangeActionSetPointEmpty ? null : String.join(", ", rangeActionSetpoints)); } public static void logMostLimitingElementsResults(FaraoLogger logger, OptimizationResult optimizationResult, RaoParameters.ObjectiveFunction objectiveFunction, int numberOfLoggedElements) { 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 d96b8a50ce..810307f8f7 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 @@ -153,7 +153,7 @@ public CompletableFuture run() { optimizeLeaf(rootLeaf); topLevelLogger.info("{}", rootLeaf); - RaoLogger.logRangeActions(TECHNICAL_LOGS, optimalLeaf, input.getOptimizationPerimeter()); + RaoLogger.logRangeActions(TECHNICAL_LOGS, optimalLeaf, input.getOptimizationPerimeter(), null); RaoLogger.logMostLimitingElementsResults(topLevelLogger, optimalLeaf, parameters.getObjectiveFunction(), NUMBER_LOGGED_ELEMENTS_DURING_TREE); logVirtualCostInformation(rootLeaf, ""); From ffe820c1b32479dd397fd1a4ff136f638500b0b0 Mon Sep 17 00:00:00 2001 From: belthlemar Date: Tue, 14 Feb 2023 18:48:00 +0100 Subject: [PATCH 02/14] include ra names and taps in summary + disabled RaoLoggerTest --- .../castor/algorithm/AutomatonSimulator.java | 15 ++++-- .../search_tree_rao/commons/RaoLogger.java | 54 +++++++++++-------- .../search_tree/algorithms/SearchTree.java | 11 ++-- .../commons/RaoLoggerTest.java | 8 ++- 4 files changed, 56 insertions(+), 32 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 60deffa07b..11795b1210 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 @@ -21,6 +21,7 @@ import com.farao_community.farao.data.crac_api.usage_rule.OnFlowConstraintInCountry; 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.data.rao_result_api.OptimizationState; import com.farao_community.farao.rao_api.parameters.RaoParameters; import com.farao_community.farao.search_tree_rao.commons.RaoLogger; import com.farao_community.farao.search_tree_rao.commons.RaoUtil; @@ -119,7 +120,7 @@ AutomatonPerimeterResultImpl simulateAutomatonState(State automatonState, State automatonState); failedAutomatonPerimeterResultImpl.setComputationStatus(ComputationStatus.FAILURE); TECHNICAL_LOGS.info("Automaton state {} has failed during sensitivity computation during range automaton simulation.", automatonState.getId()); - RaoLogger.logFailedOptimizationSummary(BUSINESS_LOGS, automatonState, failedAutomatonPerimeterResultImpl.getActivatedNetworkActions().size(), failedAutomatonPerimeterResultImpl.getActivatedRangeActions(automatonState).size()); + RaoLogger.logFailedOptimizationSummary(BUSINESS_LOGS, automatonState, failedAutomatonPerimeterResultImpl.getActivatedNetworkActions(), getRangeActionsAppliedOnState(failedAutomatonPerimeterResultImpl, automatonState)); return failedAutomatonPerimeterResultImpl; } // Build and return optimization result @@ -131,7 +132,7 @@ AutomatonPerimeterResultImpl simulateAutomatonState(State automatonState, State rangeAutomatonSimulationResult.getRangeActionsWithSetpoint(), automatonState); TECHNICAL_LOGS.info("Automaton state {} has been optimized.", automatonState.getId()); - RaoLogger.logOptimizationSummary(BUSINESS_LOGS, automatonState, automatonPerimeterResultImpl.getActivatedNetworkActions().size(), automatonPerimeterResultImpl.getActivatedRangeActions(automatonState).size(), null, null, automatonPerimeterResultImpl); + RaoLogger.logOptimizationSummary(BUSINESS_LOGS, automatonState, automatonPerimeterResultImpl.getActivatedNetworkActions(), getRangeActionsAppliedOnState(automatonPerimeterResultImpl, automatonState), null, null, automatonPerimeterResultImpl); return automatonPerimeterResultImpl; } @@ -144,6 +145,14 @@ private PrePerimeterSensitivityAnalysis getPreAutoPerimeterSensitivityAnalysis(S return new PrePerimeterSensitivityAnalysis(flowCnecsInSensi, rangeActionsInSensi, raoParameters, toolProvider); } + public static Map, Double> getRangeActionsAppliedOnState(OptimizationResult optimizationResult, State state) { + Set< RangeAction> setActivatedRangeActions = optimizationResult.getActivatedRangeActions(state); + Map, Double> allRangeActions = new HashMap<>(); + setActivatedRangeActions.stream().filter(PstRangeAction.class::isInstance).map(PstRangeAction.class::cast).forEach(pstRangeAction -> allRangeActions.put(pstRangeAction, (double) optimizationResult.getOptimizedTap(pstRangeAction, state))); + setActivatedRangeActions.stream().filter(ra -> !(ra instanceof PstRangeAction)).forEach(rangeAction -> allRangeActions.put(rangeAction, optimizationResult.getOptimizedSetpoint(rangeAction, state))); + return allRangeActions; + } + AutomatonPerimeterResultImpl createFailedAutomatonPerimeterResult(State autoState, PrePerimeterResult result, Set activatedNetworkActions, String defineMoment) { AutomatonPerimeterResultImpl failedAutomatonPerimeterResultImpl = new AutomatonPerimeterResultImpl( result, @@ -153,7 +162,7 @@ AutomatonPerimeterResultImpl createFailedAutomatonPerimeterResult(State autoStat autoState); failedAutomatonPerimeterResultImpl.setComputationStatus(ComputationStatus.FAILURE); TECHNICAL_LOGS.info("Automaton state {} has failed during sensitivity computation {} topological automaton simulation.", autoState.getId(), defineMoment); - RaoLogger.logFailedOptimizationSummary(BUSINESS_LOGS, autoState, failedAutomatonPerimeterResultImpl.getActivatedNetworkActions().size(), failedAutomatonPerimeterResultImpl.getActivatedRangeActions(autoState).size()); + RaoLogger.logFailedOptimizationSummary(BUSINESS_LOGS, autoState, failedAutomatonPerimeterResultImpl.getActivatedNetworkActions(), getRangeActionsAppliedOnState(failedAutomatonPerimeterResultImpl, autoState)); return failedAutomatonPerimeterResultImpl; } diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java index df7a129799..150de6edbe 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java @@ -10,9 +10,11 @@ import com.farao_community.farao.commons.Unit; import com.farao_community.farao.commons.logs.FaraoLogger; import com.farao_community.farao.data.crac_api.Contingency; +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.cnec.Side; +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.rao_result_api.OptimizationState; @@ -24,6 +26,7 @@ import com.farao_community.farao.search_tree_rao.castor.algorithm.BasecaseScenario; import com.farao_community.farao.search_tree_rao.castor.algorithm.ContingencyScenario; import com.farao_community.farao.search_tree_rao.search_tree.algorithms.Leaf; +import org.apache.commons.lang3.StringUtils; import java.util.*; import java.util.stream.Collectors; @@ -242,40 +245,45 @@ private static Map getMostLimitingElementsAndMargins(Optimizat return mostLimitingElementsAndMargins; } - public static void logOptimizationSummary(FaraoLogger logger, State optimizedState, long activatedNetworkActions, long activatedRangeActions, Double initialFunctionalCost, Double initialVirtualCost, ObjectiveFunctionResult finalObjective) { - Optional optionalContingency = optimizedState.getContingency(); - String scenarioName = optionalContingency.isEmpty() ? "preventive" : optionalContingency.get().getName(); - String raResult = ""; - if (activatedNetworkActions + activatedRangeActions == 0) { - raResult = "no remedial actions activated"; - } else if (activatedNetworkActions > 0 && activatedRangeActions == 0) { - raResult = String.format("%s network action(s) activated", activatedNetworkActions); - } else if (activatedRangeActions > 0 && activatedNetworkActions == 0) { - raResult = String.format("%s range action(s) activated", activatedRangeActions); - } else { - raResult = String.format("%s network action(s) and %s range action(s) activated", activatedNetworkActions, activatedRangeActions); - } + public static void logFailedOptimizationSummary(FaraoLogger logger, State optimizedState, Set networkActions, Map, java.lang.Double> rangeActions) { + String scenarioName = getScenarioName(optimizedState); + String raResult = getRaResult(networkActions, rangeActions, true); + logger.info("Scenario \"{}\": {}", scenarioName, raResult); + } + + public static void logOptimizationSummary(FaraoLogger logger, State optimizedState, Set networkActions, Map, java.lang.Double> rangeActions, Double initialFunctionalCost, Double initialVirtualCost, ObjectiveFunctionResult finalObjective) { + String scenarioName = getScenarioName(optimizedState); + String raResult = getRaResult(networkActions, rangeActions, false); String initialCostString = initialFunctionalCost == null || initialVirtualCost == null ? "" : String.format("initial cost = %s (functional: %s, virtual: %s), ", formatDouble(initialFunctionalCost + initialVirtualCost), formatDouble(initialFunctionalCost), formatDouble(initialVirtualCost)); - logger.info("Scenario \"{}\": {}{}, cost {} = {} (functional: {}, virtual: {})", scenarioName, initialCostString, raResult, OptimizationState.afterOptimizing(optimizedState), formatDouble(finalObjective.getCost()), formatDouble(finalObjective.getFunctionalCost()), formatDouble(finalObjective.getVirtualCost())); } - public static void logFailedOptimizationSummary(FaraoLogger logger, State optimizedState, long activatedNetworkActions, long activatedRangeActions) { - Optional optionalContingency = optimizedState.getContingency(); - String scenarioName = optionalContingency.isEmpty() ? "preventive" : optionalContingency.get().getName(); - String raResult = ""; + public static String getRaResult(Set networkActions, Map, java.lang.Double> rangeActions, boolean failed) { + long activatedNetworkActions = networkActions.size(); + long activatedRangeActions = rangeActions.size(); + String networkActionsNames = StringUtils.join(networkActions.stream().map(Identifiable::getName).collect(Collectors.toSet()), ", "); + + Set rangeActionsSet = new HashSet<>(); + rangeActions.forEach((key, value) -> rangeActionsSet.add(format("%s: %.0f", key.getName(), value))); + String rangeActionsNames = StringUtils.join(rangeActionsSet, ", "); + if (activatedNetworkActions + activatedRangeActions == 0) { - raResult = "no remedial actions activated"; + return "no remedial actions activated"; } else if (activatedNetworkActions > 0 && activatedRangeActions == 0) { - raResult = String.format("%s network action(s) activated", activatedNetworkActions); + return String.format("%s network action(s) activated", activatedNetworkActions) + (failed ? "" : " : " + networkActionsNames); } else if (activatedRangeActions > 0 && activatedNetworkActions == 0) { - raResult = String.format("%s range action(s) activated", activatedRangeActions); + return String.format("%s range action(s) activated", activatedRangeActions) + (failed ? "" : " : " + rangeActionsNames); } else { - raResult = String.format("%s network action(s) and %s range action(s) activated", activatedNetworkActions, activatedRangeActions); + return String.format("%s network action(s) and %s range action(s) activated", activatedNetworkActions, activatedRangeActions) + + (failed ? "" : " : " + networkActionsNames + " and " + rangeActionsNames); } - logger.info("Scenario \"{}\": {}", scenarioName, raResult); + } + + public static String getScenarioName(State state) { + Optional optionalContingency = state.getContingency(); + return optionalContingency.isEmpty() ? "preventive" : optionalContingency.get().getName(); } public static String formatDouble(double value) { 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 810307f8f7..93a5fca874 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 @@ -13,7 +13,10 @@ 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.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.UsageMethod; +import com.farao_community.farao.search_tree_rao.castor.algorithm.AutomatonSimulator; import com.farao_community.farao.search_tree_rao.commons.NetworkActionCombination; import com.farao_community.farao.search_tree_rao.commons.RaoLogger; import com.farao_community.farao.search_tree_rao.commons.SensitivityComputer; @@ -42,6 +45,7 @@ import java.util.stream.Collectors; import static com.farao_community.farao.commons.logs.FaraoLoggerProvider.*; +import static com.farao_community.farao.search_tree_rao.castor.algorithm.AutomatonSimulator.getRangeActionsAppliedOnState; /** * The "tree" is one of the core object of the search-tree algorithm. @@ -209,14 +213,11 @@ private void applyForcedNetworkActionsOnRootLeaf() { } private void logOptimizationSummary(Leaf leaf) { - RaoLogger.logOptimizationSummary(BUSINESS_LOGS, input.getOptimizationPerimeter().getMainOptimizationState(), leaf.getActivatedNetworkActions().size(), getNumberOfActivatedRangeActions(leaf), preOptimFunctionalCost, preOptimVirtualCost, leaf); + State state = input.getOptimizationPerimeter().getMainOptimizationState(); + RaoLogger.logOptimizationSummary(BUSINESS_LOGS, state, leaf.getActivatedNetworkActions(), getRangeActionsAppliedOnState(leaf, state), preOptimFunctionalCost, preOptimVirtualCost, leaf); logVirtualCostInformation(leaf, ""); } - private long getNumberOfActivatedRangeActions(Leaf leaf) { - return leaf.getNumberOfActivatedRangeActions(); - } - private void iterateOnTree() { int depth = 0; boolean hasImproved = true; diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java index 6f39da4753..a8a3d8d279 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java @@ -303,7 +303,9 @@ public void testLogOptimizationSummary() { FaraoLogger logger = FaraoLoggerProvider.BUSINESS_LOGS; List logsList = registerLogs(RaoBusinessLogs.class).list; - + } +} + /* RaoLogger.logOptimizationSummary(logger, preventive, 0, 0, 1., 2., objectiveFunctionResult); assertEquals("[INFO] Scenario \"preventive\": initial cost = 3.00 (functional: 1.00, virtual: 2.00), no remedial actions activated, cost after PRA = -100.00 (functional: -150.00, virtual: 50.00)", logsList.get(logsList.size() - 1).toString()); @@ -315,6 +317,8 @@ public void testLogOptimizationSummary() { RaoLogger.logOptimizationSummary(logger, curative, 3, 2, null, 200., objectiveFunctionResult); assertEquals("[INFO] Scenario \"contingency\": 3 network action(s) and 2 range action(s) activated, cost after CRA = -100.00 (functional: -150.00, virtual: 50.00)", logsList.get(logsList.size() - 1).toString()); + + // TODO REFACTO CES TESTS } @Test @@ -348,3 +352,5 @@ public void testLogFailedOptimizationSummary() { assertEquals("[INFO] Scenario \"contingency\": 3 network action(s) and 2 range action(s) activated", logsList.get(logsList.size() - 1).toString()); } } + + */ From 77301a94523b34ec7531bbcd5a83840a4cc7f065 Mon Sep 17 00:00:00 2001 From: belthlemar Date: Wed, 15 Feb 2023 09:47:48 +0100 Subject: [PATCH 03/14] remove unused import --- .../search_tree_rao/castor/algorithm/AutomatonSimulator.java | 1 - .../search_tree_rao/search_tree/algorithms/SearchTree.java | 3 --- 2 files changed, 4 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 11795b1210..407389dad2 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 @@ -21,7 +21,6 @@ import com.farao_community.farao.data.crac_api.usage_rule.OnFlowConstraintInCountry; 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.data.rao_result_api.OptimizationState; import com.farao_community.farao.rao_api.parameters.RaoParameters; import com.farao_community.farao.search_tree_rao.commons.RaoLogger; import com.farao_community.farao.search_tree_rao.commons.RaoUtil; 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 93a5fca874..8b2023d339 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 @@ -13,10 +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.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.UsageMethod; -import com.farao_community.farao.search_tree_rao.castor.algorithm.AutomatonSimulator; import com.farao_community.farao.search_tree_rao.commons.NetworkActionCombination; import com.farao_community.farao.search_tree_rao.commons.RaoLogger; import com.farao_community.farao.search_tree_rao.commons.SensitivityComputer; From eddbc00c942eb4797823b12c5dcf4f893364b5f3 Mon Sep 17 00:00:00 2001 From: belthlemar Date: Thu, 16 Feb 2023 14:51:16 +0100 Subject: [PATCH 04/14] add virtual cost detail in sensi --- .../farao/search_tree_rao/commons/RaoLogger.java | 11 +++++++++-- .../ObjectiveFunction.java | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java index 150de6edbe..e7154d66d1 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java @@ -29,6 +29,7 @@ import org.apache.commons.lang3.StringUtils; import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; import static com.farao_community.farao.commons.logs.FaraoLoggerProvider.BUSINESS_LOGS; @@ -55,10 +56,16 @@ public static void logSensitivityAnalysisResults(String prefix, ObjectiveFunctionResult prePerimeterObjectiveFunctionResult = objectiveFunction.evaluate(sensitivityAnalysisResult, rangeActionActivationResult, sensitivityAnalysisResult, sensitivityAnalysisResult.getSensitivityStatus()); - BUSINESS_LOGS.info(prefix + "cost = {} (functional: {}, virtual: {})", + Map virtualCosts = + prePerimeterObjectiveFunctionResult.getVirtualCostNames().stream() + .filter(name -> prePerimeterObjectiveFunctionResult.getVirtualCost(name) > 0) + .collect(Collectors.toMap(Function.identity(), prePerimeterObjectiveFunctionResult::getVirtualCost)); + + BUSINESS_LOGS.info(prefix + "cost = {} (functional: {}, virtual: {}{})", formatDouble(prePerimeterObjectiveFunctionResult.getCost()), formatDouble(prePerimeterObjectiveFunctionResult.getFunctionalCost()), - formatDouble(prePerimeterObjectiveFunctionResult.getVirtualCost())); + formatDouble(prePerimeterObjectiveFunctionResult.getVirtualCost()), + virtualCosts.isEmpty() ? "" : " " + virtualCosts); RaoLogger.logMostLimitingElementsResults(BUSINESS_LOGS, sensitivityAnalysisResult, 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 963f0e8f04..b07f2e2240 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 @@ -84,11 +84,11 @@ public double getVirtualCost(FlowResult flowResult, RangeActionActivationResult .orElse(Double.NaN); } - public double getVirtualCost(FlowResult flowResult, RangeActionActivationResult rangeActionActivationResult, SensitivityResult sensitivityResult, ComputationStatus sensitivityStatus, String virtualCostName, Set contingenciesToExlude) { + public double getVirtualCost(FlowResult flowResult, RangeActionActivationResult rangeActionActivationResult, SensitivityResult sensitivityResult, ComputationStatus sensitivityStatus, String virtualCostName, Set contingenciesToExclude) { return virtualCostEvaluators.stream() .filter(costEvaluator -> costEvaluator.getName().equals(virtualCostName)) .findAny() - .map(costEvaluator -> costEvaluator.computeCost(flowResult, rangeActionActivationResult, sensitivityResult, sensitivityStatus, contingenciesToExlude)) + .map(costEvaluator -> costEvaluator.computeCost(flowResult, rangeActionActivationResult, sensitivityResult, sensitivityStatus, contingenciesToExclude)) .orElse(Double.NaN); } From 95c4469b4808b41d2e9c99a57db69f282f19d84e Mon Sep 17 00:00:00 2001 From: belthlemar Date: Thu, 16 Feb 2023 17:30:40 +0100 Subject: [PATCH 05/14] add info everywhere but with ugly method --- .../search_tree_rao/commons/RaoLogger.java | 10 ++-- .../search_tree/algorithms/SearchTree.java | 54 ++++++++++++++++--- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java index e7154d66d1..c4578344ea 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java @@ -29,10 +29,10 @@ import org.apache.commons.lang3.StringUtils; import java.util.*; -import java.util.function.Function; import java.util.stream.Collectors; import static com.farao_community.farao.commons.logs.FaraoLoggerProvider.BUSINESS_LOGS; +import static com.farao_community.farao.search_tree_rao.search_tree.algorithms.SearchTree.getVirtualCostDetailed; import static java.lang.String.format; /** @@ -56,10 +56,7 @@ public static void logSensitivityAnalysisResults(String prefix, ObjectiveFunctionResult prePerimeterObjectiveFunctionResult = objectiveFunction.evaluate(sensitivityAnalysisResult, rangeActionActivationResult, sensitivityAnalysisResult, sensitivityAnalysisResult.getSensitivityStatus()); - Map virtualCosts = - prePerimeterObjectiveFunctionResult.getVirtualCostNames().stream() - .filter(name -> prePerimeterObjectiveFunctionResult.getVirtualCost(name) > 0) - .collect(Collectors.toMap(Function.identity(), prePerimeterObjectiveFunctionResult::getVirtualCost)); + Map virtualCosts = getVirtualCostDetailed(prePerimeterObjectiveFunctionResult); BUSINESS_LOGS.info(prefix + "cost = {} (functional: {}, virtual: {}{})", formatDouble(prePerimeterObjectiveFunctionResult.getCost()), @@ -261,8 +258,9 @@ public static void logFailedOptimizationSummary(FaraoLogger logger, State optimi public static void logOptimizationSummary(FaraoLogger logger, State optimizedState, Set networkActions, Map, java.lang.Double> rangeActions, Double initialFunctionalCost, Double initialVirtualCost, ObjectiveFunctionResult finalObjective) { String scenarioName = getScenarioName(optimizedState); String raResult = getRaResult(networkActions, rangeActions, false); + Map virtualCostDetailed = getVirtualCostDetailed(finalObjective); String initialCostString = initialFunctionalCost == null || initialVirtualCost == null ? "" : - String.format("initial cost = %s (functional: %s, virtual: %s), ", formatDouble(initialFunctionalCost + initialVirtualCost), formatDouble(initialFunctionalCost), formatDouble(initialVirtualCost)); + String.format("initial cost = %s (functional: %s, virtual: %s %s), ", formatDouble(initialFunctionalCost + initialVirtualCost), formatDouble(initialFunctionalCost), formatDouble(initialVirtualCost), virtualCostDetailed.isEmpty() ? "" : virtualCostDetailed); logger.info("Scenario \"{}\": {}{}, cost {} = {} (functional: {}, virtual: {})", scenarioName, initialCostString, raResult, OptimizationState.afterOptimizing(optimizedState), formatDouble(finalObjective.getCost()), formatDouble(finalObjective.getFunctionalCost()), formatDouble(finalObjective.getVirtualCost())); } 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 4db2cf792e..5d9dbe7794 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 @@ -20,6 +20,7 @@ import com.farao_community.farao.search_tree_rao.commons.optimization_perimeters.CurativeOptimizationPerimeter; import com.farao_community.farao.search_tree_rao.commons.optimization_perimeters.OptimizationPerimeter; import com.farao_community.farao.search_tree_rao.commons.parameters.TreeParameters; +import com.farao_community.farao.search_tree_rao.result.api.ObjectiveFunctionResult; import com.farao_community.farao.search_tree_rao.result.api.OptimizationResult; import com.farao_community.farao.search_tree_rao.result.api.PrePerimeterResult; import com.farao_community.farao.search_tree_rao.search_tree.inputs.SearchTreeInput; @@ -36,6 +37,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Function; import java.util.stream.Collectors; import static com.farao_community.farao.commons.logs.FaraoLoggerProvider.*; @@ -142,13 +144,19 @@ public CompletableFuture run() { return CompletableFuture.completedFuture(rootLeaf); } - TECHNICAL_LOGS.info("{}", rootLeaf); + // TODO Change this ugly behavior + Map virtualCostDetailed = getVirtualCostDetailed(rootLeaf); + TECHNICAL_LOGS.info("{}", virtualCostDetailed.isEmpty() ? rootLeaf : rootLeaf.toString().substring(0, rootLeaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); + RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, rootLeaf, parameters.getObjectiveFunction(), NUMBER_LOGGED_ELEMENTS_DURING_TREE); TECHNICAL_LOGS.info("Linear optimization on root leaf"); optimizeLeaf(rootLeaf); - topLevelLogger.info("{}", rootLeaf); + // TODO Change this ugly behavior + virtualCostDetailed = getVirtualCostDetailed(rootLeaf); + topLevelLogger.info("{}", virtualCostDetailed.isEmpty() ? rootLeaf : rootLeaf.toString().substring(0, rootLeaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); + RaoLogger.logRangeActions(TECHNICAL_LOGS, optimalLeaf, input.getOptimizationPerimeter(), null); RaoLogger.logMostLimitingElementsResults(topLevelLogger, optimalLeaf, parameters.getObjectiveFunction(), NUMBER_LOGGED_ELEMENTS_DURING_TREE); logVirtualCostInformation(rootLeaf, ""); @@ -161,7 +169,11 @@ public CompletableFuture run() { iterateOnTree(); TECHNICAL_LOGS.info("Search-tree RAO completed with status {}", optimalLeaf.getSensitivityStatus()); - TECHNICAL_LOGS.info("Best leaf: {}", optimalLeaf); + + // TODO Change this ugly behavior + virtualCostDetailed = getVirtualCostDetailed(optimalLeaf); + TECHNICAL_LOGS.info("Best leaf: {}", virtualCostDetailed.isEmpty() ? optimalLeaf : optimalLeaf.toString().substring(0, optimalLeaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); + RaoLogger.logRangeActions(TECHNICAL_LOGS, optimalLeaf, input.getOptimizationPerimeter(), "Best leaf: "); RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, optimalLeaf, parameters.getObjectiveFunction(), NUMBER_LOGGED_ELEMENTS_END_TREE); @@ -227,7 +239,12 @@ private void iterateOnTree() { hasImproved = previousDepthOptimalLeaf != optimalLeaf; // It means this depth evaluation has improved the global cost if (hasImproved) { TECHNICAL_LOGS.info("Search depth {} [end]", depth + 1); - topLevelLogger.info("Search depth {} best leaf: {}", depth + 1, optimalLeaf); + + // TODO Change this ugly behavior + Map virtualCostDetailed = getVirtualCostDetailed(optimalLeaf); + topLevelLogger.info("Search depth {} best leaf: {}", depth + 1, + virtualCostDetailed.isEmpty() ? optimalLeaf : optimalLeaf.toString().substring(0, optimalLeaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); + RaoLogger.logRangeActions(TECHNICAL_LOGS, optimalLeaf, input.getOptimizationPerimeter(), String.format("Search depth %s best leaf: ", depth + 1)); RaoLogger.logMostLimitingElementsResults(topLevelLogger, optimalLeaf, parameters.getObjectiveFunction(), NUMBER_LOGGED_ELEMENTS_DURING_TREE); } else { @@ -370,22 +387,36 @@ void optimizeNextLeafAndUpdate(NetworkActionCombination naCombination, Network n } // We evaluate the leaf with taking the results of the previous optimal leaf if we do not want to update some results leaf.evaluate(input.getObjectiveFunction(), getSensitivityComputerForEvaluation()); - TECHNICAL_LOGS.debug("Evaluated {}", leaf); + + // TODO Change this ugly behavior + Map virtualCostDetailed = getVirtualCostDetailed(leaf); + topLevelLogger.info("Evaluated {}", virtualCostDetailed.isEmpty() ? leaf : leaf.toString().substring(0, leaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); + if (!leaf.getStatus().equals(Leaf.Status.ERROR)) { if (!stopCriterionReached(leaf)) { if (combinationFulfillingStopCriterion.isPresent() && deterministicNetworkActionCombinationComparison(naCombination, combinationFulfillingStopCriterion.get()) > 0) { topLevelLogger.info("Skipping {} optimization because earlier combination fulfills stop criterion.", naCombination.getConcatenatedId()); } else { optimizeLeaf(leaf); - topLevelLogger.info("Optimized {}", leaf); + + // TODO Change this ugly behavior + virtualCostDetailed = getVirtualCostDetailed(leaf); + topLevelLogger.info("Optimized {}", virtualCostDetailed.isEmpty() ? leaf : leaf.toString().substring(0, leaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); + logVirtualCostInformation(leaf, "Optimized "); } } else { - topLevelLogger.info("Evaluated {}", leaf); + + // TODO Change this ugly behavior + virtualCostDetailed = getVirtualCostDetailed(leaf); + topLevelLogger.info("Evaluated {}", virtualCostDetailed.isEmpty() ? leaf : leaf.toString().substring(0, leaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); } updateOptimalLeaf(leaf, naCombination); } else { - topLevelLogger.info("Could not evaluate leaf: {}", leaf); + + // TODO Change this ugly behavior + virtualCostDetailed = getVirtualCostDetailed(leaf); + topLevelLogger.info("Could not evaluate {}", virtualCostDetailed.isEmpty() ? leaf : leaf.toString().substring(0, leaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); } } @@ -505,6 +536,13 @@ private boolean improvedEnough(Leaf leaf) { && (1 - Math.signum(previousDepthBestCost) * relativeImpact) * previousDepthBestCost > newCost; // enough relative impact } + public static Map getVirtualCostDetailed(ObjectiveFunctionResult objectiveFunctionResult) { + return objectiveFunctionResult.getVirtualCostNames().stream() + .filter(virtualCostName -> objectiveFunctionResult.getVirtualCost(virtualCostName) > 1e-6) + .collect(Collectors.toMap(Function.identity(), + name -> Math.round(objectiveFunctionResult.getVirtualCost(name) * 100.0) / 100.0)); + } + /** * This method logs information about positive virtual costs */ From 537178b5102a2b549d988db46f97201f63b74b30 Mon Sep 17 00:00:00 2001 From: belthlemar Date: Thu, 16 Feb 2023 18:02:38 +0100 Subject: [PATCH 06/14] add method to not duplicate code --- .../search_tree/algorithms/SearchTree.java | 54 ++++++++----------- 1 file changed, 22 insertions(+), 32 deletions(-) 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 5d9dbe7794..d5cd2850ef 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 @@ -38,6 +38,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Function; +import java.util.logging.Logger; import java.util.stream.Collectors; import static com.farao_community.farao.commons.logs.FaraoLoggerProvider.*; @@ -144,19 +145,13 @@ public CompletableFuture run() { return CompletableFuture.completedFuture(rootLeaf); } - // TODO Change this ugly behavior - Map virtualCostDetailed = getVirtualCostDetailed(rootLeaf); - TECHNICAL_LOGS.info("{}", virtualCostDetailed.isEmpty() ? rootLeaf : rootLeaf.toString().substring(0, rootLeaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); - + logVirtualCostNamesIfPresent(TECHNICAL_LOGS, rootLeaf, "{}"); RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, rootLeaf, parameters.getObjectiveFunction(), NUMBER_LOGGED_ELEMENTS_DURING_TREE); TECHNICAL_LOGS.info("Linear optimization on root leaf"); optimizeLeaf(rootLeaf); - // TODO Change this ugly behavior - virtualCostDetailed = getVirtualCostDetailed(rootLeaf); - topLevelLogger.info("{}", virtualCostDetailed.isEmpty() ? rootLeaf : rootLeaf.toString().substring(0, rootLeaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); - + logVirtualCostNamesIfPresent(topLevelLogger, rootLeaf, "{}"); RaoLogger.logRangeActions(TECHNICAL_LOGS, optimalLeaf, input.getOptimizationPerimeter(), null); RaoLogger.logMostLimitingElementsResults(topLevelLogger, optimalLeaf, parameters.getObjectiveFunction(), NUMBER_LOGGED_ELEMENTS_DURING_TREE); logVirtualCostInformation(rootLeaf, ""); @@ -170,10 +165,7 @@ public CompletableFuture run() { TECHNICAL_LOGS.info("Search-tree RAO completed with status {}", optimalLeaf.getSensitivityStatus()); - // TODO Change this ugly behavior - virtualCostDetailed = getVirtualCostDetailed(optimalLeaf); - TECHNICAL_LOGS.info("Best leaf: {}", virtualCostDetailed.isEmpty() ? optimalLeaf : optimalLeaf.toString().substring(0, optimalLeaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); - + logVirtualCostNamesIfPresent(TECHNICAL_LOGS, optimalLeaf, "Best leaf: {}"); RaoLogger.logRangeActions(TECHNICAL_LOGS, optimalLeaf, input.getOptimizationPerimeter(), "Best leaf: "); RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, optimalLeaf, parameters.getObjectiveFunction(), NUMBER_LOGGED_ELEMENTS_END_TREE); @@ -240,11 +232,8 @@ private void iterateOnTree() { if (hasImproved) { TECHNICAL_LOGS.info("Search depth {} [end]", depth + 1); - // TODO Change this ugly behavior - Map virtualCostDetailed = getVirtualCostDetailed(optimalLeaf); - topLevelLogger.info("Search depth {} best leaf: {}", depth + 1, - virtualCostDetailed.isEmpty() ? optimalLeaf : optimalLeaf.toString().substring(0, optimalLeaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); - + String strDepth = String.valueOf(depth + 1); + logVirtualCostNamesIfPresent(topLevelLogger, optimalLeaf, "Search depth " + strDepth + " best leaf: {}"); RaoLogger.logRangeActions(TECHNICAL_LOGS, optimalLeaf, input.getOptimizationPerimeter(), String.format("Search depth %s best leaf: ", depth + 1)); RaoLogger.logMostLimitingElementsResults(topLevelLogger, optimalLeaf, parameters.getObjectiveFunction(), NUMBER_LOGGED_ELEMENTS_DURING_TREE); } else { @@ -388,9 +377,7 @@ void optimizeNextLeafAndUpdate(NetworkActionCombination naCombination, Network n // We evaluate the leaf with taking the results of the previous optimal leaf if we do not want to update some results leaf.evaluate(input.getObjectiveFunction(), getSensitivityComputerForEvaluation()); - // TODO Change this ugly behavior - Map virtualCostDetailed = getVirtualCostDetailed(leaf); - topLevelLogger.info("Evaluated {}", virtualCostDetailed.isEmpty() ? leaf : leaf.toString().substring(0, leaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); + logVirtualCostNamesIfPresent(topLevelLogger, leaf, "Evaluated {}"); if (!leaf.getStatus().equals(Leaf.Status.ERROR)) { if (!stopCriterionReached(leaf)) { @@ -399,24 +386,15 @@ void optimizeNextLeafAndUpdate(NetworkActionCombination naCombination, Network n } else { optimizeLeaf(leaf); - // TODO Change this ugly behavior - virtualCostDetailed = getVirtualCostDetailed(leaf); - topLevelLogger.info("Optimized {}", virtualCostDetailed.isEmpty() ? leaf : leaf.toString().substring(0, leaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); - + logVirtualCostNamesIfPresent(topLevelLogger, leaf, "Optimized {}"); logVirtualCostInformation(leaf, "Optimized "); } } else { - - // TODO Change this ugly behavior - virtualCostDetailed = getVirtualCostDetailed(leaf); - topLevelLogger.info("Evaluated {}", virtualCostDetailed.isEmpty() ? leaf : leaf.toString().substring(0, leaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); + logVirtualCostNamesIfPresent(topLevelLogger, leaf, "Evaluated {}"); } updateOptimalLeaf(leaf, naCombination); } else { - - // TODO Change this ugly behavior - virtualCostDetailed = getVirtualCostDetailed(leaf); - topLevelLogger.info("Could not evaluate {}", virtualCostDetailed.isEmpty() ? leaf : leaf.toString().substring(0, leaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); + logVirtualCostNamesIfPresent(topLevelLogger, leaf, "Could not evaluate {}"); } } @@ -536,6 +514,10 @@ private boolean improvedEnough(Leaf leaf) { && (1 - Math.signum(previousDepthBestCost) * relativeImpact) * previousDepthBestCost > newCost; // enough relative impact } + /** + * For a given virtual-cost-name, if its associated virtual cost is positive, this method will return a map containing + * these information to be used in the Rao logs + */ public static Map getVirtualCostDetailed(ObjectiveFunctionResult objectiveFunctionResult) { return objectiveFunctionResult.getVirtualCostNames().stream() .filter(virtualCostName -> objectiveFunctionResult.getVirtualCost(virtualCostName) > 1e-6) @@ -543,6 +525,14 @@ public static Map getVirtualCostDetailed(ObjectiveFunctionResult name -> Math.round(objectiveFunctionResult.getVirtualCost(name) * 100.0) / 100.0)); } + /** + * Log virtual cost detail at end of the line if virtual cost > 0 + */ + private void logVirtualCostNamesIfPresent(FaraoLogger logger, Leaf leaf, String message){ + Map virtualCostDetailed = getVirtualCostDetailed(leaf); + logger.info(message, virtualCostDetailed.isEmpty() ? leaf : leaf.toString().substring(0, leaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); + } + /** * This method logs information about positive virtual costs */ From 2e304308afa816eb49eb59cba1dbabc4b01472e1 Mon Sep 17 00:00:00 2001 From: belthlemar Date: Thu, 16 Feb 2023 18:34:04 +0100 Subject: [PATCH 07/14] checkstyle + some tests fixed --- .../farao/search_tree_rao/commons/RaoLogger.java | 2 +- .../search_tree_rao/search_tree/algorithms/SearchTree.java | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java index c4578344ea..d87ed55f71 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java @@ -260,7 +260,7 @@ public static void logOptimizationSummary(FaraoLogger logger, State optimizedSta String raResult = getRaResult(networkActions, rangeActions, false); Map virtualCostDetailed = getVirtualCostDetailed(finalObjective); String initialCostString = initialFunctionalCost == null || initialVirtualCost == null ? "" : - String.format("initial cost = %s (functional: %s, virtual: %s %s), ", formatDouble(initialFunctionalCost + initialVirtualCost), formatDouble(initialFunctionalCost), formatDouble(initialVirtualCost), virtualCostDetailed.isEmpty() ? "" : virtualCostDetailed); + String.format("initial cost = %s (functional: %s, virtual: %s%s), ", formatDouble(initialFunctionalCost + initialVirtualCost), formatDouble(initialFunctionalCost), formatDouble(initialVirtualCost), virtualCostDetailed.isEmpty() ? "" : " " + virtualCostDetailed); logger.info("Scenario \"{}\": {}{}, cost {} = {} (functional: {}, virtual: {})", scenarioName, initialCostString, raResult, OptimizationState.afterOptimizing(optimizedState), formatDouble(finalObjective.getCost()), formatDouble(finalObjective.getFunctionalCost()), formatDouble(finalObjective.getVirtualCost())); } 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 d5cd2850ef..e10a222af6 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 @@ -38,7 +38,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Function; -import java.util.logging.Logger; import java.util.stream.Collectors; import static com.farao_community.farao.commons.logs.FaraoLoggerProvider.*; @@ -528,7 +527,7 @@ public static Map getVirtualCostDetailed(ObjectiveFunctionResult /** * Log virtual cost detail at end of the line if virtual cost > 0 */ - private void logVirtualCostNamesIfPresent(FaraoLogger logger, Leaf leaf, String message){ + private void logVirtualCostNamesIfPresent(FaraoLogger logger, Leaf leaf, String message) { Map virtualCostDetailed = getVirtualCostDetailed(leaf); logger.info(message, virtualCostDetailed.isEmpty() ? leaf : leaf.toString().substring(0, leaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); } From 0fbb4d6696f5464ebfba10af432c5e54e638a115 Mon Sep 17 00:00:00 2001 From: belthlemar Date: Fri, 17 Feb 2023 11:10:14 +0100 Subject: [PATCH 08/14] remove method and include it in leaf.toString() --- .../castor/algorithm/AutomatonSimulator.java | 8 ++-- .../search_tree_rao/commons/RaoLogger.java | 19 ++++++-- .../search_tree/algorithms/Leaf.java | 12 +++--- .../search_tree/algorithms/SearchTree.java | 43 +++++-------------- .../search_tree/algorithms/LeafTest.java | 4 +- 5 files changed, 38 insertions(+), 48 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 407389dad2..abb9241194 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 @@ -119,7 +119,7 @@ AutomatonPerimeterResultImpl simulateAutomatonState(State automatonState, State automatonState); failedAutomatonPerimeterResultImpl.setComputationStatus(ComputationStatus.FAILURE); TECHNICAL_LOGS.info("Automaton state {} has failed during sensitivity computation during range automaton simulation.", automatonState.getId()); - RaoLogger.logFailedOptimizationSummary(BUSINESS_LOGS, automatonState, failedAutomatonPerimeterResultImpl.getActivatedNetworkActions(), getRangeActionsAppliedOnState(failedAutomatonPerimeterResultImpl, automatonState)); + RaoLogger.logFailedOptimizationSummary(BUSINESS_LOGS, automatonState, failedAutomatonPerimeterResultImpl.getActivatedNetworkActions(), getRangeActionsAndTheirTapsAppliedOnState(failedAutomatonPerimeterResultImpl, automatonState)); return failedAutomatonPerimeterResultImpl; } // Build and return optimization result @@ -131,7 +131,7 @@ AutomatonPerimeterResultImpl simulateAutomatonState(State automatonState, State rangeAutomatonSimulationResult.getRangeActionsWithSetpoint(), automatonState); TECHNICAL_LOGS.info("Automaton state {} has been optimized.", automatonState.getId()); - RaoLogger.logOptimizationSummary(BUSINESS_LOGS, automatonState, automatonPerimeterResultImpl.getActivatedNetworkActions(), getRangeActionsAppliedOnState(automatonPerimeterResultImpl, automatonState), null, null, automatonPerimeterResultImpl); + RaoLogger.logOptimizationSummary(BUSINESS_LOGS, automatonState, automatonPerimeterResultImpl.getActivatedNetworkActions(), getRangeActionsAndTheirTapsAppliedOnState(automatonPerimeterResultImpl, automatonState), null, null, automatonPerimeterResultImpl); return automatonPerimeterResultImpl; } @@ -144,7 +144,7 @@ private PrePerimeterSensitivityAnalysis getPreAutoPerimeterSensitivityAnalysis(S return new PrePerimeterSensitivityAnalysis(flowCnecsInSensi, rangeActionsInSensi, raoParameters, toolProvider); } - public static Map, Double> getRangeActionsAppliedOnState(OptimizationResult optimizationResult, State state) { + public static Map, Double> getRangeActionsAndTheirTapsAppliedOnState(OptimizationResult optimizationResult, State state) { Set< RangeAction> setActivatedRangeActions = optimizationResult.getActivatedRangeActions(state); Map, Double> allRangeActions = new HashMap<>(); setActivatedRangeActions.stream().filter(PstRangeAction.class::isInstance).map(PstRangeAction.class::cast).forEach(pstRangeAction -> allRangeActions.put(pstRangeAction, (double) optimizationResult.getOptimizedTap(pstRangeAction, state))); @@ -161,7 +161,7 @@ AutomatonPerimeterResultImpl createFailedAutomatonPerimeterResult(State autoStat autoState); failedAutomatonPerimeterResultImpl.setComputationStatus(ComputationStatus.FAILURE); TECHNICAL_LOGS.info("Automaton state {} has failed during sensitivity computation {} topological automaton simulation.", autoState.getId(), defineMoment); - RaoLogger.logFailedOptimizationSummary(BUSINESS_LOGS, autoState, failedAutomatonPerimeterResultImpl.getActivatedNetworkActions(), getRangeActionsAppliedOnState(failedAutomatonPerimeterResultImpl, autoState)); + RaoLogger.logFailedOptimizationSummary(BUSINESS_LOGS, autoState, failedAutomatonPerimeterResultImpl.getActivatedNetworkActions(), getRangeActionsAndTheirTapsAppliedOnState(failedAutomatonPerimeterResultImpl, autoState)); return failedAutomatonPerimeterResultImpl; } diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java index d87ed55f71..f1d1cc5728 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java @@ -29,10 +29,10 @@ import org.apache.commons.lang3.StringUtils; import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; import static com.farao_community.farao.commons.logs.FaraoLoggerProvider.BUSINESS_LOGS; -import static com.farao_community.farao.search_tree_rao.search_tree.algorithms.SearchTree.getVirtualCostDetailed; import static java.lang.String.format; /** @@ -260,9 +260,9 @@ public static void logOptimizationSummary(FaraoLogger logger, State optimizedSta String raResult = getRaResult(networkActions, rangeActions, false); Map virtualCostDetailed = getVirtualCostDetailed(finalObjective); String initialCostString = initialFunctionalCost == null || initialVirtualCost == null ? "" : - String.format("initial cost = %s (functional: %s, virtual: %s%s), ", formatDouble(initialFunctionalCost + initialVirtualCost), formatDouble(initialFunctionalCost), formatDouble(initialVirtualCost), virtualCostDetailed.isEmpty() ? "" : " " + virtualCostDetailed); - logger.info("Scenario \"{}\": {}{}, cost {} = {} (functional: {}, virtual: {})", scenarioName, initialCostString, raResult, OptimizationState.afterOptimizing(optimizedState), - formatDouble(finalObjective.getCost()), formatDouble(finalObjective.getFunctionalCost()), formatDouble(finalObjective.getVirtualCost())); + String.format("initial cost = %s (functional: %s, virtual: %s), ", formatDouble(initialFunctionalCost + initialVirtualCost), formatDouble(initialFunctionalCost), formatDouble(initialVirtualCost)); + logger.info("Scenario \"{}\": {}{}, cost {} = {} (functional: {}, virtual: {}{})", scenarioName, initialCostString, raResult, OptimizationState.afterOptimizing(optimizedState), + formatDouble(finalObjective.getCost()), formatDouble(finalObjective.getFunctionalCost()), formatDouble(finalObjective.getVirtualCost()), virtualCostDetailed.isEmpty() ? "" : " " + virtualCostDetailed); } public static String getRaResult(Set networkActions, Map, java.lang.Double> rangeActions, boolean failed) { @@ -300,4 +300,15 @@ public static String formatDouble(double value) { return String.format(Locale.ENGLISH, "%.2f", value); } } + + /** + * For a given virtual-cost-name, if its associated virtual cost is positive, this method will return a map containing + * these information to be used in the Rao logs + */ + public static Map getVirtualCostDetailed(ObjectiveFunctionResult objectiveFunctionResult) { + return objectiveFunctionResult.getVirtualCostNames().stream() + .filter(virtualCostName -> objectiveFunctionResult.getVirtualCost(virtualCostName) > 1e-6) + .collect(Collectors.toMap(Function.identity(), + name -> Math.round(objectiveFunctionResult.getVirtualCost(name) * 100.0) / 100.0)); + } } diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/Leaf.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/Leaf.java index 57b735dea9..ad8cf6f08f 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/Leaf.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/Leaf.java @@ -42,6 +42,7 @@ import static com.farao_community.farao.commons.logs.FaraoLoggerProvider.BUSINESS_WARNS; import static com.farao_community.farao.commons.logs.FaraoLoggerProvider.TECHNICAL_LOGS; +import static com.farao_community.farao.search_tree_rao.commons.RaoLogger.getVirtualCostDetailed; /** * A "leaf" is a node of the search tree. @@ -302,14 +303,13 @@ public String toString() { if (status.equals(Status.OPTIMIZED)) { long nRangeActions = getNumberOfActivatedRangeActions(); info += String.format(", %s range action(s) activated", nRangeActions > 0 ? nRangeActions : "no"); + } + if (status.equals(Status.EVALUATED) || status.equals(Status.OPTIMIZED)) { + Map virtualCostDetailed = getVirtualCostDetailed(this); info += String.format(Locale.ENGLISH, ", cost: %.2f", getCost()); info += String.format(Locale.ENGLISH, " (functional: %.2f", getFunctionalCost()); - info += String.format(Locale.ENGLISH, ", virtual: %.2f)", getVirtualCost()); - } else if (status.equals(Status.EVALUATED)) { - info += ", range actions have not been optimized"; - info += String.format(Locale.ENGLISH, ", cost: %.2f", getCost()); - info += String.format(Locale.ENGLISH, " (functional: %.2f", getFunctionalCost()); - info += String.format(Locale.ENGLISH, ", virtual: %.2f)", getVirtualCost()); + info += String.format(Locale.ENGLISH, ", virtual: %.2f%s)", getVirtualCost(), + virtualCostDetailed.isEmpty() ? "" : " " + virtualCostDetailed); } return info; } 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 e10a222af6..5bfdc061df 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 @@ -20,7 +20,6 @@ import com.farao_community.farao.search_tree_rao.commons.optimization_perimeters.CurativeOptimizationPerimeter; import com.farao_community.farao.search_tree_rao.commons.optimization_perimeters.OptimizationPerimeter; import com.farao_community.farao.search_tree_rao.commons.parameters.TreeParameters; -import com.farao_community.farao.search_tree_rao.result.api.ObjectiveFunctionResult; import com.farao_community.farao.search_tree_rao.result.api.OptimizationResult; import com.farao_community.farao.search_tree_rao.result.api.PrePerimeterResult; import com.farao_community.farao.search_tree_rao.search_tree.inputs.SearchTreeInput; @@ -37,11 +36,10 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.Function; import java.util.stream.Collectors; import static com.farao_community.farao.commons.logs.FaraoLoggerProvider.*; -import static com.farao_community.farao.search_tree_rao.castor.algorithm.AutomatonSimulator.getRangeActionsAppliedOnState; +import static com.farao_community.farao.search_tree_rao.castor.algorithm.AutomatonSimulator.getRangeActionsAndTheirTapsAppliedOnState; /** * The "tree" is one of the core object of the search-tree algorithm. @@ -144,13 +142,13 @@ public CompletableFuture run() { return CompletableFuture.completedFuture(rootLeaf); } - logVirtualCostNamesIfPresent(TECHNICAL_LOGS, rootLeaf, "{}"); + TECHNICAL_LOGS.info("{}", rootLeaf); RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, rootLeaf, parameters.getObjectiveFunction(), NUMBER_LOGGED_ELEMENTS_DURING_TREE); TECHNICAL_LOGS.info("Linear optimization on root leaf"); optimizeLeaf(rootLeaf); - logVirtualCostNamesIfPresent(topLevelLogger, rootLeaf, "{}"); + topLevelLogger.info("{}", rootLeaf); RaoLogger.logRangeActions(TECHNICAL_LOGS, optimalLeaf, input.getOptimizationPerimeter(), null); RaoLogger.logMostLimitingElementsResults(topLevelLogger, optimalLeaf, parameters.getObjectiveFunction(), NUMBER_LOGGED_ELEMENTS_DURING_TREE); logVirtualCostInformation(rootLeaf, ""); @@ -164,7 +162,7 @@ public CompletableFuture run() { TECHNICAL_LOGS.info("Search-tree RAO completed with status {}", optimalLeaf.getSensitivityStatus()); - logVirtualCostNamesIfPresent(TECHNICAL_LOGS, optimalLeaf, "Best leaf: {}"); + TECHNICAL_LOGS.info("Best leaf: {}", optimalLeaf); RaoLogger.logRangeActions(TECHNICAL_LOGS, optimalLeaf, input.getOptimizationPerimeter(), "Best leaf: "); RaoLogger.logMostLimitingElementsResults(TECHNICAL_LOGS, optimalLeaf, parameters.getObjectiveFunction(), NUMBER_LOGGED_ELEMENTS_END_TREE); @@ -208,7 +206,7 @@ private void applyForcedNetworkActionsOnRootLeaf() { private void logOptimizationSummary(Leaf leaf) { State state = input.getOptimizationPerimeter().getMainOptimizationState(); - RaoLogger.logOptimizationSummary(BUSINESS_LOGS, state, leaf.getActivatedNetworkActions(), getRangeActionsAppliedOnState(leaf, state), preOptimFunctionalCost, preOptimVirtualCost, leaf); + RaoLogger.logOptimizationSummary(BUSINESS_LOGS, state, leaf.getActivatedNetworkActions(), getRangeActionsAndTheirTapsAppliedOnState(leaf, state), preOptimFunctionalCost, preOptimVirtualCost, leaf); logVirtualCostInformation(leaf, ""); } @@ -231,8 +229,7 @@ private void iterateOnTree() { if (hasImproved) { TECHNICAL_LOGS.info("Search depth {} [end]", depth + 1); - String strDepth = String.valueOf(depth + 1); - logVirtualCostNamesIfPresent(topLevelLogger, optimalLeaf, "Search depth " + strDepth + " best leaf: {}"); + topLevelLogger.info("Search depth {} best leaf: {}", depth + 1, optimalLeaf); RaoLogger.logRangeActions(TECHNICAL_LOGS, optimalLeaf, input.getOptimizationPerimeter(), String.format("Search depth %s best leaf: ", depth + 1)); RaoLogger.logMostLimitingElementsResults(topLevelLogger, optimalLeaf, parameters.getObjectiveFunction(), NUMBER_LOGGED_ELEMENTS_DURING_TREE); } else { @@ -376,8 +373,7 @@ void optimizeNextLeafAndUpdate(NetworkActionCombination naCombination, Network n // We evaluate the leaf with taking the results of the previous optimal leaf if we do not want to update some results leaf.evaluate(input.getObjectiveFunction(), getSensitivityComputerForEvaluation()); - logVirtualCostNamesIfPresent(topLevelLogger, leaf, "Evaluated {}"); - + topLevelLogger.info("Evaluated {}", leaf); if (!leaf.getStatus().equals(Leaf.Status.ERROR)) { if (!stopCriterionReached(leaf)) { if (combinationFulfillingStopCriterion.isPresent() && deterministicNetworkActionCombinationComparison(naCombination, combinationFulfillingStopCriterion.get()) > 0) { @@ -385,15 +381,15 @@ void optimizeNextLeafAndUpdate(NetworkActionCombination naCombination, Network n } else { optimizeLeaf(leaf); - logVirtualCostNamesIfPresent(topLevelLogger, leaf, "Optimized {}"); + topLevelLogger.info("Optimized {}", leaf); logVirtualCostInformation(leaf, "Optimized "); } } else { - logVirtualCostNamesIfPresent(topLevelLogger, leaf, "Evaluated {}"); + topLevelLogger.info("Optimized {}", leaf); } updateOptimalLeaf(leaf, naCombination); } else { - logVirtualCostNamesIfPresent(topLevelLogger, leaf, "Could not evaluate {}"); + topLevelLogger.info("Could not evaluate {}", leaf); } } @@ -513,25 +509,6 @@ private boolean improvedEnough(Leaf leaf) { && (1 - Math.signum(previousDepthBestCost) * relativeImpact) * previousDepthBestCost > newCost; // enough relative impact } - /** - * For a given virtual-cost-name, if its associated virtual cost is positive, this method will return a map containing - * these information to be used in the Rao logs - */ - public static Map getVirtualCostDetailed(ObjectiveFunctionResult objectiveFunctionResult) { - return objectiveFunctionResult.getVirtualCostNames().stream() - .filter(virtualCostName -> objectiveFunctionResult.getVirtualCost(virtualCostName) > 1e-6) - .collect(Collectors.toMap(Function.identity(), - name -> Math.round(objectiveFunctionResult.getVirtualCost(name) * 100.0) / 100.0)); - } - - /** - * Log virtual cost detail at end of the line if virtual cost > 0 - */ - private void logVirtualCostNamesIfPresent(FaraoLogger logger, Leaf leaf, String message) { - Map virtualCostDetailed = getVirtualCostDetailed(leaf); - logger.info(message, virtualCostDetailed.isEmpty() ? leaf : leaf.toString().substring(0, leaf.toString().length() - 1) + " " + virtualCostDetailed + ")"); - } - /** * This method logs information about positive virtual costs */ diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/LeafTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/LeafTest.java index 75cd5167b8..5b9810344e 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/LeafTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/LeafTest.java @@ -788,6 +788,8 @@ public void testToStringOnRootLeaf() { when(linearOptimizationResult.getFunctionalCost()).thenReturn(-160.); when(linearOptimizationResult.getVirtualCost()).thenReturn(59.5); - assertEquals("Root leaf, no range action(s) activated, cost: -100.50 (functional: -160.00, virtual: 59.50)", leaf.toString()); + // TODO Refacto ce test + assertEquals(Leaf.Status.OPTIMIZED, leaf.getStatus()); + //assertEquals("Root leaf, no range action(s) activated, cost: -100.50 (functional: -160.00, virtual: 59.50)", leaf.toString()); } } From 75bd870e82bb4d9b4290f8bbad2b89bec3a5acea Mon Sep 17 00:00:00 2001 From: belthlemar Date: Fri, 17 Feb 2023 15:50:18 +0100 Subject: [PATCH 09/14] fix toString() method and test --- .../search_tree/algorithms/Leaf.java | 9 ++++++++- .../search_tree/algorithms/LeafTest.java | 13 ++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/Leaf.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/Leaf.java index ad8cf6f08f..2eed4f86ff 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/Leaf.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/Leaf.java @@ -18,6 +18,7 @@ import com.farao_community.farao.data.crac_api.range_action.RangeAction; import com.farao_community.farao.data.rao_result_api.ComputationStatus; import com.farao_community.farao.search_tree_rao.commons.NetworkActionCombination; +import com.farao_community.farao.search_tree_rao.commons.RaoLogger; import com.farao_community.farao.search_tree_rao.commons.SensitivityComputer; import com.farao_community.farao.search_tree_rao.commons.objective_function_evaluator.ObjectiveFunction; import com.farao_community.farao.search_tree_rao.commons.optimization_perimeters.CurativeOptimizationPerimeter; @@ -417,7 +418,13 @@ public double getVirtualCost() { @Override public Set getVirtualCostNames() { - return preOptimObjectiveFunctionResult.getVirtualCostNames(); + if (status == Status.EVALUATED) { + return preOptimObjectiveFunctionResult.getVirtualCostNames(); + } else if (status == Status.OPTIMIZED) { + return postOptimResult.getVirtualCostNames(); + } else { + throw new FaraoException(NO_RESULTS_AVAILABLE); + } } @Override diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/LeafTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/LeafTest.java index 5b9810344e..43020a6034 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/LeafTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/LeafTest.java @@ -786,10 +786,17 @@ public void testToStringOnRootLeaf() { leaf.optimize(searchTreeInput, searchTreeParameters); when(linearOptimizationResult.getCost()).thenReturn(-100.5); when(linearOptimizationResult.getFunctionalCost()).thenReturn(-160.); + // With virtual cost when(linearOptimizationResult.getVirtualCost()).thenReturn(59.5); + when(linearOptimizationResult.getVirtualCostNames()).thenReturn(Set.of("mnec-violation-cost", "loopflow-violation-cost")); + when(linearOptimizationResult.getVirtualCost("mnec-violation-cost")).thenReturn(42.2); + when(linearOptimizationResult.getVirtualCost("loopflow-violation-cost")).thenReturn(17.3); + assertEquals("Root leaf, no range action(s) activated, cost: -100.50 (functional: -160.00, virtual: 59.50 {mnec-violation-cost=42.2, loopflow-violation-cost=17.3})", leaf.toString()); + // Without virtual cost + when(linearOptimizationResult.getVirtualCost()).thenReturn(0.); + when(linearOptimizationResult.getVirtualCost("mnec-violation-cost")).thenReturn(0.); + when(linearOptimizationResult.getVirtualCost("loopflow-violation-cost")).thenReturn(0.); + assertEquals("Root leaf, no range action(s) activated, cost: -160.00 (functional: -160.00, virtual: 0.00)", leaf.toString()); - // TODO Refacto ce test - assertEquals(Leaf.Status.OPTIMIZED, leaf.getStatus()); - //assertEquals("Root leaf, no range action(s) activated, cost: -100.50 (functional: -160.00, virtual: 59.50)", leaf.toString()); } } From c71d2d594a80ab1111a22b889534c26dd6142a7b Mon Sep 17 00:00:00 2001 From: belthlemar Date: Fri, 17 Feb 2023 17:41:59 +0100 Subject: [PATCH 10/14] add tests to cover new behavior --- .../castor/algorithm/AutomatonSimulator.java | 2 +- .../search_tree_rao/commons/RaoLogger.java | 21 ++-- .../search_tree/algorithms/Leaf.java | 5 +- .../search_tree/algorithms/SearchTree.java | 2 +- .../commons/RaoLoggerTest.java | 110 +++++++++++++----- .../algorithms/SearchTreeTest.java | 11 +- 6 files changed, 106 insertions(+), 45 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 abb9241194..17e2ef22e4 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 @@ -131,7 +131,7 @@ AutomatonPerimeterResultImpl simulateAutomatonState(State automatonState, State rangeAutomatonSimulationResult.getRangeActionsWithSetpoint(), automatonState); TECHNICAL_LOGS.info("Automaton state {} has been optimized.", automatonState.getId()); - RaoLogger.logOptimizationSummary(BUSINESS_LOGS, automatonState, automatonPerimeterResultImpl.getActivatedNetworkActions(), getRangeActionsAndTheirTapsAppliedOnState(automatonPerimeterResultImpl, automatonState), null, null, automatonPerimeterResultImpl); + RaoLogger.logOptimizationSummary(BUSINESS_LOGS, automatonState, automatonPerimeterResultImpl.getActivatedNetworkActions(), getRangeActionsAndTheirTapsAppliedOnState(automatonPerimeterResultImpl, automatonState), null, automatonPerimeterResultImpl); return automatonPerimeterResultImpl; } diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java index f1d1cc5728..82a453703a 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java @@ -251,21 +251,22 @@ private static Map getMostLimitingElementsAndMargins(Optimizat public static void logFailedOptimizationSummary(FaraoLogger logger, State optimizedState, Set networkActions, Map, java.lang.Double> rangeActions) { String scenarioName = getScenarioName(optimizedState); - String raResult = getRaResult(networkActions, rangeActions, true); + String raResult = getRaResult(networkActions, rangeActions); logger.info("Scenario \"{}\": {}", scenarioName, raResult); } - public static void logOptimizationSummary(FaraoLogger logger, State optimizedState, Set networkActions, Map, java.lang.Double> rangeActions, Double initialFunctionalCost, Double initialVirtualCost, ObjectiveFunctionResult finalObjective) { + public static void logOptimizationSummary(FaraoLogger logger, State optimizedState, Set networkActions, Map, java.lang.Double> rangeActions, ObjectiveFunctionResult preOptimObjectiveFunctionResult, ObjectiveFunctionResult finalObjective) { String scenarioName = getScenarioName(optimizedState); - String raResult = getRaResult(networkActions, rangeActions, false); + String raResult = getRaResult(networkActions, rangeActions); Map virtualCostDetailed = getVirtualCostDetailed(finalObjective); - String initialCostString = initialFunctionalCost == null || initialVirtualCost == null ? "" : - String.format("initial cost = %s (functional: %s, virtual: %s), ", formatDouble(initialFunctionalCost + initialVirtualCost), formatDouble(initialFunctionalCost), formatDouble(initialVirtualCost)); + String initialCostString = preOptimObjectiveFunctionResult == null ? "" : + String.format("initial cost = %s (functional: %s, virtual: %s%s), ", formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost() + preOptimObjectiveFunctionResult.getVirtualCost()), formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost()), formatDouble(preOptimObjectiveFunctionResult.getVirtualCost()), + virtualCostDetailed.isEmpty() ? "" : " " + virtualCostDetailed); logger.info("Scenario \"{}\": {}{}, cost {} = {} (functional: {}, virtual: {}{})", scenarioName, initialCostString, raResult, OptimizationState.afterOptimizing(optimizedState), formatDouble(finalObjective.getCost()), formatDouble(finalObjective.getFunctionalCost()), formatDouble(finalObjective.getVirtualCost()), virtualCostDetailed.isEmpty() ? "" : " " + virtualCostDetailed); } - public static String getRaResult(Set networkActions, Map, java.lang.Double> rangeActions, boolean failed) { + public static String getRaResult(Set networkActions, Map, java.lang.Double> rangeActions) { long activatedNetworkActions = networkActions.size(); long activatedRangeActions = rangeActions.size(); String networkActionsNames = StringUtils.join(networkActions.stream().map(Identifiable::getName).collect(Collectors.toSet()), ", "); @@ -277,12 +278,12 @@ public static String getRaResult(Set networkActions, Map 0 && activatedRangeActions == 0) { - return String.format("%s network action(s) activated", activatedNetworkActions) + (failed ? "" : " : " + networkActionsNames); + return String.format("%s network action(s) activated : %s", activatedNetworkActions, networkActionsNames); } else if (activatedRangeActions > 0 && activatedNetworkActions == 0) { - return String.format("%s range action(s) activated", activatedRangeActions) + (failed ? "" : " : " + rangeActionsNames); + return String.format("%s range action(s) activated : %s", activatedRangeActions, rangeActionsNames); } else { - return String.format("%s network action(s) and %s range action(s) activated", activatedNetworkActions, activatedRangeActions) + - (failed ? "" : " : " + networkActionsNames + " and " + rangeActionsNames); + return String.format("%s network action(s) and %s range action(s) activated : %s and %s", + activatedNetworkActions, activatedRangeActions, networkActionsNames, rangeActionsNames); } } diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/Leaf.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/Leaf.java index 2eed4f86ff..d60e7e125b 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/Leaf.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/Leaf.java @@ -18,7 +18,6 @@ import com.farao_community.farao.data.crac_api.range_action.RangeAction; import com.farao_community.farao.data.rao_result_api.ComputationStatus; import com.farao_community.farao.search_tree_rao.commons.NetworkActionCombination; -import com.farao_community.farao.search_tree_rao.commons.RaoLogger; import com.farao_community.farao.search_tree_rao.commons.SensitivityComputer; import com.farao_community.farao.search_tree_rao.commons.objective_function_evaluator.ObjectiveFunction; import com.farao_community.farao.search_tree_rao.commons.optimization_perimeters.CurativeOptimizationPerimeter; @@ -133,6 +132,10 @@ public FlowResult getPreOptimBranchResult() { return preOptimFlowResult; } + public ObjectiveFunctionResult getPreOptimObjectiveFunctionResult() { + return preOptimObjectiveFunctionResult; + } + public Status getStatus() { return status; } 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 5bfdc061df..708f26d512 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 @@ -206,7 +206,7 @@ private void applyForcedNetworkActionsOnRootLeaf() { private void logOptimizationSummary(Leaf leaf) { State state = input.getOptimizationPerimeter().getMainOptimizationState(); - RaoLogger.logOptimizationSummary(BUSINESS_LOGS, state, leaf.getActivatedNetworkActions(), getRangeActionsAndTheirTapsAppliedOnState(leaf, state), preOptimFunctionalCost, preOptimVirtualCost, leaf); + RaoLogger.logOptimizationSummary(BUSINESS_LOGS, state, leaf.getActivatedNetworkActions(), getRangeActionsAndTheirTapsAppliedOnState(leaf, state), leaf.getPreOptimObjectiveFunctionResult(), leaf); logVirtualCostInformation(leaf, ""); } diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java index a8a3d8d279..770d76d21e 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java @@ -20,6 +20,8 @@ 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.cnec.Side; +import com.farao_community.farao.data.crac_api.network_action.NetworkAction; +import com.farao_community.farao.data.crac_api.range_action.RangeAction; import com.farao_community.farao.rao_api.parameters.RaoParameters; import com.farao_community.farao.search_tree_rao.result.api.FlowResult; import com.farao_community.farao.search_tree_rao.result.api.ObjectiveFunctionResult; @@ -35,6 +37,7 @@ import static java.lang.String.format; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -290,67 +293,114 @@ private ListAppender registerLogs(Class clazz) { public void testLogOptimizationSummary() { State preventive = Mockito.mock(State.class); when(preventive.getInstant()).thenReturn(Instant.PREVENTIVE); - State curative = Mockito.mock(State.class); when(curative.getInstant()).thenReturn(Instant.CURATIVE); Contingency contingency = Mockito.mock(Contingency.class); when(contingency.getName()).thenReturn("contingency"); when(curative.getContingency()).thenReturn(Optional.of(contingency)); + FaraoLogger logger = FaraoLoggerProvider.BUSINESS_LOGS; + List logsList = registerLogs(RaoBusinessLogs.class).list; + // initial objective + ObjectiveFunctionResult initialObjectiveFunctionResult = Mockito.mock(ObjectiveFunctionResult.class); + when(initialObjectiveFunctionResult.getCost()).thenReturn(-200.); + when(initialObjectiveFunctionResult.getFunctionalCost()).thenReturn(-210.3); + when(initialObjectiveFunctionResult.getVirtualCost()).thenReturn(10.3); + when(initialObjectiveFunctionResult.getVirtualCostNames()).thenReturn(Set.of("sensi-fallback-cost")); + when(initialObjectiveFunctionResult.getVirtualCost("sensi-fallback-cost")).thenReturn(10.3); + // final objective when(objectiveFunctionResult.getCost()).thenReturn(-100.); when(objectiveFunctionResult.getFunctionalCost()).thenReturn(-150.); when(objectiveFunctionResult.getVirtualCost()).thenReturn(50.); + when(objectiveFunctionResult.getVirtualCostNames()).thenReturn(Set.of("mnec-violation-cost", "loopflow-violation-cost")); + when(objectiveFunctionResult.getVirtualCost("mnec-violation-cost")).thenReturn(42.2); + when(objectiveFunctionResult.getVirtualCost("loopflow-violation-cost")).thenReturn(7.8); + + // Create Remedial actions + NetworkAction fakeRA = Mockito.mock(NetworkAction.class); + when(fakeRA.getName()).thenReturn("Open_fake_RA"); + Set networkActions = Set.of(fakeRA); + Map, Double> rangeActions = new HashMap<>(); + RangeAction fakePST1 = Mockito.mock(RangeAction.class); + RangeAction fakePST2 = Mockito.mock(RangeAction.class); + when(fakePST1.getName()).thenReturn("PST_1"); + when(fakePST2.getName()).thenReturn("PST_2"); + rangeActions.put(fakePST1, -2.); + rangeActions.put(fakePST2, 4.); + + RaoLogger.logOptimizationSummary(logger, preventive, networkActions, rangeActions, initialObjectiveFunctionResult, objectiveFunctionResult); + assertEquals("[INFO] Scenario \"preventive\": initial cost = -200.00 (functional: -210.30, virtual: 10.30 {mnec-violation-cost=42.2, loopflow-violation-cost=7.8})," + + " 1 network action(s) and 2 range action(s) activated : Open_fake_RA and PST_2: 4, PST_1: -2," + + " cost after PRA = -100.00 (functional: -150.00, virtual: 50.00 {mnec-violation-cost=42.2, loopflow-violation-cost=7.8})", logsList.get(logsList.size() - 1).toString()); + + // Remove virtual cost for visibility + when(initialObjectiveFunctionResult.getCost()).thenReturn(-200.); + when(initialObjectiveFunctionResult.getFunctionalCost()).thenReturn(-200.); + when(initialObjectiveFunctionResult.getVirtualCost()).thenReturn(0.); + when(initialObjectiveFunctionResult.getVirtualCost("sensi-fallback-cost")).thenReturn(0.); + when(objectiveFunctionResult.getCost()).thenReturn(-100.); + when(objectiveFunctionResult.getFunctionalCost()).thenReturn(-100.); + when(objectiveFunctionResult.getVirtualCost()).thenReturn(0.); + when(objectiveFunctionResult.getVirtualCostNames()).thenReturn(Set.of("mnec-violation-cost", "loopflow-violation-cost")); + when(objectiveFunctionResult.getVirtualCost("mnec-violation-cost")).thenReturn(0.); + when(objectiveFunctionResult.getVirtualCost("loopflow-violation-cost")).thenReturn(0.); - FaraoLogger logger = FaraoLoggerProvider.BUSINESS_LOGS; - List logsList = registerLogs(RaoBusinessLogs.class).list; - } -} - /* - RaoLogger.logOptimizationSummary(logger, preventive, 0, 0, 1., 2., objectiveFunctionResult); - assertEquals("[INFO] Scenario \"preventive\": initial cost = 3.00 (functional: 1.00, virtual: 2.00), no remedial actions activated, cost after PRA = -100.00 (functional: -150.00, virtual: 50.00)", logsList.get(logsList.size() - 1).toString()); + RaoLogger.logOptimizationSummary(logger, curative, Collections.emptySet(), rangeActions, initialObjectiveFunctionResult, objectiveFunctionResult); + assertEquals("[INFO] Scenario \"contingency\": initial cost = -200.00 (functional: -200.00, virtual: 0.00)," + + " 2 range action(s) activated : PST_2: 4, PST_1: -2, cost after CRA = -100.00 (functional: -100.00, virtual: 0.00)", logsList.get(logsList.size() - 1).toString()); - RaoLogger.logOptimizationSummary(logger, curative, 1, 0, -100., 40., objectiveFunctionResult); - assertEquals("[INFO] Scenario \"contingency\": initial cost = -60.00 (functional: -100.00, virtual: 40.00), 1 network action(s) activated, cost after CRA = -100.00 (functional: -150.00, virtual: 50.00)", logsList.get(logsList.size() - 1).toString()); + RaoLogger.logOptimizationSummary(logger, preventive, Collections.emptySet(), Collections.emptyMap(), initialObjectiveFunctionResult, objectiveFunctionResult); + assertEquals("[INFO] Scenario \"preventive\": initial cost = -200.00 (functional: -200.00, virtual: 0.00)," + + " no remedial actions activated, cost after PRA = -100.00 (functional: -100.00, virtual: 0.00)", logsList.get(logsList.size() - 1).toString()); - RaoLogger.logOptimizationSummary(logger, curative, 0, 2, 1., null, objectiveFunctionResult); - assertEquals("[INFO] Scenario \"contingency\": 2 range action(s) activated, cost after CRA = -100.00 (functional: -150.00, virtual: 50.00)", logsList.get(logsList.size() - 1).toString()); + RaoLogger.logOptimizationSummary(logger, preventive, networkActions, Collections.emptyMap(), initialObjectiveFunctionResult, objectiveFunctionResult); + assertEquals("[INFO] Scenario \"preventive\": initial cost = -200.00 (functional: -200.00, virtual: 0.00)," + + " 1 network action(s) activated : Open_fake_RA, cost after PRA = -100.00 (functional: -100.00, virtual: 0.00)", logsList.get(logsList.size() - 1).toString()); - RaoLogger.logOptimizationSummary(logger, curative, 3, 2, null, 200., objectiveFunctionResult); - assertEquals("[INFO] Scenario \"contingency\": 3 network action(s) and 2 range action(s) activated, cost after CRA = -100.00 (functional: -150.00, virtual: 50.00)", logsList.get(logsList.size() - 1).toString()); + RaoLogger.logOptimizationSummary(logger, preventive, Collections.emptySet(), Collections.emptyMap(), null, objectiveFunctionResult); + assertEquals("[INFO] Scenario \"preventive\":" + + " no remedial actions activated, cost after PRA = -100.00 (functional: -100.00, virtual: 0.00)", logsList.get(logsList.size() - 1).toString()); - // TODO REFACTO CES TESTS + assertThrows(java.lang.NullPointerException.class, () -> RaoLogger.logOptimizationSummary(logger, preventive, Collections.emptySet(), Collections.emptyMap(), initialObjectiveFunctionResult, null)); } @Test public void testLogFailedOptimizationSummary() { State preventive = Mockito.mock(State.class); when(preventive.getInstant()).thenReturn(Instant.PREVENTIVE); - State curative = Mockito.mock(State.class); when(curative.getInstant()).thenReturn(Instant.CURATIVE); Contingency contingency = Mockito.mock(Contingency.class); when(contingency.getName()).thenReturn("contingency"); when(curative.getContingency()).thenReturn(Optional.of(contingency)); - - when(objectiveFunctionResult.getCost()).thenReturn(-100.); - when(objectiveFunctionResult.getFunctionalCost()).thenReturn(-150.); - when(objectiveFunctionResult.getVirtualCost()).thenReturn(50.); - FaraoLogger logger = FaraoLoggerProvider.BUSINESS_LOGS; List logsList = registerLogs(RaoBusinessLogs.class).list; - RaoLogger.logFailedOptimizationSummary(logger, preventive, 0, 0); + // Create Objective Function + ObjectiveFunctionResult initialObjectiveFunctionResult = Mockito.mock(ObjectiveFunctionResult.class); + + // Create Remedial actions + NetworkAction fakeRA = Mockito.mock(NetworkAction.class); + when(fakeRA.getName()).thenReturn("Open_fake_RA"); + Set networkActions = Set.of(fakeRA); + Map, Double> rangeActions = new HashMap<>(); + RangeAction fakePST1 = Mockito.mock(RangeAction.class); + RangeAction fakePST2 = Mockito.mock(RangeAction.class); + when(fakePST1.getName()).thenReturn("PST_1"); + when(fakePST2.getName()).thenReturn("PST_2"); + rangeActions.put(fakePST1, -2.); + rangeActions.put(fakePST2, 4.); + + RaoLogger.logFailedOptimizationSummary(logger, preventive, Collections.emptySet(), Collections.emptyMap()); assertEquals("[INFO] Scenario \"preventive\": no remedial actions activated", logsList.get(logsList.size() - 1).toString()); - RaoLogger.logFailedOptimizationSummary(logger, curative, 1, 0); - assertEquals("[INFO] Scenario \"contingency\": 1 network action(s) activated", logsList.get(logsList.size() - 1).toString()); + RaoLogger.logFailedOptimizationSummary(logger, curative, networkActions, Collections.emptyMap()); + assertEquals("[INFO] Scenario \"contingency\": 1 network action(s) activated : Open_fake_RA", logsList.get(logsList.size() - 1).toString()); - RaoLogger.logFailedOptimizationSummary(logger, curative, 0, 2); - assertEquals("[INFO] Scenario \"contingency\": 2 range action(s) activated", logsList.get(logsList.size() - 1).toString()); + RaoLogger.logFailedOptimizationSummary(logger, curative, Collections.emptySet(), rangeActions); + assertEquals("[INFO] Scenario \"contingency\": 2 range action(s) activated : PST_2: 4, PST_1: -2", logsList.get(logsList.size() - 1).toString()); - RaoLogger.logFailedOptimizationSummary(logger, curative, 3, 2); - assertEquals("[INFO] Scenario \"contingency\": 3 network action(s) and 2 range action(s) activated", logsList.get(logsList.size() - 1).toString()); + RaoLogger.logFailedOptimizationSummary(logger, curative, networkActions, rangeActions); + assertEquals("[INFO] Scenario \"contingency\": 1 network action(s) and 2 range action(s) activated : Open_fake_RA and PST_2: 4, PST_1: -2", logsList.get(logsList.size() - 1).toString()); } } - - */ diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/SearchTreeTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/SearchTreeTest.java index 2bb3cd4c75..bb658aaa5b 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/SearchTreeTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/SearchTreeTest.java @@ -31,6 +31,7 @@ import com.farao_community.farao.search_tree_rao.commons.parameters.GlobalRemedialActionLimitationParameters; import com.farao_community.farao.search_tree_rao.commons.parameters.NetworkActionParameters; import com.farao_community.farao.search_tree_rao.commons.parameters.TreeParameters; +import com.farao_community.farao.search_tree_rao.result.api.ObjectiveFunctionResult; import com.farao_community.farao.search_tree_rao.result.api.OptimizationResult; import com.farao_community.farao.search_tree_rao.result.api.PrePerimeterResult; import com.farao_community.farao.search_tree_rao.search_tree.inputs.SearchTreeInput; @@ -447,7 +448,10 @@ public void testLogsVerbose() { when(rootLeaf.getStatus()).thenReturn(Leaf.Status.ERROR); when(rootLeaf.toString()).thenReturn("root leaf description"); Mockito.doReturn(rootLeaf).when(searchTree).makeLeaf(optimizationPerimeter, network, prePerimeterResult, appliedRemedialActions); - + ObjectiveFunctionResult initialResult = Mockito.mock(ObjectiveFunctionResult.class); + when(initialResult.getFunctionalCost()).thenReturn(0.); + when(initialResult.getVirtualCost()).thenReturn(0.); + when(rootLeaf.getPreOptimObjectiveFunctionResult()).thenReturn(initialResult); String expectedLog1 = "[INFO] Evaluating root leaf"; String expectedLog2 = "[INFO] Could not evaluate leaf: root leaf description"; String expectedLog3 = "[INFO] Scenario \"preventive\": initial cost = 0.00 (functional: 0.00, virtual: 0.00), no remedial actions activated, cost after PRA = 0.00 (functional: 0.00, virtual: 0.00)"; @@ -470,7 +474,10 @@ public void testLogsDontVerbose() { when(rootLeaf.getStatus()).thenReturn(Leaf.Status.ERROR); when(rootLeaf.toString()).thenReturn("root leaf description"); Mockito.doReturn(rootLeaf).when(searchTree).makeLeaf(optimizationPerimeter, network, prePerimeterResult, appliedRemedialActions); - + ObjectiveFunctionResult initialResult = Mockito.mock(ObjectiveFunctionResult.class); + when(initialResult.getFunctionalCost()).thenReturn(0.); + when(initialResult.getVirtualCost()).thenReturn(0.); + when(rootLeaf.getPreOptimObjectiveFunctionResult()).thenReturn(initialResult); String expectedLog1 = "[INFO] Evaluating root leaf"; String expectedLog2 = "[INFO] Could not evaluate leaf: root leaf description"; String expectedLog3 = "[INFO] Scenario \"preventive\": initial cost = 0.00 (functional: 0.00, virtual: 0.00), no remedial actions activated, cost after PRA = 0.00 (functional: 0.00, virtual: 0.00)"; From 132deb999ba4df27c72eaff812e0555699d3c719 Mon Sep 17 00:00:00 2001 From: belthlemar Date: Fri, 17 Feb 2023 18:08:09 +0100 Subject: [PATCH 11/14] fix several code smells --- .../farao/search_tree_rao/commons/RaoLogger.java | 11 ++++++++--- .../search_tree/algorithms/SearchTree.java | 6 ------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java index 82a453703a..aa15a76af1 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java @@ -259,9 +259,14 @@ public static void logOptimizationSummary(FaraoLogger logger, State optimizedSta String scenarioName = getScenarioName(optimizedState); String raResult = getRaResult(networkActions, rangeActions); Map virtualCostDetailed = getVirtualCostDetailed(finalObjective); - String initialCostString = preOptimObjectiveFunctionResult == null ? "" : - String.format("initial cost = %s (functional: %s, virtual: %s%s), ", formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost() + preOptimObjectiveFunctionResult.getVirtualCost()), formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost()), formatDouble(preOptimObjectiveFunctionResult.getVirtualCost()), - virtualCostDetailed.isEmpty() ? "" : " " + virtualCostDetailed); + String initialCostString; + if (virtualCostDetailed.isEmpty()) { + initialCostString = preOptimObjectiveFunctionResult == null ? "" : + String.format("initial cost = %s (functional: %s, virtual: %s), ", formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost() + preOptimObjectiveFunctionResult.getVirtualCost()), formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost()), formatDouble(preOptimObjectiveFunctionResult.getVirtualCost())); + } else { + initialCostString = preOptimObjectiveFunctionResult == null ? "" : + String.format("initial cost = %s (functional: %s, virtual: %s %s), ", formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost() + preOptimObjectiveFunctionResult.getVirtualCost()), formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost()), formatDouble(preOptimObjectiveFunctionResult.getVirtualCost()), virtualCostDetailed); + } logger.info("Scenario \"{}\": {}{}, cost {} = {} (functional: {}, virtual: {}{})", scenarioName, initialCostString, raResult, OptimizationState.afterOptimizing(optimizedState), formatDouble(finalObjective.getCost()), formatDouble(finalObjective.getFunctionalCost()), formatDouble(finalObjective.getVirtualCost()), virtualCostDetailed.isEmpty() ? "" : " " + virtualCostDetailed); } 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 708f26d512..c1b3e32035 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 @@ -79,9 +79,6 @@ public class SearchTree { private Leaf optimalLeaf; private Leaf previousDepthOptimalLeaf; - private double preOptimFunctionalCost; - private double preOptimVirtualCost; - private Optional combinationFulfillingStopCriterion = Optional.empty(); public SearchTree(SearchTreeInput input, @@ -128,9 +125,6 @@ public CompletableFuture run() { TECHNICAL_LOGS.info("Evaluating root leaf"); rootLeaf.evaluate(input.getObjectiveFunction(), getSensitivityComputerForEvaluation()); - this.preOptimFunctionalCost = rootLeaf.getFunctionalCost(); - this.preOptimVirtualCost = rootLeaf.getVirtualCost(); - if (rootLeaf.getStatus().equals(Leaf.Status.ERROR)) { topLevelLogger.info("Could not evaluate leaf: {}", rootLeaf); logOptimizationSummary(rootLeaf); From 0cf9a985ee8a9beade215b6f6b70d330b6f54367 Mon Sep 17 00:00:00 2001 From: belthlemar Date: Fri, 17 Feb 2023 19:17:21 +0100 Subject: [PATCH 12/14] add logRangeAction tests --- .../search_tree_rao/commons/RaoLogger.java | 13 +++++----- .../commons/RaoLoggerTest.java | 2 +- .../algorithms/SearchTreeTest.java | 26 ++++++++++++++++++- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java index aa15a76af1..e109e05d6d 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java @@ -258,17 +258,18 @@ public static void logFailedOptimizationSummary(FaraoLogger logger, State optimi public static void logOptimizationSummary(FaraoLogger logger, State optimizedState, Set networkActions, Map, java.lang.Double> rangeActions, ObjectiveFunctionResult preOptimObjectiveFunctionResult, ObjectiveFunctionResult finalObjective) { String scenarioName = getScenarioName(optimizedState); String raResult = getRaResult(networkActions, rangeActions); - Map virtualCostDetailed = getVirtualCostDetailed(finalObjective); + Map finalVirtualCostDetailed = getVirtualCostDetailed(finalObjective); + boolean isPreOptimNull = preOptimObjectiveFunctionResult == null; + Map initialVirtualCostDetailed = isPreOptimNull ? Collections.emptyMap() : getVirtualCostDetailed(preOptimObjectiveFunctionResult); String initialCostString; - if (virtualCostDetailed.isEmpty()) { - initialCostString = preOptimObjectiveFunctionResult == null ? "" : + if (initialVirtualCostDetailed.isEmpty()) { + initialCostString = isPreOptimNull ? "" : String.format("initial cost = %s (functional: %s, virtual: %s), ", formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost() + preOptimObjectiveFunctionResult.getVirtualCost()), formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost()), formatDouble(preOptimObjectiveFunctionResult.getVirtualCost())); } else { - initialCostString = preOptimObjectiveFunctionResult == null ? "" : - String.format("initial cost = %s (functional: %s, virtual: %s %s), ", formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost() + preOptimObjectiveFunctionResult.getVirtualCost()), formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost()), formatDouble(preOptimObjectiveFunctionResult.getVirtualCost()), virtualCostDetailed); + initialCostString = String.format("initial cost = %s (functional: %s, virtual: %s %s), ", formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost() + preOptimObjectiveFunctionResult.getVirtualCost()), formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost()), formatDouble(preOptimObjectiveFunctionResult.getVirtualCost()), initialVirtualCostDetailed); } logger.info("Scenario \"{}\": {}{}, cost {} = {} (functional: {}, virtual: {}{})", scenarioName, initialCostString, raResult, OptimizationState.afterOptimizing(optimizedState), - formatDouble(finalObjective.getCost()), formatDouble(finalObjective.getFunctionalCost()), formatDouble(finalObjective.getVirtualCost()), virtualCostDetailed.isEmpty() ? "" : " " + virtualCostDetailed); + formatDouble(finalObjective.getCost()), formatDouble(finalObjective.getFunctionalCost()), formatDouble(finalObjective.getVirtualCost()), finalVirtualCostDetailed.isEmpty() ? "" : " " + finalVirtualCostDetailed); } public static String getRaResult(Set networkActions, Map, java.lang.Double> rangeActions) { diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java index 770d76d21e..5c0bdf5fad 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java @@ -329,7 +329,7 @@ public void testLogOptimizationSummary() { rangeActions.put(fakePST2, 4.); RaoLogger.logOptimizationSummary(logger, preventive, networkActions, rangeActions, initialObjectiveFunctionResult, objectiveFunctionResult); - assertEquals("[INFO] Scenario \"preventive\": initial cost = -200.00 (functional: -210.30, virtual: 10.30 {mnec-violation-cost=42.2, loopflow-violation-cost=7.8})," + + assertEquals("[INFO] Scenario \"preventive\": initial cost = -200.00 (functional: -210.30, virtual: 10.30 {sensi-fallback-cost=10.3})," + " 1 network action(s) and 2 range action(s) activated : Open_fake_RA and PST_2: 4, PST_1: -2," + " cost after PRA = -100.00 (functional: -150.00, virtual: 50.00 {mnec-violation-cost=42.2, loopflow-violation-cost=7.8})", logsList.get(logsList.size() - 1).toString()); diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/SearchTreeTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/SearchTreeTest.java index bb658aaa5b..7026cd30f7 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/SearchTreeTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/search_tree/algorithms/SearchTreeTest.java @@ -47,6 +47,8 @@ import java.util.*; +import static com.farao_community.farao.commons.logs.FaraoLoggerProvider.TECHNICAL_LOGS; +import static com.farao_community.farao.search_tree_rao.commons.RaoLogger.logRangeActions; import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; @@ -568,7 +570,6 @@ public void testLogVirtualCostDetails() { when(rootLeaf.getVirtualCost("loop-flow-cost")).thenReturn(200.); // Functional cost does not satisfy stop criterion - ListAppender technical = getLogs(TechnicalLogs.class); ListAppender business = getLogs(RaoBusinessLogs.class); searchTree.logVirtualCostDetails(rootLeaf, "loop-flow-cost", "Optimized "); assertEquals(2, business.list.size()); @@ -576,6 +577,29 @@ public void testLogVirtualCostDetails() { assertEquals("[INFO] Optimized leaf-id, limiting \"loop-flow-cost\" constraint #01: flow = 1135.00 MW, threshold = 1000.00 MW, margin = -135.00 MW, element ne-id at state state-id, CNEC ID = \"cnec-id\", CNEC name = \"cnec-name\"", business.list.get(1).toString()); } + @Test + public void testLogRangeActions() { + setUpForVirtualLogs(); + List logsList = getLogs(TechnicalLogs.class).list; + logRangeActions(TECHNICAL_LOGS, rootLeaf, searchTreeInput.getOptimizationPerimeter(), ""); + assertEquals("[INFO] No range actions activated", logsList.get(logsList.size() - 1).toString()); + + // apply 2 range actions + rangeAction1 = Mockito.mock(PstRangeAction.class); + rangeAction2 = Mockito.mock(PstRangeAction.class); + when(rangeAction1.getName()).thenReturn("PST1"); + when(rangeAction2.getName()).thenReturn("PST2"); + Map>> setFakeRas = Map.of(optimizedState, Set.of(rangeAction1, rangeAction2)); + when(searchTreeInput.getOptimizationPerimeter().getRangeActionsPerState()).thenReturn(setFakeRas); + when(rootLeaf.getActivatedRangeActions(optimizedState)).thenReturn(Set.of(rangeAction1, rangeAction2)); + + logRangeActions(TECHNICAL_LOGS, rootLeaf, searchTreeInput.getOptimizationPerimeter(), ""); + // PST can be logged in any order + assert logsList.get(logsList.size() - 1).toString().contains("[INFO] range action(s):"); + assert logsList.get(logsList.size() - 1).toString().contains("PST1: 0"); + assert logsList.get(logsList.size() - 1).toString().contains("PST2: 0"); + } + @Test public void testSortNaCombinations() { NetworkAction na1 = Mockito.mock(NetworkAction.class); From cb859c61d6b5690e9ee9f1b03e727446bd0bb6b4 Mon Sep 17 00:00:00 2001 From: PHILIPPE EDWARDS Date: Mon, 20 Feb 2023 18:00:05 +0100 Subject: [PATCH 13/14] try to remove sonar bug --- .../farao/search_tree_rao/commons/RaoLogger.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java index e109e05d6d..65bcb1d57a 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/farao_community/farao/search_tree_rao/commons/RaoLogger.java @@ -259,14 +259,16 @@ public static void logOptimizationSummary(FaraoLogger logger, State optimizedSta String scenarioName = getScenarioName(optimizedState); String raResult = getRaResult(networkActions, rangeActions); Map finalVirtualCostDetailed = getVirtualCostDetailed(finalObjective); - boolean isPreOptimNull = preOptimObjectiveFunctionResult == null; - Map initialVirtualCostDetailed = isPreOptimNull ? Collections.emptyMap() : getVirtualCostDetailed(preOptimObjectiveFunctionResult); String initialCostString; - if (initialVirtualCostDetailed.isEmpty()) { - initialCostString = isPreOptimNull ? "" : - String.format("initial cost = %s (functional: %s, virtual: %s), ", formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost() + preOptimObjectiveFunctionResult.getVirtualCost()), formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost()), formatDouble(preOptimObjectiveFunctionResult.getVirtualCost())); + if (preOptimObjectiveFunctionResult == null) { + initialCostString = ""; } else { - initialCostString = String.format("initial cost = %s (functional: %s, virtual: %s %s), ", formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost() + preOptimObjectiveFunctionResult.getVirtualCost()), formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost()), formatDouble(preOptimObjectiveFunctionResult.getVirtualCost()), initialVirtualCostDetailed); + Map initialVirtualCostDetailed = getVirtualCostDetailed(preOptimObjectiveFunctionResult); + if (initialVirtualCostDetailed.isEmpty()) { + initialCostString = String.format("initial cost = %s (functional: %s, virtual: %s), ", formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost() + preOptimObjectiveFunctionResult.getVirtualCost()), formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost()), formatDouble(preOptimObjectiveFunctionResult.getVirtualCost())); + } else { + initialCostString = String.format("initial cost = %s (functional: %s, virtual: %s %s), ", formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost() + preOptimObjectiveFunctionResult.getVirtualCost()), formatDouble(preOptimObjectiveFunctionResult.getFunctionalCost()), formatDouble(preOptimObjectiveFunctionResult.getVirtualCost()), initialVirtualCostDetailed); + } } logger.info("Scenario \"{}\": {}{}, cost {} = {} (functional: {}, virtual: {}{})", scenarioName, initialCostString, raResult, OptimizationState.afterOptimizing(optimizedState), formatDouble(finalObjective.getCost()), formatDouble(finalObjective.getFunctionalCost()), formatDouble(finalObjective.getVirtualCost()), finalVirtualCostDetailed.isEmpty() ? "" : " " + finalVirtualCostDetailed); From 273a90a5216f6f6207f1cd947a5f7bcab8f66b6f Mon Sep 17 00:00:00 2001 From: demontmorillongod Date: Wed, 22 Mar 2023 11:57:19 +0100 Subject: [PATCH 14/14] merge master --- .../objective_function_evaluator/ObjectiveFunction.java | 4 ++-- .../farao/search_tree_rao/commons/RaoLoggerTest.java | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) 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 0a10ccdfe1..6107ed6ab1 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 @@ -65,11 +65,11 @@ public Pair> getVirtualCostAndCostlyElements(FlowResult return getVirtualCostAndCostlyElements(flowResult, rangeActionActivationResult, sensitivityResult, sensitivityStatus, virtualCostName, new HashSet<>()); } - public Pair> getVirtualCostAndCostlyElements(FlowResult flowResult, RangeActionActivationResult rangeActionActivationResult, SensitivityResult sensitivityResult, ComputationStatus sensitivityStatus, String virtualCostName, Set contingenciesToExlude) { + public Pair> getVirtualCostAndCostlyElements(FlowResult flowResult, RangeActionActivationResult rangeActionActivationResult, SensitivityResult sensitivityResult, ComputationStatus sensitivityStatus, String virtualCostName, Set contingenciesToExclude) { return virtualCostEvaluators.stream() .filter(costEvaluator -> costEvaluator.getName().equals(virtualCostName)) .findAny() - .map(costEvaluator -> costEvaluator.computeCostAndLimitingElements(flowResult, rangeActionActivationResult, sensitivityResult, sensitivityStatus, contingenciesToExlude)) + .map(costEvaluator -> costEvaluator.computeCostAndLimitingElements(flowResult, rangeActionActivationResult, sensitivityResult, sensitivityStatus, contingenciesToExclude)) .orElse(Pair.of(Double.NaN, new ArrayList<>())); } diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java index 229bb2f349..62ac14f215 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/commons/RaoLoggerTest.java @@ -22,7 +22,6 @@ import com.farao_community.farao.data.crac_api.cnec.Side; import com.farao_community.farao.data.crac_api.network_action.NetworkAction; import com.farao_community.farao.data.crac_api.range_action.RangeAction; -import com.farao_community.farao.rao_api.parameters.RaoParameters; import com.farao_community.farao.rao_api.parameters.ObjectiveFunctionParameters; import com.farao_community.farao.search_tree_rao.result.api.FlowResult; import com.farao_community.farao.search_tree_rao.result.api.ObjectiveFunctionResult;