Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
/.idea/
2 changes: 1 addition & 1 deletion Algorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public abstract class Algorithm {
}


abstract public void shortestPath(Cities.cities start, Cities.cities end);
abstract public void shortestPath(City start, City end);

}
125 changes: 55 additions & 70 deletions Board.java
Original file line number Diff line number Diff line change
@@ -1,99 +1,84 @@
import javax.swing.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

class Board {
private int[][] board,tollBoard;
private int size;
public Cities myCities;

private void createGraph()
private void createGraph(String configFileName)
{
myCities.getCities(configFileName);
size = myCities.size;

board = new int[size][size];
tollBoard = new int[size][size];
for (int x = 0; x < size; x++)
for (int y = 0; y < size; y++) {
board[x][y] = 0;
tollBoard[x][y] = 0;
}

// now we read the connections from the config file
File configFile = null;
Scanner reader = null;

// All Possible Connections Between All The Train Station
setBoard(Cities.cities.Emden, Cities.cities.Groningen, 3,3);
setBoard(Cities.cities.Emden, Cities.cities.Lingen, 6,1);
setBoard(Cities.cities.Emden, Cities.cities.Emmen, 4,4);
setBoard(Cities.cities.Groningen, Cities.cities.Emmen, 3,3);
setBoard(Cities.cities.Groningen, Cities.cities.Waddeneilanden,6,4);
setBoard(Cities.cities.Groningen, Cities.cities.Leeuwarden,3,2);
setBoard(Cities.cities.Groningen, Cities.cities.Zwolle,6,4);
setBoard(Cities.cities.Waddeneilanden, Cities.cities.DenHelder,5,4);
setBoard(Cities.cities.Waddeneilanden, Cities.cities.Sneek,3,4);
setBoard(Cities.cities.Waddeneilanden, Cities.cities.Leeuwarden,2,3);
setBoard(Cities.cities.Leeuwarden, Cities.cities.Sneek,1,1);
setBoard(Cities.cities.Sneek, Cities.cities.DenHelder,4,3);
setBoard(Cities.cities.Sneek, Cities.cities.Lelystad,4,3);
setBoard(Cities.cities.DenHelder, Cities.cities.Haarlem,4,2);
setBoard(Cities.cities.Haarlem, Cities.cities.Amsterdam,1,1);
setBoard(Cities.cities.Haarlem, Cities.cities.DenHaag,2,2);
setBoard(Cities.cities.DenHaag, Cities.cities.Rotterdam,1,1);
setBoard(Cities.cities.DenHaag, Cities.cities.Middelburg,6,4);
setBoard(Cities.cities.Middelburg, Cities.cities.Antwerpen,5,3);
setBoard(Cities.cities.Antwerpen, Cities.cities.Rotterdam,5,4);
setBoard(Cities.cities.Antwerpen, Cities.cities.Turnhout,2,2);
setBoard(Cities.cities.Antwerpen, Cities.cities.Aarschot,2,1);
setBoard(Cities.cities.Aarschot, Cities.cities.Hasselt,2,1);
setBoard(Cities.cities.Aarschot, Cities.cities.Liege,5,1);
setBoard(Cities.cities.Liege, Cities.cities.Hasselt,2,1);
setBoard(Cities.cities.Liege, Cities.cities.Maastricht,2,1);
setBoard(Cities.cities.Maastricht, Cities.cities.Hasselt,2,2);
setBoard(Cities.cities.Maastricht, Cities.cities.Eindhoven,4,2);
setBoard(Cities.cities.Maastricht, Cities.cities.Roermond,2,1);
setBoard(Cities.cities.Roermond, Cities.cities.Eindhoven,3,3);
setBoard(Cities.cities.Roermond, Cities.cities.Duisburg,3,2);
setBoard(Cities.cities.Duisburg, Cities.cities.Nijmegen,4,2);
setBoard(Cities.cities.Duisburg, Cities.cities.Enschede,6,1);
setBoard(Cities.cities.Enschede, Cities.cities.Arnhem,5,2);
setBoard(Cities.cities.Enschede, Cities.cities.Zwolle,4,1);
setBoard(Cities.cities.Enschede, Cities.cities.Lingen,3,3);
setBoard(Cities.cities.Lingen, Cities.cities.Emmen,3,2);
setBoard(Cities.cities.Emmen, Cities.cities.Zwolle,4,3);
setBoard(Cities.cities.Zwolle, Cities.cities.Lelystad,2,4);
setBoard(Cities.cities.Zwolle, Cities.cities.Arnhem,4,2);
setBoard(Cities.cities.Lelystad, Cities.cities.Amsterdam,3,3);
setBoard(Cities.cities.Amsterdam, Cities.cities.Rotterdam,4,2);
setBoard(Cities.cities.Amsterdam, Cities.cities.Utrecht,1,1);
setBoard(Cities.cities.Utrecht, Cities.cities.Rotterdam,3,3);
setBoard(Cities.cities.Utrecht, Cities.cities.Arnhem,4,1);
setBoard(Cities.cities.Arnhem, Cities.cities.Nijmegen,1,3);
setBoard(Cities.cities.Nijmegen, Cities.cities.Eindhoven,3,3);
setBoard(Cities.cities.Eindhoven, Cities.cities.DenBosch,1,2);
setBoard(Cities.cities.DenBosch, Cities.cities.Breda,2,2);
setBoard(Cities.cities.Breda, Cities.cities.Rotterdam,2,4);
setBoard(Cities.cities.Breda, Cities.cities.Turnhout,2,2);
setBoard(Cities.cities.Turnhout, Cities.cities.Hasselt,3,3);
}
try {
configFile = new File(configFileName);
reader = new Scanner(configFile);

} catch (FileNotFoundException e) {
System.out.println("File not found");
System.exit(0);
}

public Board() {
this.size = Cities.cities.values().length;
board = new int[size][size];
tollBoard = new int[size][size];
this.createGraph();
reader.nextLine(); // skip the first line

while (reader.hasNext()) {

String line = reader.nextLine();
String[] tokens = line.split(",");
City from = myCities.lookup(tokens[0].trim());
if (from == null) {
System.out.println("Error: _" + tokens[0] + "_ not found");
System.exit(0);
}
City to = myCities.lookup(tokens[1].trim());
if (to == null) {
System.out.println("Error: _" + tokens[1] + "_ not found");
System.exit(0);
}
System.out.println("Adding connection: " + tokens[0] + " to " + tokens[1] + " cost: " + tokens[2] + " toll: " + tokens[3]);

setBoard(from.index, to.index, Integer.parseInt(tokens[2]), Integer.parseInt(tokens[3]));
}
}



Board(String configfileName) {
super();
myCities = new Cities();

createGraph(configfileName);
}

public void setBoard(Cities.cities x, Cities.cities y, int value, int bridgeTool) {
public void setBoard(int x, int y, int value, int bridgeToll) {
// Used to set the distance between 2 cities
// First version of this program we will use just the distance. Once we have that working
// We can switch the int to a class that contains the distance and the colours of the route and cost
board[x.ordinal()][y.ordinal()] = value;
board[y.ordinal()][x.ordinal()] = value;
tollBoard[x.ordinal()][y.ordinal()] = bridgeTool;
tollBoard[y.ordinal()][x.ordinal()] = bridgeTool;
board[x][y] = value;
board[y][x] = value;
tollBoard[x][y] = bridgeToll;
tollBoard[y][x] = bridgeToll;
}

public int getBoard(Cities.cities x, Cities.cities y) {
return board[x.ordinal()][y.ordinal()];
public int getBoard(City x, City y) {
return board[x.index][y.index];
}
public int getBridgeToll(Cities.cities x, Cities.cities y) {
return tollBoard[x.ordinal()][y.ordinal()];
public int getBridgeToll(City x, City y) {
return tollBoard[x.index][y.index];
}

public void printBoard() {
Expand Down
28 changes: 11 additions & 17 deletions BruteForce.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class BruteForce extends Algorithm {
private List<CostPath> paths;



BruteForce(Board b) {
super(b);
paths = new ArrayList<CostPath>();
Expand Down Expand Up @@ -37,17 +38,17 @@ public void listPaths()
for (CostPath cp :paths) {
System.out.print("Cost: " + cp.cost);
System.out.print(" Path: ");
for (Cities.cities c : cp.path) {
System.out.print(Cities.names[c.ordinal()]+"-");
for (City c : cp.path) {
System.out.print(c.name +"-");
}
System.out.println();
}

}

public void shortestPath(Cities.cities start, Cities.cities end) {
List<Cities.cities> visited = new ArrayList<Cities.cities>();
public void shortestPath(City start, City end) {

List<City> visited = new ArrayList<>();
visited.add(start);
long startTime = System.nanoTime();
shortestPath(start, end, 0, visited, 0);
Expand All @@ -57,11 +58,11 @@ public void shortestPath(Cities.cities start, Cities.cities end) {
return;
}

public void shortestPath(Cities.cities start, Cities.cities end, int cost, List<Cities.cities> visited, int level) {
public void shortestPath(City start, City end, int cost, List<City> visited, int level) {
// System.out.println("BruteForce Shortest Path: level " + level);

// Iterate over all cities
for (Cities.cities c : Cities.cities.values()) {
for (City c : b.myCities.cities) {

// The cost to go from start to the next step (c)
int nextStep = b.getBoard(start, c);
Expand All @@ -71,13 +72,11 @@ public void shortestPath(Cities.cities start, Cities.cities end, int cost, List<
// This is crucial step, otherwise the recursion never ends
// DO nothing
}
else if (nextStep > 0 && c == end) {
else if (nextStep > 0 && c == end) {
// If the cost != 0 and c is the end, then we are there
// We have reached the end
// Return the cost and the path
CostPath cp = new CostPath();
cp.cost = cost + nextStep;
cp.path = new ArrayList<Cities.cities>(visited);
CostPath cp = new CostPath(cost + nextStep,new ArrayList<City>(visited));
cp.path.add(end);
paths.add(cp);
//System.out.println("Found path with cost " + cp.cost);
Expand All @@ -87,19 +86,14 @@ else if (nextStep > 0)
// There is a route between start and c
// Recurse to find the all paths

List<Cities.cities>nextVisited = new ArrayList<Cities.cities>(visited);
List<City>nextVisited = new ArrayList<City>(visited);

nextVisited.add(c);

shortestPath(c, end, cost+nextStep, nextVisited, level + 1);
}

}
// we have tried all cities. Return
return;

}



}
72 changes: 61 additions & 11 deletions Cities.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,66 @@
import javax.crypto.Cipher;
import java.io.*;
import java.util.*;
import java.lang.System;


public class Cities {


static enum cities { Emden, Groningen, Lingen, Emmen, Waddeneilanden, Leeuwarden, Sneek, DenHelder, Zwolle,
Lelystad, Enschede, Haarlem, Amsterdam, Utrecht, Arnhem, Nijmegen, DenBosch, Maastricht, Rotterdam, DenHaag,
Eindhoven, Duisburg, Breda, Antwerpen, Liege, Turnhout, Roermond, Middelburg, Aarschot, Hasselt };

static String[] names = { "Emden", "Groningen", "Lingen", "Emmen", "Waddeneilanden", "Leeuwarden", "Sneek", "Den Helder", "Zwolle",
"Lelystad", "Enschede", "Haarlem", "Amsterdam", "Utrecht", "Arnhem", "Nijmegen", "Den Bosch", "Maastricht", "Rotterdam", "Den Haag",
"Eindhoven", "Duisburg", "Breda", "Antwerpen", "Liège", "Turnhout", "Roermond", "Middelburg", "Aarschot", "Hasselt"};

}
// TODO: Next step: Remove the enum completely and change the rest of the classes to
// use the cityCount() function instead of cities.length
// All instances of Cities.cities need to be replaced with a for loop and an int
// Need to figure out how to acces Cities through BruteForce

List<String> names = new ArrayList<>();

public int size;

public List<City> cities = new ArrayList<>();

public int cityCount() {
return cities.size();
}

public City lookup(String string) // Finds the city that the string is related to
{
for (City c: cities) {
if (Objects.equals(string, c.name))
return c;
}
return null;
}
public void getCities(String configFileName){
// We read the cities from the config file here
System.out.println("Reading config file: " + configFileName);
File configFile = null;
Scanner reader = null;

try {
configFile = new File(configFileName);
reader = new Scanner(configFile);

} catch (FileNotFoundException e) {
System.out.println("File not found");
System.exit(0);
}

if (reader.hasNext()) {
// The first line has the list of cities
String line = reader.nextLine();
String[] str = line.split(",");
for (String s : str) {
names.add(s.trim());
}
size = names.size();
System.out.println( "Cities: " );
for (int i = 0; i < names.size(); i++) {
City c = new City (i,names.get(i).trim());
cities.add(c);
System.out.print(c.name + ", ");
}

}
System.out.println("Reading connections");


}
}
8 changes: 8 additions & 0 deletions City.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class City {
public int index;
public String name;
public City(int index, String name){
this.index = index;
this.name = name;
}
}
3 changes: 0 additions & 3 deletions Colour.java

This file was deleted.

6 changes: 5 additions & 1 deletion CostPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@

public class CostPath {
public int cost;
public List<Cities.cities> path;
public List<City> path;
public CostPath(int cost,List<City> path){
this.path = path;
this.cost = cost;
}
}
Loading