Skip to content

Commit 26b8c2e

Browse files
Создано с помощью Colaboratory
1 parent 61f3f83 commit 26b8c2e

File tree

1 file changed

+267
-0
lines changed

1 file changed

+267
-0
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "view-in-github",
7+
"colab_type": "text"
8+
},
9+
"source": [
10+
"<a href=\"https://colab.research.google.com/github/NikolaiZolotykh/ScientificPython/blob/master/15.01.%20%D0%98%D1%80%D0%BB%D0%B0%D0%BD%D0%B4%D1%81%D0%BA%D0%B8%D0%B9%20%D0%BF%D0%B0%D1%80%D0%B0%D0%B4%D0%BE%D0%BA%D1%81.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"metadata": {
16+
"id": "mmcOipA2gZdW"
17+
},
18+
"source": [
19+
"# Ирландский парадокс"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": null,
25+
"metadata": {
26+
"colab": {
27+
"base_uri": "https://localhost:8080/"
28+
},
29+
"id": "XfAL9A6XgZdY",
30+
"outputId": "3b9b2ea7-ab34-4242-c863-fc180672f8d5"
31+
},
32+
"outputs": [
33+
{
34+
"output_type": "stream",
35+
"name": "stdout",
36+
"text": [
37+
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
38+
"Collecting pulp\n",
39+
" Downloading PuLP-2.7.0-py3-none-any.whl (14.3 MB)\n",
40+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m14.3/14.3 MB\u001b[0m \u001b[31m56.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
41+
"\u001b[?25hInstalling collected packages: pulp\n",
42+
"Successfully installed pulp-2.7.0\n"
43+
]
44+
}
45+
],
46+
"source": [
47+
"!pip install pulp\n",
48+
"import pulp"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": null,
54+
"metadata": {
55+
"id": "ZqVKSwXhgZdY"
56+
},
57+
"outputs": [],
58+
"source": [
59+
"prob = pulp.LpProblem(\"The_Irish_Paradox\", pulp.LpMaximize)"
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": null,
65+
"metadata": {
66+
"id": "bIC5XZ79gZdZ"
67+
},
68+
"outputs": [],
69+
"source": [
70+
"P = pulp.LpVariable(\"Potato\", 0, None)\n",
71+
"M = pulp.LpVariable(\"Meat\", 0, None)"
72+
]
73+
},
74+
{
75+
"cell_type": "markdown",
76+
"metadata": {
77+
"id": "rkMur28agZdZ"
78+
},
79+
"source": [
80+
"Ovjective function:"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": null,
86+
"metadata": {
87+
"id": "moSEIQqmgZdZ"
88+
},
89+
"outputs": [],
90+
"source": [
91+
"prob += M "
92+
]
93+
},
94+
{
95+
"cell_type": "markdown",
96+
"source": [
97+
"Subject to:"
98+
],
99+
"metadata": {
100+
"id": "bhJrXOCcnWtF"
101+
}
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": null,
106+
"metadata": {
107+
"id": "oUSAwbs6gZdZ"
108+
},
109+
"outputs": [],
110+
"source": [
111+
"prob += 2*P + 5*M >= 20\n",
112+
"prob += P + 5*M <= 15"
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": null,
118+
"metadata": {
119+
"id": "uBbD442YgZdZ"
120+
},
121+
"outputs": [],
122+
"source": []
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"metadata": {
128+
"id": "fQt8W2VugZdZ",
129+
"outputId": "2212a355-39eb-4af5-a0bf-e2e6b5efcf7b"
130+
},
131+
"outputs": [
132+
{
133+
"data": {
134+
"text/plain": [
135+
"[Meat, Potato]"
136+
]
137+
},
138+
"execution_count": 13,
139+
"metadata": {},
140+
"output_type": "execute_result"
141+
}
142+
],
143+
"source": [
144+
"# prob.writeLP(\"The_Irish_Paradox.lp\") # Можно сохранить"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"metadata": {
151+
"id": "fAVHzAiDgZda",
152+
"outputId": "d815340a-b01a-4c7b-a8cc-ce7e31630636"
153+
},
154+
"outputs": [
155+
{
156+
"data": {
157+
"text/plain": [
158+
"1"
159+
]
160+
},
161+
"execution_count": 14,
162+
"metadata": {},
163+
"output_type": "execute_result"
164+
}
165+
],
166+
"source": [
167+
"prob.solve()"
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": null,
173+
"metadata": {
174+
"id": "KD8sJeYigZda",
175+
"outputId": "84b5f47a-ae08-499b-eaf7-8ff74945213f"
176+
},
177+
"outputs": [
178+
{
179+
"name": "stdout",
180+
"output_type": "stream",
181+
"text": [
182+
"Status: Optimal\n"
183+
]
184+
}
185+
],
186+
"source": [
187+
"print(\"Status:\", pulp.LpStatus[prob.status])"
188+
]
189+
},
190+
{
191+
"cell_type": "code",
192+
"execution_count": null,
193+
"metadata": {
194+
"id": "hHmhDe17gZdb",
195+
"outputId": "30f9ccc7-4dde-461f-f37f-b156f7f1f381"
196+
},
197+
"outputs": [
198+
{
199+
"name": "stdout",
200+
"output_type": "stream",
201+
"text": [
202+
"Meat = 2.0\n",
203+
"Potato = 5.0\n"
204+
]
205+
}
206+
],
207+
"source": [
208+
"for v in prob.variables():\n",
209+
" print(v.name, \"=\", v.varValue)"
210+
]
211+
},
212+
{
213+
"cell_type": "code",
214+
"execution_count": null,
215+
"metadata": {
216+
"id": "U4hCn3q5gZdb",
217+
"outputId": "712ad6ed-6ad5-495f-a7a2-7db6da0e83ff"
218+
},
219+
"outputs": [
220+
{
221+
"name": "stdout",
222+
"output_type": "stream",
223+
"text": [
224+
"Optimal value = 2.0\n"
225+
]
226+
}
227+
],
228+
"source": [
229+
"print(\"Optimal value = \", pulp.value(prob.objective))"
230+
]
231+
},
232+
{
233+
"cell_type": "code",
234+
"execution_count": null,
235+
"metadata": {
236+
"id": "96hBYM75gZdb"
237+
},
238+
"outputs": [],
239+
"source": []
240+
}
241+
],
242+
"metadata": {
243+
"kernelspec": {
244+
"display_name": "Python 3",
245+
"language": "python",
246+
"name": "python3"
247+
},
248+
"language_info": {
249+
"codemirror_mode": {
250+
"name": "ipython",
251+
"version": 3
252+
},
253+
"file_extension": ".py",
254+
"mimetype": "text/x-python",
255+
"name": "python",
256+
"nbconvert_exporter": "python",
257+
"pygments_lexer": "ipython3",
258+
"version": "3.8.3"
259+
},
260+
"colab": {
261+
"provenance": [],
262+
"include_colab_link": true
263+
}
264+
},
265+
"nbformat": 4,
266+
"nbformat_minor": 0
267+
}

0 commit comments

Comments
 (0)