Skip to content

Commit

Permalink
AMPLModel: write the solution in a *.sol file (this can be disabled w…
Browse files Browse the repository at this point in the history
…ith the option AMPL_write_solution_to_file)
  • Loading branch information
cvanaret committed Oct 17, 2024
1 parent 123c422 commit fb12109
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
30 changes: 26 additions & 4 deletions bindings/AMPL/AMPLModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#include "linear_algebra/RectangularMatrix.hpp"
#include "linear_algebra/SymmetricMatrix.hpp"
#include "optimization/EvaluationErrors.hpp"
#include "optimization/Iterate.hpp"
#include "tools/Logger.hpp"
#include "tools/Infinity.hpp"
#include "tools/Options.hpp"
#include "symbolic/Concatenation.hpp"

namespace uno {
Expand All @@ -34,12 +36,13 @@ namespace uno {
}

// generate the ASL object and call the private constructor
AMPLModel::AMPLModel(const std::string& file_name) : AMPLModel(file_name, generate_asl(file_name)) {
AMPLModel::AMPLModel(const std::string& file_name, const Options& options) : AMPLModel(file_name, generate_asl(file_name), options) {
}

AMPLModel::AMPLModel(const std::string& file_name, ASL* asl) :
AMPLModel::AMPLModel(const std::string& file_name, ASL* asl, const Options& options) :
Model(file_name, static_cast<size_t>(asl->i.n_var_), static_cast<size_t>(asl->i.n_con_), (asl->i.objtype_[0] == 1) ? -1. : 1.),
asl(asl),
write_solution_to_file(options.get_bool("AMPL_write_solution_to_file")),
// allocate vectors
asl_gradient(this->number_variables),
variable_lower_bounds(this->number_variables),
Expand Down Expand Up @@ -350,8 +353,27 @@ 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 {
// do nothing
void AMPLModel::postprocess_solution(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
if (termination_status == TerminationStatus::FEASIBLE_KKT_POINT) {
this->asl->p.solve_code_ = 0;
}
if (termination_status == TerminationStatus::FEASIBLE_SMALL_STEP) {
this->asl->p.solve_code_ = 100;
}
else if (termination_status == TerminationStatus::INFEASIBLE_STATIONARY_POINT) {
this->asl->p.solve_code_ = 200;
}
else if (termination_status == TerminationStatus::UNBOUNDED) {
this->asl->p.solve_code_ = 300;
}
else if (termination_status == TerminationStatus::INFEASIBLE_SMALL_STEP) {
this->asl->p.solve_code_ = 500;
}
write_sol_ASL(this->asl, "", iterate.primals.data(), iterate.multipliers.constraints.data(), nullptr);
}
}

void AMPLModel::generate_constraints() {
Expand Down
8 changes: 6 additions & 2 deletions bindings/AMPL/AMPLModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ extern "C" {
}

namespace uno {
// forward reference
class Options;

/*! \class AMPLModel
* \brief AMPL model
*
* Description of an AMPL model
*/
class AMPLModel: public Model {
public:
explicit AMPLModel(const std::string& file_name);
AMPLModel(const std::string& file_name, const Options& options);
~AMPLModel() override;

[[nodiscard]] double evaluate_objective(const Vector<double>& x) const override;
Expand Down Expand Up @@ -61,10 +64,11 @@ namespace uno {

private:
// private constructor to pass the dimensions to the Model base constructor
AMPLModel(const std::string& file_name, ASL* asl);
AMPLModel(const std::string& file_name, ASL* asl, const Options& options);

// mutable: can be modified by const methods (internal state not seen by user)
mutable ASL* asl; /*!< Instance of the AMPL Solver Library class */
const bool write_solution_to_file;
mutable std::vector<double> asl_gradient{};
mutable std::vector<double> asl_hessian{};
size_t number_asl_hessian_nonzeros{0}; /*!< Number of nonzero elements in the Hessian */
Expand Down
2 changes: 1 addition & 1 deletion bindings/AMPL/uno_ampl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace uno {
void run_uno_ampl(const std::string& model_name, const Options& options) {
try {
// AMPL model
std::unique_ptr<Model> ampl_model = std::make_unique<AMPLModel>(model_name);
std::unique_ptr<Model> ampl_model = std::make_unique<AMPLModel>(model_name, options);

// initialize initial primal and dual points
Iterate initial_iterate(ampl_model->number_variables, ampl_model->number_constraints);
Expand Down
3 changes: 3 additions & 0 deletions uno.options
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,6 @@ least_square_multiplier_max_norm 1e3
##### BQPD options #####
BQPD_print_subproblem no
BQPD_kmax 500

##### AMPL options #####
AMPL_write_solution_to_file yes

0 comments on commit fb12109

Please sign in to comment.