-
Notifications
You must be signed in to change notification settings - Fork 0
/
Intro Transportation Proplem from Chapter 2-1.R
96 lines (68 loc) · 2.36 KB
/
Intro Transportation Proplem from Chapter 2-1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#consider this transportatin problem.
Transport <- data.frame(Leftcolumn = c("origin_a","origin_b","demand"),
cost_dest_1 = c("8", "2", "40"), cost_dest_2 = c("6", "4","35"),
destination_3= c("3", "9", "25"), capacity = c("70", "40", "Null") )
#Notice that I didn't name the rws but it is easily done by fnishing with
#row.names=c("bla", "bla", "and such"))
Transport
#Let's write and understand *LOL* some of this R code :- )
#defining parameters
#origins run from 1:m where m=2, in this case
#destinations run from 1:j where j=3, in this case
obj.fun <- c(8, 6, 3, 2, 4, 9)
#recall we wish to minimize the shipping costs
#8xa1 +6xa2 + 3xa3 + 2xb1 + 4xb2 + 9xb3
# m is the number of rows from the table setting up the problem (a & b)
# n is the number of columns from the table setting up the problem (1, 2, & 3)
(m <- 2)
n <- 3
#build our constraint matrix
constr <- matrix(0, m+n, m*n)
#let's take a look at this
constr
#just an empty matrix for the moment
for(i in 1:m){
for(j in 1:n){constr[i, n*(i-1) + j] <- 1
constr[m+j, n*(i-1) +j] <- 1
}
}
# Manually setting up the constraint matrix without the for loops
r1 <- c(1, 1, 1, 0, 0, 0)
r2 <- c(0, 0, 0, 1, 1, 1)
r3 <- c(1, 0, 0, 1, 0, 0)
r4 <- c(0, 1, 0, 0, 1, 0)
r5 <- c(0, 0, 1, 0, 0, 1)
constr <- rbind(r1, r2, r3, r4, r5)
constr
#now look at what this codes variables this codes up
#let's code up the varying equalities
const.dir <- c(rep("<=", m), rep(">=", n))
#the rep tells it is make m coplis like so
const.dir
rhs <- c(70, 40, 40, 35, 25)
#this is the right hand side
rhs
#Now lets solve this model
library(lpSolve)
prod.trans <- lp("min", obj.fun, constr, const.dir, rhs, compute.sens = TRUE)
lp("min", obj.fun, constr, const.dir, rhs, compute.sens = TRUE)
#here we go :- )
#by thier code first :- )
prod.trans$obj.val
#notice this is probably a typo the is - $obj.val should be $objective or $objval
#lets see :- )
prod.trans$objval
prod.trans$objective
prod.trans$duals.from
prod.trans$duals.to
sol <- matrix(prod.trans$solution, m, n, byrow = TRUE)
sol
lp("min", obj.fun, constr, const.dir, rhs, compute.sens = TRUE)$solution
#recall how our listing went for variables
# a1, a2, a3, b1, b2, b3
#thus the solutions are a2 = 35, a3 = 25, and b1 = 40 : )
#sensitivity analysis of the lp
prod.trans$duals.from
prod.trans$duals.to
prod.trans$sens.coef.from
prod.trans$sens.coef.to