Changeset 12901


Ignore:
Timestamp:
08/25/15 14:22:22 (6 years ago)
Author:
gkronber
Message:

#2422 reverse merge of r12900 (unintended commit)

Location:
trunk/sources
Files:
3 edited
4 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeProblem.cs

    r12900 r12901  
    2222#endregion
    2323
    24 using System;
    2524using System.Linq;
    2625using HeuristicLab.Common;
     
    5150    }
    5251
    53     public abstract double Evaluate(ISymbolicExpressionTree tree, IRandom random);
     52    public abstract double Evaluate(ISymbolicExpressionTree vector, IRandom random);
    5453    public sealed override double Evaluate(Individual individual, IRandom random) {
    5554      return Evaluate(individual.SymbolicExpressionTree(), random);
    5655    }
    5756
    58     public virtual void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results, IRandom random) {
    59       var bestQuality = Maximization ? qualities.Max() : qualities.Min();
    60       var bestIdx = Array.IndexOf(qualities, bestQuality);
    61       var best = trees[bestIdx];
     57    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
     58      base.Analyze(individuals, qualities, results, random);
     59      var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
     60      var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
     61
    6262      if (!results.ContainsKey("Best Solution")) {
    6363        results.Add(new Result("Best Solution", typeof(ISymbolicExpressionTree)));
    6464      }
    65       results["Best Solution"].Value = (IItem)best.Clone();
    66     }
    67     public sealed override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
    68       Analyze(individuals.Select(ind => ind.SymbolicExpressionTree()).ToArray(), qualities, results, random);
     65      results["Best Solution"].Value = (IItem)best.SymbolicExpressionTree().Clone();
    6966    }
    7067  }
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/ArtificialAntProblem.cs

    r12900 r12901  
    2222using System;
    2323using System.Diagnostics.Contracts;
    24 using System.Drawing.Text;
    2524using System.Linq;
    2625using HeuristicLab.Common;
     
    2827using HeuristicLab.Data;
    2928using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    30 using HeuristicLab.Optimization;
    3129using HeuristicLab.Parameters;
    3230using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    33 
     31using HeuristicLab.Problems.ArtificialAnt.Analyzers;
    3432
    3533namespace HeuristicLab.Problems.ArtificialAnt {
     
    116114      g.AddTerminalSymbols(new string[] { "Left", "Right", "Move" });
    117115      base.Encoding = new SymbolicExpressionTreeEncoding(g, 20, 10);
     116
     117      InitializeOperators();
     118      RegisterEventHandlers();
    118119    }
    119120
     
    123124      interpreter.Run();
    124125      return interpreter.FoodEaten;
    125     }
    126 
    127     public override void Analyze(ISymbolicExpressionTree[] trees, double[] qualities, ResultCollection results, IRandom random) {
    128       const string bestSolutionResultName = "Best Solution";
    129       var bestQuality = Maximization ? qualities.Max() : qualities.Min();
    130       var bestIdx = Array.IndexOf(qualities, bestQuality);
    131 
    132       if (!results.ContainsKey(bestSolutionResultName)) {
    133         results.Add(new Result(bestSolutionResultName, new AntTrail(World, trees[bestIdx], MaxTimeSteps.Value, qualities[bestIdx])));
    134       } else if (((AntTrail)(results[bestSolutionResultName].Value)).Quality < qualities[bestIdx]) {
    135         results[bestSolutionResultName].Value = new AntTrail(World, trees[bestIdx], MaxTimeSteps.Value, qualities[bestIdx]);
    136       }
    137126    }
    138127
     
    142131    [StorableHook(HookType.AfterDeserialization)]
    143132    private void AfterDeserialization() {
     133      RegisterEventHandlers();
    144134    }
    145135
     
    147137    private ArtificialAntProblem(ArtificialAntProblem original, Cloner cloner)
    148138      : base(original, cloner) {
     139      RegisterEventHandlers();
    149140    }
    150141    public override IDeepCloneable Clone(Cloner cloner) {
     
    152143    }
    153144
    154     #region helpers
     145    #region Events
     146    protected override void OnSolutionCreatorChanged() {
     147      base.OnSolutionCreatorChanged();
     148      ParameterizeAnalyzers();
     149      ParameterizeOperators();
     150    }
     151    protected override void OnEvaluatorChanged() {
     152      base.OnEvaluatorChanged();
     153      ParameterizeAnalyzers();
     154      ParameterizeOperators();
     155    }
     156    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
     157      ParameterizeAnalyzers();
     158      ParameterizeOperators();
     159    }
     160    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
     161      ParameterizeAnalyzers();
     162      ParameterizeOperators();
     163    }
     164    #endregion
     165
     166    #region Helpers
     167    private void RegisterEventHandlers() {
     168      Encoding.SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
     169      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
     170    }
     171
     172    private void InitializeOperators() {
     173      Operators.Add(new BestAntTrailAnalyzer());
     174      ParameterizeAnalyzers();
     175      ParameterizeOperators();
     176    }
     177
     178    private void ParameterizeAnalyzers() {
     179      foreach (var analyzer in Operators.OfType<BestAntTrailAnalyzer>()) {
     180        analyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
     181        analyzer.SymbolicExpressionTreeParameter.ActualName = Encoding.SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
     182        analyzer.WorldParameter.ActualName = WorldParameter.Name;
     183        analyzer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
     184      }
     185    }
     186
     187    private void ParameterizeOperators() {
     188      // no problem-specific operators to parameterize
     189    }
     190
    155191    private bool[,] ToBoolMatrix(char[][] ch) {
    156192      var rows = ch.Length;
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/HeuristicLab.Problems.ArtificialAnt-3.5.csproj

    r12900 r12901  
    106106  </ItemGroup>
    107107  <ItemGroup>
     108    <Compile Include="Analyzers\BestAntTrailAnalyzer.cs" />
    108109    <Compile Include="AntInterpreter.cs" />
    109110    <Compile Include="ArtificialAntProblem.cs" />
Note: See TracChangeset for help on using the changeset viewer.