Changeset 12900
- Timestamp:
- 08/25/15 14:19:54 (9 years ago)
- Location:
- trunk/sources
- Files:
-
- 4 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeProblem.cs
r12897 r12900 22 22 #endregion 23 23 24 using System; 24 25 using System.Linq; 25 26 using HeuristicLab.Common; … … 50 51 } 51 52 52 public abstract double Evaluate(ISymbolicExpressionTree vector, IRandom random);53 public abstract double Evaluate(ISymbolicExpressionTree tree, IRandom random); 53 54 public sealed override double Evaluate(Individual individual, IRandom random) { 54 55 return Evaluate(individual.SymbolicExpressionTree(), random); 55 56 } 56 57 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 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]; 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.SymbolicExpressionTree().Clone(); 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); 66 69 } 67 70 } -
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/ArtificialAntProblem.cs
r12898 r12900 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
r12898 r12900 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.