140 likes | 290 Views
GA implementaatiot. Oskar Norrback q86033. 10-vuotinen historia, ~130k LoC, TDD(96%) natural, best chromosomes, roulette wheel selection Crossover, averaging crossover, mutation, gaussian mutation, reproduction operator Integer- , double, boolean- ja fixed binary geenit Grid computing
E N D
GA implementaatiot Oskar Norrback q86033
10-vuotinen historia, ~130k LoC, TDD(96%) • natural, best chromosomes, roulette wheel selection • Crossover, averaging crossover, mutation, gaussian mutation, reproduction operator • Integer- , double, boolean- ja fixed binary geenit • Grid computing • Supergeenit, Pareto • Käytetty jonkin verran GA aiheisissa julkaisuissa
JGAP / TSP • Löytyy valmis abstrakti Salesman luokka • Implementaatio yksinkertaista(~40 LoC)
public class TravelingSalesMan extends Salesman { private static final long serialVersionUID = 6645643570553239308L; public static final intCITIES = 10; @Override public IChromosome createSampleChromosome(Object arg0) { try { //set up sample genes for the sample chromosome Gene[] sampleGenes = new Gene[10]; for(int i=0;i<10;i++) { sampleGenes[i] = new IntegerGene(getConfiguration(),1,CITIES); sampleGenes[i].setAllele(i+1); //create 1,2,3... Example solution for JGAP } Chromosome c = new Chromosome(getConfiguration(),sampleGenes); return c; } catch(Exception x) { throw new RuntimeException(x); } } @Override public double distance(Gene arg0, Gene arg1) { IntegerGene from = (IntegerGene)arg0; IntegerGene to = (IntegerGene)arg1; return DistanceMatrix.getDistance(from.intValue(), to.intValue()); } }
public class DistanceMatrix { private static int[][] _distances = new int[][] { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10}, { 1, 0, 6, 5, 7, 9, 2, 5, 3, 4, 1}, { 2, 5, 0, 2, 4, 4, 4, 7, 9, 6, 3}, { 3, 7, 1, 0, 3, 4, 4, 3, 8, 7, 5}, { 4, 8, 7, 6, 0, 3, 5, 4, 5, 9, 7}, { 5, 4, 4, 7, 2, 0, 9, 7, 2, 4, 9}, { 6,11, 7, 8, 1, 6, 0, 6, 2, 2, 2}, { 7, 3, 7, 3, 5, 7, 9, 0, 6, 3, 4}, { 8, 5, 7, 3, 5, 4, 6, 1, 0, 5, 6}, { 9, 6, 2, 5, 3, 1, 9,11, 5, 0, 8}, {10, 6, 5, 8, 4, 7, 3, 6, 5, 5, 0}}; public static intgetDistance(int from, int to) { return _distances[from][to]; } }
public class TSPMain { public static void main(String[] args) throws Exception { TravelingSalesMan t = new TravelingSalesMan(); IChromosome optimal = t.findOptimalPath(null); System.out.println("Optimal solution: "+optimal); } }
AForge.NET(C#) • 3-vuotinen historia, ~190k LoC • Suuri tekoälyyn ja signaalien käsittelyyn liittyvä koodikirjasto: imaging, vision, video, neuro,genetic, fuzzy, robotics, machinelearning • elite, roulette wheel ja rank selection • binary, short array, double array geenit
AForge / TSP • Ei yhtä helppoa kuin JGAPilla, mutta löytyy valmis PermutationChromosome pohjaksi • Crossoveria pitäisi luultavasti viilata, esiintyy paljon samoja ratkaisuja • C#:llä helpompi tehdä interaktiivinen sovellus kuin Javalla
class TSPFitnessfunction : AForge.Genetic.IFitnessFunction { #region IFitnessFunction Members public double Evaluate(AForge.Genetic.IChromosome chromosome) { int roundTrip = 0; PermutationChromosome pc = (PermutationChromosome)chromosome; for(int i=0;i<pc.Length-1;i++) { ushort from = pc.Value[i]; ushort to = pc.Value[i + 1]; roundTrip += DistanceMatrix.getDistance(from+1, to+1); } return 100-roundTrip; } #endregion public double getRoundTrip(IChromosome chromosome) { double fitness = Evaluate(chromosome); return 100-fitness; } }
class TSPController { public Population _pop { get; set; } private TSPFitnessfunction _tff; public int generation { get; set; } private int POP_SIZE=25; public TSPController() { init(); } private void init() { PermutationChromosome pc = new PermutationChromosome(9); _tff = new TSPFitnessfunction(); RouletteWheelSelection rws = new RouletteWheelSelection(); _pop = new Population(POP_SIZE, pc, _tff, rws); } public void evolve() { generation++; _pop.RunEpoch(); } .... }
Evolving Objects(C++) • 10-vuotinen historia, ~90k LoC • ”create your own stochastic optimization algorithms insanely fast” • Perustuu STL templateihin • Binary string, permutaatio, vektori kromosomit • Rank, roulette wheel, elitism selection • Joitakin julkaisuja
EO / TSP • ??!%¤#%&% • Suuria ongelmia saada edes esimerkkejä kääntymään(MinGW-GCC / MSVC++) • Osa ongelmaa todennäköisesti allekirjoittaneen vähäinen C++ kokemus, mutta EO vaikuttaa kolmikosta vaikeimmaltaalustalta
Lähteet • http://jgap.sourceforge.net/ • http://www.aforgenet.com/framework/ • http://eodev.sourceforge.net/ • http://www.ohloh.net/