Hey, I’m attempting to make my NPC learn to follow me using a Logistic Regression Model and I use 6 Inputs, and 8 Outputs
Everything is working as intended but the problem is the NPC’s movement is just sometimes weird unpredictable like it does work but its so weird, I’m not sure if I have to train it longer or what??
This footage was me training it for like 30 seconds
-- Input, Outputs, Learning Rate, Epochs
local lr = LogisticRegression.new(6, 8, .1, 100)
local inputs = {}
local outputs = {}
local loadedWeights = ""
--lr.weights = HttpService:JSONDecode(loadedWeights)
task.spawn(function()
-- Training Time
task.wait(30)
lr:train(inputs, outputs)
print(HttpService:JSONEncode(lr.weights))
stop = true
print("Using the data it was just trained on!")
while true do
local npc_x, npc_y, npc_z = npc.PrimaryPart.Position.X, npc.PrimaryPart.Position.Y, npc.PrimaryPart.Position.Z
local player_x, player_y, player_z = character.PrimaryPart.Position.X, character.PrimaryPart.Position.Y, character.PrimaryPart.Position.Z
local prediction = lr:predict({npc_x, npc_y, npc_z, player_x, player_y, player_z})
local max_prob = 0
local max_index = 0
for i = 1, #prediction do
if prediction[i] > max_prob then
max_prob = prediction[i]
max_index = i
end
end
-- Calculate the movement direction based on the predicted action
if max_index == 1 then
npc.Humanoid:MoveTo(npc.PrimaryPart.Position + Vector3.new(0, 0, 1))
elseif max_index == 2 then
npc.Humanoid:MoveTo(npc.PrimaryPart.Position - Vector3.new(0, 0, 1))
elseif max_index == 3 then
npc.Humanoid:MoveTo(npc.PrimaryPart.Position + Vector3.new(-1, 0, 0))
elseif max_index == 4 then
npc.Humanoid:MoveTo(npc.PrimaryPart.Position + Vector3.new(1, 0, 0))
elseif max_index == 5 then
npc.Humanoid:MoveTo(npc.PrimaryPart.Position + Vector3.new(-1, 0, 1))
elseif max_index == 6 then
npc.Humanoid:MoveTo(npc.PrimaryPart.Position + Vector3.new(1, 0, 1))
elseif max_index == 7 then
npc.Humanoid:MoveTo(npc.PrimaryPart.Position + Vector3.new(-1, 0, -1))
elseif max_index == 8 then
npc.Humanoid:MoveTo(npc.PrimaryPart.Position + Vector3.new(1, 0, -1))
end
task.wait()
end
end)
-- Making it follow my player so it records the best movements to get to me
task.spawn(function()
game:GetService("RunService").Heartbeat:Connect(function()
if not stop then
npc.Humanoid:MoveTo(character.PrimaryPart.Position)
end
end)
end)
-- Assigning the outputs
while true do
local npc_pos = npc.PrimaryPart.Position
local player_pos = character.PrimaryPart.Position
table.insert(inputs, {npc_pos.X, npc_pos.Y, npc_pos.Z, player_pos.X, player_pos.Y, player_pos.Z})
local output = {0, 0, 0, 0, 0, 0, 0, 0}
if npc_pos.X < player_pos.X then
output[4] = 1
elseif npc_pos.X > player_pos.X then
output[3] = 1
end
if npc_pos.Z < player_pos.Z then
output[1] = 1
elseif npc_pos.Z > player_pos.Z then
output[2] = 1
end
if npc_pos.X < player_pos.X and npc_pos.Z < player_pos.Z then
output[6] = 1
elseif npc_pos.X > player_pos.X and npc_pos.Z < player_pos.Z then
output[5] = 1
end
if npc_pos.X > player_pos.X and npc_pos.Z > player_pos.Z then
output[7] = 1
elseif npc_pos.X < player_pos.X and npc_pos.Z > player_pos.Z then
output[8] = 1
end
table.insert(outputs, output)
if stop then
break
end
task.wait()
end