Skip to content

Commit

Permalink
fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaGupta1 committed Oct 1, 2018
1 parent 7d4542e commit 53b535d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea/
.idea/
*.state
*.nes
28 changes: 18 additions & 10 deletions WarIO.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,12 @@ function getRandomGenome(number)
return genome
end

local currentBestFitness
local currentBestFitnessNoTime
local oldFitnessNoTime
local totalPreviousFitness
local finishedLevel = false

function testSpecie(specie, blocks)
currentBestFitness = 0
oldFitnessNoTime = 0
totalPreviousFitness = 0

-- First Layer
local nodes1 = specie.layer1
for i = 0, 16 do
Expand Down Expand Up @@ -137,14 +133,14 @@ function testSpecie(specie, blocks)
buttons["P1 Select"] = false
joypad.set(buttons)

local currentFitnessNoTime = memory.readbyte(0x0086) + 256 * memory.readbyte(0x006D)
local currentFitnessNoTime = memory.readbyte(0x0086) + 256 * memory.readbyte(0x006D) + totalPreviousFitness
currentBestFitnessNoTime = math.max(currentFitnessNoTime, currentBestFitnessNoTime)
local time = 0;
time = time + 100 * memory.readbyte(0x07F8)
time = time + 010 * memory.readbyte(0x07F9)
time = time + 001 * memory.readbyte(0x07FA)
local currentFitness = currentFitnessNoTime + 4 * time + totalPreviousFitness
currentBestFitness = math.max(currentFitness, currentBestFitness)
specie.fitness = (currentFitness + currentBestFitness) / 2
local currentFitness = ((currentFitnessNoTime + currentBestFitnessNoTime) / 2) + (4 * time)
specie.fitness = currentFitness

if (memory.readbyte(0x001D) == 0x03 and not finishedLevel) then -- sliding down flagpole
finishedLevel = true;
Expand All @@ -159,6 +155,9 @@ function testSpecie(specie, blocks)
return
end

gui.text(0, 150, "old " .. oldFitnessNoTime)
gui.text(0, 165, "new " .. currentFitnessNoTime)

if (oldFitnessNoTime == currentFitnessNoTime) then
stationaryFrames = stationaryFrames + 1
if (stationaryFrames >= 90) then
Expand All @@ -169,7 +168,8 @@ function testSpecie(specie, blocks)
end

oldFitnessNoTime = currentFitnessNoTime
gui.text(0, 40, "Fitness: " .. fitness)

gui.text(0, 40, "Fitness: " .. currentFitness)
end

function sigmoid(x)
Expand Down Expand Up @@ -348,11 +348,18 @@ function loadGenome(number)
return genome
end

function resetVariables()
currentBestFitnessNoTime = 0
oldFitnessNoTime = 0
totalPreviousFitness = 0
end

-- Initialization
local genome = getRandomGenome(speciesPerGenome) -- loadGenome() or getRandomGenome()
local startTime = os.time()
oldFitnessNoTime = memory.readbyte(0x0086) + 256 * memory.readbyte(0x071A) - 40
savestate.load("SMB.State")
resetVariables()

function saveGenome()
local filename = generation .. ".gen"
Expand All @@ -376,6 +383,7 @@ while true do

if (IsAlive == false) then
savestate.load("SMB.State")
resetVariables()
if (currentSpecie ~= #genome) then
currentSpecie = currentSpecie + 1
stationaryFrames = 0
Expand Down

0 comments on commit 53b535d

Please sign in to comment.