Constraints¶
This module implements a declarative langauge of constraints on sequence values. The language consists of:
Atomic constraints
TakesValue
,TakesValues
,AllDifferent
, andFunction
.
Examples in this file will reference the following SCHEMA
:
SCHEMA = OrderedDict()
SCHEMA["aa1"] = ["P", "L", None]
SCHEMA["aa2"] = ["N", "Y", "T", None]
SCHEMA["aa3"] = ["R", "S"]
- class pyroed.constraints.Constraint[source]¶
Bases:
abc.ABC
Abstract base class for constraints.
Derived classes must implement the
__call__()
method.
- class pyroed.constraints.Function(fn: Callable[[torch.Tensor], torch.Tensor])[source]¶
Bases:
pyroed.constraints.Constraint
Constrains the choices by a black box user-provided function.
Example:
def constraint(choices: torch.Tensor): # Put an upper bound on the sequences. return choices.float().sum(-1) < 8 CONSTRAINTS.append(Function(constraint))
- Parameters
fn (callable) – A function inputting a batch of encoded sequences and returning a batched feasability tensor.
- class pyroed.constraints.TakesValue(name: str, value: Optional[str])[source]¶
Bases:
pyroed.constraints.Constraint
Constrains a site to take a fixed value.
Example:
CONSTRAINTS.append(TakesValue("aa1", "P"))
- Parameters
name (str) – Name of a sequence variable in the schema.
value – The value that the variable can take.
- class pyroed.constraints.TakesValues(name: str, *values: Optional[str])[source]¶
Bases:
pyroed.constraints.Constraint
Constrains a site to take one of a set of values.
Example:
CONSTRAINTS.append(TakesValue("aa1", "P", "L"))
- Parameters
name (str) – Name of a sequence variable in the schema.
*values – Values that the variable can take.
- class pyroed.constraints.AllDifferent(*names: str)[source]¶
Bases:
pyroed.constraints.Constraint
Constrains a set of sites to all have distinct values.
Example:
CONSTRAINTS.append(AllDifferent("aa1", "aa2", "aa3"))
- Parameters
*names (str) – Names of sequence variables that should be distinct.
- class pyroed.constraints.Not(arg: pyroed.constraints.Constraint)[source]¶
Bases:
pyroed.constraints.Constraint
Negates a constraints.
Example:
CONSTRAINTS.append(Not(TakesValue("aa1", "P")))
- Parameters
arg (Constraint) – A constraint.
- class pyroed.constraints.And(lhs: pyroed.constraints.Constraint, rhs: pyroed.constraints.Constraint)[source]¶
Bases:
pyroed.constraints.Constraint
Conjoins two constraints.
Example:
CONSTRAINTS.append(And(TakesValue("aa1", None), TakesValue("aa2", None)))
- Parameters
lhs (Constraint) – A constraint.
rhs (Constraint) – A constraint.
- class pyroed.constraints.Or(lhs: pyroed.constraints.Constraint, rhs: pyroed.constraints.Constraint)[source]¶
Bases:
pyroed.constraints.Constraint
Disjoins two constraints.
Example:
CONSTRAINTS.append(Or(TakesValue("aa1", None), TakesValue("aa2", None)))
- Parameters
lhs (Constraint) – A constraint.
rhs (Constraint) – A constraint.
- class pyroed.constraints.Xor(lhs: pyroed.constraints.Constraint, rhs: pyroed.constraints.Constraint)[source]¶
Bases:
pyroed.constraints.Constraint
Exclusive or among constraints. Equivalent to
Not(Iff(lhs, rhs))
.Example:
CONSTRAINTS.append(Xor(TakesValue("aa1", None), TakesValue("aa2", None)))
- Parameters
lhs (Constraint) – A constraint.
rhs (Constraint) – A constraint.
- class pyroed.constraints.IfThen(lhs: pyroed.constraints.Constraint, rhs: pyroed.constraints.Constraint)[source]¶
Bases:
pyroed.constraints.Constraint
Conditional between constraints.
Example:
CONSTRAINTS.append(IfThen(TakesValue("aa1", None), TakesValue("aa2", None)))
- Parameters
lhs (Constraint) – A constraint.
rhs (Constraint) – A constraint.
- class pyroed.constraints.Iff(lhs: pyroed.constraints.Constraint, rhs: pyroed.constraints.Constraint)[source]¶
Bases:
pyroed.constraints.Constraint
Equality among constraints.
Exmaple:
CONSTRAINTS.append(Iff(TakesValue("aa1", None), TakesValue("aa2", None)))
- Parameters
lhs (Constraint) – A constraint.
rhs (Constraint) – A constraint.