Example #1
0
int foo2()
{
  int a = 0;

  int ii;
  for (ii = 0; ii < 2; ++ii) {
    a += child3();
    a += child4();
    a += child5();
  }
  return a;
}
/**
 * @brief Test if getting random solution works
 */
void TestClusterwiseCrossoverEngine::testCrossover(void)
{

    IntegerVectorEncoding p1(g);
    IntegerVectorEncoding p2(g);
    IntegerVectorEncoding child1(g), child2(g);
    IntegerVectorEncoding child3(g), child4(g);

    // Initialize first parent
    p1.addToCluster(0, 0);
    p1.addToCluster(1, 1);
    p1.addToCluster(2, 2);
    p1.addToCluster(3, 3);
    p1.addToCluster(4, 1);
    p1.addToCluster(5, 0);
    // Initialize second parent
    p2.addToCluster(0, 4);
    p2.addToCluster(1, 5);
    p2.addToCluster(2, 5);
    p2.addToCluster(3, 4);
    p2.addToCluster(4, 6);
    p2.addToCluster(5, 5);

    p1.normalize();
    p2.normalize();


    IntegerVectorEncoding case1_1(g), case2_1(g), case3_1(g);
    IntegerVectorEncoding case1_2(g), case2_2(g), case3_2(g);
    bool found1, found2, found3, foundOther;
    found1 = found2 = found3 = foundOther = false;

    //create all possible cases
    case1_1.addToCluster(0, 0);
    case1_1.addToCluster(1, 1);
    case1_1.addToCluster(2, 2);
    case1_1.addToCluster(3, 0);
    case1_1.addToCluster(4, 1);
    case1_1.addToCluster(5, 1);
    case1_2.addToCluster(0, 0);
    case1_2.addToCluster(1, 1);
    case1_2.addToCluster(2, 1);
    case1_2.addToCluster(3, 2);
    case1_2.addToCluster(4, 3);
    case1_2.addToCluster(5, 0);

    case2_1.addToCluster(0, 0);
    case2_1.addToCluster(1, 1);
    case2_1.addToCluster(2, 1);
    case2_1.addToCluster(3, 2);
    case2_1.addToCluster(4, 3);
    case2_1.addToCluster(5, 0);
    case2_2.addToCluster(0, 0);
    case2_2.addToCluster(1, 1);
    case2_2.addToCluster(2, 2);
    case2_2.addToCluster(3, 0);
    case2_2.addToCluster(4, 1);
    case2_2.addToCluster(5, 1);

    case3_1.addToCluster(0, 0);
    case3_1.addToCluster(1, 1);
    case3_1.addToCluster(2, 1);
    case3_1.addToCluster(3, 2);
    case3_1.addToCluster(4, 3);
    case3_1.addToCluster(5, 1);
    case3_2.addToCluster(0, 0);
    case3_2.addToCluster(1, 1);
    case3_2.addToCluster(2, 2);
    case3_2.addToCluster(3, 0);
    case3_2.addToCluster(4, 1);
    case3_2.addToCluster(5, 0);

    // Crossover 1000 times, make sure all cases appear
    for (int i = 0; i < 1000; i++)
    {
        testObj->crossover(p1, p2, child1, child2);
        if (compareClusters(child1, child2, case1_1, case1_2))
        {
            found1 = true;
        } 
        else if (compareClusters(child1, child2, case2_1, case2_2))
        {
            found2 = true;
        }
        else if (compareClusters(child1, child2, case3_1, case3_2))
        {
            found3 = true;
        }
        else
        {
            foundOther = true;
            break;
        }
        CPPUNIT_ASSERT(6 == child1.getEncoding().size());
        CPPUNIT_ASSERT(6 >= child1.getClusterCount());

        CPPUNIT_ASSERT(6 == child2.getEncoding().size());
        CPPUNIT_ASSERT(6 >= child2.getClusterCount());

    }
    //test makes sure that every case appeared at least once
    CPPUNIT_ASSERT(found1);
    CPPUNIT_ASSERT(found2);
    CPPUNIT_ASSERT(found3);
    CPPUNIT_ASSERT(!foundOther);


}