[12285] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16565] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12285] | 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.Linq;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
[16565] | 27 | using HEAL.Attic;
|
---|
[12285] | 28 |
|
---|
| 29 | namespace HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
[12396] | 30 | [Item("Swap Item Manipulator", "Performs N swaps operations of two items each. The same items may be swapped multiple times, at least two groups need to be present.")]
|
---|
[16565] | 31 | [StorableType("21F18233-0752-40A2-8B9A-6E6A9E2CE456")]
|
---|
[12286] | 32 | public sealed class SwapItemManipulator : LinearLinkageManipulator {
|
---|
[12285] | 33 |
|
---|
| 34 | public IValueLookupParameter<IntValue> NParameter {
|
---|
| 35 | get { return (IValueLookupParameter<IntValue>)Parameters["N"]; }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | [StorableConstructor]
|
---|
[16565] | 39 | private SwapItemManipulator(StorableConstructorFlag _) : base(_) { }
|
---|
[12286] | 40 | private SwapItemManipulator(SwapItemManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 41 | public SwapItemManipulator() {
|
---|
| 42 | Parameters.Add(new ValueLookupParameter<IntValue>("N", "The number of swaps to perform.", new IntValue(1)));
|
---|
[12285] | 43 | }
|
---|
[12286] | 44 | public SwapItemManipulator(int n)
|
---|
[12285] | 45 | : this() {
|
---|
| 46 | NParameter.Value = new IntValue(n);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[12286] | 50 | return new SwapItemManipulator(this, cloner);
|
---|
[12285] | 51 | }
|
---|
| 52 |
|
---|
[12286] | 53 | public static void Apply(IRandom random, LinearLinkage lle, int n) {
|
---|
| 54 | var grouping = lle.GetGroups().Select(x => x.ToList()).ToList();
|
---|
| 55 | if (grouping.Count == 1) return; // nothing can be changed
|
---|
[12285] | 56 |
|
---|
| 57 | var prevGroup = random.Next(grouping.Count);
|
---|
| 58 | var prevItem = random.Next(grouping[prevGroup].Count);
|
---|
[12286] | 59 | for (var i = 0; i < n; i++) {
|
---|
| 60 | int nextGroup, nextItem;
|
---|
[12285] | 61 | do {
|
---|
| 62 | nextGroup = random.Next(grouping.Count);
|
---|
| 63 | nextItem = random.Next(grouping[nextGroup].Count);
|
---|
[12286] | 64 | } while (nextGroup == prevGroup);
|
---|
[12285] | 65 | var h = grouping[nextGroup][nextItem];
|
---|
| 66 | grouping[nextGroup][nextItem] = grouping[prevGroup][prevItem];
|
---|
| 67 | grouping[prevGroup][prevItem] = h;
|
---|
| 68 | prevGroup = nextGroup;
|
---|
| 69 | prevItem = nextItem;
|
---|
| 70 | }
|
---|
[12286] | 71 |
|
---|
| 72 | lle.SetGroups(grouping);
|
---|
[12285] | 73 | }
|
---|
[12286] | 74 |
|
---|
| 75 | protected override void Manipulate(IRandom random, LinearLinkage lle) {
|
---|
| 76 | var N = NParameter.ActualValue.Value;
|
---|
| 77 | Apply(random, lle, N);
|
---|
| 78 | }
|
---|
[12285] | 79 | }
|
---|
| 80 | }
|
---|