Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/27/15 10:20:09 (9 years ago)
Author:
gkronber
Message:

#2472:

  • include Problems.GeneticProgramming in solution
  • use Problems.GeneticProgramming from Problems.GrammaticalEvolution (only ArtificialAnt part)
  • simplified GEArtificialAntProblem by using BasicProblem<IntegerVectorEncoding>
Location:
trunk/sources/HeuristicLab.Problems.GrammaticalEvolution/3.4
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.GrammaticalEvolution/3.4/ArtificialAnt/GEArtificialAntEvaluator.cs

    r12481 r12915  
    3131using HeuristicLab.Parameters;
    3232using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    33 using HeuristicLab.Problems.ArtificialAnt;
     33using HeuristicLab.Problems.GeneticProgramming.ArtificialAnt;
    3434using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
    3535
     
    123123      IntValue maxTimeSteps = MaxTimeStepsParameter.ActualValue;
    124124
    125       AntInterpreter interpreter = new AntInterpreter();
     125      Interpreter interpreter = new Interpreter();
    126126      interpreter.MaxTimeSteps = maxTimeSteps.Value;
    127127      interpreter.World = world;
  • trunk/sources/HeuristicLab.Problems.GrammaticalEvolution/3.4/ArtificialAnt/GEArtificialAntProblem.cs

    r12504 r12915  
    2222#endregion
    2323
    24 using System;
    25 using System.Collections.Generic;
     24using System.Diagnostics.Contracts;
    2625using System.Linq;
    2726using HeuristicLab.Common;
     
    2928using HeuristicLab.Data;
    3029using HeuristicLab.Encodings.IntegerVectorEncoding;
    31 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    3230using HeuristicLab.Optimization;
    3331using HeuristicLab.Parameters;
    3432using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    35 using HeuristicLab.PluginInfrastructure;
    36 using HeuristicLab.Problems.ArtificialAnt;
    37 using HeuristicLab.Problems.ArtificialAnt.Analyzers;
     33using HeuristicLab.Problems.GeneticProgramming.ArtificialAnt;
    3834using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
    3935
     
    4238  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 170)]
    4339  [StorableClass]
    44   public sealed class GEArtificialAntProblem : SingleObjectiveHeuristicOptimizationProblem<GEArtificialAntEvaluator, IIntegerVectorCreator>, IStorableContent {
     40  public sealed class GEArtificialAntProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding>, IStorableContent {
    4541    public string Filename { get; set; }
    4642
    47     #region constant for default world (Santa Fe)
    48     private readonly bool[,] santaFeAntTrail = new bool[,] {
    49       {false, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    50       {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    51       {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, false, false, false, false},
    52       {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, true, false, false},
    53       {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, true, false, false},
    54       {false, false, false, true, true, true, true, false, true, true, true, true, true, false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false, false, false, false},
    55       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false},
    56       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
    57       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
    58       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, true, false, false},
    59       {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
    60       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    61       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false},
    62       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
    63       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, true, true, true, false, false, false},
    64       {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, true, false, false, false, false, false, false, false, false},
    65       {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    66       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    67       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false},
    68       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false},
    69       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    70       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    71       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false},
    72       {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false},
    73       {false, false, false, true, true, false, false, true, true, true, true, true, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    74       {false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    75       {false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    76       {false, true, false, false, false, false, false, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    77       {false, true, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    78       {false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
    79       {false, false, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false },
    80       {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false }
    81     };
    82     #endregion
    83 
    8443    #region Parameter Properties
    85     public IValueParameter<ISymbolicExpressionGrammar> ArtificialAntExpressionGrammarParameter {
    86       get { return (IValueParameter<ISymbolicExpressionGrammar>)Parameters["ArtificialAntExpressionGrammar"]; }
    87     }
    88     public IValueParameter<IntValue> MaxExpressionLengthParameter {
    89       get { return (IValueParameter<IntValue>)Parameters["MaximumExpressionLength"]; }
    90     }
    9144    public IValueParameter<BoolMatrix> WorldParameter {
    9245      get { return (IValueParameter<BoolMatrix>)Parameters["World"]; }
    9346    }
    94     public IValueParameter<IntValue> MaxTimeStepsParameter {
    95       get { return (IValueParameter<IntValue>)Parameters["MaximumTimeSteps"]; }
    96     }
    97     public IValueParameter<IntMatrix> BoundsParameter {
    98       get { return (IValueParameter<IntMatrix>)Parameters["Bounds"]; }
     47    public IFixedValueParameter<IntValue> MaxTimeStepsParameter {
     48      get { return (IFixedValueParameter<IntValue>)Parameters["MaximumTimeSteps"]; }
    9949    }
    10050    public IValueParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
     
    10858      set { WorldParameter.Value = value; }
    10959    }
    110     public IntValue MaxTimeSteps {
    111       get { return MaxTimeStepsParameter.Value; }
    112       set { MaxTimeStepsParameter.Value = value; }
    113     }
    114     public IntValue MaxExpressionLength {
    115       get { return MaxExpressionLengthParameter.Value; }
    116       set { MaxExpressionLengthParameter.Value = value; }
    117     }
    118     public ArtificialAntExpressionGrammar ArtificialAntExpressionGrammar {
    119       get { return (ArtificialAntExpressionGrammar)ArtificialAntExpressionGrammarParameter.Value; }
    120     }
    121     public IEnumerable<IAntTrailAnalyzer> AntTrailAnalyzers {
    122       get { return Operators.OfType<IAntTrailAnalyzer>(); }
    123     }
    124     public IntMatrix Bounds {
    125       get { return BoundsParameter.Value; }
    126       set { BoundsParameter.Value = value; }
     60    public int MaxTimeSteps {
     61      get { return MaxTimeStepsParameter.Value.Value; }
     62      set { MaxTimeStepsParameter.Value.Value = value; }
    12763    }
    12864    #endregion
     
    13167    private GEArtificialAntProblem(bool deserializing) : base(deserializing) { }
    13268    [StorableHook(HookType.AfterDeserialization)]
    133     private void AfterDeserialization() {
    134       RegisterEventHandlers();
     69    private void AfterDeserialization() { }
     70
     71    public override bool Maximization {
     72      get { return true; }
    13573    }
     74
     75    [Storable]
     76    // parameters of the wrapped problem cannot be changed therefore it is not strictly necessary to clone and store it
     77    private readonly HeuristicLab.Problems.GeneticProgramming.ArtificialAnt.Problem wrappedAntProblem;
    13678
    13779    private GEArtificialAntProblem(GEArtificialAntProblem original, Cloner cloner)
    13880      : base(original, cloner) {
    139       RegisterEventHandlers();
     81      this.wrappedAntProblem = cloner.Clone(original.wrappedAntProblem);
    14082    }
    14183
     
    14587
    14688    public GEArtificialAntProblem()
    147       : base(new GEArtificialAntEvaluator(), new UniformRandomIntegerVectorCreator()) {
    148       BoolMatrix world = new BoolMatrix(santaFeAntTrail);
    149       Parameters.Add(new ValueParameter<IntValue>("MaximumExpressionLength", "Maximal length of the expression to control the artificial ant (genotype length).", new IntValue(30)));
    150       Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", new ArtificialAntExpressionGrammar()));
    151       Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
    152       Parameters.Add(new ValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
    153       IntMatrix m = new IntMatrix(new int[,] { { 0, 100 } });
    154       Parameters.Add(new ValueParameter<IntMatrix>("Bounds", "The integer number range in which the single genomes of a genotype are created.", m));
     89      : base() {
     90      wrappedAntProblem = new HeuristicLab.Problems.GeneticProgramming.ArtificialAnt.Problem();
     91      Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", wrappedAntProblem.World));
     92      Parameters.Add(new FixedValueParameter<IntValue>("MaximumTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
    15593      Parameters.Add(new ValueParameter<IGenotypeToPhenotypeMapper>("GenotypeToPhenotypeMapper", "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).", new DepthFirstMapper()));
    15694
    157       Maximization.Value = true;
    158       MaximizationParameter.Hidden = true;
    159       BestKnownQuality = new DoubleValue(89);
     95      Encoding = new IntegerVectorEncoding(30) { Bounds = new IntMatrix(new int[,] { { 0, 100 } }) };
    16096
    161       SolutionCreator.IntegerVectorParameter.ActualName = "AntTrailSolutionIntegerVector";
    162       Evaluator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolutionTree";
    163       Evaluator.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
    164       Evaluator.QualityParameter.ActualName = "FoodEaten";
    165 
    166       InitializeOperators();
    167       RegisterEventHandlers();
     97      BestKnownQuality = wrappedAntProblem.BestKnownQuality;
    16898    }
    16999
    170     #region Events
    171     protected override void OnSolutionCreatorChanged() {
    172       base.OnSolutionCreatorChanged();
     100    public override double Evaluate(Individual individual, IRandom random) {
     101      var vector = individual.IntegerVector();
    173102
    174       SolutionCreator.IntegerVectorParameter.ActualName = "AntTrailSolutionIntegerVector";
    175       SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
     103      var bounds = Encoding.Bounds;
     104      var len = Encoding.Length;
     105      var grammar = wrappedAntProblem.Encoding.Grammar;
     106      var mapper = GenotypeToPhenotypeMapperParameter.Value;
    176107
    177       ParameterizeAnalyzers();
    178       ParameterizeOperators();
     108      var tree = mapper.Map(random, bounds, len, grammar, vector);
     109
     110      Interpreter interpreter = new Interpreter(tree, World, MaxTimeSteps);
     111      interpreter.Run();
     112
     113      return interpreter.FoodEaten;
    179114    }
    180115
    181     protected override void OnEvaluatorChanged() {
    182       base.OnEvaluatorChanged();
     116    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
     117      var bounds = Encoding.Bounds;
     118      var len = Encoding.Length;
     119      var grammar = wrappedAntProblem.Encoding.Grammar;
     120      var mapper = GenotypeToPhenotypeMapperParameter.Value;
    183121
    184       Evaluator.SymbolicExpressionTreeParameter.ActualName = "AntTrailSolutionTree";
    185       Evaluator.SymbolicExpressionTreeGrammarParameter.ActualName = "ArtificialAntExpressionGrammar";
    186       Evaluator.QualityParameter.ActualName = "FoodEaten";
     122      var trees = individuals
     123        .Select(ind => mapper.Map(random, bounds, len, grammar, ind.IntegerVector()))
     124        .ToArray();
    187125
    188       Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
    189       Evaluator.SymbolicExpressionTreeGrammarParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged);
    190       Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    191 
    192       ParameterizeAnalyzers();
    193       ParameterizeOperators();
     126      wrappedAntProblem.Analyze(trees, qualities, results, random);
    194127    }
    195 
    196     private void SolutionCreator_IntegerVectorParameter_ActualNameChanged(object sender, EventArgs e) {
    197       ParameterizeAnalyzers();
    198       ParameterizeOperators();
    199     }
    200 
    201     private void Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
    202       ParameterizeAnalyzers();
    203       ParameterizeOperators();
    204     }
    205 
    206     private void Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged(object sender, EventArgs e) {
    207       ParameterizeAnalyzers();
    208       ParameterizeOperators();
    209     }
    210 
    211     private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
    212       ParameterizeAnalyzers();
    213       ParameterizeOperators();
    214     }
    215     #endregion
    216 
    217     #region Helpers
    218     private void RegisterEventHandlers() {
    219       SolutionCreator.IntegerVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_IntegerVectorParameter_ActualNameChanged);
    220       Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
    221       Evaluator.SymbolicExpressionTreeGrammarParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeGrammarParameter_ActualNameChanged);
    222       Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    223     }
    224 
    225     private void InitializeOperators() {
    226       Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>().OfType<IOperator>());
    227       Operators.Add(new BestAntTrailAnalyzer());
    228       Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
    229       Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
    230       ParameterizeAnalyzers();
    231       ParameterizeOperators();
    232     }
    233 
    234     private void ParameterizeAnalyzers() {
    235       foreach (IAntTrailAnalyzer analyzer in AntTrailAnalyzers) {
    236         analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
    237         analyzer.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
    238         analyzer.WorldParameter.ActualName = WorldParameter.Name;
    239         analyzer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
    240       }
    241       foreach (ISymbolicExpressionTreeAnalyzer analyzer in Operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
    242         analyzer.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
    243       }
    244     }
    245 
    246     private void ParameterizeOperators() {
    247       var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
    248 
    249       foreach (ISymbolicExpressionTreeGrammarBasedOperator op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
    250         op.SymbolicExpressionTreeGrammarParameter.ActualName = ArtificialAntExpressionGrammarParameter.Name;
    251       }
    252       foreach (GEArtificialAntEvaluator op in operators.OfType<GEArtificialAntEvaluator>()) {
    253         op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
    254         op.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
    255         op.WorldParameter.ActualName = WorldParameter.Name;
    256       }
    257       foreach (IIntegerVectorCrossover op in operators.OfType<IIntegerVectorCrossover>()) {
    258         op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
    259         op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
    260       }
    261       foreach (IIntegerVectorManipulator op in operators.OfType<IIntegerVectorManipulator>()) {
    262         op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
    263       }
    264       foreach (IIntegerVectorCreator op in operators.OfType<IIntegerVectorCreator>()) {
    265         op.BoundsParameter.ActualName = BoundsParameter.Name;
    266         op.LengthParameter.ActualName = MaxExpressionLengthParameter.Name;
    267       }
    268     }
    269     #endregion
    270128  }
    271129}
Note: See TracChangeset for help on using the changeset viewer.