|
indvdum@42
|
1 |
package ru.bosony.solvers;
|
|
indvdum@42
|
2 |
|
|
indvdum@42
|
3 |
import java.util.ArrayList;
|
|
indvdum@46
|
4 |
import java.util.HashMap;
|
|
indvdum@42
|
5 |
import java.util.HashSet;
|
|
indvdum@42
|
6 |
import java.util.List;
|
|
indvdum@46
|
7 |
import java.util.Map;
|
|
indvdum@42
|
8 |
import java.util.Set;
|
|
indvdum@42
|
9 |
|
|
indvdum@42
|
10 |
import ru.bosony.graph.CellsLink;
|
|
indvdum@42
|
11 |
import ru.bosony.graph.CellsLinkTransformer;
|
|
indvdum@42
|
12 |
import ru.bosony.model.cellscontents.CellContent;
|
|
indvdum@42
|
13 |
import ru.bosony.model.game.Game;
|
|
indvdum@42
|
14 |
import ru.bosony.model.game.GameState;
|
|
indvdum@42
|
15 |
import ru.bosony.model.mine.Cell;
|
|
indvdum@42
|
16 |
import ru.bosony.model.mine.Mine;
|
|
indvdum@42
|
17 |
import ru.bosony.model.moving.Movement;
|
|
indvdum@42
|
18 |
import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
|
|
indvdum@42
|
19 |
import edu.uci.ics.jung.graph.DirectedSparseGraph;
|
|
indvdum@42
|
20 |
import edu.uci.ics.jung.graph.Graph;
|
|
indvdum@42
|
21 |
|
|
indvdum@42
|
22 |
/**
|
|
indvdum@42
|
23 |
* @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
|
|
indvdum@42
|
24 |
* 16.07.2012 11:05:26
|
|
indvdum@42
|
25 |
*
|
|
indvdum@42
|
26 |
*/
|
|
indvdum@42
|
27 |
public class DijkstraSolver extends AbstractSolver {
|
|
indvdum@42
|
28 |
|
|
indvdum@47
|
29 |
protected Map<String, Set<Movement>> loseMovs = new HashMap<String, Set<Movement>>();
|
|
indvdum@47
|
30 |
protected Set<CellsLink> loseLinks = new HashSet<CellsLink>();
|
|
indvdum@46
|
31 |
|
|
indvdum@42
|
32 |
public DijkstraSolver(Mine mine, SolverListener listener) {
|
|
indvdum@42
|
33 |
super(mine, listener);
|
|
indvdum@42
|
34 |
}
|
|
indvdum@42
|
35 |
|
|
indvdum@42
|
36 |
@Override
|
|
indvdum@46
|
37 |
public void solve() {
|
|
indvdum@42
|
38 |
while (true) {
|
|
indvdum@46
|
39 |
Mine testMine = null;
|
|
indvdum@46
|
40 |
try {
|
|
indvdum@46
|
41 |
testMine = mine.clone();
|
|
indvdum@46
|
42 |
} catch (CloneNotSupportedException e1) {
|
|
indvdum@46
|
43 |
throw new RuntimeException(e1);
|
|
indvdum@46
|
44 |
}
|
|
indvdum@46
|
45 |
Game game = new Game(testMine);
|
|
indvdum@42
|
46 |
|
|
indvdum@42
|
47 |
while (game.getState() == GameState.Game) {
|
|
indvdum@47
|
48 |
String map = testMine.toText();
|
|
indvdum@47
|
49 |
if (!loseMovs.containsKey(map)) {
|
|
indvdum@47
|
50 |
loseMovs.put(map, new HashSet<Movement>());
|
|
indvdum@46
|
51 |
}
|
|
indvdum@46
|
52 |
Set<Cell> targets = testMine.findCells(CellContent.Lambda);
|
|
indvdum@46
|
53 |
targets.addAll(testMine.findCells(CellContent.HighOrderRock));
|
|
indvdum@46
|
54 |
if (targets.size() == 0)
|
|
indvdum@46
|
55 |
targets = testMine.findCells(CellContent.OpenLambdaLift);
|
|
indvdum@46
|
56 |
Graph<Cell, CellsLink> graph = getDirectedGraph(testMine);
|
|
indvdum@46
|
57 |
DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
|
|
indvdum@46
|
58 |
new CellsLinkTransformer<CellsLink, Double>());
|
|
indvdum@46
|
59 |
Cell nextTarget = null;
|
|
indvdum@46
|
60 |
List<CellsLink> routeToNextTarget = null;
|
|
indvdum@46
|
61 |
double shortestDist = Double.MAX_VALUE;
|
|
indvdum@46
|
62 |
for (Cell target : targets) {
|
|
indvdum@46
|
63 |
if (target == null)
|
|
indvdum@46
|
64 |
continue;
|
|
indvdum@46
|
65 |
List<CellsLink> route = null;
|
|
indvdum@46
|
66 |
Number dist = null;
|
|
indvdum@46
|
67 |
try {
|
|
indvdum@46
|
68 |
route = alg.getPath(testMine.getRobotCell(), target);
|
|
indvdum@46
|
69 |
dist = alg.getDistance(testMine.getRobotCell(), target);
|
|
indvdum@46
|
70 |
} catch (IllegalArgumentException e) {
|
|
indvdum@46
|
71 |
// hmmm...
|
|
indvdum@46
|
72 |
continue;
|
|
indvdum@42
|
73 |
}
|
|
indvdum@47
|
74 |
if (dist != null && route != null && route.size() > 0 && dist.doubleValue() < shortestDist) {
|
|
indvdum@47
|
75 |
CellsLink firstLink = route.get(0);
|
|
indvdum@47
|
76 |
Movement mov = firstLink.getSource().getCoordinate()
|
|
indvdum@47
|
77 |
.getNecessaryMovement(firstLink.getTarget().getCoordinate());
|
|
indvdum@47
|
78 |
if (!loseMovs.get(map).contains(mov)) {
|
|
indvdum@47
|
79 |
shortestDist = dist.doubleValue();
|
|
indvdum@47
|
80 |
nextTarget = target;
|
|
indvdum@47
|
81 |
routeToNextTarget = route;
|
|
indvdum@47
|
82 |
break;
|
|
indvdum@47
|
83 |
}
|
|
indvdum@46
|
84 |
}
|
|
indvdum@46
|
85 |
}
|
|
indvdum@46
|
86 |
if (nextTarget != null) {
|
|
indvdum@46
|
87 |
for (CellsLink link : routeToNextTarget) {
|
|
indvdum@47
|
88 |
Cell source = link.getSource();
|
|
indvdum@47
|
89 |
Cell target = link.getTarget();
|
|
indvdum@47
|
90 |
if (isSafeMovement(testMine, source, target)) {
|
|
indvdum@47
|
91 |
Movement mov = source.getCoordinate().getNecessaryMovement(target.getCoordinate());
|
|
indvdum@47
|
92 |
GameState state = game.move(mov);
|
|
indvdum@47
|
93 |
if (state == GameState.Lose || state == GameState.Abort || game.hasViciousCircle()) {
|
|
indvdum@47
|
94 |
loseMovs.get(map).add(mov);
|
|
indvdum@47
|
95 |
loseLinks.add(link);
|
|
indvdum@47
|
96 |
}
|
|
indvdum@47
|
97 |
}
|
|
indvdum@46
|
98 |
break;
|
|
indvdum@46
|
99 |
}
|
|
indvdum@47
|
100 |
} else {
|
|
indvdum@47
|
101 |
for (Movement mov : Movement.values()) {
|
|
indvdum@47
|
102 |
if (!loseMovs.get(map).contains(mov)) {
|
|
indvdum@47
|
103 |
GameState state = game.move(mov);
|
|
indvdum@47
|
104 |
if (state == GameState.Lose || state == GameState.Abort || map.equals(testMine.toText())
|
|
indvdum@47
|
105 |
|| game.hasViciousCircle())
|
|
indvdum@47
|
106 |
loseMovs.get(map).add(mov);
|
|
indvdum@47
|
107 |
break;
|
|
indvdum@47
|
108 |
}
|
|
indvdum@47
|
109 |
}
|
|
indvdum@42
|
110 |
}
|
|
indvdum@42
|
111 |
}
|
|
indvdum@42
|
112 |
if (game.getState() == GameState.Win) {
|
|
indvdum@42
|
113 |
addNewRoute(game.getStringRoute(), game.getScore());
|
|
indvdum@42
|
114 |
}
|
|
indvdum@42
|
115 |
attemptsCount++;
|
|
indvdum@42
|
116 |
}
|
|
indvdum@42
|
117 |
}
|
|
indvdum@42
|
118 |
|
|
indvdum@47
|
119 |
protected boolean isSafeMovement(Mine mine, Cell source, Cell target) {
|
|
indvdum@47
|
120 |
if (source == null || target == null)
|
|
indvdum@47
|
121 |
return false;
|
|
indvdum@47
|
122 |
if (source.getContent().getTrampolineTarget() == target.getContent())
|
|
indvdum@47
|
123 |
return true;
|
|
indvdum@47
|
124 |
Cell upCell = mine.getCell(target.getCoordinate().up());
|
|
indvdum@47
|
125 |
if (upCell == null)
|
|
indvdum@47
|
126 |
return true;
|
|
indvdum@47
|
127 |
Cell upUpCell = mine.getCell(target.getCoordinate().up().up());
|
|
indvdum@47
|
128 |
Cell upUpLeftCell = mine.getCell(target.getCoordinate().up().up().left());
|
|
indvdum@47
|
129 |
Cell upUpRightCell = mine.getCell(target.getCoordinate().up().up().right());
|
|
indvdum@47
|
130 |
if (upCell.getContent() == CellContent.Empty
|
|
indvdum@47
|
131 |
&& (upUpCell != null
|
|
indvdum@47
|
132 |
&& (upUpCell.getContent() == CellContent.Rock || upUpCell.getContent() == CellContent.HighOrderRock)
|
|
indvdum@47
|
133 |
|| upUpLeftCell != null
|
|
indvdum@47
|
134 |
&& (upUpLeftCell.getContent() == CellContent.Rock || upUpLeftCell.getContent() == CellContent.HighOrderRock) || upUpRightCell != null
|
|
indvdum@47
|
135 |
&& (upUpRightCell.getContent() == CellContent.Rock || upUpRightCell.getContent() == CellContent.HighOrderRock))) {
|
|
indvdum@47
|
136 |
return false;
|
|
indvdum@47
|
137 |
}
|
|
indvdum@47
|
138 |
return true;
|
|
indvdum@47
|
139 |
}
|
|
indvdum@47
|
140 |
|
|
indvdum@46
|
141 |
protected Graph<Cell, CellsLink> getDirectedGraph(Mine mine) {
|
|
indvdum@42
|
142 |
Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>();
|
|
indvdum@46
|
143 |
for (CellsLink link : getCellLinks(mine)) {
|
|
indvdum@42
|
144 |
graph.addEdge(link, link.getSource(), link.getTarget());
|
|
indvdum@42
|
145 |
}
|
|
indvdum@42
|
146 |
return graph;
|
|
indvdum@42
|
147 |
}
|
|
indvdum@42
|
148 |
|
|
indvdum@46
|
149 |
protected List<CellsLink> getCellLinks(Mine mine) {
|
|
indvdum@42
|
150 |
List<CellsLink> links = new ArrayList<CellsLink>();
|
|
indvdum@42
|
151 |
Cell robotCell = mine.getRobotCell();
|
|
indvdum@46
|
152 |
findAllCellLinks(mine, robotCell, links, new HashSet<Cell>());
|
|
indvdum@42
|
153 |
return links;
|
|
indvdum@42
|
154 |
}
|
|
indvdum@42
|
155 |
|
|
indvdum@46
|
156 |
protected void findAllCellLinks(Mine mine, Cell cell, List<CellsLink> links, Set<Cell> processedCells) {
|
|
indvdum@42
|
157 |
if (processedCells.contains(cell))
|
|
indvdum@42
|
158 |
return;
|
|
indvdum@42
|
159 |
else
|
|
indvdum@42
|
160 |
processedCells.add(cell);
|
|
indvdum@42
|
161 |
for (Cell neighboringCell : mine.getAdjacentCells(cell)) {
|
|
indvdum@42
|
162 |
if (neighboringCell == null)
|
|
indvdum@42
|
163 |
continue;
|
|
indvdum@42
|
164 |
CellContent neighboringContent = neighboringCell.getContent();
|
|
indvdum@42
|
165 |
if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty
|
|
indvdum@42
|
166 |
|| neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda
|
|
indvdum@42
|
167 |
|| neighboringContent == CellContent.OpenLambdaLift) {
|
|
indvdum@47
|
168 |
if (isSafeMovement(mine, cell, neighboringCell)) {
|
|
indvdum@47
|
169 |
CellsLink link = new CellsLink(mine, cell, neighboringCell);
|
|
indvdum@47
|
170 |
if (!loseLinks.contains(link)) {
|
|
indvdum@47
|
171 |
links.add(link);
|
|
indvdum@47
|
172 |
findAllCellLinks(mine, neighboringCell, links, processedCells);
|
|
indvdum@47
|
173 |
}
|
|
indvdum@47
|
174 |
}
|
|
indvdum@42
|
175 |
} else if (!CellContent.getTargets().contains(cell.getContent())
|
|
indvdum@42
|
176 |
&& CellContent.getTargets().contains(neighboringContent)) {
|
|
indvdum@46
|
177 |
findAllCellLinks(mine, neighboringCell, links, processedCells);
|
|
indvdum@42
|
178 |
}
|
|
indvdum@42
|
179 |
}
|
|
indvdum@42
|
180 |
if (CellContent.getTrampolines().contains(cell.getContent())) {
|
|
indvdum@42
|
181 |
for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) {
|
|
indvdum@42
|
182 |
if (target == null)
|
|
indvdum@42
|
183 |
continue;
|
|
indvdum@47
|
184 |
if (isSafeMovement(mine, cell, target)) {
|
|
indvdum@47
|
185 |
CellsLink link = new CellsLink(mine, cell, target);
|
|
indvdum@47
|
186 |
if (!loseLinks.contains(link)) {
|
|
indvdum@47
|
187 |
links.add(link);
|
|
indvdum@47
|
188 |
findAllCellLinks(mine, target, links, processedCells);
|
|
indvdum@47
|
189 |
}
|
|
indvdum@47
|
190 |
}
|
|
indvdum@42
|
191 |
}
|
|
indvdum@42
|
192 |
}
|
|
indvdum@42
|
193 |
}
|
|
indvdum@42
|
194 |
}
|