Changeset 12901 for trunk/sources
- Timestamp:
- 08/25/15 14:22:22 (9 years ago)
- Location:
- trunk/sources
- Files:
-
- 3 edited
- 4 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeProblem.cs
r12900 r12901 22 22 #endregion 23 23 24 using System;25 24 using System.Linq; 26 25 using HeuristicLab.Common; … … 51 50 } 52 51 53 public abstract double Evaluate(ISymbolicExpressionTree tree, IRandom random);52 public abstract double Evaluate(ISymbolicExpressionTree vector, IRandom random); 54 53 public sealed override double Evaluate(Individual individual, IRandom random) { 55 54 return Evaluate(individual.SymbolicExpressionTree(), random); 56 55 } 57 56 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 62 62 if (!results.ContainsKey("Best Solution")) { 63 63 results.Add(new Result("Best Solution", typeof(ISymbolicExpressionTree))); 64 64 } 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(); 69 66 } 70 67 } -
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/ArtificialAntProblem.cs
r12900 r12901 22 22 using System; 23 23 using System.Diagnostics.Contracts; 24 using System.Drawing.Text;25 24 using System.Linq; 26 25 using HeuristicLab.Common; … … 28 27 using HeuristicLab.Data; 29 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 using HeuristicLab.Optimization;31 29 using HeuristicLab.Parameters; 32 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 33 31 using HeuristicLab.Problems.ArtificialAnt.Analyzers; 34 32 35 33 namespace HeuristicLab.Problems.ArtificialAnt { … … 116 114 g.AddTerminalSymbols(new string[] { "Left", "Right", "Move" }); 117 115 base.Encoding = new SymbolicExpressionTreeEncoding(g, 20, 10); 116 117 InitializeOperators(); 118 RegisterEventHandlers(); 118 119 } 119 120 … … 123 124 interpreter.Run(); 124 125 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 }137 126 } 138 127 … … 142 131 [StorableHook(HookType.AfterDeserialization)] 143 132 private void AfterDeserialization() { 133 RegisterEventHandlers(); 144 134 } 145 135 … … 147 137 private ArtificialAntProblem(ArtificialAntProblem original, Cloner cloner) 148 138 : base(original, cloner) { 139 RegisterEventHandlers(); 149 140 } 150 141 public override IDeepCloneable Clone(Cloner cloner) { … … 152 143 } 153 144 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 155 191 private bool[,] ToBoolMatrix(char[][] ch) { 156 192 var rows = ch.Length; -
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/HeuristicLab.Problems.ArtificialAnt-3.5.csproj
r12900 r12901 106 106 </ItemGroup> 107 107 <ItemGroup> 108 <Compile Include="Analyzers\BestAntTrailAnalyzer.cs" /> 108 109 <Compile Include="AntInterpreter.cs" /> 109 110 <Compile Include="ArtificialAntProblem.cs" />
Note: See TracChangeset
for help on using the changeset viewer.