Last active
January 13, 2022 14:27
-
-
Save taiwotman/4eb2f25991364af12e7df283d467594d to your computer and use it in GitHub Desktop.
Capacity Optimization Heuristic for the Bus Loading Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Capacity Optimization Heuristics Approach for the Bus Loading Problem", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyM0IuJzJl2s7FLQEadXqHM6", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
}, | |
"accelerator": "GPU" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/taiwotman/4eb2f25991364af12e7df283d467594d/capacity-optimization-heuristics-approach-for-the-bus-loading-problem.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": { | |
"id": "-sG3xQ3sJuVV" | |
}, | |
"outputs": [], | |
"source": [ | |
"import random \n", | |
"\n", | |
"'''\n", | |
" n - Seating capacity of the bus, which is an integer\n", | |
" p - a list comprising weights of the people in wait denoted \n", | |
" e.g. p = [50,300, 20, 32, 93]\n", | |
" w - The maximum weight that a bus can hold, which is an integer\n", | |
"'''\n", | |
"\n", | |
"### Implement the constraints function:\n", | |
"\n", | |
"def get_eligible_people(n, w, p):\n", | |
" sum = 0\n", | |
" for i in range(0, n):\n", | |
" sum = sum + p[i]\n", | |
" \n", | |
" if sum == w:\n", | |
" return sorted(p[0:n], reverse=True)\n", | |
"\n", | |
"def capacity_optimization_heuritics_approach(n, w, p):\n", | |
" seed = 10000 # seed to initialize the random number generator\n", | |
" arr_sort = [] ## An empty list to hold the sorted result lists\n", | |
"\n", | |
" '''\n", | |
" Create the solution sample space with random shuffle\n", | |
" '''\n", | |
" for i in range(seed): \n", | |
" random.shuffle(p)\n", | |
" arr = get_eligible_people(n, w, p)\n", | |
" if arr != None:\n", | |
" arr_sort.append(arr)\n", | |
"\n", | |
" '''\n", | |
" Deduplication process\n", | |
" '''\n", | |
" set_list = list(map(list, set(map(tuple, arr_sort))))\n", | |
" for item in set_list:\n", | |
" if item != None: print(item)\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
" '''\n", | |
" Provide the test case function\n", | |
" '''\n", | |
"\n", | |
"w = 800\n", | |
"p = [205, 150, 140, 170, 230, 90, 110, 250, 135]\n", | |
"\n", | |
"def do_tests(n,w,p):\n", | |
" try:\n", | |
" print( f\"If seating capacity n={n}\\n\")\n", | |
" print(f\"List p comprising weights of people that can enter the bus with maximum weight {w}:\\n\")\n", | |
" results = capacity_optimization_heuritics_approach(n, w, p)\n", | |
" if not results:\n", | |
" print(\"No solution found\")\n", | |
" except:\n", | |
" print(\"Value entered not allowed\")" | |
], | |
"metadata": { | |
"id": "FpNeiddVLWA2" | |
}, | |
"execution_count": 51, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# Test case 1\n", | |
"test_suites = [5, 4, 3, 10]\n", | |
"\n", | |
"for n in test_suites:\n", | |
" print(\"---\" * 10) \n", | |
" do_tests(n,w,p)\n", | |
"\n" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "dYL8M-b-OI3L", | |
"outputId": "e5f0570d-fe15-4d38-f176-da1d81286234" | |
}, | |
"execution_count": 52, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"------------------------------\n", | |
"If seating capacity n=5\n", | |
"\n", | |
"List p comprising weights of people that can enter the push with maximum weight 800:\n", | |
"\n", | |
"[230, 170, 150, 140, 110]\n", | |
"[230, 205, 140, 135, 90]\n", | |
"[205, 170, 150, 140, 135]\n", | |
"[250, 170, 150, 140, 90]\n", | |
"No solution found\n", | |
"------------------------------\n", | |
"If seating capacity n=4\n", | |
"\n", | |
"List p comprising weights of people that can enter the push with maximum weight 800:\n", | |
"\n", | |
"[250, 230, 170, 150]\n", | |
"No solution found\n", | |
"------------------------------\n", | |
"If seating capacity n=3\n", | |
"\n", | |
"List p comprising weights of people that can enter the push with maximum weight 800:\n", | |
"\n", | |
"No solution found\n", | |
"------------------------------\n", | |
"If seating capacity n=10\n", | |
"\n", | |
"List p comprising weights of people that can enter the push with maximum weight 800:\n", | |
"\n", | |
"Value entered not allowed\n" | |
] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment