didppy.SetExpr

class didppy.SetExpr(value)

Set expression.

If an operator (-, &, ^, |) with a SetExpr, SetVar, or SetConst is applied, a new SetExpr is returned.

If a comparison operator (<, <=, ==, !=, >, >=) with a SetExpr, SetVar, or SetConst is applied, a Condition is returned.

Note that didppy.max() and didppy.min() should be used instead of max() and min() as comparison operators are overloaded.

Parameters:

value (SetConst) – A set constant from which a constant expression is created.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> const = model.create_set_const(object_type=obj, value=[0, 1])
>>> expr = dp.SetExpr(const)
>>> const = model.create_set_const(object_type=obj, value=[1, 2])
>>> (expr - const).eval(state, model)
{0}
>>> (expr & const).eval(state, model)
{1}
>>> (expr ^ const).eval(state, model)
{0, 2}
>>> (expr | const).eval(state, model)
{0, 1, 2}
>>> (expr < const).eval(state, model)
False
>>> (expr <= const).eval(state, model)
False
>>> (expr == const).eval(state, model)
False
>>> (expr != const).eval(state, model)
True
>>> (expr > const).eval(state, model)
False
>>> (expr >= const).eval(state, model)
False

Methods

add(element)

Adds an element to a set.

complement()

Returns the complement set.

contains(element)

Returns a condition checking if an element is included.

difference(other)

Returns a set where all elements in an input set are removed.

discard(element)

Removes an element from a set.

eval(state, model)

Evaluates the expression.

intersection(other)

Returns the intersection with another set.

is_empty()

Returns a condition checking if the set is empty.

isdisjoint(other)

Checks if two sets are disjoint.

issubset(other)

Checks if this set is a subset of another set.

issuperset(other)

Checks if this set is a superset of another set.

len()

Returns the cardinality of a set.

remove(element)

Removes an element from a set.

symmetric_difference(other)

Returns a set which only contains elements included in either of two sets but not in both.

union(other)

Returns the union of two sets.

add(element)

Adds an element to a set.

This method does not change the instance itself.

Parameters:

element (ElementExpr, ElementVar, ElementResourceVar, or int) – Element added to the set.

Returns:

The set where the element is added.

Return type:

SetExpr

Raises:

OverflowError – If element is int and negative.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> const = model.create_set_const(object_type=obj, value=[0, 1])
>>> expr = dp.SetExpr(const)
>>> expr.add(2).eval(state, model)
{0, 1, 2}
complement()

Returns the complement set.

Returns:

The complement set.

Return type:

SetExpr

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> const = model.create_set_const(object_type=obj, value=[0, 1])
>>> expr = dp.SetExpr(const)
>>> expr.complement().eval(state, model)
{2, 3}
contains(element)

Returns a condition checking if an element is included.

Parameters:

element (ElementExpr, ElementVar, ElementResourceVar, or int) – Element to check.

Returns:

The condition checking if an element is included in the set.

Return type:

Condition

Raises:

OverflowError – If element is int and negative.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> const = model.create_set_const(object_type=obj, value=[0, 1])
>>> expr = dp.SetExpr(const)
>>> expr.contains(0).eval(state, model)
True
difference(other)

Returns a set where all elements in an input set are removed.

This method is the same as - operation. This method does not change the instance itself.

Parameters:

other (SetExpr, SetVar, or SetConst) – Set to remove.

Returns:

The set where all elements in other are removed.

Return type:

SetExpr

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> const = model.create_set_const(object_type=obj, value=[0, 1])
>>> expr = dp.SetExpr(const)
>>> const = model.create_set_const(object_type=obj, value=[1, 2])
>>> expr.difference(const).eval(state, model)
{0}
discard(element)

Removes an element from a set.

This method does not change the instance itself.

Parameters:

element (ElementExpr, ElementVar, ElementResourceVar, or int) – Element removed from the set.

Returns:

The set where the element is removed.

Return type:

SetExpr

Raises:

OverflowError – If element is int and negative.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> const = model.create_set_const(object_type=obj, value=[0, 1])
>>> expr = dp.SetExpr(const)
>>> expr.discard(1).eval(state, model)
{0}
eval(state, model)

Evaluates the expression.

Parameters:
  • state (State) – State.

  • model (Model) – DyPDL Model.

Returns:

Value of the expression.

Return type:

set

Raises:

