Skip to content

Commit

Permalink
logs as a CSV, config and tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
d9w committed Sep 5, 2020
1 parent afbec82 commit 145857b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 23 deletions.
1 change: 0 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SharedArrays = "1a1011a3-84de-559e-8e89-a11a2f7dc383"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"

[compat]
Expand Down
1 change: 0 additions & 1 deletion src/Cambrian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ using Statistics
using YAML
using JSON
import Formatting
import UUIDs
import Dates

include("config.jl")
Expand Down
6 changes: 5 additions & 1 deletion src/GA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ evaluate(e::GAEvo) = fitness_evaluate(e, e.fitness)
"""
populate(e::GAEvo)
"""
function populate(e::GAEvo)
function ga_populate(e::AbstractEvolution)
new_pop = Array{Individual}(undef, 0)
if e.config.n_elite > 0
sort!(e.population)
Expand All @@ -53,3 +53,7 @@ function populate(e::GAEvo)
end
e.population = new_pop
end

function populate(e::GAEvo)
ga_populate(e)
end
22 changes: 15 additions & 7 deletions src/config.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export get_base_config, get_config
export get_config

function get_base_config()
(seed = 0, # evolution seed
Expand All @@ -7,17 +7,25 @@ function get_base_config()
n_elite = 1, # elites to carry over each generation
n_gen = 10, # number of generations
log_gen = 1, # log every x generations
save_gen = 1 # save population every x generation
save_gen = 1, # save population every x generation
id = string(Dates.now()) # run id
)
end

"convert Dict to named tuple"
function get_config(cfg::Dict)
(; (Symbol(k)=>v for (k, v) in cfg)...)
end

"combine YAML file and kwargs, make sure ID is specified"
function get_config(cfg_file::String; kwargs...)
cfg = YAML.load_file(cfg_file)
f_cfg = (; (Symbol(k) => v for (k,v) in cfg)...)
k_cfg = (; (k=>v for (k, v) in kwargs)...)
cfg = merge(f_cfg, k_cfg)
for (k, v) in kwargs
cfg[String(k)] = v
end
# generate id, use date if no existing id
if ~(:id in keys(cfg))
cfg = merge(cfg, (; id = string(UUIDs.uuid4())))
cfg["id"] = string(Dates.now())
end
cfg
get_config(cfg)
end
2 changes: 1 addition & 1 deletion src/evolution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function log_gen(e::AbstractEvolution)
for d in 1:e.config.d_fitness
maxs = map(i->i.fitness[d], e.population)
with_logger(e.logger) do
@info Formatting.format("{1:04d} {2:e} {3:e} {4:e}",
@info Formatting.format("{1:04d},{2:e},{3:e},{4:e}",
e.gen, maximum(maxs), mean(maxs), std(maxs))
end
end
Expand Down
18 changes: 7 additions & 11 deletions src/individual.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export Individual, BoolIndividual, FloatIndividual, crossover, mutate
import Base: isless, print
import Base: isless, show

"""
every Individual subtype needs to implement:
Expand All @@ -25,23 +25,19 @@ function isless(i1::Individual, i2::Individual)
end

function ind_parse(st::String)
dict = JSON.Parser.parse(st)
for i in 1:length(dict["fitness"])
if dict["fitness"][i] == nothing
dict["fitness"][i] = -Inf
d = JSON.Parser.parse(st)
for i in 1:length(d["fitness"])
if d["fitness"][i] == nothing
d["fitness"][i] = -Inf
end
end
dict
d
end

function print(io::IO, ind::Individual)
function show(io::IO, ind::Individual)
print(io, JSON.json(ind))
end

function String(ind::Individual)
string(ind)
end

"default crossover method, using uniform crossover for N parents"
function crossover(parents::Vararg{Individual})
l = minimum(map(i->length(i.genes), parents))
Expand Down
2 changes: 1 addition & 1 deletion src/logger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function handle_message(logger::CambrianLogger, level, message, _module, group,
iob = IOContext(buf, logger.stream)
levelstr = level == Warn ? "Warning" : string(level)
msglines = split(chomp(string(message)), '\n')
println(iob, Dates.now(), "\tCambrian ", levelstr, " ", msglines[1])
println(iob, Dates.now(), ",Cambrian,", levelstr, ",", msglines[1])
for i in 2:length(msglines)
println(iob, "", msglines[i])
end
Expand Down

0 comments on commit 145857b

Please sign in to comment.