- Timestamp:
- 02/24/12 17:30:16 (12 years ago)
- Location:
- branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4
- Files:
-
- 1 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreeGenealogyAnalyzer.cs
r7515 r7522 36 36 using HeuristicLab.Problems.DataAnalysis; 37 37 using HeuristicLab.Problems.DataAnalysis.Symbolic; 38 38 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; 39 39 using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>; 40 40 using TraceMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItemList<HeuristicLab.Core.IItem>>; … … 229 229 // add all individuals to the evolutionary graph 230 230 int generation = Generations.Value; 231 int count = GlobalTraceMap.Count;232 231 string label; 233 232 … … 238 237 var tree = qualities.ElementAt(i).Key; 239 238 label = (i + 1).ToString(CultureInfo.InvariantCulture); 240 graph.AddNode(tree, qualities[tree], label, generation );239 graph.AddNode(tree, qualities[tree], label, generation, i < Elites.Value); 241 240 } 242 241 return base.Apply(); 243 242 } 244 243 245 // mark and add elites 246 // elites do not appear in the trace map (because they are never the product of a genetic operation) 247 var elites = qualities.OrderByDescending(x => x.Value).Take(Elites.Value).Select(x => x.Key).ToList(); 248 for (int i = 0; i != Elites.Value; ++i) { 249 label = (generation * count + i + 1).ToString(CultureInfo.InvariantCulture); 250 var elite = elites[i]; 251 if (!graph.HasNode(elite)) 252 graph.AddNode(elite, qualities[elite], label, Generations.Value, true); 253 else 254 graph.GetNode(elite).Label += "\\n" + label; 255 256 graph.GetNode(elite).Color = new Color { R = 0, G = 100, B = 150 }; 257 } 258 259 for (int i = 0; i != count; ++i) { 260 var trace = GlobalTraceMap.ElementAt(i); 261 var child = (ISymbolicExpressionTree)trace.Key; 262 263 if (!graph.HasNode(child)) { 264 // due to the structure of the trace map, qualities[child] will never throw an exception, so we use it directly 265 label = (generation * count + i + 1 + Elites.Value).ToString(CultureInfo.InvariantCulture); 244 for (int i = 0; i != qualities.Count; ++i) { 245 var child = qualities.ElementAt(i).Key; 246 label = (generation * qualities.Count + i + 1).ToString(CultureInfo.InvariantCulture); 247 if (i < Elites.Value) { 248 if (graph.HasNode(child)) 249 graph.GetNode(child).Label += "\\n" + label; 250 else 251 graph.AddNode(child, qualities[child], label, generation, true); 252 } else 266 253 graph.AddNode(child, qualities[child], label, generation); 267 }268 var parents = trace.Value;254 if (!GlobalTraceMap.ContainsKey(child)) continue; 255 var parents = GlobalTraceMap[child]; 269 256 foreach (var parent in parents) { 270 if (!graph.HasNode(parent)) { 271 // if the node is a clone introduced pre-mutation, then its quality value has to be evaluated 272 if (!qualities.ContainsKey(parent)) 273 qualities[parent] = Evaluate((ISymbolicExpressionTree)parent); 274 label = ((generation - 1) * count + i + 1).ToString(CultureInfo.InvariantCulture); 275 graph.AddNode(parent, qualities[parent], label, generation - 1); 257 if (GlobalTraceMap.ContainsKey(parent)) { 258 double quality = Evaluate((ISymbolicExpressionTree)parent); 259 graph.AddNode(parent, quality, "", generation - 0.5); 260 foreach (var p in GlobalTraceMap[parent]) 261 graph.AddArc(p, parent); 276 262 } 277 263 graph.AddArc(parent, child); 278 264 } 279 265 } 280 GlobalTraceMap.Clear(); // no need to check for null here (see line 212) 281 282 // if we've reached the end of the run 266 267 GlobalTraceMap.Clear(); 268 GlobalCloneMap.Clear(); 269 270 #region end of the run 283 271 bool maxGenerationsReached = (Generations.Value == MaximumGenerations.Value); 284 272 bool isOsga = (SelectionPressure != null && MaximumSelectionPressure != null); 285 273 bool maxSelectionPressureReached = isOsga && (SelectionPressure.Value >= MaximumSelectionPressure.Value); 286 287 #region end of the run288 274 if (maxGenerationsReached || maxSelectionPressureReached) { 289 275 var path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); … … 300 286 301 287 // calculate impact values of nodes in the best solution, attempt to trace those with high impact to their origins 302 //var impactValuesCalculator = new RegressionSolutionImpactValuesCalculator(); 303 //var impactValues = impactValuesCalculator.CalculateImpactValues(bestSolution, SymbolicExpressionInterpreter, SymbolicRegressionProblemData); 304 ////var impactValues = CalculateImpactValues(bestSolution); 305 //foreach (var pair in impactValues.Where(pair => !(pair.Key is ConstantTreeNode || pair.Key is VariableTreeNode) && pair.Value > 0.9)) { 306 // var node = pair.Key; 307 308 // foreach (var ancestor in genealogy.Keys) { 309 // graph.GetNode(ancestor).Color = ContainsSubtree(ancestor as ISymbolicExpressionTree, node) ? new Color { R = 0, G = 0, B = 150 } : new Color { R = 255, G = 255, B = 255 }; 310 // } 311 //} 312 //WriteDot(path + @"\impactancestry.dot", genealogy); 288 var calculator = new SymbolicRegressionSolutionValuesCalculator(); 289 var impactValues = calculator.CalculateImpactValues(bestSolution, SymbolicExpressionInterpreter, SymbolicRegressionProblemData, 0, 0); 290 foreach (var pair in impactValues.Where(pair => !(pair.Key is ConstantTreeNode || pair.Key is VariableTreeNode) && pair.Value > 0.9)) { 291 var node = pair.Key; 292 293 foreach (var ancestor in genealogy.Keys) { 294 graph.GetNode(ancestor).Color = ContainsSubtree(ancestor as ISymbolicExpressionTree, node) ? new Color { R = 0, G = 0, B = 150 } : new Color { R = 255, G = 255, B = 255 }; 295 } 296 } 297 WriteDot(path + @"\impactancestry.dot", genealogy); 313 298 314 299 // trim the graph … … 361 346 foreach (var node in graph.Values) { 362 347 string fillColor = String.Format("#{0:x2}{1:x2}{2:x2}", node.Color.R, node.Color.G, node.Color.B); 348 string shape = "circle"; 363 349 if (node.IsElite) 364 fillColor = String.Format("#{0:x2}{1:x2}{2:x2}", node.Color.R, node.Color.G, 150);365 file.WriteLine("\t\"" + node.Id + "\" [ fillcolor=\"" + fillColor + "\",label=\"" + node.Label + "\"];");350 shape = "doublecircle"; 351 file.WriteLine("\t\"" + node.Id + "\" [shape=" + shape + ",fillcolor=\"" + fillColor + "\",label=\"" + node.Label + "\"];"); 366 352 if (node.InEdges == null) 367 353 continue; … … 371 357 } 372 358 } 373 foreach (var g in graph.Values.GroupBy(x => x. Generation)) {359 foreach (var g in graph.Values.GroupBy(x => x.Rank)) { 374 360 var sb = new StringBuilder(); 375 361 sb.Append("\t{rank=same;"); … … 380 366 } 381 367 file.WriteLine("}"); 382 }383 }384 #endregion385 386 #region Impact values (code for calculating to be moved in separate class)387 private Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree) {388 var interpreter = SymbolicExpressionInterpreter;389 var problemData = (IRegressionProblemData)SymbolicDataAnalysisEvaluator.Parameters["ProblemData"].ActualValue;390 var dataset = problemData.Dataset;391 var rows = problemData.TrainingIndizes;392 string targetVariable = problemData.TargetVariable;393 var impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();394 var nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();395 var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows);396 var targetValues = dataset.GetDoubleValues(targetVariable, rows);397 OnlineCalculatorError errorState;398 double originalR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, originalOutput, out errorState);399 if (errorState != OnlineCalculatorError.None) originalR2 = 0.0;400 401 var constantNode = ((ConstantTreeNode)new Constant().CreateTreeNode());402 var root = new ProgramRootSymbol().CreateTreeNode(); // root node403 var start = new StartSymbol().CreateTreeNode(); // start node404 root.AddSubtree(start);405 var tempTree = new SymbolicExpressionTree(root);406 407 foreach (ISymbolicExpressionTreeNode node in nodes) {408 var parent = node.Parent;409 constantNode.Value = CalculateReplacementValue(tempTree, node, tree);410 ISymbolicExpressionTreeNode replacementNode = constantNode;411 SwitchNode(parent, node, replacementNode);412 var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows);413 double newR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, newOutput, out errorState);414 if (errorState != OnlineCalculatorError.None) newR2 = 0.0;415 416 // impact = 0 if no change417 // impact < 0 if new solution is better418 // impact > 0 if new solution is worse419 impactValues[node] = originalR2 - newR2;420 SwitchNode(parent, replacementNode, node);421 }422 return impactValues;423 }424 425 private double CalculateReplacementValue(ISymbolicExpressionTree tempTree, ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree) {426 // remove old ADFs427 while (tempTree.Root.SubtreeCount > 1) tempTree.Root.RemoveSubtree(1);428 // clone ADFs of source tree429 for (int i = 1; i < sourceTree.Root.SubtreeCount; i++) {430 tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());431 }432 var start = tempTree.Root.GetSubtree(0);433 while (start.SubtreeCount > 0) start.RemoveSubtree(0);434 start.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());435 var interpreter = SymbolicExpressionInterpreter;436 var rows = SymbolicRegressionProblemData.TrainingIndizes;437 return interpreter.GetSymbolicExpressionTreeValues(tempTree, SymbolicRegressionProblemData.Dataset, rows).Median();438 }439 440 private static void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {441 for (int i = 0; i < root.SubtreeCount; i++) {442 if (root.GetSubtree(i) == oldBranch) {443 root.RemoveSubtree(i);444 root.InsertSubtree(i, newBranch);445 return;446 }447 368 } 448 369 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/GenealogyGraph.cs
r7515 r7522 84 84 /// <param name="l">The node label</param> 85 85 /// <param name="elite">Specifies if this node is an elite</param> 86 public void AddNode(object t, double q = 0.0, string l = "", intg = 0, bool elite = false) {86 public void AddNode(object t, double q = 0.0, string l = "", double g = 0, bool elite = false) { 87 87 if (HasNode(t)) return; 88 88 var color = new Color { R = (byte)(255 - 255 * q), G = (byte)(255 * q), B = 50 }; 89 _dictionary[t] = new Node { Data = t, Quality = q, Label = l, IsElite = elite, Generation= g, Color = color };89 _dictionary[t] = new Node { Data = t, Quality = q, Label = l, IsElite = elite, Rank = g, Color = color }; 90 90 } 91 91 … … 186 186 public object Data { get; set; } 187 187 public double Quality { get; set; } 188 public int Generation{ get; set; }188 public double Rank { get; set; } 189 189 public Color Color { get; set; } 190 190 -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/HeuristicLab.EvolutionaryTracking-3.4.csproj
r7515 r7522 102 102 <Compile Include="Plugin.cs" /> 103 103 <Compile Include="Properties\AssemblyInfo.cs" /> 104 <Compile Include="RegressionSolutionImpactValuesCalculator.cs">105 <SubType>UserControl</SubType>106 </Compile>107 104 </ItemGroup> 108 105 <ItemGroup>
Note: See TracChangeset
for help on using the changeset viewer.