Commit f89bfb28 authored by larsesselink's avatar larsesselink
Browse files

-backup

parent 052c7ac0
...@@ -15,8 +15,8 @@ C_i = Cost of the i'th disk ...@@ -15,8 +15,8 @@ C_i = Cost of the i'th disk
public class Algorithm { public class Algorithm {
// private final String fileName = "A:\\Documents\\Computing Science\\AD\\Project1\\testcases\\02-sample.in"; private final String fileName = "A:\\Documents\\Computing Science\\AD\\Project1\\testcases\\02-sample.in";
private final String fileName = "/home/ldesselink/Documents/CS/AD/project1/rep/testcases/02-sample.in"; // private final String fileName = "/home/ldesselink/Documents/CS/AD/project1/rep/testcases/02-sample.in";
private final Vector<Vector<Integer>> input = new Vector<>(); private final Vector<Vector<Integer>> input = new Vector<>();
private final Vector<Pillar> pillars = new Vector<>(); private final Vector<Pillar> pillars = new Vector<>();
private final Vector<Disk> disks = new Vector<>(); private final Vector<Disk> disks = new Vector<>();
...@@ -27,8 +27,8 @@ public class Algorithm { ...@@ -27,8 +27,8 @@ public class Algorithm {
private final Pillar src = new Pillar(0,0, 0); private final Pillar src = new Pillar(0,0, 0);
private Pillar target; private Pillar target;
private final PriorityQueue<Pillar> queue = new PriorityQueue<>(); private final PriorityQueue<Pillar> queue = new PriorityQueue<>();
private final List<Pillar> visited = new ArrayList<>(); private final LinkedHashSet<Pillar> visited = new LinkedHashSet<>();
private final HashMap<Pair<Pillar,Disk>, Long> distance = new HashMap<>(); private final HashMap<Pillar, Pair<Disk,Long>> distance = new HashMap<>();
private final List<Pair<Pillar, Disk>> pairs = new ArrayList<Pair<Pillar, Disk>>(); private final List<Pair<Pillar, Disk>> pairs = new ArrayList<Pair<Pillar, Disk>>();
...@@ -40,10 +40,11 @@ public class Algorithm { ...@@ -40,10 +40,11 @@ public class Algorithm {
initiateVector(); initiateVector();
makePairs(); makePairs();
for(Pair<Pillar, Disk> pair : getAdj(src,new Disk(0,0))){
Pillar pillar = pair.getKey(); // for(Pair<Pillar, Disk> pair : getAdj(src,new Disk(0,0))){
System.out.println(pillar.getID()); // Pillar pillar = pair.getKey();
} // System.out.println(pillar.getID() + " : " + pair.getValue().getRadius());
// }
// for(Pillar pillar: pillars){ // for(Pillar pillar: pillars){
// System.out.print("Pillar " + pillar.getID() + " has neighbour: "); // System.out.print("Pillar " + pillar.getID() + " has neighbour: ");
// for(Pillar pillar2 : pillar.getAdj().keySet()){ // for(Pillar pillar2 : pillar.getAdj().keySet()){
...@@ -61,7 +62,7 @@ public class Algorithm { ...@@ -61,7 +62,7 @@ public class Algorithm {
//// } //// }
// //
for(Pillar pillar: visited){ for(Pillar pillar: visited){
System.out.println(pillar.getID()); System.out.println(pillar.getID()+ " = " + distance.get(pillar).getValue());
} }
...@@ -72,18 +73,18 @@ public class Algorithm { ...@@ -72,18 +73,18 @@ public class Algorithm {
public void algorithm(){ public void algorithm(){
distance.put(new Pair<>(src,new Disk(0,0)),0L); distance.put(src,new Pair<>(new Disk(0,0),0L));
queue.add(src); queue.add(src);
for(Pair<Pillar, Disk> pair:pairs){ for(Pair<Pillar, Disk> pair:pairs){
distance.put(new Pair<>(pair.getKey(),pair.getValue()), Long.MAX_VALUE); distance.put(pair.getKey(),new Pair<>(pair.getValue(),Long.MAX_VALUE));
} }
src.setDisk(new Disk(0,0)); src.setDisk(new Disk(0,0));
while(!queue.isEmpty()){ while(!queue.isEmpty()){
System.out.println(queue.size()); // System.out.println(queue.size());
Pillar pillar1 = queue.remove(); Pillar pillar1 = queue.remove();
Disk disk1 = pillar1.getDisk(); Disk disk1 = pillar1.getDisk();
...@@ -93,12 +94,21 @@ public class Algorithm { ...@@ -93,12 +94,21 @@ public class Algorithm {
Pillar pillar2 = pair.getKey(); Pillar pillar2 = pair.getKey();
Disk disk2 = pair.getValue(); Disk disk2 = pair.getValue();
long tempCost = distance.get(new Pair<>(pillar1,disk1)) + disk2.getCost(); long tempCost = distance.get(pillar1).getKey().getCost() + disk2.getCost();
// System.out.println("From pillar " + pillar1.getID() + " = " + pillar2.getID() + " : " + distance.get(pillar2).getValue() + " > " + tempCost);
if((distance.get(pillar2).getValue() > tempCost)){
distance.put(pillar2, new Pair<>(disk2,tempCost+distance.get(pillar1).getValue()));
pillar1.setDisk(disk1);
pillar2.setDisk(disk2);
}
if((distance.get(new Pair<>(pillar2,disk2)) > tempCost)){ if(!visited.contains(pillar2))
distance.put(new Pair<>(pillar1, disk1),tempCost); {
queue.add(pillar2);
} }
queue.add(pillar2);
} }
...@@ -112,7 +122,10 @@ public class Algorithm { ...@@ -112,7 +122,10 @@ public class Algorithm {
Pillar pillarPair = entry.getKey(); Pillar pillarPair = entry.getKey();
Disk diskPair = entry.getValue(); Disk diskPair = entry.getValue();
if(distancePoints(pillar, pillarPair) < diskPair.getRadius() + disk.getRadius() && pillar != pillarPair){ // System.out.println("Checking " + pillar.getID() + " With " + pillarPair.getID());
// System.out.println("Doing: " + distancePoints(pillar, pillarPair) + " < " + diskPair.getRadius() + " + " + disk.getRadius() + " and " + (pillar != pillarPair));
if(distancePoints(pillar, pillarPair) <= diskPair.getRadius() + disk.getRadius() && pillar != pillarPair){
temp.add(new Pair<>(pillarPair,diskPair)); temp.add(new Pair<>(pillarPair,diskPair));
} }
...@@ -121,9 +134,9 @@ public class Algorithm { ...@@ -121,9 +134,9 @@ public class Algorithm {
} }
public void makePairs (){ public void makePairs (){
for (Pillar pillar : pillars) { for (int i = 1; i < pillars.size()-1; i++) {
for (Disk disk : disks) { for (Disk disk : disks) {
pairs.add(new Pair<>(pillar, disk)); pairs.add(new Pair<>(pillars.get(i), disk));
} }
} }
} }
...@@ -219,11 +232,6 @@ public class Algorithm { ...@@ -219,11 +232,6 @@ public class Algorithm {
} }
public void printVector(Vector<Pillar> temp){
for (Pillar tempPillars : temp) {
System.out.println(tempPillars.getX() + " " + tempPillars.getY());
}
}
} }
public class Pair<X, Y> { public class Pair<X, Y> {
private final Pillar key; private final X key;
private final Disk value; private final Y value;
public Pair(Pillar key, Disk value){ public Pair(X key, Y value){
this.key = key; this.key = key;
this.value = value; this.value = value;
} }
public Pillar getKey() { public X getKey() {
return key; return key;
} }
public Disk getValue() { public Y getValue() {
return value; return value;
} }
public int hashCode() {
return (int) ((key.getID() << 16) + value.getRadius());
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment