1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
25 | namespace HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
26 | public static class MoveGenerator {
|
---|
27 | public static IEnumerable<Move> GenerateForItem(int i, List<int> groupItems, LinearLinkage lle, int[] lleb) {
|
---|
28 | var pred = lleb[i];
|
---|
29 | var next = lle[i];
|
---|
30 | var isFirst = pred == i;
|
---|
31 | var isLast = next == i;
|
---|
32 |
|
---|
33 | // First: shift i into each previous group
|
---|
34 | foreach (var m in groupItems.Where(x => lle[x] != i)) {
|
---|
35 | yield return new ShiftMove(i, pred, m, next, lle[m]);
|
---|
36 | }
|
---|
37 |
|
---|
38 | if (!isLast) {
|
---|
39 | // Second: split group at i
|
---|
40 | yield return new SplitMove(i);
|
---|
41 |
|
---|
42 | if (isFirst) {
|
---|
43 | // Third: merge with closed groups
|
---|
44 | foreach (var m in groupItems.Where(x => lle[x] == x)) {
|
---|
45 | yield return new MergeMove(i, m);
|
---|
46 | }
|
---|
47 | } else {
|
---|
48 | // Fourth: extract i into group of its own (exclude first and last, because of SplitMove)
|
---|
49 | yield return new ExtractMove(i, pred, next);
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | }
|
---|
55 |
|
---|
56 | public static IEnumerable<Move> Generate(LinearLinkage lle) {
|
---|
57 | var groupItems = new List<int>();
|
---|
58 | var lleb = lle.ToBackLinks();
|
---|
59 | for (var i = 0; i < lle.Length; i++) {
|
---|
60 | foreach (var move in GenerateForItem(i, groupItems, lle, lleb))
|
---|
61 | yield return move;
|
---|
62 | if (lleb[i] != i)
|
---|
63 | groupItems.Remove(lleb[i]);
|
---|
64 | groupItems.Add(i);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|