Changeset 12903 for trunk/sources/HeuristicLab.Problems.ArtificialAnt
- Timestamp:
- 08/25/15 14:24:18 (9 years ago)
- 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 22 22 using System; 23 23 using System.Diagnostics.Contracts; 24 using System.Drawing.Text; 24 25 using System.Linq; 25 26 using HeuristicLab.Common; … … 27 28 using HeuristicLab.Data; 28 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 using HeuristicLab.Optimization; 29 31 using HeuristicLab.Parameters; 30 32 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Problems.ArtificialAnt.Analyzers; 33 32 34 33 35 namespace HeuristicLab.Problems.ArtificialAnt { … … 114 116 g.AddTerminalSymbols(new string[] { "Left", "Right", "Move" }); 115 117 base.Encoding = new SymbolicExpressionTreeEncoding(g, 20, 10); 116 117 InitializeOperators();118 RegisterEventHandlers();119 118 } 120 119 … … 126 125 } 127 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 } 137 } 138 128 139 // persistence 129 140 [StorableConstructor] … … 131 142 [StorableHook(HookType.AfterDeserialization)] 132 143 private void AfterDeserialization() { 133 RegisterEventHandlers();134 144 } 135 145 … … 137 147 private ArtificialAntProblem(ArtificialAntProblem original, Cloner cloner) 138 148 : base(original, cloner) { 139 RegisterEventHandlers();140 149 } 141 150 public override IDeepCloneable Clone(Cloner cloner) { … … 143 152 } 144 153 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 191 155 private bool[,] ToBoolMatrix(char[][] ch) { 192 156 var rows = ch.Length; -
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/HeuristicLab.Problems.ArtificialAnt-3.5.csproj
r12901 r12903 106 106 </ItemGroup> 107 107 <ItemGroup> 108 <Compile Include="Analyzers\BestAntTrailAnalyzer.cs" />109 108 <Compile Include="AntInterpreter.cs" /> 110 109 <Compile Include="ArtificialAntProblem.cs" />
Note: See TracChangeset
for help on using the changeset viewer.