Skip to content

Commit

Permalink
Return the Result from Uno::solve()
Browse files Browse the repository at this point in the history
  • Loading branch information
cvanaret committed Nov 25, 2024
1 parent 9db19bd commit 732487e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
8 changes: 7 additions & 1 deletion bindings/AMPL/uno_ampl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 8 additions & 7 deletions uno/Uno.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions uno/Uno.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 732487e

Please sign in to comment.