|
1 | | -package aima.gui.fx.demo.search.local; |
2 | | - |
3 | | -import java.util.Collection; |
4 | | -import java.util.Optional; |
5 | | -import java.util.function.Function; |
6 | | - |
7 | | -import aima.core.search.local.Individual; |
8 | | -import aima.gui.fx.framework.IntegrableApplication; |
9 | | -import aima.gui.fx.framework.Parameter; |
10 | | -import aima.gui.fx.framework.SimulationPaneBuilder; |
11 | | -import aima.gui.fx.framework.SimulationPaneCtrl; |
12 | | -import aima.gui.fx.views.FunctionPlotterCtrl; |
13 | | -import javafx.application.Platform; |
14 | | -import javafx.scene.canvas.Canvas; |
15 | | -import javafx.scene.layout.BorderPane; |
16 | | -import javafx.scene.layout.Pane; |
17 | | -import javafx.scene.paint.Color; |
18 | | -import javafx.scene.paint.Paint; |
19 | | - |
20 | | - |
21 | | -/** |
22 | | - * Demonstrates, how the genetic algorithm can be used, to find maximums in |
23 | | - * mathematical functions. Different parameter settings can be tried out and |
24 | | - * progress shown for each iteration. |
25 | | - * |
26 | | - * @author Ruediger Lunde |
27 | | - */ |
28 | | -public class GeneticMaximumFinderApp extends IntegrableApplication { |
29 | | - |
30 | | - public static void main(String[] args) { |
31 | | - launch(args); |
32 | | - } |
33 | | - |
34 | | - public final static String PARAM_FUNC_SELECT = "funcSelect"; |
35 | | - public final static String PARAM_MUT_PROB = "mutProb"; |
36 | | - public final static String PARAM_POPULATION = "population"; |
37 | | - public final static String PARAM_MAX_ITER = "maxIter"; |
38 | | - |
39 | | - protected FunctionPlotterCtrl funcPlotterCtrl; |
40 | | - private SimulationPaneCtrl simPaneCtrl; |
41 | | - private GeneticMaximumFinderProg experiment; |
42 | | - |
43 | | - @Override |
44 | | - public String getTitle() { |
45 | | - return "Genetic Maxium Finder App"; |
46 | | - } |
47 | | - |
48 | | - /** |
49 | | - * Defines state view, parameters, and call-back functions and calls the |
50 | | - * simulation pane builder to create layout and controller objects. |
51 | | - */ |
52 | | - @Override |
53 | | - public Pane createRootPane() { |
54 | | - BorderPane root = new BorderPane(); |
55 | | - |
56 | | - Canvas canvas = new Canvas(); |
57 | | - funcPlotterCtrl = new FunctionPlotterCtrl(canvas); |
58 | | - funcPlotterCtrl.setLimits(Functions.minX, Functions.maxX, Functions.minY, Functions.maxY); |
59 | | - Parameter[] params = createParameters(); |
60 | | - |
61 | | - SimulationPaneBuilder builder = new SimulationPaneBuilder(); |
62 | | - builder.defineParameters(params); |
63 | | - builder.defineStateView(canvas); |
64 | | - builder.defineInitMethod(this::initialize); |
65 | | - builder.defineSimMethod(this::simulate); |
66 | | - simPaneCtrl = builder.getResultFor(root); |
67 | | - |
68 | | - return root; |
69 | | - } |
70 | | - |
71 | | - protected Parameter[] createParameters() { |
72 | | - Parameter p1 = new Parameter(PARAM_FUNC_SELECT); |
73 | | - p1.setValues(Functions.f1, Functions.f2, Functions.f3); |
74 | | - p1.setValueNames("f1", "f2", "f3"); |
75 | | - Parameter p2 = new Parameter(PARAM_MUT_PROB, 0.0, 0.2, 0.5, 1.0); |
76 | | - p2.setDefaultValueIndex(1); |
77 | | - Parameter p3 = new Parameter(PARAM_POPULATION, 2, 10, 20, 100); |
78 | | - p3.setDefaultValueIndex(2); |
79 | | - Parameter p4 = new Parameter(PARAM_MAX_ITER, 100, 200, 400); |
80 | | - p4.setDefaultValueIndex(0); |
81 | | - return new Parameter[] {p1, p2, p3, p4}; |
82 | | - } |
83 | | - |
84 | | - /** Displays the selected function on the state view. */ |
85 | | - @SuppressWarnings("unchecked") |
86 | | - @Override |
87 | | - public void initialize() { |
88 | | - funcPlotterCtrl.setFunction((Function<Double, Double>) simPaneCtrl.getParamValue(PARAM_FUNC_SELECT)); |
89 | | - } |
90 | | - |
91 | | - @Override |
92 | | - public void finalize() { |
93 | | - simPaneCtrl.cancelSimulation(); |
94 | | - } |
95 | | - |
96 | | - /** Starts the experiment. */ |
97 | | - @SuppressWarnings("unchecked") |
98 | | - public void simulate() { |
99 | | - experiment = new GeneticMaximumFinderProg(); |
100 | | - experiment.setFunction((Function<Double, Double>) simPaneCtrl.getParamValue(PARAM_FUNC_SELECT)); |
101 | | - experiment.setMutationProb(simPaneCtrl.getParamAsDouble(PARAM_MUT_PROB)); |
102 | | - experiment.setPopulationSize(simPaneCtrl.getParamAsInt(PARAM_POPULATION)); |
103 | | - experiment.setMaxIterations(simPaneCtrl.getParamAsInt(PARAM_MAX_ITER)); |
104 | | - experiment.startExperiment(this::updateStateView); |
105 | | - } |
106 | | - |
107 | | - /** |
108 | | - * Caution: While the background thread should be slowed down, updates of |
109 | | - * the GUI have to be done in the GUI thread! |
110 | | - */ |
111 | | - private void updateStateView(int itCount, Collection<Individual<Double>> gen) { |
112 | | - Platform.runLater(() -> updateStateViewLater(itCount, gen)); |
113 | | - simPaneCtrl.waitAfterStep(); |
114 | | - } |
115 | | - |
116 | | - /** |
117 | | - * Must be called by the GUI thread! |
118 | | - */ |
119 | | - private void updateStateViewLater(int itCount, Collection<Individual<Double>> gen) { |
120 | | - funcPlotterCtrl.update(); |
121 | | - if (gen != null) { |
122 | | - for (Individual<Double> ind : gen) { |
123 | | - Optional<Paint> fill = Optional.empty(); |
124 | | - if (ind.getDescendants() > 0) |
125 | | - fill = Optional.of(Color.rgb(Math.max(255 - ind.getDescendants() * 20, 0), 0, 0)); |
126 | | - else |
127 | | - fill = Optional.of(Color.RED.brighter()); |
128 | | - double x = ind.getRepresentation().get(0); |
129 | | - funcPlotterCtrl.setMarker(x, fill); |
130 | | - } |
131 | | - simPaneCtrl.setStatus(experiment.getIterationInfo(itCount, gen)); |
132 | | - } else { |
133 | | - simPaneCtrl.setStatus(""); |
134 | | - } |
135 | | - } |
136 | | -} |
| 1 | +package aima.gui.fx.demo.search.local; |
| 2 | + |
| 3 | +import java.util.Collection; |
| 4 | +import java.util.Optional; |
| 5 | +import java.util.function.Function; |
| 6 | + |
| 7 | +import aima.core.search.local.Individual; |
| 8 | +import aima.gui.fx.framework.IntegrableApplication; |
| 9 | +import aima.gui.fx.framework.Parameter; |
| 10 | +import aima.gui.fx.framework.SimulationPaneBuilder; |
| 11 | +import aima.gui.fx.framework.SimulationPaneCtrl; |
| 12 | +import aima.gui.fx.views.FunctionPlotterCtrl; |
| 13 | +import javafx.application.Platform; |
| 14 | +import javafx.scene.canvas.Canvas; |
| 15 | +import javafx.scene.layout.BorderPane; |
| 16 | +import javafx.scene.layout.Pane; |
| 17 | +import javafx.scene.paint.Color; |
| 18 | +import javafx.scene.paint.Paint; |
| 19 | + |
| 20 | + |
| 21 | +/** |
| 22 | + * Demonstrates, how the genetic algorithm can be used, to find maximums in |
| 23 | + * mathematical functions. Different parameter settings can be tried out and |
| 24 | + * progress shown for each iteration. |
| 25 | + * |
| 26 | + * @author Ruediger Lunde |
| 27 | + */ |
| 28 | +public class GeneticMaximumFinderApp extends IntegrableApplication { |
| 29 | + |
| 30 | + public static void main(String[] args) { |
| 31 | + launch(args); |
| 32 | + } |
| 33 | + |
| 34 | + public final static String PARAM_FUNC_SELECT = "funcSelect"; |
| 35 | + public final static String PARAM_MUT_PROB = "mutProb"; |
| 36 | + public final static String PARAM_POPULATION = "population"; |
| 37 | + public final static String PARAM_MAX_ITER = "maxIter"; |
| 38 | + |
| 39 | + protected FunctionPlotterCtrl funcPlotterCtrl; |
| 40 | + private SimulationPaneCtrl simPaneCtrl; |
| 41 | + private GeneticMaximumFinderProg experiment; |
| 42 | + |
| 43 | + @Override |
| 44 | + public String getTitle() { |
| 45 | + return "Genetic Maxium Finder App"; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Defines state view, parameters, and call-back functions and calls the |
| 50 | + * simulation pane builder to create layout and controller objects. |
| 51 | + */ |
| 52 | + @Override |
| 53 | + public Pane createRootPane() { |
| 54 | + BorderPane root = new BorderPane(); |
| 55 | + |
| 56 | + Canvas canvas = new Canvas(); |
| 57 | + funcPlotterCtrl = new FunctionPlotterCtrl(canvas); |
| 58 | + funcPlotterCtrl.setLimits(Functions.minX, Functions.maxX, Functions.minY, Functions.maxY); |
| 59 | + Parameter[] params = createParameters(); |
| 60 | + |
| 61 | + SimulationPaneBuilder builder = new SimulationPaneBuilder(); |
| 62 | + builder.defineParameters(params); |
| 63 | + builder.defineStateView(canvas); |
| 64 | + builder.defineInitMethod(this::initialize); |
| 65 | + builder.defineSimMethod(this::simulate); |
| 66 | + simPaneCtrl = builder.getResultFor(root); |
| 67 | + |
| 68 | + return root; |
| 69 | + } |
| 70 | + |
| 71 | + protected Parameter[] createParameters() { |
| 72 | + Parameter p1 = new Parameter(PARAM_FUNC_SELECT); |
| 73 | + p1.setValues(Functions.f1, Functions.f2, Functions.f3); |
| 74 | + p1.setValueNames("f1", "f2", "f3"); |
| 75 | + Parameter p2 = new Parameter(PARAM_MUT_PROB, 0.0, 0.2, 0.5, 1.0); |
| 76 | + p2.setDefaultValueIndex(1); |
| 77 | + Parameter p3 = new Parameter(PARAM_POPULATION, 2, 10, 20, 100); |
| 78 | + p3.setDefaultValueIndex(2); |
| 79 | + Parameter p4 = new Parameter(PARAM_MAX_ITER, 100, 200, 400); |
| 80 | + p4.setDefaultValueIndex(0); |
| 81 | + return new Parameter[] {p1, p2, p3, p4}; |
| 82 | + } |
| 83 | + |
| 84 | + /** Displays the selected function on the state view. */ |
| 85 | + @SuppressWarnings("unchecked") |
| 86 | + @Override |
| 87 | + public void initialize() { |
| 88 | + funcPlotterCtrl.setFunction((Function<Double, Double>) simPaneCtrl.getParamValue(PARAM_FUNC_SELECT)); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void finalize() { |
| 93 | + simPaneCtrl.cancelSimulation(); |
| 94 | + } |
| 95 | + |
| 96 | + /** Starts the experiment. */ |
| 97 | + @SuppressWarnings("unchecked") |
| 98 | + public void simulate() { |
| 99 | + experiment = new GeneticMaximumFinderProg(); |
| 100 | + experiment.setFunction((Function<Double, Double>) simPaneCtrl.getParamValue(PARAM_FUNC_SELECT)); |
| 101 | + experiment.setMutationProb(simPaneCtrl.getParamAsDouble(PARAM_MUT_PROB)); |
| 102 | + experiment.setPopulationSize(simPaneCtrl.getParamAsInt(PARAM_POPULATION)); |
| 103 | + experiment.setMaxIterations(simPaneCtrl.getParamAsInt(PARAM_MAX_ITER)); |
| 104 | + experiment.startExperiment(this::updateStateView); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Caution: While the background thread should be slowed down, updates of |
| 109 | + * the GUI have to be done in the GUI thread! |
| 110 | + */ |
| 111 | + private void updateStateView(int itCount, Collection<Individual<Double>> gen) { |
| 112 | + Platform.runLater(() -> updateStateViewLater(itCount, gen)); |
| 113 | + simPaneCtrl.waitAfterStep(); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Must be called by the GUI thread! |
| 118 | + */ |
| 119 | + private void updateStateViewLater(int itCount, Collection<Individual<Double>> gen) { |
| 120 | + funcPlotterCtrl.update(); |
| 121 | + if (gen != null) { |
| 122 | + for (Individual<Double> ind : gen) { |
| 123 | + Optional<Paint> fill = Optional.empty(); |
| 124 | + if (ind.getDescendants() > 0) |
| 125 | + fill = Optional.of(Color.rgb(Math.max(255 - ind.getDescendants() * 20, 0), 0, 0)); |
| 126 | + else |
| 127 | + fill = Optional.of(Color.RED.brighter()); |
| 128 | + double x = ind.getRepresentation().get(0); |
| 129 | + funcPlotterCtrl.setMarker(x, fill); |
| 130 | + } |
| 131 | + simPaneCtrl.setStatus(experiment.getIterationInfo(itCount, gen)); |
| 132 | + } else { |
| 133 | + simPaneCtrl.setStatus(""); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments