[10514] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
[10685] | 22 | using System;
|
---|
[10514] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
[10746] | 25 | using System.Drawing.Drawing2D;
|
---|
[10797] | 26 | using System.Drawing.Text;
|
---|
[10514] | 27 | using System.Linq;
|
---|
[10685] | 28 | using HeuristicLab.Common;
|
---|
[10514] | 29 | using HeuristicLab.Visualization;
|
---|
[10524] | 30 | using Rectangle = HeuristicLab.Visualization.Rectangle;
|
---|
| 31 |
|
---|
[10514] | 32 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
|
---|
[10655] | 33 | /// <summary>
|
---|
| 34 | /// The SymbolicExpressionTreeTile will display either a full tree or just a subtree (case in which the SymbolicExpressionTreeProperty remains unset).
|
---|
| 35 | /// By setting the Root property, the user can use the SymbolicExpressionTreeTile to layout and display arbitrary subtrees.
|
---|
| 36 | /// </summary>
|
---|
[10514] | 37 | public class SymbolicExpressionTreeTile : Group {
|
---|
[10728] | 38 | public int PreferredNodeWidth { get; set; }
|
---|
| 39 | public int PreferredNodeHeight { get; set; }
|
---|
[10797] | 40 |
|
---|
[10833] | 41 | private const int labelHeight = 60;
|
---|
[10797] | 42 |
|
---|
[10827] | 43 | private readonly SolidBrush defaultBrush;
|
---|
| 44 | private readonly Pen defaultPen;
|
---|
| 45 | private readonly Font defaultFont;
|
---|
| 46 |
|
---|
[10746] | 47 | public ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> LayoutEngine { get; set; }
|
---|
| 48 | private readonly Dictionary<IPrimitive, ISymbolicExpressionTreeNode> primitivesToNodes;
|
---|
| 49 | private readonly Dictionary<ISymbolicExpressionTreeNode, IPrimitive> nodesToPrimitives;
|
---|
[10685] | 50 |
|
---|
[10797] | 51 | private string label;
|
---|
| 52 | public string Label {
|
---|
| 53 | get { return label; }
|
---|
| 54 | set { label = value; }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[10730] | 57 | private Size size;
|
---|
[10685] | 58 | public Size Size {
|
---|
[10730] | 59 | private set { size = value; }
|
---|
| 60 | get { return size; }
|
---|
[10685] | 61 | }
|
---|
| 62 |
|
---|
| 63 | private Point position;
|
---|
| 64 | public Point Position {
|
---|
| 65 | get { return position; }
|
---|
| 66 | set {
|
---|
| 67 | var oldpos = position;
|
---|
| 68 | position = value;
|
---|
| 69 | int ox = position.X - oldpos.X;
|
---|
| 70 | int oy = position.Y - oldpos.Y;
|
---|
| 71 | // translate all primitives to the new position
|
---|
| 72 | foreach (var p in Primitives) {
|
---|
| 73 | var rpb = p as RectangularPrimitiveBase;
|
---|
| 74 | if (rpb != null) {
|
---|
| 75 | var lowerLeft = new Point((int)Math.Floor(rpb.LowerLeft.X) + ox, (int)Math.Floor(rpb.LowerLeft.Y + oy));
|
---|
| 76 | var upperRight = new PointD(lowerLeft.X + rpb.Size.Width, lowerLeft.Y + rpb.Size.Height);
|
---|
| 77 | rpb.SetPosition(lowerLeft, upperRight);
|
---|
| 78 | }
|
---|
| 79 | var line = p as LinearPrimitiveBase;
|
---|
| 80 | if (line != null) {
|
---|
| 81 | var start = new Point((int)Math.Floor(line.Start.X) + ox, (int)Math.Floor(line.Start.Y) + oy);
|
---|
| 82 | var end = new Point(start.X + (int)line.Size.Width, start.Y + (int)line.Size.Height);
|
---|
| 83 | line.SetPosition(start, end);
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[10746] | 89 | public IPrimitive GetPrimitive(ISymbolicExpressionTreeNode node) {
|
---|
| 90 | IPrimitive primitive;
|
---|
| 91 | nodesToPrimitives.TryGetValue(node, out primitive);
|
---|
| 92 | return primitive;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | public ISymbolicExpressionTreeNode GetExpressionTreeNode(IPrimitive primitive) {
|
---|
| 96 | ISymbolicExpressionTreeNode node;
|
---|
| 97 | primitivesToNodes.TryGetValue(primitive, out node);
|
---|
| 98 | return node;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[10514] | 101 | private ISymbolicExpressionTree symbolicExpressionTree;
|
---|
| 102 | public ISymbolicExpressionTree SymbolicExpressionTree {
|
---|
| 103 | get { return symbolicExpressionTree; }
|
---|
| 104 | set {
|
---|
| 105 | symbolicExpressionTree = value;
|
---|
[10655] | 106 | Root = symbolicExpressionTree.Root;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | new private void Clear() {
|
---|
[13061] | 111 | base.Clear();
|
---|
[10746] | 112 | primitivesToNodes.Clear();
|
---|
| 113 | nodesToPrimitives.Clear();
|
---|
[10655] | 114 | }
|
---|
| 115 |
|
---|
| 116 | private ISymbolicExpressionTreeNode root;
|
---|
| 117 | public ISymbolicExpressionTreeNode Root {
|
---|
| 118 | get { return root; }
|
---|
| 119 | set {
|
---|
| 120 | root = value;
|
---|
[10514] | 121 | GeneratePrimitives(PreferredNodeWidth, PreferredNodeHeight);
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
[10650] | 124 |
|
---|
| 125 | public SymbolicExpressionTreeTile(IChart chart)
|
---|
| 126 | : base(chart) {
|
---|
[10746] | 127 | primitivesToNodes = new Dictionary<IPrimitive, ISymbolicExpressionTreeNode>();
|
---|
| 128 | nodesToPrimitives = new Dictionary<ISymbolicExpressionTreeNode, IPrimitive>();
|
---|
[10685] | 129 | PreferredNodeWidth = 80;
|
---|
| 130 | PreferredNodeHeight = 40;
|
---|
[13061] | 131 | // Group = new Group(chart);
|
---|
[10827] | 132 |
|
---|
| 133 | defaultBrush = new SolidBrush(Color.Transparent);
|
---|
[10838] | 134 | defaultPen = new Pen(Color.DimGray);
|
---|
[10827] | 135 | defaultFont = new Font(FontFamily.GenericSansSerif, 12, GraphicsUnit.Pixel);
|
---|
[10650] | 136 | }
|
---|
[10514] | 137 | public SymbolicExpressionTreeTile(IChart chart, ISymbolicExpressionTree tree)
|
---|
[10650] | 138 | : this(chart) {
|
---|
[10677] | 139 | SymbolicExpressionTree = tree;
|
---|
[10514] | 140 | }
|
---|
| 141 | private void GeneratePrimitives(double preferredNodeWidth, double preferredNodeHeight) {
|
---|
| 142 | Clear();
|
---|
[10677] | 143 | var actualRoot = Root;
|
---|
[10884] | 144 | // if (Root.Symbol is ProgramRootSymbol && Root.SubtreeCount == 1) { actualRoot = Root.GetSubtree(0).GetSubtree(0); }
|
---|
[10728] | 145 |
|
---|
| 146 | LayoutEngine.NodeWidth = PreferredNodeWidth;
|
---|
| 147 | LayoutEngine.NodeHeight = PreferredNodeHeight;
|
---|
| 148 |
|
---|
[10730] | 149 | var visualNodes = LayoutEngine.CalculateLayout(actualRoot).ToList();
|
---|
[10514] | 150 |
|
---|
[10524] | 151 | var visualNodeMap = visualNodes.ToDictionary(x => x.Content, x => x);
|
---|
[10517] | 152 |
|
---|
[10524] | 153 | foreach (var visualNode in visualNodes) {
|
---|
| 154 | var lowerLeft = new PointD(visualNode.X, visualNode.Y);
|
---|
| 155 | var upperRight = new PointD(visualNode.X + preferredNodeWidth, visualNode.Y + preferredNodeHeight);
|
---|
| 156 | var node = visualNode.Content;
|
---|
[10746] | 157 |
|
---|
[10517] | 158 | RectangularPrimitiveBase rectangularPrimitive;
|
---|
[10797] | 159 | var shortenedLabel = ShortenLabel(node);
|
---|
[10524] | 160 | if (node.SubtreeCount == 0) {
|
---|
[10827] | 161 | rectangularPrimitive = new Rectangle(Chart, lowerLeft, upperRight) {
|
---|
[13061] | 162 | // Font = defaultFont, Text = shortenedLabel, Brush = defaultBrush, Pen = defaultPen, MaximumFontSize = 12f
|
---|
[10827] | 163 | };
|
---|
[10514] | 164 | } else {
|
---|
[10827] | 165 | rectangularPrimitive = new Ellipse(Chart, lowerLeft, upperRight) {
|
---|
[13061] | 166 | // Font = defaultFont, Text = shortenedLabel, Brush = defaultBrush, Pen = defaultPen, MaximumFontSize = 12f
|
---|
[10827] | 167 | };
|
---|
[10514] | 168 | }
|
---|
[10517] | 169 |
|
---|
[10746] | 170 | primitivesToNodes.Add(rectangularPrimitive, node); // to be able to retrieve nodes via primitives
|
---|
| 171 | nodesToPrimitives.Add(node, rectangularPrimitive);
|
---|
[10517] | 172 | this.Add(rectangularPrimitive);
|
---|
[10685] | 173 |
|
---|
| 174 | if (rectangularPrimitive.Size.Width.IsAlmost(0) || rectangularPrimitive.Size.Height.IsAlmost(0)) {
|
---|
| 175 | throw new Exception("Primitive size cannot be zero.");
|
---|
| 176 | }
|
---|
[10514] | 177 | }
|
---|
| 178 |
|
---|
[10524] | 179 | foreach (var node in visualNodes.Where(n => n.Content.SubtreeCount > 0)) {
|
---|
| 180 | var parent = node.Content;
|
---|
| 181 | foreach (var child in parent.Subtrees.Select(x => visualNodeMap[x])) {
|
---|
[10514] | 182 | var start = new PointD(node.X + preferredNodeWidth / 2, node.Y + preferredNodeHeight);
|
---|
| 183 | var end = new PointD(child.X + preferredNodeWidth / 2, child.Y);
|
---|
[10838] | 184 | var line = new Line(this.Chart, start, end) { Pen = defaultPen };
|
---|
[10514] | 185 | this.Add(line);
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
[10730] | 188 | int xmin = 0, ymin = 0, xmax = 0, ymax = 0;
|
---|
| 189 | foreach (var rpb in Primitives.OfType<RectangularPrimitiveBase>()) {
|
---|
| 190 | if (xmin > rpb.LowerLeft.X) xmin = (int)Math.Floor(rpb.LowerLeft.X);
|
---|
| 191 | if (xmax < rpb.UpperRight.X) xmax = (int)Math.Ceiling(rpb.UpperRight.X);
|
---|
| 192 | if (ymin > rpb.LowerLeft.Y) ymin = (int)Math.Floor(rpb.LowerLeft.Y);
|
---|
| 193 | if (ymax < rpb.UpperRight.Y) ymax = (int)Math.Ceiling(rpb.UpperRight.Y);
|
---|
| 194 | }
|
---|
[10797] | 195 | int width = xmax - xmin;
|
---|
| 196 | int height = ymax - ymin;
|
---|
| 197 |
|
---|
| 198 | // draw a primitive to display the label
|
---|
| 199 | var labelRect = new Rectangle(this.Chart, new PointD(0, height), new PointD(width, height + labelHeight)) {
|
---|
[10827] | 200 | Pen = defaultPen,
|
---|
| 201 | Brush = defaultBrush,
|
---|
[13061] | 202 | // Text = Label,
|
---|
| 203 | // Font = defaultFont
|
---|
[10797] | 204 | };
|
---|
| 205 | this.Add(labelRect);
|
---|
| 206 |
|
---|
[10729] | 207 | // draw a rectangle to mark the countour of this tile
|
---|
[10797] | 208 | Size = new Size(width, height + labelHeight);
|
---|
| 209 |
|
---|
| 210 | var rectangle = new Rectangle(this.Chart, new PointD(0, 0), new PointD(Size.Width, Size.Height)) {
|
---|
[10827] | 211 | Pen = defaultPen,
|
---|
| 212 | Brush = defaultBrush
|
---|
[10729] | 213 | };
|
---|
| 214 | this.Add(rectangle);
|
---|
| 215 |
|
---|
[10730] | 216 | // flip primitives vertically
|
---|
[10729] | 217 | foreach (var primitive in Primitives) {
|
---|
| 218 | var rpb = primitive as RectangularPrimitiveBase;
|
---|
| 219 | if (rpb != null) {
|
---|
[13061] | 220 | var lowerLeft = new PointD(rpb.LowerLeft.X, Size.Height - rpb.UpperRight.Y);
|
---|
| 221 | var upperRight = new PointD(rpb.UpperRight.X, Size.Height - rpb.LowerLeft.Y);
|
---|
| 222 | rpb.SetPosition(lowerLeft, upperRight);
|
---|
[10730] | 223 |
|
---|
[10729] | 224 | } else {
|
---|
| 225 | var line = primitive as LinearPrimitiveBase;
|
---|
| 226 | if (line != null) {
|
---|
[13061] | 227 | var lowerLeft = new PointD(line.Start.X, Size.Height - line.Start.Y);
|
---|
| 228 | var upperRight = new PointD(line.End.X, Size.Height - line.End.Y);
|
---|
| 229 | line.SetPosition(lowerLeft, upperRight);
|
---|
[10729] | 230 | }
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
[10514] | 233 | }
|
---|
[10746] | 234 |
|
---|
| 235 | public override void Draw(Graphics graphics) {
|
---|
| 236 | graphics.SmoothingMode = SmoothingMode.HighQuality;
|
---|
[10797] | 237 | graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
|
---|
[10746] | 238 | base.Draw(graphics);
|
---|
| 239 | }
|
---|
[10752] | 240 |
|
---|
| 241 | private string ShortenLabel(ISymbolicExpressionTreeNode node) {
|
---|
| 242 | var term = node as SymbolicExpressionTreeTerminalNode;
|
---|
| 243 | if (term != null) {
|
---|
| 244 | var parts = term.ToString().Split(' '); // split by space
|
---|
[10835] | 245 | return parts.Length == 1 ? string.Format("{0:0.000}", double.Parse(parts[0])) : string.Format("{0:0.000} {1}", double.Parse(parts[0]), parts[1]);
|
---|
[10752] | 246 | }
|
---|
| 247 | return node.Symbol.ToString().Substring(0, 3);
|
---|
| 248 | }
|
---|
[10514] | 249 | }
|
---|
| 250 | }
|
---|