32e712b5cc904992c443ae2df0dff3247ba303b6
[IRC.git] / Robust / src / Benchmarks / mlp / directto / mlp-java / Algorithm.java
1 //import java.util.*;
2 //import java.io.*;
3
4 public class Algorithm {
5   public double initialTime,time;
6   public double currIteration;
7   public ConflictList cList;
8
9   public Algorithm() {
10     cList=new ConflictList();    
11   }
12   
13   public /*static*/ void setInitialTime(double time) {
14     initialTime=time;
15     currIteration=0;
16   }
17   
18   public /*static*/ boolean isConflict(D2 d2, Point4d p1, Point4d p2) {
19     Point2d pAux1=new Point2d(p1.x,p1.y);
20     Point2d pAux2=new Point2d(p2.x,p2.y);
21     if ( (Point2d.squaredDistance(pAux1,pAux2) <= 
22           Math.pow(d2.getStatic().radius(),2))
23          
24          && Math.abs(p1.z-p2.z) <= d2.getStatic().distance()
25          )
26       return true;
27
28     return false;
29   }
30
31     public /*static*/ Point4d findConflict(D2 d2, Flight a, Flight b) {
32     Point4d conflictPoint=new Point4d(Point4d.outOfRangeTime(),0,0,0);
33     if (a.flightID!=b.flightID) {
34       Vector p1=a.traject.p;
35       Vector p2=b.traject.p;
36       
37       int pos=0;
38       boolean found=false;
39       
40       while ( (pos<p1.size()) && (pos<p2.size()) && (!found) ) {
41         Point4d point1=(Point4d) p1.elementAt(pos);
42         Point4d point2=(Point4d) p2.elementAt(pos);
43         if (isConflict(d2, point1,point2)) {          
44           System.out.println(point1+" "+point2);
45           found=true;
46           conflictPoint=point1;
47         }
48         pos++;
49       }
50     }
51     return conflictPoint;
52   }
53     
54   public /*static*/ ConflictList getConflictsWith(D2 d2, double time, Flight flight) {
55     ConflictList conflicts=new ConflictList();
56
57     Vector flights=d2.getFlightList().f;
58     int n,i,j;
59     n=d2.getFlightList().noFlights;
60
61     d2.getTrajectorySynthesizer().updateTrajectory(d2, time, flight);
62     for (i=0; i<n; i++) {
63       Flight aAux=(Flight) flights.elementAt(i);
64       d2.getTrajectorySynthesizer().updateTrajectory(d2, time, aAux);
65     }
66
67     Flight aux1=flight;
68     for (i=0; i<n; i++) {
69       Flight aux2=(Flight) flights.elementAt(i);
70       Point4d conflictPoint=findConflict(d2, aux1,aux2);
71       if (!(conflictPoint.outOfRange())) {
72         conflicts.newConflict(conflictPoint,aux1,aux2);
73       }
74     }
75     return conflicts;
76   }
77
78   public /*static*/ void doIteration(D2 d2) {
79     time=initialTime+currIteration*d2.getStatic().iterationStep();
80     currIteration++;
81     System.out.println("In doIteration!");
82     System.out.println("Time:"+time);
83     
84     cList.clear();
85     
86     Vector flights=d2.getFlightList().f;
87     int n=d2.getFlightList().noFlights;
88     int i,j;
89
90     for (i=0;i<n;i++) { 
91       Flight aAux=(Flight) flights.elementAt(i);
92       d2.getTrajectorySynthesizer().updateTrajectory(d2, time,aAux);
93     }
94     
95     System.out.println("Does it get here? (after the trajectory update)");
96
97     for (i=0;i<n;i++)
98       for (j=i+1;j<n;j++) {
99         Flight aux1=(Flight) flights.elementAt(i);
100         Flight aux2=(Flight) flights.elementAt(j);
101         Point4d conflictPoint=findConflict(d2, aux1,aux2);
102         if (!(conflictPoint.outOfRange())) {
103           cList.newConflict(conflictPoint,aux1,aux2);
104         }
105       }
106   }
107 }