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.Optimization;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 | using System;
|
---|
27 | using System.Collections.Generic;
|
---|
28 | using System.Linq;
|
---|
29 | using System.Threading;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Analysis.FitnessLandscape {
|
---|
32 | [Item("Adaptive Walk Calculator", "Calculates characteristics from an adaptive walk.")]
|
---|
33 | [StorableClass]
|
---|
34 | public class AdaptiveWalkCalculator : NamedItem, ICharacteristicCalculator {
|
---|
35 | [Storable]
|
---|
36 | private AdaptiveWalk walker;
|
---|
37 |
|
---|
38 | [StorableConstructor]
|
---|
39 | private AdaptiveWalkCalculator(bool deserializing) : base(deserializing) { }
|
---|
40 | private AdaptiveWalkCalculator(AdaptiveWalkCalculator original, Cloner cloner)
|
---|
41 | : base(original, cloner) {
|
---|
42 | walker = cloner.Clone(original.walker);
|
---|
43 | }
|
---|
44 | public AdaptiveWalkCalculator() {
|
---|
45 | Name = ItemName;
|
---|
46 | Description = ItemDescription;
|
---|
47 | walker = new AdaptiveWalk();
|
---|
48 | }
|
---|
49 |
|
---|
50 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
51 | return new AdaptiveWalkCalculator(this, cloner);
|
---|
52 | }
|
---|
53 |
|
---|
54 | public IEnumerable<string> Characteristics {
|
---|
55 | get {
|
---|
56 | return new[] { "AutoCorrelation1", "CorrelationLength", "InformationContent",
|
---|
57 | "PartialInformationContent", "DensityBasinInformation", "InformationStability",
|
---|
58 | "Diversity", "Regularity", "TotalEntropy", "PeakInformationContent",
|
---|
59 | "PeakDensityBasinInformation" };
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public bool CanCalculate(IProblem problem) {
|
---|
64 | return problem is ISingleObjectiveHeuristicOptimizationProblem
|
---|
65 | && problem.Operators.Any(x => x is IManipulator);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public IEnumerable<KeyValuePair<string, IItem>> Calculate(IProblem problem, IEnumerable<string> characteristics = null) {
|
---|
69 | var soProb = (ISingleObjectiveHeuristicOptimizationProblem)problem;
|
---|
70 | walker.Problem = soProb;
|
---|
71 | walker.Prepare(true);
|
---|
72 | using (var waitHandle = new AutoResetEvent(false)) {
|
---|
73 | EventHandler evHandle = (sender, e) => {
|
---|
74 | if (walker.ExecutionState == ExecutionState.Paused
|
---|
75 | || walker.ExecutionState == ExecutionState.Stopped) waitHandle.Set();
|
---|
76 | };
|
---|
77 | walker.ExecutionStateChanged += evHandle;
|
---|
78 | walker.Start();
|
---|
79 | waitHandle.WaitOne();
|
---|
80 | walker.ExecutionStateChanged -= evHandle;
|
---|
81 | }
|
---|
82 | var props = (characteristics ?? Characteristics).ToList();
|
---|
83 | foreach (var p in props) {
|
---|
84 | yield return new KeyValuePair<string, IItem>(p, walker.Results[p].Value);
|
---|
85 | }
|
---|
86 | walker.Prepare(true);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|