File size: 2,379 Bytes
41791c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using PhysX5ForUnity;
using System;
using System.Reflection;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using UnityEngine;


[AddComponentMenu("Blood Simulation/Delayed Decision Requester")]
public class DelayedDecisionRequester : DecisionRequester
{
    public int DelayedSteps
    {
        get { return m_delayedSteps; }
        set { m_delayedSteps = value; }
    }

    private void Start()
    {
        m_propertyInfo = new PropertyInfo[m_actors.Length];
        for (int i = 0; i < m_actors.Length; i++)
        {
            m_propertyInfo[i] = m_actors[i].GetType().GetProperty("InEpisode");
        }
    }

    protected override bool ShouldRequestDecision(DecisionRequestContext context)
    {
        // Ensures that the delay has ended and is at the decision step
        if (Agent.StepCount < m_delayedSteps) m_isDelayEnd = false;
        m_isDelayEnd = m_isDelayEnd || (Agent.StepCount >= m_delayedSteps && context.AcademyStepCount % DecisionPeriod == DecisionStep);
        if (!m_isDelayEnd)
        {
            for (int i = 0; i < m_actors.Length; i++)
            {
                m_propertyInfo[i].SetValue(m_actors[i], false);
            }
            m_agent.InEpisode = false;
            m_shouldInitialize = true;
        }
        else
        {
            for (int i = 0; i < m_actors.Length; i++)
            {
                m_propertyInfo[i].SetValue(m_actors[i], true);
            }
            m_agent.InEpisode = true;
            InitialFrameCameraSensorComponent initialFrameCameraSensorComponent = m_agent.GetComponent<InitialFrameCameraSensorComponent>();
            if (m_shouldInitialize && initialFrameCameraSensorComponent != null)
            {
                initialFrameCameraSensorComponent.UpdateSensorFrame();
                m_shouldInitialize = false;
            }
        }

        return context.AcademyStepCount % DecisionPeriod == DecisionStep && m_isDelayEnd;
    }

    protected override bool ShouldRequestAction(DecisionRequestContext context)
    {
        return TakeActionsBetweenDecisions && m_isDelayEnd;
    }

    [SerializeField]
    int m_delayedSteps;
    [SerializeField]
    MonoBehaviour[] m_actors;
    [SerializeField]
    SuctionIrrigationAgentBase m_agent;

    [NonSerialized]
    PropertyInfo[] m_propertyInfo;
    bool m_shouldInitialize = false;
    bool m_isDelayEnd = false;
}