Skip to content

Commit d51a1d8

Browse files
committed
odTrip simulation: fixes after rebase on latest main
batch csv demand type has changed for the batch calculation job. We create a similar demande file for the OdTripSimulationMethod type. For sake of simplicity, for now, we keep the whole CsvFileAndMapper type instead of just the field mapping, like in the batch calculation, as it would require a different backend and frontend type with the options descriptor. When we clean the option descriptor classes, we can make sure the case is supported and the options descriptor type apply only to the options configuration and allow for subsequent types in the backend.
1 parent bd98263 commit d51a1d8

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

packages/transition-backend/src/services/networkDesign/transitNetworkDesign/TransitNetworkJobController.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ const createAndEnqueueEvolutionaryTransitNetworkDesignJob = async (
3434
// TODO Handle node weight file when supported
3535
// TODO Handle accessibility map simulation when supported
3636

37+
// FIXME For OdTripSimulation, we need to ensure the demand file is properly
38+
// set in the job parameters and go from
39+
// OdTripSimulationDemandFromCsvAttributes in the UI/frontend to simply
40+
// OdTripSimulationFromCsvAttributes in the backend, but we need different
41+
// types for the method configuration in backend and frontend to do so
42+
// correctly.
3743
const job: EvolutionaryTransitNetworkDesignJob = await ExecutableJob.createJob({
3844
user_id: userId,
3945
name: 'evolutionaryTransitNetworkDesign',
4046
data: {
4147
parameters: jobParameters
4248
},
43-
inputFiles,
44-
hasOutputFiles: true
49+
inputFiles
4550
});
4651
await job.enqueue(eventEmitter);
4752
await job.refresh();

packages/transition-backend/src/services/simulation/methods/OdTripSimulation.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { unparse } from 'papaparse';
1010
import Preferences from 'chaire-lib-common/lib/config/Preferences';
1111
import { SimulationMethodFactory, SimulationMethod } from './SimulationMethod';
1212
import {
13+
OdTripSimulationDemandFromCsvAttributes,
1314
OdTripSimulationDescriptor,
1415
OdTripSimulationOptions
1516
} from 'transition-common/lib/services/networkDesign/transit/simulationMethod/OdTripSimulationMethod';
@@ -24,6 +25,7 @@ import { BatchRouteJobType } from '../../transitRouting/BatchRoutingJob';
2425
import { BatchCalculationParameters } from 'transition-common/lib/services/batchCalculation/types';
2526
import { EventEmitter } from 'events';
2627
import { ReadStream, WriteStream } from 'fs';
28+
import { TransitDemandFromCsvRoutingAttributes } from 'transition-common/lib/services/transitDemand/types';
2729

2830
export const OdTripSimulationTitle = 'OdTripSimulation';
2931
const timeCsvColumnHeader = 'time';
@@ -239,17 +241,15 @@ export default class OdTripSimulation implements SimulationMethod {
239241
});
240242
}
241243

242-
private getBatchRouteDemandAttributes() {
244+
private getBatchRouteDemandAttributes(): TransitDemandFromCsvRoutingAttributes {
243245
// Clone the demand attributes to not modify the original
244-
const demandAttributes = _cloneDeep(this.options.demandAttributes);
246+
const demandAttributes = _cloneDeep(this.options.demandAttributes) as OdTripSimulationDemandFromCsvAttributes;
247+
const demandFieldMapping = demandAttributes.fileAndMapping.fieldMappings as TransitDemandFromCsvRoutingAttributes;
245248
// Add the time, time type and format to the demand attributes, as they are not defined or even required in the main job
246-
demandAttributes.fileAndMapping.fieldMappings.time = timeCsvColumnHeader;
247-
demandAttributes.fileAndMapping.fieldMappings.timeFormat = 'secondsSinceMidnight';
248-
demandAttributes.fileAndMapping.fieldMappings.timeType = 'departure';
249-
if (!demandAttributes.csvFields.includes(timeCsvColumnHeader)){
250-
demandAttributes.csvFields.push(timeCsvColumnHeader);
251-
}
252-
return demandAttributes;
249+
demandFieldMapping.time = timeCsvColumnHeader;
250+
demandFieldMapping.timeFormat = 'secondsSinceMidnight';
251+
demandFieldMapping.timeType = 'departure';
252+
return demandFieldMapping;
253253
}
254254

255255
async simulate(
@@ -283,8 +283,7 @@ export default class OdTripSimulation implements SimulationMethod {
283283
resources: {
284284
// Input file will be prepared later
285285
files: { input: `sampled_transit_demand_${scenarioId}.csv` }
286-
},
287-
hasOutputFiles: false //TODO Manage the result
286+
}
288287
});
289288

290289
// Create the input file for the batch routing job as a random sample of the original demand file (from the currently running job)

packages/transition-common/src/services/networkDesign/transit/simulationMethod/OdTripSimulationMethod.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,22 @@ type OdTripEvaluationOptions = {
3535
fitnessFunction: string;
3636
};
3737

38+
export type OdTripSimulationFromCsvAttributes = {
39+
projection: string;
40+
id: string;
41+
originLat: string;
42+
originLon: string;
43+
destinationLat: string;
44+
destinationLon: string;
45+
expansionFactor?: string;
46+
};
47+
48+
export type OdTripSimulationDemandFromCsvAttributes = CsvFileAndMapping<OdTripSimulationFromCsvAttributes>;
49+
3850
// Define OD trip simulation options
3951
export type OdTripSimulationOptions = {
4052
// Describe where to get the OD trip data for the simulation
41-
demandAttributes: CsvFileAndMapping;
53+
demandAttributes: OdTripSimulationDemandFromCsvAttributes;
4254
// Transit routing parameters to use for the simulation
4355
transitRoutingAttributes: TransitRoutingBaseAttributes;
4456
evaluationOptions: OdTripEvaluationOptions;
@@ -183,6 +195,15 @@ const odDemandFieldDescriptors: CsvFieldMappingDescriptor[] = [
183195
}
184196
];
185197

198+
/**
199+
* Describe a CSV file field mapping for a transition origin/destination pair file
200+
*/
201+
export class TransitOdTripSimulationDemandFromCsv extends CsvFileAndFieldMapper<OdTripSimulationFromCsvAttributes> {
202+
constructor(csvFileAndMapping?: OdTripSimulationDemandFromCsvAttributes | undefined) {
203+
super(odDemandFieldDescriptors, csvFileAndMapping);
204+
}
205+
}
206+
186207
const transitRoutingAttributesDescriptor = new TransitRoutingAttributesDescriptor();
187208
const simulationOptionsDescriptor = new SimulationOptionsDescriptor();
188209

@@ -224,9 +245,8 @@ export class OdTripSimulationDescriptor implements SimulationAlgorithmDescriptor
224245

225246
// Validate the demand attributes
226247
if (options.demandAttributes !== undefined) {
227-
const demandFieldMappers = new CsvFileAndFieldMapper(
228-
odDemandFieldDescriptors,
229-
options.demandAttributes as CsvFileAndMapping
248+
const demandFieldMappers = new TransitOdTripSimulationDemandFromCsv(
249+
options.demandAttributes as OdTripSimulationDemandFromCsvAttributes
230250
);
231251
if (!demandFieldMappers.isValid()) {
232252
valid = false;

0 commit comments

Comments
 (0)