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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Optimization;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using System;
|
---|
28 | using System.Collections.Generic;
|
---|
29 | using System.Linq;
|
---|
30 | using System.Threading;
|
---|
31 |
|
---|
32 | namespace 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() {
|
---|
61 | Name = ItemName;
|
---|
62 | Description = ItemDescription;
|
---|
63 | walker = new RandomWalk();
|
---|
64 | characteristics = new CheckedItemList<StringValue>(
|
---|
65 | new[] { "AutoCorrelation1", "CorrelationLength", "InformationContent",
|
---|
66 | "PartialInformationContent", "DensityBasinInformation", "InformationStability",
|
---|
67 | "Diversity", "Regularity", "TotalEntropy", "PeakInformationContent",
|
---|
68 | "PeakDensityBasinInformation" }.Select(x => new StringValue(x)));
|
---|
69 | }
|
---|
70 |
|
---|
71 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
72 | return new RandomWalkCalculator(this, cloner);
|
---|
73 | }
|
---|
74 |
|
---|
75 | private CheckedItemList<StringValue> characteristics;
|
---|
76 | public ReadOnlyCheckedItemList<StringValue> Characteristics {
|
---|
77 | get { return characteristics.AsReadOnly(); }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public bool CanCalculate() {
|
---|
81 | return Problem is ISingleObjectiveHeuristicOptimizationProblem
|
---|
82 | && Problem.Operators.Any(x => x is IManipulator);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public IEnumerable<IResult> Calculate() {
|
---|
86 | walker.Prepare(true);
|
---|
87 | using (var waitHandle = new AutoResetEvent(false)) {
|
---|
88 | EventHandler evHandle = (sender, e) => {
|
---|
89 | if (walker.ExecutionState == ExecutionState.Paused
|
---|
90 | || walker.ExecutionState == ExecutionState.Stopped) waitHandle.Set();
|
---|
91 | };
|
---|
92 | walker.ExecutionStateChanged += evHandle;
|
---|
93 | walker.Start();
|
---|
94 | waitHandle.WaitOne();
|
---|
95 | walker.ExecutionStateChanged -= evHandle;
|
---|
96 | }
|
---|
97 | foreach (var p in characteristics.CheckedItems) {
|
---|
98 | yield return new Result("RandomWalk." + walker.MutatorParameter.Value.Name + "." + p.Value.Value, walker.Results[p.Value.Value].Value);
|
---|
99 | }
|
---|
100 | walker.Prepare(true);
|
---|
101 | }
|
---|
102 |
|
---|
103 | public void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
104 | walker.CollectParameterValues(values);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public IKeyedItemCollection<string, IParameter> Parameters {
|
---|
108 | get { return ((IParameterizedItem)walker).Parameters; }
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|