From c5b888f244e7f1881737a9014352e337b475f045 Mon Sep 17 00:00:00 2001 From: phiedw Date: Mon, 20 Jun 2022 17:22:06 +0200 Subject: [PATCH] network_pool_fix (#626) Co-authored-by: EDWARDS Philippe Co-authored-by: Peter Mitri --- .../search_tree/algorithms/SearchTree.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 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 b17a817698..369d68a9ce 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 @@ -264,23 +264,27 @@ private void updateOptimalLeafWithNextDepthBestLeaf(AbstractNetworkPool networkP CountDownLatch latch = new CountDownLatch(naCombinations.size()); naCombinations.forEach(naCombination -> networkPool.submit(() -> { + Network networkClone = null; //This is where the threads actually wait for available networks try { - Network networkClone = networkPool.getAvailableNetwork(); //This is where the threads actually wait for available networks + networkClone = networkPool.getAvailableNetwork(); + } catch (InterruptedException e) { + throw new FaraoException(e); + } + try { + Network networkFinal = networkClone; if (combinationFulfillingStopCriterion.isEmpty() || arbitraryNetworkActionCombinationComparison(naCombination, combinationFulfillingStopCriterion.get()) < 0) { // Apply range actions that has been changed by the previous leaf on the network to start next depth leaves // from previous optimal leaf starting point // TODO: we can wonder if it's better to do this here or at creation of each leaves or at each evaluation/optimization previousDepthOptimalLeaf.getRangeActions() - .forEach(ra -> ra.apply(networkClone, previousDepthOptimalLeaf.getOptimizedSetpoint(ra, input.getOptimizationPerimeter().getMainOptimizationState()))); + .forEach(ra -> ra.apply(networkFinal, previousDepthOptimalLeaf.getOptimizedSetpoint(ra, input.getOptimizationPerimeter().getMainOptimizationState()))); // todo // set alreadyAppliedRa optimizeNextLeafAndUpdate(naCombination, networkClone, networkPool); - networkPool.releaseUsedNetwork(networkClone); } else { topLevelLogger.info("Skipping {} optimization because earlier combination fulfills stop criterion.", naCombination.getConcatenatedId()); - networkPool.releaseUsedNetwork(networkClone); } } catch (InterruptedException e) { BUSINESS_WARNS.warn("Cannot apply remedial action combination {}: {}", naCombination.getConcatenatedId(), e.getMessage()); @@ -290,6 +294,11 @@ private void updateOptimalLeafWithNextDepthBestLeaf(AbstractNetworkPool networkP } finally { TECHNICAL_LOGS.info("Remaining leaves to evaluate: {}", remainingLeaves.decrementAndGet()); latch.countDown(); + try { + networkPool.releaseUsedNetwork(networkClone); + } catch (InterruptedException ex) { + throw new FaraoException(ex); + } } }) ); @@ -323,7 +332,6 @@ void optimizeNextLeafAndUpdate(NetworkActionCombination naCombination, Network n topLevelLogger.info("Could not evaluate network action combination \"{}\": {}", printNetworkActions(networkActions), e.getMessage()); return; } catch (NotImplementedException e) { - networkPool.releaseUsedNetwork(network); throw e; } // We evaluate the leaf with taking the results of the previous optimal leaf if we do not want to update some results