1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Encodings.LinearLinkageEncoding;
|
---|
5 | using HeuristicLab.Random;
|
---|
6 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Tests.HeuristicLab.Encodings.LinearLinkageEncoding {
|
---|
9 | [TestClass]
|
---|
10 | public class LinearLinkageMoveTests {
|
---|
11 | [TestMethod]
|
---|
12 | public void TestApplyUndo() {
|
---|
13 | var random = new MersenneTwister(42);
|
---|
14 | for (var p = 0; p < 7; p++) {
|
---|
15 | var lle = ExactGroupsLinearLinkageCreator.Apply(random, 64, (int)Math.Pow(2, p));
|
---|
16 | var before = (LinearLinkage)lle.Clone();
|
---|
17 | var beforeb = before.ToBackLinks();
|
---|
18 | var moves = 0;
|
---|
19 | foreach (var move in MoveGenerator.Generate(lle)) {
|
---|
20 | var lleb = lle.ToBackLinks();
|
---|
21 | move.Apply(lle);
|
---|
22 | move.ApplyToLLEb(lleb);
|
---|
23 | Assert.IsFalse(before.SequenceEqual(lle));
|
---|
24 | Assert.IsFalse(lleb.SequenceEqual(beforeb));
|
---|
25 | Assert.IsTrue(lleb.SequenceEqual(lle.ToBackLinks()));
|
---|
26 | Assert.IsTrue(lle.SequenceEqual(LinearLinkage.FromBackLinks(lleb)));
|
---|
27 | move.Undo(lle);
|
---|
28 | Assert.IsTrue(before.SequenceEqual(lle));
|
---|
29 | moves++;
|
---|
30 | }
|
---|
31 | Assert.IsTrue(moves > 0);
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | [TestMethod]
|
---|
36 | public void TestApply() {
|
---|
37 | var random = new MersenneTwister(42);
|
---|
38 | for (var p = 0; p < 7; p++) {
|
---|
39 | var lle = ExactGroupsLinearLinkageCreator.Apply(random, 64, (int)Math.Pow(2, p));
|
---|
40 | var lleb = lle.ToBackLinks();
|
---|
41 | var groupItems = new List<int>() { 0 };
|
---|
42 | var moves = 0;
|
---|
43 | for (var i = 1; i < lle.Length; i++) {
|
---|
44 | var move = MoveGenerator.GenerateForItem(i, groupItems, lle, lleb).SampleRandom(random);
|
---|
45 | move.Apply(lle);
|
---|
46 | move.ApplyToLLEb(lleb);
|
---|
47 | Assert.IsTrue(lleb.SequenceEqual(lle.ToBackLinks()));
|
---|
48 | Assert.IsTrue(lle.SequenceEqual(LinearLinkage.FromBackLinks(lleb)));
|
---|
49 |
|
---|
50 | if (lleb[i] != i)
|
---|
51 | groupItems.Remove(lleb[i]);
|
---|
52 | groupItems.Add(i);
|
---|
53 |
|
---|
54 | moves++;
|
---|
55 | }
|
---|
56 | Assert.IsTrue(moves == lle.Length - 1);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|