PanicException – If the expression is not valid.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> obj = model.add_object_type(number=4)
>>> var = model.add_set_var(object_type=obj, target=[0])
>>> expr = var.add(1)
>>> state = model.target_state
>>> expr.eval(state, model)
{0, 1}
intersection(other)

Returns the intersection with another set.

This method is the same as & operation. This method does not change the instance itself.

Parameters:

other (SetExpr, SetVar, or SetConst) – Set to take the intersection with.

Returns:

The intersection.

Return type:

SetExpr

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> const = model.create_set_const(object_type=obj, value=[0, 1])
>>> expr = dp.SetExpr(const)
>>> const = model.create_set_const(object_type=obj, value=[1, 2])
>>> expr.intersection(const).eval(state, model)
{1}
is_empty()

Returns a condition checking if the set is empty.

Returns:

The condition checking if the set is empty.

Return type:

Condition

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> const = model.create_set_const(object_type=obj, value=[0, 1])
>>> expr = dp.SetExpr(const)
>>> expr.is_empty().eval(state, model)
False
isdisjoint(other)

Checks if two sets are disjoint.

Parameters:

other (SetExpr, SetVar, or SetConst) – The other set.

Returns:

The condition that the two sets are disjoint.

Return type:

Condition

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> const = model.create_set_const(object_type=obj, value=[0, 1])
>>> expr = dp.SetExpr(const)
>>> const = model.create_set_const(object_type=obj, value=[2, 3])
>>> expr.isdisjoint(const).eval(state, model)
True
issubset(other)

Checks if this set is a subset of another set.

Parameters:

other (SetExpr, SetVar, or SetConst) – The other set.

Returns:

The condition that the two sets are disjoint.

Return type:

Condition

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> const = model.create_set_const(object_type=obj, value=[0, 1])
>>> expr = dp.SetExpr(const)
>>> const = model.create_set_const(object_type=obj, value=[0, 1, 2])
>>> expr.issubset(const).eval(state, model)
True
issuperset(other)

Checks if this set is a superset of another set.

Parameters:

other (SetExpr, SetVar, or SetConst) – The other set.

Returns:

The condition that the two sets are disjoint.

Return type:

Condition

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> const = model.create_set_const(object_type=obj, value=[0, 1])
>>> expr = dp.SetExpr(const)
>>> const = model.create_set_const(object_type=obj, value=[0])
>>> expr.issuperset(const).eval(state, model)
True
len()

Returns the cardinality of a set.

Returns:

The cardinality.

Return type:

IntExpr

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> const = model.create_set_const(object_type=obj, value=[0, 1])
>>> expr = dp.SetExpr(const)
>>> expr.len().eval(state, model)
2
remove(element)

Removes an element from a set.

This method does not change the instance itself.

Parameters:

element (ElementExpr, ElementVar, ElementResourceVar, or int) – Element removed from the set.

Returns:

The set where the element is removed.

Return type:

SetExpr

Raises:

OverflowError – If element is int and negative.

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> const = model.create_set_const(object_type=obj, value=[0, 1])
>>> expr = dp.SetExpr(const)
>>> expr.remove(1).eval(state, model)
{0}
symmetric_difference(other)

Returns a set which only contains elements included in either of two sets but not in both.

This method is the same as ^ operation. This method does not change the instance itself.

Parameters:

other (SetExpr, SetVar, or SetConst) – Set to take the symmetric difference with.

Returns:

The symmetric difference set.

Return type:

SetExpr

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> const = model.create_set_const(object_type=obj, value=[0, 1])
>>> expr = dp.SetExpr(const)
>>> const = model.create_set_const(object_type=obj, value=[1, 2])
>>> expr.symmetric_difference(const).eval(state, model)
{0, 2}
union(other)

Returns the union of two sets.

This method is the same as | operation. This method does not change the instance itself.

Parameters:

other (SetExpr, SetVar, or SetConst) – Set to take the union with.

Returns:

The union.

Return type:

SetExpr

Examples

>>> import didppy as dp
>>> model = dp.Model()
>>> state = model.target_state
>>> obj = model.add_object_type(number=4)
>>> const = model.create_set_const(object_type=obj, value=[0, 1])
>>> expr = dp.SetExpr(const)
>>> const = model.create_set_const(object_type=obj, value=[1, 2])
>>> expr.union(const).eval(state, model)
{0, 1, 2}