Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/BoxChart/DiscreteValueBoxChartElementGenerator.cs @ 17514

Last change on this file since 17514 was 16995, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.FLA for new persistence

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