Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
NeodroidEnvironment.cs
Go to the documentation of this file.
1 using System;
2 using System.Globalization;
9 using UnityEditor;
10 using UnityEngine;
11 
12 namespace droid.Runtime.Environments {
16  public abstract class NeodroidEnvironment : PrototypingGameObject,
17  IEnvironment {
18  #region Fields
19 
20  #if UNITY_EDITOR
21  const int _script_execution_order = -20;
22  #endif
23 
26  protected bool _Configure;
27 
28 
31  [SerializeField]
32  protected float _Energy_Spent;
33 
34  [SerializeField] Reaction _last_reaction;
35 
38  [SerializeField]
39  protected float _Lastest_Reset_Time;
40 
43  [SerializeField]
44  protected string _LastTermination_Reason = "None";
45 
49 
52  [SerializeField]
53  protected bool _Resetting;
54 
57  [Header("Environment", order = 100)]
58  [SerializeField]
60 
63  [SerializeField]
64  protected bool _Terminable = true;
65 
68  [SerializeField]
69  protected bool _Terminated;
70 
71  [SerializeField] protected int _current_frame_number;
72 
73  #endregion
74 
78  public abstract override String PrototypingTypeName { get; }
79 
82  public bool Terminated { get { return this._Terminated; } set { this._Terminated = value; } }
83 
87  public Reaction LastReaction { get { return this._last_reaction; } set { this._last_reaction = value; } }
88 
89 
90 
93  public bool IsResetting { get { return this._Resetting; } }
94 
97  public String LastTerminationReason {
98  get { return this._LastTermination_Reason; }
99  set { this._LastTermination_Reason = value; }
100  }
101 
104  public abstract void PostStep();
105 
109  public abstract Reaction SampleReaction();
110 
115  public abstract EnvironmentState ReactAndCollectState(Reaction reaction);
116 
121  public abstract void React(Reaction reaction);
122 
125  public abstract void Tick();
126 
130  public abstract EnvironmentState CollectState();
131 
135  public void IdentifierString(DataPoller recipient) { recipient.PollData(this.Identifier); }
136 
140  public void EnergyString(DataPoller recipient) {
141  recipient.PollData(this._Energy_Spent.ToString(CultureInfo.InvariantCulture));
142  }
143 
144 
148  public void TerminatedBoolean(DataPoller recipient) {
149  if (this._Terminated) {
150  recipient.PollData(true);
151  }
152 
153  recipient.PollData(false);
154  }
155 
159  protected override void Setup() {
160  this.PreSetup();
161  if (this._Simulation_Manager == null) {
162  this._Simulation_Manager = FindObjectOfType<AbstractNeodroidManager>();
163  }
164 
165  #if UNITY_EDITOR
166  if (!Application.isPlaying) {
167  var manager_script = MonoScript.FromMonoBehaviour(this);
168  if (MonoImporter.GetExecutionOrder(manager_script) != _script_execution_order) {
169  MonoImporter.SetExecutionOrder(manager_script,
170  _script_execution_order); // Ensures that PreStep is called first, before all other scripts.
171  Debug.LogWarning("Execution Order changed, you will need to press play again to make everything function correctly!");
172  EditorApplication.isPlaying = false;
173  //TODO: UnityEngine.Experimental.LowLevel.PlayerLoop.SetPlayerLoop(new UnityEngine.Experimental.LowLevel.PlayerLoopSystem());
174  }
175  }
176  #endif
177  }
178 
182  protected override void RegisterComponent() {
183  if (this._Simulation_Manager != null) {
184  this._Simulation_Manager =
185  NeodroidUtilities.RegisterComponent((PausableManager)this._Simulation_Manager, this);
186  }
187  }
188 
192  protected override void UnRegisterComponent() { this._Simulation_Manager?.UnRegister(this); }
193 
196  protected virtual void PreSetup() { }
197 
201  public abstract void ObservationsString(DataPoller recipient);
202 
203  public abstract void EnvironmentReset();
204 
205 
206  #region Public Methods
207 
211  public void FrameString(DataPoller recipient) { recipient.PollData($"{this.CurrentFrameNumber}"); }
212 
213 
214  #endregion
215 
216  #region Properties
217 
220  public int CurrentFrameNumber {
221  get { return this._current_frame_number; }
222  set { this._current_frame_number = value; }
223  }
224 
225 
226  #endregion
227  }
228 }