ynd / genetic-drawing

Using evolution to make computers draw.

This URL has Read+Write access

genetic-drawing / src / gd / core / GAInitialChromosomeFactory.java
100644 76 lines (61 sloc) 2.619 kb
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
/*
* Copyright 2008 Yann N. Dauphin. All Rights Reserved.
* Distributed under the terms of the BSD License.
*/
package gd.core;
 
import java.awt.image.BufferedImage;
import org.jgap.Chromosome;
import org.jgap.Gene;
import org.jgap.IChromosome;
import org.jgap.InvalidConfigurationException;
import org.jgap.impl.IntegerGene;
 
/**
* Creates a suitable initial chromosome.
* @author lokee
*/
public class GAInitialChromosomeFactory {
 
    public final static int POINTS = 5;
 
    public static IChromosome create(GAConfiguration a_conf)
            throws InvalidConfigurationException {
        BufferedImage target = a_conf.getTarget();
        IChromosome sample_chromosome = new Chromosome(a_conf);
        Gene[] sample_genes = new Gene[getGenomeSize(a_conf)];
 
        for (int i = 0; i < getGenomeSize(a_conf);) {
            /* Genes that encode the HSB Color */
            for (int c = 0; c < getNumberOfColorGenesPerPolygon() - 1; c++) {
                sample_genes[i] = new IntegerGene(a_conf, 0, 255);
                sample_genes[i++].setToRandomValue(a_conf.getRandomGenerator());
            }
 
            /* Gene that encodes the alpha value of the color */
            sample_genes[i] = new IntegerGene(a_conf, 0, 255);
            sample_genes[i++].setAllele(new Integer(0));
 
 
            /* Genes that encode the position of the points */
            for (int j = 0; j < POINTS; j++) {
                sample_genes[i] = new IntegerGene(a_conf, 0, target.getWidth());
                sample_genes[i++].setToRandomValue(a_conf.getRandomGenerator());
                sample_genes[i] = new IntegerGene(a_conf, 0, target.getHeight());
                sample_genes[i++].setToRandomValue(a_conf.getRandomGenerator());
            }
        }
        sample_chromosome.setGenes(sample_genes);
        return sample_chromosome;
    }
 
    public static int getGenomeSize(GAConfiguration a_conf) {
        return getNumberOfPointGenes(a_conf) + getNumberOfColorGenes(a_conf);
    }
 
    public static int getNumberOfPointGenes(GAConfiguration a_conf) {
        return a_conf.getMaxPolygons() * (getNumberOfGenesPerPoint() * POINTS);
    }
 
    public static int getNumberOfColorGenes(GAConfiguration a_conf) {
        return a_conf.getMaxPolygons() * getNumberOfColorGenesPerPolygon();
    }
 
    public static int getNumberOfGenesPerPolygon() {
        return getNumberOfColorGenesPerPolygon() + getNumberOfGenesPerPoint() * POINTS;
    }
 
    public static int getNumberOfColorGenesPerPolygon() {
        return 4;
    }
 
    public static int getNumberOfGenesPerPoint() {
        return 2;
    }
}