1 package ru.bosony.solvers; |
1 package ru.bosony.solvers; |
2 |
2 |
3 import java.util.ArrayList; |
3 import java.util.ArrayList; |
|
4 import java.util.HashMap; |
4 import java.util.HashSet; |
5 import java.util.HashSet; |
5 import java.util.List; |
6 import java.util.List; |
|
7 import java.util.Map; |
6 import java.util.Set; |
8 import java.util.Set; |
7 |
9 |
8 import ru.bosony.graph.CellsLink; |
10 import ru.bosony.graph.CellsLink; |
9 import ru.bosony.graph.CellsLinkTransformer; |
11 import ru.bosony.graph.CellsLinkTransformer; |
10 import ru.bosony.model.cellscontents.CellContent; |
12 import ru.bosony.model.cellscontents.CellContent; |
11 import ru.bosony.model.game.Game; |
13 import ru.bosony.model.game.Game; |
12 import ru.bosony.model.game.GameState; |
14 import ru.bosony.model.game.GameState; |
13 import ru.bosony.model.mine.Cell; |
15 import ru.bosony.model.mine.Cell; |
14 import ru.bosony.model.mine.Mine; |
16 import ru.bosony.model.mine.Mine; |
|
17 import ru.bosony.model.moving.Coordinate; |
15 import ru.bosony.model.moving.Movement; |
18 import ru.bosony.model.moving.Movement; |
16 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath; |
19 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath; |
17 import edu.uci.ics.jung.graph.DirectedSparseGraph; |
20 import edu.uci.ics.jung.graph.DirectedSparseGraph; |
18 import edu.uci.ics.jung.graph.Graph; |
21 import edu.uci.ics.jung.graph.Graph; |
19 |
22 |
22 * 16.07.2012 11:05:26 |
25 * 16.07.2012 11:05:26 |
23 * |
26 * |
24 */ |
27 */ |
25 public class DijkstraSolver extends AbstractSolver { |
28 public class DijkstraSolver extends AbstractSolver { |
26 |
29 |
|
30 protected Map<Mine, Set<Cell>> testedTargetCells = new HashMap<Mine, Set<Cell>>(); |
|
31 |
27 public DijkstraSolver(Mine mine, SolverListener listener) { |
32 public DijkstraSolver(Mine mine, SolverListener listener) { |
28 super(mine, listener); |
33 super(mine, listener); |
29 } |
34 } |
30 |
35 |
31 @Override |
36 @Override |
32 protected void solve() { |
37 public void solve() { |
33 while (true) { |
38 while (true) { |
34 Game game = new Game(mine); |
39 Mine testMine = null; |
|
40 try { |
|
41 testMine = mine.clone(); |
|
42 } catch (CloneNotSupportedException e1) { |
|
43 throw new RuntimeException(e1); |
|
44 } |
|
45 Game game = new Game(testMine); |
35 |
46 |
36 while (game.getState() == GameState.Game) { |
47 while (game.getState() == GameState.Game) { |
37 try { |
48 if (!testedTargetCells.containsKey(testMine)) { |
38 Set<Cell> targets = mine.findCells(CellContent.Lambda); |
49 testedTargetCells.put(testMine, new HashSet<Cell>()); |
39 targets.addAll(mine.findCells(CellContent.HighOrderRock)); |
50 } |
40 if (targets.size() == 0) |
51 Set<Cell> targets = testMine.findCells(CellContent.Lambda); |
41 targets = mine.findCells(CellContent.OpenLambdaLift); |
52 targets.addAll(testMine.findCells(CellContent.HighOrderRock)); |
42 if (targets.size() == 0) |
53 if (targets.size() == 0) |
|
54 targets = testMine.findCells(CellContent.OpenLambdaLift); |
|
55 if (targets.size() == 0 || testedTargetCells.get(testMine).containsAll(targets)) { |
|
56 // No targets. Walking around |
|
57 Cell robotCell = testMine.getRobotCell(); |
|
58 for (Coordinate nextCoordinate : robotCell.getCoordinate().getAdjacentCoordinates()) { |
|
59 Cell nextCell = testMine.getCell(nextCoordinate); |
|
60 targets.add(nextCell); |
|
61 } |
|
62 if (testedTargetCells.get(testMine).containsAll(targets)) { |
|
63 // Fail. All cases are tried. |
|
64 game.move(Movement.ABORT); |
43 break; |
65 break; |
44 Graph<Cell, CellsLink> graph = getDirectedGraph(); |
|
45 DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph, |
|
46 new CellsLinkTransformer<CellsLink, Double>()); |
|
47 Cell nextTarget = null; |
|
48 List<CellsLink> routeToNextTarget = null; |
|
49 double shortestDist = Double.MAX_VALUE; |
|
50 for (Cell target : targets) { |
|
51 List<CellsLink> route = alg.getPath(mine.getRobotCell(), target); |
|
52 Number dist = alg.getDistance(mine.getRobotCell(), target); |
|
53 if (dist != null && dist.doubleValue() < shortestDist) { |
|
54 shortestDist = dist.doubleValue(); |
|
55 nextTarget = target; |
|
56 routeToNextTarget = route; |
|
57 } |
|
58 } |
66 } |
59 if (nextTarget != null) { |
67 } |
60 for (CellsLink link : routeToNextTarget) { |
68 Graph<Cell, CellsLink> graph = getDirectedGraph(testMine); |
61 Movement mov = link.getSource().getCoordinate() |
69 DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph, |
62 .getNecessaryMovement(link.getTarget().getCoordinate()); |
70 new CellsLinkTransformer<CellsLink, Double>()); |
63 game.move(mov); |
71 Cell nextTarget = null; |
64 break; |
72 List<CellsLink> routeToNextTarget = null; |
65 } |
73 double shortestDist = Double.MAX_VALUE; |
|
74 for (Cell target : targets) { |
|
75 if (target == null) |
|
76 continue; |
|
77 List<CellsLink> route = null; |
|
78 Number dist = null; |
|
79 try { |
|
80 route = alg.getPath(testMine.getRobotCell(), target); |
|
81 dist = alg.getDistance(testMine.getRobotCell(), target); |
|
82 } catch (IllegalArgumentException e) { |
|
83 // hmmm... |
|
84 testedTargetCells.get(testMine).add(target); |
|
85 continue; |
66 } |
86 } |
67 } catch (IllegalArgumentException e) { |
87 if (dist != null && route != null && dist.doubleValue() < shortestDist |
68 // hmmm... |
88 && !testedTargetCells.get(testMine).contains(target)) { |
|
89 shortestDist = dist.doubleValue(); |
|
90 nextTarget = target; |
|
91 routeToNextTarget = route; |
|
92 testedTargetCells.get(testMine).add(target); |
|
93 } |
|
94 } |
|
95 if (nextTarget != null) { |
|
96 for (CellsLink link : routeToNextTarget) { |
|
97 Movement mov = link.getSource().getCoordinate() |
|
98 .getNecessaryMovement(link.getTarget().getCoordinate()); |
|
99 game.move(mov); |
|
100 break; |
|
101 } |
69 } |
102 } |
70 } |
103 } |
71 if (game.getState() == GameState.Win) { |
104 if (game.getState() == GameState.Win) { |
72 addNewRoute(game.getStringRoute(), game.getScore()); |
105 addNewRoute(game.getStringRoute(), game.getScore()); |
73 } |
106 } |
74 attemptsCount++; |
107 attemptsCount++; |
75 } |
108 } |
76 } |
109 } |
77 |
110 |
78 protected Graph<Cell, CellsLink> getDirectedGraph() { |
111 protected Graph<Cell, CellsLink> getDirectedGraph(Mine mine) { |
79 Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>(); |
112 Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>(); |
80 for (CellsLink link : getCellLinks()) { |
113 for (CellsLink link : getCellLinks(mine)) { |
81 graph.addEdge(link, link.getSource(), link.getTarget()); |
114 graph.addEdge(link, link.getSource(), link.getTarget()); |
82 } |
115 } |
83 return graph; |
116 return graph; |
84 } |
117 } |
85 |
118 |
86 protected List<CellsLink> getCellLinks() { |
119 protected List<CellsLink> getCellLinks(Mine mine) { |
87 List<CellsLink> links = new ArrayList<CellsLink>(); |
120 List<CellsLink> links = new ArrayList<CellsLink>(); |
88 Cell robotCell = mine.getRobotCell(); |
121 Cell robotCell = mine.getRobotCell(); |
89 findAllCellLinks(robotCell, links, new HashSet<Cell>()); |
122 findAllCellLinks(mine, robotCell, links, new HashSet<Cell>()); |
90 return links; |
123 return links; |
91 } |
124 } |
92 |
125 |
93 protected void findAllCellLinks(Cell cell, List<CellsLink> links, Set<Cell> processedCells) { |
126 protected void findAllCellLinks(Mine mine, Cell cell, List<CellsLink> links, Set<Cell> processedCells) { |
94 if (processedCells.contains(cell)) |
127 if (processedCells.contains(cell)) |
95 return; |
128 return; |
96 else |
129 else |
97 processedCells.add(cell); |
130 processedCells.add(cell); |
98 for (Cell neighboringCell : mine.getAdjacentCells(cell)) { |
131 for (Cell neighboringCell : mine.getAdjacentCells(cell)) { |
102 if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty |
135 if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty |
103 || neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda |
136 || neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda |
104 || neighboringContent == CellContent.OpenLambdaLift) { |
137 || neighboringContent == CellContent.OpenLambdaLift) { |
105 CellsLink link = new CellsLink(mine, cell, neighboringCell); |
138 CellsLink link = new CellsLink(mine, cell, neighboringCell); |
106 links.add(link); |
139 links.add(link); |
107 findAllCellLinks(neighboringCell, links, processedCells); |
140 findAllCellLinks(mine, neighboringCell, links, processedCells); |
108 } else if (!CellContent.getTargets().contains(cell.getContent()) |
141 } else if (!CellContent.getTargets().contains(cell.getContent()) |
109 && CellContent.getTargets().contains(neighboringContent)) { |
142 && CellContent.getTargets().contains(neighboringContent)) { |
110 findAllCellLinks(neighboringCell, links, processedCells); |
143 findAllCellLinks(mine, neighboringCell, links, processedCells); |
111 } |
144 } |
112 } |
145 } |
113 if (CellContent.getTrampolines().contains(cell.getContent())) { |
146 if (CellContent.getTrampolines().contains(cell.getContent())) { |
114 for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) { |
147 for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) { |
115 if (target == null) |
148 if (target == null) |
116 continue; |
149 continue; |
117 CellsLink link = new CellsLink(mine, cell, target); |
150 CellsLink link = new CellsLink(mine, cell, target); |
118 links.add(link); |
151 links.add(link); |
119 findAllCellLinks(target, links, processedCells); |
152 findAllCellLinks(mine, target, links, processedCells); |
120 } |
153 } |
121 } |
154 } |
122 } |
155 } |
123 } |
156 } |