Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.SolutionCaching/3.3/Algorithm/SolutionCacheAnalyzerAlgorithm.cs @ 10442

Last change on this file since 10442 was 10090, checked in by ascheibe, 11 years ago

#1886 added an algorithm that executes analyzers on a solution cache

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Analysis.SolutionCaching {
32  [Item("SolutionCache Analyzer Algorithm", "An algorithm that runs analyzers on a solution cache.")]
33  [Creatable("Algorithms")]
34  [StorableClass]
35  public sealed class SolutionCacheAnalyzerAlgorithm : EngineAlgorithm, IStorableContent {
36    public string Filename { get; set; }
37    private const string analyzerParameterString = "SolutionCache Analyzer";
38    private const string solutionCacheParameterString = "SolutionCache";
39
40    #region Problem Properties
41    public override Type ProblemType {
42      get { return typeof(ISolutionCache); }
43    }
44    public new ISolutionCache Problem {
45      get { return (ISolutionCache)base.Problem; }
46      set { base.Problem = value; }
47    }
48    #endregion
49
50    #region Parameter Properties
51    public ValueParameter<CheckedItemList<ISolutionCacheAnalyzer>> SolutionCacheAnalyzerParameter {
52      get { return (ValueParameter<CheckedItemList<ISolutionCacheAnalyzer>>)Parameters[analyzerParameterString]; }
53    }
54    public ValueParameter<ISolutionCache> SolutionCacheParameter {
55      get { return (ValueParameter<ISolutionCache>)Parameters[solutionCacheParameterString]; }
56    }
57    #endregion
58
59    [StorableConstructor]
60    private SolutionCacheAnalyzerAlgorithm(bool deserializing) : base(deserializing) { }
61
62    private SolutionCacheAnalyzerAlgorithm(SolutionCacheAnalyzerAlgorithm original, Cloner cloner)
63      : base(original, cloner) {
64    }
65
66    public SolutionCacheAnalyzerAlgorithm()
67      : base() {
68      Parameters.Add(new ValueParameter<CheckedItemList<ISolutionCacheAnalyzer>>(analyzerParameterString, "Analyzers that can be applied to solution caches.", new CheckedItemList<ISolutionCacheAnalyzer>()));
69      Parameters.Add(new ValueParameter<ISolutionCache>(solutionCacheParameterString, "The solution cache."));
70
71      OperatorGraph.InitialOperator = new ParallelOperatorProcessor();
72      //((ParallelOperatorProcessor)OperatorGraph.InitialOperator).ParallelParameter.Value.Value = true;
73    }
74
75
76    public override IDeepCloneable Clone(Cloner cloner) {
77      return new SolutionCacheAnalyzerAlgorithm(this, cloner);
78    }
79
80    public override void Start() {
81      if (SolutionCacheAnalyzerParameter.Value.CheckedItems.Count() == 0)
82        throw new ArgumentException("At least one analyzer has to be selected. ");
83
84      var initialOperator = OperatorGraph.InitialOperator as ParallelOperatorProcessor;
85      initialOperator.Operators.Clear();
86
87      foreach (var curOpt in SolutionCacheAnalyzerParameter.Value.CheckedItems) {
88        initialOperator.Operators.Add(curOpt.Value);
89      }
90
91      base.Start();
92    }
93
94    private void UpdateSolutionCacheAnalyzers() {
95      //TODO: should we fetch the operators from the problem?
96      SolutionCacheAnalyzerParameter.Value.Clear();
97      foreach (var analyzer in ApplicationManager.Manager.GetInstances<ISolutionCacheAnalyzer>()) {
98        SolutionCacheAnalyzerParameter.Value.Add(analyzer);
99      }
100    }
101
102    #region Events
103    protected override void OnProblemChanged() {
104      UpdateSolutionCacheAnalyzers();
105      SolutionCacheParameter.Value = Problem;
106      base.OnProblemChanged();
107    }
108    #endregion
109  }
110}
Note: See TracBrowser for help on using the repository browser.