Composite traffic model: Difference between revisions

From Opasnet
Jump to navigation Jump to search
(→‎R model: new revision)
(new version, more streamlined but some bugs persist)
Line 20: Line 20:
# Trip aggregator, sampled passenger data as input
# Trip aggregator, sampled passenger data as input


times <- 0
n.intervals.per.h <- 5
from <- 1
 
trips.sample <- data.frame()
trips.next <- data.frame()
trips.next <- data.frame()
trips.left <- data.frame()
trips.left <- data.frame()
Line 28: Line 27:
trips.secondary <- data.frame()
trips.secondary <- data.frame()


t.interval <- 1
times <- seq(1, 25, 1 / n.intervals.per.h)


trips.sample$Secondary <- 0
library(OpasnetBaseUtils)


for (i in 1:length(times)) {
roads <- op_baseGetData("opasnet_base", "Op_en2634", apply.utf8 = FALSE)
colnames(roads)[6] <- "Through"
 
trips.locs <- op_baseGetLocs("opasnet_base", "Op_en2625", apply.utf8 = FALSE)
 
for (i in 1:(length(times) - 2)) {
if(i == 1) {
if(i == 1) {
trips.left <- trips.sample[trips.sample$Time <= times[i],]
trips.sample.1 <- op_baseGetData("opasnet_base", "Op_en2625", include = trips.locs$loc_id[trips.locs$ind == "Time" &
trips.locs$loc == times[1]])
trips.sample.1$Secondary <- 0
} else {
} else {
trips.left <- trips.sample[trips.sample$Time <= times[i] & trips.sample$Time > times[i] - t.interval,]
trips.sample.1 <- trips.sample.2
trips.sample.1 <- merge(trips.sample.1, trips.secondary, all.x = TRUE)
trips.sample.1$Secondary[is.na(trips.sample.1$Secondary)] <- 0
}
}
trips.sample.2 <- op_baseGetData("opasnet_base", "Op_en2625", include = trips.locs$loc_id[trips.locs$ind == "Time" &
trips.locs$loc == times[i + 1]])
# Optimizer main code
# Optimizer main code
Line 44: Line 55:
sub.optimal.d.trips <- 0
sub.optimal.d.trips <- 0
optimal.d.trips <- (trips.left$Result + trips.left$Secondary) %/% 4 * 4
optimal.d.trips <- (trips.sample.1$Result + trips.sample.1$Secondary) %/% 4 * 4
sub.optimal.d.trips <- ifelse(trips.left$Secondary - optimal.d.trips > 0, trips.left$Result + trips.left$Secondary - optimal.d.trips, 0)
sub.optimal.d.trips <- ifelse(trips.sample.1$Secondary - optimal.d.trips > 0, trips.sample.1$Result + trips.sample.1$Secondary - optimal.d.trips, 0)
busiest <- tapply(trips$Result[trips$Time == times[i+1]], trips$From[trips$Time == times[i+1]], sum)
busiest <- tapply(trips.sample.2$Result, trips.sample.2$From, sum)
busiest <- sort(busiest, decreasing = TRUE)
busiest <- sort(busiest, decreasing = TRUE)
condition <- trips.left$Result + trips.left$Secondary - optimal.d.trips - sub.optimal.d.trips > 0
condition <- trips.sample.1$Result + trips.sample.1$Secondary - optimal.d.trips - sub.optimal.d.trips > 0
trips.next <- merge(trips.left[condition, c("From","To")], roads)
trips.next <- merge(trips.sample.1[condition, c("From","To")], roads[,c("From","To","Through")], all.x = TRUE)
trips.next$Through <- match(trips.next$Through, names(busiest))
trips.next$Through <- match(trips.next$Through, names(busiest))
checkpoints <- tapply(trips.next$Through, trips.next[,c("From", "To")], min)
checkpoints <- tapply(trips.next$Through, trips.next[,c("From", "To")], min)
colnames(trips.left)[colnames(trips.left) == "Freq"] <- "Checkpoint"
trips.sample.1 <- merge(trips.sample.1, as.data.frame(as.table(checkpoints)), all.x = TRUE)
trips.left <- merge(trips.left, as.data.frame(as.table(checkpoints)), all = TRUE)
colnames(trips.sample.1)[colnames(trips.sample.1) == "Freq"] <- "Checkpoint"
trips.sample.1$Checkpoint <- names(busiest)[trips.sample.1$Checkpoint]
# Take into account those that don't have a checkpoint
# Take into account those that don't have a checkpoint
condition2 <- condition & is.na(trips.left$Checkpoint)
condition2 <- is.na(trips.sample.1$Checkpoint)
condition3 <- trips.sample.1$Result + trips.sample.1$Secondary - optimal.d.trips - sub.optimal.d.trips > 0
no.transfer <- ifelse(condition2), (trips.left$Result + trips.left$Secondary -  
no.transfer <- ifelse(condition2 & condition3, (trips.sample.1$Result + trips.sample.1$Secondary -  
optimal.d.trips - sub.optimal.d.trips)[condition2], 0)
optimal.d.trips - sub.optimal.d.trips)[condition2 & condition3], 0)
trips.left$Optim.d.trips <- optimal.d.trips
trips.sample.1$Optim.d.trips <- optimal.d.trips
trips.left$Sub.optim.d.trips <- sub.optimal.d.trips
trips.sample.1$Sub.optim.d.trips <- sub.optimal.d.trips
trips.left$No.transfer <- no.transfer
trips.sample.1$No.transfer <- no.transfer
# Transfers
# Transfers
trips.left.trans <- data.frame(trips.left[!condition2, c("From", "Checkpoint", "To", "Time")],  
trips.left.trans <- data.frame(trips.sample.1[!condition2 & condition3, c("From", "Checkpoint", "To", "Time")],  
Transferred = (trips.left$Result + trips.left$Secondary - optimal.d.trips - sub.optimal.d.trips)[!condition2])
Transferred = (trips.sample.1$Result + trips.sample.1$Secondary - optimal.d.trips - sub.optimal.d.trips)[!condition2 & condition3])
colnames(trips.left.trans)[1] <- "From"
colnames(trips.left.trans)[1] <- "From"
colnames(trips.left.trans)[3] <- "Destination"
colnames(trips.left.trans)[3] <- "Destination"
colnames(trips.left.trans)[2] <- "To"
colnames(trips.left.trans)[2] <- "To"
trips.left <- merge(trips.left, trips.left.trans[,-c(3,4)], all = TRUE)
trips.sample.1 <- merge(trips.sample.1, as.data.frame(as.table(tapply(trips.left.trans$Transferred, trips.left.trans[,c("From","To")], sum))),
trips.left$Transferred[is.na(trips.left$Transferred)] <- 0
all.x = TRUE)
colnames(trips.sample.1)[colnames(trips.sample.1) %in% "Freq"] <- "Transferred"
trips.sample.1$Transferred[is.na(trips.sample.1$Transferred)] <- 0
# Now divide passengers to cars
# Now divide passengers to cars
n.full.8.cars <- (trips.left$Optim.d.trip + trips.left$Sub.optim.d.trip + trips.left$No.transfer + trips.left$Transferred) %/% 8
n.full.8.cars <- (trips.sample.1$Optim.d.trip + trips.sample.1$Sub.optim.d.trip + trips.sample.1$No.transfer + trips.sample.1$Transferred) %/% 8
n.full.4.cars <- (trips.left$Optim.d.trip + trips.left$Sub.optim.d.trip + trips.left$No.transfer + trips.left$Transferred -  
n.full.4.cars <- (trips.sample.1$Optim.d.trip + trips.sample.1$Sub.optim.d.trip + trips.sample.1$No.transfer + trips.sample.1$Transferred -  
n.full.8.cars * 8) %/% 4
n.full.8.cars * 8) %/% 4
n.4.cars.3.pas <- (trips.left$Optim.d.trip + trips.left$Sub.optim.d.trip + trips.left$No.transfer + trips.left$Transferred -
n.4.cars.3.pas <- (trips.sample.1$Optim.d.trip + trips.sample.1$Sub.optim.d.trip + trips.sample.1$No.transfer + trips.sample.1$Transferred -
n.full.8.cars * 8 - n.full.4.cars * 4) %/% 3
n.full.8.cars * 8 - n.full.4.cars * 4) %/% 3
n.4.cars.2.pas <- (trips.left$Optim.d.trip + trips.left$Sub.optim.d.trip + trips.left$No.transfer + trips.left$Transferred -
n.4.cars.2.pas <- (trips.sample.1$Optim.d.trip + trips.sample.1$Sub.optim.d.trip + trips.sample.1$No.transfer + trips.sample.1$Transferred -
n.full.8.cars * 8 - n.full.4.cars * 4 - n.4.cars.3.pas * 3) %/% 2
n.full.8.cars * 8 - n.full.4.cars * 4 - n.4.cars.3.pas * 3) %/% 2
n.4.cars.1.pas <- trips.left$Optim.d.trip + trips.left$Sub.optim.d.trip + trips.left$No.transfer + trips.left$Transferred -
n.4.cars.1.pas <- trips.sample.1$Optim.d.trip + trips.sample.1$Sub.optim.d.trip + trips.sample.1$No.transfer + trips.sample.1$Transferred -
n.full.8.cars * 8 - n.full.4.cars * 4 - n.4.cars.3.pas * 3 - n.4.cars.2.pas * 2
n.full.8.cars * 8 - n.full.4.cars * 4 - n.4.cars.3.pas * 3 - n.4.cars.2.pas * 2
d8 <- ifelse(trips.left$Optim.d.trip + trips.left$Sub.optim.d.trip + trips.left$No.transfer < 8 * n.full.8.cars,  
d8 <- ifelse(trips.sample.1$Optim.d.trip + trips.sample.1$Sub.optim.d.trip + trips.sample.1$No.transfer < 8 * n.full.8.cars,  
trips.left$Optim.d.trip + trips.left$Sub.optim.d.trip + trips.left$No.transfer, 8 * n.full.8.cars)
trips.sample.1$Optim.d.trip + trips.sample.1$Sub.optim.d.trip + trips.sample.1$No.transfer, 8 * n.full.8.cars)
d4 <- ifelse(trips.left$Optim.d.trip + trips.left$Sub.optim.d.trip + trips.left$No.transfer - d8 < 4 * n.full.4.cars,  
d4 <- ifelse(trips.sample.1$Optim.d.trip + trips.sample.1$Sub.optim.d.trip + trips.sample.1$No.transfer - d8 < 4 * n.full.4.cars,  
trips.left$Optim.d.trip + trips.left$Sub.optim.d.trip + trips.left$No.transfer - d8, 4 * n.full.4.cars)
trips.sample.1$Optim.d.trip + trips.sample.1$Sub.optim.d.trip + trips.sample.1$No.transfer - d8, 4 * n.full.4.cars)
c8 <- 8 * n.full.8.cars - d8
c8 <- 8 * n.full.8.cars - d8
c4 <- 4 * n.full.4.cars - d4
c4 <- 4 * n.full.4.cars - d4
c3 <- n.4.cars.3.pas * (trips.left$Transferred - c8 - c4) # Note: there will be only 1 partially filled car
c3 <- n.4.cars.3.pas * (trips.sample.1$Transferred - c8 - c4) # Note: there will be only 1 partially filled car
d3 <- 3 * n.4.cars.3.pas - c3
d3 <- 3 * n.4.cars.3.pas - c3
c2 <- n.4.cars.2.pas * (trips.left$Transferred - c8 - c4)
c2 <- n.4.cars.2.pas * (trips.sample.1$Transferred - c8 - c4)
d2 <- 2 * n.4.cars.2.pas - c2
d2 <- 2 * n.4.cars.2.pas - c2
c1 <- n.4.cars.1.pas * (trips.left$Transferred - c8 - c4)
c1 <- n.4.cars.1.pas * (trips.sample.1$Transferred - c8 - c4)
d1 <- n.4.cars.1.pas - c1
d1 <- n.4.cars.1.pas - c1
Line 108: Line 123:
# delay <- distance / speed
# delay <- distance / speed
delay <- 1
delay <- 0.2
colnames(trips.left.trans)[5] <- "New.secondary"
colnames(trips.left.trans)[5] <- "Secondary"
colnames(trips.left.trans)[1] <- "Origin"
colnames(trips.left.trans)[1] <- "Origin"
colnames(trips.left.trans)[2] <- "From"
colnames(trips.left.trans)[2] <- "From"
colnames(trips.left.trans)[3] <- "To"
colnames(trips.left.trans)[3] <- "To"
trips.left.trans$Time <- trips.left.trans$Time + delay
trips.left.trans$Time <- as.character(as.numeric(as.character(trips.left.trans$Time)) + delay)
trips.left.trans <- as.data.frame(as.table(tapply(trips.left.trans$Secondary, trips.left.trans[,
c("From","To","Time")], sum)))
colnames(trips.left.trans)[4] <- "Secondary"
trips.left.trans <- trips.left.trans[!is.na(trips.left.trans$Secondary),]
trips.sample <- merge(trips.sample, trips.left.trans[,c(2,3,4,5)], all = TRUE)
trips.secondary <- rbind(trips.secondary, trips.left.trans)
trips.sample$New.secondary[is.na(trips.sample$New.secondary)] <- 0
trips.sample$Secondary <- trips.sample$Secondary + trips.sample$New.secondary
trips.sample <- trips.sample[,!colnames(trips.sample) %in% "New.secondary"]
trips.out <- rbind(trips.out, data.frame(trips.left[, c("From", "To", "Time")], d8, d4, d3, d2, d1, c8, c4, c3, c2, c1))
trips.out <- rbind(trips.out, data.frame(trips.sample.1[, c("From", "To", "Time")], d8, d4, d3, d2, d1, c8, c4, c3, c2, c1))
}
}
</rcode>
</rcode>


{{todo|Ruvetaan keräämään tälle sivulle matskua mallin uudesta versiosta.|Smxb}}
{{todo|Ruvetaan keräämään tälle sivulle matskua mallin uudesta versiosta.|Smxb}}

Revision as of 12:37, 29 July 2011



This page is about a composite traffic model that is an updated version of File:Composite traffic.ANA. The new version is coded with R.

Definition

R model

  • Trip aggregator
    • Optimization rules:
  1. No second transfer -> prioritize "secondary" passengers
  2. Fill as many 8-person-vehicles as possible
  3. Fill as many 4-person-vehicles as possible
  4. Special rule: for trips with no possible transfer-point -> direct trip
  5. Transfer the rest (will always be 4-person-vehicles)
  6. Re-check vehicle configurations, when exact numbers of primary and secondary passengers as well as transferees are known

+ Show code

TODO: {{#todo:Ruvetaan keräämään tälle sivulle matskua mallin uudesta versiosta.|Smxb|}}