Commit 1e6bf50f authored by larsesselink's avatar larsesselink
Browse files

-backup

parent c1179685
......@@ -15,12 +15,12 @@ C_i = Cost of the i'th disk
public class Algorithm {
private final String fileName = "D:\\Documenten\\Google Drive\\Computing science\\Year 2\\Semester 1\\Algorithms and data structures (NWI-IBC027)\\project 1\\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<>();
private final Vector<Edge> edges = new Vector<>();
private final HashSet<Edge> edges = new HashSet<>();
private int N;
private int M;
private int W;
......@@ -29,6 +29,7 @@ public class Algorithm {
private PriorityQueue<Pillar> queue = new PriorityQueue<>();
private HashSet<Pillar> visited = new HashSet<>();
private HashMap<Pillar, Long> distance = new HashMap<Pillar,Long>();
private Long maxDistance;
......@@ -42,8 +43,6 @@ public class Algorithm {
algorithm();
for(Pillar pillar: distance.keySet()){
......@@ -58,24 +57,21 @@ public class Algorithm {
while(!queue.isEmpty()){
Pillar pillar = queue.remove();
System.out.println("from pillar " + pillar.getID() + " with cost: " + pillar.getCost());
visited.add(pillar);
for(Map.Entry<Pillar,Long> entry: pillar.getAdj().entrySet()){
Pillar pillar1 = queue.remove();
visited.add(pillar1);
Long cost = pillar1.getCost();
System.out.println("working on: " + entry.getKey().getID());
for(Map.Entry<Pillar,Long> entry: pillar1.getAdj().entrySet()){
Pillar pillar2 = entry.getKey();
Long edgeWeight = entry.getValue();
System.out.println(entry.getKey().getCost() + " >= " + pillar.getCost() + " + " + entry.getValue());
if(entry.getKey().getCost() > pillar.getCost() + entry.getValue()){
long cost = (pillar.getCost() + entry.getValue());
System.out.println("Pillar "+ entry.getKey().getID() + " gets cost: " + cost);
distance.put(entry.getKey(),cost);
entry.getKey().setCost((pillar.getCost() + entry.getValue()));
if((pillar2.getCost() > cost + edgeWeight)){
distance.put(pillar1,cost+edgeWeight);
pillar2.setCost(cost + edgeWeight);
}
queue.add(entry.getKey());
queue.add(pillar2);
}
......@@ -87,47 +83,57 @@ public class Algorithm {
Pillar pillar1 = pillars.get(k);
for(int j = k; j < pillars.size(); j++){
Pillar pillar2 = pillars.get(j);
for(int i = 0; i < disks.size(); i++){
if(itselfCheck(pillar1,pillar2) && distanceCheck(pillar1,pillar2,disks.get(i))){
// edges.add(new Edge(pillar1,pillar2,disks.get(i).getCost()));
pillar1.addAdj(pillar2, disks.get(i).getCost());
i = disks.size();
}
if(itselfCheck(pillar1,pillar2) && distanceCheck(pillar1,pillar2)){
pillar1.addAdj(pillar2, (long) distancePoints(pillar1.getX(), pillar1.getY(), pillar2.getX(), pillar2.getY()));
}
}
}
}
public boolean itselfCheck(Pillar pillar1, Pillar pillar2){
return distancePoints(pillar1.getX(),pillar1.getY(),pillar2.getX(),pillar2.getY()) != 0;
}
public boolean distanceCheck(Pillar pillar1, Pillar pillar2, Disk disk){
public boolean distanceCheck(Pillar pillar1, Pillar pillar2){
if(pillar1.getID() != 0 && pillar2.getID() != 0 && pillar1.getID() != -1 && pillar2.getID() != -1){
return distancePoints(pillar1.getX(), pillar1.getY(), pillar2.getX(), pillar2.getY()) <= (disk.getRadius() * 2);
return distancePoints(pillar1.getX(), pillar1.getY(), pillar2.getX(), pillar2.getY()) <= maxDistance;
} else {
return distancePoints(pillar1.getX(), pillar1.getY(), pillar2.getX(), pillar2.getY()) <= disk.getRadius();
return distancePoints(pillar1.getX(), pillar1.getY(), pillar2.getX(), pillar2.getY()) <= maxDistance;
}
}
public void initiateVector(){
readFromFile();
public double distancePoints(double x1, double y1, double x2, double y2) {
return (long) Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
}
public Long distancePointToLine(Pillar pillar1, int line){
return 0L;
}
// printVector(input);
public void initiateVector(){
readFromFile();
N = input.get(0).get(0);
M = input.get(0).get(1);
W = input.get(0).get(2);
target = new Pillar(0,W, -1);
System.out.println("N: " + N + " M: " + M + " W: " + W + "\n");
Disk tempDisk = null;
for(int i = N + 1; i <= N + M; i++){
tempDisk = new Disk(input.get(i).get(0),input.get(i).get(1));
disks.add(tempDisk);
}
maxDistance = disks.get(disks.size()-1).getRadius()*2;
pillars.add(src);
Pillar tempPillar = null;
......@@ -156,13 +162,7 @@ public class Algorithm {
}
public double distancePoints(
double x1,
double y1,
double x2,
double y2) {
return Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
}
public void addToVector(String line){
String[] split = line.split("\\s+");
......
//import java.io.File;
//import java.io.FileNotFoundException;
//import java.util.*;
//
//
///*
//X_k = x coord for k'th pillar
//Y_k = y coord for k'th pillar
//R_i = Radius of the i'th disk
//C_i = Cost of the i'th disk
//
// */
//
//
//public class Algorithm1 {
//
// 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<>();
// private final HashSet<Edge> edges = new HashSet<>();
// private int N;
// private int M;
// private int W;
// private final Pillar src = new Pillar(0,0, 0);
// private Pillar target;
// private PriorityQueue<Pillar> queue = new PriorityQueue<>();
// private HashSet<Pillar> visited = new HashSet<>();
// private HashMap<Pillar, Long> distance = new HashMap<Pillar,Long>();
//
//
//
//
// public void main(){
// src.setCost(0L);
//
// initiateVector();
// makeEdges();
//
//
//
//
// algorithm();
//
// for(Pillar pillar: distance.keySet()){
// System.out.println(pillar.getID() + " : " + pillar.getCost());
// }
// }
//
// public void algorithm(){
// distance.put(src,0L);
// queue.add(src);
//
//
//
// while(!queue.isEmpty()){
// Pillar pillar = queue.remove();
// visited.add(pillar);
// Long cost = pillar.getCost();
//
// for(Map.Entry<Pair,Pair> entry: pillar.getAdj().entrySet()){
// Pillar tempPillar = (Pillar) entry.getKey().getKey();
// Long tempCost = tempPillar.getCost();
// Long edgeWeight = (Long) entry.getKey().getValue();
// Disk disk1 = (Disk) entry.getValue().getKey();
// Disk disk2 = (Disk) entry.getValue().getValue();
//
//
// if((tempCost > cost + edgeWeight)){
// distance.put(pillar,cost+edgeWeight);
// tempPillar.setCost(disk1.getCost() /*+ pillar.getCost()*/);
//
// }
// queue.add(tempPillar);
//
// }
//
// }
// }
//
// 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);
// diskCheck(pillar1,pillar2);
// }
// }
// }
//
// public void diskCheck(Pillar pillar1, Pillar pillar2){
// Long lowestCost = Long.MAX_VALUE;
// Disk disk1 = null;
// Disk disk2 = null;
//
// for(int i = 0; i < disks.size(); i++){
// for(int j = i; j < disks.size(); j++){
// if(itselfCheck(pillar1,pillar2) && distanceCheck(pillar1,pillar2,disks.get(i),disks.get(j))){
// System.out.println("Checking for: " + pillar1.getID() + " and " + pillar2.getID()+ " : " + lowestCost + " > " + (disks.get(i).getCost()+disks.get(j).getCost()));
// if(pillar1.getID() != 0 && pillar2.getID() != 0 && pillar1.getID() != -1 && pillar2.getID() != -1 && (lowestCost > (disks.get(i).getCost()+disks.get(j).getCost()))){
// lowestCost = (disks.get(i).getCost()+disks.get(j).getCost());
// System.out.println("Lowestcost: " + lowestCost);
// disk1 = disks.get(i);
// disk2 = disks.get(j);
// } else if(lowestCost > disks.get(i).getCost() && ((pillar1.getID() == -1 || pillar2.getID() == -1) || (pillar1.getID() == 0 || pillar2.getID() == 0))){
// lowestCost = disks.get(i).getCost();
// System.out.println("lowestcost: " + lowestCost);
// disk1 = disks.get(i);
// }
// }
// }
//
// }
//
// if (lowestCost != Long.MAX_VALUE){
// pillar1.addAdj(pillar2, lowestCost,disk1,disk2);
// }
//
// }
//
// public boolean itselfCheck(Pillar pillar1, Pillar pillar2){
// return distancePoints(pillar1.getX(),pillar1.getY(),pillar2.getX(),pillar2.getY()) != 0;
// }
//
// public boolean distanceCheck(Pillar pillar1, Pillar pillar2, Disk disk1, Disk disk2){
// if(pillar1.getID() != 0 && pillar2.getID() != 0 && pillar1.getID() != -1 && pillar2.getID() != -1){
// return distancePoints(pillar1.getX(), pillar1.getY(), pillar2.getX(), pillar2.getY()) <= (disk1.getRadius() + disk2.getRadius());
// } else {
// return distancePoints(pillar1.getX(), pillar1.getY(), pillar2.getX(), pillar2.getY()) <= disk1.getRadius();
// }
// }
//
//
//
//
//
//
// public void initiateVector(){
// readFromFile();
//
// N = input.get(0).get(0);
// M = input.get(0).get(1);
// W = input.get(0).get(2);
// target = new Pillar(0,W, -1);
//
// Disk tempDisk = null;
// for(int i = N + 1; i <= N + M; i++){
// tempDisk = new Disk(input.get(i).get(0),input.get(i).get(1));
// disks.add(tempDisk);
// }
//
// pillars.add(src);
// Pillar tempPillar = null;
// for(int i = 1; i < N + 1; i++){
// tempPillar = new Pillar(input.get(i).get(0),input.get(i).get(1),i);
// pillars.add(tempPillar);
// }
// pillars.add(target);
//
// }
//
// public void readFromFile(){
// File file = new File(fileName);
// Scanner sc;
// try {
// sc = new Scanner(file);
//
// while(sc.hasNextLine()){
//// System.out.print(sc.nextLine() + "\n");
// addToVector(sc.nextLine());
// }
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
//
//
// }
//
// public double distancePoints(
// double x1,
// double y1,
// double x2,
// double y2) {
// return Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
// }
//
// public void addToVector(String line){
// String[] split = line.split("\\s+");
// Vector<Integer> temp = new Vector<Integer>();
// for(String strings : split){
// temp.add(Integer.parseInt(strings));
// }
//
// input.add(temp);
//
// }
//
//// public void printVector(Vector<Vector<Integer>> temp){
//// for (Vector<Integer> tempVector : temp) {
//// for (Integer integer : tempVector) {
//// System.out.println(integer);
//// }
//// }
//// }
//
// public void printVector(Vector<Pillar> temp){
// for (Pillar tempPillars : temp) {
// System.out.println(tempPillars.getX() + " " + tempPillars.getY());
// }
// }
//
//
//}
import java.util.Comparator;
public class Edge implements Comparator<Edge> {
public class Edge{
private final Pillar pillar1;
private final Pillar pillar2;
private final long cost;
......@@ -29,16 +29,6 @@ public class Edge implements Comparator<Edge> {
return Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
}
@Override
public int compare(Edge o1, Edge o2) {
// if(distancePoints( )){
//
// } else if(){
//
// }
return 0;
}
@Override
public String toString(){
return ("(" + "(" + pillar1.getX() + "," + pillar1.getY() + ")" + "," + "(" + pillar2.getX() +"," + pillar2.getY() + ")" + ")");
......
public class Pair<X, Y> {
private final X key;
private final Y value;
public Pair(X key, Y value){
this.key = key;
this.value = value;
}
public X getKey() {
return key;
}
public Y getValue() {
return value;
}
}
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.*;
public class Pillar implements Comparable<Pillar> {
private final int X;
private final int Y;
private Long Cost = Long.MAX_VALUE;
private final int ID;
private HashMap<Pillar,Long> adj = new HashMap<Pillar,Long>();
private Disk disk;
private HashMap<Pillar, Long> adj = new HashMap<>();
public Pillar(int X, int Y, int ID){
......@@ -29,8 +27,8 @@ public class Pillar implements Comparable<Pillar> {
this.Cost = cost;
}
public void addAdj(Pillar adj, Long cost){
this.adj.put(adj,cost);
public void addAdj(Pillar pillar, Long cost){
this.adj.put(pillar, cost);
}
public HashMap<Pillar,Long> getAdj(){
return adj;
......@@ -44,17 +42,14 @@ public class Pillar implements Comparable<Pillar> {
return ID;
}
// @Override
// public int compare(Pillar pillar1, Pillar pillar2){
//
// if (pillar1.getCost() < pillar2.getCost())
// return -1;
//
// if (pillar1.getCost() > pillar2.getCost())
// return 1;
//
// return 0;
// }
public Disk getDisk(){
return disk;
}
public void setDisk(Disk disk){
this.disk = disk;
}
@Override
public int compareTo(Pillar pillar) {
......
import java.util.HashMap;
public class Pillar1 implements Comparable<Pillar1> {
private final int X;
private final int Y;
private Long Cost = Long.MAX_VALUE;
private final int ID;
private HashMap<Pair, Pair> adj = new HashMap<>();
public Pillar1(int X, int Y, int ID){
this.X = X;
this.Y = Y;
this.ID = ID;
}
public int getY(){
return Y;
}
public int getX(){
return X;
}
public void setCost(Long cost){
this.Cost = cost;
}
public void addAdj(Pillar1 pillar, Long cost, Disk disk1, Disk disk2){
Pair<Pillar1,Long> pair1 = new Pair<>(pillar,cost);
Pair<Disk,Disk> pair2 = new Pair<>(disk1,disk2);
this.adj.put(pair1, pair2);
}
public HashMap<Pair,Pair> getAdj(){
return adj;
}
public Long getCost(){
return Cost;
}
public int getID(){
return ID;
}
@Override
public int compareTo(Pillar1 pillar) {
return Long.compare(this.getCost(), pillar.getCost());
}
}
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