Tensorflow having issues teaching car how to drive
Hello. I have tried to teach a car how to drive using 5 raycasters giving distances to obstacles. The car is always moving and could just steer left right or forward , getting reward for staying for as long as possible. The problem is after teaching it, it just keeps steering left colliding almost immediatelly and I have no idea why :(.
Problem idea

Tensorboard output

I have succesfully managed to train before the balance ball example as well as problem from Unity3d College tutorials(about moving to given number). In player mode everything seems to be working perfectly. Any help is much appreciated.
Do you use discrete or continuous input? Can you provide the code of your player class and your settings in your brain object in unity?
Actions are discrete while states are continuous.
Car agent code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CarAgent : Agent
{
private const int maxDuration = 300;
[SerializeField]
private Rigidbody rigidbody;
[SerializeField]
private Vector3 startingCarPosition;
[SerializeField]
private Transform carTransform;
public Text text;
private float duration = 0f;
int count = 0;
public int count0 = 0;
public int count1 = 0;
public int count2 = 0;
[SerializeField]
private SimpleCarController simpleCarController;
public override List<float> CollectState()
{
List<float> state = new List<float>();
state.Add(RaycastCollisionDetector.leftDetectorState30);
state.Add(RaycastCollisionDetector.leftDetectorState60);
state.Add(RaycastCollisionDetector.forwardDetectorState);
state.Add(RaycastCollisionDetector.rightDetectorState30);
state.Add(RaycastCollisionDetector.rightDetectorState60);
// state.Add(SimpleCarController.carSpeed);
// state.Add(SimpleCarController.angle);
return state;
}
public override void AgentStep(float[] action)
{
switch ((int)action[0])
{
case 0:
SimpleCarController.turnLeft = true;
SimpleCarController.turnRight = false;
count0++;
break;
case 1:
SimpleCarController.turnLeft = false;
SimpleCarController.turnRight = false;
count1++;
break;
case 2:
SimpleCarController.turnLeft = false;
SimpleCarController.turnRight = true;
count2++;
break;
default:
SimpleCarController.turnLeft = false;
SimpleCarController.turnRight = false;
break;
}
if (RaycastCollisionDetector.hasCollided)
{
done = true;
reward = -10;
count++;
AgentReset();
}
else if (duration > maxDuration)
{
done = true;
count++;
}
else
{
duration += Time.deltaTime;
reward = duration;
}
text.text = "dur: " + duration + "rew: " + reward + "cout: " + count + "rew: " + reward + " " + "decisin: " + (int)action[0];
}
public override void AgentReset()
{
duration = 0;
carTransform.position = startingCarPosition;
carTransform.rotation = Quaternion.identity;
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
RaycastCollisionDetector.hasCollided = false;
simpleCarController.ResetInputs();
}
}
And the brain object settings:

I have 3 points that you should improve: 1: you probably should change your level (to random generated or at least a pool of a handful maps) The easiest ways to do this: Have multiple agents train in your scene at once (what you should always do) and distribute your agents onto the hand build levels
2: reduce the episode length (in my experience the learning improved significantly when it is below 1000 (i normally aim for 500 or lower). An easy way to reduce this is by changing fixed time step (i currently train with 10 ticks per second). Alternatively you could increase the decision frequency (i havent tried this yet)
3: the reward per tick should never be larger than 1 (or -1) so your death reward of -10 is probably to large for the agent to properly learn with. And dont set the time reward like that (i think you made it increase exponentially). Do a small value every tick, like: AddReward(0.05f);
Oh and since your observations and actions are extremely small your neural network should also be small. try these parameters: batch_size: 256 buffer_size: 2560 normalize: false num_layers: 2 hidden_units: 16 beta: 1.0e-3 gamma: 0.96 time_horizon: 256 max_steps: 5.0e5 learning_rate: 3.0e-4
i always set normalize to false because i normalize in the code. And try different hidden units (16,32,64)
Hi @Myteq - let us know if the feedback received helped you solve your issue or if you require any additional support. Thanks @Lakrix and @Hengoo.
Sorry for no response. After aplying suggestions from @Hengoo, the car moves more randomly, not just turning one way all the time. Also i found that my car has tendency to drive on 2 wheels, which causes collider detectors to wrongly measure distance(as it raycasts from rotating object). I will for sure try to fix it and se the results, but more likely towards the end of the week. I will let you know about the results when I have them.
It works now :). Thanks for your help. Turns out there was also problem with physics being updated on Update and not FixedUpdate.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.