public OddTest() { Console.WriteLine("Welcome to Neural Odd Test Example"); FeedForwardNetwork network = new FeedForwardNetwork(new SigmoidFunction(), 1, 10, 1); BackPropagationLearning teacher = new BackPropagationLearning(network); teacher.LearningRate = 0.1; Random R = new Random(Environment.TickCount); Console.WriteLine("Training..."); for (int i = 0; i < 1000; i++) { Console.Write("[T] Input >> "); int Input = R.Next(1024); Console.WriteLine(Input); teacher.Teach(Binary(Input), (Input % 2 == 0) ? (new double[] { 0 }) : (new double[] { 1 })); Console.Write("[T] Output >> "); Console.WriteLine(network.Launch(Binary(Input))[0].ToString()); } Console.WriteLine("Examing..."); Console.Write("[E] Input >> "); string userString = Console.ReadLine(); while (userString != "stop") { int Number = int.Parse(userString.Split(' ')[0]); Console.Write("[E] Output >> "); Print(network.Launch(Binary(Number))); Console.Write("[E] Input >> "); userString = Console.ReadLine(); } Console.WriteLine(); }
public void Teach(double[] Inputs, double[] Outputs) { Errors = new double[Network.Layers.Count][]; for (int i = 0; i < Network.Layers.Count; i++) { Errors[i] = new double[Network.Layers[i].Neurons.Count]; } Network.Launch(Inputs); for (int i = 0; i < Network.Layers[Network.Layers.Count - 1].Neurons.Count; i++) { Errors[Network.Layers.Count - 1][i] = (Outputs[i] - Network.Layers[Network.Layers.Count - 1].Neurons[i].OutputImpulse) * (Network.ActivationFunction.Derivative(Network.Layers[Network.Layers.Count - 1].Neurons[i].InputImpulse)); foreach (Link Bridge in Network.Layers[Network.Layers.Count - 1].Neurons[i].Inputs) { Errors[Network.Layers.Count - 2][Bridge.FromIndex] += (Bridge.Weight * Errors[Network.Layers.Count - 1][i]); Bridge.Weight += (LearningRate * Errors[Network.Layers.Count - 1][i] * Bridge.From.OutputImpulse); } Network.Layers[Network.Layers.Count - 1].Neurons[i].Bias += (LearningRate * Errors[Network.Layers.Count - 1][i]); } for (int i = (Network.Layers.Count - 2); i > 0; i--) { for (int j = 0; j < Network.Layers[i].Neurons.Count; j++) { Errors[i][j] *= Network.ActivationFunction.Derivative(Network.Layers[i].Neurons[j].InputImpulse); foreach (Link Bridge in Network.Layers[i].Neurons[j].Inputs) { Errors[i - 1][Bridge.FromIndex] += Errors[i][j]; Bridge.Weight += (LearningRate * Errors[i][j] * Bridge.From.OutputImpulse); } Network.Layers[i].Neurons[j].Bias += (LearningRate * Errors[i][j]); } } }
public void Think(Vector2 _ghostPosition, Vector2 _pacmanPosition) { Vector2 toGhost = Vector2.Zero; float Alpha = 0f; if (_ghostPosition != -Vector2.One) { toGhost = _ghostPosition - Position; float AlphaCosine = ((float)Math.Cos(Rotation) * toGhost.X + (float)Math.Sin(Rotation) * toGhost.Y) / toGhost.Length(); float AlphaSine = ((float)Math.Cos(Rotation) * toGhost.Y - (float)Math.Sin(Rotation) * toGhost.X) / toGhost.Length(); Alpha = (AlphaSine < 0) ? (-(float)Math.Acos(AlphaCosine)) : ((float)Math.Acos(AlphaCosine)); } Vector2 toPacman = Vector2.Zero; float Beta = 0f; if (_pacmanPosition != -Vector2.One) { toPacman = _pacmanPosition - Position; float BetaCosine = ((float)Math.Cos(Rotation) * toPacman.X + (float)Math.Sin(Rotation) * toPacman.Y) / toPacman.Length(); float BetaSine = ((float)Math.Cos(Rotation) * toPacman.Y - (float)Math.Sin(Rotation) * toPacman.X) / toPacman.Length(); Beta = (BetaSine < 0) ? (-(float)Math.Acos(BetaCosine)) : ((float)Math.Acos(BetaCosine)); } double[] Answer = Network.Launch(new double[] { toGhost.Length() / 250, Alpha, toPacman.Length() / 250, Beta }); float Gamma = (float)Answer[0]; //Rotation = MathHelper.Lerp(Rotation, Rotation + (float)Math.Asin(SineGamma), 0.4f); //Rotation = MathHelper.Lerp(Rotation, Rotation + (float)SineGamma, 0.4f); Rotation += (float)Gamma; Rotation %= (float)MathHelper.TwoPi; }
public void ManualExaming(FeedForwardNetwork _network) { Console.Write("[E] Inputs >> "); string userString = Console.ReadLine(); while (userString != "stop") { double[] Inputs = ParseDoubles(userString); double[] Outputs = _network.Launch(Inputs); Console.Write("[E] Outputs >> "); for (int i = 0; i < Outputs.Length; i++) { Console.Write(Outputs[i] + " "); } Console.WriteLine(); Console.Write("[E] Inputs >> "); userString = Console.ReadLine(); } }