DAY 14
1
AI & Data

# mcga套件

R Documetation : https://www.rdocumentation.org/packages/mcga

## Usage 用法

``````mcga(popsize, chsize, crossprob = 1.0, mutateprob = 0.01,
elitism = 1, minval, maxval, maxiter = 10, evalFunc)
``````

## Arguments 參數

`popsize` : 群體數量
`chsize` : 有多少參數
`crossprob` : 交配機率，預設值為 1.0
`mutateprob` : 突變機率，預設值為 0.01
`elitism` : 菁英策略，預設值為 1
`minval` : 最小數值
`maxval` : 最大數值
`maxiter` : 最多有多少代數，預設值為 10
`evalFunc` : 適應函數，預設情形都為求最小值

## Example 範例

### 下載套件

``````install.packages("mcga")
``````

### 引用套件

``````require("mcga")
``````

``````Loading required package: mcga
____    _
/ ___|  / \     Genetic
| |  _  / _ \    Algorithms
| |_| |/ ___ \
\____/_/   \_\  version 3.2
Type 'citation("GA")' for citing this R package in publications.

Attaching package: 'GA'

The following object is masked from 'package:utils':

de

Please use 'citation("mcga")' for citing the R package mcga
``````

``````citation("mcga")
``````

### 設定適應函數

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

### 執行基因演算法

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

### 印出結果

``````cat("Best chromosome:\n")
print(m\$population[1,])
cat("Cost: ",m\$costs[1],"\n")
``````

``````> cat("Best chromosome:\n")
Best chromosome:
> print(m\$population[1,])
[1] 3.958119e-01 1.549891e+01 7.770356e+02 7.777070e+03 7.777600e+04
> cat("Cost: ",m\$costs[1],"\n")
Cost:  3827.006
``````

(沒有沒關係，就表示你很聰明知道那是啥)

``````> print(m\$population[1,])
[1] 3.958119e-01 1.549891e+01 7.770356e+02 7.777070e+03 7.777600e+04
> print(m\$population[1,][1])
[1] 0.3958119
> print(m\$population[1,][2])
[1] 15.49891
> print(m\$population[1,][3])
[1] 777.0356
> print(m\$population[1,][4])
[1] 7777.07
> print(m\$population[1,][5])
[1] 77776
``````

[1]: `0.3958119`
[2]: `15.49891`
[3]: `777.0356`
[4]: `7777.07`
[5]: `77776`

``````f = (0.3958119-7)^2 + (15.49891-77)^2 +(777.0356-777)^2 +(7777.07-7777)^2 +(77776-77777)^2
``````

``````> cat("Cost: ",m\$costs[1],"\n")
Cost:  3827.006
``````

R Documetation : https://www.rdocumentation.org/packages/mcga