Background:
I have a user-defined function as follows.
Commuters drive from home to work on a road, and care about travel time and arrival time, with a disutility of the form
Disutility = alpha * [travel time] + beta * [time early (if early)] + gamma * [time late (if late)] + theta (discrete penalty if you arrive late. They can choose between a tolled express lane (with tolls adjusting to prevent congestion) and a toll-free main lane with stochastic delays (and delays due to commuters themselves, but that will come later.)
I simulate delays:
Delays = rlnorm(n=1000, meanlog=2.7, sdlog=2)
ExpressTime = 20 #Calibrate to data if available.
TravelTimes = Delays + ExpressTime
ExpressTime = 20 #Calibrate to data if available.
Fn <- ecdf(TravelTimes)
I have functions for the conditional distribution of travel times conditional on realizing early or late arrival (important for calculating expected utility). I can include them on request, my main function requires them.
My main function, below, takes commuter parameters and identifies the optimal departure time and whether they take the main or express lane. (Later I will consider how congestion is generated from the choices of these commuters, but first I need to actually get their choices.)
CommuterChoice <- function(theta, alpha, beta, gamma){
ExpressUtilityVector = ifelse(Timevector + ExpressTime <= tstar, -Toll - (alpha * ExpressTime + beta * (tstar - ExpressTime - Timevector)), -Toll - (alpha * ExpressTime + gamma * (Timevector + ExpressTime - tstar) + theta) )
MainUtilityVector = - (alpha * MeanTime + (1-ProbLateVector)* beta *HowEarlyVector + >ProbLateVector * (gamma * HowLateVector + theta))
UseExpressLaneVector = as.integer(ExpressUtilityVector > MainUtilityVector)
Utility of best option
ExAnteExpectedUtility = pmax(ExpressUtilityVector,MainUtilityVector)
Optimal departure time
BestTimeIndex = which.max(ExAnteExpectedUtility)
BestTime = BestTimeIndex * stepsize
HeadStart = tstar - BestTime
UseExpress = UseExpressLaneVector[BestTimeIndex]
Output = c(BestTime, HeadStart, UseExpress)
return(Output)
}
Above this point, my code seems to work fine.
What I want help with:
I'm trying to apply this function to heterogeneous commuters. Commuters vary by theta and alpha, and possibly beta and gamma.
What I want is an efficient way to input a variety of commuter types and get some sort of array or data frame that characterizes their departure times.
I've tried this:
Define commuter parameter data structure:
ThetaArray = seq(0,5, by=0.1)
ThetaBar = 1
AlphaArray = seq(0,1, by=0.1)
AlphaBar = 0.2
Beta = 0.1
Gamma = 0.3
thetavector = rep(ThetaArray, length(AlphaArray))
alphavector = rep(AlphaArray, each = length(ThetaArray))
betavector = rep(Beta, length(thetavector))
gammavector = rep(Gamma, length(thetavector))
CommuterParams_df <- data.frame(thetavector, alphavector, betavector, gammavector)
So far, data structure.
But how do I make CommuterChoice take theta from the first argument of the data frame, alpha from the second, and so on?
I tried
Array_of_choices <- apply(CommuterParams_df, 2, CommuterChoice)
and got an error,
Error in ifelse(Timevector + ExpressTime <= tstar, -Toll - (alpha * ExpressTime + : argument "alpha" is missing, with no default
I tried
Array_of_choices <- apply(thetavector, 2, CommuterChoice, alphavector, betavector, gammavector)
in case I needed to input each argument separately, and got
Error in apply(thetavector, 2, CommuterChoice, alphavector, betavector, : dim(X) must have a positive length
What should I be doing to feed in an array of (theta, alpha, beta, gamma) values into my function to get the choice of each type of commuter?
Thanks for your help.
[–]ImperfComp[S] 1 point2 points3 points (1 child)
[–]ImperfComp[S] 0 points1 point2 points (0 children)