-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVehicle.cpp
115 lines (102 loc) · 4.59 KB
/
Vehicle.cpp
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
#include <iostream>
#include "./Vehicle.h"
using std::cout;
using std::endl;
Vehicle::Vehicle(shared_ptr<ModelData> modelData) :
modelState(modelData),
operationState(en_route),
currentOperationTime(0.0),
totalTimeEnRoute(0.0),
totalTimeCharging(0.0),
totalTimeWaiting(0.0)
{
};
Vehicle::~Vehicle() {
// Nothing to do
};
// Implementation of ICharging interface
void Vehicle::setCharging() {
operationState = OperationState::charging;
currentOperationTime = 0;
}
bool Vehicle::isCharging () const {
return operationState == OperationState::charging;
}
bool Vehicle::isWaitingForQueue() const {
return operationState == OperationState::waiting_for_charge_queue;
}
void Vehicle::setInWaitQueue() {
operationState = OperationState::in_charge_queue;
}
OperationState Vehicle::getOpState() const {
return operationState;
}
// Iterate by delta-time in seconds (simulated minutes)
void Vehicle::iterate(double deltaT) {
double newOperationTime = currentOperationTime + deltaT;
switch (operationState) {
case en_route: {
if (newOperationTime >= modelState->params.endurance) {
operationState = OperationState::waiting_for_charge_queue; //This is an intermediate state
// Reset operation time - there may be an overrun
currentOperationTime = 0;
if (newOperationTime > modelState->params.endurance) {
currentOperationTime = newOperationTime - modelState->params.endurance;
}
} else {
currentOperationTime = newOperationTime;
totalTimeEnRoute += deltaT; // Total for vehicle (sim minutes)
modelState->totalFlightTime += deltaT; // Total for model (sim minutes)
}
break;
};
case waiting_for_charge_queue: {
// This state should never be set at the beginning of an iteration as the
// the vehicle should be moved to the wait queue before the iteration is over.
assert(true);
break;
};
case in_charge_queue: {
// This could be a bit off (one deltaT) because the following charging station iterate()
// might start charging this vehicle
totalTimeWaiting += deltaT; // Total for vehicle (sim minutes)
modelState->totalWaitingTime += deltaT; // Total for model (sim minutes)
break;
};
case charging: {
if (newOperationTime >= modelState->params.timeToCharge) {
operationState = OperationState::en_route;
// Reset operation time - there may be an overrun
currentOperationTime = 0;
if (newOperationTime > modelState->params.timeToCharge) {
currentOperationTime = newOperationTime - modelState->params.timeToCharge;
}
} else {
currentOperationTime = newOperationTime;
totalTimeCharging += deltaT; // Total for vehicle (sim minutes)
modelState->totalChargingTime += deltaT; // Total for model (sim minutes)
}
break;
};
};
}
void Vehicle::printResult() const {
cout << "Result: " << endl;
cout << " Model : " << modelState->params.label << endl;
cout << " Endurance : " << modelState->params.endurance << endl;
cout << " Total Time In Flight (mins): " << totalTimeEnRoute << endl;
cout << " Total Time Charging (mins) : " << totalTimeCharging << endl;
cout << " Total Time Waiting (mins) : " << totalTimeWaiting << endl;
cout << " Total Passenger Miles : " << (totalTimeEnRoute / 60.0) * modelState->params.cruiseSpeed * modelState->params.passengerCount << endl;
}
void ModelData::prinResult() {
double totalPassengerMiles = params.cruiseSpeed * (totalFlightTime/60.0) *params.passengerCount;
cout << params.label << " Results: " << endl;
cout << " Number of Vehicle : " << fleetCount << endl;
cout << " Average Time in Flight (mins): " << totalFlightTime/fleetCount << endl;
cout << " Average Time Waiting (mins) : " << totalWaitingTime/fleetCount << endl;
cout << " Average Time Charging (mins) : " << totalChargingTime/fleetCount << endl;
cout << " Max number of faults : " << params.MaxFaultsPerHour * (totalFlightTime/60.0) << endl;
cout << " Total Passenger Miles : " << totalPassengerMiles << endl;
cout << " Average Passenger Miles : " << totalPassengerMiles/fleetCount << endl;
}