Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LinearLinkage/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Manipulators/SwapNManipulator.cs @ 12285

Last change on this file since 12285 was 12285, checked in by abeham, 9 years ago

#2319: Added branch for linear linkage encoding (LLE-e)

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Encodings.LinearLinkageEncoding {
33  [Item("Swap-n Manipulator", "")]
34  [StorableClass]
35  public sealed class SwapNManipulator : InstrumentedOperator, ILinearLinkageManipulator, IStochasticOperator {
36    public ILookupParameter<IRandom> RandomParameter {
37      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
38    }
39
40    public ILookupParameter<LinearLinkage> LLEParameter {
41      get { return (ILookupParameter<LinearLinkage>)Parameters["LLE"]; }
42    }
43
44    public IValueLookupParameter<IntValue> NParameter {
45      get { return (IValueLookupParameter<IntValue>)Parameters["N"]; }
46    }
47
48    [StorableConstructor]
49    private SwapNManipulator(bool deserializing) : base(deserializing) { }
50    private SwapNManipulator(SwapNManipulator original, Cloner cloner) : base(original, cloner) { }
51    public SwapNManipulator() {
52      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
53      Parameters.Add(new LookupParameter<LinearLinkage>("LLE", "The encoding vector that is to be manipulated."));
54      Parameters.Add(new ValueLookupParameter<IntValue>("N", "The number of swaps to perform.", new IntValue(2)));
55    }
56    public SwapNManipulator(int n)
57      : this() {
58      NParameter.Value = new IntValue(n);
59    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new SwapNManipulator(this, cloner);
63    }
64
65    public override IOperation InstrumentedApply() {
66      var random = RandomParameter.ActualValue;
67      var vector = LLEParameter.ActualValue;
68      var N = NParameter.ActualValue.Value;
69
70      var grouping = vector.GetGroups().Select(x => x.ToList()).ToList();
71      if (grouping.Count == 1) return base.InstrumentedApply(); // nothing can be changed
72      var touched = new HashSet<int>();
73      var prevGroup = random.Next(grouping.Count);
74      var prevItem = random.Next(grouping[prevGroup].Count);
75      touched.Add(grouping[prevGroup][prevItem]);
76      for (var i = 0; i < N; i++) {
77        int nextGroup = -1, nextItem = -1, tries = 0;
78        do {
79          if (tries >= 100) break;
80          nextGroup = random.Next(grouping.Count);
81          nextItem = random.Next(grouping[nextGroup].Count);
82          tries++;
83        } while (nextGroup == prevGroup || touched.Contains(grouping[nextGroup][nextItem]));
84        if (tries >= 100) break;
85        var h = grouping[nextGroup][nextItem];
86        grouping[nextGroup][nextItem] = grouping[prevGroup][prevItem];
87        grouping[prevGroup][prevItem] = h;
88        touched.Add(h);
89        prevGroup = nextGroup;
90        prevItem = nextItem;
91      }
92      vector.SetGroups(grouping);
93      return base.InstrumentedApply();
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.