Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.TabuSearch/3.3/TabuNeighborhoodAnalyzer.cs @ 6762

Last change on this file since 6762 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 ScopeTreeLookupParameter<BoolValue> IsTabuParameter {
36      get { return (ScopeTreeLookupParameter<BoolValue>)Parameters["IsTabu"]; }
37    }
38    public LookupParameter<PercentValue> PercentTabuParameter {
39      get { return (LookupParameter<PercentValue>)Parameters["PercentTabu"]; }
40    }
41    public LookupParameter<ResultCollection> ResultsParameter {
42      get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
43    }
44
45    [StorableConstructor]
46    protected TabuNeighborhoodAnalyzer(bool deserializing) : base(deserializing) { }
47    protected TabuNeighborhoodAnalyzer(TabuNeighborhoodAnalyzer original, Cloner cloner)
48      : base(original, cloner) {
49    }
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new TabuNeighborhoodAnalyzer(this, cloner);
52    }
53    public TabuNeighborhoodAnalyzer()
54      : base() {
55      Parameters.Add(new ScopeTreeLookupParameter<BoolValue>("IsTabu", "A value that determines if a move is tabu or not."));
56      Parameters.Add(new LookupParameter<PercentValue>("PercentTabu", "Indicates how much of the neighborhood is tabu."));
57      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The result collection where the value should be stored."));
58    }
59
60    public override IOperation Apply() {
61      ItemArray<BoolValue> tabu = IsTabuParameter.ActualValue;
62      if (tabu.Length > 0) {
63        PercentValue value = PercentTabuParameter.ActualValue;
64        if (value == null) {
65          value = new PercentValue();
66          PercentTabuParameter.ActualValue = value;
67        }
68        value.Value = tabu.Where(x => x.Value).Count() / (double)tabu.Length;
69        ResultCollection results = ResultsParameter.ActualValue;
70        if (results != null) {
71          IResult result = null;
72          results.TryGetValue(PercentTabuParameter.ActualName, out result);
73          if (result != null)
74            result.Value = value;
75          else
76            results.Add(new Result(PercentTabuParameter.ActualName, "Indicates how much of the neighborhood is tabu.", (IItem)value.Clone()));
77        }
78      }
79      return base.Apply();
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.