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
-
DataFrame
getValues
() 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
- Variables and Objectives it returns the suffix
- Return
- A DataFrame containing the values for all instances
-
DataFrame
getValues
(StringArgs suffixes)¶ Get the specified suffixes value for all instances in a DataFrame.
- Return
- A DataFrame containing the specified values
- Parameters
suffixes
: Suffixes to get
-
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
-
iterator
begin
() Get an iterator pointing to the first instance in this entity.
-
iterator
end
() 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.
-
StringArray
getIndexingSets
() 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.
- Return
- The string representation of the indexing sets for this entity or an empty array if the entity is scalar
-
std::string
name
() Get the name of this entity.
-
std::size_t
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 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
- Return
- The sum of the dimensions of the indexing sets or 0 if the entity is not indexed
-
bool
isScalar
() Check whether this entity is scalar.
Equivalent to testing whether indexarity() is equal to zero.
- Return
- True if the entity is scalar (not indexed over any set)
-
std::string
toString
() Returns a string representation of this entity (its declaration)
-
std::size_t
numInstances
() Get the number of instances in this entity.
-
VariantRef