1    /**
2     * @author Nick Peters
3     * COMP 469 - Heuristic Search
4     * Creates a randomly generated graph and finds the shortest path between two 
5     * random vertices using the A* search algorithm.
6     */
7    
8    package search;
9    import java.util.*;
10   
11   public class MyVertex implements Comparable<MyVertex> {
12       private final int MAX = 100;
13       private Integer m_value;
14       // x, y coordinates
15       private int m_x, m_y, m_fScore;
16       // Determines whether or not the vertex is on the shortest path.
17       private boolean m_path;
18       // Determines if the vertex is on the closed set
19       private boolean m_close;
20       private MyVertex m_predecessor;
21       
22       public MyVertex(int value) {
23           Random rand = new Random();
24           m_value = value;
25           m_path = false;
26           m_close = false;
27           // Give it random coordinates
28           m_x = rand.nextInt(MAX);
29           m_y = rand.nextInt(MAX);
30           m_fScore = 0;
31           m_predecessor = null;
32       }
33       
34       public void setPath() {
35           m_path = true;
36       }
37       
38       public boolean getPath() {
39           return m_path;
40       }
41       
42       public Integer getValue() {
43           return  m_value;
44       }
45       
46       public String toString() {
47           return m_value.toString();
48       }
49       
50       public void setFScore(int fScore) {
51           m_fScore = fScore;
52       }
53       
54       public Integer getFScore() {
55           return m_fScore;
56       }
57       
58       public void setPredecessor(MyVertex pred) {
59           m_predecessor = pred;
60       }
61       
62       public MyVertex getPredecessor() {
63           return m_predecessor;
64       }
65       
66       // Get the euclidean distance from a given node
67       public int getDistance(MyVertex other) {
68           return (int)Math.sqrt(Math.pow(m_x - other.m_x, 2) + Math.pow(m_y - other.m_y, 2));
69       }
70       
71       public int compareTo(MyVertex other) {
72           return getFScore().compareTo(other.getFScore());
73       }
74       
75       public boolean equals(MyVertex other) {
76           return m_value == other.m_value;
77       }
78       
79       public void close() {
80           m_close = true;
81       }
82       
83       public boolean isClosed() {
84           return m_close;
85       }
86   }
87