Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save taiwotman/8a5842f3064cb3af899478fbc162c3e8 to your computer and use it in GitHub Desktop.
Save taiwotman/8a5842f3064cb3af899478fbc162c3e8 to your computer and use it in GitHub Desktop.
Capacity Optimization Heuristics for the Bus Transportation Problem
'''
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)
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)
'''
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")
'''
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