From: Brendan Hansen Date: Fri, 15 Mar 2019 01:13:10 +0000 (-0500) Subject: Fixed training issues and added terminal based training X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=a6eec97fd7b054e8a3eb2c6e3e0de771bd75da7e;p=genetic-shooter.git Fixed training issues and added terminal based training --- diff --git a/conf.lua b/conf.lua index 5015ca4..3cd3078 100644 --- 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; diff --git a/main.lua b/main.lua index 71c7094..cea19bb 100644 --- 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) diff --git a/src/trainer.lua b/src/trainer.lua index bc2af9f..2c8b14d 100644 --- a/src/trainer.lua +++ b/src/trainer.lua @@ -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 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