Search in sources :

Example 1 with Chromosome

use of org.apache.ignite.ml.genetic.Chromosome in project ignite by apache.

the class GAGridFunction method getChromosomes.

/**
 * Helper routine to return 'pivoted' results using the provided query param
 *
 * @param query Sql
 * @return Result set
 */
private static SimpleResultSet getChromosomes(String query) {
    Ignite ignite = Ignition.localIgnite();
    List<Chromosome> chromosomes = GAGridUtils.getChromosomes(ignite, query);
    SimpleResultSet rs2 = new SimpleResultSet();
    Chromosome aChrom = chromosomes.get(0);
    int genesCount = aChrom.getGenes().length;
    rs2.addColumn("Chromosome Id", Types.INTEGER, 0, 0);
    rs2.addColumn("Fitness Score", Types.DOUBLE, 0, 0);
    for (int i = 0; i < genesCount; i++) {
        int columnIndex = i + 1;
        rs2.addColumn("Gene " + columnIndex, Types.VARCHAR, 0, 0);
    }
    for (Chromosome rowChrom : chromosomes) {
        Object[] row = new Object[genesCount + 2];
        row[0] = rowChrom.id();
        row[1] = rowChrom.getFitnessScore();
        List<Gene> genes = GAGridUtils.getGenesInOrderForChromosome(ignite, rowChrom);
        int i = 2;
        for (Gene gene : genes) {
            row[i] = gene.getValue().toString();
            i = i + 1;
        }
        // Add a row for an individual Chromosome
        rs2.addRow(row);
    }
    return rs2;
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) Gene(org.apache.ignite.ml.genetic.Gene) Chromosome(org.apache.ignite.ml.genetic.Chromosome) Ignite(org.apache.ignite.Ignite)

Example 2 with Chromosome

use of org.apache.ignite.ml.genetic.Chromosome in project ignite by apache.

the class GAGridUtils method getChromosomes.

/**
 * Retrieve chromosomes
 *
 * @param ignite Ignite
 * @param query Sql
 * @return List of Chromosomes
 */
public static List<Chromosome> getChromosomes(Ignite ignite, String query) {
    List<Chromosome> chromosomes = new ArrayList();
    IgniteCache<Long, Chromosome> populationCache = ignite.getOrCreateCache(PopulationCacheConfig.populationCache());
    SqlQuery sql = new SqlQuery(Chromosome.class, query);
    try (QueryCursor<Entry<Long, Chromosome>> cursor = populationCache.query(sql)) {
        for (Entry<Long, Chromosome> e : cursor) chromosomes.add(e.getValue());
    }
    return chromosomes;
}
Also used : Entry(javax.cache.Cache.Entry) SqlQuery(org.apache.ignite.cache.query.SqlQuery) ArrayList(java.util.ArrayList) Chromosome(org.apache.ignite.ml.genetic.Chromosome)

Example 3 with Chromosome

use of org.apache.ignite.ml.genetic.Chromosome 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)

Example 4 with Chromosome

use of org.apache.ignite.ml.genetic.Chromosome in project ignite by apache.

the class HelloWorldGAExample method main.

