Created
December 17, 2022 03:11
-
-
Save rahulbhadani/a119916d26e251b939b817dde03ad032 to your computer and use it in GitHub Desktop.
Pythonic Psuedo-code of Dynamic Programming
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 minimize_fuel_consumption(num_timesteps, velocity, acceleration, fuel_consumption_function): | |
# Initialize the cost function with a large value for all states | |
cost = [[float('inf') for _ in range(len(velocity))] for _ in range(num_timesteps)] | |
# Define the set of actions | |
actions = ['accelerate', 'decelerate', 'maintain constant speed'] | |
# Initialize the cost for the initial state to zero | |
cost[0][0] = 0 | |
# Iterate over all timesteps | |
for t in range(1, num_timesteps): | |
# Iterate over all states | |
for i, (v, a) in enumerate(zip(velocity, acceleration)): | |
# Iterate over all predecessor states | |
for j, (v_prev, a_prev) in enumerate(zip(velocity, acceleration)): | |
# Check if the current state can be reached from the predecessor state by applying an action | |
if (a_prev == a and v_prev <= v) or (a_prev < a): | |
# Update the cost for the current state if it is lower than the current value | |
cost[t][i] = min(cost[t][i], cost[t-1][j] + fuel_consumption_function(v, a)) | |
# The optimal sequence of actions can be obtained by tracking the actions taken at each time step | |
action_sequence = [] | |
s = (velocity[0], acceleration[0]) # Initialize the state to the initial state | |
for t in range(num_timesteps-1, 0, -1): | |
# Find the action that minimizes the cost to reach the current state | |
min_cost = float('inf') | |
best_action = None | |
for action in actions: | |
# Compute the successor state for the current action | |
if action == 'accelerate': | |
s_next = (s[0]+1, s[1]+1) | |
elif action == 'decelerate': | |
s_next = (s[0]-1, s[1]-1) | |
else: | |
s_next = (s[0], s[1]) | |
# Check if the successor state is a valid state and has a lower cost than the current minimum | |
if s_next[0] >= 0 and s_next[0] < len(velocity) and s_next[1] >= 0 and s_next[1] < len(acceleration) and cost[t][s_next[0]] < min_cost: | |
min_cost = cost[t][s_next[0]] | |
best_action = action | |
s = s_next | |
action_sequence.append(best_action) | |
return action_sequence[::-1] # Reverse the action sequence to get the sequence in the correct order |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment