Skip to content

Commit

Permalink
Add test for new representation of environments structures
Browse files Browse the repository at this point in the history
  • Loading branch information
HervierLucas committed Nov 9, 2020
1 parent ca74889 commit 40fc012
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
44 changes: 44 additions & 0 deletions tests/cgp_sokolvl.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#--------------------Set-up for tests--------------------
cfg = Cambrian.get_config("tests/test_cgp.yaml")

width = cfg.width
height = cfg.height
agent_idx = cfg.agent_idx
objects_char_list = cfg.objects_char_list
nb_object = length(objects_char_list)

mutate(i::CGPSokoLvl) = CGPSokoLvl(i.width,i.height,i.objects_char_list,i.agent_idx,i.output_map,goldman_mutate(cfg,i.cgp))

@testset "CGPSokoLvl" begin
cgp_lvl = CGPSokoLvl(cfg)
@test cgp_lvl.width == width
@test cgp_lvl.height == height
@test cgp_lvl.agent_idx == agent_idx
@test cgp_lvl.objects_char_list == objects_char_list
@test typeof(cgp_lvl.cgp) == CartesianGeneticProgramming.CGPInd

# expected_output_map = """
# bbbb
# bAbb
# bbbb
# bbbb
# """

lvl_str = write_map!(cgp_lvl)
# @test lvl_str == expected_output_map
# @test cgp_lvl.output_map[1] == expected_output_map
@test lvl_str == cgp_lvl.output_map[1]
println(lvl_str)

new_cgp = mutate(cgp_lvl)
@test new_cgp.cgp.chromosome != cgp_lvl.cgp.chromosome
cgp_lvl.cgp.fitness[1] = 0
save_ind(cgp_lvl,"tests//save_cgp.dna")
load_str = read("tests//save_cgp.dna", String)
load_ind = CGPSokoLvl(cfg,load_str)
@test load_ind.cgp.genes == cgp_lvl.cgp.genes
@test load_ind.width == cgp_lvl.width
@test load_ind.height == cgp_lvl.height
@test load_ind.objects_char_list == cgp_lvl.objects_char_list
@test load_ind.output_map == cgp_lvl.output_map
end
13 changes: 8 additions & 5 deletions tests/run_tests.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using Griddly
using Cambrian
using CartesianGeneticProgramming
using Flux
using JSON
import Cambrian: mutate, populate, get_child
using Test

# include("../src/sokolvl_individual.jl")
include("../src/sokolvl_individual.jl")
include("../src/utils.jl")
# include("../src/soko_agent.jl")
include("../src/soko_agent.jl")
include("../src/continuous_sokolvl.jl")
include("../src/cgp_sokolvl.jl")

# include("sokolvl_individual.jl")
# include("utils.jl")
# include("soko_agent.jl")
include("sokolvl_individual.jl")
include("utils.jl")
include("soko_agent.jl")
include("continuous_sokolvl.jl")
include("cgp_sokolvl.jl")
1 change: 1 addition & 0 deletions tests/save_cgp.dna
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"width":8,"height":8,"objects_char_list":["b", "w", "h", "A", "."],"agent_idx":4,"output_map":["bbbbbbbb\nbbbbbbbb\nAbbbbbbb\nbbbbbbbb\nbbbbbbbb\nbbbbbbbb\nbbbbbbbb\nbbbbbbbb\n"],"chromosome":[0.43557019650983153, 0.7060337251934781, 0.03804949241006006, 0.7014197479149484, 0.8791616922976895, 0.3126906063663164, 0.3428255430024134, 0.924885974553822, 0.843313436295515, 0.005435752201748079, 0.9769155334560831, 0.8894324801833102, 0.4614003147718264, 0.9630858557612276, 0.7174818999124548, 0.8252125065487477, 0.5624258089777101, 0.488790611370012, 0.679719879104629, 0.5548338558904202, 0.7816355634696495, 0.45097329674352604, 0.7031240519997548, 0.1899752478270611, 0.7041504537704375, 0.49639548869203387, 0.5363000194577618, 0.7730415377005473, 0.38594180135300693, 0.7254165215500266, 0.40122852047152757, 0.3240322450722284, 0.2817668219816305, 0.12378528863209692, 0.6331171723189579, 0.6047586841901791, 0.2266868803909614, 0.4724080002148563, 0.5199869044674912, 0.46585753878026215, 0.9447445272578818, 0.9567290620521325, 0.36576437810332574, 0.9234279543106525, 0.9355019557377842, 0.8799273429363614, 0.9362260518156464, 0.5666457895549961, 0.23395497808163035, 0.516635169214954, 0.5982276292778645, 0.7982806119522614, 0.2970875523651473, 0.16670786003422533, 0.4129335726653238, 0.002572472756921629, 0.6057630840925985, 0.4036816675354502, 0.20790522157601998, 0.17152950997126726, 0.3596480060728433, 0.8555069633913497, 0.5408627068547116, 0.6261481768504091, 0.8398335029268578, 0.011696627457347342, 0.34751181183978885, 0.7126716387007428, 0.4262990647617979, 0.03039944777236503, 0.23034439713076615, 0.619474947334026, 0.4922860766773718, 0.23185306555703655, 0.04239122870916168, 0.7215527707179283, 0.9194517831605307, 0.45921724164235744, 0.46100066475761103, 0.9763751046273879, 0.4100105395046836, 0.7371025662485553, 0.8320799169900301, 0.3848044596598541, 0.922203279670853, 0.7349147557826292, 0.7572291661096238, 0.3869119383478834, 0.5819348949139127, 0.08259713580810035, 0.27099722013062433, 0.7724779490538836, 0.4590870097050137, 0.27355652813711395, 0.33557604060117563],"fitness":[0.0]}
30 changes: 30 additions & 0 deletions tests/test_cgp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Sokoban level settings
width: 8
height: 8
objects_char_list: ["b","w","h","A","."]
agent_idx: 4
# Cambrian.jl settings
seed: 0
d_fitness: 1
n_population: 5
n_elite: 1
n_gen: 10000
log_gen: 1
save_gen: 100
m_rate: 0.1
# CartesianGeneticProgramming.jl settings
rows: 3
columns: 10
recur: 0.2
n_in: 4
n_out: 5
out_m_rate: 0.3
functions:
- f_add
- f_subtract
- f_mult
- f_abs
- f_exp
- f_sin
- f_lt
- f_gt

0 comments on commit 40fc012

Please sign in to comment.