-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRepeatObservationEnvironment.cs
More file actions
61 lines (60 loc) · 2.58 KB
/
Copy pathRepeatObservationEnvironment.cs
File metadata and controls
61 lines (60 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
namespace Gradient.Samples {
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Gradient;
using mlagents_envs.base_env;
using numpy;
/// <summary>
/// A simple environment, where agents simply have to repeat observations.
/// <para>(Do what Archimonde does anyone?)</para>
/// </summary>
class RepeatObservationEnvironment : IEnvironment {
float previousObservation;
float observation;
float[] action;
readonly Random random = new Random();
public RepeatObservationEnvironment(int agents) {
this.action = new float[agents];
}
public int AgentCount => this.action.Length;
public BatchedStepResult GetStepResult(string? agentGroupName) {
if (agentGroupName != null) throw new KeyNotFoundException();
return new BatchedStepResult(
obs: new[] { np.ones(new int[] { this.AgentCount, 1 }, dtype: PythonClassContainer<float32>.Instance).__mul__(this.observation) },
reward: (ndarray)ndarray.FromList(this.action).__sub__(this.previousObservation).__abs__().__rsub__(2),
done: np.zeros(this.AgentCount),
max_step: null,
agent_id: np.zeros(this.AgentCount),
action_mask: null);
}
public void Reset() {
this.observation = this.previousObservation = 0;
Array.Fill(this.action, 0);
}
public void SetActions(string? agentGroupName, ndarray actions) {
if (agentGroupName != null) throw new KeyNotFoundException();
for (int agentN = 0; agentN < this.action.Length; agentN++)
this.action[agentN] = (float32)actions[agentN, 0];
}
public void Step() {
this.previousObservation = this.observation;
this.observation = (float)this.random.NextDouble() * 2 - 1;
}
public static void SanityCheck() {
// sanity check
var env = new RepeatObservationEnvironment(agents: 3);
env.Reset();
env.Step();
for(int episode = 0; episode < 100; episode++) {
var observation = (ndarray)env.GetStepResult(null).obs[0];
env.SetActions(null, observation);
env.Step();
var step = env.GetStepResult(null);
ndarray success = step.reward.__ge___dyn(1.99f);
bool allPass = success.all_dyn(keepdims: new ImplicitContainer<object>(null));
Trace.Assert(allPass);
}
}
}
}