In the code below, I got the optimal value of a recursive function that I set.
Now I want to get the values that helped build it - I mean, know which choice (or a "ride" (this is my decision)) was used in each step, and return that also as a string/array/somehow.
thestory : i have 6 rides, in every step i need to choose whether to go on the ride im on again, or switch. every ride has a fun rate for each time i go on it and i want to maximize the fun. right now i have the optimal value but not the rides that i went on that led me to it
The function F is the main focus here.
import math
import numpy as np
#fun rate for each ride,per loop
def funPerRide(rideNum,loopsNum):
if rideNum==1:
return 0
if rideNum==2:
return 100-np.power((4*loopsNum-10),2)
if rideNum==3:
return 50-(1000*np.power(np.e,(-loopsNum/2))/(5*loopsNum+20))
if rideNum==4:
return 4*loopsNum
if rideNum==5:
return 2.5*np.power(loopsNum,2)
if rideNum==6:
return 50*np.power(np.e,(-2*loopsNum+4))
def F(totalTime,timeLeft,rideNum,loopOnCurrRide):
#time of line+operation of ride
totalTimePerRide={1:0,2:40,3:15,4:20,5:23,6:11}
#time of operation of rides
operationTimePerRide={1:0,2:4,3:5,4:8,5:3,6:6}
#unfeasable conditions:
if timeLeft<0:
return -np.inf
if timeLeft+loopOnCurrRide*operationTimePerRide[rideNum]>totalTime:
return -np.inf
if loopOnCurrRide>3:
return -np.inf
#edge condition
if timeLeft == 0:
return 0
#fun if i stay on the ride im on right now
staying = funPerRide(rideNum,loopOnCurrRide+1)-funPerRide(rideNum,loopOnCurrRide)+F(totalTime,timeLeft-operationTimePerRide[rideNum],rideNum,loopOnCurrRide+1)
#calculating fun if i switch to the maximum-fun-ride, that is not the ride im currently at
switching = -1
whichRide=-1
for i in range(1,7):
if i>rideNum:
switchOption = funPerRide(i,loopOnCurrRide)+F(totalTime,timeLeft-4.5-totalTimePerRide[i],i,1)
if switchOption>switching:
switching, whichRide=switchOption,i
#calculating maximum fun between switching and staying
maxval,maxride=max((staying,rideNum),(switching,whichRide))
path.append(maxride)
maxval=float(maxval)
return float(maxval)
path = []
print(F(120,120,1,0),path)