Exemplo n.º 1
0
        public void Simple()
        {
            var sim = new GeneticSimulation <double[]>(
                org => 1 / (Math.Abs(10 - org.Genome.Sum()) + .01d),
                CommonGeneticFunctions.CreateSpawningCycleArray(new SampleOrg[] {
                new SampleOrg(new double[] { 1, 2, 3, 0 }),
                new SampleOrg(new double[] { 0, 1, 2 }),
                new SampleOrg(new double[] { 1, 0, 1 }),
            }),
                100);

            //var evolved = new List<SampleOrg>();
            //for (int simCount = 0; simCount < 1000; simCount ++)
            //{
            //    evolved = sim.Evolve().Cast<SampleOrg>().ToList();

            //}

            var evolved   = sim.Evolve(15);
            var finalSums = evolved.Select(o => o.Genome.Sum()).ToArray();
        }
Exemplo n.º 2
0
        public void Sin()
        {
            var rand = new Random();
            var net  = NeuralNetBuilder.From(new NeuralNetConfig()
            {
                InputLabels            = new string[] { "x" },
                OutputLabels           = new string[] { "y" },
                HiddenLayerCount       = 3,
                HiddenLayerNeuronCount = 3,
                //PreActivationFunction = (i, d) =>
                ActivationFunction = d => d > .5 ? 1 : 0
                                     //ActivationFunction = d => 1 / (1 + Math.Pow(Math.E, -d))
            });
            var trainingData = new List <Tuple <double, double> >();

            for (double i = 0; i < Math.PI * 2; i += .1)
            {
                var input  = i;
                var output = Math.Sin(i);
                //var normalizedOutput = output / Math.PI * 2;
                var normalizedOutput = .5 * (output + 1);
                var normalizedInput  = input / (Math.PI * 2);
                trainingData.Add(new Tuple <double, double>(normalizedInput, normalizedOutput));
            }

            var weightCount      = net.WeightCount;
            var biasCount        = net.BiasCount;
            var weightOrgSpawner = new SpawnFunction <double[]>(index =>
            {
                var data = new double[weightCount + biasCount];
                for (int i = 0; i < weightCount + biasCount; i++)
                {
                    var scale = i >= weightCount ? 1 : 1;
                    data[i]   = (rand.NextDouble()) * scale;
                }

                return(new Org(data));
            });

            var fitnessFunction = new FitnessFunction <double[]>(org =>
            {
                net.InitializeWeights(org.Genome.Take(weightCount).ToArray());

                net.InitializeBiases(org.Genome.Skip(weightCount).ToArray());

                var errorSum = trainingData.Select(trainingElement =>
                {
                    var input          = trainingElement.Item1;
                    var expectedOutput = trainingElement.Item2;

                    var actualOutput = net.Compute(new NamedValue[] { new NamedValue()
                                                                      {
                                                                          Name = "x", Value = input
                                                                      } });

                    var error          = expectedOutput - actualOutput[0].Value;
                    var convertedError = Math.Pow((Math.Abs(error) + 1), 2);

                    return(convertedError);
                }).Sum();
                //Trace.WriteLine(errorSum);
                return(1 / errorSum);
            });

            var geneticSim = new GeneticSimulation <double[]>(fitnessFunction, weightOrgSpawner, 50);

            var bestSet = geneticSim.Evolve(20000, false);
            var theBest = geneticSim.GetBest();

            net.InitializeWeights(theBest.Genome.Take(weightCount).ToArray());
            net.InitializeBiases(theBest.Genome.Skip(weightCount).ToArray());

            Trace.WriteLine("OUTPUT");
            trainingData.ForEach(t =>
            {
                var input          = t.Item1;
                var expectedOutput = t.Item2;

                var actualOutput = net.Compute(new NamedValue[] { new NamedValue()
                                                                  {
                                                                      Name = "x", Value = input
                                                                  } });

                var error = expectedOutput - actualOutput[0].Value;

                //Trace.WriteLine($"INPUT:{input} EXPECT:{expectedOutput} REAL:{actualOutput[0].Value} ERR:{error}");
                //Trace.WriteLine($"{input},{expectedOutput},{actualOutput[0].Value}");
                Trace.WriteLine($"{actualOutput[0].Value}");
            });
        }