Search in sources :

Example 1 with ChromosomeCriteria

use of org.apache.ignite.ml.genetic.parameter.ChromosomeCriteria in project ignite by apache.

the class OptimizeMakeChangeGAExample method main.

/**
 * Executes example.
 *
 * Specify value for -DAMOUNTCHANGE JVM system variable
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.setProperty("IGNITE_QUIET", "false");
    sAmountChange = "75";
    StringBuffer sbErrorMessage = new StringBuffer();
    sbErrorMessage.append("AMOUNTCHANGE System property not set. Please provide a valid value between 1 and 99. ");
    sbErrorMessage.append(" ");
    sbErrorMessage.append("IE: -DAMOUNTCHANGE=75");
    sbErrorMessage.append("\n");
    sbErrorMessage.append("Using default value: 75");
    // Check if -DAMOUNTCHANGE JVM system variable is provided
    if (System.getProperty("AMOUNTCHANGE") == null) {
        System.out.println(sbErrorMessage);
    } else {
        sAmountChange = System.getProperty("AMOUNTCHANGE");
    }
    try {
        // Create an Ignite instance as you would in any other use case.
        ignite = Ignition.start("examples/config/example-ignite.xml");
        logger = ignite.log();
        // Create GAConfiguration
        gaConfig = new GAConfiguration();
        // set Gene Pool
        List<Gene> genes = getGenePool();
        // set selection method
        gaConfig.setSelectionMethod(GAGridConstants.SELECTION_METHOD.SELECTON_METHOD_ELETISM);
        gaConfig.setElitismCount(10);
        // set the Chromosome Length to '4' since we have 4 coins.
        gaConfig.setChromosomeLength(4);
        // set population size
        gaConfig.setPopulationSize(500);
        // initialize gene pool
        gaConfig.setGenePool(genes);
        // set Truncate Rate
        gaConfig.setTruncateRate(.10);
        // set Cross Over Rate
        gaConfig.setCrossOverRate(.50);
        // set Mutation Rate
        gaConfig.setMutationRate(.50);
        // create and set Fitness function
        OptimizeMakeChangeFitnessFunction function = new OptimizeMakeChangeFitnessFunction(new Integer(sAmountChange));
        gaConfig.setFitnessFunction(function);
        // create and set TerminateCriteria
        OptimizeMakeChangeTerminateCriteria termCriteria = new OptimizeMakeChangeTerminateCriteria(ignite);
        ChromosomeCriteria chromosomeCriteria = new ChromosomeCriteria();
        List values = new ArrayList();
        values.add("coinType=QUARTER");
        values.add("coinType=DIME");
        values.add("coinType=NICKEL");
        values.add("coinType=PENNY");
        chromosomeCriteria.setCriteria(values);
        gaConfig.setChromosomeCriteria(chromosomeCriteria);
        gaConfig.setTerminateCriteria(termCriteria);
        // initialize GAGrid
        gaGrid = new GAGrid(gaConfig, ignite);
        logger.info("##########################################################################################");
        logger.info("Calculating optimal set of coins where amount of change is " + sAmountChange);
        logger.info("##########################################################################################");
        Chromosome fittestChromosome = gaGrid.evolve();
        Ignition.stop(true);
        ignite = null;
    } catch (Exception e) {
        System.out.println(e);
    }
}
Also used : GAConfiguration(org.apache.ignite.ml.genetic.parameter.GAConfiguration) ChromosomeCriteria(org.apache.ignite.ml.genetic.parameter.ChromosomeCriteria) ArrayList(java.util.ArrayList) Chromosome(org.apache.ignite.ml.genetic.Chromosome) Gene(org.apache.ignite.ml.genetic.Gene) List(java.util.List) ArrayList(java.util.ArrayList) GAGrid(org.apache.ignite.ml.genetic.GAGrid)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Chromosome (org.apache.ignite.ml.genetic.Chromosome)1 GAGrid (org.apache.ignite.ml.genetic.GAGrid)1 Gene (org.apache.ignite.ml.genetic.Gene)1 ChromosomeCriteria (org.apache.ignite.ml.genetic.parameter.ChromosomeCriteria)1 GAConfiguration (org.apache.ignite.ml.genetic.parameter.GAConfiguration)1