示例#1
0
        public RuleSet Solve()
        {
            const int    totalData     = 2000;
            const double trainSet      = totalData * 0.8;
            const double testSet       = totalData * 0.2;
            var          matcher       = GetMatcher(@"Data\data3.txt", (int)(trainSet), 0);
            var          test          = GetMatcher(@"Data\data3.txt", (int)(testSet), (int)(trainSet));
            var          averageLogger = new AverageLogger();

            //Hidden nodes have $#input + 1 bias weights each
            //Second hidden have $#hidden + 1 bias weights each
            //Output nodes have $#sndHidden + 1 bias weights each

            //Thus, ($#hidden * ($#input + 1)) + ($#sndHidden * ($#hidden + 1)) + ($#output * ($#sndHidden + 1)
            const int numInput        = 6;
            const int numHidden       = 6;
            const int numSecondHidden = 3;
            const int numOutput       = 2;

            const int maximumIterations = 3;

            RuleSet ruleBest = null;


            for (var run = 0.0; run <= 2.0; run += 0.1)
            {
                Console.WriteLine("Running rul with var=" + run);

                var iterations = 0;
                var fitness    = 0;

                while (iterations++ < maximumIterations)
                {
                    //We make two populations for three, so we can run the neural network and ruleset side by side.
                    var rulePopulation = new Population <RuleSet, double>(100, 10 * ((6 * 2) + 1),
                                                                          (num, random) => new RuleIndividual(num, random), individual => new RuleSet(individual.Genotype),
                                                                          matcher.CountMatches);
                    var netPopulation = new Population <NeuralNetwork2, double>(100,
                                                                                (numHidden * (numInput + 1)) + (numSecondHidden * (numHidden + 1)) +
                                                                                (numOutput * (numSecondHidden + 1)),
                                                                                (length, rng) => new DoubleIndividual(length, rng),
                                                                                individual =>
                                                                                new NeuralNetwork2(individual.Genotype, numInput, numHidden, numSecondHidden, numOutput),
                                                                                matcher.CountMatches);

                    rulePopulation.MutationMultiplier = run;
                    netPopulation.MutationMultiplier  = run;

                    var loggerRule = new FitnessLogger();
                    var loggerNet  = new FitnessLogger();

                    int i = 0;
                    while (true)
                    {
                        const double threshold = 0.9;

                        var netGeneration  = netPopulation.Generation();
                        var ruleGeneration = rulePopulation.Generation();

                        var netBest = netGeneration.AsParallel().OrderBy(matcher.CountMatches).Last();
                        ruleBest = ruleGeneration.AsParallel().OrderBy(matcher.CountMatches).Last();

                        var netAverage =
                            netGeneration.AsParallel().Select(net => matcher.CountMatches(net, true)).Sum() /
                            netGeneration.Count;
                        var ruleAverage =
                            ruleGeneration.AsParallel().Select(rul => matcher.CountMatches(rul, true)).Sum() /
                            ruleGeneration.Count;
                        var netTestAverage =
                            netGeneration.AsParallel().Select(net => test.CountMatches(net, true)).Sum() /
                            netGeneration.Count;
                        var ruleTestAverage =
                            ruleGeneration.AsParallel().Select(rul => test.CountMatches(rul, true)).Sum() /
                            ruleGeneration.Count;

                        loggerNet.LogFitness(matcher.CountMatches(netBest, true), netAverage,
                                             test.CountMatches(netBest, true), netTestAverage);
                        loggerRule.LogFitness(matcher.CountMatches(ruleBest, true), ruleAverage,
                                              test.CountMatches(ruleBest, true), ruleTestAverage);

                        //We finish when the ruleset hits the threshhold percentage, though. The neural network isn't very good.
                        if ((int)(matcher.CountMatches(ruleBest, true)) >= (int)trainSet * threshold)
                        {
                            fitness += i;
                            break;
                        }

                        if (i > 3000)
                        {
                            fitness += i;
                            break;
                        }

                        i++;
                    }
                    loggerRule.Save("rul-mutation" + run + ".csv");
                }
                averageLogger.LogFitness(fitness / maximumIterations);
            }
            averageLogger.Save("three-mutation-runs.csv");
            return(ruleBest);
        }
示例#2
0
 public double CountMatches(RuleSet rules)
 {
     return(CountMatches(rules, false));
 }