Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Analysis.FitnessLandscape/3.3/CharacteristicCalculator/RandomWalkCalculator.cs @ 14778

Last change on this file since 14778 was 14690, checked in by abeham, 8 years ago

#2457: working on identification of problem instances

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Optimization;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using System;
28using System.Collections.Generic;
29using System.Linq;
30using System.Threading;
31
32namespace HeuristicLab.Analysis.FitnessLandscape {
33  [Item("Random Walk Calculator", "Calculates characteristics from a random walk.")]
34  [StorableClass]
35  public class RandomWalkCalculator : NamedItem, ICharacteristicCalculator {
36
37    [Storable]
38    private IProblem problem;
39    public IProblem Problem {
40      get { return problem; }
41      set {
42        if (problem == value) return;
43        problem = value;
44        var soProblem = problem as ISingleObjectiveHeuristicOptimizationProblem;
45        walker.Problem = soProblem;
46      }
47    }
48
49    [Storable]
50    private RandomWalk walker;
51
52    [StorableConstructor]
53    private RandomWalkCalculator(bool deserializing) : base(deserializing) { }
54    private RandomWalkCalculator(RandomWalkCalculator original, Cloner cloner)
55      : base(original, cloner) {
56      problem = cloner.Clone(original.problem);
57      walker = cloner.Clone(original.walker);
58      characteristics = cloner.Clone(original.characteristics);
59    }
60    public RandomWalkCalculator() : this(new RandomWalk()) { }
61    public RandomWalkCalculator(RandomWalk walker) {
62      Name = ItemName;
63      Description = ItemDescription;
64      this.walker = walker;
65      characteristics = new CheckedItemList<StringValue>(
66        new[] { "AutoCorrelation1", "CorrelationLength", "InformationContent",
67        "PartialInformationContent", "DensityBasinInformation", "InformationStability",
68        "Diversity", "Regularity", "TotalEntropy", "PeakInformationContent",
69        "PeakDensityBasinInformation" }.Select(x => new StringValue(x)));
70    }
71
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new RandomWalkCalculator(this, cloner);
74    }
75
76    private CheckedItemList<StringValue> characteristics;
77    public ReadOnlyCheckedItemList<StringValue> Characteristics {
78      get { return characteristics.AsReadOnly(); }
79    }
80
81    public bool CanCalculate() {
82      return Problem is ISingleObjectiveHeuristicOptimizationProblem
83        && Problem.Operators.Any(x => x is IManipulator);
84    }
85
86    public IEnumerable<IResult> Calculate() {
87      walker.Prepare(true);
88      using (var waitHandle = new AutoResetEvent(false)) {
89        EventHandler evHandle = (sender, e) => {
90          if (walker.ExecutionState == ExecutionState.Paused
91          || walker.ExecutionState == ExecutionState.Stopped) waitHandle.Set();
92        };
93        walker.ExecutionStateChanged += evHandle;
94        try {
95          walker.Start();
96          waitHandle.WaitOne();
97        } finally { walker.ExecutionStateChanged -= evHandle; }
98      }
99      foreach (var p in characteristics.CheckedItems) {
100        var resultName = "RandomWalk." + walker.MutatorParameter.Value.Name + "." + p.Value.Value;
101        IResult result;
102        if (walker.Results.TryGetValue(p.Value.Value, out result)) {
103          yield return new Result(resultName, result.Value);
104        } else yield return new Result(resultName, new DoubleValue(0));
105      }
106      walker.Prepare(true);
107    }
108
109    public void CollectParameterValues(IDictionary<string, IItem> values) {
110      walker.CollectParameterValues(values);
111    }
112
113    public IKeyedItemCollection<string, IParameter> Parameters {
114      get { return ((IParameterizedItem)walker).Parameters; }
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.