benson skal til at cooke

This commit is contained in:
2024-04-19 14:10:31 +02:00
parent 851558c6db
commit 8f16297e05
5 changed files with 2093 additions and 1596 deletions

View File

@@ -14,8 +14,6 @@ using System;
public class AgentControllerV4 : Agent
{
public float motorForce = 300;
public float brakeForce = 500;
public float maxSpeed = 400;
public float steeringRange = 9;
Rigidbody rigidBody;
public List<GameObject> checkpoints;
@@ -24,14 +22,12 @@ public class AgentControllerV4 : Agent
int currentStep = 0;
int stepsSinceCheckpoint = 0;
public int maxStepsPerCheckpoint = 300;
public int distanceBetweenCheckpoints;
public int distanceBetweenCheckpoints = 5;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent<Rigidbody>();
// Find all child GameObjects that have the WheelControl script attached
startPosition = transform.localPosition;
startRotation = transform.localRotation;
}
@@ -67,8 +63,8 @@ public class AgentControllerV4 : Agent
}
}
print(transform.rotation.y);
sensor.AddObservation(transform.rotation.y);
// Agent rotation
sensor.AddObservation(transform.localRotation.y);
Vector3 position = transform.localPosition;
Vector3 checkpointPosition = currentCheckpoint.localPosition;
@@ -78,21 +74,15 @@ public class AgentControllerV4 : Agent
checkpointPosition.z - position.z
);
// Normalized vector in direction of checkpoint and distancce to checkpoint.
sensor.AddObservation(toCheckpoint.normalized);
sensor.AddObservation(toCheckpoint.magnitude);
sensor.AddObservation(distanceToCheckpoint(currentCheckpoint));
var FullVelocityMagnitude = rigidBody.velocity.magnitude; // Velocity including angular velocity
var angularMagnitude = rigidBody.angularVelocity.magnitude;
var forwardMagnitude = Mathf.Sqrt( Mathf.Pow(FullVelocityMagnitude, 2) - Mathf.Pow(angularMagnitude, 2)); // Agent velocity in forward direction
if (forwardMagnitude >= 0.001)
sensor.AddObservation(forwardMagnitude);
else
sensor.AddObservation(FullVelocityMagnitude);
sensor.AddObservation(angularMagnitude);
Vector2 velocity = new Vector2(rigidBody.velocity.x, rigidBody.velocity.z);
// Velocity
sensor.AddObservation(velocity);
}
public override void OnActionReceived(ActionBuffers actions)
@@ -111,13 +101,6 @@ public class AgentControllerV4 : Agent
if (actions.DiscreteActions[1] == 1)
hInput = 1f;
// reward for going forward
// if (vInput == 1f)
// {
// AddReward(0.02f);
// }
// give benson mental pain for existing (punishment for maximizing first checkpoint by standing still)
AddReward(-0.002f);
@@ -155,12 +138,13 @@ public class AgentControllerV4 : Agent
currentCheckpoint.GetComponent<Checkpoint>().isCollected = true;
stepsSinceCheckpoint = 0;
// If last checkpoint
if (currentCheckpoint == checkpoints[checkpoints.Count - 1].transform)
{
AddReward(10f);
EndEpisode();
}
AddReward(1.0f);
AddReward(1f);
}
currentStep += 1;
@@ -208,19 +192,19 @@ public class AgentControllerV4 : Agent
return angle;
}
// punishment for hitting a wall
private void OnCollisionEnter(Collision other) {
if (other.gameObject.tag == "Wall")
{
AddReward(-0.05f);
}
}
// // punishment for hitting a wall
// private void OnCollisionEnter(Collision other) {
// if (other.gameObject.tag == "Wall")
// {
// AddReward(-0.05f);
// }
// }
// punishment for staying at a wall
private void OnCollisionStay(Collision other) {
if (other.gameObject.tag == "Wall")
{
AddReward(-0.05f);
}
}
// // punishment for staying at a wall
// private void OnCollisionStay(Collision other) {
// if (other.gameObject.tag == "Wall")
// {
// AddReward(-0.005f);
// }
// }
}