-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElevator.java
More file actions
241 lines (200 loc) · 7.57 KB
/
Elevator.java
File metadata and controls
241 lines (200 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import java.util.Arrays;
// import java.io.*;
import java.util.*;
public class Elevator {
int id;
int currentFloor;
int personCapacity;
int minimumCapacity;
Person[] persons;
boolean isTripCompleted;
double interFloorTravelTime;
int[] floorTravelOrder;
int numberOfStops;
double remainTime;
double elapsedTime;
// Specifing allowed floors
int[] allowedFloors;
// Accelerate, Deaccelerate, const speed variables
double accelerate = 1;
double deaccelerate = 1;
double constSpeed = 2.5;
// Elevator Door Open, Close, wait etc Times
double doorOpenTime = 1.9;
double doorCloseTime = 2.8;
double startDelay = 0.5;
double passengerTransferTime = 1.2;
// floor details
double[] interFloorDistance;
Elevator(int id ) {
this.id = id;
}
Elevator(int id, int numberOfStops) {
this.id = id;
this.numberOfStops = numberOfStops;
}
Elevator(int id, int numberOfStops, Person[] persons) {
this.id = id;
this.numberOfStops = numberOfStops;
this.persons = persons;
}
Elevator(int cap, int NumberOfFloors, double[] interFloorDistance, int[] allowedFloors, int id) {
this.currentFloor = 0;
this.personCapacity = cap;
this.isTripCompleted = true;
this.elapsedTime = 0.0;
this.interFloorTravelTime = 10.0;
this.interFloorDistance = interFloorDistance;
this.minimumCapacity = 21;
this.allowedFloors = allowedFloors;
this.id = id;
this.numberOfStops = 0;
}
public void setID(int id){
this.id = id;
}
public void addPersons(Person[] persons) {
this.persons = persons;
// System.out.println("No of Persons " + persons.length);
}
public void showPersons() {
System.out.print(persons[0]);
}
public double run() {
elapsedTime = 0.0;
numberOfStops = 0;
this.floorTravelOrder = new int[this.personCapacity];
for(int i=0; i < floorTravelOrder.length ; i++){
floorTravelOrder[i]= 0;
}
// Get floors need to travel
int i = 0;
// System.out.println("floorTravelOrder :" + floorTravelOrder.length);
for (Person p : persons) {
// System.out.println("In Elevator "+ this.id + " - Person: " + p.id + " floor: " + p.floor + " arriveTime: " + p.arriveTime);
floorTravelOrder[i++] = p.floor;
}
// Sort array to bottom to upper floor
Arrays.sort(floorTravelOrder);
int l = removeDuplicateElements(floorTravelOrder, floorTravelOrder.length);
// removeDuplicates(floorTravelOrder);
System.out.println("In elevator :" + this.id + "No of P: " + persons.length);
int[] newArray = new int[l];
for (int j = 0; j < l; j++) {
newArray[j] = floorTravelOrder[j];
System.out.print(floorTravelOrder[j]);// + "No of P: " + persons.length);
}
floorTravelOrder = newArray;
for (int floor : floorTravelOrder) {
if(floor != 0)
numberOfStops++;
}
// System.out.println("Sorted floor list");
// for (int f : floorTravelOrder) {
// System.out.println("floor " + f);
// }
// System.out.println("NO of Floors: " + floorTravelOrder.length);
double tempTime = 0.0;
for (int j = 0; j < floorTravelOrder.length - 1; j++) {
// System.out.println("j: " + j + " floor: " + floorTravelOrder[j]);
if (j == 0) {
// First stop floor travel time
// System.out.println("inside travel floor j==0");
tempTime = calculateTravelTime(0, floorTravelOrder[j], persons);
elapsedTime += tempTime;
}
// Calculate personal journeyTime
calculatePersonWaitTime(persons, floorTravelOrder[j], elapsedTime);
// calculate number of floor to travel without stop
// int nextFloor = floorTravelOrder[j];
// System.out.println("current floor: " + j);
elapsedTime += calculateTravelTime(floorTravelOrder[j], floorTravelOrder[j + 1], persons);
if (j == floorTravelOrder.length - 2) {
// Calculate personal journeyTime
calculatePersonWaitTime(persons, floorTravelOrder[j + 1], elapsedTime);
// go to base floor
// System.out.println("inside final floor");
elapsedTime += calculateTravelTime(0, floorTravelOrder[j + 1], new Person[0]);
}
}
// Set Persons Finish the Journey
for (Person p : persons) {
p.isFinish = true;
}
return elapsedTime;
}
double calculateTravelTime(int currentFloor, int nextFloor, Person[] persons) {
double distance = 0.0;
double time = 0.0;
time += doorOpenTime + doorCloseTime + startDelay;
//Calculate Passenger Transfer Time
if(persons.length == 0){
if(currentFloor == 0)
time += passengerTransferTime*persons.length;
else{
int count = 0;
for (Person p : persons) {
if (p.floor == currentFloor)
count++;
}
time += passengerTransferTime*count;
}
}
if (currentFloor != nextFloor) {
// System.out.println("currentFloor: " + currentFloor + ", nextFloor: " + nextFloor);
for (int k = currentFloor; k < nextFloor; k++) {
distance += interFloorDistance[k];
}
// System.out.println("CF: " + currentFloor + " NF:" + nextFloor + " D: " + distance);
double accTime = this.constSpeed / this.accelerate;
double deaccTime = this.constSpeed / this.deaccelerate;
double accDistance = 0.5 * this.accelerate * accTime * accTime;
double deaccDistance = 0.5 * this.deaccelerate * deaccTime * deaccTime;
double constSpeedTime = (distance - (accDistance + deaccDistance)) / this.constSpeed;
time += accTime + constSpeedTime + deaccTime;
}
// System.out.println("distance: " + distance + ", time: " + time);
// System.out.println();
return time;
}
// Function to add wait time for persons
void calculatePersonWaitTime(Person[] persons, int currentFloor, double journeyTime) {
for (Person p : persons) {
if (p.floor == currentFloor)
p.journeyTime += journeyTime;
}
}
// To remove duplicate element in an array
public static int removeDuplicateElements(int arr[], int n) {
if (n == 0 || n == 1) {
return n;
}
int[] temp = new int[n];
int j = 0;
for (int i = 0; i < n - 1; i++) {
if (arr[i] != arr[i + 1]) {
temp[j++] = arr[i];
}
}
temp[j++] = arr[n - 1];
// Changing original array
for (int i = 0; i < j; i++) {
arr[i] = temp[i];
}
return j;
}
public static int[] removeDuplicates(int[] arr) {
HashSet<Integer> set = new HashSet<>();
final int len = arr.length;
// changed end to len
for (int i = 0; i < len; i++) {
set.add(arr[i]);
}
int[] whitelist = new int[set.size()];
int i = 0;
for (Iterator<Integer> it = set.iterator(); it.hasNext();) {
whitelist[i++] = it.next();
}
return whitelist;
}
}