Skip to content

Commit

Permalink
AMPL: write the solution with correct dual signs (signs must be flipp…
Browse files Browse the repository at this point in the history
…ed if objective is maximized)
  • Loading branch information
cvanaret committed Oct 26, 2024
1 parent 1336619 commit 117641e
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 2 deletions.
19 changes: 17 additions & 2 deletions bindings/AMPL/AMPLModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "tools/Infinity.hpp"
#include "options/Options.hpp"
#include "symbolic/Concatenation.hpp"
#include "symbolic/ScalarMultiple.hpp"
#include "symbolic/UnaryNegation.hpp"
#include "Uno.hpp"

Expand Down Expand Up @@ -354,7 +355,11 @@ namespace uno {
std::copy(this->asl->i.pi0_, this->asl->i.pi0_ + this->number_constraints, multipliers.begin());
}

void AMPLModel::postprocess_solution(Iterate& iterate, TerminationStatus termination_status) const {
void AMPLModel::postprocess_solution(Iterate& /*iterate*/, TerminationStatus /*termination_status*/) const {
// do nothing
}

void AMPLModel::terminate(Iterate& iterate, TerminationStatus termination_status) const {
if (this->write_solution_to_file) {
// write the primal-dual solution and status into a *.sol file
this->asl->p.solve_code_ = 400; // limit
Expand All @@ -373,11 +378,21 @@ namespace uno {
else if (termination_status == TerminationStatus::INFEASIBLE_SMALL_STEP) {
this->asl->p.solve_code_ = 500;
}

/*
SufDesc* tmp_lb = suf_get_ASL(this->asl, "uno_lower_bound_duals", ASL_Sufkind_var);
SufDesc* tmp_ub = suf_get_ASL(this->asl, "uno_upper_bound_duals", ASL_Sufkind_var);
suf_rput_ASL(this->asl, "uno_lower_bound_duals", ASL_Sufkind_var, iterate.multipliers.lower_bounds.data());
suf_rput_ASL(this->asl, "uno_upper_bound_duals", ASL_Sufkind_var, iterate.multipliers.upper_bounds.data());
*/

Option_Info option_info{};
option_info.wantsol = 9; // write the solution without printing the message to stdout
std::string message = "Uno ";
message.append(Uno::current_version()).append(": ").append(status_to_message(termination_status));
write_sol_ASL(this->asl, message.data(), iterate.primals.data(), iterate.multipliers.constraints.data(), &option_info);
// flip the signs of the multipliers if we maximize
this->multipliers_with_flipped_sign = this->objective_sign * iterate.multipliers.constraints;
write_sol_ASL(this->asl, message.data(), iterate.primals.data(), this->multipliers_with_flipped_sign.data(), &option_info);
}
}

Expand Down
1 change: 1 addition & 0 deletions bindings/AMPL/AMPLModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ namespace uno {
void initial_primal_point(Vector<double>& x) const override;
void initial_dual_point(Vector<double>& multipliers) const override;
void postprocess_solution(Iterate& iterate, TerminationStatus termination_status) const override;
void terminate(Iterate& iterate, TerminationStatus termination_status) const override;

[[nodiscard]] size_t number_objective_gradient_nonzeros() const override;
[[nodiscard]] size_t number_jacobian_nonzeros() const override;
Expand Down
1 change: 1 addition & 0 deletions bindings/AMPL/uno_ampl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace uno {
// solve the instance
Result result = uno.solve(*model, initial_iterate, options);
uno.print_optimization_summary(result);
model->terminate(result.solution, result.solution.status);
// std::cout << "memory_allocation_amount = " << memory_allocation_amount << '\n';
}
catch (std::exception& exception) {
Expand Down
3 changes: 3 additions & 0 deletions uno/model/BoundRelaxedModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ namespace uno {
void postprocess_solution(Iterate& iterate, TerminationStatus termination_status) const override {
this->model->postprocess_solution(iterate, termination_status);
}
void terminate(Iterate& iterate, TerminationStatus termination_status) const override {
this->model->terminate(iterate, termination_status);
}

[[nodiscard]] size_t number_objective_gradient_nonzeros() const override { return this->model->number_objective_gradient_nonzeros(); }
[[nodiscard]] size_t number_jacobian_nonzeros() const override { return this->model->number_jacobian_nonzeros(); }
Expand Down
3 changes: 3 additions & 0 deletions uno/model/HomogeneousEqualityConstrainedModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ namespace uno {
void initial_primal_point(Vector<double>& x) const override;
void initial_dual_point(Vector<double>& multipliers) const override { this->model->initial_dual_point(multipliers); }
void postprocess_solution(Iterate& iterate, TerminationStatus termination_status) const override;
void terminate(Iterate& iterate, TerminationStatus termination_status) const override {
this->model->terminate(iterate, termination_status);
}

[[nodiscard]] size_t number_objective_gradient_nonzeros() const override { return this->model->number_objective_gradient_nonzeros(); }
[[nodiscard]] size_t number_jacobian_nonzeros() const override { return this->model->number_jacobian_nonzeros() + this->slacks.size(); }
Expand Down
1 change: 1 addition & 0 deletions uno/model/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace uno {
virtual void initial_primal_point(Vector<double>& x) const = 0;
virtual void initial_dual_point(Vector<double>& multipliers) const = 0;
virtual void postprocess_solution(Iterate& iterate, TerminationStatus termination_status) const = 0;
virtual void terminate(Iterate& iterate, TerminationStatus termination_status) const = 0;

[[nodiscard]] virtual size_t number_objective_gradient_nonzeros() const = 0;
[[nodiscard]] virtual size_t number_jacobian_nonzeros() const = 0;
Expand Down
3 changes: 3 additions & 0 deletions uno/model/ScaledModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ namespace uno {
void initial_primal_point(Vector<double>& x) const override { this->model->initial_primal_point(x); }
void initial_dual_point(Vector<double>& multipliers) const override { this->model->initial_dual_point(multipliers); }
void postprocess_solution(Iterate& iterate, TerminationStatus termination_status) const override;
void terminate(Iterate& iterate, TerminationStatus termination_status) const override {
this->model->terminate(iterate, termination_status);
}

[[nodiscard]] size_t number_objective_gradient_nonzeros() const override { return this->model->number_objective_gradient_nonzeros(); }
[[nodiscard]] size_t number_jacobian_nonzeros() const override { return this->model->number_jacobian_nonzeros(); }
Expand Down

0 comments on commit 117641e

Please sign in to comment.