+ Näytä koodi- Piilota koodi
library(OpasnetUtils)
bus.routes <- opbase.data("Op_en5336")
buses <- data.frame()
for (i in levels(bus.routes$Route_id)) {
temp <- bus.routes[bus.routes$Route_id == i, c("Route_id", "Time_id", "Route_order", "Result")]
temp <- temp[order(temp$Route_order),]
for (j in 1:length(temp$Result)) {
temp2 <- data.frame(Route_id = temp$Route_id[1], Time_id = temp$Time_id[1], From = temp$Result[j], To = temp$Result[-j])
# delay between "stops" should be calculated, as well as the times when buses start; this is not needed when we assume that a bus can pick up
# anyone during an hour and deliver them anywhere on the route while running the route in both directions
#temp <- merge(temp, distance[,c("From", "To", "Result")])
#temp$Delay <- c(0, temp$Result[-nrow(temp)]) / 20 #speed
#temp$Time <- temp$Time + temp$Delay
buses <- rbind(buses, temp2)
}
}
trips <- opbase.data("Op_en2625")
trips.time.sum.margins <- data.frame(Time = seq(1, 24.8, 0.2), Time.hour = rep(1:24, each = 5))
trips <- merge(trips, trips.time.sum.margins)
trips <- tapply(trips$Result, trips[,c("Mode", "Time.hour", "From", "To")], sum)
trips <- as.data.frame(as.table(trips))
colnames(trips)[5] <- "Result"
trips.bus <- merge(trips[trips$Mode == "Public"], buses)
buses.per.stop <- tapply(trips.bus$Result, trips.bus[,c("From", "To", "Time.hour")], length)
buses.per.stop <- as.data.frame(as.table(buses.per.stop))
colnames(buses.per.stop)[4] <- "Buses.per.stop"
trips.bus.2 <- merge(trips[trips$Mode == "Public"], buses.per.stop)
trips.bus.2$Result <- trips.bus.2$Result / trips.bus.2$Buses.per.stop
trips.bus.2 <- merge(trips.bus.2, buses)
trips.bus.3 <- tapply(trips.bus.2$Result, trips.bus.2[,c("Route_id", "Time.hour")]) # Passengers per hour per bus route assuming one bus goes in
# both directions and given passengers are divided evenly among buses going into same destinations
trips.bus.3 <- as.data.frame(as.table(trips.bus.3))
trips.bus.3$Active <- trips.bus.3$Freq > minlevel # test whether number of passengers is higher than minumum required to run
trips.bus <- merge(trips.bus, trips.bus.3)
trips.possible <- tapply(trips.bus$Active, trips.bus[,c("Time.hour", "From", "To")], sum)
trips.possible <- as.data.frame(as.table(trips.possible))
trips.possible$Possible <- trips.possible$Freq > 0 # test whether at least 1 bus route that includes the considered trip is active
trips.bus.2 <- merge(trips[trips$Mode == "Public"], trips.possible)
distance <- opbase.data("Op_en5322") #include = 14299, exclude = 53098 What these should be in base2 ????!?!?
gc()
# Costs, need to be modified to fit the new scenario
test3 <- as.data.frame(as.table(tapply(test2$vehicle.kilometers, test2$Type, sum)))
colnames(test3) <- c("Type", "vehicle.kilometers")
fuel.cons.emis.CO2 <- data.frame(Type = c("n.full.8.cars", "n.full.4.cars", "n.4.cars.3.pas", "n.4.cars.2.pas", "n.4.cars.1.pas"),
Consumption = c(8.7, 5.7, 5.7, 5.7, 5.7) / 100, Emis.factor.CO2 = c(232, 153, 153, 153, 153))
emis.factor.PM <- 0.1 # gkm^-1
fuel.price <- 1.374 # 4.8. average cost of a liter of diesel fuel in Finland
maint.price <- 0.0582 # €km^-1
driver.salary <- 2313 / 160 * 1.35 # €h^-1
test3 <- merge(test3, fuel.cons.emis.CO2)
test3$Fuel.cost <- test3$vehicle.kilometers * test3$Consumption * fuel.price
test3$Maint.cost <- test3$vehicle.kilometers * maint.price
test3$Emis.CO2 <- test3$vehicle.kilometers * test3$Emis.factor.CO2
test3$Emis.PM <- test3$vehicle.kilometers * emis.factor.PM
test3$Driver.cost <- test3$vehicle.kilometers / speed * driver.salary
PM.lethality <- c(-7.223e-004, 5.640e-006, 4.228e-005, 5.987e-005, 8.013e-005, 1.150e-004, 2.037e-004, 2.939e-004, 3.598e-004, 4.132e-004,
4.640e-004, 5.139e-004, 5.662e-004, 6.233e-004, 6.854e-004, 7.577e-004, 8.441e-004, 9.519e-004, 1.093e-003, 1.314e-003, 2.805e-003)
PM.lethality <- median(PM.lethality) # deaths / kg
value.of.life <- (2e6 + 0.98e6) / 2 # runif(n, 0.98e6, 2e6) # € / death
emis.price.PM <- PM.lethality * value.of.life # 201.879 # €kg^-1
emis.price.CO2 <- 10e-3 # runif(n, 5e-3, 40e-3) # €kg^-1, the price of CO2 allowances fluctuates quite a bit, in 8.8.2011 they
# were going for 10.74 € per ton of CO2 equivalent
test3$CO2.cost <- test3$Emis.CO2 * emis.price.CO2
test3$PM.cost <- test3$Emis.PM * emis.price.PM
# bus emis: 44 - 54 * 10^-2 lkm^-1; ref: N. Nylund, K. Erkkilä, T. Hartikka 2007 http://www.vtt.fi/inf/pdf/tiedotteet/2007/T2372.pdf p.31
| |