-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAM.java
285 lines (252 loc) · 10.3 KB
/
AM.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class AM
{
private static final int ROAD = 0;
private static final int WALL = 1;
private static final int PATH = 2;
private static final int START = 3;
private static final int EXIT = 4;
private float INITFEROMON = 8;
private float FEROMONVAPORATERATE = 0.1f;
private float FEROMONTOLEFT = 4;
public int iloscKrokow = 0;
//Po wyłączeniu funkcja się kończy i zabija mrówkę
boolean wlaczCofanie = true;
public Coordinate[][] maze;
public Coordinate[][] path;
private Stack<Cell> antPosition = new Stack<Cell>();
WeightedRandomBag pp = new WeightedRandomBag();
public AM(float INITFEROMON, float FEROMONVAPORATERATE, float FEROMONTOLEFT, boolean wlaczCofanie)
{
this.INITFEROMON = INITFEROMON;
this.FEROMONVAPORATERATE = FEROMONVAPORATERATE;
this.FEROMONTOLEFT = FEROMONTOLEFT;
this.wlaczCofanie = wlaczCofanie;
}
public void setMaze(File maze) throws FileNotFoundException {
String fileText = "";
try (Scanner input = new Scanner(maze)) {
while (input.hasNextLine()) {
fileText += input.nextLine() + "\n";
}
}
initializeMaze(fileText);
}
public void resetMaze(){
maze = null;
}
private void initializeMaze(String text) {
if (text == null || (text = text.trim()).length() == 0) {
throw new IllegalArgumentException("pusty dokument!");
}
String[] lines = text.split("[\r]?\n");
maze = new Coordinate[lines.length][lines[0].length()-1];
path = new Coordinate[lines.length][lines[0].length()-1];
for (int row = 0; row < getHeight(); row++) {
for (int col = 0; col < getWidth(); col++) {
if (lines[row].charAt(col) == '1'){
maze[row][col] = new Coordinate(WALL);
path[row][col] = new Coordinate(WALL);
}
else{
maze[row][col] = new Coordinate(ROAD, INITFEROMON);
path[row][col] = new Coordinate(ROAD);
}
}
}
maze[0][0] = new Coordinate(START, INITFEROMON);
maze[getHeight()-1][getWidth()-1] = new Coordinate(EXIT, INITFEROMON);
}
public int getHeight() {
return maze.length;
}
public int getWidth() {
return maze[0].length;
}
public Coordinate getEntry() {
return maze[0][0];
}
public Coordinate getExit() {
return maze[getHeight()-1][getWidth()-1];
}
public boolean isExit(int x, int y) {
return x == getHeight()-1 && y == getWidth()-1;
}
public boolean isStart(int x, int y) {
return x == 0 && y == 0;
}
public boolean isWall(int row, int col) {
return maze[row][col].getValue() == WALL;
}
public boolean isValidLocation(int row, int col) {
if (row < 0 || row >= getHeight() || col < 0 || col >= getWidth()) {
return false;
}
return true;
}
public String toString() {
StringBuilder result = new StringBuilder(getWidth() * (getHeight() + 1));
for (int row = 0; row < getHeight(); row++) {
for (int col = 0; col < getWidth(); col++) {
if (maze[row][col].getValue() == ROAD) {
result.append('0');
} else if (maze[row][col].getValue() == WALL) {
result.append('1');
} else if (maze[row][col].getValue() == START) {
result.append('3');
} else if (maze[row][col].getValue() == EXIT) {
result.append('4');
} else {
result.append('2');
}
}
result.append('\n');
}
return result.toString();
}
public void printPath() {
for (int row = 0; row < getHeight(); row++) {
System.out.print("\t");
for (int col = 0; col < getWidth(); col++) {
if (maze[row][col].getValue() == ROAD) {
System.out.print("0");
} else if (maze[row][col].getValue() == WALL) {
System.out.print("1");
} else if (maze[row][col].getValue() == START) {
System.out.print("3");
} else if (maze[row][col].getValue() == EXIT) {
System.out.print("4");
} else {
System.out.print("2");
}
System.out.print("\t");
}
System.out.print('\n');
}
}
//Wyświetlanie tablicy z feromonami
public void showFeromonMaze() {
System.out.print('\u000C');
for (int row = 0; row < getHeight(); row++) {
System.out.print("\t\t");
for (int col = 0; col < getWidth(); col++) {
if(maze[row][col].getValue() != WALL){
System.out.printf("%.3f", (float)maze[row][col].getFeromon());
}
else{
System.out.print("WWW");
}
System.out.print("\t\t");
}
System.out.print("\n\n\n");
}
System.out.print('\n');
System.out.print('\n');
}
public void solve(int iloscIteracji, int iloscMrowek) throws InterruptedException{
for(int i = 0; i < iloscIteracji; i++){
for(int j = 0; j < iloscMrowek; j++){
resetVisited();
maze[0][0].setFeromon(FEROMONTOLEFT);
maze[0][0].setVisited(true);
findPath(0,0, true);
antPosition.clear();
}
for (int row = 0; row < getHeight(); row++) {
for (int col = 0; col < getWidth(); col++) {
maze[row][col].vaporateFeromon(FEROMONVAPORATERATE);
}
}
showFeromonMaze();
//Thread.sleep(1000);
}
//printPath();
}
public boolean findPath(int row, int col, boolean canPush){
if(canPush){
antPosition.push(new Cell(row,col));
}
iloscKrokow++;
//warunki dodania do listy feromonu pozostawionego na następnych polach
if( antPosition.peek().col+1 < getWidth() &&
!maze[antPosition.peek().row][antPosition.peek().col+1].isVisited()
&& !isWall(antPosition.peek().row, antPosition.peek().col+1)
&& isValidLocation(antPosition.peek().row, antPosition.peek().col+1)){
pp.addEntry(0, maze[row][col+1].getFeromon());
}
if( antPosition.peek().row+1 < getHeight() &&
!maze[antPosition.peek().row+1][antPosition.peek().col].isVisited()
&& !isWall(antPosition.peek().row+1, antPosition.peek().col)
&& isValidLocation(antPosition.peek().row+1, antPosition.peek().col)){
pp.addEntry(1, maze[row+1][col].getFeromon());
}
if( antPosition.peek().row-1 >= 0 &&
!maze[antPosition.peek().row-1][antPosition.peek().col].isVisited()
&& !isWall(antPosition.peek().row-1, antPosition.peek().col)
&& isValidLocation(antPosition.peek().row-1, antPosition.peek().col)){
pp.addEntry(2, maze[row-1][col].getFeromon());
}
if( antPosition.peek().col-1 >= 0 &&
!maze[antPosition.peek().row][antPosition.peek().col-1].isVisited()
&& !isWall(antPosition.peek().row, antPosition.peek().col-1)
&& isValidLocation(antPosition.peek().row, antPosition.peek().col-1)){
pp.addEntry(3, maze[row][col-1].getFeromon());
}
//Losowanie liczby którą ścieżka podąży mrówka
int r = pp.getRandom();
pp.entries.clear();
pp.accumulatedWeight = 0;
if(r>=0){
//Warunki sprawdzające która to była ścieżka oraz czy następna to nie jest meta jeżeli nie to wywołuje się ta funkcja ponownie
if(r==0){
maze[antPosition.peek().row][antPosition.peek().col+1].setFeromon(FEROMONTOLEFT);
if(isExit(antPosition.peek().row, antPosition.peek().col+1)){
return true;
}
maze[antPosition.peek().row][antPosition.peek().col+1].setVisited(true);
findPath(antPosition.peek().row, antPosition.peek().col+1, true);
}
else if(r==1){
maze[antPosition.peek().row+1][antPosition.peek().col].setFeromon(FEROMONTOLEFT);
if(isExit(antPosition.peek().row+1, antPosition.peek().col)){
return true;
}
maze[antPosition.peek().row+1][antPosition.peek().col].setVisited(true);
findPath(antPosition.peek().row+1, antPosition.peek().col, true);
}
else if(r==2){
maze[antPosition.peek().row-1][antPosition.peek().col].setFeromon(FEROMONTOLEFT);
if(isExit(antPosition.peek().row-1, antPosition.peek().col)){
return true;
}
maze[antPosition.peek().row-1][antPosition.peek().col].setVisited(true);
findPath(antPosition.peek().row-1, antPosition.peek().col, true);
}
else if(r==3){
maze[antPosition.peek().row][antPosition.peek().col-1].setFeromon(FEROMONTOLEFT);
if(isExit(antPosition.peek().row, antPosition.peek().col+1)){
return true;
}
maze[antPosition.peek().row][antPosition.peek().col-1].setVisited(true);
findPath(antPosition.peek().row, antPosition.peek().col-1, true);
}
}
//warunek else jeżeli nie znajdziemy żadnych ścieżek gdzie może iść mrówka cofa do ostatniego pola gdzie może być ścieżka
else if(wlaczCofanie){
if(isStart(antPosition.peek().row, antPosition.peek().col))
return false;
antPosition.pop();
findPath(antPosition.peek().row, antPosition.peek().col, false);
}
return false;
}
private void resetVisited(){
for(int row = 0; row < getHeight(); row++){
for(int col = 0; col < getWidth(); col++){
maze[row][col].setVisited(false);
}
}
}
}