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