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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#[cfg(test)]
#[path = "../../../tests/unit/format/problem/model_test.rs"]
mod model_test;

extern crate serde_json;

use crate::format::{FormatError, Location};
use serde::{Deserialize, Serialize};
use std::io::{BufReader, BufWriter, Error, Read, Write};

// region Plan

/// Relation type.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum RelationType {
    /// Relation type which locks jobs to specific vehicle in any order.
    Any,
    /// Relation type which locks jobs in specific order allowing insertion of other jobs in between.
    Sequence,
    /// Relation type which locks jobs in strict order, no insertions in between are allowed.
    Strict,
}

/// Relation is the way to lock specific jobs to specific vehicles.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Relation {
    /// Relation type.
    #[serde(rename(deserialize = "type", serialize = "type"))]
    pub type_field: RelationType,
    /// List of job ids.
    pub jobs: Vec<String>,
    /// Vehicle id.
    pub vehicle_id: String,
    /// Vehicle shift index.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shift_index: Option<usize>,
}

/// A job skills limitation for a vehicle.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct JobSkills {
    /// Vehicle should have all of these skills defined.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub all_of: Option<Vec<String>>,
    /// Vehicle should have at least one of these skills defined.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub one_of: Option<Vec<String>>,
    /// Vehicle should have none of these skills defined.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub none_of: Option<Vec<String>>,
}

/// Specifies a place for sub job.
#[derive(Clone, Deserialize, Debug, Serialize)]
pub struct JobPlace {
    /// A job place location.
    pub location: Location,
    /// A job place duration (service time).
    pub duration: f64,
    /// A list of job place time windows with time specified in RFC3339 format.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub times: Option<Vec<Vec<String>>>,
    /// A tag which will be propagated back within corresponding activity in solution.
    /// You can use it to identify used place in solution.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
}

/// Specifies a job task.
#[derive(Clone, Deserialize, Debug, Serialize)]
pub struct JobTask {
    /// A list of possible places where given task can be performed.
    pub places: Vec<JobPlace>,
    /// Job place demand.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub demand: Option<Vec<i32>>,
    /// An order, bigger value - later assignment in the route.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order: Option<i32>,
}

/// A customer job model. Actual tasks of the job specified by list of pickups and deliveries
/// which follows these rules:
/// * all of them should be completed or none of them.
/// * all pickups must be completed before any of deliveries.
#[derive(Clone, Deserialize, Debug, Serialize)]
pub struct Job {
    /// A job id.
    pub id: String,

    /// A list of pickup tasks.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pickups: Option<Vec<JobTask>>,

    /// A list of delivery tasks.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deliveries: Option<Vec<JobTask>>,

    /// A list of replacement tasks.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub replacements: Option<Vec<JobTask>>,

    /// A list of service tasks.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub services: Option<Vec<JobTask>>,

    /// A job skills limitations for serving a job.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub skills: Option<JobSkills>,

    /// Job value, bigger value - more chances for assignment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<f64>,

    /// Job group: jobs of the same group are assigned to the same tour or unassigned.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub group: Option<String>,

    /// A compatibility group: jobs with different compatibility cannot be assigned to the same tour.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub compatibility: Option<String>,
}

// region Clustering

/// Specifies clustering algorithm.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(tag = "type")]
pub enum Clustering {
    /// Vicinity clustering.
    #[serde(rename(deserialize = "vicinity", serialize = "vicinity"))]
    Vicinity {
        /// Specifies a vehicle profile used to calculate commute duration and distance between
        /// activities in the single stop.
        profile: VehicleProfile,
        /// Specifies threshold information.
        threshold: VicinityThresholdPolicy,
        /// Specifies visiting policy.
        visiting: VicinityVisitPolicy,
        /// Specifies service time policy.
        serving: VicinityServingPolicy,
        /// Specifies filtering policy.
        filtering: Option<VicinityFilteringPolicy>,
    },
}

