Skip to content

Commit

Permalink
fixed bug where agent could choose to stay on a tile instead of dying
Browse files Browse the repository at this point in the history
  • Loading branch information
tsi25 committed Feb 5, 2023
1 parent 61e3498 commit 3169785
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Assets/GGJ 2023/Prefabs/Training/LightningAgent.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,15 @@ MonoBehaviour:
_movementSpeed: 1
_turnSpeed: 85
_explorationCount: 30
_tileDecay: 512
_failurePenality: -10
_agentXComponent: 0
_agentYComponent: 0
_simulatedXComponent: 0
_simulatedYComponent: 0
_simulatedTurnInput: 0
_debug: 0
_training: 0
--- !u!114 &2601674652650597399
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down
8 changes: 0 additions & 8 deletions Assets/GGJ 2023/Scenes/Launch.unity
Original file line number Diff line number Diff line change
Expand Up @@ -643,14 +643,6 @@ PrefabInstance:
propertyPath: m_LocalPosition.z
value: 0.01
objectReference: {fileID: 0}
- target: {fileID: 5029766794767444028, guid: 66a817954441fd248a741acc8b6d76ca, type: 3}
propertyPath: _explorationCount
value: 29
objectReference: {fileID: 0}
- target: {fileID: 5331121885419515004, guid: 66a817954441fd248a741acc8b6d76ca, type: 3}
propertyPath: _explorationCount
value: 29
objectReference: {fileID: 0}
- target: {fileID: 5537005325459234805, guid: 66a817954441fd248a741acc8b6d76ca, type: 3}
propertyPath: m_LocalPosition.z
value: 0.01
Expand Down
21 changes: 19 additions & 2 deletions Assets/GGJ 2023/Scripts/Runtime/Agents/LightningAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class LightningAgent : Agent
[SerializeField]
protected float _explorationCount = 25;
[SerializeField]
protected int _tileDecay = 100;
[SerializeField]
protected float _failurePenality = -10f;

[SerializeField]
Expand All @@ -46,6 +48,8 @@ public class LightningAgent : Agent
protected float _simulatedTurnInput = 0f;
[SerializeField]
protected bool _debug = false;
[SerializeField]
protected bool _training = false;

// For pausing the root before user wants it to grow
protected bool _isGrowing = false;
Expand All @@ -58,6 +62,8 @@ public class LightningAgent : Agent
protected Vector3Int _currentTilePosition = Vector3Int.zero;

protected int _visitedTileCount = 0;

protected int _tileAge = 1;

protected HashSet<Vector3Int> _visitedTiles = new HashSet<Vector3Int>();

Expand Down Expand Up @@ -150,10 +156,20 @@ public override void OnActionReceived(ActionBuffers actions)
_agentYComponent = actions.ContinuousActions[1];
Vector3Int currentTilePosition = _mapManager.GetTileCoordFromWorldCoord(transform.position);

//if we have visited this tile before and we are currently on this tile, return
if (_visitedTiles.Contains(currentTilePosition) && currentTilePosition == _currentTilePosition) return;
//if we have visited this tile before and we are currently on this tile
if (_visitedTiles.Contains(currentTilePosition) && currentTilePosition == _currentTilePosition)
{
//if we arent training, check if we have stayed too long on the same tile and end the growth if so
if(!_training)
{
_tileAge++;
if (_tileAge == _tileDecay) IsGrowing = false;
}
return;
}

//just entered a new tile
_tileAge = 0;
_currentTilePosition = currentTilePosition;
_visitedTileCount++;
TileHitEvent?.Invoke(_currentTilePosition, this);
Expand Down Expand Up @@ -256,6 +272,7 @@ public override void OnEpisodeBegin()
_simulatedTurnInput = 0f;

_visitedTileCount = 0;
_tileAge = 0;

GetComponentInChildren<TrailRenderer>().Clear();

Expand Down
2 changes: 1 addition & 1 deletion Assets/ML-Agents/Timers/Launch_timers.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"count":1,"self":88.3687616,"total":89.769279,"children":{"InitializeActuators":{"count":3,"self":0.0020212,"total":0.0020212,"children":null},"InitializeSensors":{"count":3,"self":0.0020242,"total":0.0020242,"children":null},"AgentSendState":{"count":4219,"self":0.0429765,"total":0.1893471,"children":{"CollectObservations":{"count":12657,"self":0.10906049999999999,"total":0.10906049999999999,"children":null},"WriteActionMask":{"count":12657,"self":0.0098216,"total":0.0098216,"children":null},"RequestDecision":{"count":12657,"self":0.0274885,"total":0.0274885,"children":null}}},"DecideAction":{"count":4219,"self":1.1780084,"total":1.1780084,"children":null},"AgentAct":{"count":4219,"self":0.0281211,"total":0.0281211,"children":null}},"gauges":{"GGJPlant.CumulativeReward":{"count":9,"max":101.75,"min":-19.25,"runningAverage":20.6944447,"value":101.75,"weightedAverage":30.0975342}},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1675611706","unity_version":"2022.2.4f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2022.2.4f1\\Editor\\Unity.exe -projectpath C:\\Users\\TSI25\\Documents\\GitHub\\GGJ2023-MLPlants -useHub -hubIPC -cloudEnvironment production","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.3.0-exp.3","scene_name":"Launch","end_time_seconds":"1675611795"}}
{"count":1,"self":692.834304,"total":702.8682771,"children":{"InitializeActuators":{"count":3,"self":0.0009969,"total":0.0009969,"children":null},"InitializeSensors":{"count":3,"self":0.0009973,"total":0.0009973,"children":null},"AgentSendState":{"count":34876,"self":0.3646538,"total":1.4516445999999998,"children":{"CollectObservations":{"count":104628,"self":0.79650349999999992,"total":0.79650349999999992,"children":null},"WriteActionMask":{"count":104628,"self":0.0927934,"total":0.0927934,"children":null},"RequestDecision":{"count":104628,"self":0.19769389999999998,"total":0.19769389999999998,"children":null}}},"DecideAction":{"count":34876,"self":8.3231056,"total":8.323106,"children":null},"AgentAct":{"count":34876,"self":0.2572349,"total":0.2572349,"children":null}},"gauges":{"GGJPlant.CumulativeReward":{"count":135,"max":103.5,"min":-67,"runningAverage":14.827776,"value":0.25,"weightedAverage":31.9580765}},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1675617449","unity_version":"2022.2.4f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2022.2.4f1\\Editor\\Unity.exe -projectpath C:\\Users\\TSI25\\Documents\\GitHub\\GGJ2023-MLPlants -useHub -hubIPC -cloudEnvironment production","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.3.0-exp.3","scene_name":"Launch","end_time_seconds":"1675618151"}}

0 comments on commit 3169785

Please sign in to comment.