core
Introduction of core module¶
jijmodeling_transpiler.core provides core method for jijmodeling_transpiler. This module corresponds Engine layer of JijZept layer diagram. The layer is constructed three parts:
- compile
- transpile (encode)
- decode, evaluate
1. Compile¶
In compile process, an instance data is substituted to Placeholders of a user defined model. compile_model function provides that projection in JijModelingTranspiler.
Example¶
import jijmodeling as jm
import jijmodeling_transpiler as jmt
n = jm.Placeholder("n")
x = jm.Binary("x", (n, n))
i = jm.Element("i", n)
problem = jm.Problem("sample")
problem += x[:, :]
problem += jm.Constraint("onehot", x[:, i], forall=i)
compiled_instance = jmt.core.compile_model(problem, {"n": 2}, {})
compiled_instance
The user can directly manipulate this CompiledInstance. Please refer to the CompiledInstance class reference, SubstitutedExpression class reference, and the VariableMap class for the correspondence between each variable and the integer index.
2. Transpile (encode)¶
This process have to seperate from core module because the process depends each solver. However currently the process include in core module.
3. Decode, Evaluate¶
For which solver did the decoding process transpile in the transpile process? how did you encode it? depends on. Therefore, the builder object output by the Transpile process is also required for the decoding process.
CompiledInstance dataclass ¶
CompiledInstance: object return compile_model method.
Attributes:
| Name | Type | Description |
|---|---|---|
sense | ProblemSense | problem sense minimize or maximize. |
objective | SubstitutedExpression | objective expression. |
constraint | dict[str, dict[tuple[int, ...], SubstitutedExpression]] | constraints. str key represents name of constraint. tuple[int,...] is values of forall index. |
penalty | dict[str, dict[tuple[int, ...], SubstitutedExpression]] | dict[str, dict[tuple[int, ...], SubstitutedExpression]] |
var_map | VariableMap | VariableMap |
data | InstanceData | InstanceData |
problem | Problem | jijmodeling.InstanceData |
Examples:
import jijmodeling as jm
import jijmodeling_transpiler as jmt
n = jm.Placeholder("n")
x = jm.Binary("x", (n, n))
i = jm.Element("i", n)
problem = jm.Problem("sample")
problem += x[:, :]
problem += jm.Constraint("onehot", x[:, i], forall=i)
compiled_instance = jmt.core.compile_model(problem, {"n": 2}, {})
compiled_instance
CompiledInstance(
objective=SubstitutedExpression(
linear=LinearSubstitutedExpr(coeff={0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0}, constant=0.0),
nonlinear=None),
constraint={
'onehot': {
(0,): SubstitutedExpression(
linear=LinearSubstitutedExpr(coeff={0: 1.0, 1: 1.0}, constant=0.0),
nonlinear=None),
(1,): SubstitutedExpression(
linear=LinearSubstitutedExpr(coeff={2: 1.0, 3: 1.0}, constant=0.0),
nonlinear=None)}
},
penalty={},
var_map=VariableMap(var_map={'x': {(0, 0): 0, (0, 1): 2, (1, 0): 1, (1, 1): 3}},
var_num=4,
integer_bound={}),
...
)
Source code in jijmodeling_transpiler/core/compile/compiled_model.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
InstanceData ¶
Instance Data class The constructor provides type validation and conversion from user's input to valid instance data type.
Attributes:
| Name | Type | Description |
|---|---|---|
tensor_data | dict[str, ndarray] | tensor value of Placheolder. The key is uuid of the corresponding Placheolder. |
jagged_data | dict[str, list] | jagged array value of JaggedArray. The key is uuid of the corresponding JaggedArray. |
fixed_variables | dict[str, dict[tuple[int, ...], float]] | fixed variable values. |
indices | dict[str, int] | value of Element. The attribute is changing in compile process. |
Source code in jijmodeling_transpiler/core/instance_data.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
__init__(instance_data, fixed_vars, ph_names, indices=None) ¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance_data | dict | user input instance_data. | required |
fixed_vars | dict | user input fixed_variables. | required |
ph_vars | list[name] | Placeholder list include user defined model. This is used for validation. | required |
indices | Optional[dict] | value of each Elements. Defaults to None. | None |
Raises:
| Type | Description |
|---|---|
ValueError | user's input is invalid. |
Source code in jijmodeling_transpiler/core/instance_data.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
IntegerBound dataclass ¶
IntegerBound
Attributes:
| Name | Type | Description |
|---|---|---|
lower | int | float | lower bound of integer decision variable |
upper | int | float | upper bound of integer decision variable |
Source code in jijmodeling_transpiler/core/variable_map.py
5 6 7 8 9 10 11 12 13 14 15 16 | |
VariableMap dataclass ¶
VarialeMap
This class is used to manage the mapping between the label and the index of the decision variable.
Attributes:
| Name | Type | Description |
|---|---|---|
var_map | dict[str, dict[tuple[int, ...], int]] | label, subscripts -> index (int) |
var_num | int | number of variables |
integer_bound | dict[str, dict[tuple[int, ...], IntegerBound]] | bounds of integer decision variables. |
Source code in jijmodeling_transpiler/core/variable_map.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
add_int_variable(label, subscripts, lower, upper) ¶
Add an integer variable to the variable map.
Source code in jijmodeling_transpiler/core/variable_map.py
56 57 58 59 60 61 62 | |
add_variable(label, subscripts) ¶
Add a variable to the variable map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label | str | label of the variable | required |
subscripts | tuple[int, ...] | subscripts of the variable | required |
Returns:
| Name | Type | Description |
|---|---|---|
int | int | index of the variable in the variable map |
Source code in jijmodeling_transpiler/core/variable_map.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
to_serializable() ¶
Convert to a serializable object.
Returns:
| Name | Type | Description |
|---|---|---|
dict | dict | serializable object |
Examples:
varmap = VariableMap()
varmap.add_variable("x", (0, 0))
varmap.add_variable("x", (0, 1))
varmap.to_serializable()
# {
# "class": "VariableMap",
# "var_map": {
# "x": {
# "subscripts": [
# [0, 0],
# [0, 1]
# ],
# "index": [0, 1]
# }
# },
# "var_num": 2,
# "integer_bound": {}
# }
Source code in jijmodeling_transpiler/core/variable_map.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
compile_model(problem, instance, fixed_variables=None) ¶
Compile a problem and an instance into a compiled model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
problem | Problem | a problem to be compiled | required |
instance | dict[str, ndarray | list | int | float] | an instance to be compiled | required |
fixed_variables | dict[dict, dict[tuple[int, ...], float]] | fixed variables. Defaults to None. | None |
Returns:
| Name | Type | Description |
|---|---|---|
CompiledInstance | CompiledInstance | a compiled model |
Source code in jijmodeling_transpiler/core/compile/compile.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |