Composite traffic model: Difference between revisions

From Opasnet
Jump to navigation Jump to search
mNo edit summary
(→‎R model: new revision)
Line 35: Line 35:
if(i == 1) {
if(i == 1) {
trips.left <- trips.sample[trips.sample$Time <= times[i],]
trips.left <- trips.sample[trips.sample$Time <= times[i],]
#trips.left$Secondary <- 0 # the "Secondary" row gives the number of trips which started elsewhere, i.e. they transfer here
} else {
} else {
trips.left <- trips.sample[trips.sample$Time <= times[i] & trips.sample$Time > times[i] - t.interval,]
trips.left <- trips.sample[trips.sample$Time <= times[i] & trips.sample$Time > times[i] - t.interval,]
#trips.left <- merge(trips.left, trips.sample[trips.sample$Time <= times[i] & trips.sample$Time > times[i] - t.interval,])
}
}
# Optimizer main code
# Optimizer main code
d8 <- (trips.left$Result + trips.left$Secondary) %/% 8 * 8
optimal.d.trips <- 0
trips.left$Secondary <- trips.left$Secondary - d8
sub.optimal.d.trips <- 0
trips.left$Result <- trips.left$Result + trips.left$Secondary - (trips.left$Result + trips.left$Secondary + d8) %% 8
d4 <- (trips.left$Result + trips.left$Secondary) %/% 4 * 4
optimal.d.trips <- (trips.left$Result + trips.left$Secondary) %/% 4 * 4
trips.left$Secondary <- trips.left$Secondary - d4
sub.optimal.d.trips <- ifelse(trips.left$Secondary - optimal.d.trips > 0, trips.left$Result + trips.left$Secondary - optimal.d.trips, 0)
trips.left$Result <- trips.left$Result + trips.left$Secondary - (trips.left$Result + trips.left$Secondary + d4) %% 4
# Now, if there are still some passengers left who have already transferred, make direct trips for them and possible remaining new passengers
busiest <- tapply(trips$Result[trips$Time == times[i+1]], trips$From[trips$Time == times[i+1]], sum)
busiest <- sort(busiest, decreasing = TRUE)
d3 <- ifelse(trips.left$Secondary > 0, (trips.left$Result + trips.left$Secondary) %/% 3 * 3, 0)
condition <- trips.left$Result + trips.left$Secondary - optimal.d.trips - sub.optimal.d.trips > 0
trips.left$Result <- trips.left$Result - d3
trips.left$Secondary <- trips.left$Secondary + trips.left$Result - ifelse(trips.left$Secondary > 0, (trips.left$Result +
trips.left$Secondary + d3) %% 3, 0)
d2 <- ifelse(trips.left$Secondary > 0, (trips.left$Result + trips.left$Secondary) %/% 2 * 2, 0)
trips.next <- merge(trips.left[condition, c("From","To")], roads)
trips.left$Result <- trips.left$Result - d2
trips.left$Secondary <- trips.left$Secondary + trips.left$Result - ifelse(trips.left$Secondary > 0,(trips.left$Result +
trips.left$Secondary + d3) %% 2, 0)
d1 <- ifelse(trips.left$Secondary > 0, trips.left$Secondary, 0)  
trips.next$Through <- match(trips.next$Through, names(busiest))
trips.left$Secondary <- trips.left$Secondary - d1
checkpoints <- tapply(trips.next$Through, trips.next[,c("From", "To")], min)
colnames(trips.left)[colnames(trips.left) == "Freq"] <- "Checkpoint"
trips.left <- merge(trips.left, as.data.frame(as.table(checkpoints)), all = TRUE)
# Then for remaining new passengers, find a transfer point and
# Take into account those that don't have a checkpoint
if (sum(trips.left$Result) > 0) {
condition2 <- condition & is.na(trips.left$Checkpoint)
busiest <- tapply(trips$Result[trips$Time == times[i+1]], trips$From[trips$Time == times[i+1]], sum)
busiest <- sort(busiest, decreasing = TRUE)
no.transfer <- ifelse(condition2), (trips.left$Result + trips.left$Secondary -
optimal.d.trips - sub.optimal.d.trips)[condition2], 0)
trips.next <- merge(trips.left[trips.left$Result>0, c("From","To")], roads)
#colnames(trips.next)[colnames(trips.next) == "From"] <- "Origin"
trips.left$Optim.d.trips <- optimal.d.trips
#colnames(trips.next)[colnames(trips.next) == "Through"] <- "From"
trips.left$Sub.optim.d.trips <- sub.optimal.d.trips
#colnames(trips.next)[colnames(trips.next) == "To"] <- "Destination"
trips.left$No.transfer <- no.transfer
trips.next$Through <- match(trips.next$Through, names(busiest))
# Transfers
chekpoints <- tapply(trips.next$Through, trips.next[,c("From", "To")], min)
colnames(trips.left)[colnames(trips.left) == "Freq"] <- "Checkpoint"
trips.left.trans <- data.frame(trips.left[!condition2, c("From", "Checkpoint", "To", "Time")],  
trips.left <- merge(trips.left, as.data.frame(as.table(checkpoints)), all = TRUE)
Transferred = (trips.left$Result + trips.left$Secondary - optimal.d.trips - sub.optimal.d.trips)[!condition2])
colnames(trips.left.trans)[1] <- "From"
# delay <- distance / speed
colnames(trips.left.trans)[3] <- "Destination"
colnames(trips.left.trans)[2] <- "To"
ifelse(is.na(trips.left$Checkpoint) & trips.left$Result + trips.left$Secondary > 0
trips.left <- merge(trips.left, trips.left.trans[,-c(3,4)], all = TRUE)
trips.left$Transferred[is.na(trips.left$Transferred)] <- 0
trips.left <-  
# Now divide passengers to cars
# Now divide and send, and add secondary passengers to later time points etc.
}
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.4.cars <- (trips.left$Optim.d.trip + trips.left$Sub.optim.d.trip + trips.left$No.transfer + trips.left$Transferred -
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.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.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.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,
trips.left$Optim.d.trip + trips.left$Sub.optim.d.trip + trips.left$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,
trips.left$Optim.d.trip + trips.left$Sub.optim.d.trip + trips.left$No.transfer - d8, 4 * n.full.4.cars)
c8 <- 8 * n.full.8.cars - d8
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
d3 <- 3 * n.4.cars.3.pas - c3
c2 <- n.4.cars.2.pas * (trips.left$Transferred - c8 - c4)
d2 <- 2 * n.4.cars.2.pas - c2
c1 <- n.4.cars.1.pas * (trips.left$Transferred - c8 - c4)
d1 <- n.4.cars.1.pas - c1
# Add transferred passengers to the next time slot as secondary passengers
# delay <- distance / speed
delay <- 1
colnames(trips.left.trans)[5] <- "New.secondary"
colnames(trips.left.trans)[1] <- "Origin"
colnames(trips.left.trans)[2] <- "From"
colnames(trips.left.trans)[3] <- "To"
trips.left.trans$Time <- trips.left.trans$Time + delay
trips.sample <- merge(trips.sample, trips.left.trans[,c(2,3,4,5)], all = TRUE)
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))
trips.out <- rbind(trips.out, data.frame(trips.left[, 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 07:16, 21 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|}}