summaryrefslogtreecommitdiffstats
path: root/watchmaker/examples/src/java/main/org/uncommons/watchmaker/examples/monalisa/ProbabilitiesPanel.java
blob: 5267be3f08a2dfa946949af3f31101673d8aca4e (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//=============================================================================
// Copyright 2006-2010 Daniel W. Dyer
//
// Licensed 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.uncommons.watchmaker.examples.monalisa;

import java.awt.Dimension;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import org.uncommons.maths.number.ConstantGenerator;
import org.uncommons.maths.random.GaussianGenerator;
import org.uncommons.maths.random.Probability;
import org.uncommons.swing.SpringUtilities;
import org.uncommons.watchmaker.framework.EvolutionaryOperator;
import org.uncommons.watchmaker.framework.operators.EvolutionPipeline;
import org.uncommons.watchmaker.framework.operators.ListCrossover;
import org.uncommons.watchmaker.framework.operators.ListOperator;
import org.uncommons.watchmaker.swing.ProbabilityParameterControl;

/**
 * Panel that displays controls for the Mona Lisa example program.  These
 * controls allow the evolution parameters to be tweaked.
 * @author Daniel Dyer
 */
class ProbabilitiesPanel extends JPanel
{
    private static final Probability ONE_TENTH = new Probability(0.1d);
    
    private final ProbabilityParameterControl addPolygonControl;
    private final ProbabilityParameterControl removePolygonControl;
    private final ProbabilityParameterControl movePolygonControl;
    private final ProbabilityParameterControl crossOverControl;
    private final ProbabilityParameterControl addVertexControl;
    private final ProbabilityParameterControl removeVertexControl;
    private final ProbabilityParameterControl moveVertexControl;
    private final ProbabilityParameterControl changeColourControl;


    ProbabilitiesPanel()
    {
        super(new SpringLayout());
        addPolygonControl = new ProbabilityParameterControl(Probability.ZERO,
                                                            ONE_TENTH,
                                                            3,
                                                            new Probability(0.02));
        add(new JLabel("Add Polygon: "));
        add(addPolygonControl.getControl());
        addPolygonControl.setDescription("For each IMAGE, the probability that a new "
                                         + "randomly-generated polygon will be added.");

        addVertexControl = new ProbabilityParameterControl(Probability.ZERO,
                                                           ONE_TENTH,
                                                           3,
                                                           new Probability(0.01));
        add(new JLabel("Add Vertex: "));
        add(addVertexControl.getControl());
        addVertexControl.setDescription("For each POLYGON, the probability that a new "
                                        + "randomly-generated vertex will be added.");

        removePolygonControl = new ProbabilityParameterControl(Probability.ZERO,
                                                               ONE_TENTH,
                                                               3,
                                                               new Probability(0.02));
        add(new JLabel("Remove Polygon: "));
        add(removePolygonControl.getControl());
        removePolygonControl.setDescription("For each IMAGE, the probability that a "
                                            + "randomly-selected polygon will be discarded.");

        removeVertexControl = new ProbabilityParameterControl(Probability.ZERO,
                                                              ONE_TENTH,
                                                              3,
                                                              new Probability(0.01));
        add(new JLabel("Remove Vertex: "));
        add(removeVertexControl.getControl());
        removeVertexControl.setDescription("For each POLYGON, the probability that a "
                                           + "randomly-selected vertex will be discarded.");

        movePolygonControl = new ProbabilityParameterControl(Probability.ZERO,
                                                             ONE_TENTH,
                                                             3,
                                                             new Probability(0.01));
        add(new JLabel("Reorder Polygons: "));
        add(movePolygonControl.getControl());
        movePolygonControl.setDescription("For each IMAGE, the probability that the z-positions "
                                          + "of two randomly-selected polygons will be swapped.");

        moveVertexControl = new ProbabilityParameterControl(Probability.ZERO,
                                                            ONE_TENTH,
                                                            3,
                                                            new Probability(0.03));
        add(new JLabel("Move Vertex: "));
        add(moveVertexControl.getControl());
        moveVertexControl.setDescription("For each POLYGON, the probability that a randomly-selected "
                                         + "vertex will be displaced.");

        crossOverControl = new ProbabilityParameterControl(Probability.ZERO,
                                                           Probability.ONE,
                                                           2,
                                                           Probability.ONE);
        add(new JLabel("Cross-over: "));
        add(crossOverControl.getControl());
        crossOverControl.setDescription("For each PAIR of parent IMAGES, the probability that "
                                        + "2-point cross-over is applied.");

        changeColourControl = new ProbabilityParameterControl(Probability.ZERO,
                                                              ONE_TENTH,
                                                              3,
                                                              new Probability(0.01));
        add(new JLabel("Change Colour: "));
        add(changeColourControl.getControl());
        changeColourControl.setDescription("For each POLYGON, the probability that its colour will be mutated.");

        // Set component names for easy look-up from tests.
        addPolygonControl.getControl().setName("AddPolygon");
        removePolygonControl.getControl().setName("RemovePolygon");
        movePolygonControl.getControl().setName("MovePolygon");
        crossOverControl.getControl().setName("Cross-over");
        addVertexControl.getControl().setName("AddVertex");
        removeVertexControl.getControl().setName("RemoveVertex");
        moveVertexControl.getControl().setName("MoveVertex");
        changeColourControl.getControl().setName("ChangeColour");

        SpringUtilities.makeCompactGrid(this, 4, 4, 10, 0, 10, 0);
    }


    /**
     * Construct the combination of evolutionary operators that will be used to evolve the
     * polygon-based images.
     * @param factory A source of polygons.
     * @param canvasSize The size of the target image.
     * @param rng A source of randomness.
     * @return A complex evolutionary operator constructed from simpler operators.
     */
    public EvolutionaryOperator<List<ColouredPolygon>> createEvolutionPipeline(PolygonImageFactory factory,
                                                                               Dimension canvasSize,
                                                                               Random rng)
    {
        List<EvolutionaryOperator<List<ColouredPolygon>>> operators
            = new LinkedList<EvolutionaryOperator<List<ColouredPolygon>>>();
        operators.add(new ListCrossover<ColouredPolygon>(new ConstantGenerator<Integer>(2),
                                                         crossOverControl.getNumberGenerator()));
        operators.add(new RemovePolygonMutation(removePolygonControl.getNumberGenerator()));
        operators.add(new MovePolygonMutation(movePolygonControl.getNumberGenerator()));
        operators.add(new ListOperator<ColouredPolygon>(new RemoveVertexMutation(canvasSize,
                                                                                 removeVertexControl.getNumberGenerator())));
        operators.add(new ListOperator<ColouredPolygon>(new AdjustVertexMutation(canvasSize,
                                                                                 moveVertexControl.getNumberGenerator(),
                                                                                 new GaussianGenerator(0, 3, rng))));
        operators.add(new ListOperator<ColouredPolygon>(new AddVertexMutation(canvasSize,
                                                                              addVertexControl.getNumberGenerator())));
        operators.add(new ListOperator<ColouredPolygon>(new PolygonColourMutation(changeColourControl.getNumberGenerator(),
                                                                                  new GaussianGenerator(0, 20, rng))));
        operators.add(new AddPolygonMutation(addPolygonControl.getNumberGenerator(), factory, 50));
        return new EvolutionPipeline<List<ColouredPolygon>>(operators);
    }

}