Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13071 was 13071, checked in by gkronber, 9 years ago

#2499: added license headers and removed unused usings

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