Class genetictrain
java.lang.Object
|
+--genetictrain
- public class genetictrain
- extends java.lang.Object
this class is not used
by the applet, but only by its own main function.
A genetic algorithm is a method for solving an optimisation proble. Our
problem is to find the best weight vector. The methods works much like
biological evolution: random weight vectors are calculated, and in each
round they play against a randomly selected opponent and the winners survive.
The winners are then copied (with small changes), or two
winners are mixed (called cross-over) and a new round starts: they are again tested
against each other... until you say it is enough. The main advantage of a genetic algorithm
is that it works whithout assuming anything about the problem. It just let two
'individuals' against each other, does not care what they do exactly, but only needs
to know the winner.
The exact description of a round is this: there is a fixed population size
(the number of weightvectors). They are randomly ordered, and the odd even numbers
play against the next odd number. The winner is stored in the even position, the odd
position is empty. Now per group of four a random decision is made: if they mutate,
the odd positions are filled with mutated copies of the winners. If crossover is chosen,
the winners are crossed over, producing two children that are random mixtures of the
parents. Remember that individuals are just lists of numbers. For example:
parent1:-3 -5 2 6 0
parent2 -7 0 -4 1 2
could give:
child1: -3 0 -4 6 2
child2: -7 -5 2 1 0
In the literature this is called uniform crossover. There are other kinds of crossover.
the last position (which is odd) is filled with the average of all other individuals.
Method Summary |
void |
cleanup()
|
static void |
main(java.lang.String[] ps)
the main function. |
void |
read(java.lang.String filename)
read in a population from a file. |
void |
run(int rounds)
let the genetic algorithm run the given number of rounds. |
void |
write(java.lang.String filename,
java.lang.String comment)
the file to write. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
genetictrain
public genetictrain(int p)
- Parameters:
p
- the population size for the genetic algorithm.
read
public void read(java.lang.String filename)
- read in a population from a file. The file format is like this:
- first line: the population size.
- one line per weightvector in the population
each line has the weights on it, and can end with a comment.
- Parameters:
filename
- the file to open.
write
public void write(java.lang.String filename,
java.lang.String comment)
- the file to write. The same format is used as for read.
run
public void run(int rounds)
- let the genetic algorithm run the given number of rounds. It prints
the current round on the java console.
- Parameters:
rounds
- the number of rounds. reasonable values are between
1 and 1000 (although that may take some time.)
cleanup
public void cleanup()
main
public static void main(java.lang.String[] ps)
- the main function. Change this function so it does what you want, recompile
and run it to calculate a better weight vector.