1 year ago
#80871
Eloi
OnActionReceived is not invoked after RequestDecision is called
I am working on a simulation game where you need to place a medical station somewhere on the map. Thenthe simulation run and compute the effectiveness of the position (it can't be too far or too close some other building). I want to train a MLAgent to find the optimal spot. So I manually call the RequestDecision() method because it needs to wait the end of the simulation to get the reward (then the simulation reset and the agent try again). But it doesn't invoke OnActionReceived() as it is supposed to. Here my code :
public class AIController : Agent
{
public LayerMask ground;
public float[] mapSize = new float[2];
private void Update()
{
SimulationMaster.SimulationState state = SimulationMaster.Instance.State;
if (state == SimulationMaster.SimulationState.End)
{
SetReward(-SimulationMaster.Instance.GetTotalScore());
EndEpisode();
}
else if (state == SimulationMaster.SimulationState.EditMode)
{
RequestDecision();
}
}
public override void OnEpisodeBegin()
{
SimulationMaster.Instance.ResetToEditMode();
}
private void SpawnMedicalStation(float x, float y)
{
Ray ray = new Ray(new Vector3(x * mapSize[0], 10, y * mapSize[1]), Vector3.down);
if (!Physics.Raycast(ray, out RaycastHit hitInfo, Mathf.Infinity, ground))
{
Debug.LogError("AIController - Out of bound");
return;
}
SpawnManager.Instance.SpawnMedicalStation(hitInfo.point, 2, 2);
}
public override void CollectObservations(VectorSensor sensor)
{
Vector3 pos = TacticEventManager.Instance.TacticEvents[0].Barycentre;
sensor.AddObservation(new Vector2(pos.x, pos.z));
}
public override void OnActionReceived(ActionBuffers actions)
{
if (SimulationMaster.Instance.State != SimulationMaster.SimulationState.EditMode) return;
float x = actions.ContinuousActions[0];
float y = actions.ContinuousActions[1];
Debug.Log($"Action received: ({x}, {y})");
SpawnMedicalStation(x, y);
SimulationMaster.Instance.StartSimulation();
}
}
c#
unity-game-engine
ml-agent
0 Answers
Your Answer