3 using System.Collections.Generic;
17 namespace droid.Runtime.Environments {
22 [AddComponentMenu(
"Neodroid/Environments/PrototypingEnvironment")]
25 #region NeodroidCallbacks 30 protected override void Clear() {
31 this.Displayers.Clear();
32 this.Configurables.Clear();
33 this.Actuators.Clear();
34 this.Observers.Clear();
35 this.Listeners.Clear();
45 Debug.Log($
"Sampling a reaction for environment {this.Identifier}");
49 this._Sample_Motions.Clear();
51 foreach (var actuator
in this.Actuators) {
52 var actor_value = actuator.Value;
54 var actuator_value = actuator.Value;
55 if (actuator_value != null) {
56 this._Sample_Motions.Add(
new ActuatorMotion(actuator.Key, actuator.Key, actuator_value.Sample()));
60 if (this._Terminated) {
63 Debug.Log(
"SampleReaction resetting environment");
69 return new Reaction(reset_reaction, this.Identifier);
72 var rp =
new ReactionParameters(
true,
true, episode_count :
true) {IsExternal =
false};
73 return new Reaction(rp, this._Sample_Motions.ToArray(), null, null, null,
"", this.Identifier);
86 public Dictionary<string, IActuator> Actuators {
get; } =
new Dictionary<string, IActuator>();
93 public override string PrototypingTypeName {
get {
return "PrototypingEnvironment"; } }
108 if (!this.Actuators.ContainsKey(identifier)) {
110 if (this.Debugging) {
111 Debug.Log($
"Environment {this.name} has registered actuator {identifier}");
115 this.Actuators.Add(identifier, obj);
117 Debug.LogWarning($
"WARNING! Please check for duplicates, Environment {this.name} already has actuator {identifier} registered");
124 if (this.Actuators.ContainsKey(obj)) {
126 if (this.Debugging) {
127 Debug.Log($
"Environment {this.name} unregistered observer {obj}");
130 this.Actuators.Remove(obj);
138 #region PrivateMethods 145 lock (this._Reaction_Lock) {
146 if (this.Actuators != null) {
147 foreach (var m
in this.Actuators.Values) {
148 this._Energy_Spent += m.GetEnergySpend();
154 if (this._objective_function != null) {
155 signal = this._objective_function.Evaluate();
159 if (this._ReplyWithDescriptionThisStep) {
161 if (this.Debugging) {
162 Debug.Log(
"Describing Environment");
166 if (this._objective_function != null) {
170 var episode_length = 0;
171 if (this._objective_function) {
172 episode_length = this._objective_function.EpisodeLength;
175 var virtual_actors =
new Dictionary<String, IActor>();
176 virtual_actors.Add(
"All",
new VirtualActor(this.Actuators));
182 this._Observables.Clear();
183 foreach (var item
in this.Observers) {
184 if (item.Value != null) {
185 if (item.Value.FloatEnumerable != null) {
186 this._Observables.AddRange(item.Value.FloatEnumerable);
189 if (this.Debugging) {
190 Debug.Log($
"Sensor with key {item.Key} has a null FloatEnumerable value");
196 if (this.Debugging) {
197 Debug.Log($
"Sensor with key {item.Key} has a null value");
203 var obs = this._Observables.ToArray();
205 var time = Time.time - this._Lastest_Reset_Time;
209 this.CurrentFrameNumber,
214 this.LastTerminationReason,
217 if (this._Simulation_Manager.SimulatorConfiguration.AlwaysSerialiseUnobservables
218 ||
this._ReplyWithDescriptionThisStep) {
219 state.Unobservables =
new Unobservables(ref this._Tracked_Rigid_Bodies, ref this._Poses);
222 if (this._Simulation_Manager.SimulatorConfiguration.AlwaysSerialiseIndividualObservables
223 ||
this._ReplyWithDescriptionThisStep) {
224 state.Observers = this.Observers.Values.ToArray();
236 recipient.
PollData(
string.Join(
"\n\n",
237 this.Observers.Values.Select(e => $
"{e.Identifier}:\n{e.ToString()}")));
243 void SendToActuators(
Reaction reaction) {
245 foreach (var motion
in reaction.
Motions) {
247 if (this.Debugging) {
248 Debug.Log(
"Applying " + motion +
" To " + this.name +
" actuator");
251 var motion_actuator_name = motion.ActuatorName;
252 if (this.Actuators.ContainsKey(motion_actuator_name)
253 && this.Actuators[motion_actuator_name] != null) {
254 this.Actuators[motion_actuator_name].ApplyMotion(motion);
257 if (this.Debugging) {
258 Debug.Log(
"Could find not actuator with the specified name: " + motion_actuator_name);
274 foreach (var actuator
in this.Actuators.Values) {
275 actuator?.EnvironmentReset();
override Reaction SampleReaction()
Has a possible direction given by the sign of the float in strength
abstract void PollData(dynamic data)
void Register(IActuator obj)
Environment to be used with the prototyping components.
override void InnerStep(Reaction reaction)
override void InnerResetRegisteredObjects()
override EnvironmentState CollectState()
override void ObservationsString(DataPoller recipient)
void UnRegister(IActuator t, String obj)
void UnRegister(IActuator obj)
void Register(IActuator obj, String identifier)