/// Defines a various thresholds to control cluster size.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VicinityThresholdPolicy {
    /// Moving duration limit.
    pub duration: f64,
    /// Moving distance limit.
    pub distance: f64,
    /// Minimum shared time for jobs (non-inclusive).
    pub min_shared_time: Option<f64>,
    /// The smallest time window of the cluster after service time shrinking.
    pub smallest_time_window: Option<f64>,
    /// The maximum amount of jobs per cluster.
    pub max_jobs_per_cluster: Option<usize>,
}

/// Specifies cluster visiting policy.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum VicinityVisitPolicy {
    /// It is required to return to the first job's location (cluster center) before visiting a next job.
    Return,
    /// Clustered jobs are visited one by one from the cluster center finishing in the end at the
    /// first job's location.
    Continue,
}

/// Specifies service time policy.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(tag = "type")]
pub enum VicinityServingPolicy {
    /// Keep original service time.
    #[serde(rename(deserialize = "original", serialize = "original"))]
    Original {
        /// Parking time.
        parking: f64,
    },
    /// Correct service time by some multiplier.
    #[serde(rename(deserialize = "multiplier", serialize = "multiplier"))]
    Multiplier {
        /// Multiplier value applied to original job's duration.
        value: f64,
        /// Parking time.
        parking: f64,
    },
    /// Use fixed value for all clustered jobs.
    #[serde(rename(deserialize = "fixed", serialize = "fixed"))]
    Fixed {
        /// Fixed value used for all jobs in the cluster.
        value: f64,
        /// Parking time.
        parking: f64,
    },
}

/// Specifies filtering policy for vicinity clustering.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VicinityFilteringPolicy {
    /// Ids of the jobs which cannot be used within clustering.
    pub exclude_job_ids: Vec<String>,
}

// endregion

/// A plan specifies work which has to be done.
#[derive(Clone, Deserialize, Debug, Serialize)]
pub struct Plan {
    /// List of jobs.
    pub jobs: Vec<Job>,

    /// List of relations between jobs and vehicles.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub relations: Option<Vec<Relation>>,

    /// Specifies clustering parameters.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub clustering: Option<Clustering>,
}

// endregion

// region Fleet

/// Specifies vehicle costs.
#[derive(Clone, Deserialize, Debug, Serialize)]
pub struct VehicleCosts {
    /// Fixed is cost of vehicle usage per tour.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fixed: Option<f64>,

    /// Cost per distance unit.
    pub distance: f64,

    /// Cost per time unit.
    pub time: f64,
}

/// Specifies vehicle shift start.
#[derive(Clone, Deserialize, Debug, Serialize)]
pub struct ShiftStart {
    /// Earliest possible departure date time in RFC3339 format.
    pub earliest: String,

    /// Latest possible departure date time in RFC3339 format. If omitted, departure time
    /// theoretically can be shifted till arrival. Set this value, if you want to limit
    /// departure time optimization.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub latest: Option<String>,

    /// Shift start location.
    pub location: Location,
}

/// Specifies vehicle shift end.
#[derive(Clone, Deserialize, Debug, Serialize)]
pub struct ShiftEnd {
    /// Earliest possible arrival date time in RFC3339 format.
    /// At the moment, not supported, reserved for future.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub earliest: Option<String>,

    /// Latest possible arrival date time in RFC3339 format.
    pub latest: String,

    /// Shift end location.
    pub location: Location,
}

/// Specifies vehicle shift.
#[derive(Clone, Deserialize, Debug, Serialize)]
pub struct VehicleShift {
    /// Vehicle shift start.
    pub start: ShiftStart,

    /// Vehicle shift end.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end: Option<ShiftEnd>,

    /// Vehicle cargo dispatch parameters. If defined, vehicle starts empty at location,
    /// defined in ShiftStart, and navigates first to the one of specified places, e.g. to pickup
    /// the goods.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dispatch: Option<Vec<VehicleDispatch>>,

    /// Vehicle breaks.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub breaks: Option<Vec<VehicleBreak>>,

