Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Lunar Lander example and fixed small issue with GA compareTo an…
…d Equals (from a bug found in Java)
- Loading branch information
1 parent
3ee8f79
commit 5a9f6e0
Showing
11 changed files
with
352 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?xml version="1.0"?> | ||
<configuration> | ||
<startup><supportedRuntime version="v2.0.50727" sku="Client"/></startup></configuration> |
Oops, something went wrong.