Last active
January 13, 2022 14:29
-
-
Save taiwotman/8a5842f3064cb3af899478fbc162c3e8 to your computer and use it in GitHub Desktop.
Capacity Optimization Heuristics for the Bus Transportation 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
''' | |
n - Seating capacity of the bus, which is an integer | |
p - List comprising weights of the people in wait | |
e.g. p = [50, 300, 20, 32, 93] | |
w - The maximum weight that a bus can hold, which is an integer | |
''' | |
### Implement the objective function and define the constraints: | |
def get_eligible_people(n, w, p): | |
sum = 0 | |
for i in range(0, n): | |
sum = sum + p[i] | |
if sum == w: | |
return sorted(p[0:n], reverse=True) |
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
def capacity_optimization_heuritics_approach(n, w, p): | |
seed = 10000 # seed to initialize the random number generator | |
arr_sort = [] # An empty list to hold the sorted result lists | |
''' | |
Create the solution sample space with random shuffle | |
''' | |
for i in range(seed): | |
random.shuffle(p) | |
arr = get_eligible_people(n, w, p) | |
if arr != None: | |
arr_sort.append(arr) | |
''' | |
Deduplication process | |
''' | |
set_list = list(map(list, set(map(tuple, arr_sort)))) | |
for item in set_list: | |
if item != None: print(item) |
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
''' | |
Provide the test case function | |
''' | |
w = 800 | |
p = [205, 150, 140, 170, 230, 90, 110, 250, 135] | |
def do_tests(n,w,p): | |
try: | |
print( f"If seating capacity n={n}\n") | |
print(f"List p comprising weights of people that can enter the bus with maximum weight {w}:\n") | |
capacity_optimization_heuritics_approach(n, w, p) | |
except: | |
print("Value entered not allowed") |
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
''' | |
Test suite | |
''' | |
test_suite = [5, 4, 3, 10] | |
count = 1 | |
for n in test_suite: | |
print(f"\nTest case {count}") | |
print("---" * 10) | |
do_tests(n, w, p) | |
count = count + 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment