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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace 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 | }
|
---|