Fixing an Optimization Drawback in R Utilizing Linear Programming | by Andrew Josselyn | Dec, 2022

[ad_1]

Photograph by Laura Rivera on Unsplash

Query 1: How a lot does it price to make use of rail transport solely?

### Set up and Load Libraries ###
set up.packages('lpSolve')
library(lpSolve)

### Query 1: Rail Solely ###
# Write the Goal Perform
Rail_obj <- c(51, 62, 35, 45, 56, 59, 68, 50, 39, 46, 49, 56, 53, 51, 37)

# Set matrix akin to coefficients of constraints by rows
Rail_con <- matrix(c(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1
), nrow = 8, byrow = TRUE)

# Set unequality indicators
Rail_dir <- c("<=",
"<=",
"<=",
"<=",
"<=",
"=",
"=",
"=")

# Set proper hand aspect coefficients
Rail_rhs <- c(11,
12,
9,
10,
8,
15,
20,
15
)

# Variables closing values
lp("min", Rail_obj, Rail_con, Rail_dir, Rail_rhs)$resolution

Query 2: How a lot does it price to make use of ships solely?

### Query 2: Transport Solely ###
# Write the Goal Perform, change "none" with a big quantity
Ship_obj <- c(48, 68, 48, 999, 54, 66, 75, 55, 49, 57, 999, 61, 64, 59, 50)

# Set matrix akin to coefficients of constraints by rows
Ship_con <- matrix(c(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1
), nrow = 8, byrow = TRUE)

# Set unequality indicators
Ship_dir <- c("<=",
"<=",
"<=",
"<=",
"<=",
"=",
"=",
"=")

# Set proper hand aspect coefficients
Ship_rhs <- c(11,
12,
9,
10,
8,
15,
20,
15
)

# Variables closing values
lp("min", Ship_obj, Ship_con, Ship_dir, Ship_rhs)$resolution

Query 3: How a lot does it price to make use of the most cost effective accessible mode of transportation on every route?

### Query 3: Optimum ###
# Write the Goal Perform
optimal_obj <- c(48, 62, 35, 45, 54, 59, 68, 50, 39, 46, 49, 56, 53, 51, 37)

# Set matrix akin to coefficients of constraints by rows
optimal_con <- matrix(c(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1
), nrow = 8, byrow = TRUE)

# Set unequality indicators
optimal_dir <- c("<=",
"<=",
"<=",
"<=",
"<=",
"=",
"=",
"=")

# Set proper hand aspect coefficients
optimal_rhs <- c(11,
12,
9,
10,
8,
15,
20,
15
)

# Variables closing values
lp("min", optimal_obj, optimal_con, optimal_dir, optimal_rhs)$resolution

Highlighted fields characterize utilizing a ship, non-highlighted characterize rail.

Query 4: Suppose that there’s an annual price of $100,000 to function any ships (however that this price doesn’t range with the variety of transport traces saved open). What’s the optimum transportation plan?

Optimally transport with each rail and ships is probably the most price efficient

[ad_2]

Source_link

Leave a Reply

Your email address will not be published. Required fields are marked *