Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
ReachArea.cs
Go to the documentation of this file.
7 using UnityEngine;
8 
9 namespace droid.Runtime.Prototyping.Evaluation {
16  }
17 
24  }
25 
26  //[RequireComponent (typeof(BoundingBox))]
27  //[RequireComponent (typeof(BoxCollider))]
28  [AddComponentMenu(EvaluationComponentMenuPath._ComponentMenuPath
29  + "ReachArea"
30  + EvaluationComponentMenuPath._Postfix)]
31  public class ReachArea : ObjectiveFunction {
32  [SerializeField] Collider _actor = null;
33 
34  [SerializeField] Collider _area = null;
35 
36  [SerializeField] bool _based_on_tags = false;
37  [SerializeField] ActorColliding _colliding = ActorColliding.Not_colliding_;
38 
39  [SerializeField] Obstruction[] _obstructions;
40 
41  //Used for.. if outside playable area then reset
42  [SerializeField] ActorOverlapping _overlapping = ActorOverlapping.Outside_area_;
43 
44  [SerializeField] BoundingBox _playable_area;
45 
46  public override void InternalReset() { }
47 
48  public override float InternalEvaluate() {
49  /*var regularising_term = 0f;
50 
51  foreach (var ob in _obstructions) {
52  RaycastHit ray_hit;
53  Physics.Raycast (_actor.transform.position, (ob.transform.position - _actor.transform.position).normalized, out ray_hit, LayerMask.NameToLayer ("Obstruction"));
54  regularising_term += -Mathf.Abs (Vector3.Distance (ray_hit.point, _actor.transform.position));
55  //regularising_term += -Mathf.Abs (Vector3.Distance (ob.transform.position, _actor.transform.position));
56  }
57 
58  reward += 0.2 * regularising_term;*/
59 
60  //reward += 1 / Mathf.Abs (Vector3.Distance (_area.transform.position, _actor.transform.position)); // Inversely porpotional to the absolute distance, closer higher reward
61 
62  if (this._overlapping == ActorOverlapping.Inside_area_) {
63  this.ParentEnvironment.Terminate("Inside goal area");
64  return 1f;
65  }
66 
67  if (this._colliding == ActorColliding.Colliding_) {
68  this.ParentEnvironment.Terminate("Actor colliding with obstruction");
69  }
70 
71  if (this._playable_area && this._actor) {
72  if (!this._playable_area.Bounds.Intersects(this._actor.GetComponent<Collider>().bounds)) {
73  this.ParentEnvironment.Terminate("Actor is outside playable area");
74  }
75  }
76 
77  return 0f;
78  }
79 
80  protected override void PostSetup() {
81  if (!this._area) {
82  this._area = FindObjectOfType<Sensor>().gameObject.GetComponent<Collider>();
83  }
84 
85  if (!this._actor) {
86  this._actor = FindObjectOfType<Actor>().gameObject.GetComponent<Collider>();
87  }
88 
89  if (this._obstructions.Length <= 0) {
90  this._obstructions = FindObjectsOfType<Obstruction>();
91  }
92 
93  if (!this._playable_area) {
94  this._playable_area = FindObjectOfType<BoundingBox>();
95  }
96 
97  NeodroidUtilities
98  .RegisterCollisionTriggerCallbacksOnChildren<ChildCollider3DSensor, Collider, Collision>(this,
99  this
100  ._area
101  .transform,
102  this
103  .OnCollisionEnterChild,
104  this
105  .OnTriggerEnterChild,
106  this
107  .OnCollisionExitChild,
108  this
109  .OnTriggerExitChild,
110  this
111  .OnCollisionStayChild,
112  this
113  .OnTriggerStayChild);
114 
115  NeodroidUtilities
116  .RegisterCollisionTriggerCallbacksOnChildren<ChildCollider3DSensor, Collider, Collision>(this,
117  this
118  ._actor
119  .transform,
120  this
121  .OnCollisionEnterChild,
122  this
123  .OnTriggerEnterChild,
124  this
125  .OnCollisionExitChild,
126  this
127  .OnTriggerExitChild,
128  this
129  .OnCollisionStayChild,
130  this
131  .OnTriggerStayChild);
132  }
133 
134  void OnTriggerEnterChild(GameObject child_game_object, Collider other_game_object) {
135  if (this._actor) {
136  if (this._based_on_tags) {
137  if (child_game_object.CompareTag(this._area.tag) && other_game_object.CompareTag(this._actor.tag)) {
138  #if NEODROID_DEBUG
139  if (this.Debugging) {
140  Debug.Log("Actor is inside area");
141  }
142  #endif
143 
144  this._overlapping = ActorOverlapping.Inside_area_;
145  }
146 
147  if (child_game_object.CompareTag(this._actor.tag) && other_game_object.CompareTag("Obstruction")) {
148  #if NEODROID_DEBUG
149  if (this.Debugging) {
150  Debug.Log("Actor is colliding");
151  }
152  #endif
153 
154  this._colliding = ActorColliding.Colliding_;
155  }
156  } else {
157  if (child_game_object == this._area.gameObject
158  && other_game_object.gameObject == this._actor.gameObject) {
159  #if NEODROID_DEBUG
160  if (this.Debugging) {
161  Debug.Log("Actor is inside area");
162  }
163  #endif
164 
165  this._overlapping = ActorOverlapping.Inside_area_;
166  }
167 
168  if (child_game_object == this._actor.gameObject && other_game_object.CompareTag("Obstruction")) {
169  #if NEODROID_DEBUG
170  if (this.Debugging) {
171  Debug.Log("Actor is colliding");
172  }
173  #endif
174 
175  this._colliding = ActorColliding.Colliding_;
176  }
177  }
178  }
179  }
180 
181  void OnTriggerStayChild(GameObject child_game_object, Collider other_game_object) {
182  if (this._actor) {
183  if (this._based_on_tags) {
184  if (child_game_object.CompareTag(this._area.tag) && other_game_object.CompareTag(this._actor.tag)) {
185  #if NEODROID_DEBUG
186  if (this.Debugging) {
187  Debug.Log("Actor is inside area");
188  }
189  #endif
190 
191  this._overlapping = ActorOverlapping.Inside_area_;
192  }
193 
194  if (child_game_object.CompareTag(this._actor.tag) && other_game_object.CompareTag("Obstruction")) {
195  #if NEODROID_DEBUG
196  if (this.Debugging) {
197  Debug.Log("Actor is colliding");
198  }
199  #endif
200 
201  this._colliding = ActorColliding.Colliding_;
202  }
203  } else {
204  if (child_game_object == this._area.gameObject
205  && other_game_object.gameObject == this._actor.gameObject) {
206  #if NEODROID_DEBUG
207  if (this.Debugging) {
208  Debug.Log("Actor is inside area");
209  }
210  #endif
211 
212  this._overlapping = ActorOverlapping.Inside_area_;
213  }
214 
215  if (child_game_object == this._actor.gameObject && other_game_object.CompareTag("Obstruction")) {
216  #if NEODROID_DEBUG
217  if (this.Debugging) {
218  Debug.Log("Actor is colliding");
219  }
220  #endif
221 
222  this._colliding = ActorColliding.Colliding_;
223  }
224  }
225  }
226  }
227 
228  void OnTriggerExitChild(GameObject child_game_object, Collider other_game_object) {
229  if (this._actor) {
230  if (this._based_on_tags) {
231  if (child_game_object.CompareTag(this._area.tag) && other_game_object.CompareTag(this._actor.tag)) {
232  #if NEODROID_DEBUG
233  if (this.Debugging) {
234  Debug.Log("Actor is outside area");
235  }
236  #endif
237 
238  this._overlapping = ActorOverlapping.Outside_area_;
239  }
240 
241  if (child_game_object.CompareTag(this._actor.tag) && other_game_object.CompareTag("Obstruction")) {
242  #if NEODROID_DEBUG
243  if (this.Debugging) {
244  Debug.Log("Actor is not colliding");
245  }
246  #endif
247 
248  this._colliding = ActorColliding.Not_colliding_;
249  }
250  } else {
251  if (child_game_object == this._area.gameObject
252  && other_game_object.gameObject == this._actor.gameObject) {
253  #if NEODROID_DEBUG
254  if (this.Debugging) {
255  Debug.Log("Actor is outside area");
256  }
257  #endif
258 
259  this._overlapping = ActorOverlapping.Outside_area_;
260  }
261 
262  if (child_game_object == this._actor.gameObject && other_game_object.CompareTag("Obstruction")) {
263  #if NEODROID_DEBUG
264  if (this.Debugging) {
265  Debug.Log("Actor is not colliding");
266  }
267  #endif
268 
269  this._colliding = ActorColliding.Not_colliding_;
270  }
271  }
272  }
273  }
274 
275  void OnCollisionEnterChild(GameObject child_game_object, Collision collision) { }
276 
277  void OnCollisionStayChild(GameObject child_game_object, Collision collision) { }
278 
279  void OnCollisionExitChild(GameObject child_game_object, Collision collision) { }
280  }
281 }