-- Neural Network AI for Roblox Navigation
local AIBody = script.Parent
-- BodyVelocity Setup
local movement = Instance.new("BodyVelocity")
movement.Parent = AIBody
movement.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
movement.Velocity = Vector3.new(0, 0, 0)
-- Finish Line Reference
local finishingPart = game.Workspace:WaitForChild("Finish")
-- Raycasting Parameters
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {workspace.Baseplate}
-- Movement Functions
local stopped = true
local function RotateLeft()
AIBody.CFrame = AIBody.CFrame * CFrame.Angles(0, math.rad(15), 0)
end
local function RotateRight()
AIBody.CFrame = AIBody.CFrame * CFrame.Angles(0, math.rad(-15), 0)
end
local function MoveForward()
stopped = false
spawn(function()
while not stopped do
task.wait(0.05)
movement.Velocity = AIBody.CFrame.LookVector * 10
if stopped then
movement.Velocity = Vector3.new(0, 0, 0)
end
end
end)
end
local function MoveBackward()
stopped = false
spawn(function()
while not stopped do
task.wait(0.05)
movement.Velocity = AIBody.CFrame.LookVector * -10
if stopped then
movement.Velocity = Vector3.new(0, 0, 0)
end
end
end)
end
local function StopMoving()
stopped = true
movement.Velocity = Vector3.new(0, 0, 0)
end
-- Neural Network Setup
local NeuralNetwork = {}
NeuralNetwork.__index = NeuralNetwork
function NeuralNetwork:new(inputSize, hiddenSize, outputSize)
local nn = {}
setmetatable(nn, NeuralNetwork)
nn.inputSize = inputSize
nn.hiddenSize = hiddenSize
nn.outputSize = outputSize
-- Initialize weights with small random values
nn.weightsInputHidden = {}
nn.weightsHiddenOutput = {}
for i = 1, inputSize do
nn.weightsInputHidden[i] = {}
for j = 1, hiddenSize do
nn.weightsInputHidden[i][j] = math.random() * 0.2 - 0.1
end
end
for i = 1, hiddenSize do
nn.weightsHiddenOutput[i] = {}
for j = 1, outputSize do
nn.weightsHiddenOutput[i][j] = math.random() * 0.2 - 0.1
end
end
-- Biases
nn.biasHidden = {}
nn.biasOutput = {}
for i = 1, hiddenSize do
nn.biasHidden[i] = math.random() * 0.2 - 0.1
end
for i = 1, outputSize do
nn.biasOutput[i] = math.random() * 0.2 - 0.1
end
return nn
end
function NeuralNetwork:activate(x)
-- Sigmoid activation
return 1 / (1 + math.exp(-x))
end
function NeuralNetwork:feedforward(inputs)
self.inputs = inputs
-- Calculate hidden layer activations
self.hidden = {}
for i = 1, self.hiddenSize do
local sum = 0
for j = 1, self.inputSize do
sum = sum + inputs[j] * self.weightsInputHidden[j][i]
end
sum = sum + self.biasHidden[i]
self.hidden[i] = self:activate(sum)
end
-- Calculate output layer activations
self.output = {}
for i = 1, self.outputSize do
local sum = 0
for j = 1, self.hiddenSize do
sum = sum + self.hidden[j] * self.weightsHiddenOutput[j][i]
end
sum = sum + self.biasOutput[i]
self.output[i] = self:activate(sum)
end
return self.output
end
-- Simple Neural Network instance
local inputSize = 6 -- 5 rays + distance
local hiddenSize = 10
local outputSize = 5 -- RotateLeft, RotateRight, MoveForward, MoveBackward, StopMoving
local nn = NeuralNetwork:new(inputSize, hiddenSize, outputSize)
-- Store previous actions and reward
local previousInputs = nil
local previousOutputs = nil
local reward = 0
-- Function to get inputs
local function getInputs()
local distance = (AIBody.Position - finishingPart.Position).Magnitude
local directionEMLR = script.Parent.EvenMoreLeftRay.Position - AIBody.Position
local directionLR = script.Parent.LeftRay.Position - AIBody.Position
local directionMR = script.Parent.MiddleRay.Position - AIBody.Position
local directionRR = script.Parent.RightRay.Position - AIBody.Position
local directionEMRR = script.Parent.EvenMoreRightRay.Position - AIBody.Position
local ray1Result = workspace:Raycast(AIBody.Position, directionEMLR, params)
local ray2Result = workspace:Raycast(AIBody.Position, directionLR, params)
local ray3Result = workspace:Raycast(AIBody.Position, directionMR, params)
local ray4Result = workspace:Raycast(AIBody.Position, directionRR, params)
local ray5Result = workspace:Raycast(AIBody.Position, directionEMRR, params)
-- Normalize distances for inputs
local maxRayDistance = 100
local inputs = {
ray1Result and (ray1Result.Position - AIBody.Position).Magnitude / maxRayDistance or 1,
ray2Result and (ray2Result.Position - AIBody.Position).Magnitude / maxRayDistance or 1,
ray3Result and (ray3Result.Position - AIBody.Position).Magnitude / maxRayDistance or 1,
ray4Result and (ray4Result.Position - AIBody.Position).Magnitude / maxRayDistance or 1,
ray5Result and (ray5Result.Position - AIBody.Position).Magnitude / maxRayDistance or 1,
distance / maxRayDistance
}
return inputs
end
-- Function to decide movement based on neural network output
local function decideMovement(outputs)
-- Find the action with the highest output value
local maxOutput = -math.huge
local actionIndex = 1
for i, v in ipairs(outputs) do
if v > maxOutput then
maxOutput = v
actionIndex = i
end
end
-- Execute the corresponding movement
if actionIndex == 1 then
RotateLeft()
elseif actionIndex == 2 then
RotateRight()
elseif actionIndex == 3 then
MoveForward()
elseif actionIndex == 4 then
MoveBackward()
elseif actionIndex == 5 then
StopMoving()
end
-- Store the outputs for learning
previousOutputs = {0, 0, 0, 0, 0}
previousOutputs[actionIndex] = 1
end
-- Function to evaluate performance and assign reward
local function evaluatePerformance()
local currentDistance = (AIBody.Position - finishingPart.Position).Magnitude
if currentDistance <= 5 then
reward = 1 -- Reward for reaching near the finish line
else
reward = 0
end
end
-- Function to adjust neural network weights (Simple Reinforcement Learning)
local function learn()
if previousInputs and previousOutputs then
-- Simple learning: if reward was received, reinforce the actions taken
if reward == 1 then
-- Increase weights for the actions that led to reward
for i = 1, nn.inputSize do
for j = 1, nn.hiddenSize do
nn.weightsInputHidden[i][j] = nn.weightsInputHidden[i][j] + previousInputs[i] * 0.01
end
end
for i = 1, nn.hiddenSize do
for j = 1, nn.outputSize do
nn.weightsHiddenOutput[i][j] = nn.weightsHiddenOutput[i][j] + nn.hidden[i] * previousOutputs[j] * 0.01
end
end
else
-- Optionally, you can implement penalties for not reaching the goal
end
end
end
-- Reset AI Position
local initialCFrame = AIBody.CFrame
-- Main Loop
spawn(function()
while true do
-- Capture inputs
local inputs = getInputs()
previousInputs = inputs
-- Get neural network outputs
local outputs = nn:feedforward(inputs)
-- Decide movement
decideMovement(outputs)
-- Wait for 20 seconds or until the AI reaches the finish
local startTime = tick()
while tick() - startTime < 20 do
evaluatePerformance()
if reward == 1 then
break
end
task.wait(0.5)
end
-- Learn from the experience
learn()
-- Reset position
AIBody.CFrame = initialCFrame
StopMoving()
reward = 0
task.wait(1) -- Small delay before next round
end
end)