Skip to content

Commit

Permalink
reverted
Browse files Browse the repository at this point in the history
  • Loading branch information
avbasak1 committed Sep 24, 2021
1 parent f2c9b21 commit e615725
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
Expand Up @@ -67,7 +67,7 @@ protected void checkValidity(List<Integer> chromosomeRepresentation) throws Inva
*/
public static List<Integer> randomBinaryRepresentation(int length) {
// random binary list
final List<Integer> rList= new ArrayList<> (length);
List<Integer> rList= new ArrayList<> (length);
for (int j=0; j<length; j++) {
rList.add(GeneticAlgorithm.getRandomGenerator().nextInt(2));
}
Expand All @@ -81,7 +81,7 @@ protected boolean isSame(Chromosome another) {
if (! (another instanceof BinaryChromosome)) {
return false;
}
final BinaryChromosome anotherBc = (BinaryChromosome) another;
BinaryChromosome anotherBc = (BinaryChromosome) another;
// size check
if (getLength() != anotherBc.getLength()) {
return false;
Expand Down
Expand Up @@ -25,23 +25,20 @@ public class CycleCrossoverTest {

@Test
public void testCrossoverExample() {
// taken from
// http://www.rubicite.com/Tutorials/GeneticAlgorithms/CrossoverOperators/CycleCrossoverOperator.aspx
final Integer[] p1 = new Integer[] {8, 4, 7, 3, 6, 2, 5, 1, 9, 0};
final Integer[] p2 = new Integer[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// taken from http://www.rubicite.com/Tutorials/GeneticAlgorithms/CrossoverOperators/CycleCrossoverOperator.aspx
final Integer[] p1 = new Integer[] { 8, 4, 7, 3, 6, 2, 5, 1, 9, 0 };
final Integer[] p2 = new Integer[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
final DummyListChromosome p1c = new DummyListChromosome(p1);
final DummyListChromosome p2c = new DummyListChromosome(p2);

final CrossoverPolicy cp = new CycleCrossover<Integer>();
final ChromosomePair pair = cp.crossover(p1c, p2c);

final Integer[] c1 = ((DummyListChromosome) pair.getFirst()).getRepresentation()
.toArray(new Integer[p1.length]);
final Integer[] c2 = ((DummyListChromosome) pair.getSecond()).getRepresentation()
.toArray(new Integer[p2.length]);
final Integer[] c1 = ((DummyListChromosome) pair.getFirst()).getRepresentation().toArray(new Integer[p1.length]);
final Integer[] c2 = ((DummyListChromosome) pair.getSecond()).getRepresentation().toArray(new Integer[p2.length]);

final Integer[] c1e = new Integer[] {8, 1, 2, 3, 4, 5, 6, 7, 9, 0};
final Integer[] c2e = new Integer[] {0, 4, 7, 3, 6, 2, 5, 1, 8, 9};
final Integer[] c1e = new Integer[] { 8, 1, 2, 3, 4, 5, 6, 7, 9, 0 };
final Integer[] c2e = new Integer[] { 0, 4, 7, 3, 6, 2, 5, 1, 8, 9 };

Assert.assertArrayEquals(c1e, c1);
Assert.assertArrayEquals(c2e, c2);
Expand All @@ -50,30 +47,28 @@ public void testCrossoverExample() {
@Test
public void testCrossoverExample2() {
// taken from http://www.scribd.com/doc/54206412/32/Cycle-crossover
final Integer[] p1 = new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
final Integer[] p2 = new Integer[] {9, 3, 7, 8, 2, 6, 5, 1, 4};
final Integer[] p1 = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
final Integer[] p2 = new Integer[] { 9, 3, 7, 8, 2, 6, 5, 1, 4};
final DummyListChromosome p1c = new DummyListChromosome(p1);
final DummyListChromosome p2c = new DummyListChromosome(p2);

final CrossoverPolicy cp = new CycleCrossover<Integer>();
final ChromosomePair pair = cp.crossover(p1c, p2c);

final Integer[] c1 = ((DummyListChromosome) pair.getFirst()).getRepresentation()
.toArray(new Integer[p1.length]);
final Integer[] c2 = ((DummyListChromosome) pair.getSecond()).getRepresentation()
.toArray(new Integer[p2.length]);
final Integer[] c1 = ((DummyListChromosome) pair.getFirst()).getRepresentation().toArray(new Integer[p1.length]);
final Integer[] c2 = ((DummyListChromosome) pair.getSecond()).getRepresentation().toArray(new Integer[p2.length]);

final Integer[] c1e = new Integer[] {1, 3, 7, 4, 2, 6, 5, 8, 9};
final Integer[] c2e = new Integer[] {9, 2, 3, 8, 5, 6, 7, 1, 4};
final Integer[] c1e = new Integer[] { 1, 3, 7, 4, 2, 6, 5, 8, 9 };
final Integer[] c2e = new Integer[] { 9, 2, 3, 8, 5, 6, 7, 1, 4 };

Assert.assertArrayEquals(c1e, c1);
Assert.assertArrayEquals(c2e, c2);
}

@Test
public void testCrossover() {
final Integer[] p1 = new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
final Integer[] p2 = new Integer[] {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
final Integer[] p1 = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
final Integer[] p2 = new Integer[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
final DummyListChromosome p1c = new DummyListChromosome(p1);
final DummyListChromosome p2c = new DummyListChromosome(p2);

Expand All @@ -82,10 +77,8 @@ public void testCrossover() {
for (int i = 0; i < 20; i++) {
final ChromosomePair pair = cp.crossover(p1c, p2c);

final Integer[] c1 = ((DummyListChromosome) pair.getFirst()).getRepresentation()
.toArray(new Integer[p1.length]);
final Integer[] c2 = ((DummyListChromosome) pair.getSecond()).getRepresentation()
.toArray(new Integer[p2.length]);
final Integer[] c1 = ((DummyListChromosome) pair.getFirst()).getRepresentation().toArray(new Integer[p1.length]);
final Integer[] c2 = ((DummyListChromosome) pair.getSecond()).getRepresentation().toArray(new Integer[p2.length]);

int index = 0;
// Determine if it is in the same spot as in the first parent, if
Expand Down Expand Up @@ -114,8 +107,8 @@ public void testCrossover() {

@Test(expected = DimensionMismatchException.class)
public void testCrossoverDimensionMismatchException() {
final Integer[] p1 = new Integer[] {1, 0, 1, 0, 0, 1, 0, 1, 1};
final Integer[] p2 = new Integer[] {0, 1, 1, 0, 1};
final Integer[] p1 = new Integer[] { 1, 0, 1, 0, 0, 1, 0, 1, 1 };
final Integer[] p2 = new Integer[] { 0, 1, 1, 0, 1 };

final BinaryChromosome p1c = new DummyBinaryChromosome(p1);
final BinaryChromosome p2c = new DummyBinaryChromosome(p2);
Expand All @@ -126,7 +119,7 @@ public void testCrossoverDimensionMismatchException() {

@Test(expected = MathIllegalArgumentException.class)
public void testCrossoverInvalidFixedLengthChromosomeFirst() {
final Integer[] p1 = new Integer[] {1, 0, 1, 0, 0, 1, 0, 1, 1};
final Integer[] p1 = new Integer[] { 1, 0, 1, 0, 0, 1, 0, 1, 1 };
final BinaryChromosome p1c = new DummyBinaryChromosome(p1);
final Chromosome p2c = new Chromosome() {
@Override
Expand All @@ -142,7 +135,7 @@ public double fitness() {

@Test(expected = MathIllegalArgumentException.class)
public void testCrossoverInvalidFixedLengthChromosomeSecond() {
final Integer[] p1 = new Integer[] {1, 0, 1, 0, 0, 1, 0, 1, 1};
final Integer[] p1 = new Integer[] { 1, 0, 1, 0, 0, 1, 0, 1, 1 };
final BinaryChromosome p2c = new DummyBinaryChromosome(p1);
final Chromosome p1c = new Chromosome() {
@Override
Expand Down

0 comments on commit e615725

Please sign in to comment.