source: proiecte/ptvs/src/vnsim/applications/adaptiveTL/IntersectionPath.java @ 31

Last change on this file since 31 was 31, checked in by (none), 14 years ago
File size: 7.1 KB
Line 
1/************************************************************************************
2 * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University
3 * All rights reserved.
4 * Refer to LICENSE for terms and conditions of use.
5 ***********************************************************************************/
6package vnsim.applications.adaptiveTL;
7
8
9import java.lang.ref.PhantomReference;
10import java.util.LinkedList;
11import java.util.ListIterator;
12
13import vnsim.applications.trafficview.SimulatedCarInfo;
14import vnsim.core.*;
15import vnsim.map.object.*;
16import vnsim.vehicular.simulator.CarInstance;
17import vnsim.vehicular.simulator.intersections.*;
18
19
20
21
22public class IntersectionPath {
23
24        Intersection intersection;
25        DirectedRoadSegment roadIn;
26        DirectedRoadSegment roadout;
27       
28        public LinkedList<IntersectionPhaseStatistics> lastHourHistory = new LinkedList<IntersectionPhaseStatistics>();
29        public int analysisPeriod;
30       
31        public int demand, volume;
32        public double avgNoOfStops;
33       
34       
35        public IntersectionPath(Intersection intersection, DirectedRoadSegment roadIn, DirectedRoadSegment roadout){
36                this.intersection = intersection;
37                this.roadIn = roadIn;
38                this.roadout = roadout;
39               
40                lastHourHistory.add(new IntersectionPhaseStatistics(0, Globals.YELLOW));
41        }
42       
43        /**
44         * Constructor used for vector lookup
45         */
46        public IntersectionPath(DirectedRoadSegment roadIn, DirectedRoadSegment roadout){
47                this.roadIn = roadIn;
48                this.roadout = roadout;
49        }
50       
51        public boolean equals(Object arg0) {
52                IntersectionPath ip = (IntersectionPath)arg0;
53                return (this.roadIn.equals(ip.roadIn) && this.roadout.equals(ip.roadout));
54        }
55
56        public void changePhase(int crtTime, int color){
57                IntersectionPhaseStatistics ips = lastHourHistory.getLast();
58                ips.setPhaseEndFrame(crtTime - 1);
59                if (ips.getCarsNo() != 0){
60                        ips.avgGap = (double)(ips.phaseEndFrame - ips.phaseStartFrame) / (Globals.SECOND * ips.carsNo);
61                }
62                if (roadIn.segmentIndex == 2){
63                        int k = 0;
64                        k ++;
65                }
66                double avg = 0;
67                Point px = roadIn.road.points.get(roadIn.pointIndex);
68                for (int i = 1; i < roadIn.endOfQueue.length; i++){
69                        if (roadIn.endOfQueue[i].car != null){
70                                Point p = roadIn.road.points.get(roadIn.endOfQueue[i].getCar().getPointIdx());
71                                avg += Math.abs(p.getDistance() - px.getDistance());
72                        }
73                }
74                avg /= roadIn.endOfQueue.length - 1;
75                ips.setQueueSize((ips.getQueueSize() + avg) / 2);
76               
77                lastHourHistory.add((ips = new IntersectionPhaseStatistics(crtTime, color)));
78                ips.setQueueSize(avg);
79                IntersectionPhaseStatistics first = lastHourHistory.getFirst();
80                if (crtTime - first.getPhaseEndFrame() > Globals.executionFPS * 3600){
81                        lastHourHistory.removeFirst();
82                }
83        }
84       
85        public void demand(IntersectionCarRecord icr){
86                SimulatedCarInfo sc = icr.getCar();
87               
88                IntersectionPhaseStatistics ips = lastHourHistory.getLast();
89                ips.demandCarsNo ++;
90                if (sc.getSignal() == 1){
91                        ips.leftDemandCarsNo ++;
92                }
93        }
94       
95        public int computeDemand(int period){
96                demand = 0;
97                ListIterator<IntersectionPhaseStatistics> it = lastHourHistory.listIterator(lastHourHistory.size() - 1);
98                boolean skip = true;
99                analysisPeriod = 0;
100                while (it.hasPrevious()){
101                        IntersectionPhaseStatistics x = it.previous();
102                        if (skip){
103                                if (x.color == Globals.GREEN){
104                                        skip = false;
105                                }else{
106                                        continue;
107                                }
108                        }
109                       
110                        if (x.phaseEndFrame < Globals.engine.crtTime - period){
111                                break;
112                        }
113                        demand += x.demandCarsNo;
114                        analysisPeriod += x.phaseEndFrame - x.phaseStartFrame;
115                }
116                return demand;
117        }
118
119        public double computeLastCycleQueueSize(){
120                double sum = 0;
121                int count = 0;
122                ListIterator<IntersectionPhaseStatistics> it = lastHourHistory.listIterator(lastHourHistory.size() - 1);
123                boolean first = true;
124                int color = 0;
125                while (it.hasPrevious()){
126                        IntersectionPhaseStatistics x = it.previous();
127                        if (!first && x.getColor() == color)
128                                break;
129                        if (first){
130                                first = false;
131                                color = x.getColor();
132                        }
133                        sum += x.queueSize * (x.phaseEndFrame - x.phaseStartFrame);
134                        count = x.phaseEndFrame - x.phaseStartFrame;
135                }
136                if (count == 0)
137                        return 0;
138                return sum / count;
139        }
140
141        public int computeLeftDemand(int period){
142                int sum = 0;
143                ListIterator<IntersectionPhaseStatistics> it = lastHourHistory.listIterator(lastHourHistory.size() - 1);
144                while (it.hasPrevious()){
145                        IntersectionPhaseStatistics x = it.previous();
146                        if (x.phaseEndFrame < Globals.engine.crtTime - period)
147                                        break;
148                        sum += x.leftDemandCarsNo;
149                }
150                return sum;
151        }
152       
153        public int computeVolume(int period){
154                int sum = 0;
155                ListIterator<IntersectionPhaseStatistics> it = lastHourHistory.listIterator(lastHourHistory.size() - 1);
156                analysisPeriod = 0;
157                while (it.hasPrevious()){
158                        IntersectionPhaseStatistics x = it.previous();
159                        if (x.phaseEndFrame < Globals.engine.crtTime - period)
160                                break;
161                        sum += x.carsNo;
162                        analysisPeriod += x.phaseEndFrame - x.phaseStartFrame;
163                }
164                return sum;
165        }
166
167        public double computeAvgNoOfStops(int period){
168                int sum = 0;
169                double no = 0;
170                ListIterator<IntersectionPhaseStatistics> it = lastHourHistory.listIterator(lastHourHistory.size() - 1);
171                       
172                while (it.hasPrevious()){
173                        IntersectionPhaseStatistics x = it.previous();
174                        if (x.phaseEndFrame < Globals.engine.crtTime - period)
175                                break;
176                        sum += x.carsNo;
177                        no += x.carsNo * x.avgNoOfStops;
178                }
179                if (sum == 0)
180                        return 0;
181                else
182                        return (double)no/sum;
183        }
184       
185        public void recordCar(IntersectionCarRecord icr){
186                SimulatedCarInfo sc = icr.getCar();
187               
188                IntersectionPhaseStatistics ips = lastHourHistory.getLast();
189                int idx = lastHourHistory.size() - 1;
190               
191                if (sc.getTimestamp() < ips.phaseStartFrame ){
192                        idx -- ;
193                        ips = lastHourHistory.get(idx);
194                }
195               
196// compute number of cars
197                int reds = 0;
198                if (icr.getQueuedTime() == -1){
199                        reds = 0;
200                }else{
201                        if (ips.phaseStartFrame > icr.getQueuedTime()){
202                                ListIterator<IntersectionPhaseStatistics> it = lastHourHistory.listIterator(idx);
203                                while (it.hasPrevious()){
204                                        IntersectionPhaseStatistics x = it.previous();
205                                        if (x.phaseEndFrame < icr.getQueuedTime())
206                                                break;
207                                        if (x.getColor() == Globals.RED)
208                                                reds ++;
209                                }
210                        }
211                }
212               
213                //compute delay at the simulation layer
214                Road r = Globals.map.roads.get(roadout.roadIndex);
215                CarInstance ci = new CarInstance((short)roadout.roadIndex, (short)roadout.pointIndex, null);
216                ci.ID = icr.getCar().getVehicleId();
217                ci = r.carsOnThisRoad.get(ci, roadout.direction);
218                if (ci != null)
219                        ips.avgControlDelay = (int)((double)(ips.avgControlDelay * ips.carsNo + ci.controlDelay )/ (ips.carsNo + 1));
220               
221                ips.avgSpeed = (ips.avgSpeed * ips.carsNo + sc.getSpeed() )/ (ips.carsNo + 1) ;
222                       
223                ips.avgNoOfStops = (double)(ips.getAvgNoOfStops() * ips.carsNo + reds )/ (ips.carsNo + 1);
224               
225                ips.carsNo ++;
226        }
227       
228        public IntersectionPhaseStatistics getLastGreenPhase(){
229                ListIterator<IntersectionPhaseStatistics> it = lastHourHistory.listIterator(lastHourHistory.size() - 1);
230                while (it.hasPrevious()){
231                        IntersectionPhaseStatistics x = it.previous();
232                        if (x.phaseEndFrame != -1 && x.color == Globals.GREEN){
233                                return x;
234                        }
235                }
236                return null;
237        }
238       
239}
Note: See TracBrowser for help on using the repository browser.