pub struct Matrix { /* private fields */ }Expand description
Dense square matrix of travel costs between units.
Implementations§
Source§impl Matrix
impl Matrix
Sourcepub fn from_vec(n: usize, data: Vec<f64>) -> Result<Self, String>
pub fn from_vec(n: usize, data: Vec<f64>) -> Result<Self, String>
Build from a row-major vector of length n * n.
Examples found in repository?
examples/benchmark.rs (line 137)
102fn main() {
103 let args: Vec<String> = std::env::args().collect();
104 let dir = args.get(1).expect("directory with instances");
105 let iterations: usize = args.get(2).and_then(|v| v.parse().ok()).unwrap_or(2000);
106 let time_limit: f64 = args
107 .get(3)
108 .and_then(|v| v.parse().ok())
109 .unwrap_or(f64::INFINITY);
110 let mut files: Vec<_> = std::fs::read_dir(dir)
111 .expect("read dir")
112 .filter_map(|e| e.ok().map(|e| e.path()))
113 .filter(|p| {
114 matches!(
115 p.extension().and_then(|e| e.to_str()),
116 Some("tsp") | Some("vrp")
117 )
118 })
119 .collect();
120 files.sort();
121 println!(
122 "{:<12} {:>5} {:>7} {:>9} {:>9} {:>7} {:>8} {:>6}",
123 "instance", "n", "routes", "cost", "optimum", "gap%", "bound%", "sec"
124 );
125 for f in files {
126 let p = parse(&f);
127 let n = p.coords.len();
128 let mut data = vec![0.0; n * n];
129 for i in 0..n {
130 for j in 0..n {
131 let d = ((p.coords[i].0 - p.coords[j].0).powi(2)
132 + (p.coords[i].1 - p.coords[j].1).powi(2))
133 .sqrt();
134 data[i * n + j] = d.round();
135 }
136 }
137 let m = Matrix::from_vec(n, data).unwrap();
138 let units: Vec<usize> = (0..n).filter(|&i| i != p.depot).collect();
139 let limits = RouteLimits {
140 max_length: f64::INFINITY,
141 max_stops: usize::MAX,
142 capacity: p.capacity,
143 };
144 let options = GraspOptions {
145 iterations,
146 time_limit,
147 alpha: 0.3,
148 seed: 1,
149 };
150 let start = Instant::now();
151 let sol = solve_routes(&m, p.depot, &units, &[], &p.demand, limits, options).unwrap();
152 let secs = start.elapsed().as_secs_f64();
153 let gap = p.optimum.map(|o| 100.0 * (sol.total - o) / o);
154 let bound_gap = 100.0 * (sol.total - sol.lower_bound) / sol.total;
155 println!(
156 "{:<12} {:>5} {:>7} {:>9.0} {:>9} {:>7} {:>8.2} {:>6.1}",
157 p.name,
158 units.len(),
159 sol.routes.len(),
160 sol.total,
161 p.optimum.map_or("?".to_string(), |o| format!("{o:.0}")),
162 gap.map_or("?".to_string(), |g| format!("{g:.2}")),
163 bound_gap,
164 secs
165 );
166 }
167}Trait Implementations§
impl StructuralPartialEq for Matrix
Auto Trait Implementations§
impl Freeze for Matrix
impl RefUnwindSafe for Matrix
impl Send for Matrix
impl Sync for Matrix
impl Unpin for Matrix
impl UnsafeUnpin for Matrix
impl UnwindSafe for Matrix
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more