Skip to content
Permalink
Newer
Older
100644 115 lines (99 sloc) 3.97 KB
Stephen
Feb 6, 2018
1
#=
2
Parents <— {randomly generated population}
3
While not (termination criterion)
4
Calculate the fitness of each parent in the population
5
Children <- 0
6
While | Children | < | Parents |
7
Use fitnesses to probabilistically select a pair of parents for mating
8
Mate the parents to create children c\ and c<i
9
Children <— Children U {ci,C2}
10
Loop
11
Randomly mutate some of the children
12
Parents <— Children
13
Next generation
14
15
Use continuous GA, not binary
16
17
include array of independent variables along with associated range
18
19
=#
20
21
function initialize_population(populationSize::Int, chromosomeSize::Int, bounds::Array{Tuple{Float64, Float64},1})
22
"""
23
Initializes the population based on the given population and chromosome size
24
Values will be from lb to ub (lower to upper bound)
25
"""
26
27
# Check to see if we dont have enough bounds or have too many
28
if length(bounds) != chromosomeSize
29
println("Length of bounds ($(length(bounds))) not equal to specified chromosome size ($chromosomeSize)")
30
exit()
31
end
32
33
# Init population
34
population = Array{Float64}(0,chromosomeSize+1)
35
36
# Foreach member in population
37
for i = 1:populationSize
38
# Init the member
39
row = []
40
# Create member elements based on bounds
41
for bound in bounds
42
push!(row, rand(bound[1]:0.10:bound[2]))
43
end
44
# Add another element to the end to keep score
45
push!(row, 1.0)
46
47
# hcat row, append to 2D population matrix
48
population = cat(1, population, hcat(row...))
49
end
50
51
return population
52
53
end
54
55
function score_population(population::Array{Float64}, fitness_function::Function, populationSize::Int, chromosomeSize::Int)
56
"""
57
Args
58
population: The population to score. Must be 2D array of Float64 elements
59
fitness_function: Function to score the population
60
Must be able to take array as argument
61
For example f(x,y) should be f(A) where A[1]=x, A[2]=y
62
Returns scored population. Score is the last element of each chromosome (row)
63
in population
64
"""
65
#= 1-3, 4-6, 7-9, 10-12
66
1 2 3 4
67
=#
68
population = population'
69
for i in 0:populationSize-1
70
println(i)
71
member = population[(chromosomeSize+1)*i+1:(chromosomeSize+1)*(i+1)]
72
println(member)
73
population[(chromosomeSize+1)*(i+1)] = fitness_function(member)
74
end
75
println(population)
76
return population
77
end
78
79
function GA(populationSize::Int, chromosomeSize::Int, fitness_function::Function, bounds::Array{Tuple{Float64, Float64},1})
80
"""
81
Args
82
populationSize: Total number of chromosomes
83
chromosomeSize: How long each chromosome should be (variables in fitness function)
84
fitness_function: Function to determine fitness of each solution
85
Should take an array as an arg
86
Example: f(x,y,z) = x+y+z
87
should be f(X) = X[1]+X[2]+X[3]
88
in order to allow GA to take in any function with any
89
amount of args to the fitness function
90
bounds: 1D array of tuples, each tuple is the (lower, upper) bounds
91
for each variable. Both lower and upper need to be Float64 types
92
Length should match chromosomeSize
93
e.g: [(1.0,2.0), (3.5,4.5)]
94
"""
95
96
# Set recombination and mutation rate, lower and upper bound
97
recombRate = 0.7
98
mutateRate = 0.05
99
maxIterations = 1
100
# First initialize the population
101
population = initialize_population(populationSize, chromosomeSize, bounds)
102
i = 0
103
while i < maxIterations
104
scoredPopulation = score_population(population, fitness_function, populationSize, chromosomeSize)
105
106
i += 1
107
108
end
109
end
110
111
function ackley(X)
112
return e - 20*exp(-0.2*sqrt((X[1]^2 + X[2]^2)/2) - exp((cos(2*pi*X[1]) + cos(2*pi*X[2]))/2))
113
end
114
bounds = [(-2.0,2.0), (-2.0,2.0)]
115
GA(25,2,ackley,bounds)
You can’t perform that action at this time.