    /// Vehicle reloads which allows vehicle to visit place where goods can be loaded or
    /// unloaded during single tour.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reloads: Option<Vec<VehicleReload>>,
}

/// Specifies a dispatch place where vehicle can load cargo and start the tour.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VehicleDispatch {
    /// A dispatch place location.
    pub location: Location,
    /// Specifies vehicle dispatch parameters.
    pub limits: Vec<VehicleDispatchLimit>,
    /// A tag which will be propagated back within corresponding activity in solution.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
}

/// Specifies dispatch place limits to handle vehicles.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VehicleDispatchLimit {
    /// Max amount of vehicles which can be dispatched during given period.
    pub max: usize,
    /// A dispatch start time in RFC3339 time format.
    pub start: String,
    /// A dispatch end time in RFC3339 time format.
    pub end: String,
}

/// Specifies a place where vehicle can load or unload cargo.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VehicleReload {
    /// A place location.
    pub location: Location,

    /// A total loading/reloading duration (service time).
    pub duration: f64,

    /// A list of time windows with time specified in RFC3339 format.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub times: Option<Vec<Vec<String>>>,

    /// A tag which will be propagated back within corresponding activity in solution.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,

    /// A shared reload resource id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resource_id: Option<String>,
}

/// Vehicle limits.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VehicleLimits {
    /// Max traveling distance per shift/tour.
    /// No distance restrictions when omitted.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_distance: Option<f64>,

    /// Max time per shift/tour.
    /// No time restrictions when omitted.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shift_time: Option<f64>,

    /// Max amount job activities.
    /// No job activities restrictions when omitted.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tour_size: Option<usize>,
}

/// An area limit.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AreaLimit {
    /// An area id.
    pub area_id: String,
    /// A value added to the total value once job is served in given area.
    pub job_value: f64,
}

/// Vehicle optional break time variant.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(untagged)]
pub enum VehicleOptionalBreakTime {
    /// Break time is defined by a time window with time specified in RFC3339 format.
    TimeWindow(Vec<String>),
    /// Break time is defined by a time offset range.
    TimeOffset(Vec<f64>),
}

/// Vehicle required break time variant.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(untagged)]
pub enum VehicleRequiredBreakTime {
    /// Break time is defined by exact time in RFC3339 format.
    ExactTime(String),
    /// Break time is defined by amount of seconds since driving time.
    OffsetTime(f64),
}

/// Vehicle break place.
#[derive(Clone, Deserialize, Debug, Serialize)]
pub struct VehicleOptionalBreakPlace {
    /// Break duration.
    pub duration: f64,
    /// Break location.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub location: Option<Location>,
    /// A tag which will be propagated back within corresponding activity in solution.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
}

/// Vehicle break policy.
#[derive(Clone, Deserialize, Debug, Serialize)]
pub enum VehicleOptionalBreakPolicy {
    /// Allows to skip break if actual tour schedule doesn't intersect with vehicle time window.
    #[serde(rename(deserialize = "skip-if-no-intersection", serialize = "skip-if-no-intersection"))]
    SkipIfNoIntersection,
    /// Allows to skip break if vehicle arrives before break's time window end.
    #[serde(rename(deserialize = "skip-if-arrival-before-end", serialize = "skip-if-arrival-before-end"))]
    SkipIfArrivalBeforeEnd,
}

/// Specifies a vehicle break.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(untagged)]
pub enum VehicleBreak {
    /// An optional break which is more flexible, but might be not assigned.
    Optional {
        /// Break time.
        time: VehicleOptionalBreakTime,
        /// Vehicle break places.
        places: Vec<VehicleOptionalBreakPlace>,
        /// Specifies vehicle break policy.
        policy: Option<VehicleOptionalBreakPolicy>,
    },
    /// A break which has to be assigned. It is less flexible than optional break, but has strong
    /// assignment guarantee.
    Required {
        /// Break time.
        time: VehicleRequiredBreakTime,
        /// Break duration.
        duration: f64,
    },
}

