Skip to content

Commit

Permalink
Graciously fail when the AMPL model contains integer variables
Browse files Browse the repository at this point in the history
  • Loading branch information
cvanaret committed Oct 17, 2024
1 parent 73fa292 commit c0b4ad9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
8 changes: 4 additions & 4 deletions bindings/AMPL/AMPLModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project directory for details.

#include <cassert>
#include <stdexcept>
#include "AMPLModel.hpp"
#include "linear_algebra/RectangularMatrix.hpp"
#include "linear_algebra/SymmetricMatrix.hpp"
Expand All @@ -19,8 +20,8 @@ namespace uno {

int n_discrete = asl->i.nbv_ + asl->i.niv_ + asl->i.nlvbi_ + asl->i.nlvci_ + asl->i.nlvoi_;
if (0 < n_discrete) {
WARNING << "Ignoring integrality of " << n_discrete << " variables.\n";
asl->i.need_nl_ = 0;
throw std::runtime_error("Error: " + std::to_string(n_discrete) + " variables are discrete, which Uno cannot handle.");
// asl->i.need_nl_ = 0;
}

// preallocate initial primal and dual solutions
Expand All @@ -33,8 +34,7 @@ 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) : AMPLModel(file_name, generate_asl(file_name)) { }

AMPLModel::AMPLModel(const std::string& file_name, ASL* asl) :
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.),
Expand Down
42 changes: 24 additions & 18 deletions bindings/AMPL/uno_ampl.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2018-2024 Charlie Vanaret
// Licensed under the MIT license. See LICENSE file in the project directory for details.

#
#include "ingredients/globalization_mechanisms/GlobalizationMechanism.hpp"
#include "ingredients/globalization_mechanisms/GlobalizationMechanismFactory.hpp"
#include "ingredients/constraint_relaxation_strategies/ConstraintRelaxationStrategy.hpp"
Expand All @@ -23,28 +24,33 @@ void* operator new(size_t size) {

namespace uno {
void run_uno_ampl(const std::string& model_name, const Options& options) {
// AMPL model
std::unique_ptr<Model> ampl_model = std::make_unique<AMPLModel>(model_name);
try {
// AMPL model
std::unique_ptr<Model> ampl_model = std::make_unique<AMPLModel>(model_name);

// initialize initial primal and dual points
Iterate initial_iterate(ampl_model->number_variables, ampl_model->number_constraints);
ampl_model->initial_primal_point(initial_iterate.primals);
ampl_model->project_onto_variable_bounds(initial_iterate.primals);
ampl_model->initial_dual_point(initial_iterate.multipliers.constraints);
initial_iterate.feasibility_multipliers.reset();
// initialize initial primal and dual points
Iterate initial_iterate(ampl_model->number_variables, ampl_model->number_constraints);
ampl_model->initial_primal_point(initial_iterate.primals);
ampl_model->project_onto_variable_bounds(initial_iterate.primals);
ampl_model->initial_dual_point(initial_iterate.multipliers.constraints);
initial_iterate.feasibility_multipliers.reset();

// reformulate (scale, add slacks, relax the bounds, ...) if necessary
std::unique_ptr<Model> model = ModelFactory::reformulate(std::move(ampl_model), initial_iterate, options);
// reformulate (scale, add slacks, relax the bounds, ...) if necessary
std::unique_ptr<Model> model = ModelFactory::reformulate(std::move(ampl_model), initial_iterate, options);

// create the constraint relaxation strategy, the globalization mechanism and the Uno solver
auto constraint_relaxation_strategy = ConstraintRelaxationStrategyFactory::create(*model, options);
auto globalization_mechanism = GlobalizationMechanismFactory::create(*constraint_relaxation_strategy, options);
Uno uno = Uno(*globalization_mechanism, options);
// create the constraint relaxation strategy, the globalization mechanism and the Uno solver
auto constraint_relaxation_strategy = ConstraintRelaxationStrategyFactory::create(*model, options);
auto globalization_mechanism = GlobalizationMechanismFactory::create(*constraint_relaxation_strategy, options);
Uno uno = Uno(*globalization_mechanism, options);

// solve the instance
Result result = uno.solve(*model, initial_iterate, options);
Uno::print_optimization_summary(options, result);
// std::cout << "memory_allocation_amount = " << memory_allocation_amount << '\n';
// solve the instance
Result result = uno.solve(*model, initial_iterate, options);
Uno::print_optimization_summary(options, result);
// std::cout << "memory_allocation_amount = " << memory_allocation_amount << '\n';
}
catch (std::exception& exception) {
ERROR << exception.what() << '\n';
}
}
} // namespace

Expand Down

0 comments on commit c0b4ad9

Please sign in to comment.