Skip to content

Commit

Permalink
Have Uno::solve() return an optional Result
Browse files Browse the repository at this point in the history
  • Loading branch information
cvanaret committed Nov 20, 2024
1 parent 461693c commit 0e53e3e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
5 changes: 4 additions & 1 deletion bindings/AMPL/uno_ampl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion uno/Uno.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace uno {

Level Logger::level = INFO;

void Uno::solve(const Model& model, Iterate& current_iterate, const Options& options) {
std::optional<Result> Uno::solve(const Model& model, Iterate& current_iterate, const Options& options) {
Timer timer{};
Statistics statistics = Uno::create_statistics(model, options);
WarmstartInformation warmstart_information{};
Expand Down Expand Up @@ -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;
}
}

Expand Down
3 changes: 2 additions & 1 deletion uno/Uno.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#ifndef UNO_H
#define UNO_H

#include <optional>
#include "optimization/Result.hpp"
#include "optimization/TerminationStatus.hpp"

Expand All @@ -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<Result> solve(const Model& model, Iterate& initial_iterate, const Options& options);

static std::string current_version();
static void print_available_strategies();
Expand Down

0 comments on commit 0e53e3e

Please sign in to comment.