From 732487e475ac6687749692c01d7de8316232ffea Mon Sep 17 00:00:00 2001 From: Charlie Vanaret Date: Mon, 25 Nov 2024 14:26:09 +0100 Subject: [PATCH] Return the Result from Uno::solve() --- bindings/AMPL/uno_ampl.cpp | 8 +++++++- uno/Uno.cpp | 15 ++++++++------- uno/Uno.hpp | 4 ++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/bindings/AMPL/uno_ampl.cpp b/bindings/AMPL/uno_ampl.cpp index 368d0305..12409c77 100644 --- a/bindings/AMPL/uno_ampl.cpp +++ b/bindings/AMPL/uno_ampl.cpp @@ -55,7 +55,13 @@ namespace uno { AMPLUserCallbacks user_callbacks{}; // solve the instance - uno.solve(*model, initial_iterate, options, user_callbacks); + Result result = uno.solve(*model, initial_iterate, options, user_callbacks); + if (result.optimization_status == OptimizationStatus::SUCCESS) { + // check result.solution.status + } + else { + // ... + } // std::cout << "memory_allocation_amount = " << memory_allocation_amount << '\n'; } catch (std::exception& exception) { diff --git a/uno/Uno.cpp b/uno/Uno.cpp index 54bf011d..a660fba7 100644 --- a/uno/Uno.cpp +++ b/uno/Uno.cpp @@ -33,27 +33,27 @@ namespace uno { Level Logger::level = INFO; // solve without user callbacks - void Uno::solve(const Model& model, Iterate& current_iterate, const Options& options) { + Result Uno::solve(const Model& model, Iterate& current_iterate, const Options& options) { // pass user callbacks that do nothing NoUserCallbacks user_callbacks{}; - this->solve(model, current_iterate, options, user_callbacks); + return this->solve(model, current_iterate, options, user_callbacks); } // solve with user callbacks - void Uno::solve(const Model& model, Iterate& current_iterate, const Options& options, UserCallbacks& user_callbacks) { + Result Uno::solve(const Model& model, Iterate& current_iterate, const Options& options, UserCallbacks& user_callbacks) { Timer timer{}; Statistics statistics = Uno::create_statistics(model, options); WarmstartInformation warmstart_information{}; warmstart_information.whole_problem_changed(); - OptimizationStatus optimization_status = OptimizationStatus::SUCCESS; + size_t major_iterations = 0; + OptimizationStatus optimization_status = OptimizationStatus::SUCCESS; try { // use the initial primal-dual point to initialize the strategies and generate the initial iterate this->initialize(statistics, current_iterate, options); // allocate the trial iterate once and for all here Iterate trial_iterate(current_iterate); - size_t major_iterations = 0; try { bool termination = false; // check for termination @@ -84,13 +84,14 @@ namespace uno { if (Logger::level == INFO) statistics.print_footer(); Uno::postprocess_iterate(model, current_iterate, current_iterate.status); - Result result = this->create_result(model, optimization_status, current_iterate, major_iterations, timer); - this->print_optimization_summary(result); } catch (const std::exception& e) { DISCRETE << "An error occurred at the initial iterate: " << e.what() << '\n'; optimization_status = OptimizationStatus::EVALUATION_ERROR; } + Result result = this->create_result(model, optimization_status, current_iterate, major_iterations, timer); + this->print_optimization_summary(result); + return result; } void Uno::initialize(Statistics& statistics, Iterate& current_iterate, const Options& options) { diff --git a/uno/Uno.hpp b/uno/Uno.hpp index c61a9fc5..3e6f3b7d 100644 --- a/uno/Uno.hpp +++ b/uno/Uno.hpp @@ -21,8 +21,8 @@ namespace uno { Uno(GlobalizationMechanism& globalization_mechanism, const Options& options); // solve with or without user callbacks - void solve(const Model& model, Iterate& initial_iterate, const Options& options); - void solve(const Model& model, Iterate& initial_iterate, const Options& options, UserCallbacks& user_callbacks); + Result solve(const Model& model, Iterate& initial_iterate, const Options& options); + Result solve(const Model& model, Iterate& initial_iterate, const Options& options, UserCallbacks& user_callbacks); static std::string current_version(); static void print_available_strategies();