├── 00-markdown.ipynb ├── 01-variables-expressions-statements.ipynb ├── 02-boolean-expressions.ipynb ├── 03-print & f-string.ipynb ├── 04-Data Structures.ipynb ├── 05-loops.ipynb ├── 06-list comprehension.ipynb ├── 07-function.ipynb ├── 08-Pyomo, Simple Model.ipynb ├── 09-Pyomo, Set & Param.ipynb ├── 10-Sigma in pyomo.ipynb ├── 11-Pandas for Data.ipynb ├── Python-Nested-List-Indexing.png ├── README.md ├── book-ex2.png ├── or1.jpg ├── python-list.png └── table.jpg /00-markdown.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "
\n", 8 | "در این بخش مروری بر ویژگی‌های \n", 9 | "زبان\n", 10 | "markdown\n", 11 | "می‌کنیم.\n", 12 | "ابتدا با شش نوع عنوان آشنا می‌شویم. سپس بررسی می‌کنیم که چگونه می‌توان بخشی از متن را \n", 13 | "ایتالیک\n", 14 | "یا\n", 15 | "بولد\n", 16 | "کرد.\n", 17 | "بعد از آن کار با لیست‌های ترتیبی و غیرترتیبی را نشان می‌دهیم.\n", 18 | "در انتها رسم جدول را واکاوی می‌کنیم.\n", 19 | "
" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "

\n", 27 | " عنوان\n", 28 | "

" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "
\n", 36 | "برای نمایش تیتر از نشانه\n", 37 | "#\n", 38 | "قبل از کلمات عنوان استفاده می‌کنیم.\n", 39 | "تعداد نشانه‌های یادشده حداکثر شش تا خواهد بود.\n", 40 | "به طوری که هر چه تعداد نشانه‌ها بیشتر شود، اندازه عنوان کوچکتر خواهد شد.\n", 41 | "
" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "```markdown\n", 49 | "# Header1\n", 50 | "## Header2\n", 51 | "### Header3\n", 52 | "#### Header4\n", 53 | "##### Header5\n", 54 | "###### Header6\n", 55 | "```" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "# Header1\n", 63 | "## Header2\n", 64 | "### Header3\n", 65 | "#### Header4\n", 66 | "##### Header5\n", 67 | "###### Header6" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "

\n", 75 | "قالب کلمات\n", 76 | "

" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "
\n", 84 | " کلمات می‌توانند ایتالیک یا بولد شوند. برای ایتالیک کردن بخشی از متن، قبل و بعد از آن نشانه * همانند زیر می‌گذاریم.\n", 85 | "
" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "```markdown\n", 93 | "*italic word*\n", 94 | "```" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "*italic*" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "
\n", 109 | "برای بلد کردن بخشی از متن، قبل و بعد از آن \n", 110 | " دو نشانه ستاره یعنی\n", 111 | " **\n", 112 | " می‌گذاریم.\n", 113 | "
" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "```markdown\n", 121 | "**bold word**\n", 122 | "```" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "**bold**" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "

\n", 137 | "لیست\n", 138 | "

" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "
\n", 146 | "برای درست کردن لیست ترتیبی از اعداد و برای درست کردن لیست غیرترتیبی از نشانه\n", 147 | "-\n", 148 | "استفاده می‌کنیم.\n", 149 | "
" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "```markdown\n", 157 | "1. item 1\n", 158 | "2. item 2\n", 159 | "3. item 3\n", 160 | "```" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": {}, 166 | "source": [ 167 | "1. item 1\n", 168 | "2. item 2\n", 169 | "3. item 3" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "```markdown\n", 177 | "- item\n", 178 | "- item\n", 179 | "- item\n", 180 | "```" 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "metadata": {}, 186 | "source": [ 187 | "- item\n", 188 | "- item\n", 189 | "- item" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "

\n", 197 | " جدول\n", 198 | "

" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "
\n", 206 | "برای رسم جدول همانند از نشانه | استفاده می‌کنیم. نشانه دو نقطه\n", 207 | " :\n", 208 | " برای وسط‌چین، راست‌چین، و چپ‌چین به کار می‌رود.\n", 209 | "
" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "```markdown\n", 217 | "|First Name|Last Name|Job|\n", 218 | "|---------:|:-------:|:--|\n", 219 | "|Ali|Sattari|teacher|\n", 220 | "|James|Brooks|Professor|\n", 221 | "```" 222 | ] 223 | }, 224 | { 225 | "cell_type": "markdown", 226 | "metadata": {}, 227 | "source": [ 228 | "|First Name|Last Name|Job|\n", 229 | "|---------:|:-------:|:--|\n", 230 | "|Ali|Sattari|teacher|\n", 231 | "|James|Brooks|Professor|" 232 | ] 233 | } 234 | ], 235 | "metadata": { 236 | "kernelspec": { 237 | "display_name": "Python 3", 238 | "language": "python", 239 | "name": "python3" 240 | }, 241 | "language_info": { 242 | "codemirror_mode": { 243 | "name": "ipython", 244 | "version": 3 245 | }, 246 | "file_extension": ".py", 247 | "mimetype": "text/x-python", 248 | "name": "python", 249 | "nbconvert_exporter": "python", 250 | "pygments_lexer": "ipython3", 251 | "version": "3.7.3" 252 | } 253 | }, 254 | "nbformat": 4, 255 | "nbformat_minor": 4 256 | } 257 | -------------------------------------------------------------------------------- /01-variables-expressions-statements.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": null, 15 | "metadata": { 16 | "slideshow": { 17 | "slide_type": "subslide" 18 | } 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "type(2)" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": { 29 | "slideshow": { 30 | "slide_type": "subslide" 31 | } 32 | }, 33 | "outputs": [], 34 | "source": [ 35 | "type(2.5)" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": { 42 | "slideshow": { 43 | "slide_type": "subslide" 44 | } 45 | }, 46 | "outputs": [], 47 | "source": [ 48 | "type(\"Hello, World!\")" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": { 55 | "slideshow": { 56 | "slide_type": "subslide" 57 | } 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "type(\"2\")" 62 | ] 63 | }, 64 | { 65 | "cell_type": "markdown", 66 | "metadata": {}, 67 | "source": [ 68 | "\n", 69 | "\n", 70 | "\n", 71 | "
\n", 72 | "\n", 73 | "* یک رشته دنباله ای از کاراکترها است\n", 74 | "* حتی یک کاراکتر هم رشته نامیده می شود\n", 75 | "* حتی یک رشته خالی همچنان یک رشته است\n", 76 | "* با' ' یا \" \" شناسایی می شود\n", 77 | "\n", 78 | "
" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": { 85 | "slideshow": { 86 | "slide_type": "subslide" 87 | } 88 | }, 89 | "outputs": [], 90 | "source": [ 91 | "type(True)" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "metadata": { 98 | "slideshow": { 99 | "slide_type": "subslide" 100 | } 101 | }, 102 | "outputs": [], 103 | "source": [ 104 | "type(False)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": { 110 | "slideshow": { 111 | "slide_type": "fragment" 112 | } 113 | }, 114 | "source": [ 115 | "
\n", 116 | "متغیر نامی است که به یک مقدار نسبت داده می‌شود تا بعداً بتوان به آن اشاره کرد. \n", 117 | "
" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 1, 123 | "metadata": { 124 | "slideshow": { 125 | "slide_type": "slide" 126 | } 127 | }, 128 | "outputs": [], 129 | "source": [ 130 | "message = \"You are learning Python programming language\"" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 8, 136 | "metadata": { 137 | "slideshow": { 138 | "slide_type": "fragment" 139 | } 140 | }, 141 | "outputs": [], 142 | "source": [ 143 | "nstudents = 23" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 9, 149 | "metadata": { 150 | "slideshow": { 151 | "slide_type": "fragment" 152 | } 153 | }, 154 | "outputs": [], 155 | "source": [ 156 | "PI = 3.14" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "
\n", 164 | "سوال:\n", 165 | " خروجی چاپ شده چند است؟\n", 166 | "
" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": { 172 | "slideshow": { 173 | "slide_type": "slide" 174 | } 175 | }, 176 | "source": [ 177 | "```python\n", 178 | "x = 2\n", 179 | "x = x + 2\n", 180 | "print(x)\n", 181 | "```" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": { 187 | "slideshow": { 188 | "slide_type": "slide" 189 | } 190 | }, 191 | "source": [ 192 | "
\n", 193 | " نام‌گذاری‌ها در پایتون از قواعد خاصی پیروی می‌کند. در ادامه این قوانین آمده است.\n", 194 | "
" 195 | ] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": {}, 200 | "source": [ 201 | "
\n", 202 | "\n", 208 | "
" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": {}, 215 | "outputs": [], 216 | "source": [ 217 | "21number = 15" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": null, 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [ 226 | "number@! = 15" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": null, 232 | "metadata": {}, 233 | "outputs": [], 234 | "source": [ 235 | "1_number = 15" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 13, 241 | "metadata": {}, 242 | "outputs": [], 243 | "source": [ 244 | "_1number = 15" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": null, 250 | "metadata": {}, 251 | "outputs": [], 252 | "source": [ 253 | "Number = 15\n", 254 | "number = 12\n", 255 | "Number , number" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": null, 261 | "metadata": {}, 262 | "outputs": [], 263 | "source": [ 264 | "n = (Number , number)\n", 265 | "type(n)" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": null, 271 | "metadata": {}, 272 | "outputs": [], 273 | "source": [ 274 | "num1 = 15\n", 275 | "num2 = 9.86\n", 276 | "name = 'ZNU'\n", 277 | "num1,num2,name" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": null, 283 | "metadata": {}, 284 | "outputs": [], 285 | "source": [ 286 | "num1 , num2 ,name = 15, 9.86, 'ZNU'\n", 287 | "num1,num2,name" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": null, 293 | "metadata": {}, 294 | "outputs": [], 295 | "source": [ 296 | "num1\n", 297 | "num2\n", 298 | "name" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": null, 304 | "metadata": {}, 305 | "outputs": [], 306 | "source": [ 307 | "print(num1)\n", 308 | "print(num2)\n", 309 | "print(name)" 310 | ] 311 | }, 312 | { 313 | "cell_type": "markdown", 314 | "metadata": { 315 | "slideshow": { 316 | "slide_type": "slide" 317 | } 318 | }, 319 | "source": [ 320 | "
\n", 321 | "عملگرهای محاسباتی در ادامه آمده آست. \n", 322 | "
" 323 | ] 324 | }, 325 | { 326 | "cell_type": "markdown", 327 | "metadata": { 328 | "slideshow": { 329 | "slide_type": "fragment" 330 | } 331 | }, 332 | "source": [ 333 | "|Operator| Operation|\n", 334 | "|:---:|:---:|\n", 335 | "| + | Addition |\n", 336 | "| - | Subtraction |\n", 337 | "| * | Multiplication |\n", 338 | "| / | Division |\n", 339 | "| //| Floor Division|\n", 340 | "| ** | Power |\n", 341 | "| % | Remainder |" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": null, 347 | "metadata": { 348 | "slideshow": { 349 | "slide_type": "subslide" 350 | } 351 | }, 352 | "outputs": [], 353 | "source": [ 354 | "5 + 2" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": null, 360 | "metadata": { 361 | "slideshow": { 362 | "slide_type": "subslide" 363 | } 364 | }, 365 | "outputs": [], 366 | "source": [ 367 | "5 - 2" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": null, 373 | "metadata": { 374 | "slideshow": { 375 | "slide_type": "subslide" 376 | } 377 | }, 378 | "outputs": [], 379 | "source": [ 380 | "5 * 2" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": null, 386 | "metadata": { 387 | "slideshow": { 388 | "slide_type": "subslide" 389 | } 390 | }, 391 | "outputs": [], 392 | "source": [ 393 | "5 / 2" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": null, 399 | "metadata": { 400 | "slideshow": { 401 | "slide_type": "subslide" 402 | } 403 | }, 404 | "outputs": [], 405 | "source": [ 406 | "type(5 / 1)" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": null, 412 | "metadata": { 413 | "slideshow": { 414 | "slide_type": "subslide" 415 | } 416 | }, 417 | "outputs": [], 418 | "source": [ 419 | "5 ** 2" 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": null, 425 | "metadata": { 426 | "slideshow": { 427 | "slide_type": "subslide" 428 | } 429 | }, 430 | "outputs": [], 431 | "source": [ 432 | "5 // 2" 433 | ] 434 | }, 435 | { 436 | "cell_type": "code", 437 | "execution_count": null, 438 | "metadata": { 439 | "slideshow": { 440 | "slide_type": "subslide" 441 | } 442 | }, 443 | "outputs": [], 444 | "source": [ 445 | "type(5 // 2)" 446 | ] 447 | }, 448 | { 449 | "cell_type": "code", 450 | "execution_count": null, 451 | "metadata": { 452 | "slideshow": { 453 | "slide_type": "subslide" 454 | } 455 | }, 456 | "outputs": [], 457 | "source": [ 458 | "5 % 2" 459 | ] 460 | }, 461 | { 462 | "cell_type": "markdown", 463 | "metadata": { 464 | "slideshow": { 465 | "slide_type": "slide" 466 | } 467 | }, 468 | "source": [ 469 | "
\n", 470 | "سوال:\n", 471 | "جذر عدد پنج را محاسبه کنید.\n", 472 | "
" 473 | ] 474 | }, 475 | { 476 | "cell_type": "code", 477 | "execution_count": null, 478 | "metadata": { 479 | "slideshow": { 480 | "slide_type": "fragment" 481 | } 482 | }, 483 | "outputs": [], 484 | "source": [ 485 | "5 ** 0.5" 486 | ] 487 | }, 488 | { 489 | "cell_type": "code", 490 | "execution_count": null, 491 | "metadata": { 492 | "slideshow": { 493 | "slide_type": "fragment" 494 | } 495 | }, 496 | "outputs": [], 497 | "source": [ 498 | "import math\n", 499 | "math.sqrt(5)" 500 | ] 501 | }, 502 | { 503 | "cell_type": "markdown", 504 | "metadata": { 505 | "slideshow": { 506 | "slide_type": "slide" 507 | } 508 | }, 509 | "source": [ 510 | "
\n", 511 | "با استفاده از دستور\n", 512 | " input\n", 513 | " می‌توان از کاربر ورودی دریافت کرد.\n", 514 | "
" 515 | ] 516 | }, 517 | { 518 | "cell_type": "code", 519 | "execution_count": 16, 520 | "metadata": { 521 | "slideshow": { 522 | "slide_type": "fragment" 523 | } 524 | }, 525 | "outputs": [], 526 | "source": [ 527 | "name = input(\"Enter your name: \")" 528 | ] 529 | }, 530 | { 531 | "cell_type": "code", 532 | "execution_count": 17, 533 | "metadata": {}, 534 | "outputs": [ 535 | { 536 | "data": { 537 | "text/plain": [ 538 | "'ali'" 539 | ] 540 | }, 541 | "execution_count": 17, 542 | "metadata": {}, 543 | "output_type": "execute_result" 544 | } 545 | ], 546 | "source": [ 547 | "name" 548 | ] 549 | } 550 | ], 551 | "metadata": { 552 | "celltoolbar": "Slideshow", 553 | "kernelspec": { 554 | "display_name": "Python 3", 555 | "language": "python", 556 | "name": "python3" 557 | }, 558 | "language_info": { 559 | "codemirror_mode": { 560 | "name": "ipython", 561 | "version": 3 562 | }, 563 | "file_extension": ".py", 564 | "mimetype": "text/x-python", 565 | "name": "python", 566 | "nbconvert_exporter": "python", 567 | "pygments_lexer": "ipython3", 568 | "version": "3.12.5" 569 | } 570 | }, 571 | "nbformat": 4, 572 | "nbformat_minor": 4 573 | } 574 | -------------------------------------------------------------------------------- /02-boolean-expressions.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 | "" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "
\n", 24 | " در ادامه عملگرهای منطقی آمده است.\n", 25 | "
" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "- and\n", 33 | "- or\n", 34 | "- not\n", 35 | "- is" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "True and True" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "True and False" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "False and False" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "True or True" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": null, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "True or False" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": null, 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "False or False" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": null, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [ 98 | "not True" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "metadata": {}, 105 | "outputs": [], 106 | "source": [ 107 | "not False" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "metadata": {}, 113 | "source": [ 114 | "
\n", 115 | " سوال:\n", 116 | " خروجی برنامه زیر چیست؟\n", 117 | "
" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "```python\n", 125 | "x = 10\n", 126 | "if x > 0:\n", 127 | " print('x is positive')\n", 128 | "```" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "x = 10\n", 138 | "if x > 0:\n", 139 | " print('x is positive')" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "|Operator|Description|\n", 147 | "|:---:|:----|\n", 148 | "|==|If the values of two operands are equal, then the condition becomes true|\n", 149 | "|!=|If values of two operands are not equal, then condition becomes true|\n", 150 | "|>| If the value of left operand is greater than the value of right operand, then condition becomes true|\n", 151 | "|<|If the value of left operand is less than the value of right operand, then condition becomes true|\n", 152 | "|>=|If the value of left operand is greater than or equal to the value of right operand, then condition becomes true|\n", 153 | "|<=|If the value of left operand is less than or equal to the value of right operand, then condition becomes true|" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": 1, 159 | "metadata": {}, 160 | "outputs": [ 161 | { 162 | "data": { 163 | "text/plain": [ 164 | "False" 165 | ] 166 | }, 167 | "execution_count": 1, 168 | "metadata": {}, 169 | "output_type": "execute_result" 170 | } 171 | ], 172 | "source": [ 173 | "5 == 3" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 2, 179 | "metadata": {}, 180 | "outputs": [ 181 | { 182 | "data": { 183 | "text/plain": [ 184 | "True" 185 | ] 186 | }, 187 | "execution_count": 2, 188 | "metadata": {}, 189 | "output_type": "execute_result" 190 | } 191 | ], 192 | "source": [ 193 | "5 != 3" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 3, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "data": { 203 | "text/plain": [ 204 | "True" 205 | ] 206 | }, 207 | "execution_count": 3, 208 | "metadata": {}, 209 | "output_type": "execute_result" 210 | } 211 | ], 212 | "source": [ 213 | "5 > 3" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 4, 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "data": { 223 | "text/plain": [ 224 | "False" 225 | ] 226 | }, 227 | "execution_count": 4, 228 | "metadata": {}, 229 | "output_type": "execute_result" 230 | } 231 | ], 232 | "source": [ 233 | "5 < 3" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 5, 239 | "metadata": {}, 240 | "outputs": [ 241 | { 242 | "data": { 243 | "text/plain": [ 244 | "True" 245 | ] 246 | }, 247 | "execution_count": 5, 248 | "metadata": {}, 249 | "output_type": "execute_result" 250 | } 251 | ], 252 | "source": [ 253 | "5 >= 3" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 6, 259 | "metadata": {}, 260 | "outputs": [ 261 | { 262 | "data": { 263 | "text/plain": [ 264 | "True" 265 | ] 266 | }, 267 | "execution_count": 6, 268 | "metadata": {}, 269 | "output_type": "execute_result" 270 | } 271 | ], 272 | "source": [ 273 | "5 >= 5" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 7, 279 | "metadata": {}, 280 | "outputs": [ 281 | { 282 | "data": { 283 | "text/plain": [ 284 | "False" 285 | ] 286 | }, 287 | "execution_count": 7, 288 | "metadata": {}, 289 | "output_type": "execute_result" 290 | } 291 | ], 292 | "source": [ 293 | "5 <= 3" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 8, 299 | "metadata": {}, 300 | "outputs": [ 301 | { 302 | "data": { 303 | "text/plain": [ 304 | "True" 305 | ] 306 | }, 307 | "execution_count": 8, 308 | "metadata": {}, 309 | "output_type": "execute_result" 310 | } 311 | ], 312 | "source": [ 313 | "5 <= 5" 314 | ] 315 | } 316 | ], 317 | "metadata": { 318 | "kernelspec": { 319 | "display_name": "Python 3", 320 | "language": "python", 321 | "name": "python3" 322 | }, 323 | "language_info": { 324 | "codemirror_mode": { 325 | "name": "ipython", 326 | "version": 3 327 | }, 328 | "file_extension": ".py", 329 | "mimetype": "text/x-python", 330 | "name": "python", 331 | "nbconvert_exporter": "python", 332 | "pygments_lexer": "ipython3", 333 | "version": "3.12.5" 334 | } 335 | }, 336 | "nbformat": 4, 337 | "nbformat_minor": 4 338 | } 339 | -------------------------------------------------------------------------------- /03-print & f-string.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 | " f\n", 19 | "استفاده می‌کنیم. در داخل رشته هر آن‌چه که بین\n", 20 | " {\n", 21 | " و\n", 22 | " }\n", 23 | " قرار بگیرد، به عنوان متغیر محسوب می‌شود.\n", 24 | "
" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "metadata": {}, 31 | "outputs": [ 32 | { 33 | "name": "stdout", 34 | "output_type": "stream", 35 | "text": [ 36 | "I'm Ali Sattari\n" 37 | ] 38 | } 39 | ], 40 | "source": [ 41 | "print(\"I'm Ali Sattari\")" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 2, 47 | "metadata": {}, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "industrial \tengineering\n" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "# \\t --> Tab\n", 59 | "print(\"industrial \\tengineering\")" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 3, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "industrial \n", 72 | "engineering\n" 73 | ] 74 | } 75 | ], 76 | "source": [ 77 | "#\\n --> Next line\n", 78 | "print(\"industrial \\nengineering\")" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 4, 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "name": "stdout", 88 | "output_type": "stream", 89 | "text": [ 90 | "The current value of x is {x}\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "x = 5\n", 96 | "print('The current value of x is {x}')" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 5, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "The current value of x is 157\n" 109 | ] 110 | } 111 | ], 112 | "source": [ 113 | "x = 157\n", 114 | "print(f'The current value of x is {x}')" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 6, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "name": "stdout", 124 | "output_type": "stream", 125 | "text": [ 126 | "Ali Sattari is teaching Otimizatipn in Python course\n" 127 | ] 128 | } 129 | ], 130 | "source": [ 131 | "name = 'Ali Sattari'\n", 132 | "course = 'Otimizatipn in Python'\n", 133 | "print(f\"{name} is teaching {course} course\")" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 7, 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "name": "stdout", 143 | "output_type": "stream", 144 | "text": [ 145 | "Ali Sattari is teaching Otimizatipn in Python course\n" 146 | ] 147 | } 148 | ], 149 | "source": [ 150 | "name = 'Ali Sattari'\n", 151 | "course = 'Otimizatipn in Python'\n", 152 | "print(name + ' is teaching '+ course + ' course')" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 8, 158 | "metadata": {}, 159 | "outputs": [ 160 | { 161 | "name": "stdout", 162 | "output_type": "stream", 163 | "text": [ 164 | "GPA scores above 17 are called A grade\n" 165 | ] 166 | } 167 | ], 168 | "source": [ 169 | "grade = 'A'\n", 170 | "mark = 17\n", 171 | "\n", 172 | "print (f'GPA scores above {mark} are called {grade} grade')" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 9, 178 | "metadata": {}, 179 | "outputs": [ 180 | { 181 | "name": "stdout", 182 | "output_type": "stream", 183 | "text": [ 184 | "6\n", 185 | "8\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "a , b = 6 , 8\n", 191 | "print(a)\n", 192 | "print(b)" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 10, 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "name": "stdout", 202 | "output_type": "stream", 203 | "text": [ 204 | "6 8\n" 205 | ] 206 | } 207 | ], 208 | "source": [ 209 | "a , b = 6 , 8\n", 210 | "print(a,b)" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 11, 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "name": "stdout", 220 | "output_type": "stream", 221 | "text": [ 222 | "68\n" 223 | ] 224 | } 225 | ], 226 | "source": [ 227 | "a , b = 6 , 8\n", 228 | "print(a,b, sep='')" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 12, 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "name": "stdout", 238 | "output_type": "stream", 239 | "text": [ 240 | "6\n", 241 | "8\n", 242 | "6 * 8\n", 243 | "6 --> 8\n" 244 | ] 245 | } 246 | ], 247 | "source": [ 248 | "a , b = 6 , 8\n", 249 | "\n", 250 | "\n", 251 | "print(a, b, sep = '\\n')\n", 252 | "\n", 253 | "print(a, b, sep=' * ')\n", 254 | "\n", 255 | "print(a, b, sep=' --> ')\n" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": null, 261 | "metadata": {}, 262 | "outputs": [], 263 | "source": [] 264 | } 265 | ], 266 | "metadata": { 267 | "kernelspec": { 268 | "display_name": "Python 3", 269 | "language": "python", 270 | "name": "python3" 271 | }, 272 | "language_info": { 273 | "codemirror_mode": { 274 | "name": "ipython", 275 | "version": 3 276 | }, 277 | "file_extension": ".py", 278 | "mimetype": "text/x-python", 279 | "name": "python", 280 | "nbconvert_exporter": "python", 281 | "pygments_lexer": "ipython3", 282 | "version": "3.12.5" 283 | } 284 | }, 285 | "nbformat": 4, 286 | "nbformat_minor": 4 287 | } 288 | -------------------------------------------------------------------------------- /04-Data Structures.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 | "Syntax: List = [value1, value2, ..., valueN] " 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": null, 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "my_list = [3,4,6,10,8]" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "data": { 43 | "text/plain": [ 44 | "3" 45 | ] 46 | }, 47 | "metadata": {}, 48 | "output_type": "display_data" 49 | } 50 | ], 51 | "source": [ 52 | "my_list = [3,4,6,10,8]\n", 53 | "my_list[0]" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "8" 65 | ] 66 | }, 67 | "metadata": {}, 68 | "output_type": "display_data" 69 | } 70 | ], 71 | "source": [ 72 | "my_list = [3,4,6,10,8]\n", 73 | "my_list[-1]" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 2, 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "data": { 83 | "text/plain": [ 84 | "[3, 4]" 85 | ] 86 | }, 87 | "execution_count": 2, 88 | "metadata": {}, 89 | "output_type": "execute_result" 90 | } 91 | ], 92 | "source": [ 93 | "my_list = [3,4,6,10,8]\n", 94 | "# my_list[start:end:step]\n", 95 | "#تا یکی مونده به آخری\n", 96 | "my_list[0:2]" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "data": { 106 | "text/plain": [ 107 | "[3, 10]" 108 | ] 109 | }, 110 | "metadata": {}, 111 | "output_type": "display_data" 112 | } 113 | ], 114 | "source": [ 115 | "my_list = [3,4,6,10,8]\n", 116 | "my_list[::3]" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "data": { 126 | "text/plain": [ 127 | "[8, 10, 6, 4, 3]" 128 | ] 129 | }, 130 | "metadata": {}, 131 | "output_type": "display_data" 132 | } 133 | ], 134 | "source": [ 135 | "my_list = [3,4,6,10,8]\n", 136 | "my_list[::-1]" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "### Nested Lists" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [ 159 | "L = ['a', 'b', ['cc', 'dd', ['eee', 'fff']], 'g', 'h']" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": null, 165 | "metadata": {}, 166 | "outputs": [ 167 | { 168 | "name": "stdout", 169 | "output_type": "stream", 170 | "text": [ 171 | "['cc', 'dd', ['eee', 'fff']]\n" 172 | ] 173 | } 174 | ], 175 | "source": [ 176 | "print(L[2])" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": null, 182 | "metadata": {}, 183 | "outputs": [ 184 | { 185 | "name": "stdout", 186 | "output_type": "stream", 187 | "text": [ 188 | "['eee', 'fff']\n" 189 | ] 190 | } 191 | ], 192 | "source": [ 193 | "print(L[2][2])" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": null, 199 | "metadata": {}, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "eee\n" 206 | ] 207 | } 208 | ], 209 | "source": [ 210 | "print(L[2][2][0])" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": null, 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "data": { 220 | "text/plain": [ 221 | "[3, 'Ali', 6, 10, 8]" 222 | ] 223 | }, 224 | "metadata": {}, 225 | "output_type": "display_data" 226 | } 227 | ], 228 | "source": [ 229 | "my_list = [3,4,6,10,8]\n", 230 | "my_list[1] = 'Ali'\n", 231 | "my_list" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": null, 237 | "metadata": {}, 238 | "outputs": [ 239 | { 240 | "data": { 241 | "text/plain": [ 242 | "[3, 'Ali', 6, 10, 8, 3, 'Ali', 6, 10, 8]" 243 | ] 244 | }, 245 | "metadata": {}, 246 | "output_type": "display_data" 247 | } 248 | ], 249 | "source": [ 250 | "my_list = [3, 'Ali', 6, 10, 8] * 2\n", 251 | "my_list" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": null, 257 | "metadata": {}, 258 | "outputs": [ 259 | { 260 | "data": { 261 | "text/plain": [ 262 | "[3, 'Ali', 6, 10, 8, 3, 'Ali', 6, 10, 8, 'Sattari', 20]" 263 | ] 264 | }, 265 | "metadata": {}, 266 | "output_type": "display_data" 267 | } 268 | ], 269 | "source": [ 270 | "my_list = my_list + ['Sattari',20] \n", 271 | "my_list" 272 | ] 273 | }, 274 | { 275 | "cell_type": "markdown", 276 | "metadata": {}, 277 | "source": [ 278 | "

\n", 279 | "متد های لیست\n", 280 | "

" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": null, 286 | "metadata": {}, 287 | "outputs": [ 288 | { 289 | "data": { 290 | "text/plain": [ 291 | "[3, 'Ali', 6, 10, 8, 'Sattari']" 292 | ] 293 | }, 294 | "metadata": {}, 295 | "output_type": "display_data" 296 | } 297 | ], 298 | "source": [ 299 | "my_list = [3, 'Ali', 6, 10, 8]\n", 300 | "my_list.append('Sattari')\n", 301 | "my_list" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "metadata": {}, 308 | "outputs": [ 309 | { 310 | "data": { 311 | "text/plain": [ 312 | "['Sattari', 8, 10, 6, 'Ali', 3]" 313 | ] 314 | }, 315 | "metadata": {}, 316 | "output_type": "display_data" 317 | } 318 | ], 319 | "source": [ 320 | "my_list.reverse()\n", 321 | "my_list" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": null, 327 | "metadata": {}, 328 | "outputs": [ 329 | { 330 | "data": { 331 | "text/plain": [ 332 | "['Sattari', ['Shalqam', 'piyaz'], 8, 10, 6, 'Ali', 3]" 333 | ] 334 | }, 335 | "metadata": {}, 336 | "output_type": "display_data" 337 | } 338 | ], 339 | "source": [ 340 | "my_list.insert(1,['Shalqam','piyaz'])\n", 341 | "my_list" 342 | ] 343 | }, 344 | { 345 | "cell_type": "markdown", 346 | "metadata": {}, 347 | "source": [ 348 | "
\n", 349 | "درست همانند رشته‌ها، تابع\n", 350 | " len\n", 351 | " تعداد اعضا را بر می‌گرداند.\n", 352 | "
" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": null, 358 | "metadata": {}, 359 | "outputs": [ 360 | { 361 | "data": { 362 | "text/plain": [ 363 | "4" 364 | ] 365 | }, 366 | "metadata": {}, 367 | "output_type": "display_data" 368 | } 369 | ], 370 | "source": [ 371 | "len(general_list)" 372 | ] 373 | }, 374 | { 375 | "cell_type": "markdown", 376 | "metadata": {}, 377 | "source": [ 378 | "
\n", 379 | "می‌توان دو لیست را با یکدیگر ادغام کرد.\n", 380 | "
" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": null, 386 | "metadata": {}, 387 | "outputs": [ 388 | { 389 | "name": "stdout", 390 | "output_type": "stream", 391 | "text": [ 392 | "['Alireza', 41, 12.75, True, 1, 2, 3, 4, 5]\n", 393 | "[1, 2, 3, 4, 5]\n", 394 | "['Alireza', 41, 12.75, True]\n" 395 | ] 396 | } 397 | ], 398 | "source": [ 399 | "print(general_list + num_list)\n", 400 | "print(num_list)\n", 401 | "print(general_list)" 402 | ] 403 | }, 404 | { 405 | "cell_type": "markdown", 406 | "metadata": {}, 407 | "source": [ 408 | "
\n", 409 | "می‌توان لیست را با نشانه\n", 410 | "*\n", 411 | "تکرار کرد.\n", 412 | "
" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": null, 418 | "metadata": {}, 419 | "outputs": [ 420 | { 421 | "data": { 422 | "text/plain": [ 423 | "[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]" 424 | ] 425 | }, 426 | "metadata": {}, 427 | "output_type": "display_data" 428 | } 429 | ], 430 | "source": [ 431 | "num_list * 3" 432 | ] 433 | }, 434 | { 435 | "cell_type": "markdown", 436 | "metadata": {}, 437 | "source": [ 438 | "

\n", 439 | "تاپل\n", 440 | "

" 441 | ] 442 | }, 443 | { 444 | "cell_type": "markdown", 445 | "metadata": {}, 446 | "source": [ 447 | "Syntax: Tuple = (Value1, Value2, ... , ValueN)\n", 448 | "* همون لیست های تغییر ناپذیر" 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": null, 454 | "metadata": {}, 455 | "outputs": [], 456 | "source": [ 457 | "my_tuple = (3, 'Ali', 6, 10, 8)" 458 | ] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "execution_count": null, 463 | "metadata": {}, 464 | "outputs": [ 465 | { 466 | "data": { 467 | "text/plain": [ 468 | "3" 469 | ] 470 | }, 471 | "metadata": {}, 472 | "output_type": "display_data" 473 | } 474 | ], 475 | "source": [ 476 | "my_tuple[0]" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": null, 482 | "metadata": {}, 483 | "outputs": [ 484 | { 485 | "ename": "TypeError", 486 | "evalue": "'tuple' object does not support item assignment", 487 | "output_type": "error", 488 | "traceback": [ 489 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n", 490 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)\n", 491 | "\u001b[1;32me:\\University\\Modeling and optimization in Python\\OptimizationwithPython\\codes\\Unit 02-Data Structures.ipynb Cell 30\u001b[0m line \u001b[0;36m1\n", 492 | "\u001b[1;32m----> 1\u001b[0m my_tuple[\u001b[39m0\u001b[39;49m] \u001b[39m=\u001b[39m \u001b[39m10\u001b[39m\n", 493 | "\n", 494 | "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 495 | ] 496 | } 497 | ], 498 | "source": [ 499 | "my_tuple[0] = 10" 500 | ] 501 | }, 502 | { 503 | "cell_type": "markdown", 504 | "metadata": {}, 505 | "source": [ 506 | "

\n", 507 | "مجموعه ها\n", 508 | "

" 509 | ] 510 | }, 511 | { 512 | "cell_type": "markdown", 513 | "metadata": {}, 514 | "source": [ 515 | "Syntax: Set = {Value1, Value2, ... , ValueN}\n", 516 | "* فقط منحصر به فرد\n", 517 | "* تغییر ناپذیر\n", 518 | "* بدون ترتیب\n", 519 | "* بدون آدرسی دهی اندیسی" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": null, 525 | "metadata": {}, 526 | "outputs": [ 527 | { 528 | "data": { 529 | "text/plain": [ 530 | "{1, 10.8, 2, 3, 'Ali', 'Sattari'}" 531 | ] 532 | }, 533 | "metadata": {}, 534 | "output_type": "display_data" 535 | } 536 | ], 537 | "source": [ 538 | "my_set = {1,2,2,3,3,3,3,'Sattari','Sattari',10.8,'Ali'}\n", 539 | "my_set" 540 | ] 541 | }, 542 | { 543 | "cell_type": "code", 544 | "execution_count": null, 545 | "metadata": {}, 546 | "outputs": [ 547 | { 548 | "ename": "TypeError", 549 | "evalue": "'set' object is not subscriptable", 550 | "output_type": "error", 551 | "traceback": [ 552 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n", 553 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)\n", 554 | "\u001b[1;32me:\\University\\Modeling and optimization in Python\\OptimizationwithPython\\codes\\Unit 02-Data Structures.ipynb Cell 34\u001b[0m line \u001b[0;36m1\n", 555 | "\u001b[1;32m----> 1\u001b[0m my_set[\u001b[39m1\u001b[39;49m]\n", 556 | "\n", 557 | "\u001b[1;31mTypeError\u001b[0m: 'set' object is not subscriptable" 558 | ] 559 | } 560 | ], 561 | "source": [ 562 | "my_set[1]" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": null, 568 | "metadata": {}, 569 | "outputs": [ 570 | { 571 | "ename": "TypeError", 572 | "evalue": "'set' object does not support item assignment", 573 | "output_type": "error", 574 | "traceback": [ 575 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n", 576 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)\n", 577 | "\u001b[1;32me:\\University\\Modeling and optimization in Python\\OptimizationwithPython\\codes\\Unit 02-Data Structures.ipynb Cell 35\u001b[0m line \u001b[0;36m1\n", 578 | "\u001b[1;32m----> 1\u001b[0m my_set[\u001b[39m1\u001b[39;49m] \u001b[39m=\u001b[39m \u001b[39m4\u001b[39m\n", 579 | "\n", 580 | "\u001b[1;31mTypeError\u001b[0m: 'set' object does not support item assignment" 581 | ] 582 | } 583 | ], 584 | "source": [ 585 | "my_set[1] = 4" 586 | ] 587 | }, 588 | { 589 | "cell_type": "code", 590 | "execution_count": null, 591 | "metadata": {}, 592 | "outputs": [ 593 | { 594 | "data": { 595 | "text/plain": [ 596 | "{1, 10.8, 2, 3, 'Ali', 'Sattari'}" 597 | ] 598 | }, 599 | "metadata": {}, 600 | "output_type": "display_data" 601 | } 602 | ], 603 | "source": [ 604 | "s1 = set([1,2,2,3,3,3,3,'Sattari','Sattari',10.8,'Ali'])\n", 605 | "s1" 606 | ] 607 | }, 608 | { 609 | "cell_type": "code", 610 | "execution_count": null, 611 | "metadata": {}, 612 | "outputs": [ 613 | { 614 | "data": { 615 | "text/plain": [ 616 | "{'O', 'a', 'i', 'm', 'n', 'o', 'p', 't', 'z'}" 617 | ] 618 | }, 619 | "metadata": {}, 620 | "output_type": "display_data" 621 | } 622 | ], 623 | "source": [ 624 | "s2 = set('Optimization')\n", 625 | "s2" 626 | ] 627 | }, 628 | { 629 | "cell_type": "code", 630 | "execution_count": null, 631 | "metadata": {}, 632 | "outputs": [ 633 | { 634 | "data": { 635 | "text/plain": [ 636 | "[10, 15, 45, 78, 98, 52]" 637 | ] 638 | }, 639 | "metadata": {}, 640 | "output_type": "display_data" 641 | } 642 | ], 643 | "source": [ 644 | "# یک لیست\n", 645 | "my_list = [10,15,45,78,98,52]\n", 646 | "my_list" 647 | ] 648 | }, 649 | { 650 | "cell_type": "code", 651 | "execution_count": null, 652 | "metadata": {}, 653 | "outputs": [ 654 | { 655 | "data": { 656 | "text/plain": [ 657 | "{10, 15, 45, 52, 78, 98}" 658 | ] 659 | }, 660 | "metadata": {}, 661 | "output_type": "display_data" 662 | } 663 | ], 664 | "source": [ 665 | "# تبدیل لیست به مجموعه\n", 666 | "l_to_s = set(my_list)\n", 667 | "l_to_s" 668 | ] 669 | }, 670 | { 671 | "cell_type": "code", 672 | "execution_count": null, 673 | "metadata": {}, 674 | "outputs": [ 675 | { 676 | "data": { 677 | "text/plain": [ 678 | "{10, 15, 45, 52, 78, 98}" 679 | ] 680 | }, 681 | "metadata": {}, 682 | "output_type": "display_data" 683 | } 684 | ], 685 | "source": [ 686 | "# تبدیل مستقیم\n", 687 | "s3 = set([10,15,45,78,98,52])\n", 688 | "s3" 689 | ] 690 | }, 691 | { 692 | "cell_type": "code", 693 | "execution_count": null, 694 | "metadata": {}, 695 | "outputs": [ 696 | { 697 | "data": { 698 | "text/plain": [ 699 | "[1, 2, 3, 10.8, 'Sattari', 'Ali']" 700 | ] 701 | }, 702 | "metadata": {}, 703 | "output_type": "display_data" 704 | } 705 | ], 706 | "source": [ 707 | "#تبدیل مجموعه به لیست \n", 708 | "list1 = list(s1)\n", 709 | "list1" 710 | ] 711 | }, 712 | { 713 | "cell_type": "code", 714 | "execution_count": null, 715 | "metadata": {}, 716 | "outputs": [ 717 | { 718 | "data": { 719 | "text/plain": [ 720 | "['A', 'l', 'i']" 721 | ] 722 | }, 723 | "metadata": {}, 724 | "output_type": "display_data" 725 | } 726 | ], 727 | "source": [ 728 | "list2 = list('Ali')\n", 729 | "list2" 730 | ] 731 | }, 732 | { 733 | "cell_type": "code", 734 | "execution_count": null, 735 | "metadata": {}, 736 | "outputs": [ 737 | { 738 | "data": { 739 | "text/plain": [ 740 | "(1, 2, 3, 10.8, 'Sattari', 'Ali')" 741 | ] 742 | }, 743 | "metadata": {}, 744 | "output_type": "display_data" 745 | } 746 | ], 747 | "source": [ 748 | "tuple1 = tuple(list1)\n", 749 | "tuple1" 750 | ] 751 | }, 752 | { 753 | "cell_type": "code", 754 | "execution_count": null, 755 | "metadata": {}, 756 | "outputs": [ 757 | { 758 | "data": { 759 | "text/plain": [ 760 | "{1, 10.8, 2, 3, 'Ali', 'N', 'Sattari', 'U', 'Z'}" 761 | ] 762 | }, 763 | "metadata": {}, 764 | "output_type": "display_data" 765 | } 766 | ], 767 | "source": [ 768 | "# اجتماع مجموعه ها\n", 769 | "s1 = set([1, 2, 3, 10.8, 'Sattari', 'Ali'])\n", 770 | "s2 = set('ZNU')\n", 771 | "s1.union(s2)\n", 772 | "# s1 | s2" 773 | ] 774 | }, 775 | { 776 | "cell_type": "code", 777 | "execution_count": null, 778 | "metadata": {}, 779 | "outputs": [ 780 | { 781 | "data": { 782 | "text/plain": [ 783 | "{2, 3, 10.8}" 784 | ] 785 | }, 786 | "metadata": {}, 787 | "output_type": "display_data" 788 | } 789 | ], 790 | "source": [ 791 | "# اشتراک مجموعه ها\n", 792 | "s1 = set([1, 2, 3, 10.8, 'Sattari', 'Ali'])\n", 793 | "s2 = set([3,45,67,2,10.8,36,55])\n", 794 | "# s1.intersection(s2)\n", 795 | "s1 & s2" 796 | ] 797 | }, 798 | { 799 | "cell_type": "code", 800 | "execution_count": null, 801 | "metadata": {}, 802 | "outputs": [ 803 | { 804 | "data": { 805 | "text/plain": [ 806 | "{1, 10.8, 3, 'Ali', 'Sattari'}" 807 | ] 808 | }, 809 | "metadata": {}, 810 | "output_type": "display_data" 811 | } 812 | ], 813 | "source": [ 814 | "# تفاضل مجموعه ها\n", 815 | "s1 = set([1, 2, 3, 10.8, 'Sattari', 'Ali'])\n", 816 | "s2 = set([45,67,2,36,55])\n", 817 | "# s1.difference(s2)\n", 818 | "s1 - s2" 819 | ] 820 | }, 821 | { 822 | "cell_type": "markdown", 823 | "metadata": {}, 824 | "source": [ 825 | "

\n", 826 | "دیکشنری\n", 827 | "

" 828 | ] 829 | }, 830 | { 831 | "cell_type": "markdown", 832 | "metadata": {}, 833 | "source": [ 834 | "\n", 835 | "**D =** { key : value , key : value , ... , key : value } \n", 836 | "\n" 837 | ] 838 | }, 839 | { 840 | "cell_type": "code", 841 | "execution_count": null, 842 | "metadata": {}, 843 | "outputs": [ 844 | { 845 | "data": { 846 | "text/plain": [ 847 | "{'Brazil': 'Rio', 'Germany': 'Berlin', 'Iran': 'Tehran'}" 848 | ] 849 | }, 850 | "metadata": {}, 851 | "output_type": "display_data" 852 | } 853 | ], 854 | "source": [ 855 | "my_dict = {'Brazil' : 'Rio', 'Germany' : 'Berlin', 'Iran' : 'Tehran'}\n", 856 | "my_dict" 857 | ] 858 | }, 859 | { 860 | "cell_type": "code", 861 | "execution_count": null, 862 | "metadata": {}, 863 | "outputs": [ 864 | { 865 | "data": { 866 | "text/plain": [ 867 | "{'Brazil': 'Rio', 'Germany': 'Berlin', 'Iran': 'Tehran'}" 868 | ] 869 | }, 870 | "metadata": {}, 871 | "output_type": "display_data" 872 | } 873 | ], 874 | "source": [ 875 | "# another way to create a dictionary\n", 876 | "my_dict = dict( Brazil = 'Rio', Germany = 'Berlin', Iran = 'Tehran')\n", 877 | "my_dict" 878 | ] 879 | }, 880 | { 881 | "cell_type": "code", 882 | "execution_count": null, 883 | "metadata": {}, 884 | "outputs": [ 885 | { 886 | "data": { 887 | "text/plain": [ 888 | "{('p1', 1): 200, ('p1', 2): 300, ('p2', 1): 100, ('p2', 2): 400}" 889 | ] 890 | }, 891 | "metadata": {}, 892 | "output_type": "display_data" 893 | } 894 | ], 895 | "source": [ 896 | "demands = {('p1',1):200, ('p1',2):300, ('p2',1):100 , ('p2',2):400}\n", 897 | "demands" 898 | ] 899 | }, 900 | { 901 | "cell_type": "code", 902 | "execution_count": null, 903 | "metadata": {}, 904 | "outputs": [ 905 | { 906 | "data": { 907 | "text/plain": [ 908 | "300" 909 | ] 910 | }, 911 | "metadata": {}, 912 | "output_type": "display_data" 913 | } 914 | ], 915 | "source": [ 916 | "demands[('p1',2)]" 917 | ] 918 | }, 919 | { 920 | "cell_type": "code", 921 | "execution_count": null, 922 | "metadata": {}, 923 | "outputs": [ 924 | { 925 | "name": "stdout", 926 | "output_type": "stream", 927 | "text": [ 928 | "{('p1', 1): 200, ('p1', 2): 800, ('p2', 1): 100, ('p2', 2): 400}\n" 929 | ] 930 | } 931 | ], 932 | "source": [ 933 | "demands[('p1',2)] = 800\n", 934 | "print(demands)" 935 | ] 936 | }, 937 | { 938 | "cell_type": "code", 939 | "execution_count": null, 940 | "metadata": {}, 941 | "outputs": [ 942 | { 943 | "data": { 944 | "text/plain": [ 945 | "{('p1', 1): 200,\n", 946 | " ('p1', 2): 800,\n", 947 | " ('p2', 1): 100,\n", 948 | " ('p2', 2): 400,\n", 949 | " ('p3', 1): 500}" 950 | ] 951 | }, 952 | "metadata": {}, 953 | "output_type": "display_data" 954 | } 955 | ], 956 | "source": [ 957 | "demands[('p3',1)] = 500\n", 958 | "demands" 959 | ] 960 | }, 961 | { 962 | "cell_type": "code", 963 | "execution_count": null, 964 | "metadata": {}, 965 | "outputs": [ 966 | { 967 | "data": { 968 | "text/plain": [ 969 | "True" 970 | ] 971 | }, 972 | "metadata": {}, 973 | "output_type": "display_data" 974 | } 975 | ], 976 | "source": [ 977 | "('p1',1) in demands" 978 | ] 979 | }, 980 | { 981 | "cell_type": "code", 982 | "execution_count": null, 983 | "metadata": {}, 984 | "outputs": [ 985 | { 986 | "data": { 987 | "text/plain": [ 988 | "dict_items([(('p1', 1), 200), (('p1', 2), 800), (('p2', 1), 100), (('p2', 2), 400), (('p3', 1), 500)])" 989 | ] 990 | }, 991 | "metadata": {}, 992 | "output_type": "display_data" 993 | } 994 | ], 995 | "source": [ 996 | "demands.items()" 997 | ] 998 | }, 999 | { 1000 | "cell_type": "code", 1001 | "execution_count": null, 1002 | "metadata": {}, 1003 | "outputs": [ 1004 | { 1005 | "data": { 1006 | "text/plain": [ 1007 | "[(('p1', 1), 200),\n", 1008 | " (('p1', 2), 800),\n", 1009 | " (('p2', 1), 100),\n", 1010 | " (('p2', 2), 400),\n", 1011 | " (('p3', 1), 500)]" 1012 | ] 1013 | }, 1014 | "metadata": {}, 1015 | "output_type": "display_data" 1016 | } 1017 | ], 1018 | "source": [ 1019 | "list(demands.items())" 1020 | ] 1021 | }, 1022 | { 1023 | "cell_type": "code", 1024 | "execution_count": null, 1025 | "metadata": {}, 1026 | "outputs": [ 1027 | { 1028 | "data": { 1029 | "text/plain": [ 1030 | "dict_keys([('p1', 1), ('p1', 2), ('p2', 1), ('p2', 2), ('p3', 1)])" 1031 | ] 1032 | }, 1033 | "metadata": {}, 1034 | "output_type": "display_data" 1035 | } 1036 | ], 1037 | "source": [ 1038 | "demands.keys()" 1039 | ] 1040 | }, 1041 | { 1042 | "cell_type": "code", 1043 | "execution_count": null, 1044 | "metadata": {}, 1045 | "outputs": [], 1046 | "source": [ 1047 | "list(demands.keys())" 1048 | ] 1049 | }, 1050 | { 1051 | "cell_type": "code", 1052 | "execution_count": null, 1053 | "metadata": {}, 1054 | "outputs": [], 1055 | "source": [ 1056 | "demands.values()" 1057 | ] 1058 | }, 1059 | { 1060 | "cell_type": "code", 1061 | "execution_count": null, 1062 | "metadata": {}, 1063 | "outputs": [], 1064 | "source": [ 1065 | "list(demands.values())" 1066 | ] 1067 | }, 1068 | { 1069 | "cell_type": "code", 1070 | "execution_count": null, 1071 | "metadata": {}, 1072 | "outputs": [], 1073 | "source": [ 1074 | "demands.get(('p1',1))\n", 1075 | "# demands[('p1',1)]" 1076 | ] 1077 | }, 1078 | { 1079 | "cell_type": "code", 1080 | "execution_count": null, 1081 | "metadata": {}, 1082 | "outputs": [], 1083 | "source": [ 1084 | "demands[('p1',3)]" 1085 | ] 1086 | }, 1087 | { 1088 | "cell_type": "code", 1089 | "execution_count": null, 1090 | "metadata": {}, 1091 | "outputs": [], 1092 | "source": [ 1093 | "demands.get(('p1',3))" 1094 | ] 1095 | }, 1096 | { 1097 | "cell_type": "code", 1098 | "execution_count": null, 1099 | "metadata": {}, 1100 | "outputs": [], 1101 | "source": [ 1102 | "demands.get(('p1',3) , 0)" 1103 | ] 1104 | }, 1105 | { 1106 | "cell_type": "code", 1107 | "execution_count": null, 1108 | "metadata": {}, 1109 | "outputs": [], 1110 | "source": [ 1111 | "demands.get(('p1',2) , 0)" 1112 | ] 1113 | } 1114 | ], 1115 | "metadata": { 1116 | "kernelspec": { 1117 | "display_name": "Python 3", 1118 | "language": "python", 1119 | "name": "python3" 1120 | }, 1121 | "language_info": { 1122 | "codemirror_mode": { 1123 | "name": "ipython", 1124 | "version": 3 1125 | }, 1126 | "file_extension": ".py", 1127 | "mimetype": "text/x-python", 1128 | "name": "python", 1129 | "nbconvert_exporter": "python", 1130 | "pygments_lexer": "ipython3", 1131 | "version": "3.12.5" 1132 | } 1133 | }, 1134 | "nbformat": 4, 1135 | "nbformat_minor": 4 1136 | } 1137 | -------------------------------------------------------------------------------- /05-loops.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 | "for\n", 19 | "و\n", 20 | "دیگری \n", 21 | "حلقه\n", 22 | "while.\n", 23 | "حلقه\n", 24 | "for\n", 25 | "برای پیمایش اعضای یک دنباله به کار می‌رود.\n", 26 | "
" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": {}, 32 | "source": [ 33 | "
\n", 34 | "ساختار کلی حلقه\n", 35 | "for\n", 36 | "به صورت زیر است.\n", 37 | "
" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "```python\n", 45 | "for item in seq:\n", 46 | " statement1\n", 47 | " statement2\n", 48 | " ...\n", 49 | "```" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 1, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "1\n", 62 | "2\n", 63 | "3\n", 64 | "4\n", 65 | "5\n", 66 | "6\n", 67 | "7\n", 68 | "8\n", 69 | "9\n", 70 | "10\n" 71 | ] 72 | } 73 | ], 74 | "source": [ 75 | "num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", 76 | "for i in num_list:\n", 77 | " print(i)" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "
\n", 85 | "سوال:\n", 86 | " برنامه‌ای بنویسید که اعضای زوج یک لیست شامل اعداد صحیح را چاپ کند.\n", 87 | "
" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 2, 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "2\n", 100 | "6\n", 101 | "4\n", 102 | "12\n" 103 | ] 104 | } 105 | ], 106 | "source": [ 107 | "num_list = [1, 3, 2, 6, 5, 4, 7, 12]\n", 108 | "for num in num_list:\n", 109 | " if num % 2 == 0:\n", 110 | " print(num)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 3, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "name": "stdout", 120 | "output_type": "stream", 121 | "text": [ 122 | "1 is odd.\n", 123 | "3 is odd.\n", 124 | "2\n", 125 | "6\n", 126 | "5 is odd.\n", 127 | "4\n", 128 | "7 is odd.\n", 129 | "12\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "for num in num_list:\n", 135 | " if num % 2 == 0:\n", 136 | " print(num)\n", 137 | " else:\n", 138 | " print(f\"{num} is odd.\")" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "
\n", 146 | "سوال:\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 | "\"drawing\"" 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 --------------------------------------------------------------------------------