Skip to content

Commit

Permalink
Update AI behavior in Snake Game for improved performance
Browse files Browse the repository at this point in the history
This commit includes updates to the assets, scripts, and AI architecture of the Snake Game. The AI agent's decision-strategies have been modified to reinforce actions that move the agent closer to the reward, in order to improve game performance. Additionally, attributes relating to the positioning and the activity of environment features in the game level have been changed for better alignment with the new decision-logic. Overall, these changes are made to enhance the agent's learning and navigation in the game.
Score after 1HR = 36
  • Loading branch information
Pristar4 committed Jul 11, 2023
1 parent 427792e commit eae6e34
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Assets/ML-Agents/Timers/AI_timers.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"count":1,"self":1900.9214464,"total":3646.702797,"children":{"InitializeActuators":{"count":20,"self":0.0020009,"total":0.0020009,"children":null},"InitializeSensors":{"count":20,"self":0.0025141,"total":0.0025141,"children":null},"AgentSendState":{"count":214702,"self":12.033138399999999,"total":59.1252904,"children":{"CollectObservations":{"count":1526209,"self":14.3286336,"total":14.328634099999999,"children":null},"WriteActionMask":{"count":1526209,"self":2.9685319999999997,"total":2.9685319,"children":null},"RequestDecision":{"count":1526209,"self":10.107839199999999,"total":29.7949861,"children":{"AgentInfo.ToProto":{"count":1516682,"self":7.8454624,"total":19.6871471,"children":{"GenerateSensorData":{"count":1516682,"self":11.841683999999999,"total":11.841684299999999,"children":null}}}}}}},"DecideAction":{"count":214702,"self":1617.9479552,"total":1617.9479155,"children":null},"AgentAct":{"count":214702,"self":68.6426944,"total":68.703616099999991,"children":{"AgentInfo.ToProto":{"count":3103,"self":0.0373284,"total":0.0609244,"children":{"GenerateSensorData":{"count":3103,"self":0.023596,"total":0.023596,"children":null}}}}}},"gauges":{"SnakeAi.CumulativeReward":{"count":3201,"max":356.897736,"min":-1060.9856,"runningAverage":117.872993,"value":-10.5,"weightedAverage":13.1152983}},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1689105069","unity_version":"2023.1.3f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2023.1.3f1\\Editor\\Unity.exe -projectpath C:\\Users\\felix\\SnakeGame -useHub -hubIPC -cloudEnvironment production","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.3.0-exp.3","scene_name":"AI","end_time_seconds":"1689108716"}}
{"count":1,"self":1070.6020352,"total":3714.9059334,"children":{"InitializeActuators":{"count":24,"self":0.0015083,"total":0.0015083,"children":null},"InitializeSensors":{"count":24,"self":0.0015055,"total":0.0015055,"children":null},"AgentSendState":{"count":461466,"self":7.9821912,"total":43.912659399999995,"children":{"CollectObservations":{"count":3691728,"self":11.673304,"total":11.6733041,"children":null},"WriteActionMask":{"count":3691728,"self":1.7134748,"total":1.7134748,"children":null},"RequestDecision":{"count":3691728,"self":5.8726204,"total":22.5436892,"children":{"AgentInfo.ToProto":{"count":3691728,"self":5.2082856,"total":16.671068899999998,"children":{"GenerateSensorData":{"count":3691728,"self":11.462783199999999,"total":11.4627831,"children":null}}}}}}},"DecideAction":{"count":461466,"self":2493.6136704,"total":2493.6136833,"children":null},"AgentAct":{"count":461466,"self":106.6151168,"total":106.77455959999999,"children":{"AgentInfo.ToProto":{"count":20861,"self":0.0722235,"total":0.1594411,"children":{"GenerateSensorData":{"count":20861,"self":0.087217599999999992,"total":0.087217599999999992,"children":null}}}}}},"gauges":{"SnakeAi.CumulativeReward":{"count":20861,"max":281.898163,"min":-137.800354,"runningAverage":50.4565544,"value":35.8000336,"weightedAverage":68.77597}},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1689110079","unity_version":"2023.1.3f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2023.1.3f1\\Editor\\Unity.exe -projectpath C:\\Users\\felix\\SnakeGame -useHub -hubIPC -cloudEnvironment production","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.3.0-exp.3","scene_name":"AI","end_time_seconds":"1689113793"}}
21 changes: 19 additions & 2 deletions Assets/SnakeGame/Scripts/SnakeAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public float PreviousDistance
{
get => _previousDistance;
set => _previousDistance = value;
}
}



#region Event Functions

Expand Down Expand Up @@ -107,6 +109,8 @@ private void InitializeGame() {
board = new Board(width, height, snakes);
}

PreviousDistance = float.PositiveInfinity;


// food
for (int i = 0; i < foodCount; i++) {
Expand Down Expand Up @@ -139,7 +143,7 @@ public override void CollectObservations(VectorSensor sensor) {


// Add the direction to the food (2 floats)
sensor.AddObservation(directionToFood);
sensor.AddObservation(food);

// Check for immediate dangers: front, left, right relative to the snake's current direction
var forwardPosition = snake.Position + snake.Direction;
Expand Down Expand Up @@ -285,6 +289,9 @@ public override void OnActionReceived(ActionBuffers actions) {
if (normalizedCurrentDistance <= rewardRadius) {
// Compute the reward based on the Normalized current distance
// float reward = Mathf.Log((snake.Length + PreviousDistance) / (snake.Length + currentDistance));





// Define the reward
Expand All @@ -297,6 +304,16 @@ public override void OnActionReceived(ActionBuffers actions) {
// AddReward(reward);
PreviousDistance = currentDistance;
}

if (currentDistance < PreviousDistance) {
AddReward(0.01f);
Debug.Log("Closer :" + 0.01f);

} else if (currentDistance > PreviousDistance) {
AddReward(-0.01f);
Debug.Log("Further :" + -0.01f);
}
PreviousDistance = currentDistance;
}


Expand Down
Loading

0 comments on commit eae6e34

Please sign in to comment.