\n",
147 | " برنامهای بنویسید که مجموع اعضای یک لیست را محاسبه کند.\n",
148 | "
"
149 | ]
150 | },
151 | {
152 | "cell_type": "code",
153 | "execution_count": 4,
154 | "metadata": {},
155 | "outputs": [
156 | {
157 | "name": "stdout",
158 | "output_type": "stream",
159 | "text": [
160 | "40\n"
161 | ]
162 | }
163 | ],
164 | "source": [
165 | "list_sum = 0\n",
166 | "for num in num_list:\n",
167 | " list_sum += num\n",
168 | "print(list_sum)"
169 | ]
170 | },
171 | {
172 | "cell_type": "markdown",
173 | "metadata": {},
174 | "source": [
175 | "
\n",
176 | "در پایتون ما به صورت بالا عمل نمیکنیم. بلکه از دستور\n",
177 | "sum
\n",
178 | "استفاده میکنیم.\n",
179 | "
"
180 | ]
181 | },
182 | {
183 | "cell_type": "code",
184 | "execution_count": 5,
185 | "metadata": {},
186 | "outputs": [
187 | {
188 | "data": {
189 | "text/plain": [
190 | "40"
191 | ]
192 | },
193 | "execution_count": 5,
194 | "metadata": {},
195 | "output_type": "execute_result"
196 | }
197 | ],
198 | "source": [
199 | "sum(num_list)"
200 | ]
201 | },
202 | {
203 | "cell_type": "markdown",
204 | "metadata": {},
205 | "source": [
206 | "
\n",
207 | "سوال:\n",
208 | " کوچکترین عدد را در بین لیستی از اعداد صحیح پیدا کنید.\n",
209 | "
"
210 | ]
211 | },
212 | {
213 | "cell_type": "code",
214 | "execution_count": 6,
215 | "metadata": {},
216 | "outputs": [
217 | {
218 | "name": "stdout",
219 | "output_type": "stream",
220 | "text": [
221 | "2\n"
222 | ]
223 | }
224 | ],
225 | "source": [
226 | "num_list = [12, 23, 2, 7, 4, 8, 81]\n",
227 | "minimum = num_list[0]\n",
228 | "for num in num_list:\n",
229 | " if minimum > num:\n",
230 | " minimum = num\n",
231 | "print(minimum)"
232 | ]
233 | },
234 | {
235 | "cell_type": "markdown",
236 | "metadata": {},
237 | "source": [
238 | "
\n",
239 | "در پایتون ما به صورت بالا عمل نمیکنیم. بلکه از دستور \n",
240 | "min
\n",
241 | "استفاده میکنیم.\n",
242 | "
"
243 | ]
244 | },
245 | {
246 | "cell_type": "code",
247 | "execution_count": 7,
248 | "metadata": {},
249 | "outputs": [
250 | {
251 | "data": {
252 | "text/plain": [
253 | "2"
254 | ]
255 | },
256 | "execution_count": 7,
257 | "metadata": {},
258 | "output_type": "execute_result"
259 | }
260 | ],
261 | "source": [
262 | "min(num_list)"
263 | ]
264 | },
265 | {
266 | "cell_type": "code",
267 | "execution_count": 8,
268 | "metadata": {},
269 | "outputs": [
270 | {
271 | "data": {
272 | "text/plain": [
273 | "81"
274 | ]
275 | },
276 | "execution_count": 8,
277 | "metadata": {},
278 | "output_type": "execute_result"
279 | }
280 | ],
281 | "source": [
282 | "max(num_list)"
283 | ]
284 | },
285 | {
286 | "cell_type": "markdown",
287 | "metadata": {},
288 | "source": [
289 | "
\n",
290 | "تمرین:\n",
291 | " بیشینه یک لیست از اعداد صحیح را با استفاده از حلقه\n",
292 | " for
\n",
293 | " پیدا کنید.\n",
294 | "
"
295 | ]
296 | },
297 | {
298 | "cell_type": "code",
299 | "execution_count": 9,
300 | "metadata": {},
301 | "outputs": [
302 | {
303 | "data": {
304 | "text/plain": [
305 | "range(0, 10)"
306 | ]
307 | },
308 | "execution_count": 9,
309 | "metadata": {},
310 | "output_type": "execute_result"
311 | }
312 | ],
313 | "source": [
314 | "range(0, 10)"
315 | ]
316 | },
317 | {
318 | "cell_type": "code",
319 | "execution_count": 11,
320 | "metadata": {},
321 | "outputs": [
322 | {
323 | "data": {
324 | "text/plain": [
325 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
326 | ]
327 | },
328 | "execution_count": 11,
329 | "metadata": {},
330 | "output_type": "execute_result"
331 | }
332 | ],
333 | "source": [
334 | "list(range(10))"
335 | ]
336 | },
337 | {
338 | "cell_type": "code",
339 | "execution_count": 12,
340 | "metadata": {},
341 | "outputs": [
342 | {
343 | "data": {
344 | "text/plain": [
345 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
346 | ]
347 | },
348 | "execution_count": 12,
349 | "metadata": {},
350 | "output_type": "execute_result"
351 | }
352 | ],
353 | "source": [
354 | "list(range(0, 10))"
355 | ]
356 | },
357 | {
358 | "cell_type": "code",
359 | "execution_count": 13,
360 | "metadata": {},
361 | "outputs": [
362 | {
363 | "data": {
364 | "text/plain": [
365 | "[0, 2, 4, 6, 8]"
366 | ]
367 | },
368 | "execution_count": 13,
369 | "metadata": {},
370 | "output_type": "execute_result"
371 | }
372 | ],
373 | "source": [
374 | "list(range(0, 10, 2))"
375 | ]
376 | },
377 | {
378 | "cell_type": "code",
379 | "execution_count": 14,
380 | "metadata": {},
381 | "outputs": [
382 | {
383 | "name": "stdout",
384 | "output_type": "stream",
385 | "text": [
386 | "72\n"
387 | ]
388 | }
389 | ],
390 | "source": [
391 | "sum_ = 0\n",
392 | "n = 30\n",
393 | "for i in range(1, 31):\n",
394 | " if n % i == 0:\n",
395 | " sum_ += i\n",
396 | "print(sum_)"
397 | ]
398 | },
399 | {
400 | "cell_type": "markdown",
401 | "metadata": {},
402 | "source": [
403 | "
\n",
404 | "حال با دستور \n",
405 | "zip
\n",
406 | "آشنا میشویم. این دستور برای ترکیب عضو به عضو لیستها استفاده میشود.\n",
407 | "
"
408 | ]
409 | },
410 | {
411 | "cell_type": "code",
412 | "execution_count": 17,
413 | "metadata": {},
414 | "outputs": [
415 | {
416 | "name": "stdout",
417 | "output_type": "stream",
418 | "text": [
419 | "[12, 23, 2, 7, 4, 8, 81]\n"
420 | ]
421 | }
422 | ],
423 | "source": [
424 | "print(num_list)"
425 | ]
426 | },
427 | {
428 | "cell_type": "code",
429 | "execution_count": 3,
430 | "metadata": {},
431 | "outputs": [],
432 | "source": [
433 | "indecis = [0, 1, 2, 3, 4, 5, 6]"
434 | ]
435 | },
436 | {
437 | "cell_type": "code",
438 | "execution_count": 4,
439 | "metadata": {},
440 | "outputs": [
441 | {
442 | "data": {
443 | "text/plain": [
444 | "
"
445 | ]
446 | },
447 | "execution_count": 4,
448 | "metadata": {},
449 | "output_type": "execute_result"
450 | }
451 | ],
452 | "source": [
453 | "zip(indecis, num_list)"
454 | ]
455 | },
456 | {
457 | "cell_type": "code",
458 | "execution_count": 20,
459 | "metadata": {},
460 | "outputs": [
461 | {
462 | "data": {
463 | "text/plain": [
464 | "[(0, 12), (1, 23), (2, 2), (3, 7), (4, 4), (5, 8), (6, 81)]"
465 | ]
466 | },
467 | "execution_count": 20,
468 | "metadata": {},
469 | "output_type": "execute_result"
470 | }
471 | ],
472 | "source": [
473 | "list(zip(indecis, num_list))"
474 | ]
475 | },
476 | {
477 | "cell_type": "code",
478 | "execution_count": 5,
479 | "metadata": {},
480 | "outputs": [
481 | {
482 | "name": "stdout",
483 | "output_type": "stream",
484 | "text": [
485 | "pencil 200\n",
486 | "pen 300\n",
487 | "paper 400\n"
488 | ]
489 | }
490 | ],
491 | "source": [
492 | "products =['pencil', 'pen', 'paper']\n",
493 | "demands = [200, 300, 400]\n",
494 | "for p , d in zip(products,demands):\n",
495 | " print(p ,d) "
496 | ]
497 | },
498 | {
499 | "cell_type": "code",
500 | "execution_count": 21,
501 | "metadata": {},
502 | "outputs": [
503 | {
504 | "name": "stdout",
505 | "output_type": "stream",
506 | "text": [
507 | "Index 0: 12\n",
508 | "Index 1: 23\n",
509 | "Index 2: 02\n",
510 | "Index 3: 07\n",
511 | "Index 4: 04\n",
512 | "Index 5: 08\n",
513 | "Index 6: 81\n"
514 | ]
515 | }
516 | ],
517 | "source": [
518 | "for index, element in zip(indecis, num_list):\n",
519 | " print(f\"Index {index}: {element:02d}\")"
520 | ]
521 | },
522 | {
523 | "cell_type": "markdown",
524 | "metadata": {},
525 | "source": [
526 | "\n",
527 | "حلقه\n",
528 | "while
\n",
529 | "تا زمانی که یک شرط خاص برقرار باشد، مجموعه دستورهای مورد انتظار را اجرا میکند.\n",
530 | "
"
531 | ]
532 | },
533 | {
534 | "cell_type": "markdown",
535 | "metadata": {},
536 | "source": [
537 | "\n",
538 | "ساختار کلی حلقه\n",
539 | "while
\n",
540 | "به صورت زیر است:\n",
541 | "
"
542 | ]
543 | },
544 | {
545 | "cell_type": "markdown",
546 | "metadata": {},
547 | "source": [
548 | "```python\n",
549 | "while condition:\n",
550 | " statements\n",
551 | "```"
552 | ]
553 | },
554 | {
555 | "cell_type": "code",
556 | "execution_count": 6,
557 | "metadata": {},
558 | "outputs": [
559 | {
560 | "name": "stdout",
561 | "output_type": "stream",
562 | "text": [
563 | "0\n",
564 | "1\n",
565 | "2\n",
566 | "3\n",
567 | "4\n",
568 | "5\n",
569 | "6\n",
570 | "7\n",
571 | "8\n",
572 | "9\n"
573 | ]
574 | }
575 | ],
576 | "source": [
577 | "i = 0\n",
578 | "while i < 10:\n",
579 | " print(i)\n",
580 | " i += 1"
581 | ]
582 | }
583 | ],
584 | "metadata": {
585 | "kernelspec": {
586 | "display_name": "Python 3",
587 | "language": "python",
588 | "name": "python3"
589 | },
590 | "language_info": {
591 | "codemirror_mode": {
592 | "name": "ipython",
593 | "version": 3
594 | },
595 | "file_extension": ".py",
596 | "mimetype": "text/x-python",
597 | "name": "python",
598 | "nbconvert_exporter": "python",
599 | "pygments_lexer": "ipython3",
600 | "version": "3.12.5"
601 | }
602 | },
603 | "nbformat": 4,
604 | "nbformat_minor": 4
605 | }
606 |
--------------------------------------------------------------------------------
/06-list comprehension.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "\n",
8 | "فرض کنید که میخواهیم لیستی از اعداد صفر تا نه ایجاد کنیم. با توجه به دانش فعلیمان، این کار را با کد زیر انجام میدهیم.\n",
9 | "
"
10 | ]
11 | },
12 | {
13 | "cell_type": "code",
14 | "execution_count": 1,
15 | "metadata": {},
16 | "outputs": [
17 | {
18 | "name": "stdout",
19 | "output_type": "stream",
20 | "text": [
21 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
22 | ]
23 | }
24 | ],
25 | "source": [
26 | "num_list = []\n",
27 | "for i in range(10):\n",
28 | " num_list.append(i)\n",
29 | "print(num_list)"
30 | ]
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "\n",
37 | "زبان برنامهنویسی پایتون این امکان را به برنامهنویس میدهد تا لیست فوق را با استفاده از نحو خاصی تنها در یک خط ایجاد کرد.\n",
38 | "
"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": 2,
44 | "metadata": {},
45 | "outputs": [
46 | {
47 | "name": "stdout",
48 | "output_type": "stream",
49 | "text": [
50 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
51 | ]
52 | }
53 | ],
54 | "source": [
55 | "num_list = [i for i in range(10)]\n",
56 | "print(num_list)"
57 | ]
58 | },
59 | {
60 | "cell_type": "markdown",
61 | "metadata": {},
62 | "source": [
63 | "\n",
64 | "سوال:\n",
65 | " لیستی از توان دو اعداد صفر تا نه را ایجاد کنید.\n",
66 | "
"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": 4,
72 | "metadata": {},
73 | "outputs": [
74 | {
75 | "name": "stdout",
76 | "output_type": "stream",
77 | "text": [
78 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n"
79 | ]
80 | }
81 | ],
82 | "source": [
83 | "lst = []\n",
84 | "for i in range(10):\n",
85 | " lst.append(i ** 2)\n",
86 | "print(lst)"
87 | ]
88 | },
89 | {
90 | "cell_type": "code",
91 | "execution_count": 5,
92 | "metadata": {},
93 | "outputs": [
94 | {
95 | "name": "stdout",
96 | "output_type": "stream",
97 | "text": [
98 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n"
99 | ]
100 | }
101 | ],
102 | "source": [
103 | "lst = [i ** 2 for i in range(10)]\n",
104 | "print(lst)"
105 | ]
106 | },
107 | {
108 | "cell_type": "code",
109 | "execution_count": null,
110 | "metadata": {},
111 | "outputs": [],
112 | "source": [
113 | "num_list = [4, -5, -7, 8, 0, -2]\n",
114 | "\n",
115 | "aa = [i for i in num_list if i < 0]\n",
116 | "bb = [i < 0 for i in num_list]\n",
117 | "print(aa)\n",
118 | "print(bb)"
119 | ]
120 | }
121 | ],
122 | "metadata": {
123 | "kernelspec": {
124 | "display_name": "Python 3",
125 | "language": "python",
126 | "name": "python3"
127 | },
128 | "language_info": {
129 | "codemirror_mode": {
130 | "name": "ipython",
131 | "version": 3
132 | },
133 | "file_extension": ".py",
134 | "mimetype": "text/x-python",
135 | "name": "python",
136 | "nbconvert_exporter": "python",
137 | "pygments_lexer": "ipython3",
138 | "version": "3.12.5"
139 | }
140 | },
141 | "nbformat": 4,
142 | "nbformat_minor": 4
143 | }
144 |
--------------------------------------------------------------------------------
/07-function.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "\n",
8 | "تابع\n",
9 | "
"
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "\n",
17 | "تابع مجموعهای از دستورات است که هیچ، یک، یا چند ورودی میگیرد و هیچ، یک، یا چند خروجی بعد از اعمال مجموعه دستورات یادشده برمیگرداند.\n",
18 | "
"
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": null,
24 | "metadata": {},
25 | "outputs": [],
26 | "source": [
27 | "def machine(z):\n",
28 | " return z * 5"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": null,
34 | "metadata": {},
35 | "outputs": [
36 | {
37 | "data": {
38 | "text/plain": [
39 | "20"
40 | ]
41 | },
42 | "metadata": {},
43 | "output_type": "display_data"
44 | }
45 | ],
46 | "source": [
47 | "s = machine(4)\n",
48 | "s"
49 | ]
50 | },
51 | {
52 | "cell_type": "code",
53 | "execution_count": 1,
54 | "metadata": {},
55 | "outputs": [],
56 | "source": [
57 | "def add(a, b):\n",
58 | " return a + b"
59 | ]
60 | },
61 | {
62 | "cell_type": "code",
63 | "execution_count": 2,
64 | "metadata": {},
65 | "outputs": [
66 | {
67 | "name": "stdout",
68 | "output_type": "stream",
69 | "text": [
70 | "5\n"
71 | ]
72 | }
73 | ],
74 | "source": [
75 | "result = add(2, 3)\n",
76 | "print(result)"
77 | ]
78 | },
79 | {
80 | "cell_type": "code",
81 | "execution_count": 5,
82 | "metadata": {},
83 | "outputs": [],
84 | "source": [
85 | "def Hi(name):\n",
86 | " return f\"Hello {name}\""
87 | ]
88 | },
89 | {
90 | "cell_type": "code",
91 | "execution_count": 7,
92 | "metadata": {},
93 | "outputs": [
94 | {
95 | "data": {
96 | "text/plain": [
97 | "'Hello Ali'"
98 | ]
99 | },
100 | "execution_count": 7,
101 | "metadata": {},
102 | "output_type": "execute_result"
103 | }
104 | ],
105 | "source": [
106 | "Hi(\"Ali\")"
107 | ]
108 | },
109 | {
110 | "cell_type": "markdown",
111 | "metadata": {},
112 | "source": [
113 | "\n",
114 | " یک تابع میتواند چندین خروجی داشته باشد. به عنوان نمونه، تابعی را در نظر بگیرید که مساحت و محیط یک مستطیل را محاسبه میکند.\n",
115 | "
"
116 | ]
117 | },
118 | {
119 | "cell_type": "code",
120 | "execution_count": 18,
121 | "metadata": {},
122 | "outputs": [],
123 | "source": [
124 | "def rectangle(width, height):\n",
125 | " area = width * height\n",
126 | " perimeter = 2 * (width + height)\n",
127 | " return area, perimeter"
128 | ]
129 | },
130 | {
131 | "cell_type": "code",
132 | "execution_count": 19,
133 | "metadata": {},
134 | "outputs": [
135 | {
136 | "data": {
137 | "text/plain": [
138 | "(6, 10)"
139 | ]
140 | },
141 | "execution_count": 19,
142 | "metadata": {},
143 | "output_type": "execute_result"
144 | }
145 | ],
146 | "source": [
147 | "rectangle(2, 3)"
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "execution_count": 13,
153 | "metadata": {},
154 | "outputs": [],
155 | "source": [
156 | "def least_diff(a,b,c):\n",
157 | " diff1 = abs(a-b)\n",
158 | " diff2 = abs(a-c)\n",
159 | " diff3 = abs(b-c)\n",
160 | " y = min(diff1,diff2,diff3)\n",
161 | " return y"
162 | ]
163 | },
164 | {
165 | "cell_type": "code",
166 | "execution_count": null,
167 | "metadata": {},
168 | "outputs": [
169 | {
170 | "data": {
171 | "text/plain": [
172 | "2"
173 | ]
174 | },
175 | "metadata": {},
176 | "output_type": "display_data"
177 | }
178 | ],
179 | "source": [
180 | "a = least_diff(5,7,-3)\n",
181 | "a"
182 | ]
183 | },
184 | {
185 | "cell_type": "code",
186 | "execution_count": null,
187 | "metadata": {},
188 | "outputs": [
189 | {
190 | "data": {
191 | "text/plain": [
192 | "2"
193 | ]
194 | },
195 | "metadata": {},
196 | "output_type": "display_data"
197 | }
198 | ],
199 | "source": [
200 | "b = least_diff(a = 5, c = -3 ,b = 7)\n",
201 | "b"
202 | ]
203 | }
204 | ],
205 | "metadata": {
206 | "kernelspec": {
207 | "display_name": "Python 3",
208 | "language": "python",
209 | "name": "python3"
210 | },
211 | "language_info": {
212 | "codemirror_mode": {
213 | "name": "ipython",
214 | "version": 3
215 | },
216 | "file_extension": ".py",
217 | "mimetype": "text/x-python",
218 | "name": "python",
219 | "nbconvert_exporter": "python",
220 | "pygments_lexer": "ipython3",
221 | "version": "3.12.5"
222 | }
223 | },
224 | "nbformat": 4,
225 | "nbformat_minor": 4
226 | }
227 |
--------------------------------------------------------------------------------
/08-Pyomo, Simple Model.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Simple Model - Pyomo \n"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "
"
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "
"
22 | ]
23 | },
24 | {
25 | "cell_type": "markdown",
26 | "metadata": {},
27 | "source": [
28 | "## First Approach"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 1,
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "# from pyomo.environ import * \n",
38 | "import pyomo.environ as pyo\n",
39 | "m1 = pyo.ConcreteModel()"
40 | ]
41 | },
42 | {
43 | "cell_type": "code",
44 | "execution_count": 2,
45 | "metadata": {},
46 | "outputs": [],
47 | "source": [
48 | "m1.x1 = pyo.Var( domain = pyo.NonNegativeReals)\n",
49 | "m1.x2 = pyo.Var( within = pyo.NonNegativeReals) # within alias for domain\n",
50 | "m1.x3 = pyo.Var( within = pyo.NonNegativeReals)\n",
51 | "\n",
52 | "# m1.x1.pprint()\n",
53 | "# m1.x2.pprint()\n",
54 | "# m1.x3.pprint()"
55 | ]
56 | },
57 | {
58 | "cell_type": "code",
59 | "execution_count": 3,
60 | "metadata": {},
61 | "outputs": [],
62 | "source": [
63 | "m1.c1 = pyo.Constraint(expr = m1.x1 + 2*m1.x2 + m1.x3 <= 430)\n",
64 | "# m1.c1.pprint()"
65 | ]
66 | },
67 | {
68 | "cell_type": "code",
69 | "execution_count": 4,
70 | "metadata": {},
71 | "outputs": [],
72 | "source": [
73 | "m1.c2 = pyo.Constraint(expr = 3*m1.x1 + 2*m1.x3 <= 460)\n",
74 | "# m1.c2.pprint()"
75 | ]
76 | },
77 | {
78 | "cell_type": "code",
79 | "execution_count": 5,
80 | "metadata": {},
81 | "outputs": [],
82 | "source": [
83 | "m1.c3 = pyo.Constraint(expr = m1.x1 + 4*m1.x2 <= 420)\n",
84 | "# m1.c3.pprint()"
85 | ]
86 | },
87 | {
88 | "cell_type": "code",
89 | "execution_count": 6,
90 | "metadata": {},
91 | "outputs": [],
92 | "source": [
93 | "m1.obj = pyo.Objective(expr = 3*m1.x1 + 2*m1.x2 + 5*m1.x3 , \n",
94 | " sense=pyo.maximize)\n",
95 | "# m1.obj.pprint()"
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "execution_count": 10,
101 | "metadata": {},
102 | "outputs": [
103 | {
104 | "name": "stdout",
105 | "output_type": "stream",
106 | "text": [
107 | "\n",
108 | "Problem: \n",
109 | "- Name: unknown\n",
110 | " Lower bound: 1350.0\n",
111 | " Upper bound: 1350.0\n",
112 | " Number of objectives: 1\n",
113 | " Number of constraints: 3\n",
114 | " Number of variables: 3\n",
115 | " Number of binary variables: 0\n",
116 | " Number of integer variables: 0\n",
117 | " Number of continuous variables: 3\n",
118 | " Number of nonzeros: 7\n",
119 | " Sense: -1\n",
120 | " Number of solutions: 1\n",
121 | "Solver: \n",
122 | "- Name: Gurobi 10.03\n",
123 | " Status: ok\n",
124 | " Wallclock time: 0.006000041961669922\n",
125 | " Termination condition: optimal\n",
126 | " Termination message: Model was solved to optimality (subject to tolerances), and an optimal solution is available.\n",
127 | "Solution: \n",
128 | "- number of solutions: 0\n",
129 | " number of solutions displayed: 0\n",
130 | "\n"
131 | ]
132 | }
133 | ],
134 | "source": [
135 | "solver_name = 'gurobi'\n",
136 | "solver = pyo.SolverFactory(solver_name, solver_io=\"python\")\n",
137 | "result = solver.solve(m1)\n",
138 | "print(result)"
139 | ]
140 | },
141 | {
142 | "cell_type": "code",
143 | "execution_count": 7,
144 | "metadata": {},
145 | "outputs": [
146 | {
147 | "name": "stdout",
148 | "output_type": "stream",
149 | "text": [
150 | "\n",
151 | "Problem: \n",
152 | "- Name: tmpemmvb838\n",
153 | " Lower bound: 1350.0\n",
154 | " Upper bound: 1350.0\n",
155 | " Number of objectives: 1\n",
156 | " Number of constraints: 3\n",
157 | " Number of variables: 3\n",
158 | " Number of nonzeros: 7\n",
159 | " Sense: maximize\n",
160 | "Solver: \n",
161 | "- Status: ok\n",
162 | " User time: 0.0\n",
163 | " Termination condition: optimal\n",
164 | " Termination message: Dual simplex - Optimal\\x3a Objective = 1.3500000000e+03\n",
165 | " Error rc: 0\n",
166 | " Time: 0.0692300796508789\n",
167 | "Solution: \n",
168 | "- number of solutions: 0\n",
169 | " number of solutions displayed: 0\n",
170 | "\n"
171 | ]
172 | }
173 | ],
174 | "source": [
175 | "solver_name = 'cplex'\n",
176 | "solver = pyo.SolverFactory(solver_name)\n",
177 | "result = solver.solve(m1)\n",
178 | "print(result)"
179 | ]
180 | },
181 | {
182 | "cell_type": "code",
183 | "execution_count": null,
184 | "metadata": {},
185 | "outputs": [],
186 | "source": [
187 | "print(result.solver.Termination_condition)"
188 | ]
189 | },
190 | {
191 | "cell_type": "code",
192 | "execution_count": null,
193 | "metadata": {},
194 | "outputs": [],
195 | "source": [
196 | "print(f'the value of x1 = {pyo.value(m1.x1)}')\n",
197 | "print(f'the value of x2 = {pyo.value(m1.x2)}')\n",
198 | "print(f'the value of x3 = {pyo.value(m1.x3)}')\n",
199 | "print('-'*30)\n",
200 | "print(f'the value of objective function = {pyo.value(m1.obj)}')"
201 | ]
202 | },
203 | {
204 | "cell_type": "markdown",
205 | "metadata": {},
206 | "source": [
207 | "## Second Approach"
208 | ]
209 | },
210 | {
211 | "cell_type": "code",
212 | "execution_count": null,
213 | "metadata": {},
214 | "outputs": [],
215 | "source": [
216 | "import pyomo.environ as pyo\n",
217 | "m2 = pyo.ConcreteModel()"
218 | ]
219 | },
220 | {
221 | "cell_type": "code",
222 | "execution_count": null,
223 | "metadata": {},
224 | "outputs": [],
225 | "source": [
226 | "m2.x = pyo.Var( [1,2,3], domain = pyo.NonNegativeReals)\n",
227 | "m2.x.pprint()"
228 | ]
229 | },
230 | {
231 | "cell_type": "code",
232 | "execution_count": null,
233 | "metadata": {},
234 | "outputs": [],
235 | "source": [
236 | "m2.c1 = pyo.Constraint(expr = m2.x[1] + 2 * m2.x[2] + m2.x[3] <= 430)\n",
237 | "m2.c2 = pyo.Constraint(expr = 3 * m2.x[1] + 2 * m2.x[3] <= 460)\n",
238 | "m2.c3 = pyo.Constraint(expr = m2.x[1] + 4 * m2.x[2] <= 420)"
239 | ]
240 | },
241 | {
242 | "cell_type": "code",
243 | "execution_count": null,
244 | "metadata": {},
245 | "outputs": [],
246 | "source": [
247 | "m2.obj = pyo.Objective(expr = 3 * m2.x[1] + 2 * m2.x[2] + 5 * m2.x[3] , \n",
248 | " sense=pyo.maximize)"
249 | ]
250 | },
251 | {
252 | "cell_type": "code",
253 | "execution_count": null,
254 | "metadata": {},
255 | "outputs": [],
256 | "source": [
257 | "solver_name = 'cplex'\n",
258 | "solver = pyo.SolverFactory(solver_name)\n",
259 | "result = solver.solve(m2)\n",
260 | "print(result.solver.Termination_condition)"
261 | ]
262 | },
263 | {
264 | "cell_type": "code",
265 | "execution_count": null,
266 | "metadata": {},
267 | "outputs": [],
268 | "source": [
269 | "for i in range(1,len(m2.x) + 1):\n",
270 | " print(f'the value of x{i} is {pyo.value(m2.x[i])}')\n",
271 | "print('-'*30)\n",
272 | "print(f'the optimal objective function is {pyo.value(m2.obj)}')"
273 | ]
274 | },
275 | {
276 | "cell_type": "markdown",
277 | "metadata": {},
278 | "source": [
279 | "## Third Approach"
280 | ]
281 | },
282 | {
283 | "cell_type": "code",
284 | "execution_count": null,
285 | "metadata": {},
286 | "outputs": [],
287 | "source": [
288 | "import pyomo.environ as pyo\n",
289 | "m3 = pyo.ConcreteModel()"
290 | ]
291 | },
292 | {
293 | "cell_type": "code",
294 | "execution_count": null,
295 | "metadata": {},
296 | "outputs": [],
297 | "source": [
298 | "mahsoolat_list = ['mahsool1','mahsool2','mahsool3']\n",
299 | "m3.x = pyo.Var( mahsoolat_list, domain = pyo.NonNegativeReals)\n",
300 | "m3.x.pprint()"
301 | ]
302 | },
303 | {
304 | "cell_type": "code",
305 | "execution_count": null,
306 | "metadata": {},
307 | "outputs": [],
308 | "source": [
309 | "m3.c1 = pyo.Constraint(expr = m3.x['mahsool1'] + 2*m3.x['mahsool2'] + m3.x['mahsool3'] <= 430)\n",
310 | "m3.c2 = pyo.Constraint(expr = 3*m3.x['mahsool1'] + 2*m3.x['mahsool3'] <= 460)\n",
311 | "m3.c3 = pyo.Constraint(expr = m3.x['mahsool1'] + 4*m3.x['mahsool2'] <= 420)\n",
312 | "m3.c1.pprint()"
313 | ]
314 | },
315 | {
316 | "cell_type": "code",
317 | "execution_count": null,
318 | "metadata": {},
319 | "outputs": [],
320 | "source": [
321 | "m3.obj = pyo.Objective(expr =3*m3.x['mahsool1'] + 2*m3.x['mahsool2'] +5*m3.x['mahsool3'] , \n",
322 | " sense=pyo.maximize)\n",
323 | "m3.obj.pprint()"
324 | ]
325 | },
326 | {
327 | "cell_type": "code",
328 | "execution_count": null,
329 | "metadata": {},
330 | "outputs": [],
331 | "source": [
332 | "solver_name = 'cplex'\n",
333 | "solver = pyo.SolverFactory(solver_name)\n",
334 | "result = solver.solve(m3)\n",
335 | "print(result.solver.Termination_condition)"
336 | ]
337 | },
338 | {
339 | "cell_type": "code",
340 | "execution_count": null,
341 | "metadata": {},
342 | "outputs": [],
343 | "source": [
344 | "for mahsool in mahsoolat_list:\n",
345 | " print(f'The Value of {mahsool} is {pyo.value(m3.x[mahsool])}')"
346 | ]
347 | }
348 | ],
349 | "metadata": {
350 | "kernelspec": {
351 | "display_name": "Python 3",
352 | "language": "python",
353 | "name": "python3"
354 | },
355 | "language_info": {
356 | "codemirror_mode": {
357 | "name": "ipython",
358 | "version": 3
359 | },
360 | "file_extension": ".py",
361 | "mimetype": "text/x-python",
362 | "name": "python",
363 | "nbconvert_exporter": "python",
364 | "pygments_lexer": "ipython3",
365 | "version": "3.11.5"
366 | },
367 | "orig_nbformat": 4
368 | },
369 | "nbformat": 4,
370 | "nbformat_minor": 2
371 | }
372 |
--------------------------------------------------------------------------------
/09-Pyomo, Set & Param.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "\n",
8 | "\n",
9 | "تعریف مجموعه ها و پارامتر ها\n",
10 | "
"
11 | ]
12 | },
13 | {
14 | "cell_type": "markdown",
15 | "metadata": {},
16 | "source": [
17 | "# Set"
18 | ]
19 | },
20 | {
21 | "cell_type": "code",
22 | "execution_count": null,
23 | "metadata": {},
24 | "outputs": [],
25 | "source": [
26 | "from pyomo.environ import *\n",
27 | "model = ConcreteModel()"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "model.m = Set(initialize= (1,2,3)) \n",
37 | "model.m.pprint()"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "execution_count": null,
43 | "metadata": {},
44 | "outputs": [],
45 | "source": [
46 | "model.m = Set(initialize= [1,2,3])\n",
47 | "model.m.pprint()"
48 | ]
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": null,
53 | "metadata": {},
54 | "outputs": [],
55 | "source": [
56 | "print(type(model.m))"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": null,
62 | "metadata": {},
63 | "outputs": [],
64 | "source": [
65 | "print(model.m[2])\n",
66 | "print(type(model.m[2]))"
67 | ]
68 | },
69 | {
70 | "cell_type": "code",
71 | "execution_count": null,
72 | "metadata": {},
73 | "outputs": [],
74 | "source": [
75 | "print(type(model.m.get(1)))\n",
76 | "print(type(model.m.get(4)))"
77 | ]
78 | },
79 | {
80 | "cell_type": "markdown",
81 | "metadata": {},
82 | "source": [
83 | "## **RangeSet مفهوم**"
84 | ]
85 | },
86 | {
87 | "cell_type": "code",
88 | "execution_count": null,
89 | "metadata": {},
90 | "outputs": [],
91 | "source": [
92 | "model.s1 = Set(initialize = range(6))\n",
93 | "model.s1.pprint()"
94 | ]
95 | },
96 | {
97 | "cell_type": "code",
98 | "execution_count": null,
99 | "metadata": {},
100 | "outputs": [],
101 | "source": [
102 | "model.s1.get(0)"
103 | ]
104 | },
105 | {
106 | "cell_type": "code",
107 | "execution_count": null,
108 | "metadata": {},
109 | "outputs": [],
110 | "source": [
111 | "model.s2 = Set(initialize = (1,2,3,4,5,6))\n",
112 | "model.s2.pprint()"
113 | ]
114 | },
115 | {
116 | "cell_type": "code",
117 | "execution_count": null,
118 | "metadata": {},
119 | "outputs": [],
120 | "source": [
121 | "model.s2.get(0)"
122 | ]
123 | },
124 | {
125 | "cell_type": "code",
126 | "execution_count": null,
127 | "metadata": {},
128 | "outputs": [],
129 | "source": [
130 | "model.s3 = RangeSet(6) # starts at 1\n",
131 | "model.s4 = RangeSet(0,5)"
132 | ]
133 | },
134 | {
135 | "cell_type": "code",
136 | "execution_count": null,
137 | "metadata": {},
138 | "outputs": [],
139 | "source": [
140 | "model.s3.pprint()\n",
141 | "model.s4.pprint()"
142 | ]
143 | },
144 | {
145 | "cell_type": "code",
146 | "execution_count": null,
147 | "metadata": {},
148 | "outputs": [],
149 | "source": [
150 | "model.s5 = RangeSet(0,6,2) # end index is included\n",
151 | "model.s5.pprint()"
152 | ]
153 | },
154 | {
155 | "cell_type": "code",
156 | "execution_count": null,
157 | "metadata": {},
158 | "outputs": [],
159 | "source": [
160 | "[i for i in model.s5]"
161 | ]
162 | },
163 | {
164 | "cell_type": "markdown",
165 | "metadata": {},
166 | "source": [
167 | "## Operations"
168 | ]
169 | },
170 | {
171 | "cell_type": "code",
172 | "execution_count": null,
173 | "metadata": {},
174 | "outputs": [],
175 | "source": [
176 | "(model.s1 & model.s2).pprint()"
177 | ]
178 | },
179 | {
180 | "cell_type": "code",
181 | "execution_count": null,
182 | "metadata": {},
183 | "outputs": [],
184 | "source": [
185 | "(model.s1 | model.s2).pprint()"
186 | ]
187 | },
188 | {
189 | "cell_type": "code",
190 | "execution_count": null,
191 | "metadata": {},
192 | "outputs": [],
193 | "source": [
194 | "(model.s1 - model.s2).pprint()"
195 | ]
196 | },
197 | {
198 | "cell_type": "code",
199 | "execution_count": null,
200 | "metadata": {},
201 | "outputs": [],
202 | "source": [
203 | "(model.s1 * model.s2).pprint()"
204 | ]
205 | },
206 | {
207 | "cell_type": "code",
208 | "execution_count": null,
209 | "metadata": {},
210 | "outputs": [],
211 | "source": [
212 | "model.origins = Set(initialize=('s1','s2','s3'))\n",
213 | "model.destinations = Set(initialize=('d1','d2','d3'))\n",
214 | "model.origins.pprint()\n",
215 | "model.destinations.pprint()"
216 | ]
217 | },
218 | {
219 | "cell_type": "code",
220 | "execution_count": null,
221 | "metadata": {},
222 | "outputs": [],
223 | "source": [
224 | "model.routes = model.origins * model.destinations\n",
225 | "model.routes.pprint()"
226 | ]
227 | },
228 | {
229 | "cell_type": "markdown",
230 | "metadata": {},
231 | "source": [
232 | "## Predefined Virtual Sets"
233 | ]
234 | },
235 | {
236 | "cell_type": "markdown",
237 | "metadata": {},
238 | "source": [
239 | "**Any** = all possible values. \n",
240 | "**Reals** = floating point values. \n",
241 | "**PositiveReals** = strictly positive floating point values. \n",
242 | "**NonPositiveReals** = non-positive floating point values. \n",
243 | "**NegativeReals** = strictly negative floating point values. \n",
244 | "**NonNegativeReals** = non-negative floating point values. \n",
245 | "**PercentFraction** = floating point values in the interval [0,1]. \n",
246 | "**Integers** = integer values. \n",
247 | "**PositiveIntegers** = positive integer values. \n",
248 | "**NonPositiveIntegers** = non-positive integer values. \n",
249 | "**NegativeIntegers** = negative integer values. \n",
250 | "**NonNegativeIntegers** = non-negative integer values. \n",
251 | "**Boolean** = Boolean values, which can be represented as False/True, 0/1, ’False’/’True’ and ’F’/’T’. \n",
252 | "**Binary** = the integers {0, 1}."
253 | ]
254 | },
255 | {
256 | "cell_type": "markdown",
257 | "metadata": {},
258 | "source": [
259 | "# Param"
260 | ]
261 | },
262 | {
263 | "cell_type": "markdown",
264 | "metadata": {},
265 | "source": [
266 | ": default راجع به دستور "
267 | ]
268 | },
269 | {
270 | "cell_type": "markdown",
271 | "metadata": {},
272 | "source": [
273 | "**اگر از این دستور در تعریف پارامتر های خود استفاده بکنید، در صورتی که مقادیر اولیه برای پارامتر ها در نظر نگرفته باشید، مقادیر دیفالت را قرار خواهد داد**"
274 | ]
275 | },
276 | {
277 | "cell_type": "code",
278 | "execution_count": null,
279 | "metadata": {},
280 | "outputs": [],
281 | "source": [
282 | "import pyomo.environ as pyo\n",
283 | "m = pyo.ConcreteModel()"
284 | ]
285 | },
286 | {
287 | "cell_type": "code",
288 | "execution_count": null,
289 | "metadata": {},
290 | "outputs": [],
291 | "source": [
292 | "m.a = pyo.Param([1,2] , default = 0 , initialize = {1:5,2:8})\n",
293 | "m.a.pprint()"
294 | ]
295 | },
296 | {
297 | "cell_type": "code",
298 | "execution_count": null,
299 | "metadata": {},
300 | "outputs": [],
301 | "source": [
302 | "m.b = pyo.Param([1,'p'] , default = 0 , initialize = {1:5,'p':8})\n",
303 | "m.b.pprint()"
304 | ]
305 | },
306 | {
307 | "cell_type": "code",
308 | "execution_count": null,
309 | "metadata": {},
310 | "outputs": [],
311 | "source": [
312 | "m.b[1] = 6\n",
313 | "m.b.pprint()"
314 | ]
315 | },
316 | {
317 | "cell_type": "code",
318 | "execution_count": null,
319 | "metadata": {},
320 | "outputs": [],
321 | "source": [
322 | "m.c = pyo.Param([1,'p'] , initialize = {1:5,'p':8} , mutable = True)\n",
323 | "m.c.pprint()"
324 | ]
325 | },
326 | {
327 | "cell_type": "code",
328 | "execution_count": null,
329 | "metadata": {},
330 | "outputs": [],
331 | "source": [
332 | "m.c['p'] = 10\n",
333 | "m.c.pprint()"
334 | ]
335 | },
336 | {
337 | "cell_type": "code",
338 | "execution_count": null,
339 | "metadata": {},
340 | "outputs": [],
341 | "source": [
342 | "m.d = pyo.Param([1,2] ,initialize = {1:5,2:-8} \n",
343 | " ,within= pyo.NonNegativeIntegers)\n",
344 | "m.d.pprint()"
345 | ]
346 | }
347 | ],
348 | "metadata": {
349 | "kernelspec": {
350 | "display_name": "Python 3",
351 | "language": "python",
352 | "name": "python3"
353 | },
354 | "language_info": {
355 | "codemirror_mode": {
356 | "name": "ipython",
357 | "version": 3
358 | },
359 | "file_extension": ".py",
360 | "mimetype": "text/x-python",
361 | "name": "python",
362 | "nbconvert_exporter": "python",
363 | "pygments_lexer": "ipython3",
364 | "version": "3.11.5"
365 | },
366 | "orig_nbformat": 4
367 | },
368 | "nbformat": 4,
369 | "nbformat_minor": 2
370 | }
371 |
--------------------------------------------------------------------------------
/10-Sigma in pyomo.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "\n",
8 | "\n",
9 | " مثال هایی برای نحوه نمایش سیگما و انجام عملیات های مربوط به آن
"
10 | ]
11 | },
12 | {
13 | "cell_type": "code",
14 | "execution_count": 1,
15 | "metadata": {},
16 | "outputs": [],
17 | "source": [
18 | "import itertools\n",
19 | "import numpy as np\n",
20 | "import pyomo.environ as pyo\n",
21 | "m1 = pyo.ConcreteModel()"
22 | ]
23 | },
24 | {
25 | "cell_type": "code",
26 | "execution_count": 2,
27 | "metadata": {},
28 | "outputs": [],
29 | "source": [
30 | "m1.gas = pyo.Set(initialize=('gas1','gas2','gas3'))\n",
31 | "m1.oil = pyo.Set(initialize=('oil1','oil2','oil3'))"
32 | ]
33 | },
34 | {
35 | "cell_type": "code",
36 | "execution_count": 3,
37 | "metadata": {},
38 | "outputs": [
39 | {
40 | "name": "stdout",
41 | "output_type": "stream",
42 | "text": [
43 | "oil : Size=1, Index=None, Ordered=Insertion\n",
44 | " Key : Dimen : Domain : Size : Members\n",
45 | " None : 1 : Any : 3 : {'oil1', 'oil2', 'oil3'}\n"
46 | ]
47 | }
48 | ],
49 | "source": [
50 | "m1.oil.pprint()"
51 | ]
52 | },
53 | {
54 | "cell_type": "markdown",
55 | "metadata": {},
56 | "source": [
57 | "### Parameters"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": 4,
63 | "metadata": {},
64 | "outputs": [],
65 | "source": [
66 | "# m1.gas_profit = pyo.Param(m1.gas, initialize = {'gas1':70, 'gas2':60, 'gas3':50} )\n",
67 | "# m1.oil_cost = pyo.Param(m1.oil, initialize = {'oil1':45, 'oil2':35, 'oil3':25} )\n",
68 | "# m1.demands = pyo.Param(m1.gas, initialize = {'gas1':3000, 'gas2':2000, 'gas3':1000})\n",
69 | "# m1.oilpurchase = pyo.Param(m1.oil, initialize = {'oil1':5000, 'oil2':5000, 'oil3':5000})\n",
70 | "# m1.oktan_portion = pyo.Param(m1.oil, initialize = {'oil1':12, 'oil2':6, 'oil3':8} )\n",
71 | "# m1.solfor_portion = pyo.Param(m1.oil, initialize = {'oil1':0.005, 'oil2':0.02, 'oil3':0.03})\n",
72 | "# m1.oktan_need = pyo.Param(m1.gas, initialize = {'gas1':10, 'gas2':8, 'gas3':6} )\n",
73 | "# m1.solfor_need = pyo.Param(m1.gas, initialize = {'gas1':0.01, 'gas2':0.02, 'gas3':0.01})\n"
74 | ]
75 | },
76 | {
77 | "cell_type": "markdown",
78 | "metadata": {},
79 | "source": [
80 | "### Variables"
81 | ]
82 | },
83 | {
84 | "cell_type": "code",
85 | "execution_count": 5,
86 | "metadata": {},
87 | "outputs": [
88 | {
89 | "name": "stdout",
90 | "output_type": "stream",
91 | "text": [
92 | "x : Size=9, Index=x_index\n",
93 | " Key : Lower : Value : Upper : Fixed : Stale : Domain\n",
94 | " ('oil1', 'gas1') : 0 : None : None : False : True : NonNegativeReals\n",
95 | " ('oil1', 'gas2') : 0 : None : None : False : True : NonNegativeReals\n",
96 | " ('oil1', 'gas3') : 0 : None : None : False : True : NonNegativeReals\n",
97 | " ('oil2', 'gas1') : 0 : None : None : False : True : NonNegativeReals\n",
98 | " ('oil2', 'gas2') : 0 : None : None : False : True : NonNegativeReals\n",
99 | " ('oil2', 'gas3') : 0 : None : None : False : True : NonNegativeReals\n",
100 | " ('oil3', 'gas1') : 0 : None : None : False : True : NonNegativeReals\n",
101 | " ('oil3', 'gas2') : 0 : None : None : False : True : NonNegativeReals\n",
102 | " ('oil3', 'gas3') : 0 : None : None : False : True : NonNegativeReals\n"
103 | ]
104 | }
105 | ],
106 | "source": [
107 | "m1.x = pyo.Var(m1.oil, m1.gas , domain = pyo.NonNegativeReals)\n",
108 | "m1.x.pprint()"
109 | ]
110 | },
111 | {
112 | "cell_type": "markdown",
113 | "metadata": {},
114 | "source": [
115 | "### Objective function"
116 | ]
117 | },
118 | {
119 | "cell_type": "markdown",
120 | "metadata": {
121 | "slideshow": {
122 | "slide_type": "slide"
123 | }
124 | },
125 | "source": [
126 | "$$\n",
127 | "Income = \\sum_{j=1}^3 gp_j \\sum_{i=1}^{3} x_{ij} = \\sum_{j=1}^3 \\sum_{i=1}^{3} gp_j x_{ij} \\\\\n",
128 | "$$"
129 | ]
130 | },
131 | {
132 | "cell_type": "code",
133 | "execution_count": 6,
134 | "metadata": {},
135 | "outputs": [
136 | {
137 | "name": "stdout",
138 | "output_type": "stream",
139 | "text": [
140 | "70*x[oil1,gas1] + 70*x[oil2,gas1] + 70*x[oil3,gas1] + 60*x[oil1,gas2] + 60*x[oil2,gas2] + 60*x[oil3,gas2] + 50*x[oil1,gas3] + 50*x[oil2,gas3] + 50*x[oil3,gas3]\n"
141 | ]
142 | }
143 | ],
144 | "source": [
145 | "m1.gas_profit = pyo.Param(m1.gas , initialize={'gas1':70,'gas2':60,'gas3':50})\n",
146 | "income = sum(m1.gas_profit[j] * m1.x[i,j] for j in m1.gas for i in m1.oil)\n",
147 | "print(income)"
148 | ]
149 | },
150 | {
151 | "cell_type": "markdown",
152 | "metadata": {},
153 | "source": [
154 | "$$\n",
155 | "Cost_1 = \\sum_{i=1}^{3} oc_i \\sum_{j=1}^{3} x_{ij} = \\sum_{i=1}^{3} \\sum_{j=1}^{3} oc_i x_{ij}\n",
156 | "$$"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": 7,
162 | "metadata": {},
163 | "outputs": [
164 | {
165 | "name": "stdout",
166 | "output_type": "stream",
167 | "text": [
168 | "45*x[oil1,gas1] + 45*x[oil1,gas2] + 45*x[oil1,gas3] + 35*x[oil2,gas1] + 35*x[oil2,gas2] + 35*x[oil2,gas3] + 25*x[oil3,gas1] + 25*x[oil3,gas2] + 25*x[oil3,gas3]\n"
169 | ]
170 | }
171 | ],
172 | "source": [
173 | "m1.oil_cost = pyo.Param(m1.oil , initialize={'oil1':45,'oil2':35,'oil3':25} )\n",
174 | "cost_1 = sum (m1.oil_cost[i] * m1.x[i,j] for i in m1.oil for j in m1.gas)\n",
175 | "print(cost_1)"
176 | ]
177 | },
178 | {
179 | "cell_type": "markdown",
180 | "metadata": {},
181 | "source": [
182 | "$$\n",
183 | "Cost_2 = 4 \\sum_{i=1}^{3} \\sum_{j=1}^{3} x_{ij} \n",
184 | "$$"
185 | ]
186 | },
187 | {
188 | "cell_type": "code",
189 | "execution_count": 8,
190 | "metadata": {},
191 | "outputs": [
192 | {
193 | "name": "stdout",
194 | "output_type": "stream",
195 | "text": [
196 | "4*(x[oil1,gas1] + x[oil1,gas2] + x[oil1,gas3] + x[oil2,gas1] + x[oil2,gas2] + x[oil2,gas3] + x[oil3,gas1] + x[oil3,gas2] + x[oil3,gas3])\n"
197 | ]
198 | }
199 | ],
200 | "source": [
201 | "cost_2 = 4 * sum(m1.x[i,j] for i in m1.oil for j in m1.gas)\n",
202 | "print(cost_2)"
203 | ]
204 | },
205 | {
206 | "cell_type": "code",
207 | "execution_count": 9,
208 | "metadata": {},
209 | "outputs": [
210 | {
211 | "name": "stdout",
212 | "output_type": "stream",
213 | "text": [
214 | "obj : Size=1, Index=None, Active=True\n",
215 | " Key : Active : Sense : Expression\n",
216 | " None : True : maximize : 70*x[oil1,gas1] + 70*x[oil2,gas1] + 70*x[oil3,gas1] + 60*x[oil1,gas2] + 60*x[oil2,gas2] + 60*x[oil3,gas2] + 50*x[oil1,gas3] + 50*x[oil2,gas3] + 50*x[oil3,gas3] - (45*x[oil1,gas1] + 45*x[oil1,gas2] + 45*x[oil1,gas3] + 35*x[oil2,gas1] + 35*x[oil2,gas2] + 35*x[oil2,gas3] + 25*x[oil3,gas1] + 25*x[oil3,gas2] + 25*x[oil3,gas3]) - 4*(x[oil1,gas1] + x[oil1,gas2] + x[oil1,gas3] + x[oil2,gas1] + x[oil2,gas2] + x[oil2,gas3] + x[oil3,gas1] + x[oil3,gas2] + x[oil3,gas3])\n"
217 | ]
218 | }
219 | ],
220 | "source": [
221 | "m1.obj = pyo.Objective(expr = income - cost_1 - cost_2,\n",
222 | " sense = pyo.maximize)\n",
223 | "m1.obj.pprint()"
224 | ]
225 | },
226 | {
227 | "cell_type": "markdown",
228 | "metadata": {},
229 | "source": [
230 | "### Constraints"
231 | ]
232 | },
233 | {
234 | "cell_type": "markdown",
235 | "metadata": {},
236 | "source": [
237 | "$$\n",
238 | "\\sum_{i=1}^{3} x_{ij} = d_j \\quad \\quad \\forall j=1,...,3\n",
239 | "$$"
240 | ]
241 | },
242 | {
243 | "cell_type": "code",
244 | "execution_count": 10,
245 | "metadata": {},
246 | "outputs": [
247 | {
248 | "name": "stdout",
249 | "output_type": "stream",
250 | "text": [
251 | "c1 : Size=3, Index=gas, Active=True\n",
252 | " Key : Lower : Body : Upper : Active\n",
253 | " gas1 : 3000.0 : x[oil1,gas1] + x[oil2,gas1] + x[oil3,gas1] : 3000.0 : True\n",
254 | " gas2 : 2000.0 : x[oil1,gas2] + x[oil2,gas2] + x[oil3,gas2] : 2000.0 : True\n",
255 | " gas3 : 1000.0 : x[oil1,gas3] + x[oil2,gas3] + x[oil3,gas3] : 1000.0 : True\n"
256 | ]
257 | }
258 | ],
259 | "source": [
260 | "m1.demands = pyo.Param(m1.gas , initialize={'gas1':3000,'gas2':2000,'gas3':1000})\n",
261 | "def gas_demand (model, j):\n",
262 | " return sum(model.x[i,j] for i in model.oil) == model.demands[j]\n",
263 | "m1.c1 = pyo.Constraint(m1.gas , rule = gas_demand)\n",
264 | "m1.c1.pprint()"
265 | ]
266 | },
267 | {
268 | "cell_type": "code",
269 | "execution_count": 11,
270 | "metadata": {},
271 | "outputs": [],
272 | "source": [
273 | "# def gas_demand_test (model, j):\n",
274 | "# return sum(model.x[i,j] for i in model.oil) == model.demands[j]\n",
275 | "# print(gas_demand_test(m1, j='gas3'))"
276 | ]
277 | },
278 | {
279 | "cell_type": "markdown",
280 | "metadata": {},
281 | "source": [
282 | "$$\n",
283 | "\\sum_{j=1}^{3} x_{ij} \\leq p_i \\quad \\quad \\forall i=1,...,3\n",
284 | "$$"
285 | ]
286 | },
287 | {
288 | "cell_type": "code",
289 | "execution_count": 12,
290 | "metadata": {},
291 | "outputs": [
292 | {
293 | "name": "stdout",
294 | "output_type": "stream",
295 | "text": [
296 | "c2 : Size=3, Index=oil, Active=True\n",
297 | " Key : Lower : Body : Upper : Active\n",
298 | " oil1 : -Inf : x[oil1,gas1] + x[oil1,gas2] + x[oil1,gas3] : 5000.0 : True\n",
299 | " oil2 : -Inf : x[oil2,gas1] + x[oil2,gas2] + x[oil2,gas3] : 5000.0 : True\n",
300 | " oil3 : -Inf : x[oil3,gas1] + x[oil3,gas2] + x[oil3,gas3] : 5000.0 : True\n"
301 | ]
302 | }
303 | ],
304 | "source": [
305 | "m1.oil_purchase = pyo.Param(m1.oil , initialize = {'oil1':5000, 'oil2':5000 , 'oil3':5000})\n",
306 | "\n",
307 | "def oil_purchase(model, i):\n",
308 | " return sum(model.x[i,j] for j in model.gas) <= model.oil_purchase[i]\n",
309 | "m1.c2 = pyo.Constraint(m1.oil , rule = oil_purchase)\n",
310 | "m1.c2.pprint()"
311 | ]
312 | },
313 | {
314 | "cell_type": "markdown",
315 | "metadata": {},
316 | "source": [
317 | "$$\n",
318 | "\\sum_{i=1}^{3} \\sum_{j=1}^{3} x_{ij} \\leq 14000 \n",
319 | "$$"
320 | ]
321 | },
322 | {
323 | "cell_type": "code",
324 | "execution_count": 13,
325 | "metadata": {},
326 | "outputs": [
327 | {
328 | "name": "stdout",
329 | "output_type": "stream",
330 | "text": [
331 | "c3 : Size=1, Index=None, Active=True\n",
332 | " Key : Lower : Body : Upper : Active\n",
333 | " None : -Inf : x[oil1,gas1] + x[oil1,gas2] + x[oil1,gas3] + x[oil2,gas1] + x[oil2,gas2] + x[oil2,gas3] + x[oil3,gas1] + x[oil3,gas2] + x[oil3,gas3] : 14000.0 : True\n"
334 | ]
335 | }
336 | ],
337 | "source": [
338 | "m1.c3 = pyo.Constraint(expr = sum(m1.x[i,j] for i in m1.oil for j in m1.gas)<=14000)\n",
339 | "m1.c3.pprint()"
340 | ]
341 | },
342 | {
343 | "cell_type": "markdown",
344 | "metadata": {},
345 | "source": [
346 | "$$\n",
347 | "\\sum_{i=1}^{3} op_{i} x_{ij} \\geq \\ on_{j} \\sum_{i=1}^{3} x_{ij} \\quad \\forall j\n",
348 | "$$"
349 | ]
350 | },
351 | {
352 | "cell_type": "code",
353 | "execution_count": 14,
354 | "metadata": {},
355 | "outputs": [
356 | {
357 | "name": "stdout",
358 | "output_type": "stream",
359 | "text": [
360 | "c4 : Size=3, Index=gas, Active=True\n",
361 | " Key : Lower : Body : Upper : Active\n",
362 | " gas1 : -Inf : 10*(x[oil1,gas1] + x[oil2,gas1] + x[oil3,gas1]) - (12*x[oil1,gas1] + 6*x[oil2,gas1] + 8*x[oil3,gas1]) : 0.0 : True\n",
363 | " gas2 : -Inf : 8*(x[oil1,gas2] + x[oil2,gas2] + x[oil3,gas2]) - (12*x[oil1,gas2] + 6*x[oil2,gas2] + 8*x[oil3,gas2]) : 0.0 : True\n",
364 | " gas3 : -Inf : 6*(x[oil1,gas3] + x[oil2,gas3] + x[oil3,gas3]) - (12*x[oil1,gas3] + 6*x[oil2,gas3] + 8*x[oil3,gas3]) : 0.0 : True\n"
365 | ]
366 | }
367 | ],
368 | "source": [
369 | "m1.oktan_portion = pyo.Param(m1.oil , initialize ={'oil1':12,'oil2':6,'oil3':8})\n",
370 | "m1.oktan_need = pyo.Param(m1.gas ,initialize ={'gas1':10,'gas2':8,'gas3':6} )\n",
371 | "\n",
372 | "def oktan_constraint(model, j):\n",
373 | " return ( sum(model.oktan_portion[i] * model.x[i,j] for i in model.oil) >=\n",
374 | " model.oktan_need[j] * sum(model.x[i,j] for i in model.oil))\n",
375 | "m1.c4 = pyo.Constraint(m1.gas , rule= oktan_constraint)\n",
376 | "m1.c4.pprint()"
377 | ]
378 | },
379 | {
380 | "cell_type": "markdown",
381 | "metadata": {},
382 | "source": [
383 | "$$\n",
384 | "\\sum_{i=1}^{3} sp_{i} x_{ij} \\leq \\ sn_{j} \\sum_{i=1}^{3} x_{ij} \\quad \\forall j\n",
385 | "$$"
386 | ]
387 | },
388 | {
389 | "cell_type": "code",
390 | "execution_count": 15,
391 | "metadata": {},
392 | "outputs": [
393 | {
394 | "name": "stdout",
395 | "output_type": "stream",
396 | "text": [
397 | "c5 : Size=3, Index=gas, Active=True\n",
398 | " Key : Lower : Body : Upper : Active\n",
399 | " gas1 : -Inf : 0.005*x[oil1,gas1] + 0.02*x[oil2,gas1] + 0.03*x[oil3,gas1] - 0.01*(x[oil1,gas1] + x[oil2,gas1] + x[oil3,gas1]) : 0.0 : True\n",
400 | " gas2 : -Inf : 0.005*x[oil1,gas2] + 0.02*x[oil2,gas2] + 0.03*x[oil3,gas2] - 0.02*(x[oil1,gas2] + x[oil2,gas2] + x[oil3,gas2]) : 0.0 : True\n",
401 | " gas3 : -Inf : 0.005*x[oil1,gas3] + 0.02*x[oil2,gas3] + 0.03*x[oil3,gas3] - 0.01*(x[oil1,gas3] + x[oil2,gas3] + x[oil3,gas3]) : 0.0 : True\n"
402 | ]
403 | }
404 | ],
405 | "source": [
406 | "m1.solfor_portion = pyo.Param(m1.oil ,initialize ={'oil1':0.005,'oil2':0.02,'oil3':0.03})\n",
407 | "m1.solfor_need = pyo.Param(m1.gas ,initialize ={'gas1':0.01,'gas2':0.02,'gas3':0.01} )\n",
408 | "\n",
409 | "def solfor_constraint(model, j):\n",
410 | " return ( sum(model.solfor_portion[i] * model.x[i,j] for i in model.oil) <=\n",
411 | " model.solfor_need[j] * sum(model.x[i,j] for i in model.oil))\n",
412 | "m1.c5 = pyo.Constraint(m1.gas , rule= solfor_constraint)\n",
413 | "m1.c5.pprint()"
414 | ]
415 | },
416 | {
417 | "cell_type": "code",
418 | "execution_count": 18,
419 | "metadata": {},
420 | "outputs": [],
421 | "source": [
422 | "# ! pip install highspy\n",
423 | "solver_name = 'cplex'\n",
424 | "solver = pyo.SolverFactory(solver_name)\n",
425 | "result = solver.solve(m1)"
426 | ]
427 | },
428 | {
429 | "cell_type": "code",
430 | "execution_count": 19,
431 | "metadata": {},
432 | "outputs": [
433 | {
434 | "name": "stdout",
435 | "output_type": "stream",
436 | "text": [
437 | "optimal\n"
438 | ]
439 | }
440 | ],
441 | "source": [
442 | "print(result.solver.Termination_condition)"
443 | ]
444 | },
445 | {
446 | "cell_type": "code",
447 | "execution_count": 20,
448 | "metadata": {},
449 | "outputs": [
450 | {
451 | "name": "stdout",
452 | "output_type": "stream",
453 | "text": [
454 | "the optimal objective function = 126000.0\n"
455 | ]
456 | }
457 | ],
458 | "source": [
459 | "print(f'the optimal objective function = {pyo.value(m1.obj)}')"
460 | ]
461 | },
462 | {
463 | "cell_type": "code",
464 | "execution_count": 21,
465 | "metadata": {},
466 | "outputs": [
467 | {
468 | "name": "stdout",
469 | "output_type": "stream",
470 | "text": [
471 | "Value of x('oil1', 'gas1') = 2400.0\n",
472 | "Value of x('oil1', 'gas2') = 800.0\n",
473 | "Value of x('oil1', 'gas3') = 800.0\n",
474 | "Value of x('oil2', 'gas1') = 0.0\n",
475 | "Value of x('oil2', 'gas2') = 0.0\n",
476 | "Value of x('oil2', 'gas3') = 0.0\n",
477 | "Value of x('oil3', 'gas1') = 600.0\n",
478 | "Value of x('oil3', 'gas2') = 1200.0\n",
479 | "Value of x('oil3', 'gas3') = 200.0\n"
480 | ]
481 | }
482 | ],
483 | "source": [
484 | "for i , j in itertools.product(m1.oil,m1.gas):\n",
485 | " print(f'Value of x{i,j} = {np.round(pyo.value(m1.x[i,j]))}')"
486 | ]
487 | },
488 | {
489 | "cell_type": "code",
490 | "execution_count": 22,
491 | "metadata": {},
492 | "outputs": [
493 | {
494 | "name": "stdout",
495 | "output_type": "stream",
496 | "text": [
497 | "Value of gas1 = 3000.0\n",
498 | "------------------------------\n",
499 | "Value of gas2 = 2000.0\n",
500 | "------------------------------\n",
501 | "Value of gas3 = 1000.0\n",
502 | "------------------------------\n"
503 | ]
504 | }
505 | ],
506 | "source": [
507 | "for j in m1.gas:\n",
508 | " val = np.round(sum(pyo.value(m1.x[i,j]) for i in m1.oil))\n",
509 | " print(f'Value of {j} = {val}')\n",
510 | " print('-'*30)\n"
511 | ]
512 | }
513 | ],
514 | "metadata": {
515 | "kernelspec": {
516 | "display_name": "Python 3",
517 | "language": "python",
518 | "name": "python3"
519 | },
520 | "language_info": {
521 | "codemirror_mode": {
522 | "name": "ipython",
523 | "version": 3
524 | },
525 | "file_extension": ".py",
526 | "mimetype": "text/x-python",
527 | "name": "python",
528 | "nbconvert_exporter": "python",
529 | "pygments_lexer": "ipython3",
530 | "version": "3.11.5"
531 | },
532 | "orig_nbformat": 4
533 | },
534 | "nbformat": 4,
535 | "nbformat_minor": 2
536 | }
537 |
--------------------------------------------------------------------------------
/11-Pandas for Data.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## یک مثال جهت فراخوانی داده ها از فایل خارجی (همچون اکسل) و وارد کردن در پایتون"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### *این فایل را اجرا نکنید* این سلول ها صرفا برای درک نحوه انتقال فایل اکسل و وارد کردن به پایتون می باشد"
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "#### از قالب و چهارچوب آن الگوبرداری کرده و در کدنویسی خود استفاده کنید"
22 | ]
23 | },
24 | {
25 | "cell_type": "markdown",
26 | "metadata": {},
27 | "source": [
28 | "## Importing Libraries"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": null,
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "import pyomo.environ as pyo\n",
38 | "import pandas as pd\n",
39 | "model = pyo.ConcreteModel()"
40 | ]
41 | },
42 | {
43 | "cell_type": "markdown",
44 | "metadata": {},
45 | "source": [
46 | "## Sets"
47 | ]
48 | },
49 | {
50 | "cell_type": "code",
51 | "execution_count": null,
52 | "metadata": {},
53 | "outputs": [],
54 | "source": [
55 | "model.T = pyo.RangeSet(12)\n",
56 | "model.T.pprint()"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": null,
62 | "metadata": {},
63 | "outputs": [],
64 | "source": [
65 | "prod_list = pd.read_excel('APP Parameters.xlsx',sheet_name='Manufacture')['Product']\n",
66 | "model.P = pyo.Set(initialize=prod_list)\n",
67 | "model.P.pprint()"
68 | ]
69 | },
70 | {
71 | "cell_type": "markdown",
72 | "metadata": {},
73 | "source": [
74 | "## Parameters"
75 | ]
76 | },
77 | {
78 | "cell_type": "code",
79 | "execution_count": null,
80 | "metadata": {},
81 | "outputs": [],
82 | "source": [
83 | "# cost of Manufacturing\n",
84 | "cm_table = pd.read_excel('APP Parameters.xlsx',sheet_name='Manufacture', index_col=0)\n",
85 | "cm_table.iloc[:,0].to_dict()"
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": null,
91 | "metadata": {},
92 | "outputs": [],
93 | "source": [
94 | "model.manufacture_cost = pyo.Param(model.P, initialize = cm_table.iloc[:,0].to_dict())\n",
95 | "model.manufacture_cost.pprint()"
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "execution_count": null,
101 | "metadata": {},
102 | "outputs": [],
103 | "source": [
104 | "# cost of Inventory\n",
105 | "ci_table = pd.read_excel('APP Parameters.xlsx',sheet_name='Inventory')\n",
106 | "ci_table = ci_table.set_index('Product')\n",
107 | "model.inventory_cost = pyo.Param(model.P, initialize = ci_table.iloc[:,0].to_dict())\n",
108 | "model.inventory_cost.pprint()"
109 | ]
110 | },
111 | {
112 | "cell_type": "code",
113 | "execution_count": null,
114 | "metadata": {},
115 | "outputs": [],
116 | "source": [
117 | "# cost of Backorders\n",
118 | "cb_table = pd.read_excel('APP Parameters.xlsx',sheet_name='Backorder')\n",
119 | "cb_table = cb_table.set_index(keys=[cb_table.columns[0]])\n",
120 | "model.backorder_cost= pyo.Param(model.P, initialize = cb_table.iloc[:,0].to_dict())\n",
121 | "model.backorder_cost.pprint()"
122 | ]
123 | },
124 | {
125 | "cell_type": "code",
126 | "execution_count": null,
127 | "metadata": {},
128 | "outputs": [],
129 | "source": [
130 | "# cost of Outsource\n",
131 | "cs_table = pd.read_excel('APP Parameters.xlsx',sheet_name='Outsource' ,index_col=0)\n",
132 | "model.outsource_cost= pyo.Param(model.P, initialize = cs_table.iloc[:,0].to_dict())\n",
133 | "model.outsource_cost.pprint()"
134 | ]
135 | },
136 | {
137 | "cell_type": "code",
138 | "execution_count": null,
139 | "metadata": {},
140 | "outputs": [],
141 | "source": [
142 | "# Initial Inventory\n",
143 | "I0_table = pd.read_excel('APP Parameters.xlsx',sheet_name='Initial_Inventory', index_col=0)\n",
144 | "model.initial_inv = pyo.Param(model.P, initialize = I0_table.iloc[:,0].to_dict())\n",
145 | "model.initial_inv.pprint()"
146 | ]
147 | },
148 | {
149 | "cell_type": "code",
150 | "execution_count": null,
151 | "metadata": {},
152 | "outputs": [],
153 | "source": [
154 | "# Initial Backorders\n",
155 | "B0_table = pd.read_excel('APP Parameters.xlsx',sheet_name='Initial_Backorder')\n",
156 | "B0_table = B0_table.set_index(keys=[B0_table.columns[0]])\n",
157 | "model.initial_back = pyo.Param(model.P, initialize = B0_table.iloc[:,0].to_dict())\n",
158 | "model.initial_back.pprint()"
159 | ]
160 | },
161 | {
162 | "cell_type": "code",
163 | "execution_count": null,
164 | "metadata": {},
165 | "outputs": [],
166 | "source": [
167 | "# production time\n",
168 | "l_table = pd.read_excel('APP Parameters.xlsx',sheet_name='Production_Time', index_col=0)\n",
169 | "model.production_time = pyo.Param(model.P, initialize = l_table.iloc[:,0].to_dict())\n",
170 | "model.production_time.pprint()"
171 | ]
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": null,
176 | "metadata": {},
177 | "outputs": [],
178 | "source": [
179 | "# Demands\n",
180 | "demand_table = pd.read_excel('APP Parameters.xlsx',sheet_name='Demand')\n",
181 | "demand_table = demand_table.set_index(keys=[demand_table.columns[0],\n",
182 | " demand_table.columns[1]])\n",
183 | "demand_table"
184 | ]
185 | },
186 | {
187 | "cell_type": "code",
188 | "execution_count": null,
189 | "metadata": {},
190 | "outputs": [],
191 | "source": [
192 | "demand_table.iloc[:,0].to_dict()"
193 | ]
194 | },
195 | {
196 | "cell_type": "code",
197 | "execution_count": null,
198 | "metadata": {},
199 | "outputs": [],
200 | "source": [
201 | "model.demand = pyo.Param(model.P, model.T, initialize = demand_table.iloc[:,0].to_dict())\n",
202 | "model.demand.pprint()"
203 | ]
204 | },
205 | {
206 | "cell_type": "code",
207 | "execution_count": null,
208 | "metadata": {},
209 | "outputs": [],
210 | "source": [
211 | "# workforce scalar parameters\n",
212 | "workforce_table = pd.read_excel('APP Parameters.xlsx',sheet_name='Workforce')\n",
213 | "CFire = workforce_table.loc[0,'firecost']\n",
214 | "CHire = workforce_table.loc[0,'hirecost']\n",
215 | "CReg = workforce_table.loc[0,'regularcost']\n",
216 | "COver = workforce_table.loc[0,'overtimecost']\n",
217 | "initial_workforce = workforce_table.loc[0,'initial workforce']\n",
218 | "max_Reg = workforce_table.loc[0,'maxregular']\n",
219 | "max_over = workforce_table.loc[0,'maxovertime']"
220 | ]
221 | }
222 | ],
223 | "metadata": {
224 | "kernelspec": {
225 | "display_name": "Python 3",
226 | "language": "python",
227 | "name": "python3"
228 | },
229 | "language_info": {
230 | "codemirror_mode": {
231 | "name": "ipython",
232 | "version": 3
233 | },
234 | "file_extension": ".py",
235 | "mimetype": "text/x-python",
236 | "name": "python",
237 | "nbconvert_exporter": "python",
238 | "pygments_lexer": "ipython3",
239 | "version": "3.12.0"
240 | }
241 | },
242 | "nbformat": 4,
243 | "nbformat_minor": 2
244 | }
245 |
--------------------------------------------------------------------------------
/Python-Nested-List-Indexing.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alisattarii/Modeling-and-optimization-in-Python---2024-/e0e8739b6561f51749779d7a432f47661f09cafe/Python-Nested-List-Indexing.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Modeling and optimization in-Python-2024
2 | Teaching modeling and optimization in Python (2024)
3 |
--------------------------------------------------------------------------------
/book-ex2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alisattarii/Modeling-and-optimization-in-Python---2024-/e0e8739b6561f51749779d7a432f47661f09cafe/book-ex2.png
--------------------------------------------------------------------------------
/or1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alisattarii/Modeling-and-optimization-in-Python---2024-/e0e8739b6561f51749779d7a432f47661f09cafe/or1.jpg
--------------------------------------------------------------------------------
/python-list.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alisattarii/Modeling-and-optimization-in-Python---2024-/e0e8739b6561f51749779d7a432f47661f09cafe/python-list.png
--------------------------------------------------------------------------------
/table.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/alisattarii/Modeling-and-optimization-in-Python---2024-/e0e8739b6561f51749779d7a432f47661f09cafe/table.jpg
--------------------------------------------------------------------------------