Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
ScriptedEnviroment.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
8 using UnityEngine;
9 using Random = UnityEngine.Random;
10 
11 namespace droid.Runtime.Environments {
15  [AddComponentMenu("Neodroid/Environments/ScriptedEnvironment")]
19  [SerializeField]
20  Renderer _actor_renderer = null;
21 
24  [SerializeField]
25  int _actor_x = 0;
26 
29  [SerializeField]
30  int _actor_y = 0;
31 
34  [SerializeField]
35  Renderer _goal_renderer = null;
36 
39  [SerializeField]
40  int _goal_x = 0;
41 
44  [SerializeField]
45  int _goal_y = 0;
46 
49  int[,] _grid = null;
50 
53  [SerializeField]
54  int _height = 0;
55 
56  List<IMotion> _motions = new List<IMotion>();
57 
60  [SerializeField]
61  IManager _time_simulation_manager = null;
62 
65  [SerializeField]
66  int _width = 0;
67 
71  public override string PrototypingTypeName { get { return "ScriptedEnvironment"; } }
72 
75  public int ActorX {
76  get { return this._actor_x; }
77  set { this._actor_x = Mathf.Max(0, Mathf.Min(this._width - 1, value)); }
78  }
79 
82  public int ActorY {
83  get { return this._actor_y; }
84  set { this._actor_y = Mathf.Max(0, Mathf.Min(this._height - 1, value)); }
85  }
86 
89  public int GoalX {
90  get { return this._goal_x; }
91  set { this._goal_x = Mathf.Max(0, Mathf.Min(this._width - 1, value)); }
92  }
93 
96  public int GoalY {
97  get { return this._goal_y; }
98  set { this._goal_y = Mathf.Max(0, Mathf.Min(this._height - 1, value)); }
99  }
100 
104  protected override void Setup() {
105  this._grid = new int[this._width, this._height];
106 
107  var k = 0;
108  for (var i = 0; i < this._width; i++) {
109  for (var j = 0; j < this._height; j++) {
110  this._grid[i, j] = k++;
111  }
112  }
113 
114  this._time_simulation_manager =
115  NeodroidUtilities.RegisterComponent((AbstractNeodroidManager)this._time_simulation_manager, this);
116  }
117 
121  public override void PostStep() {
122  if (this._goal_renderer) {
123  this._goal_renderer.transform.position = new Vector3(this.GoalX, 0, this.GoalY);
124  }
125 
126  if (this._actor_renderer) {
127  this._actor_renderer.transform.position = new Vector3(this.ActorX, 0, this.ActorY);
128  }
129  }
130 
135  public override Reaction SampleReaction() {
136  this._motions.Clear();
137 
138  var strength = Random.Range(0, 4);
139  this._motions.Add(new ActuatorMotion("", "", strength));
140 
141  var rp = new ReactionParameters(true, true, episode_count : true) {IsExternal = false};
142  return new Reaction(rp, this._motions.ToArray(), null, null, null, "");
143  }
144 
151  public override EnvironmentState ReactAndCollectState(Reaction reaction) {
152  this.React(reaction);
153  return this.CollectState();
154  }
155 
156  public override void React(Reaction reaction) {
157  foreach (var motion in reaction.Motions) {
158  switch ((int)motion.Strength) {
159  case 0:
160  this.ActorY += 1;
161  break;
162  case 1:
163  this.ActorX += 1;
164  break;
165  case 2:
166  this.ActorY -= 1;
167  break;
168  case 3:
169  this.ActorX -= 1;
170  break;
171  default:
172  throw new ArgumentOutOfRangeException();
173  }
174  }
175  }
176 
180  public override void Tick() { }
181 
182  public override EnvironmentState CollectState() {
183  var actor_idx = this._grid[this.ActorX, this.ActorY];
184  var goal_idx = this._grid[this.GoalX, this.GoalY];
185 
186  var terminated = actor_idx == goal_idx;
187  var signal = terminated ? 1 : 0;
188 
189  var time = Time.time - this._Lastest_Reset_Time;
190 
191  var observables = new float[] {actor_idx};
192 
193  return new EnvironmentState(this.Identifier, 0, 0, time, signal, terminated, ref observables);
194  }
195 
200  public override void ObservationsString(DataPoller recipient) {
201  recipient.PollData(this.CollectState().ToString());
202  }
203 
204  public override void EnvironmentReset() { }
205  }
206 }
Has a possible direction given by the sign of the float in strength
Definition: MotorMotion.cs:9
override void ObservationsString(DataPoller recipient)
override EnvironmentState ReactAndCollectState(Reaction reaction)