BIA_T13_Optimization.Rmd 3.81 KB
Newer Older
Weigert, Andreas's avatar
Weigert, Andreas committed
1
---
Weigert, Andreas's avatar
Weigert, Andreas committed
2
title:  'Tutorial 13: Optimization'
Weigert, Andreas's avatar
Weigert, Andreas committed
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
output: html_notebook
editor_options: 
  chunk_output_type: inline
---

This file is part of the lecture Business Intelligence & Analytics (EESYS-BIA-M), Information Systems and Energy Efficient Systems, University of Bamberg.


```{r}
library(lpSolve)
library(ggplot2)
```


```{r lpSolve default example}
#
# Set up problem: maximize
#   x1 + 9 x2 +   x3 subject to
#   x1 + 2 x2 + 3 x3  <= 9
# 3 x1 + 2 x2 + 2 x3  <= 15
#
objective.in <- c(1, 9, 1)
const.mat <- matrix(c(1, 2, 3, 3, 2, 2), nrow=2, byrow=TRUE)
f.dir <- c("<=", "<=")
const.rhs <- c(9, 15)
direction <- "max"
lp(direction, objective.in, const.mat, f.dir, const.rhs)
lp(direction, objective.in, const.mat, f.dir, const.rhs)$solution
```

```{r The Stratton Company Case - Graphical solution}
# Set up problem: maximize
# 34 x1 + 40 x2 subject to
#  4 x1 +  6 x2 <= 48
#  2 x1 +  2 x2 <= 18
#  2 x1 +    x2 <= 16
#    x1 >= 0
#    x2 >= 0

graph <- ggplot(data = data.frame(x=c(0,0,8,9,0,12), y=c(16,9,0,0,8,0)), aes(x,y)) +
  geom_abline(slope = -2/3, intercept = 8, color="red") + 
  geom_abline(slope = -1, intercept = 9, color = "blue") + 
  geom_abline(slope = -2, intercept = 16, color = "green") +
  geom_point(data = data.frame(x_optimal = c(3), y_optimal = c(6)), aes(x_optimal,y_optimal)) +
  geom_text(data = data.frame(x_optimal = c(3), y_optimal = c(6)), label="optimal solution", aes(x_optimal,y_optimal)) +
  geom_point()
graph

```


```{r The Stratton Company Case - lpsolve}
# Set up problem: maximize
# 34 x1 + 40 x2 subject to
#  4 x1 +  6 x2 <= 48
#  2 x1 +  2 x2 <= 18
#  2 x1 +    x2 <= 16
#    x1 >= 0
#    x2 >= 0

objective.in <- c(34, 40)
const.mat <- matrix(c(4, 6,
                      2, 2,
                      2, 1), nrow=3, byrow=TRUE)
const.dir <- c("<=", "<=", "<=")
const.rhs <- c(48, 18, 16)
direction <- "max"
result <- lp(direction = direction, objective.in = objective.in, const.mat = const.mat, const.dir = const.dir, const.rhs = const.rhs)
result
result$solution
```

```{r The steal production plan case - lpsolve}
# Set up problem: maximize
# 25 x1 + 30 x2 + 29 x3 subject to
#    x1 >= 0
#    x2 >= 0
#    x3 >= 0
#    x1 <= 6000
#    x2 <= 4000
#    x3 <= 3500

#  1/200 x1 +  1/140 x2 + 1/160 x3 <= 40
#  1/200 x1 +  1/200 x2 + 1/200 x3 <= 35


objective.in <- c(25, 30, 29)
const.mat <- matrix(c(1/200, 1/140, 1/160,
                      1/200, 1/200, 1/200,
                      1, 0, 0,
                      0, 1, 0,
                      0, 0, 1),nrow=5, byrow=TRUE)
const.dir <- c("<=", "<=")
const.rhs <- c(40, 35, 6000, 4000, 3500)
direction <- "max"
result <- lp(direction = direction, objective.in = objective.in, const.mat = const.mat, const.dir = const.dir, const.rhs = const.rhs)
result
result$solution

```


```{r The farm - lpsolve}
# Set up problem: maximize
# 250 x1 + 45 x2 subject to
# 0 <= x1 <= 50
# 0 <= x2 <= 200
# x1 +  0.2 x2 <= 40
# 150 x1 +  25 x2 <= 10000


objective.in <- c(250, 45)
const.mat <- matrix(c(1,0, 
                      1,0,
                      0,1,
                      0,1,
                      1,0.2,
                      150,25),nrow=6, byrow=TRUE)
const.dir <- c(">=", "<=", ">=", "<=", "<=", "<=")
const.rhs <- c(0, 50, 0, 200, 40, 10000)
direction <- "max"
result <- lp(direction = direction, objective.in = objective.in, const.mat = const.mat, const.dir = const.dir, const.rhs = const.rhs)
result
result$solution
```


```{r Photovoltaic cell production - lpsolve}
objective.in <- c(5, 2, 4)
const.mat <- matrix(c(6,1,1, 
                      2,1,1,
                      4,3,0,
                      0,5,2),nrow=4, byrow=TRUE)
const.dir <- c("<=", "<=", "<=", "<=")
const.rhs <- c(125, 65, 150, 75)
direction <- "max"
result <- lp(direction = direction, objective.in = objective.in, const.mat = const.mat, const.dir = const.dir, const.rhs = const.rhs)
result
result$solution
```