1 using System.Collections.Generic;
8 namespace droid.Runtime.Environments {
13 [AddComponentMenu(
"Neodroid/Environments/ActorisedPrototypingEnvironment")]
16 #region NeodroidCallbacks 21 protected override void Clear() {
22 this.Displayers.Clear();
23 this.Configurables.Clear();
25 this.Observers.Clear();
26 this.Listeners.Clear();
36 Debug.Log($
"Sampling a reaction for environment {this.Identifier}");
40 this._Sample_Motions.Clear();
42 foreach (var actor
in this.Actors) {
43 var actor_value = actor.Value;
44 if (actor_value?.Actuators != null) {
45 foreach (var actuator
in actor_value.Actuators) {
46 var actuator_value = actuator.Value;
47 if (actuator_value != null) {
48 this._Sample_Motions.Add(
new ActuatorMotion(actor.Key, actuator.Key, actuator_value.Sample()));
54 if (this._Terminated) {
57 Debug.Log(
"SampleReaction resetting environment");
63 return new Reaction(reset_reaction, this.Identifier);
66 var rp =
new ReactionParameters(
true,
true, episode_count :
true) {IsExternal =
false};
67 return new Reaction(rp, this._Sample_Motions.ToArray(), null, null, null,
"", this.Identifier);
80 public Dictionary<string, IActor> Actors {
get; } =
new Dictionary<string, IActor>();
93 return this.CollectState();
110 if (!this.Actors.ContainsKey(identifier)) {
112 if (this.Debugging) {
113 Debug.Log($
"Environment {this.name} has registered actor {identifier}");
117 this.Actors.Add(identifier, actor);
119 Debug.LogWarning($
"WARNING! Please check for duplicates, Environment {this.name} already has actor {identifier} registered");
129 if (this.Actors.ContainsKey(obj)) {
131 if (this.Debugging) {
132 Debug.Log($
"Environment {this.name} unregistered actor {obj}");
135 this.Actors.Remove(obj);
143 #region PrivateMethods 150 lock (this._Reaction_Lock) {
151 if (this.Actors != null) {
152 foreach (var a
in this.Actors.Values) {
153 if (a.Actuators != null) {
154 foreach (var m
in a.Actuators.Values) {
155 this._Energy_Spent += m.GetEnergySpend();
163 if (this._objective_function != null) {
164 signal = this._objective_function.Evaluate();
168 if (this._ReplyWithDescriptionThisStep) {
170 if (this.Debugging) {
171 Debug.Log(
"Describing Environment");
175 if (this._objective_function != null) {
179 var episode_length = 0;
180 if (this._objective_function) {
181 episode_length = this._objective_function.EpisodeLength;
188 this._Observables.Clear();
189 foreach (var item
in this.Observers) {
190 if (item.Value != null) {
191 if (item.Value.FloatEnumerable != null) {
192 this._Observables.AddRange(item.Value.FloatEnumerable);
195 if (this.Debugging) {
196 Debug.Log($
"Sensor with key {item.Key} has a null FloatEnumerable value");
202 if (this.Debugging) {
203 Debug.Log($
"Sensor with key {item.Key} has a null value");
209 var obs = this._Observables.ToArray();
211 var time = Time.time - this._Lastest_Reset_Time;
215 this.CurrentFrameNumber,
220 this.LastTerminationReason,
223 if (this._Simulation_Manager.SimulatorConfiguration.AlwaysSerialiseUnobservables
224 ||
this._ReplyWithDescriptionThisStep) {
225 state.Unobservables =
new Unobservables(ref this._Tracked_Rigid_Bodies, ref this._Poses);
228 if (this._Simulation_Manager.SimulatorConfiguration.AlwaysSerialiseIndividualObservables
229 ||
this._ReplyWithDescriptionThisStep) {
230 state.Observers = this.Observers.Values.ToArray();
242 recipient.
PollData(
string.Join(
"\n\n",
243 this.Observers.Values.Select(e => $
"{e.Identifier}:\n{e.ToString()}")));
249 void SendToDisplayers(
Reaction reaction) {
253 if (this.Debugging) {
254 Debug.Log(
"Applying " + displayable +
" To " + this.name +
"'s displayers");
257 var displayable_name = displayable.DisplayableName;
258 if (this.Displayers.ContainsKey(displayable_name) && this.Displayers[displayable_name] != null) {
259 var v = displayable.DisplayableValue;
260 this.Displayers[displayable_name].Display(v);
263 if (this.Debugging) {
264 Debug.Log(
"Could find not displayer with the specified name: " + displayable_name);
275 void SendToActuators(
Reaction reaction) {
277 foreach (var motion
in reaction.
Motions) {
279 if (this.Debugging) {
280 Debug.Log(
"Applying " + motion +
" To " + this.name +
"'s actors");
283 var motion_actor_name = motion.ActorName;
284 if (this.Actors.ContainsKey(motion_actor_name) && this.Actors[motion_actor_name] != null) {
285 this.Actors[motion_actor_name].ApplyMotion(motion);
288 if (this.Debugging) {
289 Debug.Log(
"Could find not actor with the specified name: " + motion_actor_name);
305 foreach (var actor
in this.Actors.Values) {
306 actor?.EnvironmentReset();
310 #region EnvironmentStateSetters void Register(IActor actor)
Has a possible direction given by the sign of the float in strength
Environment to be used with the prototyping components.
abstract void PollData(dynamic data)
override void InnerStep(Reaction reaction)
override void InnerResetRegisteredObjects()
override Reaction SampleReaction()
override void ObservationsString(DataPoller recipient)
override EnvironmentState CollectState()
void UnRegister(IActor actor)
Displayable [] Displayables
void Register(IActor actor, string identifier)
override EnvironmentState ReactAndCollectState(Reaction reaction)
void UnRegister(IActor t, string obj)