Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Analysis.FitnessLandscape/3.3/CharacteristicCalculator/AdaptiveWalkCalculator.cs

Last change on this file was 16728, checked in by abeham, 6 years ago

#1614: updated to new persistence and .NET 4.6.1

File size: 4.2 KB
RevLine 
[13583]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
[16728]22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Threading;
26using HEAL.Attic;
[13583]27using HeuristicLab.Common;
28using HeuristicLab.Core;
[13594]29using HeuristicLab.Data;
[13583]30using HeuristicLab.Optimization;
31
32namespace HeuristicLab.Analysis.FitnessLandscape {
33  [Item("Adaptive Walk Calculator", "Calculates characteristics from an adaptive walk.")]
[16728]34  [StorableType("1F29D2EB-0348-4DC9-AF26-073489D7DE04")]
[13583]35  public class AdaptiveWalkCalculator : NamedItem, ICharacteristicCalculator {
[13594]36
[13583]37    [Storable]
[13594]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]
[13583]50    private AdaptiveWalk walker;
51
52    [StorableConstructor]
[16728]53    private AdaptiveWalkCalculator(StorableConstructorFlag _) : base(_) { }
[13583]54    private AdaptiveWalkCalculator(AdaptiveWalkCalculator original, Cloner cloner)
55      : base(original, cloner) {
[13594]56      problem = cloner.Clone(original.problem);
[13583]57      walker = cloner.Clone(original.walker);
[13594]58      characteristics = cloner.Clone(original.characteristics);
[13583]59    }
[14776]60    public AdaptiveWalkCalculator() : this(new AdaptiveWalk()) { }
61    public AdaptiveWalkCalculator(AdaptiveWalk walker) {
[13583]62      Name = ItemName;
63      Description = ItemDescription;
[14776]64      this.walker = walker;
[13594]65      characteristics = new CheckedItemList<StringValue>(
66        new[] { "AutoCorrelation1", "CorrelationLength", "InformationContent",
[14776]67        "PartialInformationContent", "DensityBasinInformation", "InformationStability",
[13594]68        "Diversity", "Regularity", "TotalEntropy", "PeakInformationContent",
69        "PeakDensityBasinInformation" }.Select(x => new StringValue(x)));
[13583]70    }
71
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new AdaptiveWalkCalculator(this, cloner);
74    }
75
[13594]76    private CheckedItemList<StringValue> characteristics;
77    public ReadOnlyCheckedItemList<StringValue> Characteristics {
78      get { return characteristics.AsReadOnly(); }
[13583]79    }
80
[13594]81    public bool CanCalculate() {
82      return Problem is ISingleObjectiveHeuristicOptimizationProblem
83        && Problem.Operators.Any(x => x is IManipulator);
[13583]84    }
85
[13594]86    public IEnumerable<IResult> Calculate() {
[13583]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        walker.Start();
95        waitHandle.WaitOne();
96        walker.ExecutionStateChanged -= evHandle;
97      }
[13594]98      foreach (var p in characteristics.CheckedItems) {
99        yield return new Result("AdaptiveWalk." + walker.MutatorParameter.Value.Name + "." + p.Value.Value, walker.Results[p.Value.Value].Value);
[13583]100      }
101      walker.Prepare(true);
102    }
[13594]103
104    public void CollectParameterValues(IDictionary<string, IItem> values) {
105      walker.CollectParameterValues(values);
106    }
107
108    public IKeyedItemCollection<string, IParameter> Parameters {
109      get { return ((IParameterizedItem)walker).Parameters; }
110    }
[13583]111  }
112}
Note: See TracBrowser for help on using the repository browser.