michael scholz - pik-potsdam.deluedeke/r_kram/dea/lit/optimierung_mit_r.pdf4 > b a simplex(a =...
Post on 31-Oct-2019
3 Views
Preview:
TRANSCRIPT
Inhalt
LP
NLP
Optimierung in R
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10)
Michael Scholz
Institut für Statistik und Ökonometrie
Georg-August-Universität Göttingen
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 1 / 27
Inhalt
LP
NLP
Inhalt:
1 Lineare Programmierung (LP)simplexsolveLP
2 Nichtlineare Optimierung (NLP)optimizeoptimconstrOptimnlm
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 2 / 27
Inhalt
LP
NLP
Lineare Programmierung
package: bootBefehl: simplex
Die Funktion simplex optimiert lineare Funkionen a>x unter denNebenbedingungen A1x ≤ b1, A2x ≥ b2 und/oder A3x = b3 sowie x ≥ 0.
Example 1.1 (17.8, Beispiel 1)
max 700x1 + 1000x2 unter
3x1 + 5x2 ≤ 3900
x1 + 3x2 ≤ 2100
2x1 + 2x2 ≤ 2200
x1, x2 ≥ 0.
Matrixformulierung:
max a>x unter Ax ≤ b und x ≥ 0 mit
a =
(7001000
), A =
3 51 32 2
, b =
390021002200
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 3 / 27
Inhalt
LP
NLP
Die R-Funktion simplex
1 > library(boot)2 > A <- matrix( c(3, 5, 1, 3, 2, 2), nrow = 3,3 > ncol = 2, byrow = TRUE)4 > b <- c(3900, 2100, 2200)5 > a <- c(700, 1000)6 > simplex(a = a, A1 = A, b1 = b, maxi = TRUE)
Lade Paket bootMatrix A
Vektor bKoeff.vektor der Zielfkt.Funktionsaufruf
Linear Programming Results
Call : simplex(a = a, A1 = A1, b1 = b1, maxi = TRUE)
Maximization Problem with Objective Function Coefficientsx1 x2700 1000
Optimal solution has the following valuesx1 x2800 300The optimal value of the objective function is 860000.
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 4 / 27
Inhalt
LP
NLP
Die R-Funktion simplex
Example 1.2 (17.3.1 mit weiterer NB)
max 2x1 + 7x2 unter
4x1 + 5x2 ≤ 20
3x1 + 7x2 ≤ 21
x1 + x2 ≥ 1
x1, x2 ≥ 0.
Matrixformulierung:
max a>x unter A1x ≤ b1, A2x ≥ b2 und x ≥ 0 mit
a =
(27
), A1 =
(4 53 7
), b1 =
(2021
), A2 =
(1 1
), b2 =
(1)
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 5 / 27
Inhalt
LP
NLP
Die R-Funktion simplex
1 > library(boot)2 > A1 <- matrix( c(4, 5, 3, 7), nrow = 2,3 > ncol = 2, byrow = TRUE)4 > b1 <- c(20, 21)5 > A2 <- matrix( c(1, 1), nrow = 1,6 > ncol = 2, byrow = TRUE)7 > b2 <- c(1)8 > a <- c(2, 7)9 > simplex(a = a, A1 = A1, b1 = b1, A2 = A2,
10 > b2 = b2, maxi = TRUE)
„kleiner-gleich“ NB
„größer-gleich“ NB
Funktionsaufruf
Linear Programming Results
Call : simplex(a = a, A1 = A1, b1 = b1, A2 = A2, b2 = b2, maxi = TRUE)
Maximization Problem with Objective Function Coefficientsx1 x22 7
Optimal solution has the following valuesx1 x20 3The optimal value of the objective function is 21.
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 6 / 27
Inhalt
LP
NLP
Die R-Funktion simplex
Example 1.3 (17.3.1 mit weiterer NB und Minimum)
min 2x1 + 7x2 unter
4x1 + 5x2 ≤ 20
3x1 + 7x2 ≤ 21
x1 + x2 = 1
x1, x2 ≥ 0.
Matrixformulierung:
min a>x unter A1x ≤ b1, A3x = b3 und x ≥ 0 mit
a =
(27
), A1 =
(4 53 7
), b1 =
(2021
), A3 =
(1 1
), b3 =
(1)
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 7 / 27
Inhalt
LP
NLP
Die R-Funktion simplex
1 > library(boot)2 > A1 <- matrix( c(4, 5, 3, 7), nrow = 2,3 > ncol = 2, byrow = TRUE)4 > b1 <- c(20, 21)5 > A3 <- matrix( c(1, 1), nrow = 1,6 > ncol = 2, byrow = TRUE)7 > b3 <- c(1)8 > a <- c(2, 7)9 > simplex(a = a, A1 = A1, b1 = b1, A3 = A3,
10 > b3 = b3)
„kleiner-gleich“ NB
„gleichheits“ NB
Funktionsaufruf
Linear Programming Results
Call : simplex(a = a, A1 = A1, b1 = b1, A3 = A3, b3 = b3)
Minimization Problem with Objective Function Coefficientsx1 x22 7
Optimal solution has the following valuesx1 x21 0The optimal value of the objective function is 2.
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 8 / 27
Inhalt
LP
NLP
Die R-Funktion simplex
Nur für relativ kleine Systeme empfehlenswert.NB, wie xi ≥ bi > 0, sollten durch Einführen von x̃i := xi − bi ≥ 0 imgesamten System ersetzt werden. (Rücktranfsformation nachdemLösung gefunden wurde.)Duale Lösung nicht ablesbar.Keine Sensitivitätsanalyse.Zwei-Phasen Simplex-Verfahren, falls x = 0 nicht zum ZulässigenBereich gehört, dann wird versucht eine zulässige Startlösung zufinden.
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 9 / 27
Inhalt
LP
NLP
Die R-Funktion solveLP
package: linprogBefehl: solveLP
Die Funktion solveLP optimiert lineare Funkionen a>x unter denNebenbedingungen Ax ≤ b sowie x ≥ 0.
Example 1.4 (17.5.2)
max 15x1+5x2−5x3−20x4 unter
x1 + x2 − x3 + x4 ≤ 1
6x1 + x2 + x3 − 2x4 ≤ 2x1, . . . , x4 ≥ 0.
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 10 / 27
Inhalt
LP
NLP
Die R-Funktion solveLP
Matrixformulierung:
max a>x unter Ax ≤ b und x ≥ 0 mit
a =
15
5− 5−20
, A =
(1 1 −1 16 1 1 −2
), b =
(12
)
1 > library(linprog)2 > A <- matrix( c(1, 1, -1, 1, 6, 1, 1, -2),3 > nrow = 2, ncol = 4, byrow = TRUE)4 > b <- c(1, 2)5 > a <- c(15, 5, -5, -20)6 > solveLP(a, b, A, maximum = TRUE)
Lade Paket linprogMatrix A
Vektor bKoeff.vektor der Zielfkt.Funktionsaufruf
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 11 / 27
Inhalt
LP
NLP
Die R-Funktion solveLP
Results of Linear Programming / Linear Optimization
Objective function (Maximum):[1] 7
Iterations in phase 1: 0Iterations in phase 2: 2
Basic Variablesopt
1 0.22 0.8
Constraintsmax actual diff dual price dual.reg
1 1 1 0 3 0.6666672 2 2 0 2 1.000000
All Variables (including slack variables)opt c min c max c marg. marg.reg.
1 0.2 15 5.0 30.00000 NA NA2 0.8 5 2.5 7.85714 NA NA3 0.0 -5 -Inf -1.00000 -4 0.5000004 0.0 -20 -Inf -1.00000 -19 0.500000S 1 0.0 0 NA NA -3 0.666667S 2 0.0 0 NA NA -2 1.000000
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 12 / 27
Inhalt
LP
NLP
Die R-Funktion solveLP
Ausgabe von:optimaler Wert der Zielfkt.Anzahl Iterationen in Phase 1/2Vektor der von Null verschiedenen Variablen im Optimum.Informationen über NB:
1. Spalte = b2. Spalte = NB-Werte („linke Seite“) im Optimum3. Spalte = Differenz von Spalte 1 und 24. Spalte = duale Lösung (Schattenpreise)
Resultate aller Variablen (inklusive Schlupfvariablen)1. Spalte = Optimalwerte2. Spalte = Koeffizienten der Zielfkt.3. Spalte = Minimum von 2. Spalte, dass Lösung nicht ändert4. Spalte = Maximum von 2. Spalte, dass Lösung nicht ändert5. Spalte = Koeffizienten der Zielfuntion nach letzter Iteration
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 13 / 27
Inhalt
LP
NLP
Die R-Funktion solveLP
1 > res <- solveLP(a, b, A, maximum = TRUE)2 > res$Tab
Abspeichern der LösungAnzeigen des letzten Tableaus
1 2 3 4 S 1 S 2 P02 0 1 -1.4 1.6 1.2 -0.2 0.81 1 0 0.4 -0.6 -0.2 0.2 0.2Z-C 0 0 4.0 19.0 3.0 2.0 7.0
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 14 / 27
Inhalt
LP
NLP
Die R-Funktion solveLP
Example 1.5 (17.3.1 mit weiterer NB)
max 2x1 + 7x2 unter
4x1 + 5x2 ≤ 20
3x1 + 7x2 ≤ 21
x1 + x2 ≥ 1
x1, x2 ≥ 0.
Matrixformulierung:
max a>x unter Ax ≤ b und x ≥ 0 mit
a =
(27
), A =
4 53 7−1 −1
, b =
2021−1
1 > library(linprog)2 > A <- matrix( c(4, 5, 3, 7, -1, -1),3 > nrow = 3, ncol = 2, byrow = TRUE)4 > b <- c(20, 21, -1)5 > a <- c(2, 7)6 > solveLP(a, b, A, maximum = TRUE)
Lade Paket linprogMatrix A
Vektor bKoeff.vektor der Zielfkt.Funktionsaufruf
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 15 / 27
Inhalt
LP
NLP
Die R-Funktion solveLP
Results of Linear Programming / Linear Optimization
Objective function (Maximum):[1] 21
Iterations in phase 1: 1Iterations in phase 2: 2
Basic Variablesopt
2 3S 1 5S 3 2
Constraintsmax actual diff dual price dual.reg
1 20 15 5 0 52 21 21 0 1 143 -1 -3 2 0 2
All Variables (including slack variables)opt c min c max c marg. marg.reg.
1 0 2 -Inf 3.00 -1 2.692312 3 7 4.666667 Inf NA NAS 1 5 0 -0.538462 1.40 0 5.00000S 2 0 0 NA NA -1 14.00000S 3 2 0 -7.000000 1.75 0 2.00000
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 16 / 27
Inhalt
LP
NLP
Die R-Funktion solveLP
Letztes Tableau kann ausgegeben werden, d.h. Sensitivitätsanalysemöglich.Duale Lösung direkt ablesbar.Zwei-Phasen Simplex-Verfahren, falls x = 0 nicht zum ZulässigenBereich gehört, dann wird versucht eine zulässige Startlösung zufinden.Keine Gleichheits-NB implementiert.
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 17 / 27
Inhalt
LP
NLP
Nichtlineare Programmierung
package: statsBefehl: optimize
Univariate Optimierung einer Fkt. f(x) in einem gegebenen Intervall[a, b].
Example 2.1 (8.2, Beispiel 2)min f(x) = e2x − 5ex + 4 im Intervall [−3.0, 1.5]
1 > fkt <- function(x){2 > exp(2 * x) - 5 * exp(x) + 4}3 > interval <- c(-3,1.5)4 > optimize(fkt, interval)
Definiere zu optimierende Funkti-onzu untersuchendes IntervallFunktionsaufruf
$minimum[1] 0.9162822
$objective[1] -2.25
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 18 / 27
Inhalt
LP
NLP
Die R-Funktion optimize
Example 2.2 (8.4, Beispiel 2)max f(x) = 1
4x4 − 56x3 + 1
2x2 − 1 im Intervall [−1, 3]
1 > fkt <- function(x){ 1 / 4 * x ^ 4 -2 > 5 / 6 * x ^ 3 + 1 / 2 * x ^ 2 - 1}3 > interval <- c(-1,3)4 > optimize(fkt, interval , maximum = TRUE)
Definiere zu optimierende Funkti-onzu untersuchendes IntervallFunktionsaufruf
$maximum[1] 0.5000118
$objective[1] -0.9635417
ACHTUNG: Findet nicht den richtigen globalen MAX-Wert f(3) = 1.25,sondern hier nur lokales Maximum!
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 19 / 27
Inhalt
LP
NLP
Nichtlineare Programmierung
package: statsBefehl: optim
Multivariate Optimierung einer Fkt. f : Rn → R.
Example 2.3 (13.1, Beispiel 1)max f(x, y) = −2x2 − 2xy − 2y2 + 36x + 42y − 158
1 > fkt <- function(x){-2 * x[1]^2 - 2 * x[1] * x[2]2 > -2 * x[2]^2 + 36 * x[1] + 42 * x[2] - 158}3 > optim(c(1,1), fkt, control = list(fnscale = -1))
Definiere zu optimierendeFunktionFunktionsaufruf
Benötigt werdenStartpunkt, da iteratives Optimierungsverfahren, hier: x0 = (1, 1)
zu optimierende Funktionfalls maximiert werden soll, explizite Angabe: control =list(fnscale = -1)
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 20 / 27
Inhalt
LP
NLP
Die R-Funktion optim
Ausgabe:OptimalpunktFunktionswert im OptimalpunktAnzahl der FunktionsaufrufeStatus-Information (0 = Erfolg, 1 = max. Anzahl Iterationen, . . . )zusätzliche Informationen
$par[1] 5.000343 7.999936
$value[1] 100
$countsfunction gradient
87 NA
$convergence[1] 0
$messageNULL
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 21 / 27
Inhalt
LP
NLP
Die R-Funktion optim
weitere Optionen:Angabe des Gradienten (wenn nötig)Auswahl verschiedener Verfahren (z. B. CG, BFGS, Nelder-Mead)Ausgabe der Hesse-Matrix
1 > gr <- function(x){c(-4 * x[1] - 2 * x[2] + 36,2 > -2 * x[1] - 4 * x[2] + 42)}3 > res <- optim(c(1,1), fkt, gr, method = "CG",4 > control = list(fnscale = -1),5 > hessian = TRUE)6 > res$hessian
Definiere Gradienten
Funktionsaufruf
Aufruf der Hesse-Matrix
[,1] [,2][1,] -4 -2[2,] -2 -4
1 > eigen(res$hessian) Eigenwerte und Vektoren
$values[1] -2 -6
$vectors[,1] [,2]
[1,] -0.7071068 0.7071068[2,] 0.7071068 0.7071068
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 22 / 27
Inhalt
LP
NLP
Die R-Funktion constrOptim
package: statsBefehl: constrOptim
Minimierung einer Fkt. f : Rn → R unter linearenUngleichheitsnebenbedingungen der Form Ax ≥ b.
Example 2.4 (14.8, Beispiel 2)max f(x, y) =
√x +√
y unter 3x + 5y ≤ 10
1 > fkt <- function(x){sqrt(x[1]) + sqrt(x[2])}2 > A <- matrix(c(-3,-5), nrow = 1, ncol = 2,3 > byrow = TRUE)4 > b <- c(-10)5 > constrOptim(c(1,1), fkt, NULL, ui = A, ci = b,6 > control = list(fnscale = -1))
FunktionMatrix A
rechte Seite bFunktionsaufruf
Benötigt werden: Startpunkt (muss im Inneren des zul. Bereichs liegen),Funktion, Gradient (kann NULL gesetzt werden), Nebenbedingungungen,Info für Maximierung
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 23 / 27
Inhalt
LP
NLP
Die R-Funktion constrOptim
Ausgabe analog zu optim
$par[1] 2.0830760 0.7498019
$value[1] 2.309198
$countsfunction gradient
81 NA
$convergence[1] 0
$messageNULL
$outer.iterations[1] 1
$barrier.value[1] -0.0002683532
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 24 / 27
Inhalt
LP
NLP
Die R-Funktion nlm
package: statsBefehl: nlm
Minimierung einer Fkt. f : Rn → R mittels „line-search“(Algorithmus vomNewton-Typ).
Example 2.5 (13.1, Beispiel 1)min f(x, y) = 2x2 + 2xy − 2y2 − 36x − 42y + 158
1 > fkt <- function(x){-1 * (-2 * x[1]^2 - 2 *2 > x[1] * x[2] -2 * x[2]^2 + 36 * x[1] +3 > 42 * x[2] - 158)}4 > nlm(fkt, c(1,1))
Definiere zu minimie-rende Funktion
Funktionsaufruf
Benötigt werdenStartpunkt, da iteratives Optimierungsverfahren, hier: x0 = (1, 1)
zu optimierende Funktion
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 25 / 27
Inhalt
LP
NLP
Die R-Funktion nlm
Ausgabe:Funktionswert im MinimumOptimalpunktGradient der LösungStatus-Information (1 = Gradient in der Nähe von Null, . . . )Anzahl Iterationen
$minimum[1] -100
$estimate[1] 4.999999 7.999996
$gradient[1] 0.00000e+00 -7.10543e-09
$code[1] 1
$iterations[1] 3
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 26 / 27
Inhalt
LP
NLP
Die R-Funktion nlm
weitere Optionen:Angabe des GradientenAusgabe der Hesse-Matrix
1 > gr <- function(x){c(4 * x[1] + 2 * x[2] - 36,2 > 2 * x[1] + 4 * x[2] - 42)}3 > res <- nlm(fkt, c(1,1), gr, hessian = TRUE)4 > res$hessian
Definiere Gradienten
FunktionsaufrufAufruf der Hesse-Matrix
[,1] [,2][1,] 3.999999 2.000000[2,] 2.000000 4.000000
1 > eigen(res$hessian)$values Eigenwerte
[1] 6 2
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10) 27 / 27
top related