jijmodeling 1.11.0__cp39-cp39-macosx_10_16_x86_64.whl → 1.12.1__cp39-cp39-macosx_10_16_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of jijmodeling might be problematic. Click here for more details.
- jijmodeling/__init__.pyi +2 -2
- jijmodeling/_jijmodeling.cpython-39-darwin.so +0 -0
- jijmodeling-1.12.1.dist-info/METADATA +54 -0
- {jijmodeling-1.11.0.dist-info → jijmodeling-1.12.1.dist-info}/RECORD +6 -6
- {jijmodeling-1.11.0.dist-info → jijmodeling-1.12.1.dist-info}/WHEEL +1 -1
- jijmodeling-1.11.0.dist-info/METADATA +0 -235
- {jijmodeling-1.11.0.dist-info → jijmodeling-1.12.1.dist-info}/licenses/LICENSE.txt +0 -0
jijmodeling/__init__.pyi
CHANGED
|
@@ -1707,7 +1707,7 @@ class Interpreter:
|
|
|
1707
1707
|
def eval_constraints(self, constraints:Constraint) -> builtins.list[typing.Any]:
|
|
1708
1708
|
...
|
|
1709
1709
|
|
|
1710
|
-
def eval_problem(self, problem:Problem, hints:typing.Optional[typing.Sequence[typing.Literal['OneHot']]]=None) -> typing.Any:
|
|
1710
|
+
def eval_problem(self, problem:Problem, hints:typing.Optional[typing.Sequence[typing.Literal['OneHot', 'SOS1']]]=None) -> typing.Any:
|
|
1711
1711
|
r"""
|
|
1712
1712
|
Evaluate the problem and returns `ommx.v1.Instance`.
|
|
1713
1713
|
|
|
@@ -3180,7 +3180,7 @@ class Problem:
|
|
|
3180
3180
|
"""
|
|
3181
3181
|
...
|
|
3182
3182
|
|
|
3183
|
-
def generate_random_instance(self, default:typing.Mapping[builtins.str, builtins.range | tuple[typing.Optional[builtins.int], typing.Optional[builtins.int]] | builtins.int | None | builtins.dict[builtins.str, builtins.dict[builtins.str, builtins.int] | typing.Literal['Unbounded']] | tuple[typing.Mapping[builtins.str, builtins.int] | typing.Literal['Unbounded'], typing.Mapping[builtins.str, builtins.int] | typing.Literal['Unbounded']] | builtins.range | tuple[typing.Optional[builtins.float], typing.Optional[builtins.float]] | builtins.int | builtins.float | None | builtins.dict[builtins.str, builtins.dict[builtins.str, builtins.float] | typing.Literal['Unbounded']] | tuple[builtins.dict[builtins.str, builtins.float] | typing.Literal['Unbounded'], builtins.dict[builtins.str, builtins.float] | typing.Literal['Unbounded']]]={'size': {'start': {'Included': 1}, 'end': {'Included': 5}}, 'value': {'start': {'Included': -1.0}, 'end': {'Included': 1.0}}}, options:typing.Mapping[builtins.str, typing.Mapping[builtins.str, builtins.range | tuple[typing.Optional[builtins.int], typing.Optional[builtins.int]] | builtins.int | None | builtins.dict[builtins.str, builtins.dict[builtins.str, builtins.int] | typing.Literal['Unbounded']] | tuple[typing.Mapping[builtins.str, builtins.int] | typing.Literal['Unbounded'], typing.Mapping[builtins.str, builtins.int] | typing.Literal['Unbounded']] | builtins.range | tuple[typing.Optional[builtins.float], typing.Optional[builtins.float]] | builtins.int | builtins.float | None | builtins.dict[builtins.str, builtins.dict[builtins.str, builtins.float] | typing.Literal['Unbounded']] | tuple[builtins.dict[builtins.str, builtins.float] | typing.Literal['Unbounded'], builtins.dict[builtins.str, builtins.float] | typing.Literal['Unbounded']]]]={}, seed:typing.Optional[builtins.int]=None, hints:typing.Optional[typing.Sequence[typing.Literal['OneHot']]]=None) -> typing.Any:
|
|
3183
|
+
def generate_random_instance(self, default:typing.Mapping[builtins.str, builtins.range | tuple[typing.Optional[builtins.int], typing.Optional[builtins.int]] | builtins.int | None | builtins.dict[builtins.str, builtins.dict[builtins.str, builtins.int] | typing.Literal['Unbounded']] | tuple[typing.Mapping[builtins.str, builtins.int] | typing.Literal['Unbounded'], typing.Mapping[builtins.str, builtins.int] | typing.Literal['Unbounded']] | builtins.range | tuple[typing.Optional[builtins.float], typing.Optional[builtins.float]] | builtins.int | builtins.float | None | builtins.dict[builtins.str, builtins.dict[builtins.str, builtins.float] | typing.Literal['Unbounded']] | tuple[builtins.dict[builtins.str, builtins.float] | typing.Literal['Unbounded'], builtins.dict[builtins.str, builtins.float] | typing.Literal['Unbounded']]]={'size': {'start': {'Included': 1}, 'end': {'Included': 5}}, 'value': {'start': {'Included': -1.0}, 'end': {'Included': 1.0}}}, options:typing.Mapping[builtins.str, typing.Mapping[builtins.str, builtins.range | tuple[typing.Optional[builtins.int], typing.Optional[builtins.int]] | builtins.int | None | builtins.dict[builtins.str, builtins.dict[builtins.str, builtins.int] | typing.Literal['Unbounded']] | tuple[typing.Mapping[builtins.str, builtins.int] | typing.Literal['Unbounded'], typing.Mapping[builtins.str, builtins.int] | typing.Literal['Unbounded']] | builtins.range | tuple[typing.Optional[builtins.float], typing.Optional[builtins.float]] | builtins.int | builtins.float | None | builtins.dict[builtins.str, builtins.dict[builtins.str, builtins.float] | typing.Literal['Unbounded']] | tuple[builtins.dict[builtins.str, builtins.float] | typing.Literal['Unbounded'], builtins.dict[builtins.str, builtins.float] | typing.Literal['Unbounded']]]]={}, seed:typing.Optional[builtins.int]=None, hints:typing.Optional[typing.Sequence[typing.Literal['OneHot', 'SOS1']]]=None) -> typing.Any:
|
|
3184
3184
|
r"""
|
|
3185
3185
|
Generates random `ommx.v1.Instance` for a given problem.
|
|
3186
3186
|
See also `InstanceDataValue.generate_random_dataset`.
|
|
Binary file
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: jijmodeling
|
|
3
|
+
Version: 1.12.1
|
|
4
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
5
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
6
|
+
Classifier: Operating System :: MacOS
|
|
7
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
8
|
+
Classifier: Programming Language :: Rust
|
|
9
|
+
Classifier: Programming Language :: Python
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
17
|
+
Classifier: License :: Other/Proprietary License
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
19
|
+
Requires-Dist: numpy
|
|
20
|
+
Requires-Dist: pandas
|
|
21
|
+
Requires-Dist: orjson>=3.8.0,<4.0.0
|
|
22
|
+
Requires-Dist: ommx>=1.8.1,<2.0.0
|
|
23
|
+
Requires-Dist: pyright ; extra == 'test'
|
|
24
|
+
Requires-Dist: pytest ; extra == 'test'
|
|
25
|
+
Requires-Dist: hypothesis ; extra == 'test'
|
|
26
|
+
Requires-Dist: twine ; extra == 'test'
|
|
27
|
+
Requires-Dist: pkginfo>=1.10.0 ; extra == 'test'
|
|
28
|
+
Requires-Dist: pytest-snapshot>=0.9.0 ; extra == 'test'
|
|
29
|
+
Provides-Extra: test
|
|
30
|
+
License-File: LICENSE.txt
|
|
31
|
+
Summary: Mathematical modeling tool for optimization problem
|
|
32
|
+
Author: Jij Inc.
|
|
33
|
+
Author-email: info@j-ij.com
|
|
34
|
+
Requires-Python: >=3.9, <3.13
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
Project-URL: Homepage, https://www.jijzept.com
|
|
37
|
+
|
|
38
|
+
# JijModeling
|
|
39
|
+
|
|
40
|
+
[](https://pypi.python.org/pypi/jijmodeling/)
|
|
41
|
+
[](https://pypi.python.org/pypi/jijmodeling/)
|
|
42
|
+
[](https://pypi.python.org/pypi/jijmodeling/)
|
|
43
|
+
[](https://pypi.python.org/pypi/jijmodeling/)
|
|
44
|
+
[](https://pypi.python.org/pypi/jijmodeling/)
|
|
45
|
+
[](https://pypi.python.org/pypi/jijmodeling/)
|
|
46
|
+
|
|
47
|
+
## Documentation / ドキュメント
|
|
48
|
+
|
|
49
|
+
For detailed documentation and tutorials, please visit:
|
|
50
|
+
詳細なドキュメントとチュートリアルは以下をご覧ください:
|
|
51
|
+
|
|
52
|
+
- English documentation: [https://jij-inc.github.io/JijModeling-Tutorials/en/introduction.html](https://jij-inc.github.io/JijModeling-Tutorials/en/introduction.html)
|
|
53
|
+
- 日本語ドキュメント: [https://jij-inc.github.io/JijModeling-Tutorials/ja/introduction.html](https://jij-inc.github.io/JijModeling-Tutorials/ja/introduction.html)
|
|
54
|
+
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
jijmodeling-1.
|
|
2
|
-
jijmodeling-1.
|
|
3
|
-
jijmodeling-1.
|
|
1
|
+
jijmodeling-1.12.1.dist-info/METADATA,sha256=lUsoJbiQ_qCPdTAxsDh0hK1dI-GEZ9kMPM7UrrROHzY,2717
|
|
2
|
+
jijmodeling-1.12.1.dist-info/WHEEL,sha256=LfQ04RLA0eG-yO_k8yg3YEwWU7RkmCq7foLzBJwf0E4,104
|
|
3
|
+
jijmodeling-1.12.1.dist-info/licenses/LICENSE.txt,sha256=T5HdEbP5NWG8fZbvF9pofeteW3HrS30V3_LvtPUHFYM,3400
|
|
4
4
|
jijmodeling/experimental.py,sha256=Of-tUY3kfpFHpjGbIcclbMenCP2BMW07me895YH7tG8,575
|
|
5
|
-
jijmodeling/__init__.pyi,sha256=
|
|
5
|
+
jijmodeling/__init__.pyi,sha256=IAPx1c3sQiRa2EAgqO34ka75lL1JwRxtolTdPDlVzow,154984
|
|
6
6
|
jijmodeling/__init__.py,sha256=43DV_WzDxS0n6FseNyfJZ6oQucO31T4qqbz3bx9JMtE,44
|
|
7
7
|
jijmodeling/dataset.pyi,sha256=3kKSch5XR16pEZA0c_Xr_J9Ai_IAv3aTqExR7iCd-pk,4083
|
|
8
8
|
jijmodeling/experimental.pyi,sha256=lBo4LxYFm-C4m2oC6XVYkBON6EVbbZjWl2NmMziPhbw,11275
|
|
@@ -14,5 +14,5 @@ jijmodeling/range/size.py,sha256=gBwFfLuO4GprRfjVWyCplEvFJri8cxPRtuEd5uXzo7Q,528
|
|
|
14
14
|
jijmodeling/range/size.pyi,sha256=qV7OxCU_sHBVrH7Vyf1tOpClQphMlPD3ZzBDLVacRWI,2830
|
|
15
15
|
jijmodeling/range/value.py,sha256=aRwQGzpH5MZgpgKvd8gxMkMRyloq0S5zv87Kv3PHSd4,532
|
|
16
16
|
jijmodeling/range/value.pyi,sha256=rm6hAOACwTUYPNlYOLAmqMKoT7c2XQUMH6HyzvhbVjc,2870
|
|
17
|
-
jijmodeling/_jijmodeling.cpython-39-darwin.so,sha256=
|
|
18
|
-
jijmodeling-1.
|
|
17
|
+
jijmodeling/_jijmodeling.cpython-39-darwin.so,sha256=AUMOAqfAMyllvEmlgoEFn6Xya9IqfzUO8vyfaZdXCEc,9325052
|
|
18
|
+
jijmodeling-1.12.1.dist-info/RECORD,,
|
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: jijmodeling
|
|
3
|
-
Version: 1.11.0
|
|
4
|
-
Classifier: Development Status :: 5 - Production/Stable
|
|
5
|
-
Classifier: Operating System :: POSIX :: Linux
|
|
6
|
-
Classifier: Operating System :: MacOS
|
|
7
|
-
Classifier: Operating System :: Microsoft :: Windows
|
|
8
|
-
Classifier: Programming Language :: Rust
|
|
9
|
-
Classifier: Programming Language :: Python
|
|
10
|
-
Classifier: Programming Language :: Python :: 3
|
|
11
|
-
Classifier: Programming Language :: Python :: 3 :: Only
|
|
12
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
-
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
17
|
-
Classifier: License :: Other/Proprietary License
|
|
18
|
-
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
19
|
-
Requires-Dist: numpy
|
|
20
|
-
Requires-Dist: pandas
|
|
21
|
-
Requires-Dist: orjson >=3.8.0, <4.0.0
|
|
22
|
-
Requires-Dist: ommx >=1.6.0, <2.0.0
|
|
23
|
-
Requires-Dist: pyright ; extra == 'test'
|
|
24
|
-
Requires-Dist: pytest ; extra == 'test'
|
|
25
|
-
Requires-Dist: hypothesis ; extra == 'test'
|
|
26
|
-
Requires-Dist: twine ; extra == 'test'
|
|
27
|
-
Requires-Dist: pkginfo >=1.10.0 ; extra == 'test'
|
|
28
|
-
Provides-Extra: test
|
|
29
|
-
License-File: LICENSE.txt
|
|
30
|
-
Summary: Mathematical modeling tool for optimization problem
|
|
31
|
-
Author: Jij Inc.
|
|
32
|
-
Author-email: info@j-ij.com
|
|
33
|
-
Requires-Python: >=3.9
|
|
34
|
-
Description-Content-Type: text/markdown
|
|
35
|
-
Project-URL: Homepage, https://www.jijzept.com
|
|
36
|
-
|
|
37
|
-
# JijModeling
|
|
38
|
-
|
|
39
|
-
[](https://pypi.python.org/pypi/jijmodeling/)
|
|
40
|
-
[](https://pypi.python.org/pypi/jijmodeling/)
|
|
41
|
-
[](https://pypi.python.org/pypi/jijmodeling/)
|
|
42
|
-
[](https://pypi.python.org/pypi/jijmodeling/)
|
|
43
|
-
[](https://pypi.python.org/pypi/jijmodeling/)
|
|
44
|
-
[](https://pypi.python.org/pypi/jijmodeling/)
|
|
45
|
-
|
|
46
|
-
JijModeling is a Python package designed to intuitively write mathematical optimization models. Its main features are:
|
|
47
|
-
|
|
48
|
-
- Allows writing models without specific instance data, leading to quicker verification of the model and easy reuse. Instance size does not affect the performance of writing and manipulating the model.
|
|
49
|
-
- Works as a common interface for different kinds of optimization problems, including Linear Programming, Mixed Integer Programming, or Non-Linear Programming, and so on.
|
|
50
|
-
- Models can be manipulated programmatically, allowing building up a model piece-by-piece and more complex logic in constraint construction.
|
|
51
|
-
- Supports outputting to LaTeX. Mixed with Jupyter notebooks, this allows quick interactive feedback to check that your model fits what you expect.
|
|
52
|
-
|
|
53
|
-
JijModeling is a tool for *writing* models using Python code, it does not evaluate or solve the models. The main motivation is to focus on the algebraic structure of the models, so that they can be reasoned about, verified and more nimbly altered regardless of the instance data, while still serving as a step towards generating the input formats the solvers expect.
|
|
54
|
-
|
|
55
|
-
To be used with solvers, it needs to be paired with the actual instance data and converted into the specific solver’s input format by another tool, such as our internal [JijZept services](https://www.jijzept.com), or with the freely available [jijmodeling-transpiler](https://www.documentation.jijzept.com/docs/jijmodelingtranspiler).
|
|
56
|
-
|
|
57
|
-
## Installation
|
|
58
|
-
|
|
59
|
-
If you use `pip`, you can install JijModeling with:
|
|
60
|
-
|
|
61
|
-
```bash markdown-code-runner
|
|
62
|
-
pip install jijmodeling
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
Note that JijModeling requires Python 3.9 or higher.
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
## Quickstart example
|
|
69
|
-
|
|
70
|
-
To get a better idea of how to use jijmodeling, we’ll go over a basic example, starting with writing a simple model, converting it, then executing it with a solver. For the first two sections, jijmodeling is all you need, but we recommend using a [Jupyter notebook](https://jupyter.org) to easily check LaTeX output.
|
|
71
|
-
|
|
72
|
-
For the third section, we will be using [JijModelingTranspiler](https://www.documentation.jijzept.com/docs/jijmodelingtranspiler) and [OpenJij](https://openjij.github.io/OpenJij/index.html). Note that jijmodeling models are also usable with our paid JijZept services. JijMdelingTranspiler and OpenJij can be installed as usual through `pip`:
|
|
73
|
-
|
|
74
|
-
```bash markdown-code-runner
|
|
75
|
-
pip install jijmodeling-transpiler openjij
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
### Overview
|
|
79
|
-
|
|
80
|
-
Working with JijModeling should feel natural to anyone familiar with mathematical optimization models.
|
|
81
|
-
|
|
82
|
-
We represent various mathematical expressions by combining various classes, like `BinaryVar` and `Placeholder` using basic mathematical operations. `___Var` classes refer to different kinds of decision variables. `Placeholder` represents just about any kind of constant or value to be specified later. That is, they’re something we want to abstract away from the model and mark as part of instance-specific data. You can also use number literals, of course.
|
|
83
|
-
|
|
84
|
-
```python markdown-code-runner
|
|
85
|
-
import jijmodeling as jm
|
|
86
|
-
|
|
87
|
-
x = jm.BinaryVar("x")
|
|
88
|
-
y = jm.IntegerVar("y", lower_bound=1, upper_bound=10)
|
|
89
|
-
n = jm.Placeholder("n")
|
|
90
|
-
exp = x * (y ** 2) * n
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
The placeholder and variables above are zero-dimensional (scalars), but we can also represent arrays/multi-dimensional variables and constants using these classes, as we’ll see later on.
|
|
94
|
-
|
|
95
|
-
In JijModeling, we build up a model by adding expressions like this to a `Problem` object, representing our model as a whole. Constraints are defined by the `Constraint` class wrapping around some comparison expression. (Note that only `<=`, `==`, and `>=` are supported in `Constraint`s).
|
|
96
|
-
|
|
97
|
-
```python markdown-code-runner
|
|
98
|
-
a = jm.IntegerVar("a", lower_bound=5, upper_bound=20)
|
|
99
|
-
b = jm.IntegerVar("b", lower_bound=1, upper_bound=20)
|
|
100
|
-
c = jm.IntegerVar("c", lower_bound=20, upper_bound=30)
|
|
101
|
-
n = jm.Placeholder("n")
|
|
102
|
-
|
|
103
|
-
problem = jm.Problem("my problem")
|
|
104
|
-
problem += n * (a + b + c)
|
|
105
|
-
problem += jm.Constraint("c1", 2 * (b + c) <= 75)
|
|
106
|
-
problem += jm.Constraint("c2", a + b <= 40)
|
|
107
|
-
problem
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
The above should output a model like this: (in a Jupyter notebook you’ll also see a description of the decision variables, omitted here for brevity)
|
|
111
|
-
|
|
112
|
-
$$
|
|
113
|
-
\begin{array}{cccc}\text{Problem:} & \text{my problem} & & \\
|
|
114
|
-
& & \min \quad \displaystyle n \cdot \left(a + b + c\right) & \\
|
|
115
|
-
\text{{s.t.}} & & & \\
|
|
116
|
-
& \text{c1} & \displaystyle 2 \cdot \left(b + c\right) \leq 75 & \\
|
|
117
|
-
& \text{c2} & \displaystyle a + b \leq 40 \\
|
|
118
|
-
\end{array}
|
|
119
|
-
$$
|
|
120
|
-
|
|
121
|
-
### Writing a model
|
|
122
|
-
|
|
123
|
-
Let’s see how we would model a generic [Knapsack problem](https://www.documentation.jijzept.com/docs/tutorial/knapsack).
|
|
124
|
-
|
|
125
|
-
In this problem, we have $N$ valuable items. We want to take as many items as possible, but we can only carry so much with us, determined by a weight limit $W$. We’ll be representing the decision to take some item $i$ with the binary variable $x_{i}$. That item’s weight is $w_i$ and its value $v_i$.
|
|
126
|
-
|
|
127
|
-
Let’s first define these values:
|
|
128
|
-
|
|
129
|
-
```python markdown-code-runner
|
|
130
|
-
W = jm.Placeholder("W") # The maximum weight we can carry
|
|
131
|
-
# The values and weights of each items can represented as
|
|
132
|
-
# one-dimensional Placeholders.
|
|
133
|
-
values = jm.Placeholder("v", ndim=1)
|
|
134
|
-
weights = jm.Placeholder("w", ndim=1)
|
|
135
|
-
# We can define N in relation to the size of our placeholders,
|
|
136
|
-
# so it's calculated automatically based on the instance data.
|
|
137
|
-
# The optional "latex" parameter lets us define how this is
|
|
138
|
-
# displayed in the LaTeX output.
|
|
139
|
-
N = values.len_at(0, latex="N")
|
|
140
|
-
|
|
141
|
-
# we have N decision variables, which we represent with a
|
|
142
|
-
# one-dimensional BinaryVar. We must specify that it has N elements.
|
|
143
|
-
x = jm.BinaryVar("x", shape=(N,))
|
|
144
|
-
|
|
145
|
-
# an index we'll use for summations
|
|
146
|
-
i = jm.Element("i", (0,N))
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
At the end we create an `Element`. These are used as indices. With `jijmodeling.sum`, this allows us to write summations in a style similar to Sigma notation. Here we define that this index goes from 0 (inclusive) to $N$ (non-inclusive). It can feel strange to write these ahead of time, but doing so allows us to reuse it and is more convenient overall.
|
|
150
|
-
|
|
151
|
-
For this problem we want to maximize our total value, while making sure the total weight is within our limit. We can write that like this:
|
|
152
|
-
|
|
153
|
-
```python markdown-code-runner
|
|
154
|
-
problem = jm.Problem("knapsack", sense = jm.ProblemSense.MAXIMIZE)
|
|
155
|
-
problem += jm.sum(i, values[i] * x[i])
|
|
156
|
-
problem += jm.Constraint("weight limit", jm.sum(i, weights[i] * x[i]) <= W)
|
|
157
|
-
problem
|
|
158
|
-
```
|
|
159
|
-
This gives us the model we’d expect:
|
|
160
|
-
|
|
161
|
-
$$
|
|
162
|
-
\begin{array}{cccc}\text{Problem:} & \text{knapsack} & & \\
|
|
163
|
-
& & \max \quad \displaystyle \sum_{i = 0}^{N - 1} v_{i} \cdot x_{i} & \\
|
|
164
|
-
\text{{s.t.}} & & & \\
|
|
165
|
-
& \text{weight limit} & \displaystyle \sum_{i = 0}^{N - 1} w_{i} \cdot x_{i} \leq W & \\
|
|
166
|
-
\text{{where}} & & & \\
|
|
167
|
-
& x & 1\text{-dim binary variable}\\
|
|
168
|
-
\end{array}
|
|
169
|
-
$$
|
|
170
|
-
|
|
171
|
-
As a reminder, JijModeling expressions can be stored in variables like regular python objects. When working with larger problems, complex expressions can be built up from smaller ones, which can be easier to understand and modify later. For a problem this small this is not that useful, but just to illustrate, the previous code snippet could be rewritten like this:
|
|
172
|
-
|
|
173
|
-
```python markdown-code-runner
|
|
174
|
-
chosen_v = values[i] * x[i]
|
|
175
|
-
chosen_w = weights[i] * x[i]
|
|
176
|
-
sum_of_values = jm.sum(i, chosen_v)
|
|
177
|
-
sum_of_weights = jm.sum(i, chosen_w)
|
|
178
|
-
weight_below_limit = sum_of_weights <= W
|
|
179
|
-
|
|
180
|
-
problem = jm.Problem("knapsack", sense = jm.ProblemSense.MAXIMIZE)
|
|
181
|
-
problem += sum_of_values
|
|
182
|
-
problem += jm.Constraint("weight limit", weight_below_limit)
|
|
183
|
-
problem
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
These two models are equivalent. How you write the model is a matter of preference and convenience.
|
|
187
|
-
|
|
188
|
-
### Using the model
|
|
189
|
-
|
|
190
|
-
Now we have our model, though not much can be done with it by itself other than exporting it to LaTeX. We can pair it with instance data to generate the input for an optimization solver. To demonstrate, let’s see how we’d use it with the freely available [JijModelingTranspiler](https://www.documentation.jijzept.com/docs/jijmodelingtranspiler) to convert it, and [OpenJij](https://openjij.github.io/OpenJij/index.html) to solve the model. Note that our tooling provided with [JijZept](https://www.documentation.jijzept.com/docs/jijzept) also accepts JijModeling models as input.
|
|
191
|
-
|
|
192
|
-
This is meant as a quick demonstration, not a full introduction to JijModelingTranspiler and OpenJij features. You can check their documentation for more information.
|
|
193
|
-
|
|
194
|
-
With JijModelingTranspiler, we can pass along our instance data to create an intermediary representation that can be converted to different formats quickly, but in our case we'll just convert it to QUBO.
|
|
195
|
-
|
|
196
|
-
```python markdown-code-runner
|
|
197
|
-
from jijmodeling_transpiler.core import compile_model
|
|
198
|
-
from jijmodeling_transpiler.core.pubo import transpile_to_pubo
|
|
199
|
-
|
|
200
|
-
data = {
|
|
201
|
-
"W": 100,
|
|
202
|
-
"v": [100, 90, 80, 70, 60, 50, 40, 30],
|
|
203
|
-
"w": [1, 5, 10, 20, 30, 40, 50, 60, 70],
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
compiled = compile_model(problem, data)
|
|
207
|
-
qubo, _ = transpile_to_pubo(compiled).get_qubo_dict()
|
|
208
|
-
qubo
|
|
209
|
-
```
|
|
210
|
-
|
|
211
|
-
We now have a QUBO dictionary, which is valid input for certain optimization solvers. Writing out this kind of dictionary by hand would be very prone to error, but thankfully we just had to write out the model in a human-friendly way which is easier to verify. We can now use this dictionary with `openjij` to actually solve the problem:
|
|
212
|
-
|
|
213
|
-
```python markdown-code-runner
|
|
214
|
-
import openjij as oj
|
|
215
|
-
|
|
216
|
-
sampler = oj.SASampler()
|
|
217
|
-
response = sampler.sample_qubo(qubo, num_reads=1)
|
|
218
|
-
response.first
|
|
219
|
-
```
|
|
220
|
-
|
|
221
|
-
This is using openjij's simulated annealing sampler, with the `num_reads` parameter saying we want to just sample it once. We can increase that to sample the solver multiple times, and then the response object would allow us to explore the different results. But for a problem of this size, all samples will just reach the optimal solution anyway, so here we’ve done a single sample and are looking at just the “best” solution found. It’s an object that looks like:
|
|
222
|
-
|
|
223
|
-
```
|
|
224
|
-
Sample(sample={0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 0, 7: 0}, energy=-5.501111111111113, num_occurrences=1)
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
The sample dictionary gives us the value the solver figured out for each of the decision variables. There’s a lot more that can be done here with Transpiler and OpenJij to better process and visualize the results, or reuse the same model for different purposes, but for that you should check out their respective documentation pages.
|
|
228
|
-
|
|
229
|
-
## Next Steps
|
|
230
|
-
|
|
231
|
-
- [JijModeling Tutorials](https://www.documentation.jijzept.com/docs/jijmodeling/tutorials/1introduction)
|
|
232
|
-
- [JijModelingTranspiler](https://www.documentation.jijzept.com/docs/jijmodelingtranspiler/index)
|
|
233
|
-
- [JijZept](https://www.documentation.jijzept.com/docs/jijzept/index)
|
|
234
|
-
- [Examples with JijModeling and JijZept](https://www.documentation.jijzept.com/docs/tutorial/index)
|
|
235
|
-
|
|
File without changes
|