Parameter

class ampl::Parameter : public ampl::BasicEntity<VariantRef>

Represents an AMPL parameter.

The values can be Double or String (in case of symbolic parameters).

Data can be assigned to the parameter using the methods Parameter::set() and Parameter::setValues directly from objects of this class or using AMPL::setData and a DataFrame object.

Instance access

Methods to access the instances which are part of this Entity

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

Get the instance with the specified index.

inline VariantRef get(VariantRef v1, VariantRef v2) const

Get the instance with the specified index.

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

Get the instance with the specified index.

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

Public Types

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

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

Public Functions

inline bool isSymbolic() const

Returns true if the parameter is declared as symbolic (can store both numerical and string values)

inline bool hasDefault() const

Check if the parameter has a default initial value. In case of the following AMPL code:

param a;
param b default a;

the function will return true for parameter b.

Returns

True if the parameter has a default initial value. Please note that if the parameter has a default expression which refers to another parameter which value is not defined, this will return true.

inline void setValues(const Tuple indices[], internal::Args values, std::size_t nvalues)

Assign the values (string or double) to the parameter instances with the specified indices, equivalent to the AMPL code:

let {i in indices} par[i] := values[i];

Parameters
  • indices – an array of indices of the instances to be set

  • values – values to set

  • nvalues – length of the values array

Throws

logic_error – If called on a scalar parameter

inline void setValues(std::size_t num_rows, internal::Args row_indices, std::size_t num_cols, internal::Args col_indices, const double *data, bool transpose)

Assign the specified values to a 2-d parameter, using the two dimensions as two indices.

For example, the \(m \times n\) matrix:

\[\begin{split}`A = \left( \begin{array}{cccc} a_{11} & a_{12} & ... & a_{1n} \\ a_{21} & a_{22} & ... & a_{2n} \\ ... & ... & ... & ... \\ a_{ m1} & a_{m2} & ... & a_{mn} \end{array} \right)`\end{split}\]

can be assigned to the AMPL parameter: param A {1..m, 1..n}; with the statement setValues(A, false).

As an example, to assign the matrix:

\[\begin{split}`A = \left( \begin{array}{cccc} 11 & 12 \\ 21 & 22 \\ 31 & 32 \end{array} \right)`\end{split}\]

to the AMPL paramater: param A{1..3, 1..2}; we can use the following code:

ampl.eval("param a{1..3, 1..2};");
Parameter a = ampl.getParameter("a");

double values[6];
double rows[3];
double cols[2];
for (int i = 0; i < 3; i++) {
  rows[i] = i + 1;
  for (int j = 0; j < 2; j++)
  values[i * 2 + j] = (i + 1) * 10 + (j + 1);
}
for (int i = 0; i < 2; i++)
  cols[i] = i + 1;

a.setValues(3, rows, 2, cols, values, false);

Parameters
  • num_rows – Number of rows

  • row_indices – Indices of the rows

  • num_cols – Number of rows

  • col_indices – Indices of the rows

  • data – Values to be assigned

  • transpose – True to transpose the values in the matrix

Throws
  • logic_error – If the method is called on a parameter which is not two-dimensional

  • invalid_argument – If the size of ‘values’ do not correspond to the sizes of the underlying indices

inline void setValues(internal::Args values, std::size_t n)

Assign the specified n values to this parameter, assigning them to the parameter in the same order as the indices in the entity.

The number of values in the array must be equal to the specified size.

Parameters
  • values – Values to be assigned.

  • n – Number of values to be assigned (must be equal to the number of instances in this parameter)

Throws
  • invalid_argument – If trying to assign a string to a non symbolic parameter

  • logic_error – If the number of arguments is not equal to the number of instances in this parameter

inline void set(VariantRef value)

Set the value of a scalar parameter.

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

  • logic_error – if this parameter is not scalar.

inline void set(VariantRef index, VariantRef value)

Set the value of a single instance of this parameter.

Throws

runtime_error – if the entity has been deleted in the underlying AMPL

inline void set(TupleRef index, VariantRef value)

Set the value of a single instance of this parameter.

Throws

runtime_error – if the entity has been deleted in the underlying AMPL

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