Constraint

class ampl::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

inline ConstraintInstance get() const

Get the instance corresponding to a scalar entity.

Throws
  • runtime_error – if the entity has been deleted in the underlying AMPL interpreter

  • logic_error – if the entity is not scalar

Returns

The corresponding instance.

inline ConstraintInstance get(TupleRef index) const

Get the instance with the specified index.

Parameters

index – The tuple specifying the index

Throws
  • 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

Returns

The corresponding instance

inline ConstraintInstance get(VariantRef v1) const

Get the instance with the specified index.

inline ConstraintInstance get(VariantRef v1, VariantRef v2) const

Get the instance with the specified index.

inline ConstraintInstance get(VariantRef v1, VariantRef v2, VariantRef v3) const

Get the instance with the specified index.

inline ConstraintInstance get(VariantRef v1, VariantRef v2, VariantRef v3, VariantRef v4) const

Get the instance with the specified index.

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

Throws
  • 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

Returns

The corresponding instance

inline ConstraintInstance operator[](TupleRef index) const
inline ConstraintInstance operator[](VariantRef v1) const

Public Types

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

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

Public Functions

inline 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”.

Returns

True if logical

inline void drop()

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

inline void restore()

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

inline double body() const

Get the current value of the constraint’s body.

inline std::string astatus() const

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

inline int defvar() const

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

inline double dinit() const

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

inline double dinit0() const

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

inline 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).

inline double lb() const

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

inline double ub() const

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

inline double lbs() const

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

inline double ubs() const

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

inline double ldual() const

Get the current dual value associated with the lower bound.

inline double udual() const

Get the current dual value associated with the upper bounds.

inline double lslack() const

Get the slack at lower bound body - lb

inline double uslack() const

Get the slack at upper bound ub - body

inline double slack() const

Constraint slack (the lesser of lslack and uslack)

inline std::string sstatus() const

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

inline std::string status() const

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

inline 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

inline double val() const

Get the AMPL val suffix.

Valid only for logical constraints.

inline iterator begin() const

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

inline iterator end() const

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

inline iterator find(TupleRef t) const

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

Returns

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

inline StringArray getIndexingSets() const

Get the AMPL string representation of the sets on which this entity is indexed.

The obtained vector can be modified without any effect to the entity.

Returns

The string representation of the indexing sets for this entity or an empty array if the entity is scalar

inline DataFrame getValues() const

Get the principal values of this entity as a DataFrame. The specific returned value depends on the type of entity (see list below).

For:
  • Variables and Objectives it returns the suffix val

  • Parameters it returns their values

  • Constraints it returns the suffix dual

  • Sets it returns all the of the set. Note that it does not

  • apply to indexed sets. See SetInstance::getValues

Returns

A DataFrame containing the values for all instances

inline DataFrame getValues(StringArgs suffixes) const

Get the specified suffixes value for all instances in a DataFrame.

Parameters

suffixes – Suffixes to get

Returns

A DataFrame containing the specified values

inline std::size_t indexarity() const

Get the indexarity of this entity (sum of the dimensions of the indexing sets).

This value indicates the arity of the Tuple to be passed to the method BasicEntity::get() in order to access an instance of this entity. See the following AMPL examples

var x;               # indexarity = 0
var y {1..2};        # indexarity = 1
var z {1..2,3..4};   # indexarity = 2
var zz {{(1, 2)}};   # indexarity = 2

Returns

The sum of the dimensions of the indexing sets or 0 if the entity is not indexed

inline bool isScalar() const

Check whether this entity is scalar.

Equivalent to testing whether indexarity() is equal to zero.

Returns

True if the entity is scalar (not indexed over any set)

inline std::string name() const

Get the name of this entity.

inline std::size_t numInstances() const

Get the number of instances in this entity.

inline void setValues(DataFrame data)

Set the values of this entiy to the correponding values of a DataFrame indexed over the same sets (or a subset). This function assigns the values in the first data column of the passed dataframe to the entity the function is called from. In particular, the statement:

x.setValues(y.getValues());

is semantically equivalent to the AMPL statement:

let {s in S} x[s] := y[s];

Parameters

data – The data to set the entity to

inline std::string toString() const

Returns a string representation of this entity (its declaration)

inline StringArray xref() const

Get the names of all entities which depend on this one.

Returns

An array with the names of all entities which depend on this one.