Fixed training issues and added terminal based training
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 15 Mar 2019 01:13:10 +0000 (20:13 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 15 Mar 2019 01:13:10 +0000 (20:13 -0500)
conf.lua
main.lua
src/trainer.lua
train.lua [new file with mode: 0644]

index 5015ca4f9b138c9a1b801cf24f584a34e570cf2b..3cd30786466396fbcc7071bdf71c2419d42bf83a 100644 (file)
--- a/conf.lua
+++ b/conf.lua
@@ -2,6 +2,7 @@
 local WINDOW_WIDTH = 1200
 local WINDOW_HEIGHT = 800
 
+local love = love or {}
 function love.conf(t)
        t.window.title = "Maching Learning Game"
 
@@ -43,8 +44,8 @@ local KEYMAP = {
 
 return {
        -- GENERAL PROPERTIES
-       LOAD_FILE = "";
-       SAVE_FILE = "./saved/POPULATION";
+       LOAD_FILE = "./saved/TERM_1_GEN_14";
+       SAVE_FILE = "./saved/TERM";
 
        WINDOW_WIDTH = WINDOW_WIDTH;
        WINDOW_HEIGHT = WINDOW_HEIGHT;
index 71c70940128fcdbe6394c7759e239a26863a9462..cea19bbc514d3ad43f7a605566bd962ec91bea51 100644 (file)
--- a/main.lua
+++ b/main.lua
@@ -7,30 +7,23 @@ require "src.data"
 local Trainer = (require "src.trainer").Trainer
 
 local World = world_mod.World
-local Enemy = world_mod.Enemy
 local Wall = world_mod.Wall
 local Population = Gen.Population
 
-local world, player
+local world
 local input
 local pop
 local trainer
 
-local update_speed = 30
-
 local ui_font
 local fitness_font
 
 local stored_fitnesses = {}
 
-local enemies = {}
 function love.load()
        math.randomseed(os.time())
 
-       world, player = World.new()
-       local enemy = Enemy.new(0, 0)
-       table.insert(enemies, enemy)
-       world:add_entity(enemy)
+       world = World.new()
 
        local wall1 = Wall.new(-20, -20, 840, 20)
        local wall2 = Wall.new(-20, 600, 840, 20)
index bc2af9f4eda7acf37f70956efd0e9bda029ad763..2c8b14df0fd12d721aa3101b1340eb24b4cfee5d 100644 (file)
@@ -23,6 +23,8 @@ function Trainer.new(population, world, input)
 end
 
 function Trainer:initialize_training()
+       self.world:spawn_enemies(1)
+
        self.population_step = self.population:start_training()
 
        self.after_inputs_func = function(...)
@@ -100,7 +102,11 @@ function Trainer:after_inputs(inputs, dt)
        return fitness, self.player.alive
 end
 
-function Trainer:pre_evolution(_, _, _)
+function Trainer:pre_evolution(avg, high, _)
+       print("FINISHED GENERATION: " .. self.population.generation .. " | Stats: ")
+       print("        Average: " .. tostring(avg))
+       print("        Highest: " .. tostring(high))
+       print("--------------------------------------------------------------------")
 end
 
 function Trainer:post_evolution(_)
diff --git a/train.lua b/train.lua
new file mode 100644 (file)
index 0000000..107c1fd
--- /dev/null
+++ b/train.lua
@@ -0,0 +1,43 @@
+local CONF = require "conf"
+local world_mod = require "src.world"
+local Input = require "src.input"
+local Gen = require "src.genetics"
+require "src.data"
+local Trainer = (require "src.trainer").Trainer
+
+local World = world_mod.World
+local Wall = world_mod.Wall
+local Population = Gen.Population
+
+local world, input, pop, trainer
+
+math.randomseed(os.time())
+
+world = World.new()
+
+local wall1 = Wall.new(-20, -20, 840, 20)
+local wall2 = Wall.new(-20, 600, 840, 20)
+local wall3 = Wall.new(-20, 0, 20, 600)
+local wall4 = Wall.new(800, 0, 20, 600)
+world:add_entity(wall1)
+world:add_entity(wall2)
+world:add_entity(wall3)
+world:add_entity(wall4)
+
+input = Input:new()
+
+if CONF.LOAD_FILE == "" then
+       pop = Population.new()
+       pop:create_genomes(CONF.POPULATION_SIZE, 16, 8)
+else
+       pop = Population.load(CONF.LOAD_FILE)
+end
+
+trainer = Trainer.new(pop, world, input)
+trainer:initialize_training()
+trainer.max_speed = 360
+trainer:change_speed(360)
+
+while pop.generation <= 100 do
+       trainer:update(1 / 60)
+end