Changeset 9419
- Timestamp:
- 05/02/13 13:18:57 (12 years ago)
- Location:
- branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4
- Files:
-
- 4 added
- 1 deleted
- 13 edited
- 3 copied
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreeFrequentPatternsAnalyzer.cs
r9238 r9419 204 204 var grammar = trees[0].Root.Grammar; 205 205 206 // bring trees to a canonical form to eliminate permuted fragments207 var canonicalSorter = new SymbolicExpressionTreeCanonicalSorter();208 foreach (var t in trees)209 canonicalSorter.SortSubtrees(t);210 211 206 var graph = Results["SymbolGraph"].Value as SymbolGraph; 212 207 … … 217 212 // subtree-sort fragments to bring them to a canonical form 218 213 // var canonicalSorter = new SymbolicExpressionTreeCanonicalSorter(); 219 foreach (var fragment in fragments)220 canonicalSorter.SortSubtrees(fragment.Root);221 214 222 215 if (!Results.ContainsKey(CommonFragmentsParameterName)) { … … 342 335 int minCount = grammar.GetMinimumSubtreeCount(node.Symbol); 343 336 var arity = random.Next(enforceGrammarArity ? minCount : 1, maxCount + 1); 344 var possibleChildConnections = new List< Arc>();337 var possibleChildConnections = new List<SymbolArc>(); 345 338 346 339 if (depthLimit <= 2 || lengthLimit <= maxCount) { // if we are near the depth limit then we want to add leafs on the next level -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreePopulationDiversityAnalyzer.cs
r9296 r9419 44 44 public sealed class SymbolicExpressionTreePopulationDiversityAnalyzer : SingleSuccessorOperator, IAnalyzer { 45 45 #region Parameter names 46 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 46 47 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; 47 48 private const string UpdateIntervalParameterName = "UpdateInterval"; … … 56 57 private const string SortSubtreesParameterName = "SortSubtrees"; 57 58 private const string SimilarityValuesParmeterName = "Similarity"; 58 // population graph59 private const string PopulationGraphParameterName = "PopulationGraph";60 59 // clone map 61 60 private const string GlobalCloneMapParameterName = "GlobalCloneMap"; … … 64 63 65 64 #region Parameters 65 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter { 66 get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; } 67 } 66 68 67 69 public ValueParameter<IntValue> UpdateIntervalParameter { … … 116 118 117 119 #region Parameter properties 120 public IntValue MaximumSymbolicExpressionTreeDepth { 121 get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } 122 } 118 123 119 124 public IntValue UpdateInterval { … … 164 169 Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored.")); 165 170 Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far.")); 166 Parameters.Add(new ValueParameter<BoolValue>(MatchVariablesParameterName, "Specify if the symbolic expression tree comparer should match variable names." ));167 Parameters.Add(new ValueParameter<BoolValue>(MatchVariableWeightsParameterName, "Specify if the symbolic expression tree comparer should match variable weights." ));168 Parameters.Add(new ValueParameter<BoolValue>(MatchConstantValuesParameterName, "Specify if the symbolic expression tree comparer should match constant values." ));171 Parameters.Add(new ValueParameter<BoolValue>(MatchVariablesParameterName, "Specify if the symbolic expression tree comparer should match variable names.", new BoolValue(true))); 172 Parameters.Add(new ValueParameter<BoolValue>(MatchVariableWeightsParameterName, "Specify if the symbolic expression tree comparer should match variable weights.", new BoolValue(true))); 173 Parameters.Add(new ValueParameter<BoolValue>(MatchConstantValuesParameterName, "Specify if the symbolic expression tree comparer should match constant values.", new BoolValue(true))); 169 174 Parameters.Add(new ValueParameter<BoolValue>(SortSubtreesParameterName, "Specifies whether the subtrees of a tree should be sorted before comparison.")); 170 175 Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection).")); 171 176 Parameters.Add(new ValueParameter<BoolValue>(StoreHistoryParameterName, "True if the tree lengths history of the population should be stored.", new BoolValue(false))); 172 177 Parameters.Add(new LookupParameter<DoubleValue>(SimilarityValuesParmeterName, "")); 178 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 173 179 UpdateCounterParameter.Hidden = true; 174 180 UpdateIntervalParameter.Hidden = true; … … 177 183 [StorableHook(HookType.AfterDeserialization)] 178 184 private void AfterDeserialization() { 185 if (!Parameters.ContainsKey(MaximumSymbolicExpressionTreeDepthParameterName)) { 186 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 187 } 179 188 } 180 189 181 190 #region IStatefulItem members 182 191 public override void InitializeState() { 192 UpdateCounter.Value = 0; 183 193 base.InitializeState(); 184 UpdateCounter.Value = 0;185 }186 187 public override void ClearState() {188 base.ClearState();189 UpdateCounter.Value = 0;190 194 } 191 195 #endregion … … 199 203 UpdateCounter.Value++; 200 204 if (UpdateCounter.Value != UpdateInterval.Value) return base.Apply(); 201 202 205 UpdateCounter.Value = 0; 203 206 var trees = SymbolicExpressionTreeParameter.ActualValue.ToList(); … … 208 211 209 212 SimilarityParameter.ActualValue = new DoubleValue(); 213 // needed only with the old (and inefficient) geneticitem-based similarity measure 214 // var geneticItems = trees.ToDictionary(tree => tree, tree => tree.GetGeneticItems(1, MaximumSymbolicExpressionTreeDepth.Value - 2).ToArray()); 210 215 211 216 var comp = new SymbolicExpressionTreeNodeSimilarityComparer { … … 217 222 var operations = new OperationCollection { Parallel = true }; 218 223 foreach (var tree in trees) { 219 var op = new SymbolicDataAnalysisExpressionTreeSimilarityCalculator { CurrentSymbolicExpressionTree = tree, SimilarityComparer = comp };224 var op = new SymbolicDataAnalysisExpressionTreeSimilarityCalculator { CurrentSymbolicExpressionTree = tree, SimilarityComparer = comp, MaximumTreeDepth = MaximumSymbolicExpressionTreeDepth.Value }; 220 225 var operation = ExecutionContext.CreateChildOperation(op, ExecutionContext.Scope); 221 226 operations.Add(operation); … … 256 261 relativeSelectionCountsTable.Rows["SelectedIndividuals"].Values.Add(relativeSelectionCount); 257 262 258 // build a selection frequencies histogram259 var graph = (SymbolicExpressionTreeGenealogyGraph)results[PopulationGraphParameterName].Value;260 // get graph nodes corresponding to individuals of the previous generation261 var prevGen = graph.Nodes.Where(node => node.Rank.IsAlmost(Generations.Value - 1)).OrderByDescending(n => n.Quality).ToList();262 var frequencies = new double[SymbolicExpressionTreeParameter.ActualValue.Length];263 for (int i = 0; i != prevGen.Count; ++i) {264 int selFreq = GlobalCloneMap.Values.Count(t => t == prevGen[i].SymbolicExpressionTree);265 frequencies[i] = selFreq;266 }267 DataTable selectionFrequenciesTable;268 if (!results.ContainsKey("SelectionFrequencies")) {269 selectionFrequenciesTable = new DataTable("Selection Frequencies") { VisualProperties = { YAxisTitle = "Selection Count" } };270 results.Add(new Result("SelectionFrequencies", selectionFrequenciesTable));271 }272 selectionFrequenciesTable = (DataTable)results["SelectionFrequencies"].Value;273 DataRow histoRow;274 if (!selectionFrequenciesTable.Rows.ContainsKey("SelectionFrequencies")) {275 histoRow = new DataRow("SelectionFrequencies") { VisualProperties = { StartIndexZero = true } };276 selectionFrequenciesTable.Rows.Add(histoRow);277 }278 histoRow = selectionFrequenciesTable.Rows["SelectionFrequencies"];279 histoRow.Values.Replace(frequencies);280 281 bool storeHistory = StoreHistory.Value;282 if (storeHistory) {283 DataTableHistory selectionFrequenciesHistory;284 if (!results.ContainsKey("SelectionFrequenciesHistory")) {285 selectionFrequenciesHistory = new DataTableHistory();286 results.Add(new Result("SelectionFrequenciesHistory", selectionFrequenciesHistory));287 }288 selectionFrequenciesHistory = (DataTableHistory)results["SelectionFrequenciesHistory"].Value;289 selectionFrequenciesHistory.Add((DataTable)selectionFrequenciesTable.Clone());290 }291 263 return base.Apply(); 292 264 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreeShapeAnalyzer.cs
r9082 r9419 20 20 #endregion 21 21 22 using System.Collections.Generic;23 22 using System.Linq; 24 23 using HeuristicLab.Common; … … 30 29 using HeuristicLab.Parameters; 31 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 using HeuristicLab.Problems.DataAnalysis;33 using HeuristicLab.Problems.DataAnalysis.Symbolic;34 // type definitions for convenience35 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;36 using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>;37 using TraceMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItemList<HeuristicLab.Core.IItem>>;38 31 39 32 namespace HeuristicLab.EvolutionaryTracking { … … 44 37 [Item("SymbolicExpressionTreeFragmentsAnalyzer", "An operator that provides statistics about crossover fragments")] 45 38 [StorableClass] 46 public abstract class SymbolicExpressionTreeFragmentsAnalyzer : SingleSuccessorOperator, IAnalyzer {39 public sealed class SymbolicExpressionTreeShapeAnalyzer : SingleSuccessorOperator, IAnalyzer { 47 40 #region Parameter names 48 protected const string UpdateIntervalParameterName = "UpdateInterval"; 49 protected const string UpdateCounterParameterName = "UpdateCounter"; 50 protected const string ResultsParameterName = "Results"; 51 protected const string SecondaryTraceMapParameterName = "SecondaryTraceMap"; 52 protected const string SecondaryFragmentMapParameterName = "SecondaryFragmentMap"; 53 protected const string SecondaryCloneMapParameterName = "SecondaryCloneMap"; 54 protected const string GlobalTraceMapParameterName = "GlobalTraceMap"; 55 protected const string GlobalTraceMapParameterDescription = "Global map of trees and their parents."; 56 protected const string GlobalCloneMapParameterName = "GlobalCloneMap"; 57 protected const string GlobalCloneMapParameterDescription = "Global map of trees and their clones"; 58 protected const string GlobalFragmentMapParameterName = "GlobalFragmentMap"; 59 protected const string GlobalFragmentMapParameterDescription = "Global map of trees and their respective fragments."; 60 protected const string GenerationsParameterName = "Generations"; 61 protected const string SymbolicExpressionInterpreterParameterName = "SymbolicExpressionTreeInterpreter"; 62 protected const string SymbolicRegressionProblemDataParameterName = "ProblemData"; 41 private const string UpdateIntervalParameterName = "UpdateInterval"; 42 private const string UpdateCounterParameterName = "UpdateCounter"; 43 private const string ResultsParameterName = "Results"; 44 private const string GenerationsParameterName = "Generations"; 63 45 #endregion 64 65 // impact values calculator66 private readonly SymbolicRegressionSolutionImpactValuesCalculator calculator = new SymbolicRegressionSolutionImpactValuesCalculator();67 46 68 47 #region Parameter properties … … 78 57 public LookupParameter<IntValue> GenerationsParameter { 79 58 get { return (LookupParameter<IntValue>)Parameters[GenerationsParameterName]; } 80 }81 // tracking structures82 public LookupParameter<TraceMapType> GlobalTraceMapParameter {83 get { return (LookupParameter<TraceMapType>)Parameters[GlobalTraceMapParameterName]; }84 }85 public LookupParameter<CloneMapType> GlobalCloneMapParameter {86 get { return (LookupParameter<CloneMapType>)Parameters[GlobalCloneMapParameterName]; }87 }88 public LookupParameter<CloneMapType> GlobalFragmentMapParameter {89 get { return (LookupParameter<CloneMapType>)Parameters[GlobalFragmentMapParameterName]; }90 }91 // auxiliary structures for tracking fragments and individuals from the previous generation92 // (this is needed because tracking is done in connection to the current generation, i.e., for93 // counting "successful" offspring and fragments94 public LookupParameter<TraceMapType> SecondaryTraceMapParameter {95 get { return (LookupParameter<TraceMapType>)Parameters[SecondaryTraceMapParameterName]; }96 }97 public LookupParameter<CloneMapType> SecondaryFragmentMapParameter {98 get { return (LookupParameter<CloneMapType>)Parameters[SecondaryFragmentMapParameterName]; }99 }100 public LookupParameter<CloneMapType> SecondaryCloneMapParameter {101 get { return (LookupParameter<CloneMapType>)Parameters[SecondaryCloneMapParameterName]; }102 }103 // problem data, interpreter and evaluator104 public LookupParameter<SymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionInterpreterParameter {105 get { return (LookupParameter<SymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionInterpreterParameterName]; }106 }107 public LookupParameter<RegressionProblemData> SymbolicRegressionProblemDataParameter {108 get { return (LookupParameter<RegressionProblemData>)Parameters[SymbolicRegressionProblemDataParameterName]; }109 59 } 110 60 #endregion … … 123 73 get { return ResultsParameter.ActualValue; } 124 74 } 125 public CloneMapType GlobalCloneMap {126 get { return GlobalCloneMapParameter.ActualValue; }127 }128 public TraceMapType GlobalTraceMap {129 get { return GlobalTraceMapParameter.ActualValue; }130 }131 public TraceMapType SecondaryTraceMap {132 get { return SecondaryTraceMapParameter.ActualValue; }133 set { SecondaryTraceMapParameter.ActualValue = value; }134 }135 public CloneMapType SecondaryFragmentMap {136 get { return SecondaryFragmentMapParameter.ActualValue; }137 set { SecondaryFragmentMapParameter.ActualValue = value; }138 }139 public CloneMapType SecondaryCloneMap {140 get { return SecondaryCloneMapParameter.ActualValue; }141 set { SecondaryCloneMapParameter.ActualValue = value; }142 }143 public CloneMapType GlobalFragmentMap {144 get { return GlobalFragmentMapParameter.ActualValue; }145 }146 75 public IntValue Generations { 147 76 get { return GenerationsParameter.ActualValue; } 148 }149 public SymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionInterpreter {150 get { return SymbolicExpressionInterpreterParameter.ActualValue; }151 }152 public RegressionProblemData SymbolicRegressionProblemData {153 get { return SymbolicRegressionProblemDataParameter.ActualValue; }154 77 } 155 78 #endregion 156 79 157 80 [StorableConstructor] 158 pr otected SymbolicExpressionTreeFragmentsAnalyzer(bool deserializing) : base(deserializing) { }81 private SymbolicExpressionTreeShapeAnalyzer(bool deserializing) : base(deserializing) { } 159 82 160 pr otected SymbolicExpressionTreeFragmentsAnalyzer(SymbolicExpressionTreeFragmentsAnalyzer original, Cloner cloner) : base(original, cloner) { }83 private SymbolicExpressionTreeShapeAnalyzer(SymbolicExpressionTreeShapeAnalyzer original, Cloner cloner) : base(original, cloner) { } 161 84 162 pr otected SymbolicExpressionTreeFragmentsAnalyzer()85 private SymbolicExpressionTreeShapeAnalyzer() 163 86 : base() { 164 87 #region Add parameters … … 166 89 Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1))); 167 90 Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0))); 168 // tracking169 Parameters.Add(new LookupParameter<TraceMapType>(GlobalTraceMapParameterName, GlobalTraceMapParameterDescription));170 Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, GlobalCloneMapParameterDescription));171 Parameters.Add(new LookupParameter<CloneMapType>(GlobalFragmentMapParameterName, GlobalFragmentMapParameterDescription));172 // secondary tracking173 Parameters.Add(new LookupParameter<TraceMapType>(SecondaryTraceMapParameterName, GlobalTraceMapParameterDescription));174 Parameters.Add(new LookupParameter<CloneMapType>(SecondaryCloneMapParameterName, GlobalCloneMapParameterDescription));175 Parameters.Add(new LookupParameter<CloneMapType>(SecondaryFragmentMapParameterName, GlobalFragmentMapParameterDescription));176 // impact calculation177 Parameters.Add(new LookupParameter<SymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionInterpreterParameterName, "Interpreter for symbolic expression trees"));178 Parameters.Add(new LookupParameter<RegressionProblemData>(SymbolicRegressionProblemDataParameterName, "The symbolic data analysis problem."));179 // fragment statistics parameters180 91 Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored.")); 181 92 Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far.")); … … 211 122 #endregion 212 123 213 /* A small description of the way this analyzer works 214 * 215 * We keep 3 maps in the global scope: the GlobalTraceMap, the GlobalFragmentMap and the GlobalCloneMap that are updated on each step: selection, crossover, mutation 216 * These maps provide information about the children, parents and transferred fragments as follows: 217 * 218 * 1) GlobalCloneMap 219 * This data structure provides the basis for tracking. In the selection step, every selected individual gets cloned and transferred into the recombination pool. 220 * The GlobalCloneMap keeps track of clones by mapping them to the original individuals. This is an essential step for building the genealogy graph. Using this 221 * structure we can find out how many times each individual was selected. 222 * The GlobalCloneMap consists of Key-Value pairs Clone->Original 223 * 224 * 2) GlobalTraceMap 225 * Keeps information in the form of key-value pairs Child->{Parents}, where {Parents} is a set consisting of 226 * - one parent individual (in the case of mutation), by language abuse we say that the individual that mutation acted on is the parent, 227 * and the resulting individual is the child 228 * - two parent individuals (in the case of crossover), named Parent0 and Parent1. Parent0 is the individual that crossover acts on by replacing 229 * one of its subtrees with a compatible subtree, randomly selected from Parent1 230 * 231 * CROSSOVER 232 * Parent0 Parent1 {Parents} 233 * \ / ^ 234 * \ fragment (inserted from Parent1 into Parent0 to create the Child) ^ [Mapping provided by the GlobalTraceMap] 235 * \ / ^ 236 * \ / ^ 237 * Child Child 238 * 239 * MUTATION 240 * Parent0 241 * | 242 * | [mutation acts on a random node/subtree in the 'parent' individual 243 * | 244 * Child 245 * 246 * Since crossover and mutation act on individuals in the recombination pool (which in fact are clones of individuals from the previous generation), the GlobalTraceMap 247 * is populated with values given by the mappings in the GlobalCloneMap, so that the original parent for each child is known. 248 * 249 * 3) GlobalFragmentMap 250 * Keeps information in the form of Key-Value pairs about an individual and its respective fragment 251 * (ie., the subtree received via crossover or created/altered by mutation) 252 * */ 124 public override IOperation Apply() { 253 125 254 public override IOperation Apply() {255 // save the current generation so it can be compared to the following one, next time the analyzer is applied256 SecondaryTraceMap = SecondaryTraceMap ?? new TraceMapType();257 SecondaryCloneMap = SecondaryCloneMap ?? new CloneMapType();258 SecondaryFragmentMap = SecondaryFragmentMap ?? new CloneMapType();259 260 SecondaryTraceMap.Clear();261 if (GlobalTraceMap != null)262 foreach (var m in GlobalTraceMap)263 SecondaryTraceMap.Add(m.Key, m.Value);264 265 SecondaryCloneMap.Clear();266 if (GlobalCloneMap != null)267 foreach (var m in GlobalCloneMap)268 SecondaryCloneMap.Add(m.Key, m.Value);269 270 SecondaryFragmentMap.Clear();271 if (GlobalFragmentMap != null)272 foreach (var m in GlobalFragmentMap)273 SecondaryFragmentMap.Add(m.Key, m.Value);274 126 return base.Apply(); 275 }276 277 protected virtual void InitializeParameters() {278 #region Init parameters279 280 #endregion281 127 } 282 128 … … 285 131 } 286 132 133 private static int PathLength(ISymbolicExpressionTree tree) { 134 return PathLength(tree.Root); 135 } 136 287 137 private static int VisitationLength(ISymbolicExpressionTreeNode node) { 288 138 return node.IterateNodesBreadth().Sum(n => n.GetLength()); 289 139 } 290 140 291 #region Similarity computations 292 /// <summary> 293 /// Provide a measure of how similar the fragments that get passed by crossover from one generation to the next are. 294 /// </summary> 295 /// <param name="fragments">The symbolic expression tree fragments</param> 296 /// <param name="mode">The similarity mode (0 - exact, 1 - high, 2 - relaxed)</param> 297 /// <returns>The average number of similar fragments</returns> 298 private static double CalculateSimilarity(IList<IFragment> fragments, SymbolicExpressionTreeNodeSimilarityComparer comparer) { 299 var visited = new bool[fragments.Count]; 300 int groups = 0; 301 for (int i = 0; i != fragments.Count - 1; ++i) { 302 if (visited[i]) continue; 303 for (int j = i + 1; j != fragments.Count; ++j) { 304 if (visited[j]) continue; 305 if (fragments[i].Root.IsSimilarTo(fragments[j].Root, comparer)) 306 visited[j] = true; 307 } 308 ++groups; 309 } 310 return (double)fragments.Count / groups; 141 private static int PathLength(ISymbolicExpressionTreeNode node) { 142 return node.IterateNodesBreadth().Sum(n => node.GetBranchLevel(n)) + 1; 311 143 } 312 #endregion 144 145 public override IDeepCloneable Clone(Cloner cloner) { 146 return new SymbolicExpressionTreeShapeAnalyzer(this, cloner); 147 } 313 148 } //SymbolicExpressionTreeFragmentsAnalyzer 314 149 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/FPGraph.cs
r9082 r9419 28 28 [Item("Symbol graph", "A graph used to store the relationship between symbols in a population of individuals")] 29 29 [StorableClass] 30 public class FPGraph : DirectedGraph<SymbolNode> {30 public class FPGraph : GenericGraph<SymbolNode> { 31 31 [Storable] 32 32 public Dictionary<int, List<SymbolNode>> Levels { get { return levels; } } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/GenericGraph/Arc.cs
r9082 r9419 1 1 using HeuristicLab.Common; 2 using HeuristicLab.Core; 3 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 4 2 5 namespace HeuristicLab.EvolutionaryTracking { 3 6 /// <summary> 4 7 /// An arc that can have a weight, a label, and can old additional information in the Data object 5 8 /// </summary> 6 public class Arc { 7 public IVertex Source { get; set; } 8 public IVertex Target { get; set; } 9 public string Label { get; set; } 10 public double Weight { get; set; } 11 public object Data { get; set; } 9 [StorableClass] 10 public class Arc : Item, IEdge 11 { 12 [Storable] private IVertex source; 13 public IVertex Source { get { return source; } set { source = value; } } 14 [Storable] private IVertex target; 15 public IVertex Target { get { return target; } set { target = value; } } 16 [Storable] private string label; 17 public string Label { get { return label; } set { label = value; } } 18 [Storable] private double weight; 19 public double Weight { get { return weight; } set { weight = value; } } 20 [Storable] private object data; 21 public object Data { get { return data; } set { data = value; } } 22 23 [StorableConstructor] 24 public Arc(bool deserializing) : base(deserializing) {} 25 26 public Arc() {} 27 28 private Arc(Arc original, Cloner cloner) : base(original, cloner) 29 { 30 Source = original.Source; 31 Target = original.Target; 32 Label = original.Label; 33 Weight = original.Weight; 34 Data = original.Data; 35 } 36 37 public override IDeepCloneable Clone(Cloner cloner) 38 { 39 return new Arc(this, cloner); 40 } 12 41 } 13 42 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/GenericGraph/GenericGraph.cs
r9236 r9419 31 31 [Item("Directed graph", "Generic directed graph base class.")] 32 32 [StorableClass] 33 public class DirectedGraph<T> : Item, IDirectedGraph<T> where T : class, IVertex {33 public class GenericGraph<T> : Item, IGenericGraph<T> where T : class, IVertex { 34 34 [Storable] 35 35 private readonly List<T> nodes; // graph will consist of a set of nodes of type T … … 38 38 } 39 39 40 public DirectedGraph() {40 public GenericGraph() { 41 41 nodes = new List<T>(); 42 42 } 43 43 44 public DirectedGraph(IDirectedGraph<T> g) {44 public GenericGraph(IGenericGraph<T> g) { 45 45 nodes = new List<T>(g.Nodes); 46 46 } 47 47 48 48 public override IDeepCloneable Clone(Cloner cloner) { 49 return new DirectedGraph<T>(this, cloner);49 return new GenericGraph<T>(this, cloner); 50 50 } 51 51 … … 55 55 56 56 [StorableConstructor] 57 protected DirectedGraph(bool serializing)57 protected GenericGraph(bool serializing) 58 58 : base(serializing) { 59 59 } 60 60 61 protected DirectedGraph(DirectedGraph<T> original, Cloner cloner)61 protected GenericGraph(GenericGraph<T> original, Cloner cloner) 62 62 : base(original, cloner) { 63 63 nodes = new List<T>(original.Nodes); // bburlacu: maybe the list should be empty -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/GenericGraph/Vertex.cs
r9247 r9419 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Common; 25 using HeuristicLab.Core; 24 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 27 26 28 namespace HeuristicLab.EvolutionaryTracking { 27 29 [StorableClass] 28 public class Vertex : IVertex { 29 public string Id { get; private set; } 30 public string Label { get; set; } 31 public double Weight { get; set; } 32 public List<Arc> InEdges { get; private set; } 33 public List<Arc> OutEdges { get; private set; } 34 private object content; 30 public class Vertex : Item, IVertex 31 { 32 [Storable] 33 private string id; 34 public string Id { get { return id; } } 35 [Storable] private string label; 36 public string Label { get { return label; } set { label = value; } } 37 [Storable] private double weight; 38 public double Weight { get { return weight; } set { weight = value; } } 39 [Storable] private List<IEdge> inEdges; 40 public List<IEdge> InEdges { get { return inEdges; } private set { inEdges = value; } } 41 [Storable] private List<IEdge> outEdges; 42 public List<IEdge> OutEdges { get { return outEdges; }private set { outEdges = value; }} 43 [Storable] private object content; 35 44 public object Content { 36 45 get { return content; } … … 43 52 44 53 [StorableConstructor] 45 public Vertex(bool deserializing) { }54 public Vertex(bool deserializing) : base(deserializing) { } 46 55 47 56 public Vertex() { 48 Id = Guid.NewGuid().ToString(); 57 id = Guid.NewGuid().ToString(); 58 } 59 60 [StorableHook(HookType.AfterDeserialization)] 61 private void AfterDeserialization() 62 { 63 if (id == null) { 64 id = Guid.NewGuid().ToString(); 65 } 66 } 67 68 protected Vertex(Vertex original, Cloner cloner) 69 : base(original, cloner) { 70 Content = original.Content; 71 id = Guid.NewGuid().ToString(); 72 Label = original.Label; 73 Content = original.Content; 74 InEdges = new List<IEdge>(original.InEdges); 75 OutEdges = new List<IEdge>(original.OutEdges); 76 } 77 78 public override IDeepCloneable Clone(Cloner cloner) { 79 return new Vertex(this, cloner); 49 80 } 50 81 51 82 public Vertex(Vertex node) { 52 Id = Guid.NewGuid().ToString();83 id = Guid.NewGuid().ToString(); 53 84 Label = node.Label; 54 85 Content = node.Content; 55 InEdges = new List< Arc>(node.InEdges);56 OutEdges = new List< Arc>(node.OutEdges);86 InEdges = new List<IEdge>(node.InEdges); 87 OutEdges = new List<IEdge>(node.OutEdges); 57 88 } 58 89 … … 108 139 public void AddForwardArc(IVertex target, double weight = 0.0, object data = null) { 109 140 var e = new Arc { Source = this, Target = target, Data = data, Weight = weight }; 110 if (OutEdges == null) OutEdges = new List< Arc> { e };141 if (OutEdges == null) OutEdges = new List<IEdge> { e }; 111 142 else OutEdges.Add(e); 112 143 } 113 public void AddForwardArc( Arcarc) {144 public void AddForwardArc(IEdge arc) { 114 145 if (arc.Source == null) { arc.Source = this; } 115 146 if (arc.Source != this) { throw new Exception("AddForwardArc: Source should be equal to this."); } 116 if (OutEdges == null) { OutEdges = new List< Arc> { arc }; } else { OutEdges.Add(arc); }147 if (OutEdges == null) { OutEdges = new List<IEdge> { arc }; } else { OutEdges.Add(arc); } 117 148 } 118 public void AddReverseArc( Arcarc) {149 public void AddReverseArc(IEdge arc) { 119 150 if (arc.Target == null) { arc.Target = this; } 120 151 if (arc.Target != this) { throw new Exception("AddReverseArc: Target should be equal to this."); }; 121 if (InEdges == null) { InEdges = new List< Arc> { arc }; } else { InEdges.Add(arc); }152 if (InEdges == null) { InEdges = new List<IEdge> { arc }; } else { InEdges.Add(arc); } 122 153 } 123 154 public void AddReverseArc(IVertex source, double weight = 0.0, object data = null) { 124 155 var e = new Arc { Source = source, Target = this, Data = data, Weight = weight }; 125 if (InEdges == null) InEdges = new List< Arc> { e };156 if (InEdges == null) InEdges = new List<IEdge> { e }; 126 157 else InEdges.Add(e); 127 158 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/HeuristicLab.EvolutionaryTracking-3.4.csproj
r9296 r9419 18 18 <DebugType>full</DebugType> 19 19 <Optimize>false</Optimize> 20 <OutputPath>..\..\..\..\ Trunk\sources\bin\</OutputPath>20 <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath> 21 21 <DefineConstants>DEBUG;TRACE</DefineConstants> 22 22 <ErrorReport>prompt</ErrorReport> … … 39 39 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> 40 40 <DebugSymbols>true</DebugSymbols> 41 <OutputPath> bin\x64\Debug\</OutputPath>41 <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath> 42 42 <DefineConstants>DEBUG;TRACE</DefineConstants> 43 43 <DebugType>full</DebugType> … … 56 56 </PropertyGroup> 57 57 <ItemGroup> 58 <Reference Include="HeuristicLab.Analysis-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 58 <Reference Include="HeuristicLab.Analysis-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 59 <Private>False</Private> 60 </Reference> 59 61 <Reference Include="HeuristicLab.Collections-3.3"> 60 62 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Collections-3.3.dll</HintPath> 63 <Private>False</Private> 61 64 </Reference> 62 65 <Reference Include="HeuristicLab.Common-3.3"> 63 66 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath> 64 </Reference> 65 <Reference Include="HeuristicLab.Common.Resources-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 67 <Private>False</Private> 68 </Reference> 69 <Reference Include="HeuristicLab.Common.Resources-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 70 <Private>False</Private> 71 </Reference> 66 72 <Reference Include="HeuristicLab.Core-3.3"> 67 73 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath> 68 </Reference> 69 <Reference Include="HeuristicLab.Core.Views-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 74 <Private>False</Private> 75 </Reference> 76 <Reference Include="HeuristicLab.Core.Views-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 77 <Private>False</Private> 78 </Reference> 70 79 <Reference Include="HeuristicLab.Data-3.3"> 71 80 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Data-3.3.dll</HintPath> 72 </Reference> 73 <Reference Include="HeuristicLab.MainForm-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 74 <Reference Include="HeuristicLab.MainForm.WindowsForms-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 81 <Private>False</Private> 82 </Reference> 83 <Reference Include="HeuristicLab.MainForm-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 84 <Private>False</Private> 85 </Reference> 86 <Reference Include="HeuristicLab.MainForm.WindowsForms-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 87 <Private>False</Private> 88 </Reference> 75 89 <Reference Include="HeuristicLab.Operators-3.3"> 76 90 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Operators-3.3.dll</HintPath> 91 <Private>False</Private> 77 92 </Reference> 78 93 <Reference Include="HeuristicLab.Optimization-3.3"> 79 94 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Optimization-3.3.dll</HintPath> 95 <Private>False</Private> 80 96 </Reference> 81 97 <Reference Include="HeuristicLab.Parameters-3.3"> 82 98 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Parameters-3.3.dll</HintPath> 99 <Private>False</Private> 83 100 </Reference> 84 101 <Reference Include="HeuristicLab.Persistence-3.3"> 85 102 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Persistence-3.3.dll</HintPath> 86 </Reference> 87 <Reference Include="HeuristicLab.PluginInfrastructure-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 88 <Reference Include="HeuristicLab.Problems.DataAnalysis-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 89 <Reference Include="HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 90 <Reference Include="HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 91 <Reference Include="HeuristicLab.Problems.DataAnalysis.Symbolic.Views-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 103 <Private>False</Private> 104 </Reference> 105 <Reference Include="HeuristicLab.PluginInfrastructure-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 106 <Private>False</Private> 107 </Reference> 108 <Reference Include="HeuristicLab.Problems.DataAnalysis-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 109 <Private>False</Private> 110 </Reference> 111 <Reference Include="HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 112 <Private>False</Private> 113 </Reference> 114 <Reference Include="HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 115 <Private>False</Private> 116 </Reference> 117 <Reference Include="HeuristicLab.Problems.DataAnalysis.Symbolic.Views-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 118 <Private>False</Private> 119 </Reference> 92 120 <Reference Include="HeuristicLab.Random-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 93 121 <SpecificVersion>False</SpecificVersion> 94 122 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath> 123 <Private>False</Private> 95 124 </Reference> 96 125 <Reference Include="System" /> … … 105 134 </ItemGroup> 106 135 <ItemGroup> 136 <Compile Include="Analyzers\BuildingBlocks\RelevantBuildingBlocksAnalyzer.cs" /> 137 <Compile Include="Analyzers\BuildingBlocks\Poly10BuildingBlocksAnalyzer.cs" /> 107 138 <Compile Include="Analyzers\SymbolicExpressionTreeShapeAnalyzer.cs" /> 108 139 <Compile Include="Analyzers\SymbolicExpressionTreeEvolvabilityAnalyzer.cs" /> … … 113 144 <Compile Include="Analyzers\Fragments\SymbolicExpressionTreeFragmentsAnalyzer.cs" /> 114 145 <Compile Include="FPGraph.cs" /> 146 <Compile Include="Interfaces\IEdge.cs" /> 115 147 <Compile Include="LineageExplorer.cs" /> 116 148 <Compile Include="Operators\LineageAggregator.cs" /> … … 119 151 <Compile Include="Analyzers\SymbolicExpressionTreePopulationDiversityAnalyzer.cs" /> 120 152 <Compile Include="Analyzers\SymbolicExpressionTreeRelativeLengthAnalyzer.cs" /> 121 <Compile Include=" DirectedGraph\DirectedGraph.cs" />122 <Compile Include=" DirectedGraph\Arc.cs" />123 <Compile Include=" DirectedGraph\Vertex.cs" />124 <Compile Include="Interfaces\I DirectedGraph.cs" />153 <Compile Include="GenericGraph\GenericGraph.cs" /> 154 <Compile Include="GenericGraph\Arc.cs" /> 155 <Compile Include="GenericGraph\Vertex.cs" /> 156 <Compile Include="Interfaces\IGenericGraph.cs" /> 125 157 <Compile Include="Interfaces\IVertex.cs" /> 126 158 <Compile Include="Interfaces\ISymbolicExpressionTreeGenealogyGraph.cs" /> … … 132 164 <Compile Include="SymbolGraph.cs" /> 133 165 <Compile Include="SymbolicExpressionTreeGenealogyGraph.cs" /> 166 <Compile Include="TreeDistance\TreeDistanceCalculator.cs" /> 134 167 </ItemGroup> 135 168 <ItemGroup /> … … 138 171 <Project>{06D4A186-9319-48A0-BADE-A2058D462EEA}</Project> 139 172 <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4</Name> 173 <Private>False</Private> 140 174 </ProjectReference> 141 175 <ProjectReference Include="..\..\HeuristicLab.Problems.DataAnalysis.Symbolic\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj"> 142 176 <Project>{3d28463f-ec96-4d82-afee-38be91a0ca00}</Project> 143 177 <Name>HeuristicLab.Problems.DataAnalysis.Symbolic-3.4</Name> 178 <Private>False</Private> 144 179 </ProjectReference> 145 180 <ProjectReference Include="..\..\HeuristicLab.Selection\3.3\HeuristicLab.Selection-3.3.csproj"> 146 181 <Project>{2C36CD4F-E5F5-43A4-801A-201EA895FE17}</Project> 147 182 <Name>HeuristicLab.Selection-3.3</Name> 183 <Private>False</Private> 148 184 </ProjectReference> 149 185 </ItemGroup> -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Interfaces/IGenericGraph.cs
r9236 r9419 25 25 26 26 namespace HeuristicLab.EvolutionaryTracking { 27 public interface I DirectedGraph<T> : IItem where T : class, IVertex {27 public interface IGenericGraph<T> : IItem where T : class, IVertex { 28 28 bool Contains(T t); // graph contains specific node? 29 29 bool Any(Func<T, bool> predicate); // graph contains any nodes matching the given predicate? -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Interfaces/ISymbolicExpressionTreeGenealogyGraph.cs
r9082 r9419 24 24 25 25 namespace HeuristicLab.EvolutionaryTracking { 26 public interface ISymbolicExpressionTreeGenealogyGraph : I DirectedGraph<SymbolicExpressionGenealogyGraphNode> {26 public interface ISymbolicExpressionTreeGenealogyGraph : IGenericGraph<SymbolicExpressionGenealogyGraphNode> { 27 27 List<SymbolicExpressionGenealogyGraphNode> GetGraphNodes(ISymbolicExpressionTree tree); 28 28 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Interfaces/IVertex.cs
r9082 r9419 25 25 public interface IVertex { 26 26 string Id { get; } 27 List<Arc> InEdges { get; } 28 List<Arc> OutEdges { get; } 27 List<IEdge> InEdges { get; } 28 List<IEdge> OutEdges { get; } 29 30 29 31 int InDegree { get; } 30 32 int OutDegree { get; } … … 38 40 39 41 void AddForwardArc(IVertex target, double weight = 0.0, object content = null); 40 void AddForwardArc( Arcarc);42 void AddForwardArc(IEdge arc); 41 43 void AddReverseArc(IVertex target, double weight = 0.0, object content = null); 42 void AddReverseArc( Arcarc);44 void AddReverseArc(IEdge arc); 43 45 } 44 46 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/LineageExplorer.cs
r9238 r9419 9 9 [StorableClass] 10 10 public class LineageExplorer : Item { 11 public ISymbolicExpressionTreeGenealogyGraph GenealogyGraph { get; set; } 12 public IList<Tuple<ISymbolicExpressionTree, double>> Trees { get; set; } 13 public int Generations { get; set; } 11 [Storable] 12 private ISymbolicExpressionTreeGenealogyGraph genealogyGraph; 13 public ISymbolicExpressionTreeGenealogyGraph GenealogyGraph { get { return genealogyGraph; } set { genealogyGraph = value; } } 14 [Storable] 15 private IList<Tuple<ISymbolicExpressionTree, double>> trees; 16 public IList<Tuple<ISymbolicExpressionTree, double>> Trees { get { return trees; } set { trees = value; } } 17 [Storable] 18 private int generations; 19 public int Generations { get { return generations; } set { generations = value; } } 14 20 15 21 private LineageExplorer(bool deserializing) : base(deserializing) { } 16 private LineageExplorer(LineageExplorer original, Cloner cloner) : base(original, cloner) { } 22 private LineageExplorer(LineageExplorer original, Cloner cloner) 23 : base(original, cloner) { 24 genealogyGraph = original.genealogyGraph; 25 trees = original.trees; 26 generations = original.generations; 27 } 17 28 public LineageExplorer() { } 18 29 public override IDeepCloneable Clone(Cloner cloner) { return new LineageExplorer(this, cloner); } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Operators/SymbolicExpressionTreeFPBuilder.cs
r9082 r9419 32 32 33 33 private SymbolicRegressionSolutionImpactValuesCalculator calculator; 34 private Dictionary<SymbolNode, double> averageNodeImpacts;34 // private Dictionary<SymbolNode, double> averageNodeImpacts; 35 35 36 36 #region Parameter properties -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Operators/SymbolicExpressionTreeGenealogyGraphBuilder.cs
r9247 r9419 201 201 var qualities = SymbolicExpressionTreeQualityParameter.ActualValue.ToArray(); 202 202 203 if (trees.Length != qualities.Length) throw new Exception("Error: trees and qualities array sizes do not match!"); 203 if (trees.Length != qualities.Length) 204 throw new Exception("Error: trees and qualities array sizes do not match!"); 204 205 205 206 // add all individuals to the evolutionary graph 206 207 int generation = Generations.Value; 207 208 208 var pairs = trees.Zip(qualities, (t, q) => new { Tree = t, Quality = q.Value }).OrderByDescending(x => x.Quality).ToList(); 209 var pairs = 210 trees.Zip(qualities, (t, q) => new { Tree = t, Quality = q.Value }).OrderByDescending(x => x.Quality).ToList(); 209 211 210 212 if (generation == 0) { … … 214 216 var quality = pair.Quality; 215 217 graph.AddNode(new SymbolicExpressionGenealogyGraphNode { 216 SymbolicExpressionTree = tree, Quality = quality, Rank = generation 218 SymbolicExpressionTree = tree, 219 Quality = quality, 220 Rank = generation 217 221 }); 218 222 } … … 231 235 // the following query addds the intermediate nodes that were created when 232 236 // crossover was followed by mutation (they are not present in the scope) 233 (from t in trees 234 where GlobalTraceMap.ContainsKey(t) 235 let parents = GlobalTraceMap[t] 236 where parents.Count == 1 // 1 parent means mutation 237 let p = (ISymbolicExpressionTree)parents[0] 238 select new SymbolicExpressionGenealogyGraphNode { 239 SymbolicExpressionTree = p, 240 Quality = Evaluate(p), 241 Rank = generation - 0.5 // an intermediate parent that would normale be 'invisible' to the evolutionary process 242 } 243 ).ToList(); 237 (from t in trees 238 where GlobalTraceMap.ContainsKey(t) 239 let parents = GlobalTraceMap[t] 240 where parents.Count == 1 241 // 1 parent means mutation 242 let p = (ISymbolicExpressionTree)parents[0] 243 select new SymbolicExpressionGenealogyGraphNode { 244 SymbolicExpressionTree = p, 245 Quality = Evaluate(p), 246 Rank = generation - 0.5 247 // an intermediate parent that would normale be 'invisible' to the evolutionary process 248 } 249 ).ToList(); 244 250 245 251 foreach (var node in graphNodes) { … … 262 268 continue; 263 269 } 264 var parents = GlobalTraceMap[tree].Cast<ISymbolicExpressionTree>(); 270 var parents = GlobalTraceMap[tree].Cast<ISymbolicExpressionTree>().ToList(); 271 var fragment = GlobalFragmentMap[tree]; 265 272 foreach (var p in parents) { 266 273 var nodes = graph.GetGraphNodes(p); … … 272 279 throw new Exception("Source node cannot be null"); 273 280 } 274 var arc = new Arc { Source = sourceNode, Target = node, Data = GlobalFragmentMap[tree]};281 var arc = new Arc { Source = sourceNode, Target = node, Data = fragment }; 275 282 sourceNode.AddForwardArc(arc); 276 283 node.AddReverseArc(arc); 277 284 } 285 // the root parent is the one that receives the subtree/fragment from the other parent 286 // so it does not actually pass a subtree to the child, therefore the fragment is null 287 // so in case of crossover, we add a null fragment below 288 if (parents.Count > 1) node.InEdges[0].Data = new Fragment(null); 278 289 } 290 291 // notify listeners that the genealogy graph was updated 292 graph.Updated(); 293 279 294 return base.Apply(); 280 295 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Operators/Util.cs
r9082 r9419 28 28 } 29 29 30 public static void WriteDot(this I DirectedGraph<SymbolNode> graph, string path) {30 public static void WriteDot(this IGenericGraph<SymbolNode> graph, string path) { 31 31 const double minNorm = 1.0, maxNorm = 3.0; 32 32 … … 37 37 var max = arcs.Max(x => x.Weight); 38 38 39 var normalizedWeights = arcs.Select(arc => new KeyValuePair< Arc, double>(arc, (arc.Weight - min) * (maxNorm - minNorm) / (max - min) + minNorm))39 var normalizedWeights = arcs.Select(arc => new KeyValuePair<SymbolArc, double>(arc, (arc.Weight - min) * (maxNorm - minNorm) / (max - min) + minNorm)) 40 40 .ToDictionary(x => x.Key, x => x.Value); 41 41 … … 75 75 } 76 76 } 77 public static void WriteGml(this I DirectedGraph<SymbolNode> graph, string path) {77 public static void WriteGml(this IGenericGraph<SymbolNode> graph, string path) { 78 78 var idMap = new Dictionary<string, int>(); 79 79 const double minNorm = 1.0, maxNorm = 3.0; … … 86 86 var max = arcs.Max(x => x.Weight); 87 87 88 var normalizedWeights = arcs.Select(arc => new KeyValuePair< Arc, double>(arc, (arc.Weight - min) * (maxNorm - minNorm) / (max - min) + minNorm))88 var normalizedWeights = arcs.Select(arc => new KeyValuePair<SymbolArc, double>(arc, (arc.Weight - min) * (maxNorm - minNorm) / (max - min) + minNorm)) 89 89 .ToDictionary(x => x.Key, x => x.Value); 90 90 -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/SymbolGraph.cs
r9082 r9419 21 21 22 22 using System.Collections.Generic; 23 using System.Linq; 23 24 using HeuristicLab.Core; 24 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 28 29 [Item("Symbol graph", "A graph used to store the relationship between symbols in a population of individuals")] 29 30 [StorableClass] 30 public class SymbolGraph : DirectedGraph<SymbolNode> {31 public class SymbolGraph : GenericGraph<SymbolNode> { 31 32 [Storable] 32 33 private Dictionary<string, SymbolNode> nodes = new Dictionary<string, SymbolNode>(); … … 58 59 public Dictionary<int, int> Positions { get; set; } 59 60 public double AverageArity { get; set; } 61 62 public new List<SymbolArc> InEdges { 63 get { return base.InEdges == null ? null : base.InEdges.Cast<SymbolArc>().ToList(); } 64 } 65 66 public new List<SymbolArc> OutEdges { 67 get { return base.OutEdges == null ? null : base.OutEdges.Cast<SymbolArc>().ToList(); } 68 } 69 } 70 71 public class SymbolArc : IEdge { 72 public IVertex Source { get; set; } 73 public IVertex Target { get; set; } 74 public double Weight { get; set; } 60 75 } 61 76 } 62 -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/SymbolicExpressionTreeGenealogyGraph.cs
r9247 r9419 31 31 [Item("SymbolicExpressionTreeGenealogyGraph", "A genealogy graph for symbolic expression tree individuals")] 32 32 [StorableClass] 33 public sealed class SymbolicExpressionTreeGenealogyGraph : DirectedGraph<SymbolicExpressionGenealogyGraphNode>, ISymbolicExpressionTreeGenealogyGraph, IDisposable { 33 public sealed class SymbolicExpressionTreeGenealogyGraph : GenericGraph<SymbolicExpressionGenealogyGraphNode>, 34 ISymbolicExpressionTreeGenealogyGraph, IDisposable { 34 35 [Storable] 35 private readonly Dictionary<ISymbolicExpressionTree, List<SymbolicExpressionGenealogyGraphNode>> nodeMap = new Dictionary<ISymbolicExpressionTree, List<SymbolicExpressionGenealogyGraphNode>>(); 36 private readonly Dictionary<ISymbolicExpressionTree, List<SymbolicExpressionGenealogyGraphNode>> nodeMap 37 = new Dictionary<ISymbolicExpressionTree, List<SymbolicExpressionGenealogyGraphNode>>(); 36 38 37 public SymbolicExpressionTreeGenealogyGraph() { } 39 public SymbolicExpressionTreeGenealogyGraph() { 40 } 41 42 public void Updated() { 43 var updated = GraphUpdated; 44 if (updated != null) updated(this, null); 45 } 38 46 39 47 [StorableConstructor] 40 public SymbolicExpressionTreeGenealogyGraph(bool serializing) : base(serializing) { } 48 public SymbolicExpressionTreeGenealogyGraph(bool serializing) 49 : base(serializing) { 50 } 41 51 42 public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicExpressionTreeGenealogyGraph(this, cloner); } 52 public override IDeepCloneable Clone(Cloner cloner) { 53 return new SymbolicExpressionTreeGenealogyGraph(this, cloner); 54 } 43 55 44 private SymbolicExpressionTreeGenealogyGraph(SymbolicExpressionTreeGenealogyGraph original, Cloner cloner) : base(original, cloner) { } 56 private SymbolicExpressionTreeGenealogyGraph(SymbolicExpressionTreeGenealogyGraph original, Cloner cloner) 57 : base(original, cloner) { 58 nodeMap = new Dictionary<ISymbolicExpressionTree, List<SymbolicExpressionGenealogyGraphNode>>(original.nodeMap); 59 } 45 60 46 61 public override void AddNode(SymbolicExpressionGenealogyGraphNode node) { … … 50 65 else 51 66 nodeMap[tree] = new List<SymbolicExpressionGenealogyGraphNode> { node }; 52 base.AddNode(node); // the genealogy graph should have as many nodes as individuals in the population multiplied by the number of generations 67 base.AddNode(node); 68 // the genealogy graph should have as many nodes as individuals in the population multiplied by the number of generations 69 // plus a number of intermediate individuals ~= mutation probability * population size 53 70 } 54 71 … … 70 87 } 71 88 72 // #region Fragment tracing 73 // public IEnumerable<ISymbolicExpressionTree> TraceFragment(ISymbolicExpressionTreeNode fragment, SymbolicExpressionTreeNodeSimilarityComparer comparer) { 74 // return Nodes.Select(x => x.SymbolicExpressionTree).Where(tree => tree.ContainsFragment(fragment, comparer)); 75 // } 76 // #endregion 89 public SymbolicExpressionGenealogyGraphNode MostRecentCommonAncestor() { 90 double currentRank = Nodes.Last().Rank; 91 var currentGen = Nodes.Where(n => n.Rank.IsAlmost(currentRank)).ToList(); 92 var lineages = currentGen.Select(n => n.RootLineage().ToArray()).ToArray(); 93 for (int i = 0; i < (int)currentRank; ++i) { 94 if (lineages.All(lineage => lineages[0][i].SymbolicExpressionTree == lineage[i].SymbolicExpressionTree)) 95 return lineages[0][i]; 96 } 97 return null; 98 } 77 99 78 100 public void Dispose() { … … 80 102 nodeMap.Clear(); 81 103 // call GC.SupressFinalize? 104 } 105 106 public event EventHandler GraphUpdated; 107 private void OnGraphUpdated(object sender, EventArgs args) { 108 var updated = GraphUpdated; 109 if (updated != null) updated(sender, args); 82 110 } 83 111 } … … 89 117 set { Content = value; } 90 118 } 91 public double Quality { get; set; } 92 public bool IsElite { get; set; } 93 public double Rank { get; set; } 119 120 [Storable] 121 private double quality; 122 public double Quality { get { return quality; } set { quality = value; } } 123 [Storable] 124 private bool isElite; 125 public bool IsElite { get { return isElite; } set { isElite = value; } } 126 [Storable] 127 private double rank; 128 public double Rank { get { return rank; } set { rank = value; } } 129 130 [StorableHook(HookType.AfterDeserialization)] 131 private void AfterDeserialization() { 132 if (Id == null) throw new Exception(); 133 } 94 134 95 135 [StorableConstructor] 96 public SymbolicExpressionGenealogyGraphNode(bool deserializing) : base(deserializing) { } 136 public SymbolicExpressionGenealogyGraphNode(bool deserializing) 137 : base(deserializing) { 138 } 97 139 98 public SymbolicExpressionGenealogyGraphNode() { } 140 public SymbolicExpressionGenealogyGraphNode() { 141 } 99 142 100 public SymbolicExpressionGenealogyGraphNode(object content) { 101 Content = content; 143 private SymbolicExpressionGenealogyGraphNode(SymbolicExpressionGenealogyGraphNode original, Cloner cloner) 144 : base(original, cloner) { 145 Quality = original.Quality; 146 IsElite = original.IsElite; 147 Rank = original.Rank; 148 } 149 150 public override IDeepCloneable Clone(Cloner cloner) { 151 return new SymbolicExpressionGenealogyGraphNode(this, cloner); 102 152 } 103 153 … … 109 159 return base.Descendants().Cast<SymbolicExpressionGenealogyGraphNode>(); 110 160 } 161 162 public IEnumerable<SymbolicExpressionGenealogyGraphNode> RootLineage() { 163 var lineage = new List<SymbolicExpressionGenealogyGraphNode> { this }; 164 int i = 0; 165 while (i != lineage.Count) { 166 if (lineage[i].InEdges != null) { 167 lineage.Add((SymbolicExpressionGenealogyGraphNode)lineage[i].InEdges[0].Source); 168 } 169 ++i; 170 } 171 return lineage; 172 } 173 174 public new List<Arc> InEdges { 175 get { 176 return base.InEdges == null ? null : base.InEdges.Cast<Arc>().ToList(); 177 } 178 } 179 180 public new List<Arc> OutEdges { 181 get { 182 return base.OutEdges == null ? null : base.InEdges.Cast<Arc>().ToList(); 183 } 184 } 111 185 } 112 186 }
Note: See TracChangeset
for help on using the changeset viewer.