source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Analysis/MultiOperatorUsageAnalyzer.cs @ 13069

Last change on this file since 13069 was 13069, checked in by gkronber, 7 years ago

#2499: imported source code for HeuristicLab.BioBoost from private repository with some changes

File size: 3.6 KB
Line 
1using HeuristicLab.Analysis;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Operators;
6using HeuristicLab.Optimization;
7using HeuristicLab.Parameters;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using System.Linq;
10
11namespace HeuristicLab.BioBoost.Analysis {
12
13  [Item("MultiOperatorUsageAnalyzer", "An operator for analyzing utilization of different choices of a multi manipulation operatoutilization of different choices of a multi manipulation operator.")]
14  [StorableClass]
15  public class MultiOperatorUsageAnalyzer : SingleSuccessorOperator, IAnalyzer {
16
17    public bool EnabledByDefault { get { return false; } }
18
19    #region Parameters
20    public ScopeTreeLookupParameter<StringValue> SelectedOperatorParameter {
21      get { return (ScopeTreeLookupParameter<StringValue>) Parameters["SelectedOperator"]; }
22    }
23    public ValueLookupParameter<ResultCollection> ResultsParameter {
24      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
25    }
26    #endregion
27
28    #region Construction & Cloning
29    [StorableConstructor]
30    protected MultiOperatorUsageAnalyzer(bool isDeserializing) : base(isDeserializing) {}
31    protected MultiOperatorUsageAnalyzer(MultiOperatorUsageAnalyzer orig, Cloner cloner) : base(orig, cloner) {}
32    public MultiOperatorUsageAnalyzer() {
33      Parameters.Add(new ScopeTreeLookupParameter<StringValue>("SelectedOperator", "The selected operators in a population."));
34      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best solution should be stored."));
35    }
36    public override IDeepCloneable Clone(Cloner cloner) {
37      return new MultiOperatorUsageAnalyzer(this, cloner);
38    }
39    #endregion
40
41    public override IOperation Apply() {
42      var operators = SelectedOperatorParameter.ActualValue;
43      var table = GetOrCreateTable(SelectedOperatorParameter.ActualName + "Usages");
44      AddValues(table, operators);
45      var avgTable = GetOrCreateTable(SelectedOperatorParameter.ActualName + "Avg Usages");
46      foreach (var row in table.Rows) {
47        DataRow avgRow;
48        if (!avgTable.Rows.TryGetValue(row.Name, out avgRow)) {
49          avgRow = new DataRow(row.Name);
50          avgRow.Values.AddRange(Enumerable.Repeat(0d, row.Values.Count - 1));
51          avgTable.Rows.Add(avgRow);
52        }
53        avgRow.Values.Add(row.Values.Average());
54      }
55      return base.Apply();
56    }
57
58    private static void AddValues(DataTable table, ItemArray<StringValue> operators) {
59      var n = operators.Length;
60      var groups = operators
61        .GroupBy(v => v.Value)
62        .ToDictionary(g => g.Key, g => 1.0*g.Count()/n);
63      var len = table.Rows.Count == 0 ? 0 : table.Rows.Max(r => r.Values.Count);
64      foreach (var row in table.Rows) {
65        var probability = 0d;
66        if (groups.TryGetValue(row.Name, out probability))
67          groups.Remove(row.Name);
68        row.Values.Add(probability);
69      }
70      foreach (var newOp in groups) {
71        var row = new DataRow(newOp.Key);
72        row.Values.AddRange(Enumerable.Repeat(0d, len));
73        row.Values.Add(newOp.Value);
74        table.Rows.Add(row);
75      }
76    }
77
78    private DataTable GetOrCreateTable(string tableName) {
79      IResult result = null;
80      if (ResultsParameter.ActualValue.TryGetValue(tableName, out result)) {
81        return (DataTable)result.Value;
82      } else {
83        var table = new DataTable(tableName);
84        ResultsParameter.ActualValue.Add(new Result(tableName, table));
85        return table;
86      }
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.