From 60977f667965ec4614a56feb0dcb9032ca3e98fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jason=20Mar=C3=A9chal?= <45510813+JasonMarechal25@users.noreply.github.com> Date: Thu, 27 Jun 2024 11:38:21 +0200 Subject: [PATCH 1/4] Expose API [ANT-1158] (#1993) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Expose Antares Solver in a library. the library and target are called AntaresAPI. This API expose one entry point to run a simulation on a study. The return value of this is a data structure with the path to the simulation and the weekly problems. - New component API - Create a new project "api_client" and corresponding tests to detect API lib install regressions - rename "utils" project in tests as "in-memory-study" - promote this project at top level in tests. This allow in-memory-study to be used for any tests - create SimulationObserver. Object passed throughout all "frames" of the simulation. --------- Co-authored-by: Florian Omnès Co-authored-by: Florian OMNES <26088210+flomnes@users.noreply.github.com> --- .../workflows/install-cmake-328/action.yml | 4 +- .github/workflows/ubuntu.yml | 19 +- .../Expose_cpp_API.md | 10 +- .../separate_install_exe_api.md | 25 +++ src/CMakeLists.txt | 3 +- src/api/API.cpp | 116 +++++++++++ src/api/CMakeLists.txt | 42 ++++ src/api/SimulationObserver.cpp | 73 +++++++ src/api/SimulationResults.cpp | 22 ++ .../include/antares/api/SimulationResults.h | 62 ++++++ src/api/include/antares/api/solver.h | 35 ++++ src/api/private/API.h | 56 ++++++ src/api/private/SimulationObserver.h | 61 ++++++ src/api/solver.cpp | 46 +++++ src/api_client_example/CMakeLists.txt | 12 ++ src/api_client_example/README.md | 3 + src/api_client_example/src/API_client.cpp | 30 +++ src/api_client_example/src/API_client.h | 28 +++ src/api_client_example/src/CMakeLists.txt | 14 ++ src/api_client_example/tests/CMakeLists.txt | 17 ++ src/api_client_example/tests/test.cpp | 37 ++++ .../FileTreeStudyLoader.cpp | 14 +- src/libs/antares/inifile/CMakeLists.txt | 4 +- src/libs/antares/study/CMakeLists.txt | 1 - .../study/parts/short-term-storage/cluster.h | 38 ++-- src/packaging/CMakeLists.txt | 3 + src/packaging/Config.cmake.in | 6 +- src/solver/CMakeLists.txt | 1 + src/solver/application/application.cpp | 9 +- src/solver/lps/CMakeLists.txt | 24 +++ src/solver/lps/LpsFromAntares.cpp | 56 ++++++ .../antares/solver/lps/LpsFromAntares.h | 149 ++++++++++++++ src/solver/main/economy.cpp | 47 ----- src/solver/misc/options.cpp | 40 ++-- src/solver/optimisation/CMakeLists.txt | 4 +- .../HebdoProblemToLpsTranslator.cpp | 109 ++++++++++ .../adequacy_patch_weekly_optimization.cpp | 49 ++--- .../optimisation/base_weekly_optimization.cpp | 55 ++--- .../HebdoProblemToLpsTranslator.h | 81 ++++++++ .../adequacy_patch_weekly_optimization.h | 5 +- .../optimisation/base_weekly_optimization.h | 48 +++-- .../constraints/constraint_builder_utils.h | 1 + .../optimisation/opt_export_structure.h | 58 ++---- .../solver/optimisation/opt_fonctions.h | 62 +++--- .../solver/optimisation/weekly_optimization.h | 44 ++-- .../optimisation/opt_optimisation_hebdo.cpp | 45 +++-- .../opt_optimisation_lineaire.cpp | 71 ++++--- .../opt_pilotage_optimisation_lineaire.cpp | 44 ++-- .../optimisation/opt_rename_problem.cpp | 1 + .../optimisation/weekly_optimization.cpp | 43 ++-- src/solver/simulation/CMakeLists.txt | 55 ++--- src/solver/simulation/adequacy.cpp | 51 ++--- src/solver/simulation/adequacy_mode.cpp | 63 +++--- src/solver/simulation/common-eco-adq.cpp | 42 ++-- src/solver/simulation/economy.cpp | 48 +++-- src/solver/simulation/economy_mode.cpp | 64 +++--- .../solver/simulation/ISimulationObserver.h | 65 ++++++ .../antares/solver/simulation/adequacy.h | 48 ++--- .../antares/solver/simulation/adequacy_mode.h | 4 +- .../antares/solver/simulation/economy.h | 43 ++-- .../antares/solver/simulation/economy_mode.h | 5 +- .../antares/solver/simulation/solver.h | 45 +++-- .../antares/solver/simulation/solver.hxx | 23 ++- .../antares/solver/utils/name_translator.h | 38 ++-- src/tests/CMakeLists.txt | 1 + src/tests/end-to-end/CMakeLists.txt | 1 - .../binding_constraints/CMakeLists.txt | 3 +- .../test_binding_constraints.cpp | 2 +- .../end-to-end/simple_study/CMakeLists.txt | 3 +- .../end-to-end/simple_study/simple-study.cpp | 2 +- src/tests/end-to-end/utils/CMakeLists.txt | 20 -- src/tests/inmemory-study/CMakeLists.txt | 25 +++ .../in-memory-study.cpp} | 51 ++--- .../include/in-memory-study.h} | 43 ++-- src/tests/src/CMakeLists.txt | 2 + src/tests/src/api_internal/CMakeLists.txt | 27 +++ src/tests/src/api_internal/test_api.cpp | 96 +++++++++ src/tests/src/api_lib/CMakeLists.txt | 20 ++ src/tests/src/api_lib/test_api.cpp | 35 ++++ src/tests/src/libs/CMakeLists.txt | 2 +- src/tests/src/solver/CMakeLists.txt | 1 + .../test-unfeasible-problem-analyzer.cpp | 5 +- src/tests/src/solver/lps/CMakeLists.txt | 20 ++ src/tests/src/solver/lps/test_lps.cpp | 77 +++++++ .../src/solver/optimisation/CMakeLists.txt | 26 +-- .../adequacy_patch/CMakeLists.txt | 24 +++ .../{ => adequacy_patch}/adequacy_patch.cpp | 0 .../optimisation/translator/CMakeLists.txt | 20 ++ .../translator/test_translator.cpp | 189 ++++++++++++++++++ .../renderer/scenario-builder-ntc-renderer.h | 38 ++-- src/vcpkg.json | 2 +- 91 files changed, 2427 insertions(+), 729 deletions(-) create mode 100644 docs/Architecture_Decision_Records/separate_install_exe_api.md create mode 100644 src/api/API.cpp create mode 100644 src/api/CMakeLists.txt create mode 100644 src/api/SimulationObserver.cpp create mode 100644 src/api/SimulationResults.cpp create mode 100644 src/api/include/antares/api/SimulationResults.h create mode 100644 src/api/include/antares/api/solver.h create mode 100644 src/api/private/API.h create mode 100644 src/api/private/SimulationObserver.h create mode 100644 src/api/solver.cpp create mode 100644 src/api_client_example/CMakeLists.txt create mode 100644 src/api_client_example/README.md create mode 100644 src/api_client_example/src/API_client.cpp create mode 100644 src/api_client_example/src/API_client.h create mode 100644 src/api_client_example/src/CMakeLists.txt create mode 100644 src/api_client_example/tests/CMakeLists.txt create mode 100644 src/api_client_example/tests/test.cpp create mode 100644 src/solver/lps/CMakeLists.txt create mode 100644 src/solver/lps/LpsFromAntares.cpp create mode 100644 src/solver/lps/include/antares/solver/lps/LpsFromAntares.h delete mode 100644 src/solver/main/economy.cpp create mode 100644 src/solver/optimisation/HebdoProblemToLpsTranslator.cpp create mode 100644 src/solver/optimisation/include/antares/solver/optimisation/HebdoProblemToLpsTranslator.h create mode 100644 src/solver/simulation/include/antares/solver/simulation/ISimulationObserver.h delete mode 100644 src/tests/end-to-end/utils/CMakeLists.txt create mode 100644 src/tests/inmemory-study/CMakeLists.txt rename src/tests/{end-to-end/utils/utils.cpp => inmemory-study/in-memory-study.cpp} (88%) rename src/tests/{end-to-end/utils/utils.h => inmemory-study/include/in-memory-study.h} (86%) create mode 100644 src/tests/src/api_internal/CMakeLists.txt create mode 100644 src/tests/src/api_internal/test_api.cpp create mode 100644 src/tests/src/api_lib/CMakeLists.txt create mode 100644 src/tests/src/api_lib/test_api.cpp create mode 100644 src/tests/src/solver/lps/CMakeLists.txt create mode 100644 src/tests/src/solver/lps/test_lps.cpp create mode 100644 src/tests/src/solver/optimisation/adequacy_patch/CMakeLists.txt rename src/tests/src/solver/optimisation/{ => adequacy_patch}/adequacy_patch.cpp (100%) create mode 100644 src/tests/src/solver/optimisation/translator/CMakeLists.txt create mode 100644 src/tests/src/solver/optimisation/translator/test_translator.cpp diff --git a/.github/workflows/install-cmake-328/action.yml b/.github/workflows/install-cmake-328/action.yml index a6f111746c..1cf045dbee 100644 --- a/.github/workflows/install-cmake-328/action.yml +++ b/.github/workflows/install-cmake-328/action.yml @@ -1,4 +1,4 @@ -name: "Install cmake 3.28 using devtoolset 10" +name: "Install cmake 3.28 using devtoolset 10 if possible" description: "Download and install system wide cmake 3.28" runs: @@ -7,7 +7,7 @@ runs: - name: Build cmake shell: bash run: | - source /opt/rh/devtoolset-10/enable + source /opt/rh/devtoolset-10/enable || true # Ignore error if devtoolset-10 is not available yum -y install openssl-devel wget https://github.com/Kitware/CMake/releases/download/v3.28.2/cmake-3.28.2.tar.gz tar -xvf cmake-3.28.2.tar.gz diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 85713eeb70..88fc65ceac 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -29,7 +29,6 @@ env: RUN_EXTENDED_TESTS: ${{ github.event_name == 'schedule' || inputs.run-tests == 'true' }} REF: ${{ inputs.target_branch =='' && github.ref || inputs.target_branch}} VCPKG_ROOT: ${{ github.workspace }}/vcpkg - vcpkgPackages: yaml-cpp antlr4 boost-test triplet: x64-linux WX_CONFIG: /usr/bin/wx-config @@ -115,6 +114,24 @@ jobs: run: | cmake --build _build -j$(nproc) + - name: Run API tests + run: | + cmake --install _build --prefix antares_install + cd src/api_client_example + cmake -B _build -S . \ + -DCMAKE_C_COMPILER_LAUNCHER=ccache \ + -DCMAKE_C_COMPILER=/usr/bin/gcc-10 \ + -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ + -DCMAKE_CXX_COMPILER=/usr/bin/g++-10 \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_TESTING=ON \ + -DCMAKE_TOOLCHAIN_FILE=${{github.workspace}}/vcpkg/scripts/buildsystems/vcpkg.cmake \ + -DVCPKG_TARGET_TRIPLET=x64-linux-release \ + -DCMAKE_PREFIX_PATH="${{github.workspace}}/rte-antares-deps-Release;${{github.workspace}}/install;${{ env.ORTOOLS_DIR }}/install;${{github.workspace}}/antares_install;${{github.workspace}}/rte-antares-deps-Release;${{github.workspace}}/_build/vcpkg_installed/x64-linux-release" + cmake --build _build -j$(nproc) + cd _build + ctest -C Release --output-on-failure + # simtest - name: Read simtest version id: simtest-version diff --git a/docs/Architecture_Decision_Records/Expose_cpp_API.md b/docs/Architecture_Decision_Records/Expose_cpp_API.md index bae68cd5b4..fcc11c8a93 100644 --- a/docs/Architecture_Decision_Records/Expose_cpp_API.md +++ b/docs/Architecture_Decision_Records/Expose_cpp_API.md @@ -4,11 +4,11 @@ ## Context -Some clients (e.g. Xpansion) would prefer to use a library than a CLI tool. Exposing a library require exposing a public API. -The API can be exposed either as pure C or as C++. -A C API can always be developped as a facade on top of a C++ API. -C++ API will not have to care about ABI compatibility because Simulator lib will be provided as static, meaning a client -upgrading would need to be recompiled and relink with the new lib version. +Several clients, such as Xpansion, express a preference for utilizing a library over a command-line interface (CLI) tool. Exposing a library necessitates the exposure of a public API. + +This API can be presented in either pure C or C++. It's worth noting that a C API can always serve as a facade atop a C++ API. + +In the case of a C++ API, concerns regarding ABI compatibility are alleviated, as the Simulator library is provided in a static form. Consequently, when a client upgrades, they would simply need to recompile and relink with the new version of the library. ## Decision diff --git a/docs/Architecture_Decision_Records/separate_install_exe_api.md b/docs/Architecture_Decision_Records/separate_install_exe_api.md new file mode 100644 index 0000000000..3a9662ec1d --- /dev/null +++ b/docs/Architecture_Decision_Records/separate_install_exe_api.md @@ -0,0 +1,25 @@ +# Expose public C++ API + +## Status: Rejected [2024/04/11] + +## Context + +Originally, there exists a single install target designated to deploy the diverse Antares executables, predominantly packaged within the release assets. However, there's a rationale behind avoiding the creation of a unified package containing both executables and libraries, given that they cater to distinct user demographics. This mirrors the flexibility offered by package managers such as apt, where users can opt to install binary packages and separate development (devel) packages according to their requirements. + +## Decision + +Split installation into two targets + +Produce different assets/packages for each install targets. + + +## Consequences + +* Mutltiplication of assets +* Two install targets to manage +* Users of executable not "polluted" by libraries + +## Reason for rejection + +The decision was rejected because it was deemed unnecessary to split the installation targets. It adds +some difficulties for few benefits at the moment \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 862c7fdbc8..44715f0650 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -233,7 +233,7 @@ message(STATUS "OR-Tools tag ${ORTOOLS_TAG}") FetchContent_MakeAvailable(ortools) endif() -find_package(minizip-ng) +find_package(minizip-ng QUIET) if (minizip-ng_FOUND) add_library(MINIZIP::minizip ALIAS MINIZIP::minizip-ng) else () @@ -267,6 +267,7 @@ add_subdirectory("ext/yuni/src") OMESSAGE("") # empty line # Sub Directories +add_subdirectory(api) add_subdirectory(libs) #antares-core fswalker if(BUILD_UI) diff --git a/src/api/API.cpp b/src/api/API.cpp new file mode 100644 index 0000000000..38e9778eab --- /dev/null +++ b/src/api/API.cpp @@ -0,0 +1,116 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#include "API.h" +#include "antares/solver/simulation/economy_mode.h" +#include "antares/solver/simulation/adequacy_mode.h" +#include "antares/solver/misc/options.h" +#include "antares/infoCollection/StudyInfoCollector.h" +#include "antares/benchmarking/DurationCollector.h" +#include "antares/exception/LoadingError.hpp" +#include +#include + +namespace Antares::API +{ +SimulationResults APIInternal::run(const IStudyLoader& study_loader) +{ + try { + study_ = study_loader.load(); + } catch (const ::Antares::Error::StudyFolderDoesNotExist& e) { + Antares::API::Error err{.reason = e.what()}; + return { + .simulationPath = "", + .antares_problems = {}, + .error = err + }; + } + return execute(); +} + +/** + * @brief The execute method is used to execute the simulation. + * @return SimulationResults object which contains the results of the simulation. + * + * This method is initialy a copy of Application::execute with some modifications hence the apparent dupllication + */ +SimulationResults APIInternal::execute() const +{ + // study_ == nullptr e.g when the -h flag is given + if (!study_) + { + using namespace std::string_literals; + Antares::API::Error err{.reason = "Couldn't create study"s}; + return {.simulationPath{}, .antares_problems{}, .error = err}; + } + + study_->computePThetaInfForThermalClusters(); + + // Only those two fields are used un simulation + Settings settings; + settings.tsGeneratorsOnly = false; + settings.noOutput = false; + + Benchmarking::DurationCollector durationCollector; + Benchmarking::OptimizationInfo optimizationInfo; + auto ioQueueService = std::make_shared(); + ioQueueService->maximumThreadCount(1); + ioQueueService->start(); + auto resultWriter = Solver::resultWriterFactory( + study_->parameters.resultFormat, study_->folderOutput, ioQueueService, durationCollector); + SimulationObserver simulationObserver; + // Run the simulation + switch (study_->runtime->mode) + { + case Data::SimulationMode::Economy: + case Data::SimulationMode::Expansion: + Solver::runSimulationInEconomicMode(*study_, + settings, + durationCollector, + *resultWriter, + optimizationInfo, + simulationObserver); + break; + case Data::SimulationMode::Adequacy: + Solver::runSimulationInAdequacyMode(*study_, + settings, + durationCollector, + *resultWriter, + optimizationInfo, + simulationObserver); + break; + default: + break; + } + + // Importing Time-Series if asked + study_->importTimeseriesIntoInput(); + + // Stop the display of the progression + study_->progression.stop(); + return + { + .simulationPath = study_->folderOutput.c_str(), + .antares_problems = simulationObserver.acquireLps(), + .error{} + }; +} +} // namespace Antares::API \ No newline at end of file diff --git a/src/api/CMakeLists.txt b/src/api/CMakeLists.txt new file mode 100644 index 0000000000..109c640578 --- /dev/null +++ b/src/api/CMakeLists.txt @@ -0,0 +1,42 @@ +add_library(solver_api) +add_library(Antares::solver_api ALIAS solver_api) + +set(PUBLIC_HEADERS + include/antares/api/SimulationResults.h + include/antares/api/solver.h +) + +set(PRIVATE_HEADERS + private/API.h + private/SimulationObserver.h +) + +target_sources(solver_api + PRIVATE + solver.cpp + API.cpp + SimulationObserver.cpp + SimulationResults.cpp + ${PUBLIC_HEADERS} + ${PRIVATE_HEADERS} +) + +target_include_directories(solver_api + PUBLIC + $ + PRIVATE + $ +) + +target_link_libraries(solver_api + PRIVATE + Antares::study + Antares::study-loader + Antares::file-tree-study-loader + antares-solver-simulation + PUBLIC + Antares::lps +) +install(DIRECTORY include/antares + DESTINATION "include" +) \ No newline at end of file diff --git a/src/api/SimulationObserver.cpp b/src/api/SimulationObserver.cpp new file mode 100644 index 0000000000..7842ce8099 --- /dev/null +++ b/src/api/SimulationObserver.cpp @@ -0,0 +1,73 @@ + +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#include "SimulationObserver.h" + +#include "antares/solver/optimisation/HebdoProblemToLpsTranslator.h" + +namespace Antares::API +{ +namespace +{ +auto translate(const PROBLEME_HEBDO& problemeHebdo, + std::string_view name, + const Solver::HebdoProblemToLpsTranslator& translator, + const unsigned int year, + const unsigned int week) +{ + auto weekly_data = translator.translate(problemeHebdo.ProblemeAResoudre.get(), name); + Solver::ConstantDataFromAntares common_data; + if (year == 1 && week == 1) + { + common_data = translator.commonProblemData(problemeHebdo.ProblemeAResoudre.get()); + } + return std::make_pair(common_data, weekly_data); +} +} // namespace + +void SimulationObserver::notifyHebdoProblem(const PROBLEME_HEBDO& problemeHebdo, + int optimizationNumber, + std::string_view name) +{ + if (optimizationNumber != 1) + { + return; // We only care about first optimization + } + Solver::HebdoProblemToLpsTranslator translator; + const unsigned int year = problemeHebdo.year + 1; + const unsigned int week = problemeHebdo.weekInTheYear + 1; + // common_data and weekly_data computed before the mutex lock to prevent blocking the thread + auto [common_data, weekly_data] = translate(problemeHebdo, name, translator, year, week); + std::lock_guard lock(mutex_); + if (year == 1 && week == 1) + { + lps_.setConstantData(common_data); + } + lps_.addWeeklyData({year, week}, weekly_data); +} + +Solver::LpsFromAntares&& SimulationObserver::acquireLps() noexcept +{ + std::lock_guard lock(mutex_); + return std::move(lps_); +} +} // namespace Antares::API diff --git a/src/api/SimulationResults.cpp b/src/api/SimulationResults.cpp new file mode 100644 index 0000000000..63c5127a49 --- /dev/null +++ b/src/api/SimulationResults.cpp @@ -0,0 +1,22 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#include "antares/api/SimulationResults.h" diff --git a/src/api/include/antares/api/SimulationResults.h b/src/api/include/antares/api/SimulationResults.h new file mode 100644 index 0000000000..5a5e93982a --- /dev/null +++ b/src/api/include/antares/api/SimulationResults.h @@ -0,0 +1,62 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#pragma once +#include +#include +#include +#include "antares/solver/lps/LpsFromAntares.h" + +namespace Antares::API +{ +/** + * @struct Error + * @brief The Error structure is used to represent an error that occurred during the simulation. + */ +struct Error { + /** + * @brief The reason for the error. + */ + std::string reason; +}; + +/** + * @struct SimulationResults + * @brief The SimulationResults structure is used to represent the results of a simulation. + * @details It contains the path to the simulation, weekly problems, and an optional error. + */ +struct [[nodiscard("Contains results and potential error")]] SimulationResults +{ + /** + * @brief The path to the simulation (output). + */ + std::filesystem::path simulationPath; + /** + * @brief weekly problems + */ + Antares::Solver::LpsFromAntares antares_problems; + /** + * @brief An optional error that occurred during the simulation. + */ + std::optional error; +}; + +} \ No newline at end of file diff --git a/src/api/include/antares/api/solver.h b/src/api/include/antares/api/solver.h new file mode 100644 index 0000000000..d016a9a08a --- /dev/null +++ b/src/api/include/antares/api/solver.h @@ -0,0 +1,35 @@ + +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#pragma once +#include "SimulationResults.h" + +namespace Antares::API +{ +/** + * @brief The PerformSimulation function is used to perform a simulation. + * @param study_path The path to the study to be simulated. + * @return SimulationResults object which contains the results of the simulation. + * @exception noexcept This function does not throw exceptions. + */ +SimulationResults PerformSimulation(const std::filesystem::path& study_path) noexcept; +} // namespace Antares::API diff --git a/src/api/private/API.h b/src/api/private/API.h new file mode 100644 index 0000000000..91f8a4e2d3 --- /dev/null +++ b/src/api/private/API.h @@ -0,0 +1,56 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#pragma once +#include + +#include +#include "antares/api/SimulationResults.h" + +namespace Antares::Data +{ +class Study; +} + +namespace Antares::API +{ + +/** + * @class APIInternal + * @brief The APIInternal class is used to run simulations. + */ +class APIInternal +{ +public: + /** + * @brief The run method is used to run the simulation. + * @param study_loader A pointer to an IStudyLoader object. The IStudyLoader object is used to + * load the study that will be simulated. + * @return SimulationResults object which contains the results of the simulation. + */ + SimulationResults run(const IStudyLoader& study_loader); + +private: + std::shared_ptr study_; + SimulationResults execute() const; +}; + +} // namespace Antares::API diff --git a/src/api/private/SimulationObserver.h b/src/api/private/SimulationObserver.h new file mode 100644 index 0000000000..f2d78fc4eb --- /dev/null +++ b/src/api/private/SimulationObserver.h @@ -0,0 +1,61 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#pragma once +#include +#include + +namespace Antares::API +{ + +/** + * @class SimulationObserver + * @brief The SimulationObserver class is used to observe the simulation. + * @details It inherits from the ISimulationObserver interface and overrides the notifyHebdoProblem + * method. + */ +class SimulationObserver: public Solver::Simulation::ISimulationObserver +{ +public: + /** + * @brief Used to notify of a solver HEBDO_PROBLEM. + * HEBDO_PROBLEM is assumed to be properly constructed and valid in order to build + * LpsFromAntares properly + * @param problemeHebdo A pointer to a PROBLEME_HEBDO object representing the problem. + * @param optimizationNumber The number of the optimization. + * @param name The name of the problem. + */ + void notifyHebdoProblem(const PROBLEME_HEBDO& problemeHebdo, + int optimizationNumber, + std::string_view name) override; + + /** + * @brief The acquireLps method is used to take ownership of Antares problems. + * @return An LpsFromAntares object containing the linear programming problems. + */ + Solver::LpsFromAntares&& acquireLps() noexcept; + +private: + Solver::LpsFromAntares lps_; + std::mutex mutex_; +}; + +} // namespace Antares::API diff --git a/src/api/solver.cpp b/src/api/solver.cpp new file mode 100644 index 0000000000..6e2f7068ea --- /dev/null +++ b/src/api/solver.cpp @@ -0,0 +1,46 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#include +#include "antares/file-tree-study-loader/FileTreeStudyLoader.h" +#include "antares/study-loader/IStudyLoader.h" + +#include "private/API.h" + +namespace Antares::API +{ + +SimulationResults PerformSimulation(const std::filesystem::path& study_path) noexcept +{ + try + { + APIInternal api; + FileTreeStudyLoader study_loader(study_path); + return api.run(study_loader); + } + catch (const std::exception& e) + { + Antares::API::Error err{.reason = e.what()}; + return SimulationResults{.simulationPath = study_path, .antares_problems{}, .error = err}; + } +} + +} // namespace Antares::API diff --git a/src/api_client_example/CMakeLists.txt b/src/api_client_example/CMakeLists.txt new file mode 100644 index 0000000000..570ee9341b --- /dev/null +++ b/src/api_client_example/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.5) + +PROJECT(API_client) + +set(CMAKE_CXX_STANDARD 20) + +add_subdirectory(src) + +include(CTest) +if (BUILD_TESTING) + add_subdirectory(tests) +endif () \ No newline at end of file diff --git a/src/api_client_example/README.md b/src/api_client_example/README.md new file mode 100644 index 0000000000..151b1d346b --- /dev/null +++ b/src/api_client_example/README.md @@ -0,0 +1,3 @@ +Client for antares solver API + +To demonstrate as much capabilities as possible this client should stay stand alone this mean not added as a sub_directory for cmake diff --git a/src/api_client_example/src/API_client.cpp b/src/api_client_example/src/API_client.cpp new file mode 100644 index 0000000000..6489806c51 --- /dev/null +++ b/src/api_client_example/src/API_client.cpp @@ -0,0 +1,30 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + + +#include "API_client.h" + +#include + +Antares::API::SimulationResults solve(std::filesystem::path study_path) { + return Antares::API::PerformSimulation(std::move(study_path)); +} + diff --git a/src/api_client_example/src/API_client.h b/src/api_client_example/src/API_client.h new file mode 100644 index 0000000000..5d8500649e --- /dev/null +++ b/src/api_client_example/src/API_client.h @@ -0,0 +1,28 @@ + +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#pragma once + +#include +#include + +Antares::API::SimulationResults solve(std::filesystem::path study_path); diff --git a/src/api_client_example/src/CMakeLists.txt b/src/api_client_example/src/CMakeLists.txt new file mode 100644 index 0000000000..55ebb33579 --- /dev/null +++ b/src/api_client_example/src/CMakeLists.txt @@ -0,0 +1,14 @@ +add_library(API_client) + +target_sources(API_client + PRIVATE + API_client.cpp + API_client.h +) + +find_package(Antares REQUIRED) +target_link_libraries(API_client PUBLIC Antares::solver_api) +target_include_directories(API_client + PUBLIC + $ +) \ No newline at end of file diff --git a/src/api_client_example/tests/CMakeLists.txt b/src/api_client_example/tests/CMakeLists.txt new file mode 100644 index 0000000000..1fc9c3cc89 --- /dev/null +++ b/src/api_client_example/tests/CMakeLists.txt @@ -0,0 +1,17 @@ +find_package(Boost COMPONENTS unit_test_framework REQUIRED) + +add_executable(test_client) + +target_sources(test_client + PRIVATE + test.cpp +) + +target_link_libraries(test_client + PRIVATE + API_client + Boost::unit_test_framework +) + +add_test(NAME testclient COMMAND test_client) +set_property(TEST testclient PROPERTY LABELS integ) \ No newline at end of file diff --git a/src/api_client_example/tests/test.cpp b/src/api_client_example/tests/test.cpp new file mode 100644 index 0000000000..4adee7bfd6 --- /dev/null +++ b/src/api_client_example/tests/test.cpp @@ -0,0 +1,37 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#define BOOST_TEST_MODULE test_client_api + +#include +#include "API_client.h" + +BOOST_AUTO_TEST_CASE(test_run) { + auto results = solve("dummy_study_test_client_api"); + BOOST_CHECK(results.error); + BOOST_CHECK(!results.error->reason.empty()); + auto c = results.error->reason; + std::cout << c << std::endl; + BOOST_CHECK(results.error->reason.find("Study") != std::string::npos); + BOOST_CHECK(results.error->reason.find("folder") != std::string::npos); + BOOST_CHECK(results.error->reason.find("not") != std::string::npos); + BOOST_CHECK(results.error->reason.find("exist") != std::string::npos); +} \ No newline at end of file diff --git a/src/libs/antares/file-tree-study-loader/FileTreeStudyLoader.cpp b/src/libs/antares/file-tree-study-loader/FileTreeStudyLoader.cpp index 5302c19263..8ff1c2730d 100644 --- a/src/libs/antares/file-tree-study-loader/FileTreeStudyLoader.cpp +++ b/src/libs/antares/file-tree-study-loader/FileTreeStudyLoader.cpp @@ -49,15 +49,10 @@ namespace * @param study_path A string_view representing the study path. * @return std::vector A vector of strings containing the prepared arguments. */ -[[nodiscard]] std::vector prepareArgs(std::span argv, - std::string_view study_path) +void prepareArgs(std::array& argv, std::span data) { - using namespace std::literals::string_literals; - std::string arg0{""s}; - std::string arg1{study_path}; - argv[0] = arg0.data(); - argv[1] = arg1.data(); - return {std::move(arg0), std::move(arg1)}; + argv[0] = data[0].data(); + argv[1] = data[1].data(); } } // namespace @@ -66,8 +61,9 @@ std::unique_ptr FileTreeStudyLoader::load() const using namespace std::literals::string_literals; Antares::Solver::Application application; constexpr unsigned int argc = 2; + std::array keep_alive{""s, study_path_.string()}; std::array argv; - auto keep_alive = prepareArgs(argv, study_path_.string()); + prepareArgs(argv, keep_alive); application.prepare(argc, argv.data()); return application.acquireStudy(); diff --git a/src/libs/antares/inifile/CMakeLists.txt b/src/libs/antares/inifile/CMakeLists.txt index a68b3f2786..089591f002 100644 --- a/src/libs/antares/inifile/CMakeLists.txt +++ b/src/libs/antares/inifile/CMakeLists.txt @@ -17,11 +17,11 @@ target_link_libraries(inifile PRIVATE io logs - Boost::boost + Boost::headers ) target_include_directories(inifile - PUBLIC + PUBLIC $ ) diff --git a/src/libs/antares/study/CMakeLists.txt b/src/libs/antares/study/CMakeLists.txt index c90d543fff..65ad5398a6 100644 --- a/src/libs/antares/study/CMakeLists.txt +++ b/src/libs/antares/study/CMakeLists.txt @@ -285,7 +285,6 @@ add_library(Antares::study ALIAS study) target_link_libraries(study PUBLIC - Boost::boost yuni-static-core Antares::date #parameters Antares::inifile #parameters diff --git a/src/libs/antares/study/include/antares/study/parts/short-term-storage/cluster.h b/src/libs/antares/study/include/antares/study/parts/short-term-storage/cluster.h index afe2e1976f..cc9af74015 100644 --- a/src/libs/antares/study/include/antares/study/parts/short-term-storage/cluster.h +++ b/src/libs/antares/study/include/antares/study/parts/short-term-storage/cluster.h @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #pragma once #include diff --git a/src/packaging/CMakeLists.txt b/src/packaging/CMakeLists.txt index 6d68ec5e38..868cd7a8ec 100644 --- a/src/packaging/CMakeLists.txt +++ b/src/packaging/CMakeLists.txt @@ -10,6 +10,8 @@ set(TARGET_LIBS #No alias #not dependency is present since once a dependency is in the export set #it is available for everything + solver_api #What we want to export + #solver_api study study-loader @@ -33,6 +35,7 @@ set(TARGET_LIBS #No alias exception benchmarking antares-solver-variable + lps #study-loader #nothing diff --git a/src/packaging/Config.cmake.in b/src/packaging/Config.cmake.in index 2cbf63a275..c9bcb039e1 100644 --- a/src/packaging/Config.cmake.in +++ b/src/packaging/Config.cmake.in @@ -1,8 +1,8 @@ @PACKAGE_INIT@ include(CMakeFindDependencyMacro) -find_dependency(ortools REQUIRED) -find_dependency(minizip-ng) +find_dependency(Boost REQUIRED) +find_package(minizip-ng QUIET) #Optional, don't use find_dependency it forces return in case of dep not found if (minizip-ng_FOUND) add_library(MINIZIP::minizip ALIAS MINIZIP::minizip-ng) else () @@ -11,6 +11,8 @@ else () message (FATAL_ERROR "Minizip dependency (minizip or minizip-ng) not found.") endif () endif () +find_dependency(ortools REQUIRED) +find_dependency(sirius_solver REQUIRED) include("${CMAKE_CURRENT_LIST_DIR}/AntaresTargets.cmake") diff --git a/src/solver/CMakeLists.txt b/src/solver/CMakeLists.txt index aeb17fb3a0..13ab3bfa6c 100644 --- a/src/solver/CMakeLists.txt +++ b/src/solver/CMakeLists.txt @@ -11,6 +11,7 @@ add_subdirectory(application) add_subdirectory(constraints-builder) add_subdirectory(hydro) add_subdirectory(infeasible-problem-analysis) +add_subdirectory(lps) add_subdirectory(misc) add_subdirectory(optimisation) add_subdirectory(signal-handling) diff --git a/src/solver/application/application.cpp b/src/solver/application/application.cpp index d222ad88c0..90d5e559c5 100644 --- a/src/solver/application/application.cpp +++ b/src/solver/application/application.cpp @@ -34,7 +34,6 @@ #include #include "antares/antares/version.h" #include "antares/config/config.h" -#include "antares/file-tree-study-loader/FileTreeStudyLoader.h" #include "antares/signal-handling/public.h" #include "antares/solver/misc/system-memory.h" #include "antares/solver/misc/write-command-line.h" @@ -410,20 +409,24 @@ void Application::execute() void Application::runSimulationInEconomicMode() { + Simulation::NullSimulationObserver observer; Solver::runSimulationInEconomicMode(*pStudy, pSettings, pDurationCollector, *resultWriter, - pOptimizationInfo); + pOptimizationInfo, + observer); } void Application::runSimulationInAdequacyMode() { + Simulation::NullSimulationObserver observer; Solver::runSimulationInAdequacyMode(*pStudy, pSettings, pDurationCollector, *resultWriter, - pOptimizationInfo); + pOptimizationInfo, + observer); } static std::string timeToString() diff --git a/src/solver/lps/CMakeLists.txt b/src/solver/lps/CMakeLists.txt new file mode 100644 index 0000000000..8c3e48cab0 --- /dev/null +++ b/src/solver/lps/CMakeLists.txt @@ -0,0 +1,24 @@ +set(PROJ lps) +set(HEADERS + include/antares/solver/lps/LpsFromAntares.h +) +set(SRC_PROJ + ${HEADERS} + LpsFromAntares.cpp +) +source_group("solver\\lps" FILES ${SRC_PROJ}) + +add_library(${PROJ}) +target_sources(${PROJ} PRIVATE + ${SRC_PROJ} +) +add_library(Antares::${PROJ} ALIAS ${PROJ}) + +target_include_directories(${PROJ} + PUBLIC + $ +) + +install(DIRECTORY include/antares + DESTINATION "include" +) \ No newline at end of file diff --git a/src/solver/lps/LpsFromAntares.cpp b/src/solver/lps/LpsFromAntares.cpp new file mode 100644 index 0000000000..a32484038d --- /dev/null +++ b/src/solver/lps/LpsFromAntares.cpp @@ -0,0 +1,56 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#include "antares/solver/lps/LpsFromAntares.h" + +namespace Antares::Solver +{ +bool LpsFromAntares::empty() const +{ + return constantProblemData.VariablesCount == 0 || weeklyProblems.empty(); +} + +void LpsFromAntares::setConstantData(const ConstantDataFromAntares& data) +{ + constantProblemData = data; +} + +void LpsFromAntares::addWeeklyData(WeeklyProblemId id, const WeeklyDataFromAntares& data) +{ + weeklyProblems.emplace(id, data); +} + +const WeeklyDataFromAntares& LpsFromAntares::weeklyData(WeeklyProblemId id) const +{ + auto it = weeklyProblems.find(id); + if (it == weeklyProblems.end()) + { + return WeeklyDataFromAntares(); // TODO Better error handling + } + return it->second; +} + +size_t LpsFromAntares::weekCount() const noexcept +{ + return weeklyProblems.size(); +} + +} // namespace Antares::Solver diff --git a/src/solver/lps/include/antares/solver/lps/LpsFromAntares.h b/src/solver/lps/include/antares/solver/lps/LpsFromAntares.h new file mode 100644 index 0000000000..f85d99b060 --- /dev/null +++ b/src/solver/lps/include/antares/solver/lps/LpsFromAntares.h @@ -0,0 +1,149 @@ + +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#pragma once +#include +#include +#include +#include +#include + +namespace Antares::Solver +{ + +/** + * @struct WeeklyProblemId + * @brief The WeeklyProblemId struct is used to identify a weekly problem by year and week. + */ +struct WeeklyProblemId +{ + unsigned int year = 0; + unsigned int week = 0; + // Order of comparison is order of member declaration + auto operator<=>(const WeeklyProblemId& other) const = default; +}; + +// Type de données inutile car les matrices de tous les pbs Weekly sont +// identiques. Cela pourra changer à l'avenir si des coefficients de contraintes +// couplantes peuvent varier au cours du temps (ex: rendement d'une pompe à +// chaleur qui varie selon la température, FlowBased ?, etc) +/** + * @class ConstantDataFromAntares + * @brief The ConstantDataFromAntares class is used to store constant data across all weeks + * of Antares problems. + */ +struct ConstantDataFromAntares +{ + unsigned VariablesCount = 0; // Mathématiquement : Nb colonnes de la matrice, + // Informatiquement = TypeDeVariable.size() + unsigned ConstraintesCount = 0; // Mathématiqument : Nb lignes de la matrice, + // Informatiquement = Mdeb.size() + unsigned CoeffCount = 0; // Mathématiquement : Nb coeffs non nuls de la + // matrice, Informatiquement = Nbterm.size() = + // IndicesColonnes.size()= + // CoefficientsDeLaMatriceDesContraintes.size() + + std::vector VariablesType; // Variables entières ou biniaires + std::vector Mdeb; // Indique dans les indices dans le vecteur IndicesColonnes qui + // correspondent au début de chaque ligne. Ex : Mdeb[3] = 8 et + // Mdeb[4] = 13 -> Les termes IndicesColonnes[8] à + // IndicesColonnes[12] correspondent à des Id de colonnes de la + // ligne 3 de la matrice (en supposant que les lignes sont indexées + // à partir de 0) + std::vector NotNullTermCount; // Nombre de termes non nuls sur chaque ligne. + // Inutile car NbTerm[i] = Mdeb[i+1] - Mdeb[i] + std::vector ColumnIndexes; // Id des colonnes des termes de + // CoefficientsDeLaMatriceDesContraintes : Ex + // IndicesColonnes[3] = 8 -> + // CoefficientsDeLaMatriceDesContraintes[8] donne la + // valeur du terme de la colonne 8, et de la ligne i où + // i est tel que Mdeb[i] <= 3 < Mdeb[i+1] + std::vector ConstraintsMatrixCoeff; // Coefficients de la matrice + + std::vector VariablesMeaning; + std::vector ConstraintsMeaning; + + auto operator<=>(const ConstantDataFromAntares& other) const = default; +}; + +/** + * @class WeeklyDataFromAntares + * @brief The WeeklyDataFromAntares class is used to store weekly data for an Antares Problem. + */ +struct WeeklyDataFromAntares +{ + std::vector Direction; // Sens de la contrainte : < ou > ou =, taille = + // NombreDeContraintes + std::vector Xmax; // Borne max des variables de la semaine + // considérée, taille = NombreDeVariables + std::vector Xmin; // Borne min des variables de la semaine + // considérée, taille = NombreDeVariables + std::vector LinearCost; // Coefficients du vecteur de coût de la fonction objectif, + // taille = NombreDeVariables + std::vector RHS; // Vecteur des second membre des contraintes, taille = + // NombreDeContraintes + std::string name; + + std::vector variables; + std::vector constraints; + + auto operator<=>(const WeeklyDataFromAntares& other) const = default; +}; + +using WeeklyDataByYearWeek = std::map; + +/** + * @class LpsFromAntares + * @brief The LpsFromAntares class is used to manage the constant and weekly data for Antares + * problems. + */ +class LpsFromAntares +{ +public: + /* + * @brief Checks if the LpsFromAntares object is empty. + * Emptiness is defined by either the constant data or the weekly data being empty. + */ + bool empty() const; + /* + * @brief Replaces the constant data in the LpsFromAntares object. + * Copy happens + */ + void setConstantData(const ConstantDataFromAntares& data); + /* + * @brief Adds weekly data to the LpsFromAntares object. + */ + void addWeeklyData(WeeklyProblemId id, const WeeklyDataFromAntares& data); + /* + * @brief Retrieves weekly data from the LpsFromAntares object. + */ + const WeeklyDataFromAntares& weeklyData(WeeklyProblemId id) const; + /* + * @brief Retrieves the number of weeks in the LpsFromAntares object. + */ + [[nodiscard]] size_t weekCount() const noexcept; + + ConstantDataFromAntares constantProblemData; + WeeklyDataByYearWeek weeklyProblems; +}; + +} // namespace Antares::Solver diff --git a/src/solver/main/economy.cpp b/src/solver/main/economy.cpp deleted file mode 100644 index f4fc8ba907..0000000000 --- a/src/solver/main/economy.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ - -#include "antares/solver/simulation/economy.h" - -#include -#include -#include "antares/application/application.h" -#include "antares/solver/simulation/solver.h" - -namespace Antares::Solver -{ -void Application::runSimulationInEconomicMode() -{ - // Type of the simulation - typedef Solver::Simulation::ISimulation SimulationType; - SimulationType simulation(*pStudy, pSettings, pDurationCollector, *resultWriter); - simulation.checkWriter(); - simulation.run(); - - if (!(pSettings.noOutput || pSettings.tsGeneratorsOnly)) - { - pDurationCollector("synthesis_export") - << [&simulation] { simulation.writeResults(/*synthesis:*/ true); }; - - this->pOptimizationInfo = simulation.getOptimizationInfo(); - } -} -} // namespace Antares::Solver diff --git a/src/solver/misc/options.cpp b/src/solver/misc/options.cpp index 9429b50e79..a09b83e17d 100644 --- a/src/solver/misc/options.cpp +++ b/src/solver/misc/options.cpp @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #include "antares/solver/misc/options.h" @@ -45,7 +45,7 @@ std::unique_ptr CreateParser(Settings& settings, StudyLoad { settings.reset(); - auto parser = std::unique_ptr(new Yuni::GetOpt::Parser()); + auto parser = std::make_unique(); parser->addParagraph(Yuni::String() << "Antares Solver v" << ANTARES_VERSION_PUB_STR << "\n"); diff --git a/src/solver/optimisation/CMakeLists.txt b/src/solver/optimisation/CMakeLists.txt index cbf8f33fc3..a6c6f01e66 100644 --- a/src/solver/optimisation/CMakeLists.txt +++ b/src/solver/optimisation/CMakeLists.txt @@ -172,7 +172,8 @@ set(RTESOLVER_OPT variables/VariableManagement.cpp variables/VariableManagerUtils.h variables/VariableManagerUtils.cpp - + include/antares/solver/optimisation/HebdoProblemToLpsTranslator.h + HebdoProblemToLpsTranslator.cpp ) @@ -196,6 +197,7 @@ target_link_libraries(model_antares antares-solver-simulation Antares::benchmarking Antares::optimization-options + Antares::lps PRIVATE infeasible_problem_analysis ) diff --git a/src/solver/optimisation/HebdoProblemToLpsTranslator.cpp b/src/solver/optimisation/HebdoProblemToLpsTranslator.cpp new file mode 100644 index 0000000000..1109e9c780 --- /dev/null +++ b/src/solver/optimisation/HebdoProblemToLpsTranslator.cpp @@ -0,0 +1,109 @@ + +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#include "antares/solver/optimisation/HebdoProblemToLpsTranslator.h" +#include "antares/solver/utils/filename.h" + +namespace Antares::Solver +{ + +namespace +{ +/** + * @brief Copies elements from one container to another. + * + * This function takes two containers as arguments. It copies elements from the first container to the second one. + * + * @param in The container from which to copy elements. + * @param out The container to which to copy elements. + */ +template +void copy(const T& in, U& out) +{ + std::ranges::copy(in, std::back_inserter(out)); +} +} + +WeeklyDataFromAntares HebdoProblemToLpsTranslator::translate( + const PROBLEME_ANTARES_A_RESOUDRE* problem, + std::string_view name) const +{ + if (problem == nullptr) + return {}; + auto ret = WeeklyDataFromAntares(); + + copy(problem->CoutLineaire, ret.LinearCost); + copy(problem->Xmax, ret.Xmax); + copy(problem->Xmin, ret.Xmin); + copy(problem->NomDesVariables, ret.variables); + copy(problem->NomDesContraintes, ret.constraints); + copy(problem->SecondMembre, ret.RHS); + copy(problem->Sens, ret.Direction); + + copy(name, ret.name); + + return ret; +} + +ConstantDataFromAntares HebdoProblemToLpsTranslator::commonProblemData(const PROBLEME_ANTARES_A_RESOUDRE* problem) const { + if (problem == nullptr) + return ConstantDataFromAntares(); + + if (problem->NombreDeVariables <= 0) { + throw WeeklyProblemTranslationException("VariablesCount must be strictly positive"); + } + if (problem->NombreDeContraintes <= 0) { + throw WeeklyProblemTranslationException("ConstraintesCount must be strictly positive"); + } + + if (problem->NombreDeContraintes > problem->IndicesDebutDeLigne.size()) { + throw WeeklyProblemTranslationException("ConstraintesCount exceed IndicesDebutDeLigne size"); + } + + if (problem->NombreDeContraintes > problem->NombreDeTermesDesLignes.size()) { + throw WeeklyProblemTranslationException("ConstraintesCount exceed NombreDeTermesDesLignes size"); + } + + ConstantDataFromAntares ret; + + ret.VariablesCount = problem->NombreDeVariables; + ret.ConstraintesCount = problem->NombreDeContraintes; + + ret.CoeffCount = problem->IndicesDebutDeLigne[problem->NombreDeContraintes - 1] + + problem->NombreDeTermesDesLignes[problem->NombreDeContraintes - 1]; + + copy(problem->TypeDeVariable, ret.VariablesType); + + copy(problem->CoefficientsDeLaMatriceDesContraintes, + ret.ConstraintsMatrixCoeff); + copy(problem->IndicesColonnes, ret.ColumnIndexes); + + copy(problem->IndicesDebutDeLigne, ret.Mdeb); + return ret; +} + +WeeklyProblemTranslationException::WeeklyProblemTranslationException( + const std::string& string) noexcept +: std::runtime_error{string} +{ +} +} // namespace Antares::Solver \ No newline at end of file diff --git a/src/solver/optimisation/adequacy_patch_local_matching/adequacy_patch_weekly_optimization.cpp b/src/solver/optimisation/adequacy_patch_local_matching/adequacy_patch_weekly_optimization.cpp index 577cfa55b7..174d32564d 100644 --- a/src/solver/optimisation/adequacy_patch_local_matching/adequacy_patch_weekly_optimization.cpp +++ b/src/solver/optimisation/adequacy_patch_local_matching/adequacy_patch_weekly_optimization.cpp @@ -1,28 +1,29 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #include "antares/solver/optimisation/adequacy_patch_local_matching/adequacy_patch_weekly_optimization.h" #include "antares/solver/optimisation/opt_fonctions.h" #include "antares/solver/simulation/adequacy_patch_runtime_data.h" +#include "antares/solver/simulation/ISimulationObserver.h" #include "antares/study/fwd.h" using namespace Antares::Data::AdequacyPatch; @@ -35,16 +36,18 @@ AdequacyPatchOptimization::AdequacyPatchOptimization(const Antares::Data::Study& PROBLEME_HEBDO* problemeHebdo, AdqPatchParams& adqPatchParams, uint thread_number, - IResultWriter& writer): - WeeklyOptimization(options, problemeHebdo, adqPatchParams, thread_number, writer), + IResultWriter& writer, + Simulation::ISimulationObserver& observer): + WeeklyOptimization(options, problemeHebdo, adqPatchParams, thread_number, writer, observer), study_(study) { } void AdequacyPatchOptimization::solve() { + Simulation::NullSimulationObserver nullSimulationObserver; problemeHebdo_->adequacyPatchRuntimeData->AdequacyFirstStep = true; - OPT_OptimisationHebdomadaire(options_, problemeHebdo_, adqPatchParams_, writer_); + OPT_OptimisationHebdomadaire(options_, problemeHebdo_, adqPatchParams_, writer_, nullSimulationObserver); problemeHebdo_->adequacyPatchRuntimeData->AdequacyFirstStep = false; for (uint32_t pays = 0; pays < problemeHebdo_->NombreDePays; ++pays) @@ -61,7 +64,7 @@ void AdequacyPatchOptimization::solve() } } - OPT_OptimisationHebdomadaire(options_, problemeHebdo_, adqPatchParams_, writer_); + OPT_OptimisationHebdomadaire(options_, problemeHebdo_, adqPatchParams_, writer_, nullSimulationObserver); } } // namespace Antares::Solver::Optimization diff --git a/src/solver/optimisation/base_weekly_optimization.cpp b/src/solver/optimisation/base_weekly_optimization.cpp index d4da2a1167..80efece4b9 100644 --- a/src/solver/optimisation/base_weekly_optimization.cpp +++ b/src/solver/optimisation/base_weekly_optimization.cpp @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #include "antares/solver/optimisation/base_weekly_optimization.h" @@ -34,12 +34,15 @@ WeeklyOptimization::WeeklyOptimization(const OptimizationOptions& options, PROBLEME_HEBDO* problemesHebdo, AdqPatchParams& adqPatchParams, uint thread_number, - IResultWriter& writer): + IResultWriter& writer, + Simulation::ISimulationObserver& simulationObserver + ) : options_(options), problemeHebdo_(problemesHebdo), adqPatchParams_(adqPatchParams), thread_number_(thread_number), - writer_(writer) + writer_(writer), + simulationObserver_(simulationObserver) { } @@ -48,7 +51,9 @@ std::unique_ptr WeeklyOptimization::create(const Antares::Da AdqPatchParams& adqPatchParams, PROBLEME_HEBDO* problemeHebdo, uint thread_number, - IResultWriter& writer) + IResultWriter& writer, + Simulation::ISimulationObserver& simulationObserver + ) { if (adqPatchParams.enabled && adqPatchParams.localMatching.enabled) { @@ -57,7 +62,8 @@ std::unique_ptr WeeklyOptimization::create(const Antares::Da problemeHebdo, adqPatchParams, thread_number, - writer); + writer, + simulationObserver); } else { @@ -65,7 +71,8 @@ std::unique_ptr WeeklyOptimization::create(const Antares::Da problemeHebdo, adqPatchParams, thread_number, - writer); + writer, + simulationObserver); } } diff --git a/src/solver/optimisation/include/antares/solver/optimisation/HebdoProblemToLpsTranslator.h b/src/solver/optimisation/include/antares/solver/optimisation/HebdoProblemToLpsTranslator.h new file mode 100644 index 0000000000..7deb23a0ca --- /dev/null +++ b/src/solver/optimisation/include/antares/solver/optimisation/HebdoProblemToLpsTranslator.h @@ -0,0 +1,81 @@ + +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#pragma once + +#include "antares/solver/lps/LpsFromAntares.h" +#include "antares/solver/simulation/sim_structure_probleme_economique.h" + +namespace Antares::Solver +{ + +/** + * @class WeeklyProblemTranslationException + * @brief Exception class for errors during the translation of a weekly problem. + * + * This class is a custom exception class that is thrown when an error occurs during the translation + * of a weekly problem. + */ +class WeeklyProblemTranslationException: public std::runtime_error +{ +public: + explicit WeeklyProblemTranslationException(const std::string& string) noexcept; +}; + +/** + * @class HebdoProblemToLpsTranslator + * @brief Class for translating a weekly problem to a linear programming problem. + * + * This class is responsible for translating a weekly problem to a linear programming problem. + */ +class HebdoProblemToLpsTranslator +{ +public: + /** + * @brief Translates a weekly problem to a linear programming problem. + * + * This function takes a pointer to a PROBLEME_ANTARES_A_RESOUDRE object and a string_view + * representing the name of the problem. It translates the weekly problem to a linear + * programming problem and returns a WeeklyDataFromAntaresPtr to the translated problem. Datas + * from the PROBLEME_ANTARES_A_RESOUDRE are copied to the WeeklyDataFromAntaresPtr. + * + * @param problem A pointer to the weekly problem to be translated. + * @param name The name of the problem. + * @return WeeklyDataFromAntaresPtr A WeeklyDataFromAntaresPtr to the translated problem. + */ + [[nodiscard]] WeeklyDataFromAntares translate(const PROBLEME_ANTARES_A_RESOUDRE* problem, + std::string_view name) const; + + /** + * @brief Retrieves common problem data, the part common to every weekly problems + * + * This function takes a pointer to a PROBLEME_ANTARES_A_RESOUDRE object and retrieves the + * common problem data. It returns a ConstantDataFromAntaresPtr to the common problem data. + * + * @param problem A pointer to the problem from which to retrieve the common data. + * @return ConstantDataFromAntaresPtr A ConstantDataFromAntaresPtr to the common problem data. + */ + [[nodiscard]] ConstantDataFromAntares commonProblemData( + const PROBLEME_ANTARES_A_RESOUDRE* problem) const; +}; + +} // namespace Antares::Solver diff --git a/src/solver/optimisation/include/antares/solver/optimisation/adequacy_patch_local_matching/adequacy_patch_weekly_optimization.h b/src/solver/optimisation/include/antares/solver/optimisation/adequacy_patch_local_matching/adequacy_patch_weekly_optimization.h index 2585fc8053..b11bdc817a 100644 --- a/src/solver/optimisation/include/antares/solver/optimisation/adequacy_patch_local_matching/adequacy_patch_weekly_optimization.h +++ b/src/solver/optimisation/include/antares/solver/optimisation/adequacy_patch_local_matching/adequacy_patch_weekly_optimization.h @@ -37,9 +37,10 @@ class AdequacyPatchOptimization: public WeeklyOptimization explicit AdequacyPatchOptimization(const Antares::Data::Study& study, const OptimizationOptions& options, PROBLEME_HEBDO* problemeHebdo, - Antares::Data::AdequacyPatch::AdqPatchParams&, + Data::AdequacyPatch::AdqPatchParams&, uint numSpace, - IResultWriter& writer); + IResultWriter& writer, + Simulation::ISimulationObserver& observer); ~AdequacyPatchOptimization() override = default; void solve() override; diff --git a/src/solver/optimisation/include/antares/solver/optimisation/base_weekly_optimization.h b/src/solver/optimisation/include/antares/solver/optimisation/base_weekly_optimization.h index 7f4b755bb5..e25ffca086 100644 --- a/src/solver/optimisation/include/antares/solver/optimisation/base_weekly_optimization.h +++ b/src/solver/optimisation/include/antares/solver/optimisation/base_weekly_optimization.h @@ -1,29 +1,30 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #pragma once #include // for "uint" definition #include +#include "antares/solver/simulation/ISimulationObserver.h" #include "antares/solver/simulation/sim_structure_donnees.h" #include "antares/solver/simulation/sim_structure_probleme_economique.h" @@ -40,18 +41,23 @@ class WeeklyOptimization Antares::Data::AdequacyPatch::AdqPatchParams& adqPatchParams, PROBLEME_HEBDO* problemesHebdo, uint numSpace, - IResultWriter& writer); + IResultWriter& writer, + Simulation::ISimulationObserver& simulationObserver + ); protected: explicit WeeklyOptimization(const OptimizationOptions& options, PROBLEME_HEBDO* problemesHebdo, Antares::Data::AdequacyPatch::AdqPatchParams&, uint numSpace, - IResultWriter& writer); + IResultWriter& writer, + Simulation::ISimulationObserver& simulationObserver + ); Antares::Solver::Optimization::OptimizationOptions options_; PROBLEME_HEBDO* const problemeHebdo_ = nullptr; Antares::Data::AdequacyPatch::AdqPatchParams& adqPatchParams_; const uint thread_number_ = 0; IResultWriter& writer_; + std::reference_wrapper simulationObserver_; }; } // namespace Antares::Solver::Optimization diff --git a/src/solver/optimisation/include/antares/solver/optimisation/constraints/constraint_builder_utils.h b/src/solver/optimisation/include/antares/solver/optimisation/constraints/constraint_builder_utils.h index 9e4981700e..9e940aa200 100644 --- a/src/solver/optimisation/include/antares/solver/optimisation/constraints/constraint_builder_utils.h +++ b/src/solver/optimisation/include/antares/solver/optimisation/constraints/constraint_builder_utils.h @@ -22,6 +22,7 @@ #include #include "ConstraintBuilder.h" +struct PROBLEME_HEBDO; ConstraintBuilderData NewGetConstraintBuilderFromProblemHebdoAndProblemAResoudre( PROBLEME_HEBDO* problemeHebdo, diff --git a/src/solver/optimisation/include/antares/solver/optimisation/opt_export_structure.h b/src/solver/optimisation/include/antares/solver/optimisation/opt_export_structure.h index 4d69d8711e..47aa3838e1 100644 --- a/src/solver/optimisation/include/antares/solver/optimisation/opt_export_structure.h +++ b/src/solver/optimisation/include/antares/solver/optimisation/opt_export_structure.h @@ -1,50 +1,32 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ -#ifndef __EXPORT_STRUCTURE__ -#define __EXPORT_STRUCTURE__ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ +#pragma once -#include -#include #include -#include - -#include #include "antares/antares/Enum.hpp" #include "antares/writer/i_writer.h" - -namespace Antares -{ -namespace Data -{ - -class Study; -} // namespace Data -} // namespace Antares - struct PROBLEME_HEBDO; void OPT_ExportInterco(Antares::Solver::IResultWriter& writer, PROBLEME_HEBDO* problemeHebdo); void OPT_ExportAreaName(Antares::Solver::IResultWriter& writer, const std::vector& areaNames); void OPT_ExportStructures(PROBLEME_HEBDO* problemeHebdo, Antares::Solver::IResultWriter& writer); - -#endif diff --git a/src/solver/optimisation/include/antares/solver/optimisation/opt_fonctions.h b/src/solver/optimisation/include/antares/solver/optimisation/opt_fonctions.h index 1a52f7e331..2150ea991a 100644 --- a/src/solver/optimisation/include/antares/solver/optimisation/opt_fonctions.h +++ b/src/solver/optimisation/include/antares/solver/optimisation/opt_fonctions.h @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #ifndef __SOLVER_OPTIMISATION_FUNCTIONS_H__ #define __SOLVER_OPTIMISATION_FUNCTIONS_H__ @@ -26,6 +26,7 @@ #include #include "antares/config/config.h" #include "antares/solver/optimisation/opt_structure_probleme_a_resoudre.h" +#include "antares/solver/simulation/ISimulationObserver.h" #include "antares/study/parameters/adq-patch-params.h" #include "adequacy_patch_csr/hourly_csr_problem.h" @@ -34,9 +35,10 @@ using AdqPatchParams = Antares::Data::AdequacyPatch::AdqPatchParams; using OptimizationOptions = Antares::Solver::Optimization::OptimizationOptions; void OPT_OptimisationHebdomadaire(const OptimizationOptions& options, - PROBLEME_HEBDO*, - const AdqPatchParams&, - Antares::Solver::IResultWriter& writer); + PROBLEME_HEBDO* pProblemeHebdo, + const AdqPatchParams& adqPatchParams, + Solver::IResultWriter& writer, + Solver::Simulation::ISimulationObserver& simulationObserver); void OPT_NumeroDeJourDuPasDeTemps(PROBLEME_HEBDO*); void OPT_NumeroDIntervalleOptimiseDuPasDeTemps(PROBLEME_HEBDO*); void OPT_ConstruireLaListeDesVariablesOptimiseesDuProblemeLineaire(PROBLEME_HEBDO*); @@ -65,9 +67,11 @@ bool ADQ_PATCH_CSR(PROBLEME_ANTARES_A_RESOUDRE&, int year); bool OPT_PilotageOptimisationLineaire(const OptimizationOptions& options, - PROBLEME_HEBDO*, - const AdqPatchParams&, - Antares::Solver::IResultWriter& writer); + PROBLEME_HEBDO* problemeHebdo, + const AdqPatchParams& adqPatchParams, + Solver::IResultWriter& writer, + Solver::Simulation::ISimulationObserver& simulationObserver + ); void OPT_VerifierPresenceReserveJmoins1(PROBLEME_HEBDO*); bool OPT_PilotageOptimisationQuadratique(PROBLEME_HEBDO*); @@ -85,9 +89,11 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options, void OPT_LiberationProblemesSimplexe(const OptimizationOptions& options, const PROBLEME_HEBDO*); bool OPT_OptimisationLineaire(const OptimizationOptions& options, - PROBLEME_HEBDO*, - const AdqPatchParams&, - Antares::Solver::IResultWriter& writer); + PROBLEME_HEBDO* problemeHebdo, + const AdqPatchParams& adqPatchParams, + Solver::IResultWriter& writer, + Solver::Simulation::ISimulationObserver& simulationObserver + ); void OPT_RestaurerLesDonnees(PROBLEME_HEBDO*); /*------------------------------*/ diff --git a/src/solver/optimisation/include/antares/solver/optimisation/weekly_optimization.h b/src/solver/optimisation/include/antares/solver/optimisation/weekly_optimization.h index 1f9ae23137..d0f33adb39 100644 --- a/src/solver/optimisation/include/antares/solver/optimisation/weekly_optimization.h +++ b/src/solver/optimisation/include/antares/solver/optimisation/weekly_optimization.h @@ -1,27 +1,29 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #pragma once #include "antares/solver/simulation/sim_structure_probleme_economique.h" +#include "antares/solver/simulation/ISimulationObserver.h" + #include "base_weekly_optimization.h" @@ -34,7 +36,9 @@ class DefaultWeeklyOptimization: public WeeklyOptimization PROBLEME_HEBDO* problemeHebdo, Antares::Data::AdequacyPatch::AdqPatchParams&, uint numSpace, - IResultWriter& writer); + IResultWriter& writer, + Simulation::ISimulationObserver& simulationObserver + ); ~DefaultWeeklyOptimization() override = default; void solve() override; }; diff --git a/src/solver/optimisation/opt_optimisation_hebdo.cpp b/src/solver/optimisation/opt_optimisation_hebdo.cpp index 89c59ec57a..6883089378 100644 --- a/src/solver/optimisation/opt_optimisation_hebdo.cpp +++ b/src/solver/optimisation/opt_optimisation_hebdo.cpp @@ -1,29 +1,30 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #include #include #include #include "antares/solver/optimisation/opt_fonctions.h" #include "antares/solver/optimisation/opt_structure_probleme_a_resoudre.h" +#include "antares/solver/simulation/ISimulationObserver.h" #include "antares/solver/simulation/sim_extern_variables_globales.h" extern "C" @@ -39,11 +40,13 @@ using Antares::Solver::Optimization::OptimizationOptions; void OPT_OptimisationHebdomadaire(const OptimizationOptions& options, PROBLEME_HEBDO* pProblemeHebdo, const AdqPatchParams& adqPatchParams, - Solver::IResultWriter& writer) + Solver::IResultWriter& writer, + Solver::Simulation::ISimulationObserver& simulationObserver) { if (pProblemeHebdo->TypeDOptimisation == OPTIMISATION_LINEAIRE) { - if (!OPT_PilotageOptimisationLineaire(options, pProblemeHebdo, adqPatchParams, writer)) + if (!OPT_PilotageOptimisationLineaire( + options, pProblemeHebdo, adqPatchParams, writer, simulationObserver)) { logs.error() << "Linear optimization failed"; throw UnfeasibleProblemError("Linear optimization failed"); diff --git a/src/solver/optimisation/opt_optimisation_lineaire.cpp b/src/solver/optimisation/opt_optimisation_lineaire.cpp index 67c475e396..d3635ff326 100644 --- a/src/solver/optimisation/opt_optimisation_lineaire.cpp +++ b/src/solver/optimisation/opt_optimisation_lineaire.cpp @@ -1,29 +1,32 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #include +#include "antares/solver/lps/LpsFromAntares.h" +#include "antares/solver/optimisation/HebdoProblemToLpsTranslator.h" #include "antares/solver/optimisation/LinearProblemMatrix.h" #include "antares/solver/optimisation/constraints/constraint_builder_utils.h" #include "antares/solver/optimisation/opt_export_structure.h" #include "antares/solver/optimisation/opt_fonctions.h" +#include "antares/solver/simulation/ISimulationObserver.h" #include "antares/solver/simulation/sim_structure_probleme_economique.h" #include "antares/solver/utils/filename.h" using namespace Antares; @@ -61,11 +64,27 @@ void OPT_EcrireResultatFonctionObjectiveAuFormatTXT( writer.addEntryFromBuffer(filename, buffer); } +namespace +{ +void notifyProblemHebdo(const PROBLEME_HEBDO* problemeHebdo, + int optimizationNumber, + Solver::Simulation::ISimulationObserver& simulationObserver, + const OptPeriodStringGenerator* optPeriodStringGenerator) +{ + simulationObserver.notifyHebdoProblem(*problemeHebdo, + optimizationNumber, + createMPSfilename(*optPeriodStringGenerator, + optimizationNumber)); +} +} + bool runWeeklyOptimization(const OptimizationOptions& options, PROBLEME_HEBDO* problemeHebdo, const AdqPatchParams& adqPatchParams, Solver::IResultWriter& writer, - int optimizationNumber) + int optimizationNumber, + Solver::Simulation::ISimulationObserver& simulationObserver + ) { const int NombreDePasDeTempsPourUneOptimisation = problemeHebdo ->NombreDePasDeTempsPourUneOptimisation; @@ -102,6 +121,8 @@ bool runWeeklyOptimization(const OptimizationOptions& options, problemeHebdo->weekInTheYear, problemeHebdo->year); + notifyProblemHebdo(problemeHebdo, optimizationNumber, simulationObserver, optPeriodStringGenerator.get()); + if (!OPT_AppelDuSimplexe(options, problemeHebdo, numeroDeLIntervalle, @@ -142,7 +163,9 @@ void runThermalHeuristic(PROBLEME_HEBDO* problemeHebdo) bool OPT_OptimisationLineaire(const OptimizationOptions& options, PROBLEME_HEBDO* problemeHebdo, const AdqPatchParams& adqPatchParams, - Solver::IResultWriter& writer) + Solver::IResultWriter& writer, + Solver::Simulation::ISimulationObserver& simulationObserver + ) { if (!problemeHebdo->OptimisationAuPasHebdomadaire) { @@ -175,7 +198,8 @@ bool OPT_OptimisationLineaire(const OptimizationOptions& options, problemeHebdo, adqPatchParams, writer, - PREMIERE_OPTIMISATION); + PREMIERE_OPTIMISATION, + simulationObserver); // We only need the 2nd optimization when NOT solving with integer variables // We also skip the 2nd optimization in the hidden 'Expansion' mode @@ -188,7 +212,8 @@ bool OPT_OptimisationLineaire(const OptimizationOptions& options, problemeHebdo, adqPatchParams, writer, - DEUXIEME_OPTIMISATION); + DEUXIEME_OPTIMISATION, + simulationObserver); } return ret; } diff --git a/src/solver/optimisation/opt_pilotage_optimisation_lineaire.cpp b/src/solver/optimisation/opt_pilotage_optimisation_lineaire.cpp index 79ce32f0cb..2ca9c71d56 100644 --- a/src/solver/optimisation/opt_pilotage_optimisation_lineaire.cpp +++ b/src/solver/optimisation/opt_pilotage_optimisation_lineaire.cpp @@ -1,27 +1,28 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #include "antares/optimization-options/options.h" #include "antares/solver/optimisation/opt_fonctions.h" #include "antares/solver/optimisation/opt_structure_probleme_a_resoudre.h" +#include "antares/solver/simulation/ISimulationObserver.h" #include "antares/solver/simulation/sim_extern_variables_globales.h" using Antares::Solver::Optimization::OptimizationOptions; @@ -29,7 +30,8 @@ using Antares::Solver::Optimization::OptimizationOptions; bool OPT_PilotageOptimisationLineaire(const OptimizationOptions& options, PROBLEME_HEBDO* problemeHebdo, const AdqPatchParams& adqPatchParams, - Solver::IResultWriter& writer) + Solver::IResultWriter& writer, + Solver::Simulation::ISimulationObserver& simulationObserver) { if (!problemeHebdo->LeProblemeADejaEteInstancie) { @@ -75,5 +77,5 @@ bool OPT_PilotageOptimisationLineaire(const OptimizationOptions& options, OPT_InitialiserNombreMinEtMaxDeGroupesCoutsDeDemarrage(problemeHebdo); } - return OPT_OptimisationLineaire(options, problemeHebdo, adqPatchParams, writer); + return OPT_OptimisationLineaire(options, problemeHebdo, adqPatchParams, writer, simulationObserver); } diff --git a/src/solver/optimisation/opt_rename_problem.cpp b/src/solver/optimisation/opt_rename_problem.cpp index 7a7553a59a..85edf581b8 100644 --- a/src/solver/optimisation/opt_rename_problem.cpp +++ b/src/solver/optimisation/opt_rename_problem.cpp @@ -21,6 +21,7 @@ #include "antares/solver/optimisation/opt_rename_problem.h" +#include #include const std::string HOUR("hour"); diff --git a/src/solver/optimisation/weekly_optimization.cpp b/src/solver/optimisation/weekly_optimization.cpp index 164cabf32f..cf943b2125 100644 --- a/src/solver/optimisation/weekly_optimization.cpp +++ b/src/solver/optimisation/weekly_optimization.cpp @@ -1,22 +1,22 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . */ #include "antares/solver/optimisation/weekly_optimization.h" @@ -29,14 +29,15 @@ DefaultWeeklyOptimization::DefaultWeeklyOptimization(const OptimizationOptions& PROBLEME_HEBDO* problemeHebdo, AdqPatchParams& adqPatchParams, uint thread_number, - IResultWriter& writer): - WeeklyOptimization(options, problemeHebdo, adqPatchParams, thread_number, writer) + IResultWriter& writer, + Simulation::ISimulationObserver& simulationObserver) : + WeeklyOptimization(options, problemeHebdo, adqPatchParams, thread_number, writer, simulationObserver) { } void DefaultWeeklyOptimization::solve() { - OPT_OptimisationHebdomadaire(options_, problemeHebdo_, adqPatchParams_, writer_); + OPT_OptimisationHebdomadaire(options_, problemeHebdo_, adqPatchParams_, writer_, simulationObserver_.get()); } } // namespace Antares::Solver::Optimization diff --git a/src/solver/simulation/CMakeLists.txt b/src/solver/simulation/CMakeLists.txt index 0d2bc666eb..6a5355ea56 100644 --- a/src/solver/simulation/CMakeLists.txt +++ b/src/solver/simulation/CMakeLists.txt @@ -22,36 +22,37 @@ set(SRC_SIMULATION apply-scenario.cpp - # Solver - include/antares/solver/simulation/solver_utils.h - solver_utils.cpp - include/antares/solver/simulation/solver.h - include/antares/solver/simulation/solver.hxx - include/antares/solver/simulation/solver.data.h - solver.data.cpp - include/antares/solver/simulation/common-eco-adq.h - common-eco-adq.cpp - common-hydro-remix.cpp - common-hydro-levels.cpp - include/antares/solver/simulation/adequacy.h - adequacy.cpp - include/antares/solver/simulation/economy.h - economy.cpp - include/antares/solver/simulation/base_post_process.h - base_post_process.cpp - include/antares/solver/simulation/opt_time_writer.h - opt_time_writer.cpp - include/antares/solver/simulation/adequacy_patch_runtime_data.h - adequacy_patch_runtime_data.cpp - include/antares/solver/simulation/ITimeSeriesNumbersWriter.h + # Solver + include/antares/solver/simulation/solver_utils.h + solver_utils.cpp + include/antares/solver/simulation/solver.h + include/antares/solver/simulation/solver.hxx + include/antares/solver/simulation/solver.data.h + solver.data.cpp + include/antares/solver/simulation/common-eco-adq.h + common-eco-adq.cpp + common-hydro-remix.cpp + common-hydro-levels.cpp + include/antares/solver/simulation/adequacy.h + adequacy.cpp + include/antares/solver/simulation/economy.h + economy.cpp + include/antares/solver/simulation/base_post_process.h + base_post_process.cpp + include/antares/solver/simulation/opt_time_writer.h + opt_time_writer.cpp + include/antares/solver/simulation/adequacy_patch_runtime_data.h + adequacy_patch_runtime_data.cpp + include/antares/solver/simulation/ITimeSeriesNumbersWriter.h include/antares/solver/simulation/hydro-final-reservoir-level-functions.h hydro-final-reservoir-level-functions.cpp - TimeSeriesNumbersWriter.cpp + TimeSeriesNumbersWriter.cpp include/antares/solver/simulation/BindingConstraintsTimeSeriesNumbersWriter.h - economy_mode.cpp - adequacy_mode.cpp - include/antares/solver/simulation/economy_mode.h - include/antares/solver/simulation/adequacy_mode.h + include/antares/solver/simulation/ISimulationObserver.h + economy_mode.cpp + adequacy_mode.cpp + include/antares/solver/simulation/economy_mode.h + include/antares/solver/simulation/adequacy_mode.h ) source_group("simulation" FILES ${SRC_SIMULATION}) diff --git a/src/solver/simulation/adequacy.cpp b/src/solver/simulation/adequacy.cpp index c2ac21dbea..8ba97fde33 100644 --- a/src/solver/simulation/adequacy.cpp +++ b/src/solver/simulation/adequacy.cpp @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #include "antares/solver/simulation/adequacy.h" @@ -29,10 +29,10 @@ using Antares::Constants::nbHoursInAWeek; namespace Antares::Solver::Simulation { -Adequacy::Adequacy(Data::Study& study, IResultWriter& resultWriter): - study(study), - preproOnly(false), - resultWriter(resultWriter) +Adequacy::Adequacy(Data::Study& study, + IResultWriter& resultWriter, + Simulation::ISimulationObserver& simulationObserver) : + study(study), resultWriter(resultWriter), simulationObserver_(simulationObserver) { } @@ -207,7 +207,8 @@ bool Adequacy::year(Progression::Task& progression, OPT_OptimisationHebdomadaire(createOptimizationOptions(study), ¤tProblem, study.parameters.adqPatchParams, - resultWriter); + resultWriter, + simulationObserver_.get()); computingHydroLevels(study.areas, currentProblem, false); @@ -367,7 +368,7 @@ bool Adequacy::year(Progression::Task& progression, return true; } -void Adequacy::incrementProgression(Progression::Task& progression) +void Adequacy::incrementProgression(Progression::Task& progression) const { for (uint w = 0; w < pNbWeeks; ++w) { diff --git a/src/solver/simulation/adequacy_mode.cpp b/src/solver/simulation/adequacy_mode.cpp index 59048ddf7b..86435743ce 100644 --- a/src/solver/simulation/adequacy_mode.cpp +++ b/src/solver/simulation/adequacy_mode.cpp @@ -1,23 +1,23 @@ /* - * Copyright 2007-2024, RTE (https://www.rte-france.com) - * See AUTHORS.txt - * SPDX-License-Identifier: MPL-2.0 - * This file is part of Antares-Simulator, - * Adequacy and Performance assessment for interconnected energy networks. - * - * Antares_Simulator is free software: you can redistribute it and/or modify - * it under the terms of the Mozilla Public Licence 2.0 as published by - * the Mozilla Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * Antares_Simulator is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Mozilla Public Licence 2.0 for more details. - * - * You should have received a copy of the Mozilla Public Licence 2.0 - * along with Antares_Simulator. If not, see . - */ +* Copyright 2007-2024, RTE (https://www.rte-france.com) +* See AUTHORS.txt +* SPDX-License-Identifier: MPL-2.0 +* This file is part of Antares-Simulator, +* Adequacy and Performance assessment for interconnected energy networks. +* +* Antares_Simulator is free software: you can redistribute it and/or modify +* it under the terms of the Mozilla Public Licence 2.0 as published by +* the Mozilla Foundation, either version 2 of the License, or +* (at your option) any later version. +* +* Antares_Simulator is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* Mozilla Public Licence 2.0 for more details. +* +* You should have received a copy of the Mozilla Public Licence 2.0 +* along with Antares_Simulator. If not, see . +*/ #include "antares/solver/simulation/adequacy_mode.h" @@ -27,23 +27,24 @@ namespace Antares::Solver { void runSimulationInAdequacyMode(Antares::Data::Study& study, - const Settings& settings, + const Settings& settings, Benchmarking::DurationCollector& durationCollector, - IResultWriter& resultWriter, - Benchmarking::OptimizationInfo& info) + IResultWriter& resultWriter, + Benchmarking::OptimizationInfo& info, + Simulation::ISimulationObserver& simulationObserver) { - // Type of the simulation - typedef Solver::Simulation::ISimulation SimulationType; - SimulationType simulation(study, settings, durationCollector, resultWriter); - simulation.checkWriter(); - simulation.run(); + // Type of the simulation + typedef Solver::Simulation::ISimulation SimulationType; + SimulationType simulation(study, settings, durationCollector, resultWriter, simulationObserver); + simulation.checkWriter(); + simulation.run(); - if (!(settings.noOutput || settings.tsGeneratorsOnly)) - { + if (!(settings.noOutput || settings.tsGeneratorsOnly)) + { durationCollector("synthesis_export") << [&simulation] { simulation.writeResults(/*synthesis:*/ true); }; - info = simulation.getOptimizationInfo(); - } + info = simulation.getOptimizationInfo(); + } } } // namespace Antares::Solver diff --git a/src/solver/simulation/common-eco-adq.cpp b/src/solver/simulation/common-eco-adq.cpp index bc63e19708..4917e76f09 100644 --- a/src/solver/simulation/common-eco-adq.cpp +++ b/src/solver/simulation/common-eco-adq.cpp @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #include "antares/solver/simulation/common-eco-adq.h" @@ -91,10 +91,12 @@ static void RecalculDesEchangesMoyens(Data::Study& study, try { NullResultWriter resultWriter; + NullSimulationObserver simulationObserver; OPT_OptimisationHebdomadaire(createOptimizationOptions(study), &problem, study.parameters.adqPatchParams, - resultWriter); + resultWriter, + simulationObserver); } catch (Data::UnfeasibleProblemError&) { diff --git a/src/solver/simulation/economy.cpp b/src/solver/simulation/economy.cpp index 4baef3d5b4..1fa0b63625 100644 --- a/src/solver/simulation/economy.cpp +++ b/src/solver/simulation/economy.cpp @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #include "antares/solver/simulation/economy.h" @@ -33,10 +33,13 @@ using Antares::Constants::nbHoursInAWeek; namespace Antares::Solver::Simulation { -Economy::Economy(Data::Study& study, IResultWriter& resultWriter): +Economy::Economy(Data::Study& study, + IResultWriter& resultWriter, + Simulation::ISimulationObserver& simulationObserver) : study(study), preproOnly(false), - resultWriter(resultWriter) + resultWriter(resultWriter), + simulationObserver_(simulationObserver) { } @@ -82,7 +85,8 @@ bool Economy::simulationBegin() study.parameters.adqPatchParams, &pProblemesHebdo[numSpace], numSpace, - resultWriter); + resultWriter, + simulationObserver_.get()); postProcessesList_[numSpace] = interfacePostProcessList::create( study.parameters.adqPatchParams, &pProblemesHebdo[numSpace], diff --git a/src/solver/simulation/economy_mode.cpp b/src/solver/simulation/economy_mode.cpp index e79d7a1363..816eb1a212 100644 --- a/src/solver/simulation/economy_mode.cpp +++ b/src/solver/simulation/economy_mode.cpp @@ -1,23 +1,23 @@ /* - * Copyright 2007-2024, RTE (https://www.rte-france.com) - * See AUTHORS.txt - * SPDX-License-Identifier: MPL-2.0 - * This file is part of Antares-Simulator, - * Adequacy and Performance assessment for interconnected energy networks. - * - * Antares_Simulator is free software: you can redistribute it and/or modify - * it under the terms of the Mozilla Public Licence 2.0 as published by - * the Mozilla Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * Antares_Simulator is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Mozilla Public Licence 2.0 for more details. - * - * You should have received a copy of the Mozilla Public Licence 2.0 - * along with Antares_Simulator. If not, see . - */ +* Copyright 2007-2024, RTE (https://www.rte-france.com) +* See AUTHORS.txt +* SPDX-License-Identifier: MPL-2.0 +* This file is part of Antares-Simulator, +* Adequacy and Performance assessment for interconnected energy networks. +* +* Antares_Simulator is free software: you can redistribute it and/or modify +* it under the terms of the Mozilla Public Licence 2.0 as published by +* the Mozilla Foundation, either version 2 of the License, or +* (at your option) any later version. +* +* Antares_Simulator is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* Mozilla Public Licence 2.0 for more details. +* +* You should have received a copy of the Mozilla Public Licence 2.0 +* along with Antares_Simulator. If not, see . +*/ #include "antares/solver/simulation/economy_mode.h" @@ -27,23 +27,25 @@ namespace Antares::Solver { void runSimulationInEconomicMode(Antares::Data::Study& study, - const Settings& settings, + const Settings& settings, Benchmarking::DurationCollector& durationCollector, - IResultWriter& resultWriter, - Benchmarking::OptimizationInfo& info) + IResultWriter& resultWriter, + Benchmarking::OptimizationInfo& info, + Simulation::ISimulationObserver& simulationObserver +) { - // Type of the simulation - typedef Solver::Simulation::ISimulation SimulationType; - SimulationType simulation(study, settings, durationCollector, resultWriter); - simulation.checkWriter(); - simulation.run(); + // Type of the simulation + typedef Solver::Simulation::ISimulation SimulationType; + SimulationType simulation(study, settings, durationCollector, resultWriter, simulationObserver); + simulation.checkWriter(); + simulation.run(); - if (!(settings.noOutput || settings.tsGeneratorsOnly)) - { + if (!(settings.noOutput || settings.tsGeneratorsOnly)) + { durationCollector("synthesis_export") << [&simulation] { simulation.writeResults(/*synthesis:*/ true); }; - info = simulation.getOptimizationInfo(); - } + info = simulation.getOptimizationInfo(); + } } } // namespace Antares::Solver diff --git a/src/solver/simulation/include/antares/solver/simulation/ISimulationObserver.h b/src/solver/simulation/include/antares/solver/simulation/ISimulationObserver.h new file mode 100644 index 0000000000..414695a580 --- /dev/null +++ b/src/solver/simulation/include/antares/solver/simulation/ISimulationObserver.h @@ -0,0 +1,65 @@ + +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#pragma once + +#include "sim_structure_probleme_economique.h" + +namespace Antares::Solver::Simulation +{ + +/** + * @class ISimulationObserver + * @brief The ISimulationObserver class is an interface for observing the simulation. + * @details It declares the notifyHebdoProblem method. + */ +class ISimulationObserver +{ +public: + virtual ~ISimulationObserver() = default; + /** + * @brief The notifyHebdoProblem method is used to notify of a problem during the simulation. + * @param problemeHebdo A pointer to a PROBLEME_HEBDO object representing the problem. + * @param optimizationNumber The number of the optimization. + * @param name The name of the problem. + */ + virtual void notifyHebdoProblem(const PROBLEME_HEBDO& problemeHebdo, + int optimizationNumber, + std::string_view name) + = 0; +}; + +/** + * @class NullSimulationObserver + * @brief The NullSimulationObserver class is a null object for the ISimulationObserver interface. + * @details It overrides the notifyHebdoProblem method with an empty implementation. + */ +class NullSimulationObserver : public ISimulationObserver +{ +public: + ~NullSimulationObserver() override = default; + void notifyHebdoProblem(const PROBLEME_HEBDO&, int, std::string_view) override + { + //null object pattern + } +}; +} // namespace Antares::Solver::Simulation \ No newline at end of file diff --git a/src/solver/simulation/include/antares/solver/simulation/adequacy.h b/src/solver/simulation/include/antares/solver/simulation/adequacy.h index 6d659103c3..130f3c18f5 100644 --- a/src/solver/simulation/include/antares/solver/simulation/adequacy.h +++ b/src/solver/simulation/include/antares/solver/simulation/adequacy.h @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #ifndef __SOLVER_SIMULATION_ADEQUACY_H__ #define __SOLVER_SIMULATION_ADEQUACY_H__ @@ -41,7 +41,6 @@ class Adequacy return "adequacy"; } -public: //! \name Constructor & Destructor //@{ /*! @@ -49,20 +48,20 @@ class Adequacy ** ** \param study The current study */ - Adequacy(Data::Study& study, IResultWriter& resultWriter); + Adequacy(Data::Study& study, IResultWriter& resultWriter, + Simulation::ISimulationObserver& simulationObserver); //! Destructor ~Adequacy() = default; //@} Benchmarking::OptimizationInfo getOptimizationInfo() const; -public: //! Current study Data::Study& study; //! All variables Solver::Variable::Adequacy::AllVariables variables; //! Prepro only - bool preproOnly; + bool preproOnly = false; protected: void setNbPerformedYearsInParallel(uint nbMaxPerformedYearsInParallel); @@ -79,7 +78,7 @@ class Adequacy OptimizationStatisticsWriter& optWriter, const Antares::Data::Area::ScratchMap& scratchmap); - void incrementProgression(Progression::Task& progression); + void incrementProgression(Progression::Task& progression) const; void simulationEnd(); @@ -102,6 +101,7 @@ class Adequacy Matrix<> pRES; IResultWriter& resultWriter; + std::reference_wrapper simulationObserver_; }; // class Adequacy } // namespace Antares::Solver::Simulation diff --git a/src/solver/simulation/include/antares/solver/simulation/adequacy_mode.h b/src/solver/simulation/include/antares/solver/simulation/adequacy_mode.h index 260ba2c92f..66734027b9 100644 --- a/src/solver/simulation/include/antares/solver/simulation/adequacy_mode.h +++ b/src/solver/simulation/include/antares/solver/simulation/adequacy_mode.h @@ -24,6 +24,7 @@ #include "antares/infoCollection/StudyInfoCollector.h" #include "antares/solver/misc/options.h" +#include "antares/solver/simulation/ISimulationObserver.h" #include "antares/writer/i_writer.h" namespace Antares::Solver @@ -32,5 +33,6 @@ void runSimulationInAdequacyMode(Antares::Data::Study& study, const Settings& settings, Benchmarking::DurationCollector& durationCollector, IResultWriter& resultWriter, - Benchmarking::OptimizationInfo& info); + Benchmarking::OptimizationInfo& info, + Simulation::ISimulationObserver& simulationObserver); } diff --git a/src/solver/simulation/include/antares/solver/simulation/economy.h b/src/solver/simulation/include/antares/solver/simulation/economy.h index b7661c3f11..592fac59f8 100644 --- a/src/solver/simulation/include/antares/solver/simulation/economy.h +++ b/src/solver/simulation/include/antares/solver/simulation/economy.h @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #ifndef __SOLVER_SIMULATION_ECONOMY_H__ #define __SOLVER_SIMULATION_ECONOMY_H__ @@ -50,7 +50,9 @@ class Economy ** ** \param study The current study */ - Economy(Data::Study& study, IResultWriter& resultWriter); + Economy(Data::Study& study, + IResultWriter& resultWriter, + Simulation::ISimulationObserver& simulationObserver); //! Destructor ~Economy() = default; //@} @@ -100,6 +102,7 @@ class Economy weeklyOptProblems_; std::vector> postProcessesList_; IResultWriter& resultWriter; + std::reference_wrapper simulationObserver_; }; // class Economy } // namespace Antares::Solver::Simulation diff --git a/src/solver/simulation/include/antares/solver/simulation/economy_mode.h b/src/solver/simulation/include/antares/solver/simulation/economy_mode.h index 5e1f454884..7437d39cf1 100644 --- a/src/solver/simulation/include/antares/solver/simulation/economy_mode.h +++ b/src/solver/simulation/include/antares/solver/simulation/economy_mode.h @@ -24,6 +24,7 @@ #include "antares/infoCollection/StudyInfoCollector.h" #include "antares/solver/misc/options.h" +#include "antares/solver/simulation/ISimulationObserver.h" #include "antares/writer/i_writer.h" namespace Antares::Solver @@ -32,5 +33,7 @@ void runSimulationInEconomicMode(Antares::Data::Study& study, const Settings& settings, Benchmarking::DurationCollector& durationCollector, IResultWriter& resultWriter, - Benchmarking::OptimizationInfo& info); + Benchmarking::OptimizationInfo& info, + Simulation::ISimulationObserver& simulationObserver +); } diff --git a/src/solver/simulation/include/antares/solver/simulation/solver.h b/src/solver/simulation/include/antares/solver/simulation/solver.h index fe16b660b9..cd2abfa366 100644 --- a/src/solver/simulation/include/antares/solver/simulation/solver.h +++ b/src/solver/simulation/include/antares/solver/simulation/solver.h @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #ifndef __SOLVER_SIMULATION_SOLVER_H__ #define __SOLVER_SIMULATION_SOLVER_H__ @@ -26,6 +26,7 @@ #include #include +#include #include #include #include "antares/solver/hydro/management/management.h" @@ -54,7 +55,8 @@ class ISimulation: public Impl ISimulation(Data::Study& study, const ::Settings& settings, Benchmarking::DurationCollector& duration_collector, - IResultWriter& resultWriter); + IResultWriter& resultWriter, + Simulation::ISimulationObserver& simulationObserver); //! Destructor ~ISimulation(); //@} @@ -76,7 +78,6 @@ class ISimulation: public Impl */ void writeResults(bool synthesis, uint year = 0, uint numSpace = 9999); -public: //! Reference to the current study Data::Study& study; //! The global settings @@ -136,7 +137,6 @@ class ISimulation: public Impl */ void loopThroughYears(uint firstYear, uint endYear, std::vector& state); -private: //! Some temporary to avoid performing useless complex checks Solver::Private::Simulation::CacheData pData; //! @@ -160,6 +160,7 @@ class ISimulation: public Impl //! Result writer Antares::Solver::IResultWriter& pResultWriter; + std::reference_wrapper simulationObserver_; }; // class ISimulation } // namespace Antares::Solver::Simulation diff --git a/src/solver/simulation/include/antares/solver/simulation/solver.hxx b/src/solver/simulation/include/antares/solver/simulation/solver.hxx index 232fa520fc..57dff06fd9 100644 --- a/src/solver/simulation/include/antares/solver/simulation/solver.hxx +++ b/src/solver/simulation/include/antares/solver/simulation/solver.hxx @@ -31,8 +31,8 @@ #include #include #include "antares/concurrency/concurrency.h" -#include "antares/solver//variable/constants.h" -#include "antares/solver//variable/print.h" +#include "antares/solver/variable/constants.h" +#include "antares/solver/variable/print.h" #include "antares/solver/hydro/management/management.h" // Added for use of randomReservoirLevel(...) #include "antares/solver/simulation/apply-scenario.h" #include "antares/solver/simulation/opt_time_writer.h" @@ -61,7 +61,8 @@ public: Variable::State& pState, bool pYearByYear, Benchmarking::DurationCollector& durationCollector, - IResultWriter& resultWriter): + IResultWriter& resultWriter, + ISimulationObserver& simulationObserver): simulation_(simulation), y(pY), yearFailed(pYearFailed), @@ -75,6 +76,7 @@ public: yearByYear(pYearByYear), pDurationCollector(durationCollector), pResultWriter(resultWriter), + simulationObserver_(simulationObserver), hydroManagement(study.areas, study.parameters, study.calendar, @@ -101,6 +103,7 @@ private: bool yearByYear; Benchmarking::DurationCollector& pDurationCollector; IResultWriter& pResultWriter; + std::reference_wrapper simulationObserver_; HydroManagement hydroManagement; Antares::Data::Area::ScratchMap scratchmap; @@ -235,8 +238,9 @@ inline ISimulation::ISimulation( Data::Study& study, const ::Settings& settings, Benchmarking::DurationCollector& duration_collector, - IResultWriter& resultWriter): - ImplementationType(study, resultWriter), + IResultWriter& resultWriter, + Simulation::ISimulationObserver& simulationObserver): + ImplementationType(study, resultWriter, simulationObserver), study(study), settings(settings), pNbYearsReallyPerformed(0), @@ -245,7 +249,8 @@ inline ISimulation::ISimulation( pFirstSetParallelWithAPerformedYearWasRun(false), pDurationCollector(duration_collector), pQueueService(study.pQueueService), - pResultWriter(resultWriter) + pResultWriter(resultWriter), + simulationObserver_(simulationObserver) { // Ask to the interface to show the messages logs.info(); @@ -300,9 +305,6 @@ void ISimulation::run() // Determine if we have to use the preprocessors at least one time. pData.initialize(study.parameters); - // Prepro only ? - ImplementationType::preproOnly = settings.tsGeneratorsOnly; - ImplementationType::setNbPerformedYearsInParallel(pNbMaxPerformedYearsInParallel); TSGenerator::ResizeGeneratedTimeSeries(study.areas, study.parameters); @@ -1042,7 +1044,8 @@ void ISimulation::loopThroughYears(uint firstYear, state[numSpace], pYearByYear, pDurationCollector, - pResultWriter); + pResultWriter, + simulationObserver_.get()); results.add(Concurrency::AddTask(*pQueueService, task)); } // End loop over years of the current set of parallel years diff --git a/src/solver/utils/include/antares/solver/utils/name_translator.h b/src/solver/utils/include/antares/solver/utils/name_translator.h index e7a54b3614..5612e30c76 100644 --- a/src/solver/utils/include/antares/solver/utils/name_translator.h +++ b/src/solver/utils/include/antares/solver/utils/name_translator.h @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #pragma once #include diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index a771c25503..fcbc5cd6b2 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -34,6 +34,7 @@ if (${RECENT_BOOST}) add_subdirectory(end-to-end) add_subdirectory(src) add_subdirectory(kirchhoff-cbuilder) + add_subdirectory(inmemory-study) else() message(STATUS "Boost >= 1.60.0 is required for end-to-end tests, found ${Boost_VERSION}. Skipping") endif() diff --git a/src/tests/end-to-end/CMakeLists.txt b/src/tests/end-to-end/CMakeLists.txt index 9830013131..6c2031e372 100644 --- a/src/tests/end-to-end/CMakeLists.txt +++ b/src/tests/end-to-end/CMakeLists.txt @@ -1,3 +1,2 @@ -add_subdirectory(utils) add_subdirectory(simple_study) add_subdirectory(binding_constraints) \ No newline at end of file diff --git a/src/tests/end-to-end/binding_constraints/CMakeLists.txt b/src/tests/end-to-end/binding_constraints/CMakeLists.txt index 1b7cf52e86..72e2e46f62 100644 --- a/src/tests/end-to-end/binding_constraints/CMakeLists.txt +++ b/src/tests/end-to-end/binding_constraints/CMakeLists.txt @@ -12,18 +12,17 @@ add_executable(tests-binding_constraints target_link_libraries(tests-binding_constraints PRIVATE - test_utils Boost::unit_test_framework model_antares antares-solver-simulation antares-solver-hydro antares-solver-ts-generator + Antares::tests::in-memory-study ) target_include_directories(tests-binding_constraints PRIVATE ${CMAKE_SOURCE_DIR}/solver - ${CMAKE_CURRENT_SOURCE_DIR}/../utils ) add_test(NAME end-to-end-binding_constraints COMMAND tests-binding_constraints) diff --git a/src/tests/end-to-end/binding_constraints/test_binding_constraints.cpp b/src/tests/end-to-end/binding_constraints/test_binding_constraints.cpp index c1b875378f..10f91f2a40 100644 --- a/src/tests/end-to-end/binding_constraints/test_binding_constraints.cpp +++ b/src/tests/end-to-end/binding_constraints/test_binding_constraints.cpp @@ -23,7 +23,7 @@ #include #include -#include "utils.h" +#include "in-memory-study.h" namespace utf = boost::unit_test; namespace tt = boost::test_tools; diff --git a/src/tests/end-to-end/simple_study/CMakeLists.txt b/src/tests/end-to-end/simple_study/CMakeLists.txt index 670cb16187..cc5c93b410 100644 --- a/src/tests/end-to-end/simple_study/CMakeLists.txt +++ b/src/tests/end-to-end/simple_study/CMakeLists.txt @@ -12,19 +12,18 @@ add_executable(tests-simple-study target_link_libraries(tests-simple-study PRIVATE - test_utils antares-solver-hydro antares-solver-variable antares-solver-simulation antares-solver-ts-generator model_antares ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} + Antares::tests::in-memory-study ) target_include_directories(tests-simple-study PRIVATE ${CMAKE_SOURCE_DIR}/solver - ${CMAKE_CURRENT_SOURCE_DIR}/../utils ) add_test(NAME end-to-end-simple-study COMMAND tests-simple-study) diff --git a/src/tests/end-to-end/simple_study/simple-study.cpp b/src/tests/end-to-end/simple_study/simple-study.cpp index bfad50326f..7992165af2 100644 --- a/src/tests/end-to-end/simple_study/simple-study.cpp +++ b/src/tests/end-to-end/simple_study/simple-study.cpp @@ -22,7 +22,7 @@ #include #include -#include "utils.h" +#include "in-memory-study.h" namespace utf = boost::unit_test; namespace tt = boost::test_tools; diff --git a/src/tests/end-to-end/utils/CMakeLists.txt b/src/tests/end-to-end/utils/CMakeLists.txt deleted file mode 100644 index 166e083bc0..0000000000 --- a/src/tests/end-to-end/utils/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -add_library(test_utils - utils.cpp - utils.h - ) - -target_link_libraries(test_utils - PUBLIC - ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} - Antares::infoCollection - antares-solver-simulation - ) - -target_include_directories(test_utils - PRIVATE - ${CMAKE_SOURCE_DIR}/solver - ) - -set_target_properties(test_utils PROPERTIES FOLDER Unit-tests/end_to_end) - -target_include_directories(test_utils PUBLIC ${Boost_INCLUDE_DIRS}) diff --git a/src/tests/inmemory-study/CMakeLists.txt b/src/tests/inmemory-study/CMakeLists.txt new file mode 100644 index 0000000000..36c757f717 --- /dev/null +++ b/src/tests/inmemory-study/CMakeLists.txt @@ -0,0 +1,25 @@ +add_library(in-memory-study) +add_library(Antares::tests::in-memory-study ALIAS in-memory-study) + +target_sources(in-memory-study + PRIVATE + in-memory-study.cpp + include/in-memory-study.h +) + +target_link_libraries(in-memory-study + PUBLIC + ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} + antares-solver-simulation + PRIVATE + Antares::infoCollection +) + +target_include_directories(in-memory-study + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/include +) + +set_target_properties(in-memory-study PROPERTIES FOLDER Unit-tests) + +target_include_directories(in-memory-study PUBLIC ${Boost_INCLUDE_DIRS}) diff --git a/src/tests/end-to-end/utils/utils.cpp b/src/tests/inmemory-study/in-memory-study.cpp similarity index 88% rename from src/tests/end-to-end/utils/utils.cpp rename to src/tests/inmemory-study/in-memory-study.cpp index a42891ea45..77b984fb45 100644 --- a/src/tests/end-to-end/utils/utils.cpp +++ b/src/tests/inmemory-study/in-memory-study.cpp @@ -1,27 +1,28 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #define WIN32_LEAN_AND_MEAN -#include "utils.h" +#include "in-memory-study.h" -void initializeStudy(Study::Ptr study) + +void initializeStudy(Study* study) { study->parameters.reset(); } @@ -187,11 +188,11 @@ void SimulationHandler::create() { study_.initializeRuntimeInfos(); addScratchpadToEachArea(study_); - simulation_ = std::make_shared>(study_, settings_, durationCollector_, - resultWriter_); + resultWriter_, + observer_); SIM_AllocationTableaux(study_); } @@ -203,10 +204,10 @@ StudyBuilder::StudyBuilder() // Make logs shrink to errors (and higher) only logs.verbosityLevel = Logs::Verbosity::Error::level; - study = std::make_shared(); + study = std::make_unique(true); simulation = std::make_shared(*study); - initializeStudy(study); + initializeStudy(study.get()); } void StudyBuilder::simulationBetweenDays(const unsigned int firstDay, const unsigned int lastDay) diff --git a/src/tests/end-to-end/utils/utils.h b/src/tests/inmemory-study/include/in-memory-study.h similarity index 86% rename from src/tests/end-to-end/utils/utils.h rename to src/tests/inmemory-study/include/in-memory-study.h index 1fa55efbff..101b6b47ab 100644 --- a/src/tests/end-to-end/utils/utils.h +++ b/src/tests/inmemory-study/include/in-memory-study.h @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #pragma once #define WIN32_LEAN_AND_MEAN #include "antares/solver/simulation/economy.h" @@ -30,7 +30,7 @@ using namespace Antares::Solver; using namespace Antares::Solver::Simulation; using namespace Antares::Data::ScenarioBuilder; -void initializeStudy(Study::Ptr study); +void initializeStudy(Study* study); void configureLinkCapacities(AreaLink* link); class TimeSeriesConfigurer @@ -207,6 +207,7 @@ class SimulationHandler Settings settings_; Study& study_; NullResultWriter resultWriter_; + NullSimulationObserver observer_; }; // ========================= @@ -224,7 +225,7 @@ struct StudyBuilder void giveWeightToYear(float weight, unsigned int year); // Data members - std::shared_ptr study; + std::unique_ptr study; std::shared_ptr simulation; }; diff --git a/src/tests/src/CMakeLists.txt b/src/tests/src/CMakeLists.txt index f6275f8386..14c9929f56 100644 --- a/src/tests/src/CMakeLists.txt +++ b/src/tests/src/CMakeLists.txt @@ -1,3 +1,5 @@ +add_subdirectory(api_internal) +add_subdirectory(api_lib) add_subdirectory(utils) add_subdirectory(libs) diff --git a/src/tests/src/api_internal/CMakeLists.txt b/src/tests/src/api_internal/CMakeLists.txt new file mode 100644 index 0000000000..2186dc6f4b --- /dev/null +++ b/src/tests/src/api_internal/CMakeLists.txt @@ -0,0 +1,27 @@ +set(EXECUTABLE_NAME test-api) +add_executable(${EXECUTABLE_NAME}) + +target_sources(${EXECUTABLE_NAME} + PRIVATE + test_api.cpp +) + +target_link_libraries(${EXECUTABLE_NAME} + PRIVATE + Boost::unit_test_framework + Antares::solver_api + Antares::tests::in-memory-study +) + +target_include_directories(${EXECUTABLE_NAME} + PRIVATE + #Allow to use the private headers + $ +) + +# Storing tests-ts-numbers under the folder Unit-tests in the IDE +set_target_properties(${EXECUTABLE_NAME} PROPERTIES FOLDER Unit-tests) + +add_test(NAME test-api COMMAND ${EXECUTABLE_NAME}) + +set_property(TEST test-api PROPERTY LABELS unit) diff --git a/src/tests/src/api_internal/test_api.cpp b/src/tests/src/api_internal/test_api.cpp new file mode 100644 index 0000000000..d60f6315d5 --- /dev/null +++ b/src/tests/src/api_internal/test_api.cpp @@ -0,0 +1,96 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#define BOOST_TEST_MODULE test_api +#define WIN32_LEAN_AND_MEAN + +#include + +#include + +#include "API.h" +#include "in-memory-study.h" + +class InMemoryStudyLoader: public Antares::IStudyLoader +{ +public: + explicit InMemoryStudyLoader(bool success = true): + success_(success){}; + + [[nodiscard]] std::unique_ptr load() const override + { + if (!success_) + { + return nullptr; + } + StudyBuilder builder; + builder.addAreaToStudy("area1"); + builder.addAreaToStudy("area2"); + builder.setNumberMCyears(1); + builder.study->initializeRuntimeInfos(); + builder.study->parameters.resultFormat = ResultFormat::inMemory; + builder.study->prepareOutput(); + return std::move(builder.study); + } + + bool success_ = true; +}; + +BOOST_AUTO_TEST_CASE(simulation_path_points_to_results) +{ + Antares::API::APIInternal api; + auto study_loader = std::make_unique(); + auto results = api.run(*study_loader.get()); + BOOST_CHECK_EQUAL(results.simulationPath, std::filesystem::path{"no_output"}); + // Testing for "no_output" is a bit weird, but it's the only way to test this without actually + // running the simulation +} + +BOOST_AUTO_TEST_CASE(api_run_contains_antares_problem) +{ + Antares::API::APIInternal api; + auto study_loader = std::make_unique(); + auto results = api.run(*study_loader.get()); + + BOOST_CHECK(!results.antares_problems.empty()); + BOOST_CHECK(!results.error); +} + +BOOST_AUTO_TEST_CASE(result_failure_when_study_is_null) +{ + Antares::API::APIInternal api; + auto study_loader = std::make_unique(false); + auto results = api.run(*study_loader.get()); + + BOOST_CHECK(results.error); +} + +// Test where data in problems are consistant with data in study +BOOST_AUTO_TEST_CASE(result_contains_problems) +{ + Antares::API::APIInternal api; + auto study_loader = std::make_unique(); + auto results = api.run(*study_loader.get()); + + BOOST_CHECK(!results.antares_problems.empty()); + BOOST_CHECK(!results.error); + BOOST_CHECK_EQUAL(results.antares_problems.weeklyProblems.size(), 52); +} diff --git a/src/tests/src/api_lib/CMakeLists.txt b/src/tests/src/api_lib/CMakeLists.txt new file mode 100644 index 0000000000..b5ce633955 --- /dev/null +++ b/src/tests/src/api_lib/CMakeLists.txt @@ -0,0 +1,20 @@ +set(EXECUTABLE_NAME test-client-api) +add_executable(${EXECUTABLE_NAME}) + +target_sources(${EXECUTABLE_NAME} + PRIVATE + test_api.cpp +) + +target_link_libraries(${EXECUTABLE_NAME} + PRIVATE + Boost::unit_test_framework + Antares::solver_api +) + +# Storing tests-ts-numbers under the folder Unit-tests in the IDE +set_target_properties(${EXECUTABLE_NAME} PROPERTIES FOLDER Unit-tests) + +add_test(NAME test-client-api COMMAND ${EXECUTABLE_NAME}) + +set_property(TEST test-client-api PROPERTY LABELS unit) diff --git a/src/tests/src/api_lib/test_api.cpp b/src/tests/src/api_lib/test_api.cpp new file mode 100644 index 0000000000..0dbddfae7f --- /dev/null +++ b/src/tests/src/api_lib/test_api.cpp @@ -0,0 +1,35 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#define BOOST_TEST_MODULE test_api +#define WIN32_LEAN_AND_MEAN + +#include + +#include "antares/api/solver.h" + +BOOST_AUTO_TEST_CASE(result_failure_when_study_path_invalid) +{ + using namespace std::string_literals; + auto results = Antares::API::PerformSimulation("dummy"s); + BOOST_CHECK(results.error); + BOOST_CHECK(!results.error->reason.empty()); +} diff --git a/src/tests/src/libs/CMakeLists.txt b/src/tests/src/libs/CMakeLists.txt index b97a7df1bc..81b6173280 100644 --- a/src/tests/src/libs/CMakeLists.txt +++ b/src/tests/src/libs/CMakeLists.txt @@ -1,2 +1,2 @@ -add_subdirectory(antares) \ No newline at end of file +add_subdirectory(antares) diff --git a/src/tests/src/solver/CMakeLists.txt b/src/tests/src/solver/CMakeLists.txt index 690cf4fa68..e301a13a77 100644 --- a/src/tests/src/solver/CMakeLists.txt +++ b/src/tests/src/solver/CMakeLists.txt @@ -3,3 +3,4 @@ add_subdirectory(simulation) add_subdirectory(optimisation) add_subdirectory(utils) add_subdirectory(infeasible-problem-analysis) +add_subdirectory(lps) diff --git a/src/tests/src/solver/infeasible-problem-analysis/test-unfeasible-problem-analyzer.cpp b/src/tests/src/solver/infeasible-problem-analysis/test-unfeasible-problem-analyzer.cpp index d333f5104c..5f10a75879 100644 --- a/src/tests/src/solver/infeasible-problem-analysis/test-unfeasible-problem-analyzer.cpp +++ b/src/tests/src/solver/infeasible-problem-analysis/test-unfeasible-problem-analyzer.cpp @@ -24,16 +24,15 @@ #include #include #include +#include #include "antares/solver/infeasible-problem-analysis/constraint-slack-analysis.h" #include "antares/solver/infeasible-problem-analysis/unfeasible-pb-analyzer.h" #include "antares/solver/infeasible-problem-analysis/variables-bounds-consistency.h" -#include "ortools/linear_solver/linear_solver.h" - namespace bdata = boost::unit_test::data; -using operations_research::MPSolver; +using namespace operations_research; using Antares::Optimization::ConstraintSlackAnalysis; using Antares::Optimization::UnfeasibilityAnalysis; diff --git a/src/tests/src/solver/lps/CMakeLists.txt b/src/tests/src/solver/lps/CMakeLists.txt new file mode 100644 index 0000000000..89121152ba --- /dev/null +++ b/src/tests/src/solver/lps/CMakeLists.txt @@ -0,0 +1,20 @@ +set(EXECUTABLE_NAME test-lps) +add_executable(${EXECUTABLE_NAME}) + +target_sources(${EXECUTABLE_NAME} + PRIVATE + test_lps.cpp +) + +target_link_libraries(${EXECUTABLE_NAME} + PRIVATE + Boost::unit_test_framework + antares-solver-simulation +) + +# Storing tests-ts-numbers under the folder Unit-tests in the IDE +set_target_properties(${EXECUTABLE_NAME} PROPERTIES FOLDER Unit-tests) + +add_test(NAME test-lps COMMAND ${EXECUTABLE_NAME}) + +set_property(TEST test-lps PROPERTY LABELS unit) diff --git a/src/tests/src/solver/lps/test_lps.cpp b/src/tests/src/solver/lps/test_lps.cpp new file mode 100644 index 0000000000..3a0bf4aa34 --- /dev/null +++ b/src/tests/src/solver/lps/test_lps.cpp @@ -0,0 +1,77 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#define BOOST_TEST_MODULE test_translator +#define WIN32_LEAN_AND_MEAN + +#include + +#include + +using namespace Antares::Solver; + +BOOST_AUTO_TEST_CASE(new_lps_is_empty) +{ + LpsFromAntares lps; + BOOST_CHECK(lps.empty()); +} + +BOOST_AUTO_TEST_CASE(lps_with_only_constant_data_is_empty) +{ + LpsFromAntares lps; + lps.setConstantData(ConstantDataFromAntares()); + BOOST_CHECK(lps.empty()); +} + +BOOST_AUTO_TEST_CASE(lps_with_no_variabled_is_empty) +{ + LpsFromAntares lps; + lps.constantProblemData.VariablesCount = 0; + lps.addWeeklyData({0, 0}, WeeklyDataFromAntares()); + BOOST_CHECK(lps.empty()); +} + +BOOST_AUTO_TEST_CASE(lps_with_both_constant_and_weekly_data_is_not_empty) +{ + LpsFromAntares lps; + lps.constantProblemData.VariablesCount = 65; + lps.addWeeklyData({0, 0}, WeeklyDataFromAntares()); + BOOST_CHECK(!lps.empty()); +} + +BOOST_AUTO_TEST_CASE(replace_const_data) +{ + LpsFromAntares lps; + ConstantDataFromAntares var; + var.VariablesCount = 42; + lps.setConstantData(var); + BOOST_CHECK(lps.constantProblemData.VariablesCount == 42); +} + +// Add weekly data for week 1 year 1 +BOOST_AUTO_TEST_CASE(add_weekly_data_for_week_1_year_1) +{ + LpsFromAntares lps; + WeeklyDataFromAntares w; + w.RHS.push_back(43); + lps.addWeeklyData({1, 1}, w); + BOOST_CHECK(lps.weeklyData({1, 1}).RHS.size() != WeeklyDataFromAntares().RHS.size()); +} diff --git a/src/tests/src/solver/optimisation/CMakeLists.txt b/src/tests/src/solver/optimisation/CMakeLists.txt index 72d92448a3..d3b9a9a82e 100644 --- a/src/tests/src/solver/optimisation/CMakeLists.txt +++ b/src/tests/src/solver/optimisation/CMakeLists.txt @@ -1,24 +1,2 @@ -# Useful variables definitions -set(src_solver_optimisation "${CMAKE_SOURCE_DIR}/solver/optimisation") - -set(EXECUTABLE_NAME tests-adq-patch) -add_executable(${EXECUTABLE_NAME} adequacy_patch.cpp) - -target_include_directories(${EXECUTABLE_NAME} - PRIVATE - "${src_solver_optimisation}" -) - -target_link_libraries(${EXECUTABLE_NAME} - PRIVATE - Boost::unit_test_framework - model_antares - array -) - -# Storing tests-ts-numbers under the folder Unit-tests in the IDE -set_target_properties(${EXECUTABLE_NAME} PROPERTIES FOLDER Unit-tests) - -add_test(NAME test-adq-patch COMMAND ${EXECUTABLE_NAME}) - -set_property(TEST test-adq-patch PROPERTY LABELS unit) +add_subdirectory(adequacy_patch) +add_subdirectory(translator) \ No newline at end of file diff --git a/src/tests/src/solver/optimisation/adequacy_patch/CMakeLists.txt b/src/tests/src/solver/optimisation/adequacy_patch/CMakeLists.txt new file mode 100644 index 0000000000..bb28a99864 --- /dev/null +++ b/src/tests/src/solver/optimisation/adequacy_patch/CMakeLists.txt @@ -0,0 +1,24 @@ +# Useful variables definitions +set(src_solver_optimisation "${CMAKE_SOURCE_DIR}/solver/optimisation") + +set(EXECUTABLE_NAME tests-adq-patch) +add_executable(${EXECUTABLE_NAME} adequacy_patch.cpp) + +target_include_directories(${EXECUTABLE_NAME} + PRIVATE + "${src_solver_optimisation}" +) + +target_link_libraries(${EXECUTABLE_NAME} + PRIVATE + Boost::unit_test_framework + model_antares + array +) + +# Storing tests-ts-numbers under the folder Unit-tests in the IDE +set_target_properties(${EXECUTABLE_NAME} PROPERTIES FOLDER Unit-tests) + +add_test(NAME test-adq-patch COMMAND ${EXECUTABLE_NAME}) + +set_property(TEST test-adq-patch PROPERTY LABELS unit) diff --git a/src/tests/src/solver/optimisation/adequacy_patch.cpp b/src/tests/src/solver/optimisation/adequacy_patch/adequacy_patch.cpp similarity index 100% rename from src/tests/src/solver/optimisation/adequacy_patch.cpp rename to src/tests/src/solver/optimisation/adequacy_patch/adequacy_patch.cpp diff --git a/src/tests/src/solver/optimisation/translator/CMakeLists.txt b/src/tests/src/solver/optimisation/translator/CMakeLists.txt new file mode 100644 index 0000000000..87f3d5a0cb --- /dev/null +++ b/src/tests/src/solver/optimisation/translator/CMakeLists.txt @@ -0,0 +1,20 @@ +set(EXECUTABLE_NAME test-translator) +add_executable(${EXECUTABLE_NAME}) + +target_sources(${EXECUTABLE_NAME} + PRIVATE + test_translator.cpp +) + +target_link_libraries(${EXECUTABLE_NAME} + PRIVATE + Boost::unit_test_framework + model_antares +) + +# Storing tests-ts-numbers under the folder Unit-tests in the IDE +set_target_properties(${EXECUTABLE_NAME} PROPERTIES FOLDER Unit-tests) + +add_test(NAME test-translator COMMAND ${EXECUTABLE_NAME}) + +set_property(TEST test-translator PROPERTY LABELS unit) diff --git a/src/tests/src/solver/optimisation/translator/test_translator.cpp b/src/tests/src/solver/optimisation/translator/test_translator.cpp new file mode 100644 index 0000000000..6fdc32c8fd --- /dev/null +++ b/src/tests/src/solver/optimisation/translator/test_translator.cpp @@ -0,0 +1,189 @@ +/* + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ + +#define BOOST_TEST_MODULE test_translator +#define WIN32_LEAN_AND_MEAN + +#include + +#include + +#include +#include "antares/solver/utils/opt_period_string_generator.h" + +using namespace Antares::Solver; + +class StubOptPeriodStringGenerator: public OptPeriodStringGenerator +{ +public: + std::string to_string() const override + { + return "Plop"; + } +}; + +BOOST_AUTO_TEST_CASE(null_hebdo_is_empty_lps) +{ + HebdoProblemToLpsTranslator translator; + auto ret = translator.translate(nullptr, std::string()); + BOOST_CHECK(ret == WeeklyDataFromAntares()); +} + +BOOST_AUTO_TEST_CASE(non_null_hebdo_returns_non_empty_lps) +{ + HebdoProblemToLpsTranslator translator; + PROBLEME_ANTARES_A_RESOUDRE problemHebdo; + problemHebdo.CoutLineaire.push_back(45); + auto ret = translator.translate(&problemHebdo, std::string()); + BOOST_CHECK(ret != WeeklyDataFromAntares()); +} + +BOOST_AUTO_TEST_CASE(Data_properly_copied) +{ + HebdoProblemToLpsTranslator translator; + PROBLEME_ANTARES_A_RESOUDRE problemHebdo; + problemHebdo.CoutLineaire = {0, 1, 2}; + problemHebdo.Xmax = {10, 11, 12}; + problemHebdo.Xmin = {20, 21, 22}; + problemHebdo.NomDesVariables = {"a", "b", "c"}; + problemHebdo.NomDesContraintes = {"d", "e", "f"}; + problemHebdo.SecondMembre = {30, 31, 32}; + + auto ret = translator.translate(&problemHebdo, std::string()); + BOOST_CHECK(ret.LinearCost == problemHebdo.CoutLineaire); + BOOST_CHECK(ret.Xmax == problemHebdo.Xmax); + BOOST_CHECK(ret.Xmin == problemHebdo.Xmin); + BOOST_CHECK(ret.RHS == problemHebdo.SecondMembre); + + BOOST_CHECK(ret.variables == problemHebdo.NomDesVariables); + BOOST_CHECK(ret.constraints == problemHebdo.NomDesContraintes); +} + +BOOST_AUTO_TEST_CASE(translate_sens) +{ + HebdoProblemToLpsTranslator translator; + PROBLEME_ANTARES_A_RESOUDRE problemHebdo; + problemHebdo.Sens = "<=>"; + + auto ret = translator.translate(&problemHebdo, std::string()); + BOOST_CHECK(ret.Direction == std::vector({'<', '=', '>'})); +} + +BOOST_AUTO_TEST_CASE(translate_name_is_filled) +{ + HebdoProblemToLpsTranslator translator; + PROBLEME_ANTARES_A_RESOUDRE problemHebdo; + + auto ret = translator.translate(&problemHebdo, "dummy"); + BOOST_CHECK(!ret.name.empty()); +} + +BOOST_AUTO_TEST_CASE(translate_name_is_properly_filled) +{ + HebdoProblemToLpsTranslator translator; + PROBLEME_ANTARES_A_RESOUDRE problemHebdo; + + auto ret = translator.translate(&problemHebdo, "problem-Plop--optim-nb-1.mps"); + BOOST_CHECK_EQUAL(ret.name, "problem-Plop--optim-nb-1.mps"); +} + +BOOST_AUTO_TEST_CASE(empty_problem_empty_const_data) +{ + HebdoProblemToLpsTranslator translator; + auto ret = translator.commonProblemData(nullptr); + BOOST_CHECK(ret == ConstantDataFromAntares()); +} + +BOOST_AUTO_TEST_CASE(common_data_properly_copied) +{ + HebdoProblemToLpsTranslator translator; + PROBLEME_ANTARES_A_RESOUDRE problemHebdo; + problemHebdo.NombreDeVariables = 1; + problemHebdo.NombreDeContraintes = 2; + problemHebdo.TypeDeVariable = {0, 1, 2}; + problemHebdo.IndicesDebutDeLigne = {0, 3}; + problemHebdo.NombreDeTermesDesLignes = {3, 3}; + problemHebdo.CoefficientsDeLaMatriceDesContraintes = {0, 1, 2, 3, 4, 5}; + problemHebdo.IndicesColonnes = {0, 1, 2, 3, 4, 5}; + + auto ret = translator.commonProblemData(&problemHebdo); + BOOST_CHECK_EQUAL(ret.VariablesCount, problemHebdo.NombreDeVariables); + BOOST_CHECK_EQUAL(ret.ConstraintesCount, problemHebdo.NombreDeContraintes); + BOOST_CHECK(std::ranges::equal(ret.VariablesType, problemHebdo.TypeDeVariable)); + BOOST_CHECK(ret.ConstraintsMatrixCoeff == problemHebdo.CoefficientsDeLaMatriceDesContraintes); + BOOST_CHECK(std::ranges::equal(ret.ColumnIndexes, problemHebdo.IndicesColonnes)); + BOOST_CHECK(std::ranges::equal(ret.Mdeb, problemHebdo.IndicesDebutDeLigne)); +} + +// throw exception if NombreDeVariables is 0 +BOOST_AUTO_TEST_CASE(throw_exception_if_NombreDeVariables_is_0) +{ + HebdoProblemToLpsTranslator translator; + PROBLEME_ANTARES_A_RESOUDRE problemHebdo; + problemHebdo.NombreDeVariables = 0; + BOOST_CHECK_THROW(translator.commonProblemData(&problemHebdo), std::runtime_error); +} + +BOOST_AUTO_TEST_CASE(throw_exception_if_NombreDeContraintes_is_0) +{ + HebdoProblemToLpsTranslator translator; + PROBLEME_ANTARES_A_RESOUDRE problemHebdo; + problemHebdo.NombreDeContraintes = 0; + BOOST_CHECK_THROW(translator.commonProblemData(&problemHebdo), std::runtime_error); +} + +BOOST_AUTO_TEST_CASE(throw_exception_if_IndicesDebutDeLigne_out_of_bound) +{ + HebdoProblemToLpsTranslator translator; + PROBLEME_ANTARES_A_RESOUDRE problemHebdo; + problemHebdo.NombreDeVariables = 1; + problemHebdo.NombreDeContraintes = 3; + problemHebdo.IndicesDebutDeLigne = {0, 3}; + problemHebdo.NombreDeTermesDesLignes = {0, 3, 6, 7, 8}; + BOOST_CHECK_THROW(translator.commonProblemData(&problemHebdo), std::runtime_error); +} + +BOOST_AUTO_TEST_CASE(throw_exception_if_NombreDeTermesDesLignes_out_of_bound) +{ + HebdoProblemToLpsTranslator translator; + PROBLEME_ANTARES_A_RESOUDRE problemHebdo; + problemHebdo.NombreDeVariables = 1; + problemHebdo.NombreDeContraintes = 3; + problemHebdo.NombreDeTermesDesLignes = {0, 3}; + problemHebdo.IndicesDebutDeLigne = {0, 3, 6, 7, 8}; + BOOST_CHECK_THROW(translator.commonProblemData(&problemHebdo), std::runtime_error); +} + +// NombreDeCoefficients +BOOST_AUTO_TEST_CASE(NombreDeCoefficients_is_properly_computed) +{ + HebdoProblemToLpsTranslator translator; + PROBLEME_ANTARES_A_RESOUDRE problemHebdo; + problemHebdo.NombreDeVariables = 1; + problemHebdo.NombreDeContraintes = 3; + problemHebdo.IndicesDebutDeLigne = {0, 3, 6}; + problemHebdo.NombreDeTermesDesLignes = {3, 3, 3}; + problemHebdo.CoefficientsDeLaMatriceDesContraintes = {0, 1, 2, 3, 4, 5, 6, 7, 8}; + problemHebdo.IndicesColonnes = {0, 1, 2, 3, 4, 5, 6, 7, 8}; + + auto ret = translator.commonProblemData(&problemHebdo); + BOOST_CHECK_EQUAL(ret.CoeffCount, 9); +} diff --git a/src/ui/simulator/toolbox/components/datagrid/renderer/scenario-builder-ntc-renderer.h b/src/ui/simulator/toolbox/components/datagrid/renderer/scenario-builder-ntc-renderer.h index 9bb7cdfd1f..edc1c49580 100644 --- a/src/ui/simulator/toolbox/components/datagrid/renderer/scenario-builder-ntc-renderer.h +++ b/src/ui/simulator/toolbox/components/datagrid/renderer/scenario-builder-ntc-renderer.h @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #pragma once #include "scenario-builder-renderer-base.h" diff --git a/src/vcpkg.json b/src/vcpkg.json index fdd3963fd5..08acfb9e79 100644 --- a/src/vcpkg.json +++ b/src/vcpkg.json @@ -24,7 +24,7 @@ "version>=": "1.81.0" }, { - "name": "boost-algorithm", + "name": "boost-core", "version>=": "1.81.0" }, { From b2cede8835a9a0261579cab30868fa9b6ed3a687 Mon Sep 17 00:00:00 2001 From: payetvin <113102157+payetvin@users.noreply.github.com> Date: Thu, 27 Jun 2024 16:48:42 +0200 Subject: [PATCH 2/4] add develop to sonar branch (#2206) --- .github/workflows/sonarcloud.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index cfe5324d3c..e16bb1180e 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -1,6 +1,8 @@ name: SonarCloud on: + branches: + - develop pull_request: jobs: From 52c7bd6303062d263001e5a3ddea7eb8cbeb5bdd Mon Sep 17 00:00:00 2001 From: payetvin <113102157+payetvin@users.noreply.github.com> Date: Thu, 27 Jun 2024 16:58:04 +0200 Subject: [PATCH 3/4] Fix/sonar (#2207) --- .github/workflows/sonarcloud.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index e16bb1180e..e72287894d 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -1,6 +1,7 @@ name: SonarCloud on: + push: branches: - develop pull_request: From 5a592b4fd748b1ace805c1f061bf7ee1df7aef69 Mon Sep 17 00:00:00 2001 From: payetvin <113102157+payetvin@users.noreply.github.com> Date: Fri, 28 Jun 2024 09:05:04 +0200 Subject: [PATCH 4/4] Improve error message for ts-generation arg parsing (#2205) --- src/libs/antares/utils/utils.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libs/antares/utils/utils.cpp b/src/libs/antares/utils/utils.cpp index 047881b0e6..5f5efc9e9e 100644 --- a/src/libs/antares/utils/utils.cpp +++ b/src/libs/antares/utils/utils.cpp @@ -116,6 +116,8 @@ std::vector> splitStringIntoPairs(const std: else { logs.warning() << "Error while parsing: " << token; + logs.warning() << "Correct format is: \"object1" << delimiter2 << "object2" + << delimiter1 << "object3" << delimiter2 << "object4\""; } }