Skip to content

Commit

Permalink
Added Lunar Lander example and fixed small issue with GA compareTo an…
Browse files Browse the repository at this point in the history
…d Equals (from a bug found in Java)
  • Loading branch information
jeffheaton committed Jul 29, 2011
1 parent 3ee8f79 commit 5a9f6e0
Show file tree
Hide file tree
Showing 11 changed files with 352 additions and 36 deletions.
2 changes: 2 additions & 0 deletions ConsoleExamples/ConsoleExamples.cs
Expand Up @@ -39,6 +39,7 @@
using Encog.Examples.Hopfield.Simple;
using Encog.Examples.Image;
using Encog.Examples.JordanNetwork;
using Encog.Examples.Lunar;
using Encog.Examples.Market;
using Encog.Examples.Persist;
using Encog.Examples.XOR;
Expand Down Expand Up @@ -88,6 +89,7 @@ public ConsoleExamples()
examples.Add(ForestCover.Info);
examples.Add(Encog.Examples.Market.MarketPredict.Info);
examples.Add(CSVMarketExample.Info);
examples.Add(LunarLander.Info);
}

public void ListCommands()
Expand Down
4 changes: 4 additions & 0 deletions ConsoleExamples/ConsoleExamples.csproj
Expand Up @@ -72,6 +72,10 @@
<Compile Include="Examples\Image\ImageNeuralNetwork.cs" />
<Compile Include="Examples\Image\ImagePair.cs" />
<Compile Include="Examples\JordanNetwork\JordanExample.cs" />
<Compile Include="Examples\Lunar\LanderSimulator.cs" />
<Compile Include="Examples\Lunar\LunarLander.cs" />
<Compile Include="Examples\Lunar\NeuralPilot.cs" />
<Compile Include="Examples\Lunar\PilotScore.cs" />
<Compile Include="Examples\Market\Config.cs" />
<Compile Include="Examples\Market\MarketBuildTraining.cs" />
<Compile Include="Examples\Market\MarketEvaluate.cs" />
Expand Down
4 changes: 2 additions & 2 deletions ConsoleExamples/ConsoleExamples.csproj.user
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<StartArguments>Tests</StartArguments>
<StartArguments>-pause lander</StartArguments>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<StartArguments>-pause forest evaluate</StartArguments>
<StartArguments>-pause lander</StartArguments>
</PropertyGroup>
</Project>
70 changes: 70 additions & 0 deletions ConsoleExamples/Examples/Lunar/LanderSimulator.cs
@@ -0,0 +1,70 @@
using System;
using System.Text;
using Encog.Util;

namespace Encog.Examples.Lunar
{
public class LanderSimulator
{
public const double Gravity = 1.62;
public const double Thrust = 10;
public const double TerminalVelocity = 40;

public LanderSimulator()
{
Fuel = 200;
Seconds = 0;
Altitude = 10000;
Velocity = 0;
}

public int Fuel { get; set; }
public int Seconds { get; set; }
public double Altitude { get; set; }
public double Velocity { get; set; }

public int Score
{
get { return (int) ((Fuel*10) + Seconds + (Velocity*1000)); }
}

public void Turn(bool thrust)
{
Seconds++;
Velocity -= Gravity;
Altitude += Velocity;

if (thrust && Fuel > 0)
{
Fuel--;
Velocity += Thrust;
}

Velocity = Math.Max(-TerminalVelocity, Velocity);
Velocity = Math.Min(TerminalVelocity, Velocity);

if (Altitude < 0)
Altitude = 0;
}

public String Telemetry()
{
var result = new StringBuilder();
result.Append("Elapsed: ");
result.Append(Seconds);
result.Append(" s, Fuel: ");
result.Append(Fuel);
result.Append(" l, Velocity: ");
result.Append(Format.FormatDouble(Velocity, 4));
result.Append(" m/s, ");
result.Append((int) Altitude);
result.Append(" m");
return result.ToString();
}

public bool Flying
{
get { return Altitude > 0; }
}
}
}
90 changes: 90 additions & 0 deletions ConsoleExamples/Examples/Lunar/LunarLander.cs
@@ -0,0 +1,90 @@
using System;
using ConsoleExamples.Examples;
using Encog.Engine.Network.Activation;
using Encog.Mathutil.Randomize;
using Encog.ML.Train;
using Encog.Neural.Networks;
using Encog.Neural.Networks.Training.Anneal;
using Encog.Neural.Networks.Training.Genetic;
using Encog.Neural.Pattern;

namespace Encog.Examples.Lunar
{
/// <summary>
/// A lunar lander game where the neural network learns to land a space craft.
/// The neural network learns the proper amount of thrust to land softly
/// and conserve fuel.
///
/// This example is unique because it uses supervised training, yet does not
/// have expected values. For this it can use genetic algorithms or
/// simulated annealing.
/// </summary>
public class LunarLander : IExample
{
public static ExampleInfo Info
{
get
{
var info = new ExampleInfo(
typeof(LunarLander),
"lander",
"Train a neural network to land a space ship.",
"This example uses a genetic algorithm to land a space craft on the moon. No training data is given, but it uses supervised training to land.");
return info;
}
}

#region IExample Members

/// <summary>
/// Program entry point.
/// </summary>
/// <param name="app">Holds arguments and other info.</param>
public void Execute(IExampleInterface app)
{
BasicNetwork network = CreateNetwork();

IMLTrain train;

if (app.Args.Length > 0 && String.Compare(app.Args[0], "anneal", true) == 0)
{
train = new NeuralSimulatedAnnealing(
network, new PilotScore(), 10, 2, 100);
}
else
{
train = new NeuralGeneticAlgorithm(
network, new FanInRandomizer(),
new PilotScore(), 500, 0.1, 0.25);
}

int epoch = 1;

for (int i = 0; i < 50; i++)
{
train.Iteration();
Console.WriteLine(@"Epoch #" + epoch + @" Score:" + train.Error);
epoch++;
}

Console.WriteLine(@"\nHow the winning network landed:");
network = (BasicNetwork) train.Method;
var pilot = new NeuralPilot(network, true);
Console.WriteLine(pilot.ScorePilot());
EncogFramework.Instance.Shutdown();
}

#endregion

public static BasicNetwork CreateNetwork()
{
var pattern = new FeedForwardPattern {InputNeurons = 3};
pattern.AddHiddenLayer(50);
pattern.OutputNeurons = 1;
pattern.ActivationFunction = new ActivationTANH();
var network = (BasicNetwork) pattern.Generate();
network.Reset();
return network;
}
}
}
59 changes: 59 additions & 0 deletions ConsoleExamples/Examples/Lunar/NeuralPilot.cs
@@ -0,0 +1,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);
}
}
}
25 changes: 25 additions & 0 deletions ConsoleExamples/Examples/Lunar/PilotScore.cs
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Encog.ML;
using Encog.Neural.Networks;
using Encog.Neural.Networks.Training;

namespace Encog.Examples.Lunar
{
public class PilotScore : ICalculateScore
{
public double CalculateScore(IMLRegression network)
{
NeuralPilot pilot = new NeuralPilot((BasicNetwork)network, false);
return pilot.ScorePilot();
}


public bool ShouldMinimize
{
get { return false; }
}
}
}
3 changes: 3 additions & 0 deletions EncogCmd/app.config
@@ -0,0 +1,3 @@
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v2.0.50727" sku="Client"/></startup></configuration>

0 comments on commit 5a9f6e0

Please sign in to comment.