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;
}
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;
}
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;
}
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);
}
}
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());
}
}
Aggregations