diff --git a/src/main/java/assessment/BestPathFinder.java b/src/main/java/assessment/BestPathFinder.java index 0128d05..39bc187 100644 --- a/src/main/java/assessment/BestPathFinder.java +++ b/src/main/java/assessment/BestPathFinder.java @@ -4,16 +4,12 @@ import java.util.*; //TODO make non static (CH) public class BestPathFinder { - private final static double timeDistanceFactor = .2; - // overall best path - private static Path max; + private static final double timeValueFactor = .2; // paths to be considered - private final static PriorityQueue paths = new PriorityQueue<>(); - // TODO replace by time check (MH) - private static volatile boolean running = true; + private final PriorityQueue paths = new PriorityQueue<>(); - private final static Set takenPaths = new HashSet<>(); + private final Set takenPaths = new HashSet<>(); /** * @param g het Grid (vierkant) @@ -24,78 +20,70 @@ public class BestPathFinder { * @param y startpositie Y * @return het meest waardevolle pad */ - public static Path findMaxValPath(Grid g, int N, int t, int T, int x, int y) { + public Path findMaxValPath(Grid g, int N, int t, int T, int x, int y) { Path path = Path.newPath(g, x, y); paths.add(path); - max = path; + // overall best path + Path max = path; - // start looping until T is reached or all paths have been considered - while (running && !paths.isEmpty()) { + long t0 = System.currentTimeMillis(); + long t1 = t0 + T; - //TODO take out (MH) - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - -// if (System.currentTimeMillis() > T) { -// running = false; -// } + // start looping until max duration T is reached or all paths have been evaluated + while (System.currentTimeMillis() <= t1 && !paths.isEmpty()) { // take current highest ranking path path = paths.peek(); assert path != null; - while (path.length() > t) { + while (path.length() >= t) { // dit pad heeft lengte t bereikt, we kunnen niet verder - paths.poll(); // geen nieuwe paden op basis van deze, we pakken de vorige in waardering - // heeft dit pad meer waarde dan de huidige max ? + paths.poll(); + // meer waarde dan de huidige max ? if (path.value() > max.value()) { max = path; } + + // pak de volgende path = paths.peek(); } - System.out.println("paths:" + paths.size()); - System.out.println("CUR: " + path); +// System.out.println("paths:" + paths.size()); +// System.out.println("CUR: " + path); Point currentPos = path.getHead(); x = currentPos.x; y = currentPos.y; // find best new directions - List newPointsFromHere = new ArrayList<>(); + List newDirections = new ArrayList<>(); if (y > 0) { - newPointsFromHere.add(new Point(x, y - 1, getValueFromGrid(g, path, x, y - 1))); + newDirections.add(new Point(x, y - 1, getValueFromGrid(g, path, x, y - 1))); if (x < N - 1) { - newPointsFromHere.add(new Point(x + 1, y - 1, getValueFromGrid(g, path, x + 1, y - 1))); + newDirections.add(new Point(x + 1, y - 1, getValueFromGrid(g, path, x + 1, y - 1))); } } if (x > 0) { - newPointsFromHere.add(new Point(x - 1, y, getValueFromGrid(g, path, x - 1, y))); + newDirections.add(new Point(x - 1, y, getValueFromGrid(g, path, x - 1, y))); if (y > 0) { - newPointsFromHere.add(new Point(x - 1, y - 1, getValueFromGrid(g, path, x - 1, y - 1))); + newDirections.add(new Point(x - 1, y - 1, getValueFromGrid(g, path, x - 1, y - 1))); } } if (x < N - 1) { - newPointsFromHere.add(new Point(x + 1, y, getValueFromGrid(g, path, x + 1, y))); + newDirections.add(new Point(x + 1, y, getValueFromGrid(g, path, x + 1, y))); if (y < N - 1) { - newPointsFromHere.add(new Point(x + 1, y + 1, getValueFromGrid(g, path, x + 1, y + 1))); + newDirections.add(new Point(x + 1, y + 1, getValueFromGrid(g, path, x + 1, y + 1))); } } if (y < N - 1) { - newPointsFromHere.add(new Point(x, y + 1, getValueFromGrid(g, path, x, y + 1))); + newDirections.add(new Point(x, y + 1, getValueFromGrid(g, path, x, y + 1))); if (x > 0) - newPointsFromHere.add(new Point(x - 1, y + 1, getValueFromGrid(g, path, x - 1, y + 1))); + newDirections.add(new Point(x - 1, y + 1, getValueFromGrid(g, path, x - 1, y + 1))); } - // sort the new points in descending order of value - newPointsFromHere.sort(Point::compareTo); - - if (!newPointsFromHere.isEmpty()) { + if (!newDirections.isEmpty()) { boolean pointsAdded = false; - for (Point p : newPointsFromHere) { + for (Point p : newDirections) { // is it worthwile going there? - if (p.value > 0) { + if (p.value > 0) { // use (higher) cutoff point? // create a new Path based on the current Path newPath = path.copy(); @@ -105,7 +93,7 @@ public class BestPathFinder { paths.add(newPath); takenPaths.add(newPath); pointsAdded = true; - System.out.println("add " + newPath); +// System.out.println("add " + newPath); } } } @@ -122,18 +110,16 @@ public class BestPathFinder { } /** - * de waarde van een punt x,y wordt bepaald door de beginwaarde - * tenzij we er al geweest zijn - * dan telt de tijd sinds we er geweest zijn - * = afstand in sindsdien afgelegd pad + * de waarde van een punt x,y wordt bepaald door de beginwaarde, tenzij we er al geweest zijn + * dan telt de tijd sinds we er geweest zijn = afstand in sindsdien afgelegd pad * de waarde is rechtevenredig met de afstand */ - private static double getValueFromGrid(Grid grid, Path path, int x, int y) { + private double getValueFromGrid(Grid grid, Path path, int x, int y) { int gridValue = grid.get(x, y); if (path.hasPoint(grid, x, y)) { // been there int distanceInPath = path.getDistanceInPath(x, y); - double increment = gridValue * timeDistanceFactor; + double increment = gridValue * timeValueFactor; return Math.min((distanceInPath - 1) * increment, gridValue); } else { diff --git a/src/test/java/PathFinderTest.java b/src/test/java/PathFinderTest.java index c8278fc..ab4c860 100644 --- a/src/test/java/PathFinderTest.java +++ b/src/test/java/PathFinderTest.java @@ -6,9 +6,23 @@ import org.junit.jupiter.api.Test; public class PathFinderTest { @Test - public void testBestPath() { + public void testBestPath20() { Grid grid = Grid.fromFile("grids/20.txt"); - Path path = BestPathFinder.findMaxValPath(grid, 20, 3, 1000, 9, 9); + Path path = new BestPathFinder().findMaxValPath(grid, 20, 8, 1000, 9, 9); + System.out.println(path); + } + + @Test + public void testBestPath100() { + Grid grid = Grid.fromFile("grids/100.txt"); + Path path = new BestPathFinder().findMaxValPath(grid, 100, 8, 10000, 50, 50); + System.out.println(path); + } + + @Test + public void testBestPath1000() { + Grid grid = Grid.fromFile("grids/1000.txt"); + Path path = new BestPathFinder().findMaxValPath(grid, 1000, 18, 100000, 500, 500); System.out.println(path); } }