Search in sources :

Example 11 with Gene

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

the class HelloWorldGAExample method getGenePool.

/**
 * Helper routine to initialize Gene pool
 *
 * In typical usecase genes may be stored in database.
 *
 * @return List<Gene>
 */
private static List<Gene> getGenePool() {
    List<Gene> list = new ArrayList();
    char[] chars = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ' };
    for (int i = 0; i < chars.length; i++) {
        Gene gene = new Gene(new Character(chars[i]));
        list.add(gene);
    }
    return list;
}
Also used : Gene(org.apache.ignite.ml.genetic.Gene) ArrayList(java.util.ArrayList)

Example 12 with Gene

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

the class MovieGAExample method getGenePool.

private static List<Gene> getGenePool() {
    List list = new ArrayList();
    Movie movie1 = new Movie();
    movie1.setName("The Matrix");
    movie1.setImdbRating(7);
    List genre1 = new ArrayList();
    genre1.add("SciFi");
    genre1.add("Action");
    movie1.setGenre(genre1);
    movie1.setRating("PG-13");
    movie1.setYear("1999");
    Gene gene1 = new Gene(movie1);
    Movie movie2 = new Movie();
    movie2.setName("The Dark Knight");
    movie2.setImdbRating(9.6);
    List genre2 = new ArrayList();
    genre2.add("Action");
    movie2.setGenre(genre2);
    movie2.setRating("PG-13");
    movie2.setYear("2008");
    Gene gene2 = new Gene(movie2);
    Movie movie3 = new Movie();
    movie3.setName("The Avengers");
    movie3.setImdbRating(9.6);
    movie3.setYear("2012");
    List genre3 = new ArrayList();
    genre3.add("Action");
    movie3.setGenre(genre3);
    movie3.setRating("PG-13");
    Gene gene3 = new Gene(movie3);
    Movie movie4 = new Movie();
    movie4.setName("The Hangover");
    movie4.setImdbRating(7.6);
    List genre4 = new ArrayList();
    genre4.add("Comedy");
    movie4.setGenre(genre4);
    movie4.setRating("R");
    movie4.setYear("2009");
    Gene gene4 = new Gene(movie4);
    Movie movie5 = new Movie();
    movie5.setName("The Hangover 2");
    movie5.setImdbRating(9.6);
    List genre5 = new ArrayList();
    genre5.add("Comedy");
    movie5.setGenre(genre5);
    movie5.setRating("R");
    movie5.setYear("2012");
    Gene gene5 = new Gene(movie5);
    Movie movie6 = new Movie();
    movie6.setName("This Means War");
    movie6.setImdbRating(6.4);
    List genre6 = new ArrayList();
    genre6.add("Comedy");
    genre6.add("Action");
    genre6.add("Romance");
    movie6.setGenre(genre6);
    movie6.setRating("PG-13");
    movie6.setYear("2012");
    Gene gene6 = new Gene(movie6);
    Movie movie7 = new Movie();
    movie7.setName("Hitch");
    movie7.setImdbRating(10);
    List genre7 = new ArrayList();
    genre7.add("Comedy");
    genre7.add("Romance");
    movie7.setGenre(genre7);
    movie7.setRating("PG-13");
    movie7.setYear("2005");
    Gene gene7 = new Gene(movie7);
    Movie movie8 = new Movie();
    movie8.setName("21 Jump Street");
    movie8.setImdbRating(6.7);
    List genre8 = new ArrayList();
    genre8.add("Comedy");
    genre8.add("Action");
    movie8.setGenre(genre8);
    movie8.setRating("R");
    movie8.setYear("2012");
    Gene gene8 = new Gene(movie8);
    Movie movie9 = new Movie();
    movie9.setName("Killers");
    movie9.setImdbRating(5.1);
    List genre9 = new ArrayList();
    genre9.add("Comedy");
    genre9.add("Action");
    genre9.add("Romance");
    movie9.setGenre(genre9);
    movie9.setRating("PG-13");
    movie9.setYear("2010");
    Gene gene9 = new Gene(movie9);
    Movie movie10 = new Movie();
    movie10.setName("What to Expect When You're Expecting");
    movie10.setImdbRating(5.1);
    List genre10 = new ArrayList();
    genre10.add("Comedy");
    genre10.add("Romance");
    movie10.setGenre(genre10);
    movie10.setRating("PG-13");
    movie10.setYear("2012");
    Gene gene10 = new Gene(movie10);
    list.add(gene1);
    list.add(gene2);
    list.add(gene3);
    list.add(gene4);
    list.add(gene5);
    list.add(gene6);
    list.add(gene7);
    list.add(gene8);
    list.add(gene9);
    list.add(gene10);
    return list;
}
Also used : Gene(org.apache.ignite.ml.genetic.Gene) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 13 with Gene

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

the class HelloWorldTerminateCriteria method printPhrase.

/**
 * Helper to print Phrase
 *
 * @param List of Genes
 */
private void printPhrase(List<Gene> genes) {
    StringBuffer sbPhrase = new StringBuffer();
    for (Gene gene : genes) {
        sbPhrase.append(((Character) gene.getValue()).toString());
    }
    igniteLogger.info(sbPhrase.toString());
}
Also used : Gene(org.apache.ignite.ml.genetic.Gene)

Example 14 with Gene

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

the class GAGridUtils method getGenesInOrderForChromosome.

/**
 * Retrieve genes in order
 *
 * @param ignite Ignite
 * @param chromosome Chromosome
 * @return List of Genes
 */
public static List<Gene> getGenesInOrderForChromosome(Ignite ignite, Chromosome chromosome) {
    List<Gene> genes = new ArrayList();
    IgniteCache<Long, Gene> cache = ignite.cache(GAGridConstants.GENE_CACHE);
    long[] primaryKeys = chromosome.getGenes();
    for (int k = 0; k < primaryKeys.length; k++) {
        StringBuffer sbSqlClause = new StringBuffer();
        sbSqlClause.append("_key IN ");
        sbSqlClause.append("(");
        sbSqlClause.append(primaryKeys[k]);
        sbSqlClause.append(")");
        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)

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