-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvironment.java
182 lines (144 loc) · 4.31 KB
/
Environment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package assignment1;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Environment {
String initialRoad;
String goalRoad;
Node startingNode=null;
Node goalNode=null;
public HashMap<String,Node> nodes = new HashMap<String,Node>();
public HashMap<String,RoadEntry> roads = new HashMap<String,RoadEntry>();
public void setInitial(String inRoad){
this.initialRoad =inRoad;
}
public void setGoal(String inRoad){
this.goalRoad = inRoad;
}
public HashMap<String,RoadEntry> getRoads() {
return roads;
}
public void setRoads( HashMap<String,RoadEntry> roads) {
this.roads = roads;
}
public HashMap<String,Node> getNodes() {
return nodes;
}
public void setNodes(HashMap<String,Node> nodes) {
this.nodes = nodes;
}
public Environment(){
this.roads= new HashMap<String,RoadEntry>();
this.nodes = new HashMap<String,Node>();
this.startingNode = null;
this.goalNode = null;
}
public void addRoad(RoadEntry inRoad){
this.roads.put(inRoad.getRoadName(),inRoad);
}
public Node addNode(Node inNode)
{
this.nodes.put(inNode.getJunction(),inNode);
return inNode;
}
public int getSize(){
return this.roads.size();
}
public RoadEntry findRoad(String streetName){
return this.roads.get(streetName);
}
public Node findNode(String juncName){
return this.nodes.get(juncName);
}
public void addJunctions(String roadName,String j1,String j2,int roadLength){
Node startJunc,endJunc;
Node checkSJ = findNode(j1);
if(checkSJ==null){
startJunc = this.addNode(new Node(j1));
addNode(startJunc);
}
else{
startJunc = findNode(j1);
}
Node checkEJ = findNode(j2);
if(checkEJ==null){
endJunc = this.addNode(new Node(j2));
addNode(endJunc);
}
else{
endJunc = findNode(j2);
}
Edge edge1 = new Edge(endJunc,roadLength,roadName);
Edge edge2 = new Edge(startJunc,roadLength, roadName);
startJunc.addChildren(edge1);
endJunc.addChildren(edge2);
}
public boolean addGoals(String startingRoad,String endingRoad,int start,int end){
if(startingNode!=null && goalNode!=null){
startingNode=null;
for(int i=0;i<2;i++)
{
if(!goalNode.getAdjencies().isEmpty()){
ArrayList<Edge> adjencies=goalNode.getAdjencies().get(i).getTarget().getAdjencies();
for(int j=0;j<adjencies.size();j++)
{
Edge edge = adjencies.get(j);
if(edge.getTarget().getJunction().equals("goal"))
adjencies.remove(j);
}
}
}
goalNode=null;
}
Node startingNode = new Node("initial");
this.startingNode = startingNode;
Node goalNode = new Node("goal");
this.goalNode = goalNode;
RoadEntry startRoad = this.roads.get(startingRoad);
RoadEntry endRoad = this.roads.get(endingRoad);
if(startRoad!=null && endRoad!= null && start<=startRoad.nLots && end<=endRoad.nLots && start>0 && end>0)
{
if(startRoad.equals(endRoad))
{
double cost = startRoad.distanceBetweenTwoNodes(start, end);
Edge edge = new Edge(goalNode,cost,startingRoad);
startingNode.addChildren(edge);
}
else
{
String junc1 = startRoad.startJunc;
String junc2 = startRoad.endJunc;
String junc3 = endRoad.startJunc;
String junc4 = endRoad.endJunc;
Node node1 = this.nodes.get(junc1);
Node node2 = this.nodes.get(junc2);
Node node3 = this.nodes.get(junc3);
Node node4 = this.nodes.get(junc4);
double cost1 = startRoad.distanceToStartJunc(start);
double cost2 = startRoad.distanceToEndJunc(start);
double cost3 = endRoad.distanceToStartJunc(end);
double cost4 = endRoad.distanceToEndJunc(end);
Edge newEdge = new Edge(node1,cost1,startingRoad);
Edge newEdge2 = new Edge(node2,cost2,startingRoad);
Edge newEdge3 = new Edge(node3,cost3,startingRoad);
Edge newEdge4 = new Edge(node4,cost4,startingRoad);
startingNode.addChildren(newEdge);
startingNode.addChildren(newEdge2);
goalNode.addChildren(newEdge3);
goalNode.addChildren(newEdge4);
Edge edge3 = new Edge(goalNode,cost3,endingRoad);
Edge edge4 = new Edge(goalNode,cost4,endingRoad);
node3.addChildren(edge3);
node4.addChildren(edge4);
}
return true;
}
return false;
}
}