Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.TabuSearch/3.3/TabuNeighborhoodAnalyzer.cs @ 11761

Last change on this file since 11761 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.TabuSearch {
32  [StorableClass]
33  [Item("TabuNeighborhoodAnalyzer", "Analyzes the tabu neighborhood")]
34  public class TabuNeighborhoodAnalyzer : SingleSuccessorOperator, IAnalyzer {
35    public virtual bool EnabledByDefault {
36      get { return true; }
37    }
38
39    public ScopeTreeLookupParameter<BoolValue> IsTabuParameter {
40      get { return (ScopeTreeLookupParameter<BoolValue>)Parameters["IsTabu"]; }
41    }
42    public LookupParameter<PercentValue> PercentTabuParameter {
43      get { return (LookupParameter<PercentValue>)Parameters["PercentTabu"]; }
44    }
45    public LookupParameter<ResultCollection> ResultsParameter {
46      get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
47    }
48
49    [StorableConstructor]
50    protected TabuNeighborhoodAnalyzer(bool deserializing) : base(deserializing) { }
51    protected TabuNeighborhoodAnalyzer(TabuNeighborhoodAnalyzer original, Cloner cloner)
52      : base(original, cloner) {
53    }
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new TabuNeighborhoodAnalyzer(this, cloner);
56    }
57    public TabuNeighborhoodAnalyzer()
58      : base() {
59      Parameters.Add(new ScopeTreeLookupParameter<BoolValue>("IsTabu", "A value that determines if a move is tabu or not."));
60      Parameters.Add(new LookupParameter<PercentValue>("PercentTabu", "Indicates how much of the neighborhood is tabu."));
61      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The result collection where the value should be stored."));
62    }
63
64    public override IOperation Apply() {
65      ItemArray<BoolValue> tabu = IsTabuParameter.ActualValue;
66      if (tabu.Length > 0) {
67        PercentValue value = PercentTabuParameter.ActualValue;
68        if (value == null) {
69          value = new PercentValue();
70          PercentTabuParameter.ActualValue = value;
71        }
72        value.Value = tabu.Where(x => x.Value).Count() / (double)tabu.Length;
73        ResultCollection results = ResultsParameter.ActualValue;
74        if (results != null) {
75          IResult result = null;
76          results.TryGetValue(PercentTabuParameter.ActualName, out result);
77          if (result != null)
78            result.Value = value;
79          else
80            results.Add(new Result(PercentTabuParameter.ActualName, "Indicates how much of the neighborhood is tabu.", (IItem)value.Clone()));
81        }
82      }
83      return base.Apply();
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.