Skip to content

Commit

Permalink
bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
robertBallard committed Feb 26, 2018
1 parent e91d669 commit b1b88cf
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 40 deletions.
11 changes: 7 additions & 4 deletions src/main/java/mars/MARS.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import mars.algorithm.*;
import mars.algorithm.limited.AlgorithmLimitedDijkstra;
import mars.coordinate.Coordinate;
import mars.out.OutputFactory;
import mars.rover.MarsRover;
import mars.ui.TerminalInterface;

Expand All @@ -22,20 +23,22 @@ public static void main(String[] args) throws Exception {
Coordinate endCoord = new Coordinate(7568,1727);
String mapPath = "src/main/resources/Phobos_Viking_Mosaic_40ppd_DLRcontrol.tif";
//String mapPath = "src/main/resources/Phobos_Viking_Mosaic_40ppd_DLRcontrol.tif";
MarsRover rover = new MarsRover(6 ,startCoord,endCoord,mapPath,3);
MarsRover rover = new MarsRover(6 ,startCoord,endCoord,mapPath,50);
//Algorithm algorithm = new AlgorithmGreedy(rover,"limited");
//Algorithm algorithm = new AlgorithmUnlimitedScopeRecursive(rover);
//Algorithm algorithm = new AlgorithmUnlimitedScopeNonrecursive(rover);
//Algorithm algorithm = new AlgorithmLimitedScopeAStar(rover);
//Algorithm algorithm = new AlgorithmLimitedDijkstra(rover);
//Algorithm algorithm = new AlgorithmLimitedDijkstrdIDAStara(rover);
//Algorithm algorithm = new AlgorithmUnlimitedDijkstra(rover);
Algorithm algorithm = new AlgorithmLimitedIDAStar(rover,"TerminalOutput");
//Algorithm algorithm = new AlgorithmLimitedScopeAStar(rover,"MapImageOutput");
Algorithm algorithm = new AlgorithmUnlimitedBestFirst(rover,"MapImageOutput");
try {
algorithm.findPath();

} catch (Exception expectedException) {
//assertTrue("Failed to find a route it should have",false);
}

OutputFactory.getOutput(algorithm);
TerminalInterface ti = new TerminalInterface();
ti.promptUser();
}
Expand Down
71 changes: 38 additions & 33 deletions src/main/java/mars/algorithm/limited/AlgorithmLimitedBestFirst.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public AlgorithmLimitedBestFirst(MarsRover r, String output) {
map = rover.getMap();
goal = r.getEndPosition();
fieldOfView = r.getFieldOfView();
outputClass = output;
}

/*
Expand Down Expand Up @@ -79,7 +80,6 @@ public void BestFirstSearch(ArrayList<BestFirstCoordinate> coords) throws Except
backtrackDistance++;
}
}

thisCoord = coords.get(coords.size() - 1);
System.out.println((thisCoord.getX()) + "," + (thisCoord.getY())); //debug
}
Expand Down Expand Up @@ -143,6 +143,39 @@ public ArrayList<BestFirstCoordinate> BestFirst(BestFirstCoordinate startPositio
}

//------------------Helper_methods---------------------------------------------------

/**
* finds angle between two coordinates
* @param current first coordinate
* @param goal second coordinate
* @return angle to second coordinate (0 = east)
*/
public double getAngleToGoal(Coordinate current, Coordinate goal) {
int xdiff = goal.getX() - current.getX();
int ydiff = goal.getY() - current.getY();
double result = Math.toDegrees(Math.atan2(ydiff,xdiff));
while(result < 0){result += 360;}
return result;
}

/**
* boolean to check if a given coordinate is unique and in range of what the rover has seen by this point
* @param target coord to check
* @return boolean if acceptable
*/
public boolean checkIfViewed(Coordinate target){
boolean viewed = false;
for(BestFirstCoordinate item : path){ //for each item in the overall path (not just for the iteration!)
if(target.equals(item)){ //if we're considering a coord that's unvisited for the iteration but not the overall run, then fail
return false; //no repeats allowed
}
if(getDistanceToPoint(target,item) <= fieldOfView){ //and if we've seen this coord, it's acceptable
viewed = true; //we now know it's in range, but still have to check for repeats
}
}
return viewed; //if it's in range, true, if not, false
}

