Follow us on Twitter and LinkedIn to get the latest updates from the dev team!
The Set object stores the information regarding a set in the algebraic model. It can be indexed or simple. In case of simple sets, its members are accessible from this object, through methods like Set.getValues or asSet.
Set.getValues
asSet
If it is indexed, the single sets, represented as SetInstance are accessible through the function Set.get.
SetInstance
Set.get
Data can be assigned to the set via Set.setValues and SetInstance.setValues
Set.setValues
SetInstance.setValues
v = get() v = get(key)
v = get()
v = get(key)
v = get() returns the SetInstance (only for not indexed sets) v = get(key) returns the SetInstance corresponding to the specified key Note that the same effect can be achieved using MATLAB’s indexing operator (round brackets).
key
The key of the set instance to look up.
v
The specified set instance (SetInstance)
Create an indexed set and get an instance
ampl.eval('set A{1..2}; let A[1] := {''a'', ''b''};'); A = ampl.getSet('A'); setinstance = A.get(1); cell(setinstance.asStrings())
gives:
ans = 'a' 'b'
df = v.getValues()
v.getValues()
This functions return a dataframe with all the values of the set, decoupled from the underlying AMPL. To gain access to the data, see DataFrame.
DataFrame
df
A dataframe containing all the values in the set
Create an AMPL set and get its values in a dataframe
ampl.eval('set A{1..2}; let A[1] := {''a'', ''b''}; let A[2] := {''c'', ''d''};'); A = ampl.getSet('A'); df = A.getValues(); df
df = 1 .. 2 | A 1.0 | [a, b] 2.0 | [c, d]
v = set.asSet()
v = set.asSet() Get a copy of the elements in this set as a java Set of Objects (Strings and double numbers). Valid only for not indexed sets.
Get a copy of the elements in this set as a java Set of Objects (Strings and double numbers).
Create a set in AMPL and get it as a Java Set.
ampl.eval('set A := 2000..2020 by 5;'); A = ampl.getSet('A'); A = s.asSet()
set = [2000.0, 2005.0, 2010.0, 2015.0, 2020.0]
set.setValues(members)
v = set.setValues(members) Assign data to this set instance.
v = set.setValues(members)
members
List of strings and numbers which represent the members of this set.
For multidimensional sets, each item of the members list must be either a cell array or a Tuple representing each item.
Create a set in AMPL and assign some data to it from MATLAB
ampl.eval('set A;'); s = ampl.getSet('A').get(); s.setValues({4.2, '4.2', 'a', 'b'}); s.asSet
ans = [4.2, 4.2, a, b]
Create a multidimensional set in AMPL and assign some data to it from MATLAB, using Tuples and using cell arrays.
ampl.eval('set A dimen 2;'); A = ampl.getSet('A').get(); A.setValues( {1, 'a'}, {2, 'b'}); A % Alternative way, with tuples ampl.eval('set B dimen 2;'); B = ampl.getSet('B').get(); t1 = Tuple(1, 'a'); t2 = Tuple(2, 'b'); B.setValues(t1, t2); B
A = set A = {(1.0,'a'), (2.0,'b')}; B = set B = {(1.0,'a'), (2.0,'b')};
b = set.contains(member) b = set.containsAll(members)
b = set.contains(member)
b = set.containsAll(members)
b = set.contains(member) Checks whether the set contains the specified member. Valid only for not indexed sets. b = set.containsAll(members) Checks whether the set contains all the specified members. Valid only for not indexed sets.
member
The element (string or number) to be found in the set
A cell arrray of strings and numbers which represent the members to be found
Create a set in AMPL and checks for its members
ampl.eval('set A := {''a'', ''b'', 4};'); A = ampl.getSet('A'); A.contains('a') A.contains(4) A.containsAll({'a', 'b', 4}) A.containsAll({'a', 'b', 5})
ans = 0 ans = 1 ans = 1 ans = 0
b = set.size(member)
b = set.size(member) For not indexed sets, get the cardinality of the set. For indexed sets, get the number of set instances into this set.
b
The cardinality (number of elements) of this set or the number of set instances.
Create a set in AMPL and look at its size
ampl.eval('set A := {''a'', ''b'', 4};'); A = ampl.getSet('A'); A.size()
ans = 3
b = Set.arity()
b = Set.arity() Returns the dimension of the items in this set. set.
The arity (number of elements) of the members of this set.
Create a set in AMPL as a set product and look at its arity
ampl.eval('set A; set B;'); ampl.eval('set C := A cross B;'); C = ampl.getSet('C'); C.arity()
ans = 2
previous
Parameter
next