Parameter¶
-
class
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
-
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 interpreterlogic_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 existout_of_range
: if the entity has been deleted in the underlying AMPL interpreterampl::UnsupportedOperationException
: if the entity is scalar
-
VariantRef
get
(VariantRef v1) const¶ Get the instance with the specified index.
-
VariantRef
get
(VariantRef v1, VariantRef v2) const¶ Get the instance with the specified index.
-
VariantRef
get
(VariantRef v1, VariantRef v2, VariantRef v3) const¶ Get the instance with the specified index.
-
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 tuplev2
: The VariantRef specifying the second element of the indexing tuplev3
: The VariantRef specifying the third element of the indexing tuplev4
: The VariantRef specifying the fourth element of the indexing tuple
- Exceptions
out_of_range
: if an instance with the specified index does not existruntime_error
: if the entity has been deleted in the underlying AMPL interpreterampl::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 setvalues
: values to setnvalues
: 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 statementsetValues(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 rowsrow_indices
: Indices of the rowsnum_cols
: Number of rowscol_indices
: Indices of the rowsdata
: Values to be assignedtranspose
: True to transpose the values in the matrix
- Exceptions
logic_error
: If the method is called on a parameter which is not two-dimensionalinvalid_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 parameterlogic_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 AMPLlogic_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.
-
VariantRef