# Compact Genetic Algorithm

## cgasample

`ErrorsInVariables.CGA.cgasample`

— FunctionGenerates a binary vector of values using a probability vector. Each single element of the probability vector is the probability of bit having the value of 1. When the probability vector is [1, 1, 1, ..., 1] then the sampled vector is [1.0, 1.0, 1.0, ..., 1.0] whereas it is [0.0, 0.0, 0.0, ..., 0.0] when the probability vector is a vector of zeros. The CGA (compact genetic algorithms) search is started using the probability vector of [0.5, 0.5, 0.5, ..., 0.5] which produces random vectors of either zeros or ones.

**Examples**

```
julia> sample([1, 1, 1, 1, 1])
5-element Vector{Int}:
1
1
1
1
1
julia> cgasample(ones(10) * 0.5)
10-element Vector{Int}:
1
1
1
1
0
0
0
1
1
0
```

## cga

`ErrorsInVariables.CGA.cga`

— FunctionPerforms a CGA (Compact Genetic Algorithm) search for minimization of an objective function. In the example below, the objective function is to minimize sum of bits of a binary vector. The search method results the optimum vector of [0, 0, ..., 0] where the objective function is zero.

**Examples**

```
julia> function f(x)
return sum(x)
end
f (generic function with 1 method)
julia> cga(chsize = 10, costfunction = f, popsize = 100)
10-element Vector{Int}:
0
0
0
0
0
0
0
0
0
0
```