Commit bb839801 authored by Lars Esselink's avatar Lars Esselink
Browse files

- backup

parent 32722ee9
......@@ -16,8 +16,8 @@ C_i = Cost of the i'th disk
public class Algorithm {
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 = "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 Vector<Vector<Integer>> input = new Vector<>();
private final Vector<Pillar> pillars = new Vector<>();
private final Vector<Disk> disks = new Vector<>();
......@@ -27,9 +27,10 @@ public class Algorithm {
private int W;
private final Pillar src = new Pillar(0,0, 0);
private Pillar target;
private PriorityQueue<Pillar> queue = new PriorityQueue<>();
private List<Pillar> visited = new ArrayList<>();
private HashMap<Pillar, Long> distance = new HashMap<Pillar,Long>();
private final PriorityQueue<Pillar> queue = new PriorityQueue<>();
private final List<Pillar> visited = new ArrayList<>();
private final HashMap<Pair<Pillar,Disk>, Long> distance = new HashMap<>();
private final List<Pair> pairs = new ArrayList<>();
private double maxDistance;
private double maxDistanceHalf;
......@@ -40,10 +41,12 @@ public class Algorithm {
src.setCost(0L);
initiateVector();
makeEdges();
makePairs();
for(Pair pair : getAdj(src,new Disk(0,0))){
Pillar pillar = (Pillar) pair.getKey();
System.out.println(pillar.getID());
}
// for(Pillar pillar: pillars){
// System.out.print("Pillar " + pillar.getID() + " has neighbour: ");
// for(Pillar pillar2 : pillar.getAdj().keySet()){
......@@ -53,88 +56,47 @@ public class Algorithm {
// }
algorithm();
//
// visited = visited.stream().distinct().collect(Collectors.toList());
//
//// for(Pillar pillar: distance.keySet()){
//// System.out.println(pillar.getID() + " : " + pillar.getCost());
//// }
//
for(Pillar pillar: visited){
System.out.println(pillar.getID());
}
visited = visited.stream().distinct().collect(Collectors.toList());
// for(Pillar pillar: distance.keySet()){
// System.out.println(pillar.getID() + " : " + pillar.getCost());
// }
// for(Pillar pillar: visited){
// System.out.println(pillar.getID());
// }
calculateCost();
}
public void calculateCost(){
for(int i = 0; i < visited.size()-1; i++){
Pillar pillar1 = visited.get(i);
Pillar pillar2 = visited.get(i+1);
Disk disk1;
Disk disk2;
for(int k = 0; k < disks.size(); k++){
for(int l = k; l < disks.size(); l++){
disk1 = disks.get(k);
disk2 = disks.get(l);
long distanceDisk = (disk1.getRadius()+disk2.getRadius());
long distancePillars = (long) distancePoints(pillar1.getX(),pillar1.getY(),pillar2.getX(),pillar2.getY());
if((distancePillars <= distanceDisk) && (pillar1.getDisk().getRadius() < disk1.getRadius()) && checkIfSoT(pillar1,pillar2)){
System.out.println("Pillar " + pillar1.getID() + " gets disk with radius: " + disk1.getRadius());
// System.out.println("Pillar " + pillar2.getID() + " gets disk with radius: " + disk2.getRadius());
pillar1.setDisk(disk1);
pillar2.setDisk(disk2);
}else if ((distancePointToLine(pillar1,pillar2) >= disk1.getRadius()) && pillar1.getDisk().getRadius() < disk1.getRadius() && pillar2.getID() == -1){
System.out.println("Pillar " + pillar1.getID() + " gets disk with radius: " + disk1.getRadius());
pillar1.setDisk(disk1);
} else if ((distancePointToLine(pillar2,pillar1) >= disk2.getRadius()) && pillar1.getDisk().getRadius() < disk1.getRadius() && pillar1.getID() == 0){
System.out.println("Pillar " + pillar2.getID() + " gets disk with radius: " + disk2.getRadius());
pillar2.setDisk(disk2);
}
}
}
}
long cost = 0;
pillars.get(0).setDisk(new Disk(0,0));
pillars.get(pillars.size()-1).setDisk(new Disk(0,0));
for(Pillar pillar: visited){
System.out.println(pillar.getID() + " : " + pillar.getDisk().getCost());
cost += pillar.getDisk().getCost();
}
System.out.println(cost);
}
public void algorithm(){
distance.put(src,0L);
distance.put(new Pair<>(src,null),0L);
queue.add(src);
for(Pair pair:pairs){
distance.put(new Pair<>(pair.getKey(),pair.getValue()), Long.MAX_VALUE);
}
Disk disk1 = new Disk(0,0);
while(!queue.isEmpty()){
System.out.println(queue.size());
Pillar pillar1 = queue.remove();
visited.add(pillar1);
Long cost = pillar1.getCost();
for(Map.Entry<Pillar,Long> entry: pillar1.getAdj().entrySet()){
Pillar pillar2 = entry.getKey();
Long edgeWeight = entry.getValue();
for(Pair pair : getAdj(pillar1,disk1)){
Pillar pillar2 = (Pillar) pair.getKey();
Disk disk2 = (Disk) pair.getValue();
System.out.println("Cost: " + cost + " edgeweight: " + edgeWeight);
if((pillar2.getCost() > cost + edgeWeight)){
distance.put(pillar1,cost+edgeWeight);
pillar2.setCost(cost + edgeWeight);
long tempCost = distance.get(pillar1) + disk2.getCost();
if((distance.get(pillar2) > tempCost)){
distance.put(pillar1,tempCost);
}
queue.add(pillar2);
......@@ -143,36 +105,44 @@ public class Algorithm {
}
}
public void makeEdges(){
for(int k = 0; k < pillars.size(); k++){
Pillar pillar1 = pillars.get(k);
for(int j = k; j < pillars.size(); j++){
Pillar pillar2 = pillars.get(j);
if(!itselfCheck(pillar1,pillar2) && distanceCheck(pillar1,pillar2) && checkIfSoT(pillar1,pillar2)){
pillar1.addAdj(pillar2, (long) distancePoints(pillar1.getX(), pillar1.getY(), pillar2.getX(), pillar2.getY()));
} else if (!itselfCheck(pillar1,pillar2) && pillar1.getID() == 0 && distanceCheck(pillar1,pillar2)) {
pillar1.addAdj(pillar2, distancePointToLine(pillar2, pillar1));
} else if (!itselfCheck(pillar1,pillar2) && distanceCheck(pillar1,pillar2)){
pillar1.addAdj(pillar2, distancePointToLine(pillar1,pillar2));
}
public List<Pair<Pillar,Disk>> getAdj(Pillar pillar, Disk disk){
List<Pair<Pillar,Disk>> temp = new ArrayList<>();
for(Map.Entry<Pillar,Disk> entry : pairs.entrySet()){
Pillar pillarPair = entry.getKey();
Disk diskPair = entry.getValue();
if(distancePoints(pillar, pillarPair) < diskPair.getRadius() + disk.getRadius() && pillar != pillarPair){
temp.add(new Pair<>(pillarPair,diskPair));
}
}
}
public boolean itselfCheck(Pillar pillar1, Pillar pillar2){
return pillar1 == pillar2;
}
return temp;
}
public boolean distanceCheck(Pillar pillar1, Pillar pillar2){
if(checkIfSoT(pillar1,pillar2)){
return distancePoints(pillar1.getX(), pillar1.getY(), pillar2.getX(), pillar2.getY()) <= maxDistance;
} else{
return distancePoints(pillar1.getX(), pillar1.getY(), pillar2.getX(), pillar2.getY()) <= maxDistanceHalf;
public void makePairs (){
for(int i = 0; i < pillars.size();i++){
for(int j = 0; j < disks.size();j++){
pairs.put(pillars.get(i),disks.get(j));
}
}
}
public double distancePoints(double x1, double y1, double x2, double y2) {
// public boolean distanceCheck(Pillar pillar1, Pillar pillar2){
// if(checkIfSoT(pillar1,pillar2)){
// return distancePoints(pillar1.getX(), pillar1.getY(), pillar2.getX(), pillar2.getY()) <= maxDistance;
// } else{
// return distancePoints(pillar1.getX(), pillar1.getY(), pillar2.getX(), pillar2.getY()) <= maxDistanceHalf;
// }
// }
public double distancePoints(Pillar pillar1,Pillar pillar2) {
double x1 = pillar1.getX();
double y1 = pillar1.getY();
double x2 = pillar2.getX();
double y2 = pillar2.getY();
return (long) Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
}
......@@ -201,7 +171,7 @@ public class Algorithm {
N = input.get(0).get(0);
M = input.get(0).get(1);
W = input.get(0).get(2);
target = new Pillar(0,W, -1);
target = new Pillar(0,W, 10000 );
Disk tempDisk = null;
for(int i = N + 1; i <= N + M; i++){
......
......@@ -6,7 +6,7 @@ public class Pillar implements Comparable<Pillar> {
private Long Cost = Long.MAX_VALUE;
private final int ID;
private Disk disk = new Disk(0, Long.MAX_VALUE);
private HashMap<Pillar, Long> adj = new HashMap<>();
private final HashMap<Pillar, Long> adj = new HashMap<>();
public Pillar(int X, int Y, int ID){
......@@ -27,8 +27,12 @@ public class Pillar implements Comparable<Pillar> {
this.Cost = cost;
}
public void addAdj(Pillar pillar, Long cost){
this.adj.put(pillar, cost);
public void addAdj(Pillar pillar, Long weight){
if(this.adj.get(pillar) == null){
this.adj.put(pillar, weight);
} else if (this.adj.get(pillar) > weight){
this.adj.put(pillar, weight);
}
}
public HashMap<Pillar,Long> getAdj(){
return adj;
......
Markdown is supported
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