public static void main(String[] args) {
    System.setProperty("IGNITE_QUIET", "false");
    try {
        // Create an Ignite instance as you would in any other use case.
        ignite = Ignition.start("examples/config/example-ignite.xml");
        // Create GAConfiguration
        gaConfig = new GAConfiguration();
        // set Gene Pool
        List<Gene> genes = getGenePool();
        // set the Chromosome Length to '11' since 'HELLO WORLD' contains 11 characters.
        gaConfig.setChromosomeLength(11);
        // initialize gene pool
        gaConfig.setGenePool(genes);
        // create and set Fitness function
        HelloWorldFitnessFunction function = new HelloWorldFitnessFunction();
        gaConfig.setFitnessFunction(function);
        // create and set TerminateCriteria
        HelloWorldTerminateCriteria termCriteria = new HelloWorldTerminateCriteria(ignite);
        gaConfig.setTerminateCriteria(termCriteria);
        ignite.log();
        gaGrid = new GAGrid(gaConfig, ignite);
        // evolve the population
        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) Gene(org.apache.ignite.ml.genetic.Gene) Chromosome(org.apache.ignite.ml.genetic.Chromosome) GAGrid(org.apache.ignite.ml.genetic.GAGrid)

Example 5 with Chromosome

use of org.apache.ignite.ml.genetic.Chromosome in project ignite by apache.

the class MovieGAExample method main.

/**
 * Executes example.
 *
 * Specify value for -DGENRES JVM system variable
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.setProperty("IGNITE_QUIET", "false");
    List genres = new ArrayList();
    String sGenres = "Action,Comedy,Romance";
    StringBuffer sbErrorMessage = new StringBuffer();
    sbErrorMessage.append("GENRES System property not set. Please provide GENRES information.");
    sbErrorMessage.append(" ");
    sbErrorMessage.append("IE: -DGENRES=Action,Comedy,Romance");
    sbErrorMessage.append("\n");
    sbErrorMessage.append("Using default value: Action,Comedy,Romance");
    if (System.getProperty("GENRES") == null) {
        System.out.println(sbErrorMessage);
    } else {
        sGenres = System.getProperty("GENRES");
    }
    StringTokenizer st = new StringTokenizer(sGenres, ",");
    while (st.hasMoreElements()) {
        String genre = st.nextToken();
        genres.add(genre);
    }
    // Create GAConfiguration
    gaConfig = new GAConfiguration();
    // set Gene Pool
    List<Gene> genes = getGenePool();
    // Define Chromosome
    gaConfig.setChromosomeLength(3);
    gaConfig.setPopulationSize(100);
    gaConfig.setGenePool(genes);
    gaConfig.setTruncateRate(.10);
    gaConfig.setCrossOverRate(.50);
    gaConfig.setMutationRate(.50);
    gaConfig.setSelectionMethod(GAGridConstants.SELECTION_METHOD.SELECTION_METHOD_TRUNCATION);
    // Create fitness function
    MovieFitnessFunction function = new MovieFitnessFunction(genres);
    // set fitness function
    gaConfig.setFitnessFunction(function);
    try {
        // Create an Ignite instance as you would in any other use case.
        ignite = Ignition.start("examples/config/example-ignite.xml");
        MovieTerminateCriteria termCriteria = new MovieTerminateCriteria(ignite);
        gaConfig.setTerminateCriteria(termCriteria);
        gaGrid = new GAGrid(gaConfig, ignite);
        ignite.log();
        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) ArrayList(java.util.ArrayList) Chromosome(org.apache.ignite.ml.genetic.Chromosome) StringTokenizer(java.util.StringTokenizer) Gene(org.apache.ignite.ml.genetic.Gene) List(java.util.List) ArrayList(java.util.ArrayList) GAGrid(org.apache.ignite.ml.genetic.GAGrid)

Aggregations

Chromosome (org.apache.ignite.ml.genetic.Chromosome)5 Gene (org.apache.ignite.ml.genetic.Gene)4 ArrayList (java.util.ArrayList)3 GAGrid (org.apache.ignite.ml.genetic.GAGrid)3 GAConfiguration (org.apache.ignite.ml.genetic.parameter.GAConfiguration)3 List (java.util.List)2 StringTokenizer (java.util.StringTokenizer)1 Entry (javax.cache.Cache.Entry)1 Ignite (org.apache.ignite.Ignite)1 SqlQuery (org.apache.ignite.cache.query.SqlQuery)1 ChromosomeCriteria (org.apache.ignite.ml.genetic.parameter.ChromosomeCriteria)1 SimpleResultSet (org.h2.tools.SimpleResultSet)1