small improvements and scaling up
This commit is contained in:
parent
99db1345ae
commit
a3cf7b6a27
2 changed files with 50 additions and 50 deletions
|
|
@ -4,16 +4,12 @@ import java.util.*;
|
||||||
|
|
||||||
//TODO make non static (CH)
|
//TODO make non static (CH)
|
||||||
public class BestPathFinder {
|
public class BestPathFinder {
|
||||||
private final static double timeDistanceFactor = .2;
|
private static final double timeValueFactor = .2;
|
||||||
// overall best path
|
|
||||||
private static Path max;
|
|
||||||
|
|
||||||
// paths to be considered
|
// paths to be considered
|
||||||
private final static PriorityQueue<Path> paths = new PriorityQueue<>();
|
private final PriorityQueue<Path> paths = new PriorityQueue<>();
|
||||||
// TODO replace by time check (MH)
|
|
||||||
private static volatile boolean running = true;
|
|
||||||
|
|
||||||
private final static Set<Path> takenPaths = new HashSet<>();
|
private final Set<Path> takenPaths = new HashSet<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param g het Grid (vierkant)
|
* @param g het Grid (vierkant)
|
||||||
|
|
@ -24,78 +20,70 @@ public class BestPathFinder {
|
||||||
* @param y startpositie Y
|
* @param y startpositie Y
|
||||||
* @return het meest waardevolle pad
|
* @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);
|
Path path = Path.newPath(g, x, y);
|
||||||
paths.add(path);
|
paths.add(path);
|
||||||
max = path;
|
// overall best path
|
||||||
|
Path max = path;
|
||||||
|
|
||||||
// start looping until T is reached or all paths have been considered
|
long t0 = System.currentTimeMillis();
|
||||||
while (running && !paths.isEmpty()) {
|
long t1 = t0 + T;
|
||||||
|
|
||||||
//TODO take out (MH)
|
// start looping until max duration T is reached or all paths have been evaluated
|
||||||
try {
|
while (System.currentTimeMillis() <= t1 && !paths.isEmpty()) {
|
||||||
Thread.sleep(1000);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// if (System.currentTimeMillis() > T) {
|
|
||||||
// running = false;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// take current highest ranking path
|
// take current highest ranking path
|
||||||
path = paths.peek();
|
path = paths.peek();
|
||||||
assert path != null;
|
assert path != null;
|
||||||
while (path.length() > t) {
|
while (path.length() >= t) {
|
||||||
// dit pad heeft lengte t bereikt, we kunnen niet verder
|
// 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
|
paths.poll();
|
||||||
// heeft dit pad meer waarde dan de huidige max ?
|
// meer waarde dan de huidige max ?
|
||||||
if (path.value() > max.value()) {
|
if (path.value() > max.value()) {
|
||||||
max = path;
|
max = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pak de volgende
|
||||||
path = paths.peek();
|
path = paths.peek();
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("paths:" + paths.size());
|
// System.out.println("paths:" + paths.size());
|
||||||
System.out.println("CUR: " + path);
|
// System.out.println("CUR: " + path);
|
||||||
Point currentPos = path.getHead();
|
Point currentPos = path.getHead();
|
||||||
x = currentPos.x;
|
x = currentPos.x;
|
||||||
y = currentPos.y;
|
y = currentPos.y;
|
||||||
|
|
||||||
// find best new directions
|
// find best new directions
|
||||||
List<Point> newPointsFromHere = new ArrayList<>();
|
List<Point> newDirections = new ArrayList<>();
|
||||||
if (y > 0) {
|
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) {
|
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) {
|
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) {
|
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) {
|
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) {
|
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) {
|
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)
|
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
|
if (!newDirections.isEmpty()) {
|
||||||
newPointsFromHere.sort(Point::compareTo);
|
|
||||||
|
|
||||||
if (!newPointsFromHere.isEmpty()) {
|
|
||||||
boolean pointsAdded = false;
|
boolean pointsAdded = false;
|
||||||
for (Point p : newPointsFromHere) {
|
for (Point p : newDirections) {
|
||||||
// is it worthwile going there?
|
// 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
|
// create a new Path based on the current
|
||||||
Path newPath = path.copy();
|
Path newPath = path.copy();
|
||||||
|
|
||||||
|
|
@ -105,7 +93,7 @@ public class BestPathFinder {
|
||||||
paths.add(newPath);
|
paths.add(newPath);
|
||||||
takenPaths.add(newPath);
|
takenPaths.add(newPath);
|
||||||
pointsAdded = true;
|
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
|
* de waarde van een punt x,y wordt bepaald door de beginwaarde, tenzij we er al geweest zijn
|
||||||
* tenzij we er al geweest zijn
|
* dan telt de tijd sinds we er geweest zijn = afstand in sindsdien afgelegd pad
|
||||||
* dan telt de tijd sinds we er geweest zijn
|
|
||||||
* = afstand in sindsdien afgelegd pad
|
|
||||||
* de waarde is rechtevenredig met de afstand
|
* 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);
|
int gridValue = grid.get(x, y);
|
||||||
if (path.hasPoint(grid, x, y)) {
|
if (path.hasPoint(grid, x, y)) {
|
||||||
// been there
|
// been there
|
||||||
int distanceInPath = path.getDistanceInPath(x, y);
|
int distanceInPath = path.getDistanceInPath(x, y);
|
||||||
double increment = gridValue * timeDistanceFactor;
|
double increment = gridValue * timeValueFactor;
|
||||||
|
|
||||||
return Math.min((distanceInPath - 1) * increment, gridValue);
|
return Math.min((distanceInPath - 1) * increment, gridValue);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -6,9 +6,23 @@ import org.junit.jupiter.api.Test;
|
||||||
public class PathFinderTest {
|
public class PathFinderTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBestPath() {
|
public void testBestPath20() {
|
||||||
Grid grid = Grid.fromFile("grids/20.txt");
|
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);
|
System.out.println(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue