Free cookie consent management tool by TermsFeed Policy Generator

Changeset 12903


Ignore:
Timestamp:
08/25/15 14:24:18 (9 years ago)
Author:
gkronber
Message:

#2421 moved code from analyzer directly into the Analyze(...) method of the problem class

Location:
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/ArtificialAntProblem.cs

    r12901 r12903  
    2222using System;
    2323using System.Diagnostics.Contracts;
     24using System.Drawing.Text;
    2425using System.Linq;
    2526using HeuristicLab.Common;
     
    2728using HeuristicLab.Data;
    2829using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     30using HeuristicLab.Optimization;
    2931using HeuristicLab.Parameters;
    3032using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    31 using HeuristicLab.Problems.ArtificialAnt.Analyzers;
     33
    3234
    3335namespace HeuristicLab.Problems.ArtificialAnt {
     
    114116      g.AddTerminalSymbols(new string[] { "Left", "Right", "Move" });
    115117      base.Encoding = new SymbolicExpressionTreeEncoding(g, 20, 10);
    116 
    117       InitializeOperators();
    118       RegisterEventHandlers();
    119118    }
    120119
     
    126125    }
    127126
     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      }
     137    }
     138
    128139    // persistence
    129140    [StorableConstructor]
     
    131142    [StorableHook(HookType.AfterDeserialization)]
    132143    private void AfterDeserialization() {
    133       RegisterEventHandlers();
    134144    }
    135145
     
    137147    private ArtificialAntProblem(ArtificialAntProblem original, Cloner cloner)
    138148      : base(original, cloner) {
    139       RegisterEventHandlers();
    140149    }
    141150    public override IDeepCloneable Clone(Cloner cloner) {
     
    143152    }
    144153
    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 
     154    #region helpers
    191155    private bool[,] ToBoolMatrix(char[][] ch) {
    192156      var rows = ch.Length;
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/HeuristicLab.Problems.ArtificialAnt-3.5.csproj

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