Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[新バージョン対応] ナップサック問題 #417

Merged
merged 3 commits into from
Apr 9, 2024
Merged

Conversation

github-nakasho
Copy link
Collaborator

Changes

ナップサック問題を題材にしたOpenJijチュートリアル文章を、最新バージョンに対応させました。

Related issue

https://github.com/Jij-Inc/JijZeptIssues/issues/191

Copy link

codecov bot commented Mar 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.09%. Comparing base (a4ac4e5) to head (f00d30f).
Report is 19 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #417   +/-   ##
=======================================
  Coverage   74.09%   74.09%           
=======================================
  Files          42       42           
  Lines        2567     2567           
  Branches      925      925           
=======================================
  Hits         1902     1902           
  Misses        379      379           
  Partials      286      286           
Flag Coverage Δ
openjij 67.46% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.


```python
# set problem
problem = jm.Problem('Knapsack')
problem = jm.Problem('Knapsack', sense=jm.ProblemSense.MAXIMIZE)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing spaces


![](../../../assets/knapsack_01.png)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple consecutive blank lines


![](../../../assets/knapsack_01.png)


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple consecutive blank lines


$$\begin{array}{cccc}\text{Problem:} & \text{Knapsack} & & \\& & \max \quad \displaystyle \sum_{i = 0}^{N - 1} v_{i} \cdot x_{i} & \\\text{{s.t.}} & & & \\ & \text{capacity} & \displaystyle \sum_{i = 0}^{N - 1} w_{i} \cdot x_{i} \leq W & \\\text{{where}} & & & \\& x & 1\text{-dim binary variable}\\\end{array}$$


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple consecutive blank lines

```

### JijModeling transpilerによるPyQUBOへの変換

ここまで行われてきた実装は、全てJijModelingによるものでした。
これを[PyQUBO](https://pyqubo.readthedocs.io/en/latest/)に変換することで、OpenJijはもちろん、他のソルバーを用いた組合せ最適化計算を行うことが可能になります。


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple consecutive blank lines

Copy link
Contributor

@NY57 NY57 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2024/3/25 14:05 JST時点でのレビューを行いました。
1点、修正頂きたい箇所があった為、Suggestionを出させていただきました。

また、codeclimateによる指摘内容も合わせて修正頂けると幸いです!

docs/tutorial/ja/optimization/knapsack.md Outdated Show resolved Hide resolved
i = jm.Element('i', (0, N))
```

`v = jm.Placeholder('v', dim=1)`でナップサックに入れる物の価値を表す一次元のリストを宣言し、その具体的な要素数を`N`としています。その`N`を用いて、ナップサックに入れる物の重さを表す一次元のリストを`w = jm.Placeholder('w', shape=(N))`のように定義することで、`v`と`w`が同じ長さであることを保証できます。`W = jm.Placeholder('W')`ではナップサックの容量制限を表す$W$を定義しています。続く`x = jm.Binary('x', shape=(N))`により、`v, w`と同じ長さのバイナリ変数リスト`x`を定義します。最後に`i = jm.Element('i', (0, N))`は$v_i, w_i, x_i$の添字を定義しており、これは$0\leq i < N$の範囲の整数であることを表しています。
`v = jm.Placeholder('v', ndim=1), w = jm.Placeholder('w', ndim=1)`でナップサックに入れる物の価値と重さを表現する一次元のリストを定義し、さらに`W = jm.Placeholder('W')`ではナップサックの容量制限を表す$W$を定義しています。
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line length

実際に実装された数式をJupyter Notebookで表示してみましょう。

![](../../../assets/knapsack_01.png)
$$\begin{array}{cccc}\text{Problem:} & \text{Knapsack} & & \\& & \max \quad \displaystyle \sum_{i = 0}^{N - 1} v_{i} \cdot x_{i} & \\\text{{s.t.}} & & & \\ & \text{capacity} & \displaystyle \sum_{i = 0}^{N - 1} w_{i} \cdot x_{i} \leq W & \\\text{{where}} & & & \\& x & 1\text{-dim binary variable}\\\end{array}$$
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line length

Value of constraint term: [0.0]
Total weight: 18
```
```python
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dollar signs used before commands without showing output

Copy link

codeclimate bot commented Apr 9, 2024

Code Climate has analyzed commit f00d30f and detected 11 issues on this pull request.

Here's the issue category breakdown:

Category Count
Style 11

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 60.2% (0.0% change).

View more on Code Climate.

Copy link
Contributor

@NY57 NY57 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@NY57 NY57 merged commit 45c5703 into main Apr 9, 2024
25 checks passed
@NY57 NY57 deleted the feature/knapsack branch April 9, 2024 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants