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:
Inhalt LP NLP
1
Lineare Programmierung (LP) simplex solveLP
2
Nichtlineare Optimierung (NLP) optimize optim constrOptim nlm
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10)
2 / 27
Lineare Programmierung
Inhalt LP
package: boot Befehl: simplex
NLP
Die Funktion simplex optimiert lineare Funkionen a > x unter den Nebenbedingungen A1 x ≤ b1 , A2 x ≥ b2 und/oder A3 x = b3 sowie x ≥ 0. Example 1.1 (17.8, Beispiel 1)
3x1 + 5x2 ≤ 3900 max 700x1 + 1000x2 unter x1 + 3x2 ≤ 2100 2x1 + 2x2 ≤ 2200
x1 , x2 ≥ 0.
Matrixformulierung: max a > x unter Ax ≤ b und x ≥ 0 mit
3 5 3900 700 a= , A = 1 3 , b = 2100 1000 !
2 2
Fortgeschrittene Mathematik: Optimierung (WiSe 09/10)
2200
3 / 27
Die R-Funktion simplex
Inhalt LP NLP
1 2 3 4 5 6
> > > > > >
library (boot) A > > > > > > > >
library (boot) A1 > > > > > >
library (boot) A1 > > > >
library ( linprog ) A > > > > >
library ( linprog ) A fkt 5 / 6 * x ^ 3 + 1 / 2 * x ^ 2 - 1} > interval optimize (fkt , interval , maximum = TRUE)
Definiere zu optimierende Funktion zu untersuchendes Intervall Funktionsaufruf
$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
Nichtlineare Programmierung
package: stats Befehl: optim
Inhalt LP NLP
Multivariate Optimierung einer Fkt. f : Rn → R. Example 2.3 (13.1, Beispiel 1) max f (x , y ) = −2x 2 − 2xy − 2y 2 + 36x + 42y − 158 1 2 3
> fkt -2 * x[2]^2 + 36 * x[1] + 42 * x[2] - 158} > optim (c(1 ,1) , fkt , control = list( fnscale = -1))
Definiere zu optimierende Funktion Funktionsaufruf
Benötigt werden Startpunkt, da iteratives Optimierungsverfahren, hier: x0 = (1, 1) zu optimierende Funktion falls maximiert werden soll, explizite Angabe: control =
list(fnscale = -1) Fortgeschrittene Mathematik: Optimierung (WiSe 09/10)
20 / 27
Die R-Funktion optim
Ausgabe: Inhalt LP NLP
Optimalpunkt Funktionswert im Optimalpunkt Anzahl der Funktionsaufrufe Status-Information (0 = Erfolg, 1 = max. Anzahl Iterationen, . . . ) zusätzliche Informationen $par [1] 5.000343 7.999936 $value [1] 100 $counts function gradient 87 NA $convergence [1] 0 $message NULL Fortgeschrittene Mathematik: Optimierung (WiSe 09/10)
21 / 27
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
Inhalt LP NLP
1 2 3 4 5 6
> gr -2 * x[1] - 4 * x[2] + 42)} > res control = list( fnscale = -1), > hessian = TRUE) > res$ hessian
[1,] [2,] 1
Definiere Gradienten Funktionsaufruf
Aufruf der Hesse-Matrix
[,1] [,2] -4 -2 -2 -4
> 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
Die R-Funktion constrOptim
package: stats Befehl: constrOptim
Inhalt LP NLP
Minimierung einer Fkt. f : Rn → R unter linearen Ungleichheitsnebenbedingungen der Form Ax ≥ b. Example 2.4 (14.8, Beispiel 2) max f (x , y ) = 1 2 3 4 5 6
> > > > > >
√
x+
√
y unter 3x + 5y ≤ 10
fkt nlm(fkt , c(1 ,1))
Definiere zu minimierende Funktion Funktionsaufruf
Benötigt werden Startpunkt, da iteratives Optimierungsverfahren, hier: x0 = (1, 1) zu optimierende Funktion Fortgeschrittene Mathematik: Optimierung (WiSe 09/10)
25 / 27
Die R-Funktion nlm
Ausgabe: Inhalt LP
Funktionswert im Minimum
NLP
Optimalpunkt Gradient der Lösung Status-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
Die R-Funktion nlm
Inhalt LP
weitere Optionen:
NLP
Angabe des Gradienten Ausgabe der Hesse-Matrix 1 2 3 4
> gr 2 * x[1] + 4 * x[2] - 42)} > res res$ hessian
Definiere Gradienten Funktionsaufruf Aufruf 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