/// Specifies a vehicle type.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VehicleType {
    /// Vehicle type id.
    pub type_id: String,

    /// Concrete vehicle ids.
    pub vehicle_ids: Vec<String>,

    /// Vehicle profile.
    pub profile: VehicleProfile,

    /// Vehicle costs.
    pub costs: VehicleCosts,

    /// Vehicle shifts.
    pub shifts: Vec<VehicleShift>,

    /// Vehicle capacity.
    pub capacity: Vec<i32>,

    /// Vehicle skills.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub skills: Option<Vec<String>>,

    /// Vehicle limits.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limits: Option<VehicleLimits>,
}

/// Specifies a vehicle profile.
#[derive(Clone, Deserialize, Debug, Serialize)]
pub struct VehicleProfile {
    /// Routing matrix profile name.
    pub matrix: String,

    /// Traveling duration scale factor.
    /// Default value is 1.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scale: Option<f64>,
}

/// Specifies routing matrix profile.
#[derive(Clone, Deserialize, Debug, Serialize)]
pub struct MatrixProfile {
    /// Profile name.
    pub name: String,

    /// Approximation speed (meters per second). Used only when routing matrix is not specified.
    /// Default value is 10.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub speed: Option<f64>,
}

/// Specifies vehicle resource type.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(tag = "type")]
pub enum VehicleResource {
    /// A shared reload resource.
    #[serde(rename(deserialize = "reload", serialize = "reload"))]
    Reload {
        /// Resource id.
        id: String,
        /// A total resource capacity.
        capacity: Vec<i32>,
    },
}

/// Specifies fleet.
#[derive(Clone, Deserialize, Debug, Serialize)]
pub struct Fleet {
    /// Vehicle types.
    pub vehicles: Vec<VehicleType>,

    /// Routing profiles.
    pub profiles: Vec<MatrixProfile>,

    /// Specifies vehicle resources.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resources: Option<Vec<VehicleResource>>,
}

// endregion

// region Objective

/// Specifies objective function types.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(tag = "type")]
pub enum Objective {
    /// An objective to minimize total cost as linear combination of total time and distance.
    #[serde(rename(deserialize = "minimize-cost", serialize = "minimize-cost"))]
    MinimizeCost,

    /// An objective to minimize total distance.
    #[serde(rename(deserialize = "minimize-distance", serialize = "minimize-distance"))]
    MinimizeDistance,

    /// An objective to minimize total duration.
    #[serde(rename(deserialize = "minimize-duration", serialize = "minimize-duration"))]
    MinimizeDuration,

    /// An objective to minimize total tour amount.
    #[serde(rename(deserialize = "minimize-tours", serialize = "minimize-tours"))]
    MinimizeTours,

    /// An objective to maximize total tour amount.
    #[serde(rename(deserialize = "maximize-tours", serialize = "maximize-tours"))]
    MaximizeTours,

    /// An objective to maximize value of served jobs.
    #[serde(rename(deserialize = "maximize-value", serialize = "maximize-value"))]
    MaximizeValue {
        /// Specifies a weight of skipped breaks.
        #[serde(skip_serializing_if = "Option::is_none")]
        breaks: Option<f64>,
    },

    /// An objective to minimize amount of unassigned jobs.
    #[serde(rename(deserialize = "minimize-unassigned", serialize = "minimize-unassigned"))]
    MinimizeUnassignedJobs {
        /// A skipped break weight to increase/decrease break is importance.
        /// Default is 1.
        #[serde(skip_serializing_if = "Option::is_none")]
        breaks: Option<f64>,
    },

    /// An objective to minimize sum of arrival times from all routes.
    #[serde(rename(deserialize = "minimize-arrival-time", serialize = "minimize-arrival-time"))]
    MinimizeArrivalTime,

    /// An objective to balance max load across all tours.
    #[serde(rename(deserialize = "balance-max-load", serialize = "balance-max-load"))]
    BalanceMaxLoad {
        /// A relative load in single tour before balancing takes place.
        #[serde(skip_serializing_if = "Option::is_none")]
        options: Option<BalanceOptions>,
    },

