Skip to content

Commit

Permalink
Fixed major bug with mutations
Browse files Browse the repository at this point in the history
Fixed major bug with iterations

Signed-off-by: Daniel Shumway <shumway.danny@gmail.com>
  • Loading branch information
danShumway committed Aug 27, 2014
1 parent 61c7efc commit 2286ed5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 40 deletions.
45 changes: 26 additions & 19 deletions Piglet/Processors/tests/guessTestRevise.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@ local guessTestRevise = {}
--
function guessTestRevise.iterate()

--For each step.
local strategy = Piglet.Memory.Short.strategies[Piglet.Memory.Short.strategies.currentIndex];
for k, v in pairs(Piglet.Memory.Instant.currentChanges) do
if(Piglet.Memory.Short.updateSeen(k, v) == 0) then
--You get a huge bonus.
strategy.score = strategy.score + 1
else
--Repetitive changes either shouldn't count towards you, or should count as nothing.
--Since we'll be able to see how many times you've seen that, we could possibly bias stuff(?)
--Not really useful, I think we thought through a couple of reasons that doesn't work.
end
end

--Increase the cost calculation for that strategy.
strategy.cost = strategy.cost + 1

--If we've reached the end, iterate on the winning formula.
if(Piglet.Memory.Short.strategies.refresh == true) then

Expand All @@ -29,16 +13,16 @@ function guessTestRevise.iterate()
local bestScore = 0
--Find winner
for i=1, Piglet.Memory.Short.strategies.count, 1 do
if(Piglet.Memory.Short.strategies[i].score > bestScore) then
bestScore = Piglet.Memory.Short.strategies[i].score
if(Piglet.Memory.Short.strategies[i].score/Piglet.Memory.Short.strategies[i].cost > bestScore) then
bestScore = Piglet.Memory.Short.strategies[i].score/Piglet.Memory.Short.strategies[i].cost
bestStrategy = i
end
end
--Let me know.
print("trial finished: "..bestScore.." - ["..Piglet.Memory.Short.strategies[bestStrategy].strategy.toString(0).."]")

--If your best trial gave you a zero, then you might be stuck, and I think it's time to reset.
if(bestScore/Piglet.Memory.Short.strategies[bestStrategy].cost <= 3) then
if(bestScore/Piglet.Memory.Short.strategies[bestStrategy].cost <= 7) then
print("bored, resetting")
Piglet.Memory.Short.forgetSeen()
Piglet.Memory.Short.resetInitialState()
Expand All @@ -48,7 +32,30 @@ function guessTestRevise.iterate()

--And iterate.
Piglet.Memory.Short.strategies.iterate(Piglet.Memory.Short.strategies[bestStrategy].strategy)

else--If we're still gathering data.

--For each step.
local strategy = Piglet.Memory.Short.strategies[Piglet.Memory.Short.strategies.currentIndex];
for k, v in pairs(Piglet.Memory.Instant.currentChanges) do
if(Piglet.Memory.Short.updateSeen(k, v) == 0) then
--You get a huge bonus.
strategy.score = strategy.score + 1
else
--Repetitive changes either shouldn't count towards you, or should count as nothing.
--Since we'll be able to see how many times you've seen that, we could possibly bias stuff(?)
--Not really useful, I think we thought through a couple of reasons that doesn't work.
end
end

--Increase the cost calculation for that strategy.
strategy.cost = strategy.cost + 1

end




end

return guessTestRevise
2 changes: 1 addition & 1 deletion Piglet/index.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ end
--Remember some stuff.
--Piglet.Memory.Short.rememberCauses(Piglet.Memory.Long.load("Mario_6Golden_Coins", "test_01"))
--Should probably move this.
Piglet.Memory.Short.strategies.init(10, 4)
Piglet.Memory.Short.strategies.init(8, 6)
Piglet.Memory.Short.recordInitialState()
while(true) do
--Piglet sees.
Expand Down
51 changes: 31 additions & 20 deletions Piglet/memory/Short/DataStructures/strategy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ function strategies.keyNode(step_keys, node_prev, node_next)
return {toPress=keys, nextNode=next}
end

function node.delete()
if prev ~= nil then
prev.updateNext(next)
end
if next ~= nil then
next.updatePrev(prev)
end
end

--Handle mutation.
--Recursively via first node.
function node.mutate()

if math.random() < .07 then
if math.random() < .05 then
--Get some random keys.
local random_keys = {}
local available = Piglet.Hardware.Hand.getAvailableKeys()
Expand All @@ -40,7 +50,7 @@ function strategies.keyNode(step_keys, node_prev, node_next)
end

--Choose where to attach them.
local r = math.random(5)
local r = math.random(1, 6)



Expand All @@ -59,49 +69,49 @@ function strategies.keyNode(step_keys, node_prev, node_next)

elseif r == 4 then --Delete a previous node.
if prev ~= nil then
--Finish this soon.
prev.delete()
end

else --Delete a next node.
if r == 5 then
--Finish this soon.
elseif r == 5 then --Delete next node.
if(next ~= nil) then
next.delete()
end
end
end


--Move on to the next node.
if(next ~= nil) then
next.mutate()
end
end

--Returns a new strategy, optionally do mutations as well.
function node.duplicate(do_mutation)
function node.duplicate()
--If you're not at the end of the sequence.
local duplicated_next = nil
local count = 1
if next ~= nil then
duplicated_next = next.duplicate(do_mutation)
count = count + 1
end

local toReturn = strategies.keyNode(keys, nil, duplicated_next) --Replace current node.

--Having a second if here is inneficient, todo: take a look at fixing it.
if next ~= nil then
duplicated_next.updatePrev(duplicated_next) --Fix current node.
end

--If you're meant to mutate.
if do_mutation then
toReturn.mutate()
--Possible error here, we might be off by a couple.
if duplicated_next ~= nil then
duplicated_next.updatePrev(toReturn) --Fix current node.
end

--Give back the head (or if recursive the currently generated node).
return toReturn, count
return toReturn
end

function node.toString(i)
local toReturn = i..":"
local toReturn = i.."{"
for k,v in pairs(keys) do
toReturn = toReturn..k
end
toReturn = toReturn.."}"
if(next ~= nil) then toReturn = toReturn..", "..next.toString(i+1) end
return toReturn
end
Expand Down Expand Up @@ -131,8 +141,9 @@ end
function strategies.iterate(strategy)
for i=1, strategies.count, 1 do
local _strategy = nil local _size = nil
_strategy, _size = strategy.duplicate(true)
strategies[i] = {score=0, chancesLeft=strategies.baseChances, size=_size, cost=0, strategy=_strategy }
_strategy = strategy.duplicate()
strategies[i] = {score=0, chancesLeft=strategies.baseChances, cost=0, strategy=_strategy }
_strategy.mutate()
end

--Used to figure out where we are in the strategies.
Expand Down

0 comments on commit 2286ed5

Please sign in to comment.