/**
* Given a coordinate, return all the neighboring coordinates
* which can be visited by this algorithm's rover (meaning that
Expand Down Expand Up @@ -176,23 +209,7 @@ public ArrayList<BestFirstCoordinate> getReachableNeighbors(BestFirstCoordinate
return neighbors;
}

/**
* boolean to check if a given coordinate is unique and in range of what the rover has seen by this point
* @param target coord to check
* @return boolean if acceptable
*/
public boolean checkIfViewed(Coordinate target){
boolean viewed = false;
for(BestFirstCoordinate item : path){ //for each item in the overall path (not just for the iteration!)
if(target.equals(item)){ //if we're considering a coord that's unvisited for the iteration but not the overall run, then fail
return false; //no repeats allowed
}
if(getDistanceToPoint(target,item) <= fieldOfView){ //and if we've seen this coord, it's acceptable
viewed = true; //we now know it's in range, but still have to check for repeats
}
}
return viewed; //if it's in range, true, if not, false
}


/**
* Heuristic function for BestFirst implementing diagonal distance between two nodes
Expand All @@ -211,32 +228,20 @@ private double estimateHeuristic(Coordinate current, Coordinate goal) {
double goalXPos = goal.getX();
double goalYPos = goal.getY();

return Math.max(Math.abs(currentXPos - goalXPos) , Math.abs(currentYPos - goalYPos));
return (Math.abs(currentXPos - goalXPos) + Math.abs(currentYPos - goalYPos));
}

private BestFirstCoordinate getLowestFScore(ArrayList<BestFirstCoordinate> list) {
BestFirstCoordinate lowest = list.get(0);
for (BestFirstCoordinate n : list) {
if (estimateHeuristic(n, goal) < estimateHeuristic(lowest, goal)) {
if (estimateHeuristic(n, interimGoal) < estimateHeuristic(lowest, interimGoal)) {
lowest = n;
}
}
return lowest;
}

/**
* finds angle between two coordinates
* @param current first coordinate
* @param goal second coordinate
* @return angle to second coordinate (0 = east)
*/
public double getAngleToGoal(Coordinate current, Coordinate goal) {
int xdiff = goal.getX() - current.getX();
int ydiff = goal.getY() - current.getY();
double result = Math.toDegrees(Math.atan2(ydiff,xdiff));
while(result < 0){result += 360;}
return result;
}


/**
* Given a coordinate, calculate its euclidean distance to the rover's goal coordinate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public AlgorithmLimitedDijkstra(MarsRover r, String output) {
map = r.getMap();
goal = new DijkstraNode(r.getEndPosition());
fieldOfView = r.getFieldOfView();
outputClass = output;
}

public ArrayList<Coordinate> getPath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ public void IDAStarSearch(ArrayList<AStarCoordinate> coords) throws Exception {
*/
public ArrayList<AStarCoordinate> IDAStar(AStarCoordinate thisNode, Coordinate currentGoal) throws Exception {
ArrayList<AStarCoordinate> currentPath = constructPath(thisNode);
if (coordIsInSet(thisNode, currentPath.subList(0,currentPath.size()-1))){
/*if (coordIsInSet(thisNode, currentPath.subList(0,currentPath.size()-1))){
throw new Exception("WARNING: A path to the goal could not be found.");
// no loops
}
}*/
if (thisNode == null) {
if(getDistanceToPoint(bestNode,interimGoal) < (fieldOfView-1)){
return constructPath(bestNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void AStarSearch(ArrayList<AStarCoordinate> coords) throws Exception {
}else{
//System.out.printf("bt"); //backtrack by one. it can't visit thisCoord anymore since it already visited it
backtrackDistance++; //first backtrackDistance to get the next backtrack
backCoord = coords.get(coords.size()-1-backtrackDistance);
backCoord = coords.get(coords.size() - 1 - backtrackDistance);
coords.add(backCoord); //add the backtrack coordinate as the next place.
backtrackDistance++; //and a second one to account for the new entry to the overall path
}
Expand Down

0 comments on commit b1b88cf

Please sign in to comment.