    /// An objective to balance activities across all tours.
    #[serde(rename(deserialize = "balance-activities", serialize = "balance-activities"))]
    BalanceActivities {
        /// An options which can be used to specify minimum activity amount in a tour before
        /// it considered for balancing.
        #[serde(skip_serializing_if = "Option::is_none")]
        options: Option<BalanceOptions>,
    },

    /// An objective to balance distance across all tours.
    #[serde(rename(deserialize = "balance-distance", serialize = "balance-distance"))]
    BalanceDistance {
        /// An options which can be used to specify minimum distance of a tour before
        /// it considered for balancing.
        #[serde(skip_serializing_if = "Option::is_none")]
        options: Option<BalanceOptions>,
    },

    /// An objective to balance duration across all tours.
    #[serde(rename(deserialize = "balance-duration", serialize = "balance-duration"))]
    BalanceDuration {
        /// An options which can be used to specify minimum duration of a tour before
        /// it considered for balancing.
        #[serde(skip_serializing_if = "Option::is_none")]
        options: Option<BalanceOptions>,
    },

    /// An objective to control order of job activities in the tour.
    #[serde(rename(deserialize = "tour-order", serialize = "tour-order"))]
    TourOrder,
}

/// Specifies balance objective options. At the moment, it uses coefficient of variation as
/// balancing measure.
#[derive(Clone, Deserialize, Debug, Serialize)]
pub struct BalanceOptions {
    /// A balancing threshold specifies desired balancing level. Lower values can be ignored in
    /// favor of another objective.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub threshold: Option<f64>,
}

// endregion

// region Common

/// A VRP problem definition.
#[derive(Clone, Deserialize, Debug, Serialize)]
pub struct Problem {
    /// Problem plan: customers to serve.
    pub plan: Plan,

    /// Problem resources: vehicles to be used, routing info.
    pub fleet: Fleet,

    /// Specifies objective function hierarchy.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub objectives: Option<Vec<Vec<Objective>>>,
}

/// A routing matrix.
#[derive(Clone, Deserialize, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Matrix {
    /// A name of profile.
    pub profile: Option<String>,

    /// A date in RFC3999 for which routing info is applicable.
    pub timestamp: Option<String>,

    /// Travel distances (used to be in seconds).
    #[serde(alias = "durations")]
    pub travel_times: Vec<i64>,

    /// Travel durations (use to be in meters).
    pub distances: Vec<i64>,

    /// Error codes to mark unreachable locations.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_codes: Option<Vec<i64>>,
}

// endregion

/// Deserializes problem in json format from `BufReader`.
pub fn deserialize_problem<R: Read>(reader: BufReader<R>) -> Result<Problem, Vec<FormatError>> {
    serde_json::from_reader(reader).map_err(|err| {
        vec![FormatError::new(
            "E0000".to_string(),
            "cannot deserialize problem".to_string(),
            format!("check input json: '{}'", err),
        )]
    })
}

/// Deserializes routing matrix in json format from `BufReader`.
pub fn deserialize_matrix<R: Read>(reader: BufReader<R>) -> Result<Matrix, Vec<FormatError>> {
    serde_json::from_reader(reader).map_err(|err| {
        vec![FormatError::new(
            "E0001".to_string(),
            "cannot deserialize matrix".to_string(),
            format!("check input json: '{}'", err),
        )]
    })
}

/// Deserializes json list of locations from `BufReader`.
pub fn deserialize_locations<R: Read>(reader: BufReader<R>) -> Result<Vec<Location>, Vec<FormatError>> {
    serde_json::from_reader(reader).map_err(|err| {
        vec![FormatError::new(
            "E0000".to_string(),
            "cannot deserialize locations".to_string(),
            format!("check input json: '{}'", err),
        )]
    })
}

/// Serializes `problem` in json from `writer`.
pub fn serialize_problem<W: Write>(writer: BufWriter<W>, problem: &Problem) -> Result<(), Error> {
    serde_json::to_writer_pretty(writer, problem).map_err(Error::from)
}