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 | using HeuristicLab.Collections;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
27 | public static class MoveGenerator {
|
---|
28 | public static IEnumerable<Move> GenerateForItem(int i, int next, BidirectionalDictionary<int, int> links) {
|
---|
29 | var pred = -1;
|
---|
30 | var isFirst = !links.TryGetBySecond(i, out pred);
|
---|
31 | var isLast = next == i;
|
---|
32 |
|
---|
33 | // First: shift i into each previous group
|
---|
34 | foreach (var l in links.Where(x => x.Value != i)) {
|
---|
35 | yield return new ShiftMove(i, isFirst ? i : pred, l.Key, next, l.Value);
|
---|
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 l in links.Where(x => x.Key == x.Value)) {
|
---|
45 | yield return new MergeMove(i, l.Key);
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 | if (!isFirst)
|
---|
50 | // Fourth: extract i into group of its own (exclude first, because of Second)
|
---|
51 | yield return new ExtractMove(i, pred, next);
|
---|
52 |
|
---|
53 | }
|
---|
54 |
|
---|
55 | public static IEnumerable<Move> Generate(LinearLinkage lle) {
|
---|
56 | var links = new BidirectionalDictionary<int, int>();
|
---|
57 | for (var i = 0; i < lle.Length; i++) {
|
---|
58 | foreach (var move in GenerateForItem(i, lle[i], links))
|
---|
59 | yield return move;
|
---|
60 | links.RemoveBySecond(i);
|
---|
61 | links.Add(i, lle[i]);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|