1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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.Parameters;
|
---|
28 | using HEAL.Attic;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
31 | [Item("Move Item Manipulator", "Performs a maximum of N move operations between groups or to new groups. An already moved item may be moved again.")]
|
---|
32 | [StorableType("26B371F4-8E37-406A-AC5A-3E7734087890")]
|
---|
33 | public sealed class MoveItemManipulator : LinearLinkageManipulator {
|
---|
34 |
|
---|
35 | public IValueLookupParameter<IntValue> NParameter {
|
---|
36 | get { return (IValueLookupParameter<IntValue>)Parameters["N"]; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | [StorableConstructor]
|
---|
40 | private MoveItemManipulator(StorableConstructorFlag _) : base(_) { }
|
---|
41 | private MoveItemManipulator(MoveItemManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
42 | public MoveItemManipulator() {
|
---|
43 | Parameters.Add(new ValueLookupParameter<IntValue>("N", "The number of items to move.", new IntValue(1)));
|
---|
44 | }
|
---|
45 | public MoveItemManipulator(int n)
|
---|
46 | : this() {
|
---|
47 | NParameter.Value = new IntValue(n);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
51 | return new MoveItemManipulator(this, cloner);
|
---|
52 | }
|
---|
53 |
|
---|
54 | public static void Apply(IRandom random, LinearLinkage lle, int n) {
|
---|
55 | var grouping = lle.GetGroups().ToList();
|
---|
56 | grouping.Add(new List<int>()); // add an empty group
|
---|
57 |
|
---|
58 | for (var i = 0; i < n; i++) {
|
---|
59 | var src = random.Next(grouping.Count - 1); // only select from non-empty groups
|
---|
60 | var dest = random.Next(grouping.Count);
|
---|
61 | while (src == dest || grouping[src].Count == 1 && dest == grouping.Count - 1) {
|
---|
62 | src = random.Next(grouping.Count - 1);
|
---|
63 | dest = random.Next(grouping.Count);
|
---|
64 | }
|
---|
65 | if (dest == grouping.Count - 1) grouping.Add(new List<int>());
|
---|
66 |
|
---|
67 | var e = random.Next(grouping[src].Count);
|
---|
68 | grouping[dest].Add(grouping[src][e]);
|
---|
69 | grouping[src].RemoveAt(e);
|
---|
70 | if (grouping[src].Count == 0)
|
---|
71 | grouping.RemoveAt(src);
|
---|
72 | }
|
---|
73 |
|
---|
74 | lle.SetGroups(grouping.Where(x => x.Count > 0));
|
---|
75 | }
|
---|
76 |
|
---|
77 | protected override void Manipulate(IRandom random, LinearLinkage lle) {
|
---|
78 | var N = NParameter.ActualValue.Value;
|
---|
79 | Apply(random, lle, N);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|