Constraint

class Constraint : public ampl::BasicEntity<ConstraintInstance>

This class represents an algebraic or logical constraint.

In case the constraint is scalar, its values can be accessed via functions like Constraint::body and Constraint::dual. All the AMPL suffixes for constraints (see http://www.ampl.com/NEW/suffbuiltin.html) are available through methods of this class with the same name (and methods of ConstraintInstance for indexed constraints).

Note that, since this class represents both algebraic and logical constraints, some suffixes might not be available for every entity.

An std::logic_error is thrown if one of such methods is called for a non-scalar constraint and if a method corresponding to a suffix which is not supported by the type of the constraint is called. A runtime error is thrown if any property of an entity which has been deleted from the underlying interpreter is accessed.

The instances, represented by the class ConstraintInstance can be accessed via the operator Constraint::operator[](), via the methods Constraint::get() or via the iterators provided.

To gain access to all the values in an entity (for all instances and all suffixes for that entities), see Entity::getValues() and the ampl::DataFrame class.

Instance access

Methods to access the instances which are part of this Entity

InstanceClass get() const

Get the instance corresponding to a scalar entity.

Return
The corresponding instance.
Exceptions
  • runtime_error: if the entity has been deleted in the underlying AMPL interpreter
  • logic_error: if the entity is not scalar

InstanceClass get(TupleRef index) const

Get the instance with the specified index.

Return
The corresponding instance
Parameters
  • index: The tuple specifying the index
Exceptions
  • out_of_range: if an instance with the specified index does not exist
  • out_of_range: if the entity has been deleted in the underlying AMPL interpreter
  • ampl::UnsupportedOperationException: if the entity is scalar

InstanceClass get(VariantRef v1) const

Get the instance with the specified index.

InstanceClass get(VariantRef v1, VariantRef v2) const

Get the instance with the specified index.

InstanceClass get(VariantRef v1, VariantRef v2, VariantRef v3) const

Get the instance with the specified index.

InstanceClass get(VariantRef v1, VariantRef v2, VariantRef v3, VariantRef v4) const

Get the instance with the specified index.

Return
The corresponding instance
Parameters
  • v1: The VariantRef specifying the first element of the indexing tuple
  • v2: The VariantRef specifying the second element of the indexing tuple
  • v3: The VariantRef specifying the third element of the indexing tuple
  • v4: The VariantRef specifying the fourth element of the indexing tuple
Exceptions
  • out_of_range: if an instance with the specified index does not exist
  • runtime_error: if the entity has been deleted in the underlying AMPL interpreter
  • ampl::UnsupportedOperationException: if the entity is scalar

InstanceClass operator[](TupleRef index) const
InstanceClass operator[](VariantRef v1) const

Public Types

typedef internal::CountedIterator<internal::EntityWrapper<InstanceClass>> iterator

Iterator for entities, represented by an iterator pointing to elements of type std::pair<TupleRef, InstanceClass>

Public Functions

bool isLogical() const

Check if the constraint is a logical constraint.

The available suffixes differ between logical and non logical constraints. See http://www.ampl.com/NEW/suffbuiltin.html

for a list of the available suffixes for algebraic constraints. The suffixes available for logical constraints are marked on the method description by “Valid only for logical

constraints”.

Return
True if logical

void drop()

Drop all instances in this constraint entity, corresponding to the AMPL code: drop constraintname;

void restore()

Restore all instances in this constraint entity, corresponding to the AMPL code: restore constraintname;

double body() const

Get the current value of the constraint’s body.

std::string astatus() const

Get the current AMPL status (dropped, presolved, or substituted out)

int defvar() const

Get the index in _var of “defined variable” substituted out by the constraint.

double dinit() const

Get the current initial guess for the constraint’s dual variable.

double dinit0() const

Get the original initial guess for the constraint’s dual variable.

double dual() const

Get the current value of the constraint’s dual variable.

Note that dual values are often reset by the underlying AMPL interpreter by the presolve functionalities triggered by some methods. A possible workaround is to set the option presolve; to false (see setBoolOption).

double lb() const

Get the current value of the constraint’s lower bound.

double ub() const

Get the current value of the constraint’s upper bound.

double lbs() const

Get the constraint lower bound sent to the solver (reflecting adjustment for fixed variables)

double ubs() const

Get the constraint upper bound sent to the solver (reflecting adjustment for fixed variables)

double ldual() const

Get the current dual value associated with the lower bound.

double udual() const

Get the current dual value associated with the upper bounds.

double lslack() const

Get the slack at lower bound body - lb

double uslack() const

Get the slack at upper bound ub - body

double slack() const

Constraint slack (the lesser of lslack and uslack)

std::string sstatus() const

Get the solver status (basis status of constraint’s slack or artificial variable)

std::string status() const

Get the AMPL status if not in, otherwise solver status.

void setDual(double dual)

Set the value of the dual variable associated to this constraint (valid only if the constraint is scalar).

Equivalent to the AMPL statement:

let c := dual;

Note that dual values are often reset by the underlying AMPL interpreter by the presolve functionalities triggered by some methods. A possible workaround is to set the option presolve to false (see setBoolOption).

Parameters
  • dual: The value to be assigned to the dual variable

double val() const

Get the AMPL val suffix.

Valid only for logical constraints.

iterator begin() const

Get an iterator pointing to the first instance in this entity.

iterator end() const

Get an iterator pointing after the last instance in this entity.

iterator find(TupleRef t) const

Searches the current entity for an instance with the specified index.

Return
an iterator to the wanted entity if found, otherwise an iterator to BasicEntity::end.