Skip to content

Commit 3fb075c

Browse files
committed
2piの処理入れた
1 parent 88bd7cb commit 3fb075c

6 files changed

+969
-114
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 2,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"0.6666666666666667\n",
13+
"0.6666666666666667 0.33333333333333337\n",
14+
"1.3333333333333337 0.0\n"
15+
]
16+
}
17+
],
18+
"source": [
19+
"import numpy as np\n",
20+
"import cvxpy as cp # pip install cvxpy\n",
21+
"\n",
22+
"# 変数\n",
23+
"x_1 = cp.Variable()\n",
24+
"x_2 = cp.Variable()\n",
25+
"\n",
26+
"# 最小化したい関数 (目的関数)\n",
27+
"objective = cp.Minimize(\n",
28+
" cp.power(x_1, 2) + 2 * cp.power(x_2, 2)\n",
29+
")\n",
30+
"\n",
31+
"# 制約条件 (不等式制約)\n",
32+
"constraints = [\n",
33+
" x_2 >= - x_1 + 1,\n",
34+
" x_2 >= (x_1 - 1) / 2.0\n",
35+
"]\n",
36+
"\n",
37+
"# 問題を定義\n",
38+
"problem = cp.Problem(objective, constraints)\n",
39+
"\n",
40+
"# 最適化 (戻り値は最適化後に得られた値=最小値)\n",
41+
"result = problem.solve()\n",
42+
"print(result) # 0.6666666666666667\n",
43+
"\n",
44+
"# 最小値の時のx_1とx_2の値\n",
45+
"print(x_1.value, x_2.value) # 0.6666666666666667 0.33333333333333337\n",
46+
"\n",
47+
"# 最小値の時のラグランジュ定数\n",
48+
"print(constraints[0].dual_value, constraints[1].dual_value) # 1.3333333333333337 0.0"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": null,
54+
"metadata": {},
55+
"outputs": [],
56+
"source": []
57+
}
58+
],
59+
"metadata": {
60+
"kernelspec": {
61+
"display_name": "Python 3.7.4 64-bit ('base': conda)",
62+
"language": "python",
63+
"name": "python37464bitbaseconda5e623a37214d4771b9ad7f37e3a0bc43"
64+
},
65+
"language_info": {
66+
"codemirror_mode": {
67+
"name": "ipython",
68+
"version": 3
69+
},
70+
"file_extension": ".py",
71+
"mimetype": "text/x-python",
72+
"name": "python",
73+
"nbconvert_exporter": "python",
74+
"pygments_lexer": "ipython3",
75+
"version": "3.7.4"
76+
}
77+
},
78+
"nbformat": 4,
79+
"nbformat_minor": 2
80+
}

0 commit comments

Comments
 (0)