Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.85 KB | None | 0 0
  1. import java.util.*;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.Statement;
  7. /**
  8. *
  9. * @author Owner
  10. */
  11. public class AStar {
  12.  
  13. public AStar()
  14. {
  15.  
  16. }
  17. public Node[] PathGen(Node start, Node goal,boolean accesible) throws Exception
  18. {
  19.  
  20. Class.forName("org.sqlite.JDBC");
  21. Connection conn = DriverManager.getConnection("jdbc:sqlite:Nodes.db");
  22. Statement stat = conn.createStatement();
  23. PriorityQueue<Node> open=new PriorityQueue<Node>();
  24. open.add(start);
  25. HashMap hm=new HashMap();
  26. hm.put(start.label,start);
  27. hm.put(goal.label, goal);
  28. while(open.size()!=0)
  29. {
  30.  
  31. //System.out.println(open.size());
  32. Node hold= open.remove();
  33. if(hold ==goal) return path(hold);
  34. hold.traversed=true;
  35. hold.g=0;
  36. double x1= hold.x-goal.x;
  37. double y1 =hold.y-goal.y;
  38. double z1= hold.z-goal.z;
  39. hold.h=Math.sqrt(x1*x1+y1*y1+z1*z1);
  40.  
  41.  
  42. for(int i =0; i<hold.accesible.length;i++)
  43. {
  44. String u=hold.accesible[i];
  45. Node c;
  46. if(hm.containsKey(u))
  47. {
  48.  
  49. c=(Node)(hm.get(u));
  50.  
  51.  
  52. }
  53. else
  54. {
  55. int xx,yy,zz;
  56.  
  57. ResultSet rs1 = stat.executeQuery("select * from node where (label ==\""+u+"\");");
  58.  
  59. xx=rs1.getInt("x");
  60. System.out.println("hello");
  61. yy=rs1.getInt("y");
  62. zz=rs1.getInt("z");
  63. c=new Node(u,xx,yy,zz);
  64. c.accesible=new String[rs1.getInt("num1")];
  65. c.nonaccesible=new String[rs1.getInt("num2")];
  66. ResultSet rs2=stat.executeQuery("select * from sArr where label =\""+u+"\"");
  67. int j=0;
  68. while(rs2.next())
  69. {
  70. if(rs2.getBoolean("bool"))
  71. {
  72. c.accesible[i]=rs2.getString("point");
  73. }
  74. else
  75. {
  76. c.nonaccesible[i]=rs2.getString("point");
  77. }
  78. }
  79. //rs1.close();
  80. rs2.close();
  81. hm.put(c.label,c);
  82. }
  83. if(c.traversed) continue;
  84. double tempg=hold.g;
  85. double x=hold.x-c.x;
  86. double y=hold.y-c.y;
  87. double z=hold.z-c.z;
  88. double dist=Math.sqrt(x*x+y*y+z*z);
  89. tempg+=dist;
  90. //System.out.println(tempg);
  91.  
  92.  
  93. if(tempg < c.g )
  94. {
  95. open.remove(c);
  96. c.from=hold;
  97. c.g=tempg;
  98.  
  99. x1= c.x-goal.x;
  100. y1 =c.y-goal.y;
  101. z1= c.z-goal.z;
  102.  
  103.  
  104. c.h=Math.sqrt(x1*x1+y1*y1+z1*z1);
  105. c.f= c.g+c.h;
  106. c.traversed=true;
  107. open.add(c);
  108. }
  109.  
  110. }
  111. /*if(!accesible&& hold.nonaccesible!=null)
  112. {
  113. for(int i =0; i<hold.nonaccesible.length;i++)
  114. {
  115. String u=hold.nonaccesible[i];
  116. Node c;
  117. if(hm.containsKey(u))
  118. {
  119.  
  120. c=(Node)(hm.get(u));
  121.  
  122.  
  123. }
  124. else
  125. {
  126. int xx,yy,zz;
  127. String acce[],nacce[];
  128. ResultSet rs = stat.executeQuery("select label, x,y,z,acc,nacc from people where label ="+u+";");
  129. xx=rs.getInt("x");
  130. yy=rs.getInt("y");
  131. zz=rs.getInt("z");
  132. c=new Node(u,xx,yy,zz);
  133. c.accesible=(String[])rs.getObject("acc");
  134. c.nonaccesible=(String[])rs.getObject("nacc");
  135. }
  136. if(c.traversed) continue;
  137. double tempg=hold.g;
  138. double x=hold.x-c.x;
  139. double y=hold.y-c.y;
  140. double z=hold.z-c.z;
  141. double dist=Math.sqrt(x*x+y*y+z*z);
  142. tempg+=dist;
  143.  
  144. if(tempg < c.g )
  145. {
  146. open.remove(c);
  147. c.from=hold;
  148. c.g=tempg;
  149.  
  150. x1= c.x-goal.x;
  151. y1 =c.y-goal.y;
  152. z1= c.z-goal.z;
  153.  
  154.  
  155. c.h=Math.sqrt(x1*x1+y1*y1+z1*z1);
  156. c.f= c.g+c.h;
  157. open.add(c);
  158. }
  159.  
  160. }
  161. }*/
  162.  
  163. }
  164.  
  165. System.out.println("fuck");
  166. return null;
  167. }
  168.  
  169. public Node[] path( Node end) throws ClassCastException
  170. {
  171. ArrayList<Node> l =new ArrayList<Node>();
  172. l.add(end);
  173.  
  174. while(end.from!=null)
  175. {
  176. end=end.from;
  177. l.add(end);
  178. }
  179. Object p[] =(l.toArray());
  180. Node o[] =new Node[p.length];
  181. for(int i=0; i<o.length;i++)
  182. {
  183. o[i]=(Node)(p[o.length-i-1]);
  184. }
  185. return o;
  186. }
  187. }
  188.  
  189.  
  190. public class Node implements Comparable {
  191. public String label;
  192. public double x,y,z, f,g,h;
  193. public String accesible[];//all accesible paths
  194. public String nonaccesible[]; //those routes that are non accesible
  195. boolean traversed;
  196. public Node from;
  197.  
  198.  
  199. public Node()
  200. {
  201. label =null;
  202. x=0;
  203. y=0;
  204. z=0;
  205. f=Double.MAX_VALUE;
  206. g=Double.MAX_VALUE;
  207. h=-1;
  208.  
  209. traversed=false;
  210. from=null;
  211. }
  212.  
  213. public Node(String l,double x1, double y1, double z1)
  214. {
  215. label=l;
  216. x=x1;
  217. y=y1;
  218. z=z1;
  219. f=Double.MAX_VALUE;
  220. g=Double.MAX_VALUE;
  221. h=Double.MAX_VALUE;
  222. traversed=false;
  223. from=null;
  224. }
  225.  
  226.  
  227.  
  228.  
  229. public int compareTo(Object g)
  230. {
  231. Node l=(Node) g;
  232. return (int)(f - l.f);
  233. }
  234.  
  235.  
  236. }
  237.  
  238.  
  239. public class Main {
  240. Node end;
  241. /**
  242. * @param args the command line arguments
  243. */
  244. public static void main(String[] args) throws Exception{
  245.  
  246. new Main();
  247.  
  248. }
  249.  
  250. public Main() throws Exception
  251. {
  252. initialize();
  253. AStar temp=new AStar();
  254. Node A=new Node("A",83,80,0);
  255. String a[] ={"B"};
  256. A.accesible= a;
  257.  
  258. Node G=new Node("A",83,80,0);
  259. String g[] ={"F"};
  260. A.accesible= g;
  261.  
  262. Node t[]= temp.PathGen(A, G, true);
  263. for(int i=0; i<t.length;i++)
  264. {
  265. System.out.println(t[i].label);
  266. }
  267. }
  268. /*
  269. public Node sample()
  270. {
  271.  
  272. Node a,b,c,d,e,f,g;
  273.  
  274. a=new Node(83,80,0);
  275. b=new Node(200,192,0);
  276. c=new Node(50,187,0);
  277. d=new Node(166,133,0);
  278. e=new Node(360,205,0);
  279. f=new Node(333,77,0);
  280. g=new Node(489,133,0);
  281.  
  282. a.accesible=new Node[1];
  283. a.accesible[0]=b;
  284.  
  285. b.accesible=new Node[4];
  286. b.accesible[0]=a;
  287. b.accesible[1]=c;
  288. b.accesible[2]=f;
  289. b.accesible[3]=e;
  290.  
  291. c.accesible=new Node[2];
  292. c.accesible[0]=b;
  293. c.accesible[1]=d;
  294.  
  295. d.accesible=new Node[1];
  296. d.accesible[0]=c;
  297. d.nonaccesible=new Node[1];
  298. d.nonaccesible[0]=e;
  299.  
  300. e.accesible=new Node[2];
  301. e.accesible[0]=b;
  302. e.accesible[1]=f;
  303. e.nonaccesible=new Node[1];
  304. e.nonaccesible[0]=d;
  305.  
  306. f.accesible=new Node[3];
  307. f.accesible[0]=b;
  308. f.accesible[1]=e;
  309. f.accesible[2]=g;
  310.  
  311. g.accesible=new Node[1];
  312. g.accesible[0]=f;
  313. end=g;
  314. return b;
  315. }
  316.  
  317. */
  318. public void initialize() throws Exception
  319. {
  320. Class.forName("org.sqlite.JDBC");
  321. Connection conn = DriverManager.getConnection("jdbc:sqlite:Nodes.db");
  322. Statement stat = conn.createStatement();
  323. stat.executeUpdate("drop table if exists node;");
  324. stat.executeUpdate("drop table if exists sArr;");
  325. stat.executeUpdate("create table node (label, x,y,z,num1,num2);");
  326. stat.executeUpdate("create table sArr (label, point,bool);");
  327. PreparedStatement prep = conn.prepareStatement(
  328. "insert into node values (?, ? ,? ,?,? ,?);");
  329. PreparedStatement prep2 = conn.prepareStatement(
  330. "insert into sArr values (?, ? ,?);");
  331.  
  332.  
  333.  
  334. prep.setString(1, "A");
  335. prep.setInt(2, 83);
  336. prep.setInt(3, 80);
  337. prep.setInt(4, 0);
  338. prep.setInt(5,1);
  339. prep.setInt(6,0);
  340.  
  341. prep2.setString(1, "A");
  342. prep2.setString(2,"B");
  343. prep2.setBoolean(3, true);
  344. prep.addBatch();
  345. prep2.addBatch();
  346.  
  347. //Node A=new Node("A",83,80,0);
  348. //String g[] ={"B"};
  349.  
  350. //A.accesible= g;
  351.  
  352. prep.setString(1, "B");
  353. prep.setInt(2, 200);
  354. prep.setInt(3, 192);
  355. prep.setInt(4, 0);
  356. prep.setInt(5,4);
  357. prep.setInt(6,0);
  358. prep.addBatch();
  359.  
  360. prep2.setString(1, "B");
  361. prep2.setString(2, "A");
  362. prep2.setBoolean(3,true);
  363. prep2.addBatch();
  364. prep2.setString(1, "B");
  365. prep2.setString(2, "C");
  366. prep2.setBoolean(3,true);
  367. prep2.addBatch();
  368. prep2.setString(1, "B");
  369. prep2.setString(2, "F");
  370. prep2.setBoolean(3,true);
  371. prep2.addBatch();
  372. prep2.setString(1, "B");
  373. prep2.setString(2, "E");
  374. prep2.setBoolean(3,true);
  375. prep2.addBatch();
  376.  
  377.  
  378.  
  379.  
  380.  
  381. prep.setString(1, "C");
  382. prep.setInt(2, 50);
  383. prep.setInt(3, 187);
  384. prep.setInt(4, 0);
  385. prep.setInt(5,2);
  386. prep.setInt(6,0);
  387.  
  388. prep.addBatch();
  389.  
  390. prep2.setString(1, "C");
  391. prep2.setString(2, "B");
  392. prep2.setBoolean(3,true);
  393. prep2.addBatch();
  394. prep2.setString(1, "C");
  395. prep2.setString(2, "D");
  396. prep2.setBoolean(3,true);
  397. prep2.addBatch();
  398.  
  399.  
  400. prep.setString(1, "D");
  401. prep.setInt(2, 166);
  402. prep.setInt(3, 133);
  403. prep.setInt(4, 0);
  404. prep.setInt(5,1);
  405. prep.setInt(6,1);
  406.  
  407. prep.addBatch();
  408. prep2.setString(1, "D");
  409. prep2.setString(2, "C");
  410. prep2.setBoolean(3,true);
  411. prep2.addBatch();
  412. prep2.setString(1, "D");
  413. prep2.setString(2, "E");
  414. prep2.setBoolean(3,false);
  415. prep2.addBatch();
  416.  
  417.  
  418.  
  419. prep.setString(1, "E");
  420. prep.setInt(2, 360);
  421. prep.setInt(3, 205);
  422. prep.setInt(4, 0);
  423. prep.setInt(5,2);
  424. prep.setInt(6,1);
  425.  
  426. prep.addBatch();
  427.  
  428. prep2.setString(1, "E");
  429. prep2.setString(2, "B");
  430. prep2.setBoolean(3,true);
  431. prep2.addBatch();
  432. prep2.setString(1, "E");
  433. prep2.setString(2, "F");
  434. prep2.setBoolean(3,true);
  435. prep2.addBatch();
  436. prep2.setString(1, "E");
  437. prep2.setString(2, "D");
  438. prep2.setBoolean(3,false);
  439. prep2.addBatch();
  440.  
  441.  
  442.  
  443.  
  444.  
  445. prep.setString(1, "F");
  446. prep.setInt(2, 333);
  447. prep.setInt(3, 77);
  448. prep.setInt(4, 0);
  449. prep.setInt(5,3);
  450. prep.setInt(6,0);
  451.  
  452. prep.addBatch();
  453.  
  454. prep2.setString(1, "F");
  455. prep2.setString(2, "B");
  456. prep2.setBoolean(3,true);
  457. prep2.addBatch();
  458. prep2.setString(1, "F");
  459. prep2.setString(2, "E");
  460. prep2.setBoolean(3,true);
  461. prep2.addBatch();
  462. prep2.setString(1, "F");
  463. prep2.setString(2, "G");
  464. prep2.setBoolean(3,true);
  465. prep2.addBatch();
  466.  
  467.  
  468. prep.setString(1, "G");
  469. prep.setInt(2, 489);
  470. prep.setInt(3, 133);
  471. prep.setInt(4, 0);
  472. prep.setInt(5,2);
  473. prep.setInt(6,0);
  474.  
  475. prep.addBatch();
  476.  
  477. prep2.setString(1, "G");
  478. prep2.setString(2, "F");
  479. prep2.setBoolean(3,true);
  480. prep2.addBatch();
  481.  
  482.  
  483.  
  484. conn.setAutoCommit(false);
  485. prep.executeBatch();
  486. prep2.executeBatch();
  487. conn.setAutoCommit(true);
  488.  
  489.  
  490. conn.close();
  491. }
  492.  
  493. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement