DAY 16
0
AI & Data

## 【Day16】GA with you - R Package mcga R套件mcga (3)

``````f<-function(x){
return ((x[1]-7)^2 + (x[2]-77)^2 +(x[3]-777)^2 +(x[4]-7777)^2 +(x[5]-77777)^2)
}
``````

`f`要尋求x[1]、x[2]、x[3]、x[4]、x[5]五個數值最佳的答案，來求出其最小值

``````x[1]=7
x[2]=77
x[3]=777
x[4]=7777
x[5]=77777
``````

``````m <- mcga( popsize=200,
chsize=5,
minval=0.0,
maxval=999999999.9,
maxiter=2500,
crossprob=1.0,
mutateprob=0.01,
evalFunc=f)
``````

### 修改上限值

``````> m1 <- mcga(popsize=200,
+            chsize=5,
+            minval=0.0,
+            maxval=99999.9,
+            maxiter=2500,
+            crossprob=1.0,
+            mutateprob=0.01,
+            evalFunc=f)
> cat("Best chromosome:\n")
Best chromosome:
> print(m1\$population[1,])
[1]     3.110001    75.989349   767.962032  7776.994563 77777.003116
> cat("Cost: ",m1\$costs[1],"\n")
Cost:  97.83841
``````

### 玩弄族群數

``````> m1p <- mcga(popsize=2000,
+             chsize=5,
+             minval=0.0,
+             maxval=99999.9,
+             maxiter=2500,
+             crossprob=1.0,
+             mutateprob=0.01,
+             evalFunc=f)
> cat("Best chromosome:\n")
Best chromosome:
> print(m1p\$population[1,])
[1]     7.00000    76.40625   777.00000  7777.00000 77777.00000
> cat("Cost: ",m1p\$costs[1],"\n")
Cost:  0.3525391
``````

### 玩弄代數

``````> m1i <- mcga( popsize=200,
+              chsize=5,
+              minval=0.0,
+              maxval=99999.9,
+              maxiter=25000,
+              crossprob=1.0,
+              mutateprob=0.01,
+              evalFunc=f)
> cat("Best chromosome:\n")
Best chromosome:
> print(m1p\$population[1,])
[1]     7.00000    76.40625   777.00000  7777.00000 77777.00000
> cat("Cost: ",m1p\$costs[1],"\n")
Cost:  0.3525391
``````

Command: click
Result :