f.mean <- mean
library("MASS")
library("Matrix")
library("igraph")
library("data.table")
Truck <- function(name, capacity, speed, route, trash_collected = 0) {
list(name = name, capacity = capacity, speed = speed, route = route, trash_collected = trash_collected)
}
Block <- function(name, trash_per_day) {
list(name = name, trash_per_day = trash_per_day)
}
DaysOfTheWeek <- setNames(1:7, c("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"))
TruckRouting <- function(num_blocks, mean = 7, sigma = 2) {
adj_matrix <- create_map(num_blocks, mean, sigma)
list(num_blocks = num_blocks, mean = mean, sigma = sigma, adj_matrix = adj_matrix)
}
create_map <- function(num_blocks, mean, sigma) {
new_map <- matrix(0, nrow = num_blocks, ncol = num_blocks)
for (i in seq(1,num_blocks)) {
for (j in seq(1,num_blocks)) {
if (i == j) {
new_map[i, j] <- Inf
} else {
new_map[i, j] <- as.integer(log(rlnorm(1, mean, sigma)))
}
}
}
return (new_map)
}
generate_truck_route <- function(adj_matrix, start_block, end_block) {
unvisited_blocks <- setdiff(seq_along(adj_matrix), start_block)
truck_route <- c(start_block)
while (length(unvisited_blocks) > 0 && truck_route[length(truck_route)] != end_block) {
next_block <- unvisited_blocks[which.min(adj_matrix[truck_route[length(truck_route)], unvisited_blocks])]
truck_route <- c(truck_route, next_block)
unvisited_blocks <- setdiff(unvisited_blocks, next_block)
}
return (truck_route)
}
create_trucks <- function(num_trucks, truck_routing) {
end_blocks <- c(10, 20)
truck_routes <- lapply(end_blocks, function(end_block) generate_truck_route(truck_routing$adj_matrix, 1, end_block))
return (lapply(seq_len(num_trucks), function(i) Truck(paste0("truck ", i), 500, 30, truck_routes[[i]])))
}
collect_trash <- function(truck, block, time_available) {
if (truck$capacity >= block$trash & time_available >= block$distance) {
truck$trash_collected <- truck$trash_collected + block$trash
truck$capacity <- truck$capacity - block$trash
block$trash <- 0
} else if (truck$capacity >= block$trash) {
truck$trash_collected <- truck$trash_collected + (block$trash * (time_available / block$distance))
truck$capacity <- truck$capacity - (block$trash * (time_available / block$distance))
block$trash <- 0
} else if (time_available >= block$distance) {
truck$trash_collected <- truck$trash_collected + truck$capacity
block$trash <- block$trash - truck$capacity
truck$capacity <- 0
} else {
truck$trash_collected <- truck$trash_collected + (truck$capacity * (time_available / block$distance))
block$trash <- block$trash - (truck$capacity * (time_available / block$distance))
truck$capacity <- 0
}
}
run_shift <- function(truck, blocks, landfill_distance, shift_hours) {
time_per_block <- shift_hours / length(blocks)
for (block in blocks) {
time_available <- time_per_block - (block$distance / truck$speed)
collect_trash(truck, block, time_available)
}
wait_at_landfill(truck, landfill_distance)
}
wait_at_landfill <- function(truck, landfill_distance) {
time_waiting <- landfill_distance / truck$speed
# Wait at landfill until the next shift starts
}
create_blocks <- function(num_blocks, total_trash_per_day) {
trash_per_block <- total_trash_per_day / num_blocks
blocks <- lapply(1:num_blocks, function(i) {
Block <- list(name = paste0("block ", i), distance = sample(1:10, 1), trash_per_day = trash_per_block)
return(Block)
})
return(blocks)
}
simulate_trucks <- function(trucks, blocks, special_increments=NULL) {
trash_left <- sapply(blocks, function(block) block$trash_per_day)
trash_collected <- rep(0, length(trucks))
trash_left_per_shift <- data.frame(block_index = integer(), trash_left = numeric())
for (truck_idx in seq_along(trucks)) {
truck <- trucks[[truck_idx]]
for (block_idx in seq_along(blocks)) {
if (!(block_idx %in% truck$route)) {
next
}
time_to_block <- sum(sapply(1:(length(truck$route)-1), function(i) {
if (truck$route[i+1] != block_idx) {
truck$adj_matrix[truck$route[i], truck$route[i+1]] / truck$speed
} else {
0
}
}))
time_available <- 8 - time_to_block # Assuming an 8 hour shift for now
if (!is.null(special_increments) && block_idx %in% names(special_increments)) {
time_available <- time_available + special_increments[[as.character(block_idx)]]
}
truck$collect_trash(blocks[[block_idx]], time_available)
trash_left[block_idx] <- trash_left[block_idx] - truck$trash_collected
trash_collected[truck_idx] <- trash_collected[truck_idx] + truck$trash_collected
truck$trash_collected <- 0
}
landfill_distance <- sum(sapply(1:(length(truck$route)-1), function(i) truck$adj_matrix[truck$route[i], truck$route[i+1]]))
truck$wait_at_landfill(landfill_distance)
}
for (block_idx in seq_along(blocks)) {
shift <- (block_idx %% length(trash_left_per_shift)) + 1
trash_left_per_shift <- rbind(trash_left_per_shift, data.frame(block_index = block_idx, trash_left = trash_left[block_idx], shift = shift))
}
return(list(trash_collected = trash_collected, trash_left_per_shift = trash_left_per_shift))
}
calculate_total_trash_collected <- function(trucks, blocks, special_increments=NULL) {
results <- simulate_trucks(trucks, blocks, special_increments)
return(sum(results$trash_collected))
}
write_trash_left_to_csv <- function(trucks, blocks, special_increments=NULL, filename='trash_left.csv') {
results <- simulate_trucks(trucks, blocks, special_increments)
write.csv(results$trash_left_per_shift, filename, row.names = FALSE)
}
I am not sure if I need to used [i] to save the information or if the code is wrong ,