[14663] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[14663] | 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;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Random;
|
---|
| 26 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Encodings.LinearLinkageEncoding.Tests {
|
---|
| 29 | [TestClass]
|
---|
| 30 | public class LinearLinkageMoveTests {
|
---|
| 31 | [TestMethod]
|
---|
| 32 | [TestCategory("Encodings.LinearLinkage")]
|
---|
| 33 | [TestProperty("Time", "short")]
|
---|
| 34 | public void TestLinearLinkageMoveApplyUndo() {
|
---|
| 35 | var random = new MersenneTwister(42);
|
---|
| 36 | for (var p = 0; p < 7; p++) {
|
---|
| 37 | var lle = ExactGroupsLinearLinkageCreator.Apply(random, 64, (int)Math.Pow(2, p));
|
---|
| 38 | var before = (LinearLinkage)lle.Clone();
|
---|
| 39 | var beforeb = before.ToBackLinks();
|
---|
| 40 | var moves = 0;
|
---|
[15217] | 41 | foreach (var move in ExhaustiveEMSSMoveGenerator.Generate(lle)) {
|
---|
[14663] | 42 | var lleb = lle.ToBackLinks();
|
---|
| 43 | move.Apply(lle);
|
---|
| 44 | move.ApplyToLLEb(lleb);
|
---|
| 45 | Assert.IsFalse(before.SequenceEqual(lle));
|
---|
| 46 | Assert.IsFalse(lleb.SequenceEqual(beforeb));
|
---|
| 47 | Assert.IsTrue(lleb.SequenceEqual(lle.ToBackLinks()));
|
---|
| 48 | Assert.IsTrue(lle.SequenceEqual(LinearLinkage.FromBackLinks(lleb)));
|
---|
| 49 | move.Undo(lle);
|
---|
| 50 | Assert.IsTrue(before.SequenceEqual(lle));
|
---|
| 51 | moves++;
|
---|
| 52 | }
|
---|
| 53 | Assert.IsTrue(moves > 0);
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | [TestMethod]
|
---|
| 58 | [TestCategory("Encodings.LinearLinkage")]
|
---|
| 59 | [TestProperty("Time", "short")]
|
---|
| 60 | public void TestLinearLinkageMoveApply() {
|
---|
| 61 | var random = new MersenneTwister(42);
|
---|
| 62 | for (var p = 0; p < 7; p++) {
|
---|
| 63 | var lle = ExactGroupsLinearLinkageCreator.Apply(random, 64, (int)Math.Pow(2, p));
|
---|
| 64 | var lleb = lle.ToBackLinks();
|
---|
| 65 | var groupItems = new List<int>() { 0 };
|
---|
| 66 | var moves = 0;
|
---|
| 67 | for (var i = 1; i < lle.Length; i++) {
|
---|
[15217] | 68 | var move = ExhaustiveEMSSMoveGenerator.GenerateForItem(i, groupItems, lle, lleb).SampleRandom(random);
|
---|
[14663] | 69 | move.Apply(lle);
|
---|
| 70 | move.ApplyToLLEb(lleb);
|
---|
| 71 | Assert.IsTrue(lleb.SequenceEqual(lle.ToBackLinks()));
|
---|
| 72 | Assert.IsTrue(lle.SequenceEqual(LinearLinkage.FromBackLinks(lleb)));
|
---|
| 73 |
|
---|
| 74 | if (lleb[i] != i)
|
---|
| 75 | groupItems.Remove(lleb[i]);
|
---|
| 76 | groupItems.Add(i);
|
---|
| 77 |
|
---|
| 78 | moves++;
|
---|
| 79 | }
|
---|
| 80 | Assert.IsTrue(moves == lle.Length - 1);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|