-
Notifications
You must be signed in to change notification settings - Fork 0
/
transportation.ts
207 lines (175 loc) · 5.95 KB
/
transportation.ts
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
import { getDistance } from 'geolib';
import { ClusterResult, YearlyResult, YearlyTripResults } from 'models/types';
import OSRM from '@project-osrm/osrm';
import { performance } from 'perf_hooks';
const MILES_PER_GALLON = 6;
export const KM_TO_MILES = 0.621371;
export const FULL_TRUCK_PAYLOAD = 25; // FRCS assumption (in green tons)
export const TRUCK_OWNERSHIP_COST = 13.1; // $/h
export const getTransportationCostTotal = (
feedstockAmount: number,
distance: number,
duration: number,
dieselFuelPrice: number,
wageTruckDriver: number,
driverBenefits: number,
oilCost: number
) => {
let transportationCostFullPayloadPerGT = 0;
if (feedstockAmount >= FULL_TRUCK_PAYLOAD) {
transportationCostFullPayloadPerGT = getTransportationCostPerGT(
distance,
duration,
dieselFuelPrice,
FULL_TRUCK_PAYLOAD,
wageTruckDriver,
driverBenefits,
oilCost
);
}
let transportationCostPartialPayloadPerGT = 0;
const partialPayload = feedstockAmount % FULL_TRUCK_PAYLOAD;
if (partialPayload > 0) {
transportationCostPartialPayloadPerGT = getTransportationCostPerGT(
distance,
duration,
dieselFuelPrice,
partialPayload,
wageTruckDriver,
driverBenefits,
oilCost
);
}
const transportationCostTotal =
(feedstockAmount - partialPayload) * transportationCostFullPayloadPerGT +
partialPayload * transportationCostPartialPayloadPerGT;
return transportationCostTotal;
};
export const getTransportationCostPerGT = (
distance: number,
duration: number,
dieselFuelPrice: number,
payload: number,
wageTruckDriver: number,
driverBenefits: number,
oilCost: number
) => {
const miles = distance * KM_TO_MILES * 2; // 2* cause you have to drive back
const hours = duration * 2;
const labor = (1 + driverBenefits / 100) * wageTruckDriver * hours;
const fuel = (1 / MILES_PER_GALLON) * dieselFuelPrice * miles;
const oil = oilCost * miles;
const truckOwnership = TRUCK_OWNERSHIP_COST * hours;
let cost = oil + fuel + labor + truckOwnership;
cost = cost / payload;
return cost;
};
export const getMoveInTrip = (
osrm: OSRM,
facilityLat: number,
facilityLng: number,
clusters: ClusterResult[]
): Promise<YearlyTripResults> => {
const clusterCoordinates = clusters.map((cluster) => [cluster.center_lng, cluster.center_lat]);
const options: OSRM.TripOptions = {
roundtrip: true,
generate_hints: false,
geometries: 'geojson',
// overview: 'false',
source: 'first',
coordinates: [
[facilityLng, facilityLat], // start at facility
...clusterCoordinates,
],
};
return new Promise((resolve, reject) => {
if (clusters.length === 0) {
resolve({ distance: 0, trips: [] });
}
osrm.trip(options, async (err, result) => {
if (err) {
console.log('rejecting move in...');
console.log(err);
reject(err);
}
const osrmDistance = result.trips.reduce((dist, trip) => dist + trip.distance, 0);
// const osrmDuration = result.trips.reduce((dur, trip) => dur + trip.duration, 0);
console.log('resolving move in...');
const results: YearlyTripResults = {
trips: result.trips,
distance: osrmDistance, // in meters
};
resolve(results);
});
});
};
export const calculateMoveInDistance = async (
osrm: OSRM,
results: YearlyResult,
facilityLat: number,
facilityLng: number
) => {
let totalMoveInDistance = 0;
const maxClustersPerChunk = 2000;
if (results.clusters.length > maxClustersPerChunk) {
// want enough chunks so that we don't exceed max clusters per chunk
const numChunks = Math.ceil(results.clusters.length / maxClustersPerChunk);
console.log(
`${results.clusters.length} is too many clusters, breaking into ${numChunks} chunks`
);
// assuming facility coordinates are biomass coordinates
const sortedClusters = results.clusters.sort(
(a, b) =>
getDistance(
{ latitude: facilityLat, longitude: facilityLng },
{ latitude: a.center_lat, longitude: a.center_lng }
) -
getDistance(
{ latitude: facilityLat, longitude: facilityLng },
{ latitude: b.center_lat, longitude: b.center_lng }
)
);
// break up into numChunks chunks by taking clusters in order
const groupedClusters = sortedClusters.reduce((resultArray, item, index) => {
const chunkIndex = Math.floor(index / maxClustersPerChunk);
if (!resultArray[chunkIndex]) {
resultArray[chunkIndex] = []; // start a new chunk
}
resultArray[chunkIndex].push(item);
return resultArray;
// tslint:disable-next-line:align
}, [] as ClusterResult[][]);
// for each chunk, calculate the move in distance and add them up
for (let i = 0; i < groupedClusters.length; i++) {
const clustersInGroup = groupedClusters[i];
console.log(
`calculating move in distance on ${clustersInGroup.length} clusters in chunk ${i + 1}...`
);
const t0_chunk = performance.now();
const chunkedMoveInTripResults = await getMoveInTrip(
osrm,
facilityLat,
facilityLng,
clustersInGroup
);
const t1_chunk = performance.now();
console.log(
`Running took ${t1_chunk - t0_chunk} milliseconds, move in distance: ${
chunkedMoveInTripResults.distance
}.`
);
totalMoveInDistance += chunkedMoveInTripResults.distance;
}
} else {
// not that many clusters, so don't bother chunking
console.log(`calculating move in distance on ${results.clusters.length} clusters...`);
const t0 = performance.now();
const moveInTripResults = await getMoveInTrip(osrm, facilityLat, facilityLng, results.clusters);
const t1 = performance.now();
console.log(
`Running took ${t1 - t0} milliseconds, move in distance: ${moveInTripResults.distance}.`
);
totalMoveInDistance = moveInTripResults.distance;
}
return totalMoveInDistance;
};