Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/BoxChart/DiscreteValueBoxChartElementGenerator.cs @ 11194

Last change on this file since 11194 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

File size: 6.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Drawing.Drawing2D;
5using System.Linq;
6using System.Text;
7using HeuristicLab.Common;
8using HeuristicLab.Core;
9using HeuristicLab.Data;
10using HeuristicLab.Optimization;
11using HeuristicLab.Parameters;
12using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
13using HeuristicLab.Analysis.FitnessLandscape.BoxChart;
14
15namespace HeuristicLab.Analysis.FitnessLandscape.BoxCharts {
16
17  [StorableClass]
18  public class DiscreteValueBoxChartElementGenerator : EmptyBoxChartElementGenerator {
19
20    public ValueParameter<StringValue> ValueNameParameter {
21      get { return (ValueParameter<StringValue>)Parameters["ValueName"]; }
22    }
23    public ValueParameter<ItemList<StringValue>> LevelsParameter {
24      get { return (ValueParameter<ItemList<StringValue>>)Parameters["Levels"]; }
25    }
26    public ValueParameter<ItemList<ColorValue>> ColorsParameter {
27      get { return (ValueParameter<ItemList<ColorValue>>)Parameters["Colors"]; }
28    }
29    public ValueParameter<BoolValue> InvertParameter {
30      get { return (ValueParameter<BoolValue>)Parameters["Invert"]; }
31    }
32
33    protected string ValueName { get { return ValueNameParameter.Value.Value; } }
34    protected List<string> Levels { get { return LevelsParameter.Value.Select(v => v.Value).ToList(); } }
35    protected List<Color> Colors { get { return ColorsParameter.Value.Select(v => v.Color).ToList(); } }
36    protected bool Invert { get { return InvertParameter.Value.Value; } }
37
38    #region Construction & Cloning
39
40    [StorableConstructor]
41    protected DiscreteValueBoxChartElementGenerator(bool deserializing) : base(deserializing) { }
42    protected DiscreteValueBoxChartElementGenerator(DiscreteValueBoxChartElementGenerator original, Cloner cloner)
43      : base(original, cloner) {
44      RegisterEvents();
45    }
46    public DiscreteValueBoxChartElementGenerator() {
47      Parameters.Add(new ValueParameter<StringValue>("ValueName", "The name of the result of parameter ot be visualized.", new StringValue("Value")));
48      Parameters.Add(new ValueParameter<ItemList<StringValue>>("Levels", "The names and order of the different possible values.",
49        new ItemList<StringValue> {
50          new StringValue("Very Low"),
51          new StringValue("Low"),
52          new StringValue("Average"),
53          new StringValue("High"),
54          new StringValue("Very High") }));
55      Parameters.Add(new ValueParameter<ItemList<ColorValue>>("Colors", "The list of colors to assigned to the different levels",
56        new ItemList<ColorValue> {
57          new ColorValue(Color.Blue),
58          new ColorValue(Color.Green),
59          new ColorValue(Color.White),
60          new ColorValue(Color.Orange),
61          new ColorValue(Color.Red) }));
62      Parameters.Add(new ValueParameter<BoolValue>("Invert", "Whether the color assignment should be inverted and highlighted.", new BoolValue(false)));
63      RegisterEvents();
64      if (GetType() == typeof(DiscreteValueBoxChartElementGenerator))
65        UpdateName();
66    }
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new DiscreteValueBoxChartElementGenerator(this, cloner);
69    }
70    [StorableHook(HookType.AfterDeserialization)]
71    private void AfterDeserialization() {
72      if (!Parameters.ContainsKey("Levels"))
73        Parameters.Add(new ValueParameter<ItemList<StringValue>>("Levels", "The names and order of the different possible values.",
74          new ItemList<StringValue> {
75            new StringValue("Very Low"),
76            new StringValue("Low"),
77            new StringValue("Average"),
78            new StringValue("High"),
79            new StringValue("Very High") }));
80      if (!Parameters.ContainsKey("Colors"))
81        Parameters.Add(new ValueParameter<ItemList<ColorValue>>("Colors", "The list of colors to assigned to the different levels",
82          new ItemList<ColorValue> {
83          new ColorValue(Color.Blue),
84          new ColorValue(Color.Green),
85          new ColorValue(Color.White),
86          new ColorValue(Color.Orange),
87          new ColorValue(Color.Red) }));
88      if (!Parameters.ContainsKey("Invert"))
89        Parameters.Add(new ValueParameter<BoolValue>("Invert", "Whether the color assignment should be inverted and highlighted.", new BoolValue(false)));
90      RegisterEvents();
91    }
92    #endregion
93
94    private void RegisterEvents() {
95      ValueNameParameter.ToStringChanged += new EventHandler(Parameters_Changed);
96    }
97
98    private void Parameters_Changed(object sender, EventArgs eventArgs) {
99      UpdateName();
100    }
101
102    protected override string CalculateName() {
103      return string.Format("{0} d {1}", base.CalculateName(), ValueName);
104    }
105
106    private string GetValue(IRun run) {
107      IItem value;
108      run.Results.TryGetValue(ValueName, out value);
109      if (value != null)
110        return value.ToString();
111      run.Parameters.TryGetValue(ValueName, out value);
112      if (value != null)
113        return value.ToString();
114      return "";
115    }
116
117    private Color GetColor(string value) {
118      var idx = Levels.IndexOf(value);
119      if (Invert)
120        idx = Colors.Count - 1 - idx;
121      if (idx < 0 || idx >= Colors.Count)
122        return Color.Black;
123      return Colors[idx];
124    }
125
126    public override void Draw(IRun run, Graphics g) {
127      using (var b = new SolidBrush(GetColor(GetValue(run))))
128        g.FillRectangle(b, g.ClipBounds.Left, g.ClipBounds.Top, g.ClipBounds.Width, g.ClipBounds.Height);
129      if (Invert) {
130        using (var p = new Pen(SystemBrushes.ControlDarkDark, 3)) {
131          g.DrawLines(p, new[] {
132            new PointF(g.ClipBounds.Left+1, g.ClipBounds.Bottom-1),
133            new PointF(g.ClipBounds.Left+1, g.ClipBounds.Top+1),
134            new PointF(g.ClipBounds.Right-1, g.ClipBounds.Top+1)});
135          p.Color = SystemColors.ControlLightLight;
136          g.DrawLines(p, new[] {
137            new PointF(g.ClipBounds.Right-1, g.ClipBounds.Top+1),
138            new PointF(g.ClipBounds.Right-1, g.ClipBounds.Bottom-1),
139            new PointF(g.ClipBounds.Left+1, g.ClipBounds.Bottom-1) });
140        }
141      }
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.