From 0e53e3ebec67f0046eb8f93041fa195f7eebabaf Mon Sep 17 00:00:00 2001 From: Charlie Vanaret Date: Wed, 20 Nov 2024 12:41:53 +0100 Subject: [PATCH] Have Uno::solve() return an optional Result --- bindings/AMPL/uno_ampl.cpp | 5 ++++- uno/Uno.cpp | 4 +++- uno/Uno.hpp | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/bindings/AMPL/uno_ampl.cpp b/bindings/AMPL/uno_ampl.cpp index 1b054961..36a41930 100644 --- a/bindings/AMPL/uno_ampl.cpp +++ b/bindings/AMPL/uno_ampl.cpp @@ -51,7 +51,10 @@ namespace uno { Uno uno = Uno(*globalization_mechanism, options); // solve the instance - uno.solve(*model, initial_iterate, options); + auto optional_result = uno.solve(*model, initial_iterate, options); + if (optional_result.has_value()) { + const auto result = *optional_result; + } // std::cout << "memory_allocation_amount = " << memory_allocation_amount << '\n'; } catch (std::exception& exception) { diff --git a/uno/Uno.cpp b/uno/Uno.cpp index c0419b07..e0787140 100644 --- a/uno/Uno.cpp +++ b/uno/Uno.cpp @@ -30,7 +30,7 @@ namespace uno { Level Logger::level = INFO; - void Uno::solve(const Model& model, Iterate& current_iterate, const Options& options) { + std::optional Uno::solve(const Model& model, Iterate& current_iterate, const Options& options) { Timer timer{}; Statistics statistics = Uno::create_statistics(model, options); WarmstartInformation warmstart_information{}; @@ -71,9 +71,11 @@ namespace uno { Uno::postprocess_iterate(model, current_iterate, current_iterate.status); Result result = this->create_result(model, current_iterate, major_iterations, timer); this->print_optimization_summary(result); + return result; } catch (const std::exception& e) { DISCRETE << "An error occurred at the initial iterate: " << e.what() << '\n'; + return std::nullopt; } } diff --git a/uno/Uno.hpp b/uno/Uno.hpp index e9a10369..445b7a06 100644 --- a/uno/Uno.hpp +++ b/uno/Uno.hpp @@ -4,6 +4,7 @@ #ifndef UNO_H #define UNO_H +#include #include "optimization/Result.hpp" #include "optimization/TerminationStatus.hpp" @@ -19,7 +20,7 @@ namespace uno { public: Uno(GlobalizationMechanism& globalization_mechanism, const Options& options); - void solve(const Model& model, Iterate& initial_iterate, const Options& options); + std::optional solve(const Model& model, Iterate& initial_iterate, const Options& options); static std::string current_version(); static void print_available_strategies();