|
| 1 | +package aima.gui.fx.demo.search; |
| 2 | + |
| 3 | +import aima.core.search.csp.*; |
| 4 | +import aima.gui.fx.framework.IntegrableApplication; |
| 5 | +import aima.gui.fx.framework.Parameter; |
| 6 | +import aima.gui.fx.framework.SimulationPaneBuilder; |
| 7 | +import aima.gui.fx.framework.SimulationPaneCtrl; |
| 8 | +import aima.gui.fx.views.BinaryCspViewCtrl; |
| 9 | +import aima.gui.fx.views.NQueensViewCtrl; |
| 10 | +import javafx.application.Platform; |
| 11 | +import javafx.scene.layout.BorderPane; |
| 12 | +import javafx.scene.layout.Pane; |
| 13 | +import javafx.scene.layout.StackPane; |
| 14 | +import javafx.scene.paint.Color; |
| 15 | + |
| 16 | +/** |
| 17 | + * Application which demonstrates basic constraint algorithms based on map |
| 18 | + * coloring problems. It shows the constraint graph, lets the user select a |
| 19 | + * solution strategy, and allows then to follow the progress step by step. |
| 20 | + * |
| 21 | + * @author Ruediger Lunde |
| 22 | + */ |
| 23 | +public class MapColoringApp extends IntegrableApplication { |
| 24 | + |
| 25 | + public static void main(String[] args) { |
| 26 | + launch(args); |
| 27 | + } |
| 28 | + |
| 29 | + public final static String PARAM_MAP = "map"; |
| 30 | + public final static String PARAM_STRATEGY = "strategy"; |
| 31 | + |
| 32 | + private BinaryCspViewCtrl stateViewCtrl; |
| 33 | + private SimulationPaneCtrl simPaneCtrl; |
| 34 | + |
| 35 | + private CSP csp; |
| 36 | + private SolutionStrategy strategy; |
| 37 | + private int stepCounter; |
| 38 | + |
| 39 | + public MapColoringApp() { |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public String getTitle() { |
| 44 | + return "Map Coloring App"; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Defines state view, parameters, and call-back functions and calls the |
| 49 | + * simulation pane builder to create layout and controller objects. |
| 50 | + */ |
| 51 | + @Override |
| 52 | + public Pane createRootPane() { |
| 53 | + BorderPane root = new BorderPane(); |
| 54 | + |
| 55 | + StackPane stateView = new StackPane(); |
| 56 | + stateViewCtrl = new BinaryCspViewCtrl(stateView); |
| 57 | + |
| 58 | + Parameter[] params = createParameters(); |
| 59 | + |
| 60 | + SimulationPaneBuilder builder = new SimulationPaneBuilder(); |
| 61 | + builder.defineParameters(params); |
| 62 | + builder.defineStateView(stateView); |
| 63 | + builder.defineInitMethod(this::initialize); |
| 64 | + builder.defineSimMethod(this::simulate); |
| 65 | + simPaneCtrl = builder.getResultFor(root); |
| 66 | + |
| 67 | + return root; |
| 68 | + } |
| 69 | + |
| 70 | + protected Parameter[] createParameters() { |
| 71 | + Parameter p1 = new Parameter(PARAM_MAP, "Map of Australia", |
| 72 | + "Map of Australia NSW=BLUE (for LCV)", |
| 73 | + "Map of Australia WA=RED (for LCV)"); |
| 74 | + Parameter p2 = new Parameter(PARAM_STRATEGY, "Backtracking", |
| 75 | + "Backtracking + MRV & DEG", |
| 76 | + "Backtracking + Forward Checking", |
| 77 | + "Backtracking + Forward Checking + MRV", |
| 78 | + "Backtracking + Forward Checking + LCV", |
| 79 | + "Backtracking + AC3", |
| 80 | + "Backtracking + AC3 + MRV & DEG + LCV", |
| 81 | + "Min-Conflicts (50)"); |
| 82 | + return new Parameter[]{p1, p2}; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Displays the selected function on the state view. |
| 87 | + */ |
| 88 | + @Override |
| 89 | + public void initialize() { |
| 90 | + csp = null; |
| 91 | + switch (simPaneCtrl.getParamValueIndex(PARAM_MAP)) { |
| 92 | + case 0: |
| 93 | + csp = new MapCSP(); |
| 94 | + break; |
| 95 | + case 1: // three moves |
| 96 | + csp = new MapCSP(); |
| 97 | + csp.setDomain(MapCSP.NSW, new Domain(new Object[]{MapCSP.BLUE})); |
| 98 | + break; |
| 99 | + case 2: // three moves |
| 100 | + csp = new MapCSP(); |
| 101 | + csp.setDomain(MapCSP.WA, new Domain(new Object[]{MapCSP.RED})); |
| 102 | + break; |
| 103 | + } |
| 104 | + |
| 105 | + stateViewCtrl.clearMappings(); |
| 106 | + int c = 15; |
| 107 | + stateViewCtrl.setPositionMapping(MapCSP.WA, c*5, c*10); |
| 108 | + stateViewCtrl.setPositionMapping(MapCSP.NT, c*15, c*3); |
| 109 | + stateViewCtrl.setPositionMapping(MapCSP.SA, c*20, c*15); |
| 110 | + stateViewCtrl.setPositionMapping(MapCSP.Q, c*30, c*5); |
| 111 | + stateViewCtrl.setPositionMapping(MapCSP.NSW, c*35, c*15); |
| 112 | + stateViewCtrl.setPositionMapping(MapCSP.V, c*30, c*23); |
| 113 | + stateViewCtrl.setPositionMapping(MapCSP.T, c*33, c*30); |
| 114 | + |
| 115 | + stateViewCtrl.setColorMapping(MapCSP.RED, Color.RED); |
| 116 | + stateViewCtrl.setColorMapping(MapCSP.GREEN, Color.GREEN); |
| 117 | + stateViewCtrl.setColorMapping(MapCSP.BLUE, Color.BLUE); |
| 118 | + |
| 119 | + ImprovedBacktrackingStrategy iStrategy = null; |
| 120 | + switch (simPaneCtrl.getParamValueIndex(PARAM_STRATEGY)) { |
| 121 | + case 0: |
| 122 | + strategy = new BacktrackingStrategy(); |
| 123 | + break; |
| 124 | + case 1: // MRV + DEG |
| 125 | + strategy = new ImprovedBacktrackingStrategy |
| 126 | + (true, true, false, false); |
| 127 | + break; |
| 128 | + case 2: // FC |
| 129 | + iStrategy = new ImprovedBacktrackingStrategy(); |
| 130 | + iStrategy.setInference(ImprovedBacktrackingStrategy |
| 131 | + .Inference.FORWARD_CHECKING); |
| 132 | + break; |
| 133 | + case 3: // MRV + FC |
| 134 | + iStrategy = new ImprovedBacktrackingStrategy |
| 135 | + (true, false, false, false); |
| 136 | + iStrategy.setInference(ImprovedBacktrackingStrategy |
| 137 | + .Inference.FORWARD_CHECKING); |
| 138 | + break; |
| 139 | + case 4: // FC + LCV |
| 140 | + iStrategy = new ImprovedBacktrackingStrategy |
| 141 | + (false, false, false, true); |
| 142 | + iStrategy.setInference(ImprovedBacktrackingStrategy |
| 143 | + .Inference.FORWARD_CHECKING); |
| 144 | + break; |
| 145 | + case 5: // AC3 |
| 146 | + strategy = new ImprovedBacktrackingStrategy |
| 147 | + (false, false, true, false); |
| 148 | + break; |
| 149 | + case 6: // MRV + DEG + AC3 + LCV |
| 150 | + strategy = new ImprovedBacktrackingStrategy |
| 151 | + (true, true, true, true); |
| 152 | + break; |
| 153 | + case 7: |
| 154 | + strategy = new MinConflictsStrategy(50); |
| 155 | + break; |
| 156 | + } |
| 157 | + if (iStrategy != null) |
| 158 | + strategy = iStrategy; |
| 159 | + |
| 160 | + strategy.addCSPStateListener(new CSPStateListener() { |
| 161 | + |
| 162 | + @Override |
| 163 | + public void stateChanged(Assignment assignment, CSP csp) { |
| 164 | + stepCounter++; |
| 165 | + updateStateView(csp, assignment); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void stateChanged(CSP csp) { |
| 170 | + stepCounter++; |
| 171 | + updateStateView(csp, null); |
| 172 | + } |
| 173 | + }); |
| 174 | + |
| 175 | + stateViewCtrl.initialize(csp); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public void finalize() { |
| 180 | + simPaneCtrl.cancelSimulation(); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Starts the experiment. |
| 185 | + */ |
| 186 | + public void simulate() { |
| 187 | + stepCounter = 0; |
| 188 | + Assignment result = strategy.solve(csp.copyDomains()); |
| 189 | + updateStateView(csp, result); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Caution: While the background thread should be slowed down, updates of |
| 194 | + * the GUI have to be done in the GUI thread! |
| 195 | + */ |
| 196 | + private void updateStateView(CSP csp, Assignment assignment) { |
| 197 | + Platform.runLater(() -> updateStateViewLater(csp, assignment)); |
| 198 | + simPaneCtrl.waitAfterStep(); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Must be called by the GUI thread! |
| 203 | + */ |
| 204 | + private void updateStateViewLater(CSP csp, Assignment assignment) { |
| 205 | + stateViewCtrl.update(csp, assignment); |
| 206 | + String txt1 = "Step " + stepCounter + ": "; |
| 207 | + String txt2 = assignment != null ? assignment.toString() : "Domain reduced"; |
| 208 | + simPaneCtrl.setStatus(txt1 + txt2); |
| 209 | + } |
| 210 | +} |
0 commit comments