alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Math example source code file (OnePointCrossoverTest.java)

This example Commons Math source code file (OnePointCrossoverTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Math tags/keywords

binarychromosome, binarychromosome, chromosomepair, dummybinarychromosome, dummybinarychromosome, integer, integer, onepointcrossover, onepointcrossover, onepointcrossovertest, test

The Commons Math OnePointCrossoverTest.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.math.genetics;

import static org.junit.Assert.*;
import org.junit.Test;

public class OnePointCrossoverTest {

    @Test
    public void testCrossover() {
        Integer[] p1 = new Integer[] {1,0,1,0,0,1,0,1,1};
        Integer[] p2 = new Integer[] {0,1,1,0,1,0,1,1,1};

        BinaryChromosome p1c = new DummyBinaryChromosome(p1);
        BinaryChromosome p2c = new DummyBinaryChromosome(p2);

        OnePointCrossover<Integer> opc = new OnePointCrossover();

        // how to test a stochastic method?
        for (int i=0; i<20; i++) {
            ChromosomePair pair = opc.crossover(p1c,p2c);

            Integer[] c1 = new Integer[p1.length];
            Integer[] c2 = new Integer[p2.length];

            c1 = ((BinaryChromosome) pair.getFirst()).getRepresentation().toArray(c1);
            c2 = ((BinaryChromosome) pair.getSecond()).getRepresentation().toArray(c2);

            // first and last values will be the same
            assertEquals((int) p1[0], (int) c1[0]);
            assertEquals((int) p2[0], (int) c2[0]);
            assertEquals((int) p1[p1.length-1], (int) c1[c1.length-1]);
            assertEquals((int) p2[p2.length-1], (int) c2[c2.length-1]);
            // moreover, in the above setting, the 2nd, 3rd and 7th values will be the same
            assertEquals((int) p1[2], (int) c1[2]);
            assertEquals((int) p2[2], (int) c2[2]);
            assertEquals((int) p1[3], (int) c1[3]);
            assertEquals((int) p2[3], (int) c2[3]);
            assertEquals((int) p1[7], (int) c1[7]);
            assertEquals((int) p2[7], (int) c2[7]);
        }
    }

}

Other Commons Math examples (source code examples)

Here is a short list of links related to this Commons Math OnePointCrossoverTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.