Skip to content

Commit

Permalink
Addressed issue with dual convention in AMPL: dual signs are flipped …
Browse files Browse the repository at this point in the history
…and call to lagscale() removed
  • Loading branch information
cvanaret committed Oct 25, 2024
1 parent 20e0b2c commit fd9ad8f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 6 additions & 6 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/UnaryNegation.hpp"
#include "Uno.hpp"

namespace uno {
Expand Down Expand Up @@ -53,6 +54,7 @@ namespace uno {
variable_status(this->number_variables),
constraint_type(this->number_constraints),
constraint_status(this->number_constraints),
multipliers_with_flipped_sign(this->number_constraints),
equality_constraints_collection(this->equality_constraints),
inequality_constraints_collection(this->inequality_constraints),
lower_bounded_variables_collection(this->lower_bounded_variables),
Expand Down Expand Up @@ -201,10 +203,6 @@ namespace uno {
const int upper_triangular = 1;
this->number_asl_hessian_nonzeros = static_cast<size_t>((*(this->asl)->p.Sphset)(this->asl, nullptr, objective_number, 1, 1, upper_triangular));
this->asl_hessian.reserve(this->number_asl_hessian_nonzeros);

// use Lagrangian scale: in AMPL, the Lagrangian is f + lambda.g, while Uno uses f - lambda.g
fint error_flag{};
lagscale_ASL(this->asl, -1., &error_flag);
}

size_t AMPLModel::number_objective_gradient_nonzeros() const {
Expand Down Expand Up @@ -282,15 +280,17 @@ namespace uno {

// evaluate the Hessian: store the matrix in a preallocated array this->asl_hessian
const int objective_number = -1;
// flip the signs of the multipliers: in AMPL, the Lagrangian is f + lambda.g, while Uno uses f - lambda.g
this->multipliers_with_flipped_sign = -multipliers;
if (this->fixed_hessian_sparsity) {
(*(this->asl)->p.Sphes)(this->asl, nullptr, const_cast<double*>(this->asl_hessian.data()), objective_number, &objective_multiplier,
const_cast<double*>(multipliers.data()));
const_cast<double*>(this->multipliers_with_flipped_sign.data()));
}
else {
double* objective_multiplier_pointer = (objective_multiplier != 0.) ? &objective_multiplier : nullptr;
const bool all_zeros_multipliers = are_all_zeros(multipliers);
(*(this->asl)->p.Sphes)(this->asl, nullptr, const_cast<double*>(this->asl_hessian.data()), objective_number, objective_multiplier_pointer,
all_zeros_multipliers ? nullptr : const_cast<double*>(multipliers.data()));
all_zeros_multipliers ? nullptr : const_cast<double*>(this->multipliers_with_flipped_sign.data()));
}

// generate the sparsity pattern in the right sparse format
Expand Down
2 changes: 2 additions & 0 deletions bindings/AMPL/AMPLModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <vector>
#include "model/Model.hpp"
#include "linear_algebra/SparseVector.hpp"
#include "linear_algebra/Vector.hpp"
#include "symbolic/CollectionAdapter.hpp"

// include AMPL Solver Library (ASL)
Expand Down Expand Up @@ -82,6 +83,7 @@ namespace uno {
std::vector<BoundType> constraint_status; /*!< Status of the constraints (EQUAL_BOUNDS, BOUNDED_LOWER, BOUNDED_UPPER, BOUNDED_BOTH_SIDES,
* UNBOUNDED) */
std::vector<size_t> linear_constraints;
mutable Vector<double> multipliers_with_flipped_sign;

// lists of variables and constraints + corresponding collection objects
std::vector<size_t> equality_constraints{};
Expand Down

0 comments on commit fd9ad8f

Please sign in to comment.