Entity

public abstract class Entity<T>

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

Fields

ARE_DEPENDENCIES_VALID

static final int ARE_DEPENDENCIES_VALID

Flag that specifies whether the entity’s dependencies might have changed (so far just through normal “interpret”)

ENTITY_DELETED

static final int ENTITY_DELETED

Flag that specifies whether the entity has been deleted (is invalid)

IS_VALID

static final int IS_VALID

Flag that specifies whether the entity contains valid (up to date) data. Initialization is performed lazily when the entity is first accessed except when it is done through the data statements.

NO_DATA

static final int NO_DATA

Flag that specifies whether the entity doesn’t contain any data.

ampl

AMPL ampl

declaration

String declaration

engine

Engine engine

map

Map<Object, T> map

Key can be Double, String or Tuple. If this entity represents a single object, the entity contains at most one item and the key is null.

name

String name

Constructors

Entity

Entity()

Constructor

Entity

Entity(AMPL ampl, Engine engine, String name, EntityType type)

Constructor

Parameters:
  • ampl – AMPL instance
  • engine – Engine instance
  • name – Entity’s name

Methods

addDependent

final void addDependent(Entity<?> dep)

areDependenciesUpdated

final boolean areDependenciesUpdated()

checkDimensionOfKey

boolean checkDimensionOfKey(Object t)

checkForDeletion

boolean checkForDeletion()

checkHasIndexing

void checkHasIndexing()

clearNoData

final void clearNoData()

Clear the nodata flag

displayEmbedded

String displayEmbedded()

Display the value(s) of the entity, using the embedded display routine

Returns:String visualization

entrySet

public Set<Map.Entry<Object, T>> entrySet()

Get a set view of this entity. Each entry in the set contains the key (value of the indices) and the corresponding instance. For a scalar entity, a set with just one entry is returned, which has a null key and the only instance as value.

The following example prints indices and values of the variable Buy indexed over the set FOOD (taken from the diet model):

for(Map.Entry<Object, VariableInstance> e: ampl.getVariable("Buy").entrySet()) {
        Object food = e.getKey();
        VariableInstance buy = e.getValue();
        System.out.format("%s %g%n", food, buy.value());
}
Returns:An unmodifiable set containing all the entries of this entity.

executeAMPLStatement

boolean executeAMPLStatement(String statement)

fromMatlab

Object[] fromMatlab(double[] indices)

fromMatlab

<J> Object[] fromMatlab(J[] indices)

get

public abstract T get()

Get the instance corresponding to a scalar entity.

Throws:
Returns:

The corresponding instance.

get

public abstract T get(Object... key)

Get the instance corresponding to the specified key. See Access to instances and values for further information on how the key can be specified.

Parameters:
  • key – Key of the instance to be found.
Throws:
Returns:

The instance corresponding to the key, or null if the specified key is not found.

get

public abstract T get(Tuple key)

Get the instance corresponding to the specified tuple. See Access to instances and values for further information on how the key can be specified.

Parameters:
  • key – Key (in form of Tuple) of the instance to be found.
Throws:
Returns:

The instance corresponding to the key, or null if the specified key is not found.

getAsDoubles

double[] getAsDoubles()

Get value of all the instances in the current entity as doubles. Returns default suffixes: value for variables and objectives, body for constraints and the value of parameters. Casts the members of sets to doubles.

Throws:
Returns:

Array of double numbers

getAsDoubles

double[] getAsDoubles(String suffix)

Get specific suffixes of the current entity as doubles

Parameters:
  • suffix – The specific suffix to return
Returns:

Array of double numbers

getAsObjects

Object[] getAsObjects(String suffix)

Get specific suffixes of the current entity as objects

Parameters:
  • suffix – The specific suffix to return
Returns:

Array of double numbers

getDeclaration

String getDeclaration()

Get the declaration of the AMPL entity represented by this Entity.

Returns:AMPL declaration of this entity.

getDependents

ArrayList<Entity<?>> getDependents()

getIndexingDummies

String getIndexingDummies()

getIndexingSets

public String[] getIndexingSets()

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

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

getIndices

public Set<Object> getIndices()

Get all the keys in this entity. Not modifiable as a collection.

Returns:All the keys currently in the entity.

getInstances

public Collection<T> getInstances()

Get all instances in this entity. Not modifiable as a collection

Returns:All instances currently generated for this entity.

getMethod

T getMethod(Object... key)

getMethod

T getMethod(Tuple key)

getValues

public DataFrame getValues()

Get the principal values of this entity as a DataFrame. For variables and objectives it returns their suffix val for parameters it returns their values for constraints it returns the suffix dual for sets it returns all the members of the set

Returns:All instances

getValues

public DataFrame getValues(String... suffixes)

Get all the specified values for this entity as a DataFrame object. The valid strings are all AMPL built-in suffixes for the entity under consideration.

Parameters:
  • suffixes – A list of suffixes to be returned
Throws:
Returns:

A DataFrame object with a column for each indexing set and a column for each one of the specified suffixes.

hasData

boolean hasData()

hasIndexing

public boolean hasIndexing()

Check whether this entity is indexed over any set. Equivalent to testing whether Entity.indexarity is greater than zero.

Returns:True if the entity is not scalar (is indexed over a set)

hasIndexingInstances

boolean hasIndexingInstances()

indexarity

public int indexarity()

Get the indexarity of this entity (sum of the dimensions of the indexing sets). This value indicates how many elements are to be passed to the method Entity.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

invalidate

void invalidate()

invalidateDependencies

void invalidateDependencies()

invalidateDependents

void invalidateDependents()

isDeleted

final boolean isDeleted()

isScalar

public boolean isScalar()

Check whether this entity is scalar. Equivalent to testing whether Entity.indexarity is equal to zero.

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

isValid

final boolean isValid()

name

public final String name()

Get the AMPL name of this entity

numInstances

public int numInstances()

Get the number of instances in this entity

onDelete

final void onDelete()

printArguments

void printArguments(Object o)

refreshDeclaration

boolean refreshDeclaration()

Refresh the declaration (and sets) of this entity

refreshInstances

abstract void refreshInstances()

setDeclaration

abstract void setDeclaration(String declaration)

setIndexArity

void setIndexArity(int indexarity)

setName

final void setName(String name)

setNoData

final void setNoData()

setSets

void setSets(String[] sets)

setValues

public 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

setnewInstances

void setnewInstances(Instance<?>[] newinstances)

toString

public String toString()

Get a string representation of this entity; for all entity types, it returns the AMPL declaration.

transformKey

Object transformKey(Object... key)

update

final void update()

Updates the entity if necessary by refreshing the instances

updateDependenciesFlag

void updateDependenciesFlag()

SetInstance dependencies valid flag

validate

void validate()

Flag this entity to valid

xref

public List<Entity<?>> xref()

Get all the entities which depend on this one