Parameter

class ampl::Parameter

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

VariantRef get() const

Get the instance corresponding to a scalar entity.

Return
The corresponding instance.
Exceptions
  • runtime_error -

    if the entity has been deleted in the underlying AMPL interpreter

  • logic_error -

    if the entity is not scalar

VariantRef get(TupleRef index) const

Get the instance with the specified index.

Return
The corresponding instance
Parameters
  • index -

    The tuple specifying the index

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

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

Get the instance with the specified index.

Return
The corresponding instance
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

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

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

bool isSymbolic() const

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

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.

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

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

Exceptions
  • logic_error -

    If called on a scalar parameter

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

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

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)

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

void set(VariantRef value)

Set the value of a scalar parameter.

Exceptions
  • runtime_error -

    if the entity has been deleted in the underlying AMPL

  • logic_error -

    if this parameter is not scalar.

void set(VariantRef index, VariantRef value)

Set the value of a single instance of this parameter.

Exceptions
  • runtime_error -

    if the entity has been deleted in the underlying AMPL

void set(TupleRef index, VariantRef value)

Set the value of a single instance of this parameter.

Exceptions
  • runtime_error -

    if the entity has been deleted in the underlying AMPL

iterator begin() const

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

iterator end() const

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

iterator find(TupleRef t) const

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

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