I tried implementing this, but I can’t figure out how I can get the GeneticAlgorithm to use the trained network. The cars seem to only change their behavior when the generation has fiinished.
This is part of my code:
-- Create a new network with 5 inputs, 2 layers with 4 nodes each and 1 output "steerDirection"
local tempNet = FeedforwardNetwork.new({"front", "frontLeft", "frontRight", "left", "right"}, 2, 4, {"steerDirection"}, feedForwardSettings)
--local tempNet = FeedforwardNetwork.newFromSave(game.ServerStorage.NetworkSave.Value)
local populationSize = 15
local geneticAlgo = ParamEvo.new(tempNet, populationSize, geneticSetting) -- Create ParamEvo with the tempNet template, population size and settings
local scoreTable = {}
local generations = 50 -- Number of generations to train network with
local firstRun = true
for _ = 0, generations do
for index = 1, populationSize+1 do
spawn(function()
local startTime = os.clock()
local clone = game:GetService("ServerStorage").Car:Clone()
clone.RemoteControl.MaxSpeed = 200
-- Parent to workspace and then setup Scripts of car
clone.Parent = workspace
local score = 0
local bool = true
local checkpointsHit = {}
for _, v in pairs(clone:GetDescendants()) do
if v:IsA("BasePart") and v.CanCollide == true then
v.Touched:Connect(function(hit)
if hit.Parent.Parent == workspace.Walls then -- Destroy car on hit of walls
bool = false
elseif hit.Parent == workspace.Checkpoints and not checkpointsHit[tonumber(hit.Name)] then -- Give extra points when car reaches checkpoint
local numHit = tonumber(hit.Name)
score += (numHit * 2)
checkpointsHit[numHit] = hit
end
end)
end
end
while bool do
local distances = getRayDistances(clone) -- Get Distances of rays
local output
if firstRun then
output = tempNet(distances) -- Get output of NN with input distances
else
local population = geneticAlgo:GetPopulation()
local net = population[1].Network
output = net(distances) -- Get output of NN with input distances
end
-- Set steering direction to direction of NN
clone.RemoteControl.SteerFloat = output.steerDirection
-- Set speed of car
--clone.RemoteControl.MaxSpeed = math.abs(output.speed) * 300
-- Check if this simulation has been running for longer than x seconds
if os.clock() > startTime + 90 then
score -= 40 -- Punish algorithm
break
end
wait()
end
score += (os.clock() - startTime)/2 -- Increment score based on time alive (longer is better)
print("Exit score: "..math.floor(score*100)/100)
clone:Destroy()
scoreTable[index] = score
end)
wait(1)
end
-- Wait until generation finished
repeat
wait(1)
until #scoreTable >= populationSize
geneticAlgo:ProcessGeneration(scoreTable)
scoreTable = {}
firstRun = false
end
Also, would this save the right (best) network?
local save = geneticAlgo:GetBestNetwork():Save()