/
NeuralPilot.cs
59 lines (52 loc) · 1.96 KB
/
NeuralPilot.cs
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
using System;
using Encog.ML.Data;
using Encog.ML.Data.Basic;
using Encog.Neural.Networks;
using Encog.Util.Arrayutil;
namespace Encog.Examples.Lunar
{
public class NeuralPilot
{
private readonly NormalizedField _fuelStats;
private readonly BasicNetwork _network;
private readonly bool _track;
private readonly NormalizedField _altitudeStats;
private readonly NormalizedField _velocityStats;
public NeuralPilot(BasicNetwork network, bool track)
{
_fuelStats = new NormalizedField(NormalizationAction.Normalize, "fuel", 200, 0, -0.9, 0.9);
_altitudeStats = new NormalizedField(NormalizationAction.Normalize, "altitude", 10000, 0, -0.9, 0.9);
_velocityStats = new NormalizedField(NormalizationAction.Normalize, "velocity",
LanderSimulator.TerminalVelocity, -LanderSimulator.TerminalVelocity,
-0.9, 0.9);
_track = track;
_network = network;
}
public int ScorePilot()
{
var sim = new LanderSimulator();
while (sim.Flying)
{
IMLData input = new BasicMLData(3);
input[0] = _fuelStats.Normalize(sim.Fuel);
input[1] = _altitudeStats.Normalize(sim.Altitude);
input[2] = _velocityStats.Normalize(sim.Velocity);
IMLData output = _network.Compute(input);
double value = output[0];
bool thrust;
if (value > 0)
{
thrust = true;
if (_track)
Console.WriteLine(@"THRUST");
}
else
thrust = false;
sim.Turn(thrust);
if (_track)
Console.WriteLine(sim.Telemetry());
}
return (sim.Score);
}
}
}