Ejemplo n.º 1
0
 private static bool IsSuccess(BulldozerAlgorithm<double> algorithm, double threshold, out Chromosome chromosome)
 {
     var chromosomes = algorithm.ChromosomePool
                                .Union(algorithm.ChromosomeBank)
                                .OfType<TreeChromosome>()
                                .ToList();
     var maxValue = chromosomes.Max(c => c.Metrics[2]);
     chromosome = chromosomes.FirstOrDefault(c => c.Metrics[2] == maxValue);
     return maxValue >= threshold;
 }
Ejemplo n.º 2
0
        private static RunInfo RunOnce(BulldozerAlgorithm<double> algorithm, Properties properties, string evolutionLogName)
        {
            var success = false;
            Chromosome chromosome = null;
            var simplificationRules = AlgebraicRules.Get()
                                                    .Where(z => z.Tags.Contains(StdTags.SafeResection))
                                                    .ToList();
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            for (var i = 0; i < properties.IterationCount; ++i)
            {
                algorithm.MakeIteration();

                if (OnlineSimplification(algorithm, properties))
                {
                    if (Balancer.TraceEvolution) WriteEvolutionLog(algorithm, evolutionLogName);
                    algorithm.SimplifyPool(simplificationRules);
                }

                if (Balancer.ShowTrace) Trace(algorithm);
                if (Balancer.TraceEvolution) WriteEvolutionLog(algorithm, evolutionLogName);

                success = IsSuccess(algorithm, properties.Threshold, out chromosome);
                if(success)
                {
                    break;
                }
            }
            stopwatch.Stop();

            var treeChromosome = (TreeChromosome)chromosome;
            return new RunInfo
                       {
                           Success = success,
                           ElapsedSeconds = stopwatch.Elapsed.TotalSeconds,
                           ElapsedIterations = algorithm.CurrentIteration,
                           ID = chromosome != null ? treeChromosome.ID : -1,
                           ResultRepresentation = chromosome != null
                                                      ? treeChromosome.Tree.ToPrefixForm()
                                                      : null
                       };
        }
Ejemplo n.º 3
0
 private static bool OnlineSimplification(BulldozerAlgorithm<double> algorithm, Properties properties)
 {
     return properties.OnlineSimplification
         && algorithm.CurrentIteration % properties.OnlineSimplificationRate == 0;
 }
Ejemplo n.º 4
0
 private static void WriteEvolutionLog(BulldozerAlgorithm<double> algorithm, string logName)
 {
     using (var file = File.Open(logName, FileMode.Append))
     {
         var text = new StreamWriter(file) {AutoFlush = true};
         text.WriteLine("[{0}]", algorithm.CurrentIteration);
         var tags = algorithm.Pool.Select(GetTag);
         tags.ForEach(text.WriteLine);
     }
 }
Ejemplo n.º 5
0
 private static void Trace(BulldozerAlgorithm<double> algorithm)
 {
     if(algorithm.CurrentIteration % 100 != 0) return;
     Console.Clear();
     Console.SetCursorPosition(0,0);
     Console.WriteLine("===============================");
     algorithm.ChromosomePool.Union(algorithm.ChromosomeBank)
         .OfType<TreeChromosome>()
         .OrderByDescending(c => c.Value)
         .Take(5)
         .Select(c => Tuple.Create(c.Tree.ToPrefixForm(), c.Value))
         .ForEach(p => Console.WriteLine("{0}   {1}", p.Item1, p.Item2));
     Console.WriteLine("===============================");
     Console.WriteLine();
 }
        public static BulldozerAlgorithm<double> AssembleAlgorithm(TaskDataSet dataSet, Dictionary<string, double> weights, Dictionary<string, double> merics, bool onlineSimplification)
        {
            _dataSet = dataSet;

            _alg = new BulldozerAlgorithm<double>();

            AlgebraicRules.RandomMin = -10;
            AlgebraicRules.RandomMax = 10;

            var mutationSet = new List<RuleSet>
                                  {
                                      new RuleSet
                                          {
                                              Name = StdTags.SafeResection,
                                              Weight = weights[StdTags.SafeResection],
                                              Rules = AlgebraicRules.Get()
                                                                    .Where(r => r.Tags.Contains(StdTags.SafeResection))
                                                                    .ToList()
                                          },
                                      new RuleSet
                                          {
                                              Name = StdTags.UnsafeResection,
                                              Weight = weights[StdTags.UnsafeResection],
                                              Rules = new List<Rule>(BasicRules.MutationRules())
                                          },
                                      new RuleSet
                                          {
                                              Name = StdTags.SafeBlowing,
                                              Weight = weights[StdTags.SafeBlowing],
                                              Rules = AlgebraicRules.Get()
                                                                    .Where(r => r.Tags.Contains(StdTags.SafeBlowing))
                                                                    .ToList()
                                          },
                                      new RuleSet
                                          {
                                              Name = StdTags.UnsafeBlowing,
                                              Weight = weights[StdTags.UnsafeBlowing],
                                              Rules = AlgebraicRules.Get()
                                                                    .Where(r => r.Tags.Contains(StdTags.UnsafeBlowing))
                                                                    .Union(BasicRules.VariableRules(_dataSet.Args.First().Length))
                                                                    .ToList()
                                          },
                                      new RuleSet
                                          {
                                              Name = StdTags.Tunning,
                                              Weight = weights[StdTags.Tunning],
                                              Rules = AlgebraicRules.Get()
                                                                    .Where(r => r.Tags.Contains(StdTags.Tunning))
                                                                    .ToList()
                                          }
                                  };

            var inductiveSet = mutationSet.Where(s => s.Name != StdTags.SafeResection
                                                   && s.Name != StdTags.UnsafeResection)
                                          .ToList();

            _alg.MutationRules.AddRange(!onlineSimplification ? mutationSet : inductiveSet);
            _alg.CrossoverRules.Add(new RuleSet
                                        {
                                            Name = "Crossing",
                                            Rules = BasicRules.CrossingRules().ToList()
                                        });
            _alg.Metrics.Add(new Metric(tc => GetReciprocalError(tc, _dataSet.Learn), "Eval", 1));
            _alg.Metrics.Add(new Metric(tc => -tc.Tree.GetOperationCount(), "Length", merics["Length"]));
            _alg.Metrics.Add(new Metric(tc => GetReciprocalError(tc, _dataSet.Control), "CheatEval", 0));

            if (Environment.OSVersion.Platform != PlatformID.Unix)
                _alg.IterationCallBack += Print;

            _alg.PerformBanking = () => _alg.Bank.AddRange(_alg.Pool.Where(z => z.Age > 20 && z.Metrics[0] < EvalThreshold));
            _alg.PerformSelection = GASolutions.PerformSelection(ExceptCondition, 30);
            return _alg;
        }