summaryrefslogtreecommitdiff
path: root/Pathfinding Visualizer/Assets/Scripts/SampleWorld.java
blob: dd8e2ab44afc01f6452206a904334c224a79d927 (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
package sampleworld;

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Stack;
import java.util.Queue;
import java.util.PriorityQueue;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;

@SuppressWarnings( "javadoc" )
public class SampleWorld
{	
	//////////////////////////////////////////////////////////
	// GLOBAL VARIABLES THAT REPRESENT STATIC WORLD FEATURES//
	//////////////////////////////////////////////////////////
	
	// global variables used to hold world dimensions
	public static int worldCols, worldRows;
	
	// used to store coordinates of all obstacles in our world
	public static List<int[]> obstacles = new ArrayList<int[]>();
	
	// used to store all unique states that agent has visited (holds agent and sample coords)
	public static List<State> closed = new ArrayList<State>();
	
	// used to keep track of number of operations and is used in output
	public static int expansions = 0;
	public static int nodesGenerated = 0;

	// helper variables just to make indexing arrays easier to read
	public static int row = 0;
	public static int col = 1;
	
    public static void main(String[] args)
    {

    	//////////////////////////////
    	// COLLECT BASIC WORLD INFO //
    	//////////////////////////////
    	Scanner input = new Scanner(System.in);
    	input.useDelimiter(System.lineSeparator());

		worldCols = Integer.parseInt(input.next());
		worldRows = Integer.parseInt(input.next());
		
		/////////////////////////////
		// CREATE WORLD STRUCTURES // 
		/////////////////////////////
		char[][] world = new char[worldRows][worldCols]; 
		List<int[]> samples = new ArrayList<int[]>();
		int[] agent = new int[] {-1, -1}; 
		
		////////////////////////////////////
		// SAVE INITIAL STATE AS 2D ARRAY //
		////////////////////////////////////
		for(int rows = 0; rows < worldRows; rows++)
		{
			// grab one row at a time
			char[] line = input.next().toCharArray();			
			
			for(int cols = 0; cols < worldCols; cols++)
			{			
				// check each column for special characters
				if(line[cols] == '@')
				{
					agent = new int[]{rows, cols};
				}
				if(line[cols] == '#')
				{
					obstacles.add(new int[]{rows, cols});
				}
				if(line[cols] == '*')
				{
					samples.add(new int[]{rows, cols});
				}
				
				// then add character to our local world
				world[rows][cols] = line[cols];	
			}
		}
		
		input.close();
				
		//////////////////////////////////////////////////
		// CONSTRUCT INTIAL STATE NODE AND BEGIN SEARCH //
		//////////////////////////////////////////////////
		
		// initial state has no parent and no previous action. '0' is being interpreted as null here
		Node initialState = new Node(null, agent, samples, '0', 0, 0);
		Stack<Node> result = new Stack<Node>();
		
		// select search algorithm to use
        if(args[0].equals("dfs"))
        	result = dfs(initialState, -1);
        	
        if(args[0].equals("ucs"))
        	result = ucs(initialState);
        
        if(args[0].equals("ids"))
        	result = ids(initialState);
        
        if(args[0].equals("astar"))
        	result = astar(initialState, args[1]);
        
        int length = result.size();

        // decide whether to animate
        if(args.length == 2)
        {
            if(args[1].equals("animate"))
        	{
        		try 
        		{
					animate(world, result);
				} 
        		catch (InterruptedException e) 
        		{
					e.printStackTrace();
				}
                System.out.println("Distance traveled: " + length + " steps");
                System.out.println("Nodes Expanded: " + SampleWorld.expansions);        
                System.out.println("Total Nodes Generated: " + SampleWorld.nodesGenerated);
        		return;
        	}		
        }
        else if(args.length == 3)
        {
        	if(args[2].equals("animate"))
        	{
        		try {
        			animate(world, result);
        		} catch (InterruptedException e) {
        			e.printStackTrace();
        		}
                System.out.println("Distance traveled: " + length + " steps");
        		System.out.println("Nodes Expanded: " + SampleWorld.expansions);        
        		System.out.println("Total Nodes Generated: " + SampleWorld.nodesGenerated);
        		return;
        	}
        }
        
        // Print the results and searching stats
        if(length == 0)
        {
        	System.out.println("No solution found :(");
        }
        else
        {        	
        	for(int i = 0; i < length; i++)
        	{
        		Node state = result.pop();
        		System.out.println(state.lastMove);
        	}	
        }
        
        System.out.println("\nDistance traveled: " + length + " steps");
        System.out.println("Nodes Expanded: " + SampleWorld.expansions);        
        System.out.println("Total Nodes Generated: " + SampleWorld.nodesGenerated);
    }
    
    // helper function for me. is fun to watch and also is genuinely useful for debugging
    public static void animate(char[][] world, Stack<Node> result) throws InterruptedException
    {
    	clearScreen();
    	int agentR, agentC;

    	// print out initial state
    	for(int r = 0; r < worldRows; r++)
    	{
    		for(int c = 0; c < worldCols; c++)
    		{
    			System.out.print(world[r][c]);
    		}
    		// only make newline after row if there is another row
    		if(r != worldCols - 1)
    			System.out.println();
    	}
    	TimeUnit.MILLISECONDS.sleep(2000);
    	clearScreen();

    	// read and print each of the agent's moves
    	while(!result.isEmpty())
    	{
	    	Node state = result.pop();
	    	agentR = state.agent[row];
	    	agentC = state.agent[col];
	    	
	    	world[agentR][agentC] = '@';
	    	

	    	// does inverse of each move to replace agent's old location with _
	    	if(state.lastMove == 'U')
	    	{
	    		world[agentR + 1][agentC] = '_';
	    	}
	    	if(state.lastMove == 'D')
	    	{
	    		world[agentR - 1][agentC] = '_';
	    	}
	    	if(state.lastMove == 'L')
	    	{
	    		world[agentR][agentC + 1] = '_';
	    	}	    	
	    	if(state.lastMove == 'R')
	    	{
	    		world[agentR][agentC - 1] = '_';
	    	}
	    	if(state.lastMove == 'S')
	    	{
	    		world[agentR][agentC] = 'S';
	    	}

	    	// TODO try a new loop that just erases the line the agent was/is on 
	    	
	    	// print out new world
	    	for(int r = 0; r < worldRows; r++)
	    	{
	    		for(int c = 0; c < worldCols; c++)
	    		{
	    			System.out.print(world[r][c]);
	    		}
	    		System.out.println();
	    	}
	    	
	    	TimeUnit.SECONDS.sleep(1);
	    	clearScreen();
    	}
    	
    }
    // helper function for animate()
    public static void clearScreen()
    {
    	System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");    		
    }

    ///////////////////////////////////
    // SUITE OF SEARCHING ALGORITHMS //
    ///////////////////////////////////
    public static Stack<Node> astar(Node initialState, String heuristic)
    {
    	Stack<Node> solution = new Stack<Node>();
    	Queue<Node> open = new PriorityQueue<Node>();
    	
		open.add(initialState);
    	while(true)
    	{
    		if(open.isEmpty())
    			return solution;
    		
    		Node currentNode = open.remove();
    		
    		// check whether we are in goal state
    		if(currentNode.samples.size() == 0)
    		{
    			while(currentNode.parent != null)
				{
					solution.push(currentNode);
					currentNode = currentNode.parent;
				}
				return solution;
    		}

    		// if not, expand children and continue
			List<Node> children = currentNode.expand(-1);	

			if(heuristic.equals("h0"))
				h0(children);
			else if(heuristic.equals("h1"))
				h1(children);
			else if(heuristic.equals("h2"))
				h2(children);
			
			for(Node child : children)
			{
				open.add(child);
			}		
    	}
    }
    
    public static Stack<Node> ids(Node initialState)
    {
    	Stack<Node> result = new Stack<Node>();
    	int depth = 1;
    	
    	while(true)
    	{
    		result = dfs(initialState, depth);
    		if(result.size() != 0)
    			return result;
			depth++;
			SampleWorld.closed.clear();
    	}    	
    }
    
    public static Stack<Node> ucs(Node initialState)
    {
    	// used to store nodes not visited (generated but not expanded)
    	Queue<Node> open = new LinkedList<Node>();
		open.add(initialState);
		
		Stack<Node> solution = new Stack<Node>();
		while(true)
		{
			if(open.isEmpty())
				return solution;
			
			Node currentNode = open.remove();

			// check if agent is in goal state
			if(currentNode.samples.size() == 0)
			{
				while(currentNode.parent != null)
				{
					solution.push(currentNode);
					currentNode = currentNode.parent;
				}
				return solution;
			}
			
			// otherwise, expand and continue down path
			List<Node> children = currentNode.expand(-1);	
			for(Node child : children)
			{
				open.add(child);
			}
		}
    }
    
    public static Stack<Node> dfs(Node initialState, int depth)
    {	    	
    	// used to store nodes not visited (generated but not expanded)
    	Stack<Node> open = new Stack<Node>();
    	open.push(initialState); 
    	
    	Stack<Node> solution = new Stack<Node>();
    	
    	while(true)
    	{
    		// if open is empty, we have exhausted all of our options and there is no solution
    		if(open.empty())
    			return solution;
    		
    		Node currentNode = open.pop();
    		
    		// check if agent is in goal state
    		if(currentNode.samples.size() == 0)
    		{
    			while(currentNode.parent != null)
    			{
    				solution.push(currentNode);
    				currentNode = currentNode.parent;
    			}
    			return solution;
    		}
    		
    		// otherwise, expand and continue down path
			List<Node> children = currentNode.expand(depth);	
			for(Node child : children)
			{
				open.push(child);
			}		
    	}
    }


    //////////////////////////////////
    // SUITE OF HEURISTIC FUNCTIONS //
    //////////////////////////////////
    
    public static List<Node> h0(List<Node> children)
    {
    	Queue<Node> sorted = new PriorityQueue<>(new NodeComparator());
    	
    	for(int i = 0; i < children.size(); i++)
    	{
    		// estimated moves left: zero
    		children.get(i).heuristic = 0;
			sorted.add(children.get(i));
    	}
    	
		return children;    	
    }
    
    public static List<Node> h1(List<Node> children)
    {
    	Queue<Node> sorted = new PriorityQueue<>(new NodeComparator());
    	
    	for(int i = 0; i < children.size(); i++)
    	{	
    		// estimated moves left: samples left
    		children.get(i).heuristic = children.get(i).samples.size();
    		sorted.add(children.get(i));
    	}
    	return children;
    }
    
    public static Queue<Node> h2(List<Node> children)
    {
    	Queue<Node> sorted = new PriorityQueue<>(new NodeComparator());

    	if(children.size() == 0)
    		return sorted;
    	
    	Node child;
    	  	
    	for(int i = 0; i < children.size(); i++)
    	{	
    		// estimated moves left: 
    		// distance to nearest sample + 1 (+1 to account for sample action)
    		child = children.get(i);
    		child.heuristic = Node.distance(child.agent, child.nearestSample()) + 1;
    					
			sorted.add(child);
    	}

    	return sorted;
    }
}
// end class A1




// Node is used to store all of the current state's details
class Node implements Comparable<Node>
{
	// helper variables just to make indexing arrays easier to read
	public static int row = 0;
	public static int col = 1;
	
	Node parent;
	int[] agent;
	List<int[]> samples;
	char lastMove; // for output/animation
	int distanceTraveled; // for calculating f(n) value
	double heuristic;
	double fn; // distance traveled + heuristic value
	boolean canSample; // for expansion 

	// construct node :)
	public Node(Node parent, int[] agent, List<int[]> samples, char lastMove, int distanceTraveled, double heuristic)
	{
		this.parent = parent;
		this.agent = agent;
		this.samples = new ArrayList<int[]>(samples);
		this.lastMove = lastMove;
		this.distanceTraveled = distanceTraveled;
		this.heuristic = heuristic;
		this.fn = distanceTraveled + heuristic;
	}
	
	// determine valid moves and collect children to be sent back to search
	public List<Node> expand(Integer depth)
	{
		List<Node> children = new ArrayList<Node>();
		if(this.distanceTraveled >= depth && depth != -1)
			return children;
		
		// document expansion of node and get ready to collect this node's children
		SampleWorld.expansions++;
		int[] onSample = new int[2];
	
		// since this state is being currently visited (expanded), put in closed list
		SampleWorld.closed.add(this.getState());
		
		////////////////////////////////////
		// BEGIN CHECKING FOR VALID MOVES //
		////////////////////////////////////
		
		// store coordinates for all potential moves to be checked
		int[] up = 		{ this.agent[row] - 1, this.agent[col] };
		int[] down = 	{ this.agent[row] + 1, this.agent[col] };
		int[] left = 	{ this.agent[row], this.agent[col] - 1 };
		int[] right =  	{ this.agent[row], this.agent[col] + 1 };
		
		// make sure going up doesn't go outside world-bounds or into obstacle
		if(isOpen(up))
		{
			// if move is valid, create that new node/state and document
			Node child = new Node(this, up, this.samples, 'U', this.distanceTraveled+1, this.heuristic);
			SampleWorld.nodesGenerated++;
			
			// make sure that we have not already made that move
			if(!child.getState().inClosed())
				children.add(child);
		}
		
		// same idea but for the different potential moves
		if(isOpen(down))
		{
			Node child = new Node(this, down, this.samples, 'D', this.distanceTraveled+1, this.heuristic);
			SampleWorld.nodesGenerated++;
			
			if(!child.getState().inClosed())
				children.add(child);
		}	
		
		if(isOpen(left))
		{
			Node child = new Node(this, left, this.samples, 'L', this.distanceTraveled+1, this.heuristic);
			SampleWorld.nodesGenerated++;

			if(!child.getState().inClosed())
				children.add(child);
		}

		if(isOpen(right))
		{
			Node child = new Node(this, right, this.samples, 'R', this.distanceTraveled+1, this.heuristic);
			SampleWorld.nodesGenerated++;
			
			if(!child.getState().inClosed())
				children.add(child);
		}			
	
		// CHECK IF CAN SAMPLE
		onSample = canSample();
		if(onSample[0] == 1)
		{			
			Node child = new Node(this, this.agent, this.samples, 'S', this.distanceTraveled+1, this.heuristic);
			child.samples.remove(onSample[1]);					

			SampleWorld.nodesGenerated++;
			children.add(child);
		}
		
		return children;
	}
	
	// helper for expand, verifies whether potential move is legal
	public boolean isOpen(int[] position)
	{
		// check that agent is not trying to move into an obstacle
		for(int i = 0; i < SampleWorld.obstacles.size(); i++)
		{
			if(Arrays.compare(SampleWorld.obstacles.get(i), position) == 0)
				return false;
		}	

		// check that agent is not stepping out of the world
		if(!((position[row] >= 0) && (position[row] <= SampleWorld.worldRows - 1) && (position[col] >= 0) && (position[col] <= SampleWorld.worldCols - 1)))
			return false;
		
		return true;
	}
	
	// returns the coordinates of the sample closest to the agent's current location
	public int[] nearestSample()
	{
		if(samples.size() == 0)
			return agent;
		
		int[] s = samples.get(0);
		double lowest = distance(agent, samples.get(0));
		double distance;
		
		for(int i = 1; i < samples.size(); i++)
		{
			distance = distance(agent, samples.get(i));
			if(distance < lowest)
			{
				lowest = distance;
				s = samples.get(i);
			}
		}
		return s;
	}
	// helper function for nearestSample()
	public static double distance(int[] a, int[] b)
	{	//								 _________________________
		// distance between 2D points = √(y2 - y1)^2 + (x2 - x1)^2
		double total = (((b[row] - a[row])*(b[row] - a[row])) + ((b[col] - a[col])*(b[col] - a[col])));
		total = Math.sqrt(total);
		
		return total;
	}
	
	// helper function for h2
	/////////////////////////////
	// returns two pieces of information if true: 
	// [0] is 1 to indicate agent can sample
	// [1] is the index of the valid sample in the list
	public int[] canSample()
	{
		// default to false
		int[] result = new int[2];
		result[0] = 0;
		result[1] = -1;
		
		for(int i = 0; i < this.samples.size(); i++)
		{		
			if((this.agent[row] == samples.get(i)[row]) && (this.agent[col] == samples.get(i)[col]))
			{
				result[0] = 1;
				result[1] = i;
				return result;			
			}
		}		
		return result;
	}
	
	// returns only the dynamic information directly related to the Node's state.
	// we need a way to extract just this information for the sake of our closed list
	public State getState() 
	{
		return new State(this.agent, this.samples);
	}
	
	// we use to override comparisons for priorityQueues so that our heuristic calculations are actually used
	@Override
	public int compareTo(Node other)
	{
		if(this.fn > other.fn)
			return 1;
		else
			return -1;
	}
	
}
// helper class for Node
class NodeComparator implements Comparator<Node>
{
	// used to sort nodes based on sum of distance traveled + heuristic estimation of work left
	@Override
	public int compare(Node a, Node b)
	{
		if(a.fn > b.fn)
			return 1;
		else
			return -1;
	}
}
// helper class for Node
class State
{
	int[] agent;
	int[][] samples;
	
	public State(int[] agent, List<int[]> samples)
	{
		this.agent = agent;
		this.samples = new int[samples.size()][];
		
		// convert List<int[]> into 2D array of ints [][]
		for(int i = 0; i < samples.size(); i++)
		{
			this.samples[i] = samples.get(i).clone();
		}
	}
	
	public boolean inClosed() 
	{
		for(int i = 0; i < SampleWorld.closed.size(); i++) 
		{
			if(this.equals(SampleWorld.closed.get(i))) 
			{
				return true;
			}
		}
		return false;
	}
	
	public boolean equals(State other)
	{
		if(this.samples.length != other.samples.length)
			return false;
		
		if(Arrays.compare(this.agent, other.agent) != 0)
			return false; 
			
		for(int i = 0; i < this.samples.length; i++)
		{
			if(Arrays.compare(this.samples[i], other.samples[i]) != 0)
				return false;
		}
		
		return true;
	}
}