Essential classes#
Summary#
Here are data structures essential for Puan toolkit.
Data types#
variableThe
variableclass is a central key in all Puan packages. It consists of an id,Boundsand data type (dtype).
Proposition#
- class Proposition#
Bases:
object- assume(fixed: Dict[str, int]) Proposition#
Assumes something about variable’s bounds and returns a new proposition with these new bounds set. Other variables, not declared in new_variable_bounds, may also get new bounds as a consequence from the ones set in new_variable_bounds.
- Parameters
- new_variable_boundstyping.Dict[str, Union[int, Tuple[int, int], puan.Bounds]]
A dict of ids and either
int,tupleorpuan.Boundsas bounds for the variable.
- Returns
- outProposition
- static from_json(data, class_map) Any#
Convert from JSON into class object. Class map is a list of other classes maybe related to this class
- to_json() Dict[str, Any]#
JSON representation of class
- to_short() Tuple[str, int, object, int, Tuple[int, int]]#
Short data type has (id, sign, propositions, value, bounds)
variable#
- class variable(id: str, bounds: Optional[Union[int, Tuple[int, int], Bounds]] = None, dtype: Optional[Dtype] = None)#
Bases:
PropositionThe
variableclass is a central key in all Puan packages. It consists of an id,Boundsand data type (dtype).- Raises
- ValueError
If
dtypeis bool and bounds are not (0, 1)
Methods
to_json()Converts into JSON data type.
to_short()Converts into short data type.
Default support vector variable settings.
from_strings(*variables[, default_bounds])Returns a list of
variablefrom a list of strings (ids)from_mixed(*variables[, default_bounds])Returns a list of
variablefrom a list of mixed data type.from_json(data[, class_map])Creates
variablefrom json format.to_dict
- assume(fixed: Dict[str, Union[int, Tuple[int, int], Bounds]]) Proposition#
Assumes something about variable’s bounds and returns a new proposition with these new bounds set. Other variables, not declared in new_variable_bounds, may also get new bounds as a consequence from the ones set in new_variable_bounds.
- Parameters
- new_variable_boundstyping.Dict[str, Union[int, Tuple[int, int], puan.Bounds]]
A dict of ids and either
int,tupleorpuan.Boundsas bounds for the variable.
- Returns
- outProposition
- bounds: puan.Bounds#
- evaluate(interpretation: Dict[str, Union[int, Tuple[int, int], Bounds]]) Bounds#
Evaluates
interpretationon this proposition.- Parameters
- interpretationDict[str, Union[Bounds, Tuple[int,int], int]]
the values of the variables in the model to evaluate it for
- Returns
- outOptional[int]
Examples
>>> variable("a", bounds=(0,1)).evaluate({"x": 1}) Bounds(lower=0, upper=1)
>>> variable("a", bounds=(0,2)).evaluate({"a": 2}) Bounds(lower=2, upper=2)
>>> variable("a", bounds=(0,2)).evaluate({"a": (2,3)}) Bounds(lower=2, upper=3)
>>> variable("a", bounds=(0,2)).evaluate({"a": Bounds(1,1)}) Bounds(lower=1, upper=1)
>>> variable("a", bounds=(-1,0)).evaluate({"a": 1}) Bounds(lower=1, upper=1)
>>> variable("a", bounds=(1,1)).evaluate({"a": 0}) Bounds(lower=0, upper=0)
>>> variable("a", bounds=(1,1)).evaluate({"x": 1}) Bounds(lower=1, upper=1)
- evaluate_propositions(interpretation: ~typing.Dict[str, ~typing.Union[int, ~typing.Tuple[int, int], ~puan.Bounds]], out: ~typing.Callable[[~puan.Bounds], ~typing.Union[~puan.Bounds, ~typing.Tuple[int, int], int]] = <function variable.<lambda>>) Dict[str, Bounds]#
Evaluates
interpretationon this proposition.- Parameters
- interpretationDict[Union[str,
variable], int] the values of the variables in the model to evaluate it for
- outCallback[[Bounds], Union[Bounds, Tuple[int,int], int]]
an optional callback function for changing output data type.
- interpretationDict[Union[str,
- Returns
- outOptional[int]
Examples
>>> variable("a", bounds=(0,1)).evaluate_propositions({"x": 1}) {'a': Bounds(lower=0, upper=1)}
>>> variable("a", bounds=(0,1)).evaluate_propositions({"a": (1,1)}) {'a': Bounds(lower=1, upper=1)}
>>> variable("a", bounds=(0,1)).evaluate_propositions({"a": Bounds(1,1)}) {'a': Bounds(lower=1, upper=1)}
>>> variable("a", bounds=(0,2)).evaluate_propositions({"a": 2}) {'a': Bounds(lower=2, upper=2)}
>>> variable("a", bounds=(-1,0)).evaluate_propositions({"a": 1}) {'a': Bounds(lower=1, upper=1)}
>>> variable("a", bounds=(1,1)).evaluate_propositions({"a": 0}) {'a': Bounds(lower=0, upper=0)}
>>> variable("a", bounds=(1,1)).evaluate_propositions({"x": 1}) {'a': Bounds(lower=1, upper=1)}
- flatten() List[Proposition]#
Returns all its propositions and their sub propositions as a unique list of propositions.
- Returns
- outList[Proposition]
Examples
>>> variable("a", bounds=(0,2)).flatten() [variable(id='a', bounds=Bounds(lower=0, upper=2))]
- static from_json(data: Dict[str, Any], class_map: List[Type] = []) variable#
Creates
variablefrom json format.Notes
Bounds are defaulted to
Bounds(lower=0, upper=1)if not provided.class_mapis ignored.Examples
>>> variable.from_json({'id': 'x'}) variable(id='x', bounds=Bounds(lower=0, upper=1))
>>> variable.from_json({'id': 'x', 'bounds': {'lower': -2, 'upper': 3}}) variable(id='x', bounds=Bounds(lower=-2, upper=3))
- static from_mixed(*variables: List[Union[str, int, tuple, list, variable]], default_bounds: Tuple[int, int] = (0, 1)) List[variable]#
Returns a list of
variablefrom a list of mixed data type.- Parameters
- variablesA list of either str, int, tuple, list or
variable - default_boundsTuple of ints (lower bound, upper bound)
- variablesA list of either str, int, tuple, list or
- Returns
- outList[
variable]
- outList[
Notes
Every item x in
*variablesthat is not an instance ofvariablewill be converted to avariablewith id set to x and default bounds.List of variables are returned sorted.
Examples
>>> variable.from_mixed("a",4,("b","c"),variable("x",(1,2))) [variable(id="('b', 'c')", bounds=Bounds(lower=0, upper=1)), variable(id='4', bounds=Bounds(lower=0, upper=1)), variable(id='a', bounds=Bounds(lower=0, upper=1)), variable(id='x', bounds=Bounds(lower=1, upper=2))]
>>> variable.from_mixed("a",4,variable("x",(2,4)), default_bounds=(-1, 1)) [variable(id='4', bounds=Bounds(lower=-1, upper=1)), variable(id='a', bounds=Bounds(lower=-1, upper=1)), variable(id='x', bounds=Bounds(lower=2, upper=4))]
- static from_strings(*variables: List[str], default_bounds: Tuple[int, int] = (0, 1)) List[variable]#
Returns a list of
variablefrom a list of strings (ids)- Parameters
- variablesA list of strings (ids)
- default_boundsTuple of ints (lower bound, upper bound)
- Returns
- outList[
variable]
- outList[
Notes
List of variables are returned sorted
Examples
>>> variable.from_strings("a","b") [variable(id='a', bounds=Bounds(lower=0, upper=1)), variable(id='b', bounds=Bounds(lower=0, upper=1))]
>>> variable.from_strings("a","b", default_bounds=(-1,10)) [variable(id='a', bounds=Bounds(lower=-1, upper=10)), variable(id='b', bounds=Bounds(lower=-1, upper=10))]
- id: str#
- static support_vector_variable() variable#
Default support vector variable settings.
- Returns
- out
variable
- out
Examples
>>> variable.support_vector_variable() variable(id=0, bounds=Bounds(lower=1, upper=1))
- to_json() Dict[str, Any]#
Converts into JSON data type.
Examples
>>> variable(id="x", bounds=Bounds(lower=0, upper=10)).to_json() {'id': 'x', 'bounds': {'lower': 0, 'upper': 10}}
- to_short() Tuple[str, int, List, int, Tuple[int, int]]#
Converts into short data type.
Examples
>>> variable(id="x", bounds=Bounds(lower=0, upper=10)).to_short() ('x', 1, [], 0, (0, 10))
Bounds#
- class Bounds(lower: int, upper: int)#
Bases:
objectThe
Boundsclass defines lower and upper bounds of avariable- Raises
- ValueError
If
loweris greater thanupper
Methods
as_tuple()Bounds as tuple
- as_tuple() Tuple[int, int]#
Bounds as tuple
- Returns
- outTuple[int, int]
- property constant: Optional[int]#
If lower and upper bounds are the same, that value is returned. Else None is returned.
- Returns
- outOptional[int]
Examples
>>> Bounds(0, 1).constant
>>> Bounds(-2, -2).constant -2