|
2 | 2 | //! |
3 | 3 | //! A vehicle routing solver library. |
4 | 4 | //! |
5 | | -//! This library provides functionalities to solve various vehicle routing problems (VRP), including |
| 5 | +//! ! WARNING: Some APIs like the Plan API are still unimplemented and subject to change. |
| 6 | +//! |
| 7 | +//! This library provides functionalities to solve various vehicle routing problems (VRP), including: |
6 | 8 | //! |
7 | 9 | //! - Pickup and Delivery Problem (PDP) |
8 | 10 | //! |
|
14 | 16 | //! use solver_vrp::random::Random; |
15 | 17 | //! |
16 | 18 | //! // Implement a custom objective to add optimized features like linehaul costs to the model. |
| 19 | +//! struct ZeroObjective { |
| 20 | +//! zero: f64, |
| 21 | +//! } |
| 22 | +//! |
17 | 23 | //! impl Objective for ZeroObjective { |
18 | 24 | //! fn name(&self) -> String { |
19 | | -//! String::from("Zero Objective") |
| 25 | +//! String::from("My Zero Objective") |
20 | 26 | //! } |
21 | 27 | //! |
22 | | -//! // Returns the computed value of the objective for the given plan. |
23 | | -//! fn compute(&self, _model: &Model, _solution: &Solution, _plan: &Plan) -> f64 { |
24 | | -//! 0.0 |
25 | | -//! } |
| 28 | +//! // Computes the value of the objective for the given plan. |
| 29 | +//! fn compute(&self, _model: &Model, _solution: &Solution, _plan: &Plan) -> f64 { |
| 30 | +//! self.zero |
| 31 | +//! } |
26 | 32 | //! } |
27 | 33 | //! |
28 | 34 | //! // Implement a custom constraint to enforce unique business rules in the model. |
29 | | -//! struct MaxVehicleWeight((f64, f64)); |
| 35 | +//! struct MyVehicleCapacities(vec![f64; 2]); |
30 | 36 | //! |
31 | | -//! impl Constraint for MaxVehicleWeight { |
| 37 | +//! impl Constraint for MyVehicleCapacities { |
32 | 38 | //! fn name(&self) -> String { |
33 | | -//! String::from("Max Vehicle Weight") |
| 39 | +//! String::from("My Vehicle Capacities") |
34 | 40 | //! } |
35 | 41 | //! |
36 | 42 | //! // Returns true if the plan is feasible. |
37 | 43 | //! fn is_feasible(&self, plan: &Plan) -> bool { |
38 | | -//! plan.route() |
39 | | -//! .changes() |
40 | | -//! .last() |
41 | | -//! .map_or(true, |change| { |
42 | | -//! change.capacity.utilization(1) <= self.0.1 |
43 | | -//! }); |
| 44 | +//! let i = 1; |
| 45 | +//! self.0.get(i) |
| 46 | +//! .zip(plan.route().changes().last()) |
| 47 | +//! .and_then(|(max, change)| change.required_capacity().get(i).map(|d| d <= max)) |
| 48 | +//! .unwrap_or(true) |
44 | 49 | //! } |
45 | 50 | //! } |
46 | 51 | //! |
47 | 52 | //! // Add new operators to refine the solver's search and heuristic capabilities. |
48 | | -//! #![derive(Default)] |
49 | | -//! struct RejectEverything { |
50 | | -//! parameters: OperatorParameters, |
51 | | -//! }; |
| 53 | +//! struct SimpleOperator {}; |
52 | 54 | //! |
53 | | -//! impl Operator for CustomOperator { |
| 55 | +//! impl Operator for SimpleOperator { |
54 | 56 | //! fn name(&self) -> String { |
55 | | -//! String::from("Custom Operator") |
| 57 | +//! String::from("Simple Operator") |
56 | 58 | //! } |
57 | 59 | //! |
58 | | -//! // Returns the new solution after executing the operator. |
| 60 | +//! // Returns the new plan after executing the operator. |
59 | 61 | //! fn execute(&self, _model: &Model, _solution: &Solution, _random: &mut Random) -> Plan { |
60 | | -//! Plan::default() |
| 62 | +//! return Plan::new(); |
61 | 63 | //! } |
62 | 64 | //! } |
63 | 65 | //! |
64 | 66 | //! fn run() { |
65 | 67 | //! // Build the model with custom components. |
66 | 68 | //! let model = ModelBuilder::new() |
67 | | -//! .objective(CustomObjective::default()) |
68 | | -//! .constraint(CustomConstraint((26.0, 40_000.0))) |
| 69 | +//! .objective(ZeroObjective { zero: 0.0 }) |
| 70 | +//! .constraint(MyVehicleCapacities(vec![26.0, 40_000.0])) |
69 | 71 | //! .build(); |
70 | 72 | //! |
71 | 73 | //! // Define options for the solver. |
|
77 | 79 | //! let solver = SolverBuilder::new() |
78 | 80 | //! .options(options) |
79 | 81 | //! .model(model) |
80 | | -//! .plan(initial_solution) |
81 | | -//! .operator(CustomOperator {}) |
| 82 | +//! .solution(initial_solution) |
| 83 | +//! .operator(SimpleOperator {}) |
82 | 84 | //! .build(); |
83 | 85 | //! |
84 | 86 | //! let best_solution = solver.solve(); |
|
0 commit comments