Search in sources :

Example 1 with Gene

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

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

the class GAGridUtils method getGenesForChromosome.

/**
 * @param ignite Ignite
 * @param chromosome Chromosome
 * @return List of Genes
 */
public static List<Gene> getGenesForChromosome(Ignite ignite, Chromosome chromosome) {
    List<Gene> genes = new ArrayList();
    IgniteCache<Long, Gene> cache = ignite.cache(GAGridConstants.GENE_CACHE);
    StringBuffer sbSqlClause = new StringBuffer();
    sbSqlClause.append("_key IN ");
    String sqlInClause = Arrays.toString(chromosome.getGenes());
    sqlInClause = sqlInClause.replace("[", "(");
    sqlInClause = sqlInClause.replace("]", ")");
    sbSqlClause.append(sqlInClause);
    SqlQuery sql = new SqlQuery(Gene.class, sbSqlClause.toString());
    try (QueryCursor<Entry<Long, Gene>> cursor = cache.query(sql)) {
        for (Entry<Long, Gene> e : cursor) genes.add(e.getValue());
    }
    return genes;
}
Also used : Entry(javax.cache.Cache.Entry) SqlQuery(org.apache.ignite.cache.query.SqlQuery) Gene(org.apache.ignite.ml.genetic.Gene) ArrayList(java.util.ArrayList)

Example 3 with Gene

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

the class OptimizeMakeChangeGAExample method getGenePool.

/**
 * Helper routine to initialize Gene pool
 *
 * In typical usecase genes may be stored in database.
 *
 * @return List of Genes
 */
private static List<Gene> getGenePool() {
    List<Gene> list = new ArrayList();
    Gene quarterGene1 = new Gene(new Coin(Coin.CoinType.QUARTER, 3));
    Gene quarterGene2 = new Gene(new Coin(Coin.CoinType.QUARTER, 2));
    Gene quarterGene3 = new Gene(new Coin(Coin.CoinType.QUARTER, 1));
    Gene quarterGene4 = new Gene(new Coin(Coin.CoinType.QUARTER, 0));
    Gene dimeGene1 = new Gene(new Coin(Coin.CoinType.DIME, 2));
    Gene dimeGene2 = new Gene(new Coin(Coin.CoinType.DIME, 1));
    Gene dimeGene3 = new Gene(new Coin(Coin.CoinType.DIME, 0));
    Gene nickelGene1 = new Gene(new Coin(Coin.CoinType.NICKEL, 1));
    Gene nickelGene2 = new Gene(new Coin(Coin.CoinType.NICKEL, 0));
    Gene pennyGene1 = new Gene(new Coin(Coin.CoinType.PENNY, 4));
    Gene pennyGene2 = new Gene(new Coin(Coin.CoinType.PENNY, 3));
    Gene pennyGene3 = new Gene(new Coin(Coin.CoinType.PENNY, 2));
    Gene pennyGene4 = new Gene(new Coin(Coin.CoinType.PENNY, 1));
    Gene pennyGene5 = new Gene(new Coin(Coin.CoinType.PENNY, 0));
    list.add(quarterGene1);
    list.add(quarterGene2);
    list.add(quarterGene3);
    list.add(quarterGene4);
    list.add(dimeGene1);
    list.add(dimeGene2);
    list.add(dimeGene3);
    list.add(nickelGene1);
    list.add(nickelGene2);
    list.add(pennyGene1);
    list.add(pennyGene2);
    list.add(pennyGene3);
    list.add(pennyGene4);
    list.add(pennyGene5);
    return list;
}
Also used : Gene(org.apache.ignite.ml.genetic.Gene) ArrayList(java.util.ArrayList)

Example 4 with Gene

use of org.apache.ignite.ml.genetic.Gene 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 5 with Gene

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

the class MovieTerminateCriteria method printMovies.

/**
 * Helper to print change detail
 *
 * @param genes List of Genes
 */
private void printMovies(List<Gene> genes) {
    for (Gene gene : genes) {
        igniteLogger.info("Name: " + ((Movie) gene.getValue()).getName().toString());
        igniteLogger.info("Genres: " + ((Movie) gene.getValue()).getGenre().toString());
        igniteLogger.info("IMDB Rating: " + ((Movie) gene.getValue()).getImdbRating());
    }
}
Also used : Gene(org.apache.ignite.ml.genetic.Gene)

Aggregations

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