-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
HelloWorldGAExample.java
120 lines (94 loc) · 3.89 KB
/
HelloWorldGAExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.examples.ml.genetic.helloworld;
import java.util.ArrayList;
import java.util.List;
import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
import org.apache.ignite.ml.genetic.Chromosome;
import org.apache.ignite.ml.genetic.GAGrid;
import org.apache.ignite.ml.genetic.Gene;
import org.apache.ignite.ml.genetic.parameter.GAConfiguration;
/**
* This example demonstrates how to use the GAGrid framework.
*
* In this example, we want to evolve a string of 11 characters such that the word 'HELLO WORLD'.
*
* is found.
*
*
* How To Run:
*
* mvn exec:java -Dexec.mainClass="org.apache.ignite.examples.ml.genetic.helloworld.HelloWorldGAExample"
*
* <p> Remote nodes should always be started with special configuration file which enables P2P class loading: {@code
* 'ignite.{sh|bat} examples/config/example-ignite.xml'}.</p> <p> Alternatively you can run ExampleNodeStartup in
* another JVM which will start node with {@code examples/config/example-ignite.xml} configuration.</p>
*/
public class HelloWorldGAExample {
private static Ignite ignite = null;
private static GAGrid gaGrid = null;
private static GAConfiguration gaConfig = null;
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);
}
}
/**
* 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;
}
}