package ml.shifu.shifu.core.dvarsel.wrapper;

import ml.shifu.shifu.core.dvarsel.CandidatePerf;
import ml.shifu.shifu.core.dvarsel.CandidateSeed;
import ml.shifu.shifu.core.dvarsel.CandidatePopulation;
import ml.shifu.shifu.core.dvarsel.VarSelWorkerResult;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.*;

public class CandidateGeneratorTest {
    private CandidateGenerator generator;

    private static final int EXPECT_ITERATION_COUNT = 4;
    private static final int ITERATION_SEED_COUNT = 10;
    private static final int EXPECT_VARIABLE_COUNT = 10;
    private static final int CROSS_PERCENT = 60;
    private static final int MUTATION_PERCENT = 20;

    @BeforeClass
    public void setUpBeforeClass() throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("EXPECT_ITERATION_COUNT", EXPECT_ITERATION_COUNT);
        params.put("ITERATION_SEED_COUNT", ITERATION_SEED_COUNT);
        params.put("EXPECT_VARIABLE_COUNT", EXPECT_VARIABLE_COUNT);
        params.put("CROSS_PERCENT", CROSS_PERCENT);
        params.put("MUTATION_PERCENT", MUTATION_PERCENT);

        List<Integer> variables = new ArrayList<Integer>(100);
        for (int i = 0; i < 100; i++) {
            variables.add(i);
        }
        generator = new CandidateGenerator(params, variables);
    }

    @Test
    public void testInitSeeds() throws Exception {
        CandidatePopulation seeds = generator.initSeeds();
        System.out.println(seeds);
    }

    @Test
    public void testNextGeneration() throws Exception {
        CandidatePopulation seed = generator.initSeeds();
        System.out.println(seed);

        Random random = new Random();

        for (int i = 0; i < EXPECT_ITERATION_COUNT - 1; i++) {
            seed = generateNext(seed, random);
            System.out.println(seed);
        }
    }

    private CandidatePopulation generateNext(CandidatePopulation seed, Random random) {
        List<VarSelWorkerResult> workerResults = new ArrayList<VarSelWorkerResult>();
        workerResults.add(new VarSelWorkerResult(getCandidatePerfs(random, seed)));
        workerResults.add(new VarSelWorkerResult(getCandidatePerfs(random, seed)));
        workerResults.add(new VarSelWorkerResult(getCandidatePerfs(random, seed)));

        final List<VarSelWorkerResult> workerResultList = Collections.unmodifiableList(workerResults);
        Iterable<VarSelWorkerResult> results = new Iterable<VarSelWorkerResult>() {
            @Override
            public Iterator<VarSelWorkerResult> iterator() {
                return workerResultList.iterator();
            }
        };
        return generator.nextGeneration(results, seed);
    }

    private List<CandidatePerf> getCandidatePerfs(Random random, CandidatePopulation seed) {
        List<CandidatePerf> seedPerfList = new ArrayList<CandidatePerf>();
        for (CandidateSeed s : seed.getSeedList()) {
            seedPerfList.add(new CandidatePerf(s.getId(), random.nextDouble()));
        }
        return seedPerfList;
    }
}
@@ -250,16 +250,42 @@
 Result;%0A
+import org.testng.Assert;%0A
 import o
@@ -319,16 +319,16 @@
 eClass;%0A
-
 import o
@@ -892,32 +892,50 @@
 put(
-%22EXPECT_ITERATION_COU
+CandidateGenerator.POPULATION_MULTIPLY_C
 NT
-%22
 , EX
@@ -980,30 +980,47 @@
 put(
-%22ITERATION_SEED_COUNT%22
+CandidateGenerator.POPULATION_LIVE_SIZE
 , IT
@@ -1059,17 +1059,35 @@
 ams.put(
-%22
+CandidateGenerator.
 EXPECT_V
@@ -1099,13 +1099,10 @@
 LE_C
-OU
 NT
-%22
 , EX
@@ -1146,22 +1146,41 @@
 put(
-%22CROSS
+CandidateGenerator.HYBRID
 _PERCENT
 %22, C
@@ -1175,17 +1175,16 @@
 _PERCENT
-%22
 , CROSS_
@@ -1212,17 +1212,35 @@
 ams.put(
-%22
+CandidateGenerator.
 MUTATION
@@ -1251,9 +1251,8 @@
 CENT
-%22
 , MU
@@ -1952,16 +1952,16 @@
 andom);%0A
-
         
@@ -1985,24 +1985,89 @@
 intln(seed);
+%0A%0A            Assert.assertEquals(10, seed.getSeedList().size());
 %0A        %7D%0A 

fix unittest for varsel