Entity

class amplpy.Entity(_impl, wrap_function=None)

An AMPL entity such as a parameter or a variable.

An entity can either represent a single instance of an AMPL algebraic entity or, if the corresponding declaration has an indexing expression, a mapping from keys to instances. In the derived classes, it has methods to access instance-level properties which can be used in case the represented entity is scalar.

To gain access to all the values in an entity (for all instances and all suffixes for that entities), use the function get_values().

The algebraic entities which currently have an equivalent class in the API are:

__iter__()
__getitem__(index)
get(*index)

Get the instance with the specified index.

Returns:

The corresponding instance.

find(*index)

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

Returns:

The wanted instance if found, otherwise it returns None.

instances()

Get all the instances in this entity..

name()

Get the name of this entity.

indexarity()

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

is_scalar()

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

num_instances()

Get the number of instances in this entity.

get_indexing_sets()

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.

xref()

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

Returns:

A list with the names of all entities which depend on this one.

get_values(suffixes=None)

If a list of suffixes is provided, get the specified suffixes value for all instances. Otherwise, get all the principal values of this entity. 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 members of the set. Note that it does not apply to indexed sets. See get_values().

Raises:

RuntimeError: if there are issues with the data.

Retruns:

A DataFrame containing the values for all instances.

to_pandas(**kwargs)

Equivalent to Entity.get_values().to_pandas().

to_dict(**kwargs)

Equivalent to Entity.get_values().to_dict().

to_list(**kwargs)

Equivalent to Entity.get_values().to_list().

set_values(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.set_values(y.get_values())

is semantically equivalent to the AMPL statement:

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

data: The data to set the entity to.

__annotations__ = {}