svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / solvers / ShortestPathSolverAStar.java @ 30840
History | View | Annotate | Download (8.7 KB)
1 | 8499 | fjp | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | 22182 | fpenarrubia | package org.gvsig.graph.solvers; |
42 | 8499 | fjp | |
43 | import java.util.ArrayList; |
||
44 | |||
45 | 13583 | fjp | import org.gvsig.exceptions.BaseException; |
46 | 22182 | fpenarrubia | import org.gvsig.graph.core.GlobalCounter; |
47 | import org.gvsig.graph.core.GraphException; |
||
48 | 25339 | fpenarrubia | import org.gvsig.graph.core.GvConnector; |
49 | 22182 | fpenarrubia | import org.gvsig.graph.core.GvEdge; |
50 | import org.gvsig.graph.core.GvFlag; |
||
51 | import org.gvsig.graph.core.GvNode; |
||
52 | 25339 | fpenarrubia | import org.gvsig.graph.core.GvTurn; |
53 | 22182 | fpenarrubia | import org.gvsig.graph.core.IGraph; |
54 | 13583 | fjp | |
55 | 8499 | fjp | /**
|
56 | 13583 | fjp | * @author fjp Este es ?til solo cuando podemos calcular la distancia estimada
|
57 | * entre 2 nodos. (Es decir, para temas de cartograf?a). Para analizar
|
||
58 | * relaciones creo que no servir?a).
|
||
59 | 8499 | fjp | */
|
60 | 25339 | fpenarrubia | public class ShortestPathSolverAStar extends AbstractShortestPathSolver { |
61 | 13583 | fjp | |
62 | 8499 | fjp | /**
|
63 | * @return a list of features
|
||
64 | 13583 | fjp | * @throws GraphException
|
65 | 8499 | fjp | */
|
66 | public Route calculateRoute() throws GraphException { |
||
67 | GvFlag[] flags = net.getFlags();
|
||
68 | if (flags.length == 0) |
||
69 | throw new RuntimeException("Please, add flags before"); |
||
70 | int desde = 0; |
||
71 | int hasta = 1; |
||
72 | double elCoste1 = 0; |
||
73 | route = new Route();
|
||
74 | 8616 | fjp | GvFlag fFrom = flags[0];
|
75 | fFrom.setCost(0);
|
||
76 | 8499 | fjp | for (int i = 0; i < flags.length - 1; i++) { |
77 | 8616 | fjp | fFrom = flags[desde]; |
78 | 8499 | fjp | GvFlag fTo = flags[hasta]; |
79 | |||
80 | if (fFrom != fTo) {
|
||
81 | int idStart = net.creaArcosVirtuales(fFrom);
|
||
82 | int idStop = net.creaArcosVirtuales(fTo);
|
||
83 | |||
84 | 30840 | fpenarrubia | long tA1= System.currentTimeMillis(); |
85 | 8522 | fjp | double newCost = AStar(idStart, idStop);
|
86 | 30840 | fpenarrubia | long tA2= System.currentTimeMillis(); |
87 | System.out.println("T Astar = " + (tA2-tA1)); |
||
88 | |||
89 | 8499 | fjp | elCoste1 += newCost; |
90 | 8616 | fjp | fTo.setCost(elCoste1); |
91 | 8499 | fjp | |
92 | 13583 | fjp | if (newCost != Double.MAX_VALUE) { |
93 | 8499 | fjp | try {
|
94 | 30840 | fpenarrubia | long t1 = System.currentTimeMillis(); |
95 | 8499 | fjp | populateRoute(fFrom, fTo, idStart, idStop); |
96 | 30840 | fpenarrubia | long t2 = System.currentTimeMillis(); |
97 | System.out.println("T populateRoute=" + (t2-t1)); |
||
98 | 25339 | fpenarrubia | } catch (BaseException e) {
|
99 | 8499 | fjp | e.printStackTrace(); |
100 | throw new GraphException(e); |
||
101 | } |
||
102 | 13583 | fjp | } else {
|
103 | 8499 | fjp | // No way
|
104 | } |
||
105 | |||
106 | net.reconstruyeTramo(fFrom.getIdArc()); |
||
107 | net.reconstruyeTramo(fTo.getIdArc()); |
||
108 | desde = hasta; |
||
109 | } // if son puntos distintos
|
||
110 | hasta++; |
||
111 | } |
||
112 | |||
113 | return route;
|
||
114 | } |
||
115 | |||
116 | private double AStar(int idStart, int idStop) { |
||
117 | int nodeNum;
|
||
118 | int linkNum;
|
||
119 | double newCost;
|
||
120 | int idSiguienteNodo;
|
||
121 | GvNode node, toNode, finalNode, bestNode; // , *pNodoProv;
|
||
122 | GvEdge link; |
||
123 | boolean bExit = false; |
||
124 | |||
125 | boolean bGiroProhibido;
|
||
126 | ArrayList candidatos = new ArrayList(); |
||
127 | |||
128 | // char Mensaje[200];
|
||
129 | 13583 | fjp | |
130 | 8499 | fjp | IGraph graph = net.getGraph(); |
131 | |||
132 | // NUEVO: 27-6-2003
|
||
133 | // Cada nodo y cada arco llevan un nuemero de soluci?n. Se supone
|
||
134 | // que si lo del nodo y el arco no coincide con
|
||
135 | // este numero, todav?a no ha sido inicializado y hay que hacerlo.
|
||
136 | // Para evitar coincidencias cuando de la vuelta el contador, cada
|
||
137 | // 65000 peticiones (por ejemplo), repasamos toda
|
||
138 | // la red y ponemos numSolucGlobal a -1
|
||
139 | 14780 | fpenarrubia | if (GlobalCounter.increment())
|
140 | { |
||
141 | 8499 | fjp | for (nodeNum = 0; nodeNum < graph.numVertices(); nodeNum++) { |
142 | node = graph.getNodeByID(nodeNum); |
||
143 | node.initialize(); |
||
144 | } // for nodeNum */
|
||
145 | } |
||
146 | |||
147 | candidatos.clear(); |
||
148 | // A?adimos el Start Node a la lista de candidatosSTL
|
||
149 | // Nodo final
|
||
150 | finalNode = graph.getNodeByID(idStop); |
||
151 | finalNode.initialize(); |
||
152 | |||
153 | node = graph.getNodeByID(idStart); |
||
154 | node.initialize(); |
||
155 | bestNode = node; |
||
156 | |||
157 | candidatos.add(node); |
||
158 | 25339 | fpenarrubia | node.setCostZero(); |
159 | 8499 | fjp | node.setStatus(GvNode.statNowInList); |
160 | 8523 | fjp | node.calculateStimation(finalNode, 0);
|
161 | 8522 | fjp | |
162 | 8499 | fjp | // Mientras que la lista de candidatosSTL no est? vac?a, procesamos
|
163 | // Nodos
|
||
164 | 8522 | fjp | double bestStimation;
|
165 | 8499 | fjp | |
166 | while ((!bExit) && (candidatos.size() > 0)) { |
||
167 | // Buscamos el nodo con m?nimo coste
|
||
168 | node = (GvNode) candidatos.get(0);
|
||
169 | bestNode = node; |
||
170 | 8522 | fjp | bestStimation = node.getStimation(); |
171 | 8499 | fjp | for (nodeNum = 1; nodeNum < candidatos.size(); nodeNum++) { |
172 | node = (GvNode) candidatos.get(nodeNum); |
||
173 | 8522 | fjp | if (node.getStimation() < bestStimation) {
|
174 | bestStimation = node.getStimation(); |
||
175 | 8499 | fjp | bestNode = node; |
176 | } |
||
177 | } // for nodeNum candidatosSTL
|
||
178 | |||
179 | node = bestNode; |
||
180 | // Borramos el mejor nodo de la lista de candidatosSTL
|
||
181 | node.setStatus(GvNode.statWasInList); |
||
182 | 13583 | fjp | // TODO: BORRAR POR INDEX, NO AS?. ES M?S LENTO QUE SI BORRAMOS EL
|
183 | // i-?simo.
|
||
184 | 8499 | fjp | candidatos.remove(node); |
185 | // System.out.println("LINK " + link.getIdArc() + " from ");
|
||
186 | 13583 | fjp | // System.out.println("from " + idStart + " to " +
|
187 | // finalNode.getIdNode() + ". node=" + node.getIdNode());
|
||
188 | 8499 | fjp | // Miramos si hemos llegado donde quer?amos
|
189 | if (bestNode.getIdNode() == idStop) {
|
||
190 | bExit = true;
|
||
191 | break;
|
||
192 | } |
||
193 | |||
194 | // sprintf(Mensaje,"Enlaces en el nodo %ld:
|
||
195 | // %ld.",pNodo->idNodo,pNodo->Enlaces.GetSize());
|
||
196 | // AfxMessageBox(Mensaje);
|
||
197 | |||
198 | // A?adimos a la lista de candidatosSTL los vecinos del nodo que
|
||
199 | // acabamos de borrar
|
||
200 | // HAY Arcos QUE SALEN Y Arcos QUE LLEGAN. SOLO MIRAMOS LOS QUE
|
||
201 | // SALEN.
|
||
202 | 25339 | fpenarrubia | // for (linkNum = 0; linkNum < node.getOutputLinks().size(); linkNum++) {
|
203 | for (int iConec=0; iConec< node.getConnectors().size(); iConec++) { |
||
204 | 8499 | fjp | // Pillamos el nodo vecino
|
205 | 25339 | fpenarrubia | GvConnector c = node.getConnectors().get(iConec); |
206 | if (c.getEdgeOut() == null) continue; |
||
207 | |||
208 | link = (GvEdge) c.getEdgeOut(); |
||
209 | // Pillamos el nodo vecino
|
||
210 | // link = (GvEdge) node.getOutputLinks().get(linkNum);
|
||
211 | 8499 | fjp | idSiguienteNodo = link.getIdNodeEnd(); |
212 | |||
213 | toNode = graph.getNodeByID(idSiguienteNodo); |
||
214 | 25339 | fpenarrubia | |
215 | 8499 | fjp | // 27_5_2004
|
216 | // Si un arco tiene coste negativo, lo ignoramos
|
||
217 | if (link.getWeight() < 0) |
||
218 | continue;
|
||
219 | |||
220 | // Fin arco con coste negativo
|
||
221 | 25339 | fpenarrubia | // int from_link = c.getFrom_link_c();
|
222 | //
|
||
223 | // if (from_link != -1) {
|
||
224 | // if (c.getEdgeIn().getIdEdge() == from_link) continue; // No queremos entrar y salir
|
||
225 | // por el mismo conector
|
||
226 | 8499 | fjp | // NUEVO: 26-7-2003: Comprobamos si est? inicializado
|
227 | 14780 | fpenarrubia | if (toNode.getNumSoluc() != GlobalCounter.getGlobalSolutionNumber()) {
|
228 | 8499 | fjp | toNode.initialize(); |
229 | 13583 | fjp | } else {
|
230 | 8499 | fjp | // System.out.println("Nodo ya inicializado");
|
231 | } |
||
232 | 25339 | fpenarrubia | // Miramos a ver si podemos mejorar su best_cost
|
233 | // double costeGiro = 0;
|
||
234 | //
|
||
235 | // // Miramos la lista de Giros de ese nodo
|
||
236 | // bGiroProhibido = false;
|
||
237 | // if (from_link != -1) {
|
||
238 | // GvEdge edgeFrom = graph.getEdgeByID(from_link);
|
||
239 | // for (int idGiro=0; idGiro < node.getTurnCosts().size(); idGiro++)
|
||
240 | // {
|
||
241 | // // Si est? prohibido, a por otro
|
||
242 | // GvTurn elGiro = node.getTurnCosts().get(idGiro);
|
||
243 | // if ((elGiro.getIdArcFrom() == edgeFrom.getIdArc()) &&
|
||
244 | // (elGiro.getIdArcTo() == link.getIdArc()))
|
||
245 | // {
|
||
246 | // if (elGiro.getCost() < 0)
|
||
247 | // {
|
||
248 | // bGiroProhibido = true;
|
||
249 | // }
|
||
250 | // else
|
||
251 | // costeGiro = elGiro.getCost();
|
||
252 | //
|
||
253 | // // Para que pueda volver a entrar en los c?lculos
|
||
254 | // node.setStatus(GvNode.statNotInList);
|
||
255 | // break; // Salimos del for porque ya hemos encontrado el giro
|
||
256 | // }
|
||
257 | // }
|
||
258 | // }
|
||
259 | // // Si est? prohibido, vamos a por otro enlace
|
||
260 | // if (bGiroProhibido)
|
||
261 | // {
|
||
262 | // continue;
|
||
263 | // }
|
||
264 | // TODO: REVISAR SI HAY QUE SUMAR EL COSTE DEL GIRO A NEWCOST
|
||
265 | // Y SI LO DE TURNCOSTS NO DEBE IR EN EXISTEMEJORA
|
||
266 | 8499 | fjp | |
267 | 8522 | fjp | // Miramos a ver si podemos mejorar su best_cost
|
268 | 25339 | fpenarrubia | newCost = c.getBestCostOut() + link.getWeight(); |
269 | 8499 | fjp | |
270 | 25339 | fpenarrubia | // Change to take care of turn costs
|
271 | if (toNode.existeMejora(link, newCost)) { // Es una mejora, as? que actualizamos el vecino y |
||
272 | // // lo a?adimos a los candidatosSTL
|
||
273 | // toNode.setBestCost(newCost);
|
||
274 | //
|
||
275 | // toNode.setFromLink(link.getIdEdge());
|
||
276 | 13583 | fjp | |
277 | 8523 | fjp | toNode.calculateStimation(finalNode, newCost); |
278 | 8499 | fjp | |
279 | 8522 | fjp | if (toNode.getStatus() != GvNode.statNowInList) {
|
280 | toNode.setStatus(GvNode.statNowInList); |
||
281 | candidatos.add(toNode); |
||
282 | } |
||
283 | } // Si hay mejora
|
||
284 | 8499 | fjp | |
285 | } // for linkNum
|
||
286 | } // while candidatosSTL
|
||
287 | |||
288 | newCost = finalNode.getBestCost(); |
||
289 | |||
290 | return newCost;
|
||
291 | } |
||
292 | |||
293 | } |