Variable

class ampl::Variable : public ampl::BasicEntity<VariableInstance>

Represents an AMPL decision variable.

Note that, in case of a scalar variable, all the properties of the variable instance can be accessed through methods like Variable.value. The methods have the same name of the corresponding AMPL suffixes. See http://www.ampl.com/NEW/suffbuiltin.html for a list of the available suffixes.

All these methods throw an std::logic_error if called for an entity which is not scalar and an std::runtime_error if the entity has been deleted.

The instances, represented by the class VariableInstance can be accessed via the operator Variable::operator[](), via the methods Variable::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 DataFrame class.

Instance access

Methods to access the instances which are part of this Entity

inline VariableInstance 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 VariableInstance 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 VariableInstance get(VariantRef v1) const

Get the instance with the specified index.

inline VariableInstance get(VariantRef v1, VariantRef v2) const

Get the instance with the specified index.

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

Get the instance with the specified index.

inline VariableInstance 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 VariableInstance operator[](TupleRef index) const
inline VariableInstance operator[](VariantRef v1) const

Public Types

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

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

Public Functions

inline double value() const

Get the current value of this variable.

inline var::Integrality integrality() const

Get the integrality type for this variable.

Returns

Type of integrality (integer, binary, continuous)

inline void fix()

Fix all instances of this variable to their current value.

inline void fix(double value)

Fix all instances of this variable to the specified value.

inline void unfix()

Unfix this variable instances.

inline void setValue(double value)

Set the current value of this variable (does not fix it), equivalent to the AMPL command let

Parameters

value – Value to be set

inline std::string astatus() const

Get the AMPL status (fixed, presolved, or substituted out)

inline int defeqn() const

Get the index in _con of “defining constraint” used to substitute variable out.

inline double dual() const

Get the dual value on defining constraint of variable substituted out.

inline double init() const

Get the current initial guess.

inline double init0() const

Get the original initial guess (set by := ordefault or by a data statement)

inline double lb() const

Returns the current lower bound. See Note on variables suffixes.

inline double ub() const

Returns the current lower bound. See Note on variables suffixes.

inline double lb0() const

Returns the initial lower bounds, from the var declaration.

inline double ub0() const

Returns the initial upper bound, from the var declaration.

inline double lb1() const

Returns the weaker lower bound from AMPL’s presolve phase.

inline double ub1() const

Returns the weaker upper bound from AMPL’s presolve phase.

inline double lb2() const

Returns the stronger lower bound from AMPL’s presolve phase.

inline double ub2() const

Returns the stronger upper bound from AMPL’s presolve phase.

inline double lrc() const

Returns the reduced cost at lower bound.

inline double urc() const

Returns the reduced cost at upper bound.

inline double lslack() const

Return the slack at lower bound (val - lb). See Note on variables suffixes.

inline double uslack() const

Return the slack at upper bound (ub - val). See

Note on variables suffixes.

inline double rc() const

Get the reduced cost (at the nearer bound)

inline double slack() const

Returns the bound slack which is the lesser of lslack() and uslack(). See Note on variables suffixes.

inline std::string sstatus() const

Solver status (basis status of variable)

inline std::string status() const

AMPL status if not in, otherwise solver status

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.

enum ampl::var::Integrality

Integrality of variables.

See

Variable::integrality()

Values:

enumerator CONTINUOUS

Continuous variable.

enumerator BINARY

Binary variable.

enumerator INTEGER

Integer variable.