[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 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
[13594] | 24 | using HeuristicLab.Data;
|
---|
[13583] | 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("Up/Down Walk Calculator", "Calculates characteristics from an up/down walk.")]
|
---|
| 34 | [StorableClass]
|
---|
| 35 | public class UpDownWalkCalculator : 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 UpDownWalk walker;
|
---|
| 51 |
|
---|
| 52 | [StorableConstructor]
|
---|
| 53 | private UpDownWalkCalculator(bool deserializing) : base(deserializing) { }
|
---|
| 54 | private UpDownWalkCalculator(UpDownWalkCalculator 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 | }
|
---|
[15031] | 60 | public UpDownWalkCalculator() : this(new UpDownWalk()) { }
|
---|
| 61 | public UpDownWalkCalculator(UpDownWalk walker) {
|
---|
[13583] | 62 | Name = ItemName;
|
---|
| 63 | Description = ItemDescription;
|
---|
[15031] | 64 | this.walker = walker;
|
---|
[13594] | 65 | characteristics = new CheckedItemList<StringValue>(
|
---|
| 66 | new[] { "AutoCorrelation1", "CorrelationLength", "InformationContent",
|
---|
| 67 | "PartialInformationContent", "DensityBasinInformation", "InformationStability",
|
---|
| 68 | "Diversity", "Regularity", "TotalEntropy", "PeakInformationContent",
|
---|
| 69 | "PeakDensityBasinInformation", "DownWalkLength", "UpWalkLength", "UpWalkLenVar",
|
---|
| 70 | "DownWalkLenVar", "LowerVariance", "UpperVariance" }.Select(x => new StringValue(x)));
|
---|
[13583] | 71 | }
|
---|
| 72 |
|
---|
| 73 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 74 | return new UpDownWalkCalculator(this, cloner);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[13594] | 77 | private CheckedItemList<StringValue> characteristics;
|
---|
| 78 | public ReadOnlyCheckedItemList<StringValue> Characteristics {
|
---|
| 79 | get { return characteristics.AsReadOnly(); }
|
---|
[13583] | 80 | }
|
---|
| 81 |
|
---|
[13594] | 82 | public bool CanCalculate() {
|
---|
| 83 | return Problem is ISingleObjectiveHeuristicOptimizationProblem
|
---|
| 84 | && Problem.Operators.Any(x => x is IManipulator);
|
---|
[13583] | 85 | }
|
---|
| 86 |
|
---|
[13594] | 87 | public IEnumerable<IResult> Calculate() {
|
---|
[13583] | 88 | walker.Prepare(true);
|
---|
| 89 | using (var waitHandle = new AutoResetEvent(false)) {
|
---|
| 90 | EventHandler evHandle = (sender, e) => {
|
---|
| 91 | if (walker.ExecutionState == ExecutionState.Paused
|
---|
| 92 | || walker.ExecutionState == ExecutionState.Stopped) waitHandle.Set();
|
---|
| 93 | };
|
---|
| 94 | walker.ExecutionStateChanged += evHandle;
|
---|
| 95 | walker.Start();
|
---|
| 96 | waitHandle.WaitOne();
|
---|
| 97 | walker.ExecutionStateChanged -= evHandle;
|
---|
| 98 | }
|
---|
[13594] | 99 | foreach (var p in characteristics.CheckedItems) {
|
---|
| 100 | yield return new Result("UpDownWalk." + walker.MutatorParameter.Value.Name + "." + p.Value.Value, walker.Results[p.Value.Value].Value);
|
---|
[13583] | 101 | }
|
---|
| 102 | walker.Prepare(true);
|
---|
| 103 | }
|
---|
[13594] | 104 |
|
---|
| 105 | public void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
| 106 | walker.CollectParameterValues(values);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | public IKeyedItemCollection<string, IParameter> Parameters {
|
---|
| 110 | get { return ((IParameterizedItem)walker).Parameters; }
|
---|
| 111 | }
|
---|
[13583] | 112 | }
|
---|
| 113 | }
|
---|