R 統計軟體 -- 優化方法 (人工智慧常用)

# 訊息

## English

• Using R for Data Analysis and Graphics , Introduction, Code and Commentary

Optimize 在台灣稱為最佳化，在中國大陸稱為優化。

# 一維空間優化方法：optimize()

## 範例一：\$optimize(f(x) = (x-1/3)^2) ; 0 \le x \le1\$

``````> f <- function (x) { r = (x-1/3)^2; cat("x=", x, "f(x)=", r, "\n"); r; }
> optimize(f, c(-7, 20))
x= 3.313082 f(x)= 8.878904
x= 9.686918 f(x)= 87.48954
x= -0.6261646 f(x)= 0.9206363
x= 0.3333333 f(x)= 2.496005e-31
x= 0.333374 f(x)= 1.656089e-09
x= 0.3332926 f(x)= 1.656089e-09
x= 0.3333333 f(x)= 2.496005e-31
\$minimum
[1] 0.3333333

\$objective
[1] 2.496005e-31

> optimize(f, c(0, 1))
x= 0.381966 f(x)= 0.002365137
x= 0.618034 f(x)= 0.08105446
x= 0.236068 f(x)= 0.009460549
x= 0.3333333 f(x)= 0
x= 0.3332926 f(x)= 1.656089e-09
x= 0.333374 f(x)= 1.656089e-09
x= 0.3333333 f(x)= 0
\$minimum
[1] 0.3333333

\$objective
[1] 0```
```

## 範例二：\$optimize(f(x) = x^2 * (x-1) = x^3 - x^2) ; 0 \le x \le 10\$

``````> ## See where the function is evaluated:
> optimize(function(x) x^2*(print(x)-1), lower=0, upper=10)
[1] 3.81966
[1] 6.18034
[1] 2.36068
[1] 2.077939
[1] 1.505823
[1] 0.9306496
[1] 0.9196752
[1] 0.772905
[1] 0.4776816
[1] 0.6491436
[1] 0.656315
[1] 0.6653777
[1] 0.6667786
[1] 0.6666728
[1] 0.6666321
[1] 0.6667135
[1] 0.6666728
\$minimum
[1] 0.6666728

\$objective
[1] -0.1481481```
```

## 範例三：optimize() 走錯方向了

``````> fp <- function(x) { cat("x=", x, "f(x)=", f(x), "\n"); f(x) }
> optimize(fp, c(-4, 20))# doesn't see the minimum
x= 5.167184 f(x)= 10
x= 10.83282 f(x)= 10
x= 14.33437 f(x)= 10
x= 16.49845 f(x)= 10
x= 17.83592 f(x)= 10
x= 18.66253 f(x)= 10
x= 19.1734 f(x)= 10
x= 19.48913 f(x)= 10
x= 19.68427 f(x)= 10
x= 19.80487 f(x)= 10
x= 19.8794 f(x)= 10
x= 19.92547 f(x)= 10
x= 19.95393 f(x)= 10
x= 19.97153 f(x)= 10
x= 19.9824 f(x)= 10
x= 19.98913 f(x)= 10
x= 19.99328 f(x)= 10
x= 19.99585 f(x)= 10
x= 19.99743 f(x)= 10
x= 19.99841 f(x)= 10
x= 19.99902 f(x)= 10
x= 19.99939 f(x)= 10
x= 19.99963 f(x)= 10
x= 19.99977 f(x)= 10
x= 19.99986 f(x)= 10
x= 19.99991 f(x)= 10
x= 19.99995 f(x)= 10
x= 19.99995 f(x)= 10
\$minimum
[1] 19.99995

\$objective
[1] 10

>
> optimize(fp, c(-7, 20))# o```
```

``````> optimize(fp, c(-7, 20))
x= 3.313082 f(x)= 0.6489993
x= 9.686918 f(x)= 10
x= -0.6261646 f(x)= 0.5406712
x= 1.244956 f(x)= 0.01686762
x= 1.250965 f(x)= 0.01859948
x= 0.771827 f(x)= 0.01249235
x= 0.2378417 f(x)= 0.2692639
x= 1.000451 f(x)= 0
x= 0.9906964 f(x)= 2.088206e-47
x= 0.9955736 f(x)= 7.684301e-99
x= 0.9980122 f(x)= 3.320444e-219
x= 0.9992315 f(x)= 0
x= 0.9998411 f(x)= 0
x= 0.9996083 f(x)= 0
x= 0.9994644 f(x)= 0
x= 0.9993754 f(x)= 0
x= 0.9993204 f(x)= 0
x= 0.9992797 f(x)= 0
x= 0.9992797 f(x)= 0
\$minimum
[1] 0.9992797

\$objective
[1] 0

>```
```

# 非線性優化方法：nlm() 最小化

``````> f <- function(x) sum((x-1:length(x))^2)
> nlm(f, c(10,10))
\$minimum
[1] 4.303458e-26

\$estimate
[1] 1 2

[1]  2.757794e-13 -3.099743e-13

\$code
[1] 1

\$iterations
[1] 2

> nlm(f, c(10,10), print.level = 2)
iteration = 0
Step:
[1] 0 0
Parameter:
[1] 10 10
Function Value
[1] 145
[1] 18.00001 16.00001

iteration = 1
Step:
[1] -9 -8
Parameter:
[1] 1 2
Function Value
[1] 1.721748e-13
[1] 1.551336e-06 1.379735e-06

iteration = 2
Parameter:
[1] 1 2
Function Value
[1] 4.303458e-26
[1]  2.757794e-13 -3.099743e-13

\$minimum
[1] 4.303458e-26

\$estimate
[1] 1 2

[1]  2.757794e-13 -3.099743e-13

\$code
[1] 1

\$iterations
[1] 2

> utils::str(nlm(f, c(5), hessian = TRUE))
List of 6
\$ minimum   : num 2.44e-24
\$ estimate  : num 1
\$ hessian   : num [1, 1] 2
\$ code      : int 1
\$ iterations: int 1
>
> f <- function(x, a) sum((x-a)^2)
> nlm(f, c(10,10), a=c(3,5))
\$minimum
[1] 3.371781e-25

\$estimate
[1] 3 5

[1]  6.750156e-13 -9.450218e-13

\$code
[1] 1

\$iterations
[1] 2

> f <- function(x, a)
+ {
+     res <- sum((x-a)^2)
+     res
+ }
> nlm(f, c(10,10), a=c(3,5))
\$minimum
[1] 0

\$estimate
[1] 3 5

[1] 0 0

\$code
[1] 1

\$iterations
[1] 1

>
> ## more examples, including the use of derivatives.
> ## Not run: demo(nlm)
>```
```

# 多維空間優化方法 optim()

1. 參考：R 學習筆記/數學應用‎/數值分析/最佳化（Numerical Optimization）

# 線性不等式限制區求最小值: constrOptim()

1. constrOptim {stats} Linearly Constrained Optimization

page revision: 14, last edited: 20 Dec 2013 09:59