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