Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MemPRAlgorithm/HeuristicLab.Tests/HeuristicLab.Encodings.LinearLinkageEncoding-3.4/GroupCrossoverTest.cs @ 15728

Last change on this file since 15728 was 14475, checked in by sraggl, 8 years ago

#2666
Updated LinearLinkageEncoding by:

  • Speeding up GroupCrossover and SetGroups and GetGroups
  • making Constructor private adding static initializer Methods
File size: 2.9 KB
Line 
1using System;
2using HeuristicLab.Random;
3using HeuristicLab.Tests;
4using Microsoft.VisualStudio.TestTools.UnitTesting;
5
6namespace HeuristicLab.Encodings.LinearLinkageEncoding.Tests {
7  [TestClass()]
8  public class GroupCrossoverTest {
9    [TestMethod()]
10    [TestCategory("Encodings.LinearLinkage")]
11    [TestProperty("Time", "short")]
12    public void EqualParentsTest() {
13      var random = new FastRandom(0);
14      var parent = LinearLinkage.SingleElementGroups(10);
15      var child = GroupCrossover.Apply(random, parent, parent);
16      Assert.IsTrue(Auxiliary.LinearLinkageIsEqualByPosition(parent, child));
17
18      parent = RandomLinearLinkageCreator.Apply(random, 10);
19      child = GroupCrossover.Apply(random, parent, parent);
20      Assert.IsTrue(Auxiliary.LinearLinkageIsEqualByPosition(parent, child));
21    }
22
23    // Example from paper:
24    // Multi-objective Genetic Algorithms for grouping problems. Korkmaz, E.E. Applied Intelligence (2010) 33: 179.
25    [TestMethod()]
26    [TestCategory("Encodings.LinearLinkage")]
27    [TestProperty("Time", "short")]
28    public void ExampleFromPaperTest() {
29      var parent1 = LinearLinkage.FromEndLinks(new[] { 3, 4, 3, 3, 4 });
30
31      var parent2 = LinearLinkage.FromEndLinks(new[] { 2, 2, 2, 4, 4 });
32
33      CheckGroupCrossover(parent1, parent2, new[] { 3, 4, 2, 3, 4 }, new[] { 0.0, 0.0, 0.0, 0.0 }); // (i)
34      CheckGroupCrossover(parent1, parent2, new[] { 2, 4, 2, 3, 4 }, new[] { 0.0, 0.0, 0.0, 0.9 }); // (iv)
35      CheckGroupCrossover(parent1, parent2, new[] { 3, 2, 2, 3, 4 }, new[] { 0.0, 0.0, 0.9, 0.0 }); // (iii)
36      CheckGroupCrossover(parent1, parent2, new[] { 2, 2, 2, 3, 4 }, new[] { 0.0, 0.0, 0.9, 0.9 }); // (ii)
37      CheckGroupCrossover(parent1, parent2, new[] { 3, 4, 3, 3, 4 }, new[] { 0.0, 0.9 });
38      CheckGroupCrossover(parent1, parent2, new[] { 2, 4, 2, 4, 4 }, new[] { 0.9, 0.0, 0.0 });
39      CheckGroupCrossover(parent1, parent2, new[] { 2, 2, 2, 4, 4 }, new[] { 0.9, 0.0, 0.9 });
40      CheckGroupCrossover(parent1, parent2, new[] { 4, 4, 4, 4, 4 }, new[] { 0.9, 0.9, 0.0, 0.0 });
41      CheckGroupCrossover(parent1, parent2, new[] { 4, 4, 4, 4, 4 }, new[] { 0.9, 0.9, 0.0, 0.9 });
42      CheckGroupCrossover(parent1, parent2, new[] { 2, 4, 2, 4, 4 }, new[] { 0.9, 0.9, 0.9, 0.0 });
43      CheckGroupCrossover(parent1, parent2, new[] { 4, 4, 2, 4, 4 }, new[] { 0.9, 0.9, 0.9, 0.9 });
44    }
45
46    private void CheckGroupCrossover(LinearLinkage parent1, LinearLinkage parent2, int[] expectedllee, double[] randomNumbers) {
47      var expected = LinearLinkage.FromEndLinks(expectedllee);
48      var random = new TestRandom() { DoubleNumbers = randomNumbers };
49      var child = GroupCrossover.Apply(random, parent1, parent2);
50      Assert.IsTrue(Auxiliary.LinearLinkageIsEqualByPosition(expected, child), "Expected [{0}] but was [{1}]!", string.Join(", ", expected), string.Join(", ", child));
51    }
52  }
53}
Note: See TracBrowser for help on using the repository browser.