Search in sources :

Example 6 with Gene

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

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

Example 8 with Gene

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

the class OptimizeMakeChangeFitnessFunction method getTotalNumberOfCoins.

/**
 * Return the total number of coins
 *
 * @param genes Genes
 * @return Number of coins
 */
private int getTotalNumberOfCoins(List<Gene> genes) {
    int totalNumberOfCoins = 0;
    for (Gene gene : genes) {
        int numberOfCoins = ((Coin) gene.getValue()).getNumberOfCoins();
        totalNumberOfCoins = totalNumberOfCoins + numberOfCoins;
    }
    return totalNumberOfCoins;
}
Also used : Gene(org.apache.ignite.ml.genetic.Gene)

Example 9 with Gene

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

the class OptimizeMakeChangeFitnessFunction method getAmountOfChange.

/**
 * Calculate amount of change
 *
 * @param genes Genes
 * @return Amount of change
 */
private int getAmountOfChange(List<Gene> genes) {
    Gene quarterGene = (Gene) genes.get(0);
    Gene dimeGene = (Gene) genes.get(1);
    Gene nickelGene = (Gene) genes.get(2);
    Gene pennyGene = (Gene) genes.get(3);
    int numQuarters = ((Coin) quarterGene.getValue()).getNumberOfCoins();
    int numDimes = ((Coin) dimeGene.getValue()).getNumberOfCoins();
    int numNickels = ((Coin) nickelGene.getValue()).getNumberOfCoins();
    int numPennies = ((Coin) pennyGene.getValue()).getNumberOfCoins();
    return (numQuarters * 25) + (numDimes * 10) + (numNickels * 5) + numPennies;
}
Also used : Gene(org.apache.ignite.ml.genetic.Gene)

Example 10 with Gene

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

the class OptimizeMakeChangeTerminateCriteria method printCoins.

/**
 * Helper to print change detail
 *
 * @param genes List if Genes
 */
private void printCoins(List<Gene> genes) {
    for (Gene gene : genes) {
        igniteLogger.info("Coin Type: " + ((Coin) gene.getValue()).getCoinType().toString());
        igniteLogger.info("Number of Coins: " + ((Coin) gene.getValue()).getNumberOfCoins());
    }
}
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