public void CheckAccuracy() { Random rand = new Random(31415); float[] GetSample() { var s = new float[3]; s[0] = rand.Next(2); s[1] = rand.Next(2); s[2] = ((int)s[0]) ^ ((int)s[1]); return(s); } float Normalize(float value) { return(value * 2f - 1); } const int inputCount = 2; const int hiddenCount = 5; const int outputCount = 1; var referenceNetwork = new ReferenceFFNetwork(inputCount, hiddenCount, outputCount); var neatNetwork = new Network(GetNeatNetworkConnections(referenceNetwork.GetWeights(), inputCount, hiddenCount, outputCount), inputCount + BiasCount, outputCount, new DummyNeatChromosomeEncoder()); var sample = GetSample(); neatNetwork.Sensors[0] = Normalize(sample[0]); neatNetwork.Sensors[1] = Normalize(sample[1]); neatNetwork.Activate(); var err = MathF.Abs(Normalize(neatNetwork.Effectors[0]) - sample[2]); for (var i = 0; i < 100; i++) { var s = GetSample(); neatNetwork.ActivateAndTrain(s, 0.1f); referenceNetwork.Train(new[] { s }, 0.1f); } neatNetwork.Sensors[0] = Normalize(sample[0]); neatNetwork.Sensors[1] = Normalize(sample[1]); neatNetwork.Activate(); var referenceNetworkOutput = referenceNetwork.ComputeOutputs(sample.Take(2).ToArray()); Assert.Equal(referenceNetworkOutput[0], neatNetwork.Effectors[0], 7); Assert.True(err > MathF.Abs(Normalize(neatNetwork.Effectors[0]) - sample[2])); }
public double DoEvaluation(Network net, HardMaze env, int runcount = 1000) { Session session = new Session(env, new FitnessHandler(env.compute_fitness), new NWSELib.EventHandler(doEventHandler), new InstinctActionHandler(HardMaze.createInstinctAction), new OptimaGestureHandler(env.GetOptimaGestureHandler), new TaskBeginHandler(env.TaskBeginHandler)); (List <double> obs, List <double> gesture) = env.reset(net); net.Reset(); double reward = 0.0; bool end = false; for (int time = 0; time < runcount; time++) { List <double> inputs = new List <double>(obs); inputs.AddRange(gesture); List <double> actions = net.Activate(inputs, time, session, reward); (obs, gesture, actions, reward, end) = env.action(net, actions); if (end) { break; } } net.Fitness = Session.fitnessHandler == null ? 0 : Session.fitnessHandler(net, session); net.TaskCompleted = end; return(net.Fitness); }
private void 主页ToolStripMenuItem_Click(object sender, EventArgs e) { if (network == null || network.IsDisposed) { network = new Network(); network.MdiParent = this; network.FormBorderStyle = FormBorderStyle.None; network.Dock = DockStyle.Fill; network.Show(); } else { network.Activate(); } }
public void CheckFeedforwardBp() { const int inputCount = 2; const int hiddenCount = 2; const int outputCount = 1; var referenceNetwork = new ReferenceFFNetwork(inputCount, hiddenCount, outputCount); var neatNetwork = new Network(GetNeatNetworkConnections(referenceNetwork.GetWeights(), inputCount, hiddenCount, outputCount), inputCount + BiasCount, outputCount, new DummyNeatChromosomeEncoder()); var sampleNetworkOutput = referenceNetwork.ComputeOutputs(new[] { 0f, 1f }); neatNetwork.Sensors[0] = 1f; neatNetwork.Sensors[1] = 0f; neatNetwork.Activate(); var neatNetworkOutputBeforeTraining = neatNetwork.Effectors.ToArray(); //the sample network and the NEAT network are identical Assert.Equal(sampleNetworkOutput, neatNetworkOutputBeforeTraining); referenceNetwork.Train(new[] { new[] { 0f, 1f, 1f } }, 0.3f); neatNetwork.ActivateAndTrain(new[] { 1f, 0f, 1f }, 0.3f); //NEAT network effector values have not changed after training before Activate call Assert.Equal(neatNetworkOutputBeforeTraining, neatNetwork.Effectors.ToArray()); sampleNetworkOutput = referenceNetwork.ComputeOutputs(new[] { 0f, 1f }); neatNetwork.Activate(); //backpropagation works fine Assert.Equal(sampleNetworkOutput, neatNetwork.Effectors.ToArray()); Assert.NotEqual(neatNetworkOutputBeforeTraining, neatNetwork.Effectors.ToArray()); }
public void ActivateTest_HiddenNotConnected_Expected_True() { // Create network and leave hidden layer not connected Network net = new Network(); Node inNode1 = new Node(Node.ENodeType.SENSOR, 1); Node inNode2 = new Node(Node.ENodeType.SENSOR, 2); Node hiddenNode = new Node(Node.ENodeType.HIDDEN, 3); Node outNode1 = new Node(Node.ENodeType.OUTPUT, 4); Node outNode2 = new Node(Node.ENodeType.OUTPUT, 5); net.AddNodes(new Node[] { inNode1, inNode2, hiddenNode, outNode1, outNode2 }); net.AddLink(new Link(inNode1, outNode1, false, 1.0f)); net.AddLink(new Link(inNode2, outNode2, false, 1.0f)); Assert.AreEqual(true, net.Activate()); }
public void ActivateTest_OneOutputNotConnected_Expected_False() { // Create network and leave one of the output nodes unconnected Network net = new Network(); Node inNode1 = new Node(Node.ENodeType.SENSOR, 1); Node inNode2 = new Node(Node.ENodeType.SENSOR, 2); Node hiddenNode = new Node(Node.ENodeType.HIDDEN, 3); Node outNode1 = new Node(Node.ENodeType.OUTPUT, 4); Node outNode2 = new Node(Node.ENodeType.OUTPUT, 5); net.AddNodes(new Node[] { inNode1, inNode2, hiddenNode, outNode1, outNode2 }); net.AddLink(new Link(inNode1, hiddenNode, false, 1.0f)); net.AddLink(new Link(inNode2, hiddenNode, false, 1.0f)); net.AddLink(new Link(hiddenNode, outNode1, false, 1.0f)); Assert.AreEqual(false, net.Activate()); }
public void ActivateTest_AllOutputsConnected_Expected_True() { // Create network and make all outputs connected to the network Network net = new Network(); Node inNode1 = new Node(Node.ENodeType.SENSOR, 1); Node inNode2 = new Node(Node.ENodeType.SENSOR, 2); Node hiddenNode = new Node(Node.ENodeType.HIDDEN, 3); Node outNode1 = new Node(Node.ENodeType.OUTPUT, 4); Node outNode2 = new Node(Node.ENodeType.OUTPUT, 5); net.AddNodes(new Node[] { inNode1, inNode2, hiddenNode, outNode1, outNode2 }); net.AddLink(new Link(inNode1, hiddenNode, false, 1.0f)); net.AddLink(new Link(inNode2, hiddenNode, false, 1.0f)); net.AddLink(new Link(hiddenNode, outNode1, false, 1.0f)); net.AddLink(new Link(hiddenNode, outNode2, false, 1.0f)); Assert.AreEqual(true, net.Activate()); }
private static void XorTest(Random r) { Genome xorGene = new Genome(r); xorGene.AddNode(new Node(Node.ENodeType.SENSOR, 1)); xorGene.AddNode(new Node(Node.ENodeType.SENSOR, 2)); xorGene.AddNode(new Node(Node.ENodeType.SENSOR, 3)); xorGene.AddNode(new Node(Node.ENodeType.OUTPUT, 4)); xorGene.AddConnectionGene(1, 4, 0.0f); xorGene.AddConnectionGene(2, 4, 0.0f); xorGene.AddConnectionGene(3, 4, 0.0f); // Run simulation Simulation sim = new Simulation(r, xorGene, 150); sim.Parameters.AreConnectionWeightsCapped = false; sim.Parameters.MaxWeight = 1.0f; sim.Parameters.WeightMutationPower = 2.5f; int numberOfRuns = 200; bool solutionFound = false; Genome bestGenome = null; float[] output = { 0.0f, 0.0f, 0.0f, 0.0f }; float[] bestOutput = new float[4]; float[,] input = { { 1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 1.0f } }; for (int i = 0; i < numberOfRuns; ++i) { double epochBestFitness = 0.0f; float avgConnectionGenes = 0.0f; // Evaluate all genomes foreach (Genome gen in sim.Genomes) { Network network = gen.GetNetwork(); //network.ActivationFunction = new NeuralEvolution.ActivationFunctions.ReLU(); bool couldActivate = true; for (int inputIdx = 0; inputIdx < 4; ++inputIdx) { float[] inputRow = new float[3]; for (int k = 0; k < 3; ++k) { inputRow[k] = input[inputIdx, k]; } network.SetInput(inputRow); if (!network.Activate()) { couldActivate = false; } // Relax network int networkDepth = network.GetMaxDepth(); for (int relax = 0; relax <= networkDepth; ++relax) { network.Activate(); } output[inputIdx] = network.Output[0].Activation; network.Reset(); } float errorsum = (float)(Math.Abs(output[0]) + Math.Abs(1.0 - output[1]) + Math.Abs(1.0 - output[2]) + Math.Abs(output[3])); if (!couldActivate) { errorsum = 4; } gen.Fitness = Math.Pow((4.0 - errorsum), 2); gen.Error = errorsum; if (gen.Fitness > epochBestFitness) { bestOutput[0] = output[0]; bestOutput[1] = output[1]; bestOutput[2] = output[2]; bestOutput[3] = output[3]; epochBestFitness = gen.Fitness; } avgConnectionGenes += gen.ConnectionGenes.Count; if ((output[0] < 0.5f) && (output[1] >= 0.5f) && (output[2] >= 0.5f) && (output[3] < 0.5f)) { bestOutput[0] = output[0]; bestOutput[1] = output[1]; bestOutput[2] = output[2]; bestOutput[3] = output[3]; bestGenome = gen; solutionFound = true; break; } } avgConnectionGenes /= sim.Genomes.Count; Console.WriteLine(String.Format("Epoch {0} | best: {1} | best output: [{2}, {3}, {4}, {5}] | avg genes: {6} | species: {7}", i, epochBestFitness, bestOutput[0], bestOutput[1], bestOutput[2], bestOutput[3], avgConnectionGenes, sim.Species.Count)); // We found a solution so we can exit already if (solutionFound) { break; } // Advance to the next step of simulation sim.Epoch(); } if (solutionFound) { Console.WriteLine(String.Format("Solution found: [{0}, {1}, {2}, {3}]", bestOutput[0], bestOutput[1], bestOutput[2], bestOutput[3])); Console.WriteLine(String.Format("Solution network nodes count: {0} | connections count: {1}", bestGenome.Nodes.Count, bestGenome.ConnectionGenes.Count)); } else { Console.WriteLine("Solution NOT found!"); } }
// cart_and_pole() was take directly from the pole simulator written // by Richard Sutton and Charles Anderson. private static int Go_cart(Network net, int max_steps, int thresh, Random rnd) { float x, /* cart position, meters */ x_dot, /* cart velocity */ theta, /* pole angle, radians */ theta_dot; /* pole angular velocity */ int steps = 0, y; float[] input = new float[5]; //Input loading array float out1; float out2; float twelve_degrees = 0.2094384f; /* if (random_start) { * x = (lrand48()%4800)/1000.0 - 2.4; * x_dot = (lrand48()%2000)/1000.0 - 1; * theta = (lrand48()%400)/1000.0 - .2; * theta_dot = (lrand48()%3000)/1000.0 - 1.5; * } * else */ //x = x_dot = theta = theta_dot = 0.0f; x = (rnd.Next(0, 4800) / 1000.0f - 2.4f); x_dot = (rnd.Next(0, 2000) / 1000.0f - 1.0f); theta = (rnd.Next(0, 400) / 1000.0f - 0.2f); theta_dot = (rnd.Next(0, 3000) / 1000.0f - 1.5f); /*--- Iterate through the action-learn loop. ---*/ while (steps++ < max_steps) { /*-- setup the input layer based on the four iputs --*/ input[0] = 1.0f; //Bias input[1] = (x + 2.4f) / 4.8f; input[2] = (x_dot + 0.75f) / 1.5f; input[3] = (theta + twelve_degrees) / 0.41f; input[4] = (theta_dot + 1.0f) / 2.0f; net.SetInput(input); // Activate the net // If it loops, exit returning only fitness of 1 step if (!(net.Activate())) { return(1); } /*-- Decide which way to push via which output unit is greater --*/ List <Node> outputs = net.Output; out1 = outputs[0].Activation; out2 = outputs[1].Activation; if (out1 > out2) { y = 0; } else { y = 1; } /*--- Apply action to the simulated cart-pole ---*/ Cart_pole(y, ref x, ref x_dot, ref theta, ref theta_dot); /*--- Check for failure. If so, return steps ---*/ if (x < -2.4 || x > 2.4 || theta < -twelve_degrees || theta > twelve_degrees) { return(steps); } } return(steps); }