title: “Logistics Optimization LP”
output: html_document
“`{r setup, include=FALSE}
Copyright By PowCoder代写 加微信 powcoder
knitr::opts_chunk$set(echo = TRUE)
library(lpSolveAPI)
library(reshape2)
Tropicsun is a leading grower and distributer of fresh citrus products with three large citrus groves scattered around central Florida in the cities of Mt. Dora, Eustis and Clermont. Tropicsun currently has 275,000 bushels of citrus at the grove in Mt. Dora, 400,000 bushels of citrus in the grove at Eustis and 300,000 bushels of citrus at the grove in Clermont.
Tropicsun has citrus processing plants in Ocala and Orlando with processing capacites to handle 900,000 and 200,000 bushels respectively. The Clermont grove also has a processing plant on site with a processing capacty of 100,000 bushels.
Finally, Tropic sun ships processed products from the processing plants to the demand centers, located in Lessburg, Orlando, Tampa and Sarasota. The current demand levels at each of the centers in listed in Table 1.
**Table 1: Demand at Demand Centers**
|Center |Demand |
|:——-:|:—–:|
|Leesburg |200,000|
|Orlando |300,000|
|Tampa |150,000|
|Sarasota |200,000|
Tropicsun contracts with a local trucking company to transport its fruit from the groves to the processing plants and from the plants to the demand centers. The trucking company charges a flat rate for every mile that each bushel of fruit must be transported. Each mile a bushel of fruit travels is known as a bushel-mile. Table 2 summarizes the distances (in miles) between the groves and the processing plants.
**Table 2: Distance(miles) between locations**
| |Clermont |Ocala |Orlando |Lessburg|Tampa |Sarasota |
|:——:|:——-:|:——:|:——:|:——:|:——:|:——-:|
|Mt Dora |21 |50 |40 |n/a |n/a |n/a |
|Eustis |35 |30 |22 |n/a |n/a |n/a |
|Clermont|0 |20 |25 |15 |60 |55 |
|Ocala |20 |0 |20 |30 |45 |55 |
|Orlando |25 |20 |0 |40 |25 |30 |
Tropicsun wants to determine how many bushels to ship from each grove to each processing plant and from each plant to its demand centers in order to process all the fruit from the groves and meet demand at the demand centers while minimizing the total number of bushel-miles the fruit must be shipped.
data <- read.csv(file.choose())
##Use Reshape2 to melt df to long format - one row for each start-end combination
data_long <- melt(data, id = "Location", variable.name = "Destination", value.name = "Miles", na.rm = TRUE)
groves <- c('Mt_Dora', 'Eustis', 'Clermont')
processing <- c('Ocala', 'Orlando', 'Clermont')
centers <- c('Lessburg', 'Orlando', 'Tampa', 'Sarasota')
grove_supply = data.frame(grove = groves, supply = c(275000, 400000, 300000))
processing_capacity = data.frame(process = processing, capacity = c(900000, 200000, 100000))
center_demand = data.frame(center = centers, demand = c(200000, 300000, 150000, 200000))
##create decision variables df
grove_processing <- data.frame(route = apply(expand.grid(groves, processing), 1, paste, collapse="_"),
start = rep(groves, length(processing)),
end = rep(processing, each = length(groves)),
type = 'g-to-p')
processing_centers <- data.frame(route = apply(expand.grid(processing, centers), 1, paste, collapse="_"),
start = rep(processing, length(centers)),
end = rep(centers, each = length(processing)),
type = 'p-to-c')
decision_variables <- rbind(grove_processing, processing_centers)
decision_variables
model <- make.lp(0, length(groves) * length(processing) + length(processing) * length(centers))
for (g in groves){
add.constraint(model,
rep(1, length(processing)),
type = '=',
rhs = grove_supply[grove_supply$grove == g, 'supply'],
indices = (which(decision_variables$type == 'g-to-p' & decision_variables$start == g)))
for (p in processing){
add.constraint(model,
rep(1, length(groves)),
type = '<=',
rhs = processing_capacity[processing_capacity$process == p, 'capacity'],
indices = (which(decision_variables$type == 'g-to-p' & decision_variables$end == p)))
for (c in centers){
add.constraint(model,
rep(1, length(processing)),
type = '>=’,
rhs = center_demand[center_demand$center == c, ‘demand’],
indices = (which(decision_variables$type == ‘p-to-c’ & decision_variables$end == c)))
write.lp(model, filename = ‘model.lp’, ‘lp’)
## create miles – vector of distances
miles <- NULL for (i in 1:nrow(decision_variables)){ miles <- c(miles, data_long[data_long$Location == decision_variables[i, 'start'] & data_long$Destination == decision_variables[i, 'end'], 'Miles']) miles <- as.numeric(miles) ## set objective function set.objfn(model, miles) set.type(model, c(1:nrow(decision_variables)), "integer") dimnames(model)[[2]] <- decision_variables$route write.lp(model, "model.lp", "lp") solve(model) min_miles <- get.objective(model) bushels <- get.variables(model) names(bushels) <- decision_variables$route 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com