Define parameters
operating_cost = [4000, 4200, 4800, 5800, 7200, 9000]
salvage_value = [10500, 7300, 5100, 3600, 2500, 1600]
replacement_cost = 15000
maintenance_cost = 3500
years = 5
Initialize DP table
C[t][a] will store the minimum cost for year t with drone age a
C = Array{Float64}(undef, years + 1, length(operating_cost))
Initialize end of planning horizon
for a in 1:length(operating_cost)
C[years + 1, a] = 0 # Boundary condition: No future costs after year 5
end
DP recursion to fill the table from the last year backwards
for t in years:-1:1
for a in 1:length(operating_cost)
# Cost of keeping the drone
keep_cost = operating_cost[a] + C[t + 1, min(a + 1, length(operating_cost))]
# Cost of performing maintenance
new_age_after_maintenance = max(1, a - 2)
total_maintenance_cost = maintenance_cost + operating_cost[new_age_after_maintenance]
maintenance_option_cost = total_maintenance_cost + C[t + 1, new_age_after_maintenance]
# Cost of replacing the drone
replace_cost = replacement_cost - salvage_value[a] + C[t + 1, 1]
# Store the minimum cost for year t and age a
C[t, a] = minimum([keep_cost, maintenance_option_cost, replace_cost])
end
end
Output the minimum cost to start from year 1 with the current age of 2 years
optimal_cost = C[1, 3] # Drone starts at age 2, so we look at the third column (age 2)
println("The minimum cumulative cost for the optimal replacement policy is: ", optimal_cost)
Display the DP table with intermediate calculations
println(“Dynamic Programming Table (Minimum Costs):”)
for t in 1:years+1
println("Year $t: ", C[t, :])
end
Traceback for Optimal Path
println(“\nTracing Optimal Replacement Strategy:”)
Define initial age at the start of the traceback section
current_age = 3 # Initial index for age 2 (third column in DP table)
for t in 1:years
keep_cost = operating_cost[current_age] + C[t + 1, min(current_age + 1, length(operating_cost))]
new_age_after_maintenance = max(1, current_age - 2)
maintenance_option_cost = maintenance_cost + operating_cost[new_age_after_maintenance] + C[t + 1, new_age_after_maintenance]
replace_cost = replacement_cost - salvage_value[current_age] + C[t + 1, 1]
# Determine the action taken at this stage
optimal_action_cost = C[t, current_age]
if optimal_action_cost == keep_cost
println("Year $t: Keep the drone (age ", current_age, ")")
current_age = min(current_age + 1, length(operating_cost))
elseif optimal_action_cost == maintenance_option_cost
println("Year $t: Perform maintenance (reset to age ", new_age_after_maintenance, ")")
current_age = new_age_after_maintenance
else
println("Year $t: Replace the drone (new age 1)")
current_age = 1
end
end
whenever I try this code running it gives error for age/ current_age not defined