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-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
|
|
|
|
|
|
|
|
function breeder(parent)
|
|
|
|
end
|
|
|
|
|
2019-08-15 16:28:41 +00:00
|
|
|
function fitness(candidate::DataFrames.DataFrame)
|
|
|
|
sum(+, candidate[:Calories])
|
2019-08-14 17:50:50 +00:00
|
|
|
end
|
|
|
|
|
2019-08-15 16:28:41 +00:00
|
|
|
function breed(candidates::Array{DataFrames.DataFrame})
|
2019-08-14 17:50:50 +00:00
|
|
|
# Truncation selection, top 3 as parents.
|
|
|
|
# First, check everyone's fitness.
|
|
|
|
# Then, generate new solutions by selecting parents and breeding
|
|
|
|
sort!(candidates, by = x -> fitness(x))
|
|
|
|
parents = candidates[1:3]
|
|
|
|
end
|
|
|
|
|
2019-08-14 14:15:24 +00:00
|
|
|
function randRow()
|
|
|
|
# Generate a random row index
|
|
|
|
abs(rand(Int) % size(df, 1))
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
|
|
|
function generateInitialPopulation()
|
|
|
|
[randomCandidate(5) for i = 1:POPSIZE]
|
|
|
|
end
|
|
|
|
|
|
|
|
# search(generateInitialPopulation())
|