Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Analysis/MultiOperatorUsageAnalyzer.cs @ 16575

Last change on this file since 16575 was 16575, checked in by gkronber, 5 years ago

#2520: changed HeuristicLab.BioBoost addon to compile with new HL.Persistence

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