2019-08-14 17:50:50 +00:00
|
|
|
import ExcelReaders
|
|
|
|
import DataFrames
|
|
|
|
|
2019-08-15 16:28:41 +00:00
|
|
|
POPSIZE = 5
|
|
|
|
mu = 1
|
|
|
|
lambda = 4
|
2019-08-16 13:52:28 +00:00
|
|
|
GENLIMIT = 20
|
2019-08-19 10:15:29 +00:00
|
|
|
TARGETCALORIES = 2000
|
2019-08-21 13:35:37 +00:00
|
|
|
CANDIDATESIZE = 4
|
2019-08-15 16:28:41 +00:00
|
|
|
|
2019-08-14 17:50:50 +00:00
|
|
|
data = ExcelReaders.readxlsheet("./data/nutrional_information_5917.xlsx", "Sheet2", skipstartrows=1)
|
|
|
|
header = ExcelReaders.readxlsheet("./data/nutrional_information_5917.xlsx", "Sheet2", nrows=1)
|
|
|
|
|
|
|
|
# Convert to symbols to build header row.
|
|
|
|
for i = 1:length(header)
|
|
|
|
tmp = header[i]
|
|
|
|
tmp = Symbol(tmp)
|
|
|
|
header[i] = tmp
|
|
|
|
end
|
|
|
|
header = dropdims(reshape(header, :, 1), dims=2)
|
|
|
|
df = DataFrames.DataFrame()
|
|
|
|
|
|
|
|
# Finally, construct our dataframe
|
|
|
|
for i = 1:length(header)
|
|
|
|
df[header[i]] = data[2:end, i]
|
|
|
|
end
|
|
|
|
|
2019-08-19 10:15:29 +00:00
|
|
|
"""
|
2019-08-19 10:20:10 +00:00
|
|
|
Our mutator function
|
|
|
|
|
|
|
|
steps through the parent, and randomly selects the
|
|
|
|
allelles to delete. Will replace the alleles with new
|
|
|
|
alleles (meals).
|
2019-08-19 10:15:29 +00:00
|
|
|
"""
|
2019-08-16 13:52:28 +00:00
|
|
|
function mutate(parent)
|
2019-08-19 10:15:29 +00:00
|
|
|
|
|
|
|
# Copy the parent so we can do some work.
|
|
|
|
child = deepcopy(parent)
|
|
|
|
toDelete = []
|
|
|
|
|
|
|
|
for i in 1:size(parent, 1)
|
|
|
|
if rand(Float64) > 0.5 # NOTE: make this tunable
|
|
|
|
push!(toDelete, i)
|
|
|
|
end
|
|
|
|
# If we get tails, delete the row and push a new one to it.
|
|
|
|
end
|
|
|
|
|
|
|
|
# Delete all rows we don't want at once.
|
|
|
|
DataFrames.deleterows!(child, toDelete)
|
|
|
|
|
|
|
|
# Add new random rows from the ones we deleted
|
2019-08-19 15:48:31 +00:00
|
|
|
for i in 1:length(toDelete)
|
2019-08-19 10:15:29 +00:00
|
|
|
push!(child, df[randRow(), :])
|
|
|
|
end
|
|
|
|
|
|
|
|
child
|
2019-08-14 17:50:50 +00:00
|
|
|
end
|
|
|
|
|
2019-08-16 13:52:28 +00:00
|
|
|
"""
|
|
|
|
fitness(candidate::DataFrames.DataFrame)
|
|
|
|
|
|
|
|
Calculate the fitness of the candidate, which is the
|
|
|
|
absolute value of the difference of TARGETCALORIES and the sum
|
|
|
|
of all calories in the meal.
|
|
|
|
"""
|
2019-08-15 16:28:41 +00:00
|
|
|
function fitness(candidate::DataFrames.DataFrame)
|
2019-08-16 13:52:28 +00:00
|
|
|
abs(TARGETCALORIES - sum(+, candidate[:Calories]))
|
2019-08-14 17:50:50 +00:00
|
|
|
end
|
|
|
|
|
2019-08-19 10:20:10 +00:00
|
|
|
"""
|
|
|
|
Helper function to generate a random row index
|
|
|
|
used by randomCandidate
|
|
|
|
"""
|
2019-08-14 14:15:24 +00:00
|
|
|
function randRow()
|
|
|
|
# Generate a random row index
|
2019-08-16 14:28:51 +00:00
|
|
|
abs(rand(Int) % size(df, 1)) + 1
|
2019-08-14 14:15:24 +00:00
|
|
|
end
|
|
|
|
|
2019-08-19 10:20:10 +00:00
|
|
|
"""
|
|
|
|
Helper function to generate a random candidate from the dataset.
|
|
|
|
|
|
|
|
n is the size of the candidate.
|
|
|
|
"""
|
2019-08-15 16:28:41 +00:00
|
|
|
function randomCandidate(n::Integer)
|
2019-08-14 14:15:24 +00:00
|
|
|
# Select n random rows from the dataset.
|
|
|
|
rows = [randRow() for i = 1:n]
|
|
|
|
df[rows, :]
|
2019-08-15 16:28:41 +00:00
|
|
|
end
|
|
|
|
|
2019-08-19 10:20:10 +00:00
|
|
|
|
|
|
|
"""
|
2019-08-19 15:48:31 +00:00
|
|
|
generateInitialPopulation(lambda::Integer, candidateSize::Integer)
|
2019-08-19 10:20:10 +00:00
|
|
|
|
|
|
|
From our dataset, generate an array of initial candidates to begin the search.
|
|
|
|
"""
|
2019-08-16 13:52:28 +00:00
|
|
|
function generateInitialPopulation(lambda::Integer, candidateSize::Integer)
|
|
|
|
[randomCandidate(candidateSize) for i = 1:lambda]
|
2019-08-15 16:28:41 +00:00
|
|
|
end
|
|
|
|
|
2019-08-21 13:35:37 +00:00
|
|
|
function main(genLimit)
|
2019-08-16 13:52:28 +00:00
|
|
|
# Generate the initial population.
|
2019-08-21 13:35:37 +00:00
|
|
|
pop = generateInitialPopulation(lambda, CANDIDATESIZE)
|
2019-08-16 13:52:28 +00:00
|
|
|
best = nothing
|
|
|
|
generationNum = 0
|
|
|
|
fit = nothing
|
|
|
|
parents = nothing
|
2019-08-21 13:35:37 +00:00
|
|
|
results = Dict{Integer, Float64}()
|
2019-08-16 13:52:28 +00:00
|
|
|
|
2019-08-21 13:35:37 +00:00
|
|
|
while generationNum <= genLimit
|
2019-08-16 13:52:28 +00:00
|
|
|
# Assess the fitness of parents
|
|
|
|
for parent in pop
|
|
|
|
fit = fitness(parent)
|
|
|
|
if best === nothing || fit < fitness(best)
|
|
|
|
best = parent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-19 10:20:10 +00:00
|
|
|
# Grab our best fitness for logging purposes.
|
2019-08-16 13:52:28 +00:00
|
|
|
bestFitness = fitness(best)
|
2019-08-21 13:35:37 +00:00
|
|
|
results[generationNum+1] = bestFitness
|
2019-08-16 13:52:28 +00:00
|
|
|
|
2019-08-19 10:20:10 +00:00
|
|
|
# Copy the best mu parents into the population.
|
2019-08-16 13:52:28 +00:00
|
|
|
sort!(pop, by = x -> fitness(x))
|
|
|
|
parents = pop[1:mu]
|
2019-08-19 09:10:45 +00:00
|
|
|
pop = deepcopy(parents)
|
2019-08-16 13:52:28 +00:00
|
|
|
|
2019-08-19 10:20:10 +00:00
|
|
|
# Employ our (mu + lambda) strategy by generating lambda/mu kids.
|
2019-08-16 13:52:28 +00:00
|
|
|
for p in parents
|
2019-08-19 09:10:45 +00:00
|
|
|
for i = 1:(lambda/mu)
|
2019-08-16 13:52:28 +00:00
|
|
|
push!(pop, mutate(p))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-21 13:35:37 +00:00
|
|
|
# println("Generation $generationNum, best $best, fitness $bestFitness")
|
2019-08-16 13:52:28 +00:00
|
|
|
generationNum += 1
|
|
|
|
end
|
|
|
|
|
2019-08-21 13:35:37 +00:00
|
|
|
results
|
|
|
|
|
2019-08-16 13:52:28 +00:00
|
|
|
end
|
2019-08-15 16:28:41 +00:00
|
|
|
# search(generateInitialPopulation())
|