Skip to content

Commit cc1d19f

Browse files
committed
filter out too short constraints
1 parent b944503 commit cc1d19f

File tree

8 files changed

+245
-137
lines changed

8 files changed

+245
-137
lines changed

examples/simple.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ use ordered_float::OrderedFloat;
1010
struct Model {
1111
agent_size: f64,
1212
graph: Arc<Graph<SimpleNodeData, SimpleEdgeData>>,
13-
solution:
14-
Option<Vec<Solution<Arc<SippState<SimpleState, MyTime>>, GraphEdgeId, MyTime, MyTime>>>,
13+
solution: Option<
14+
Vec<Solution<Arc<SippState<SimpleState, MyTime, MyTime>>, GraphEdgeId, MyTime, MyTime>>,
15+
>,
1516
start_time: f32,
1617
colors: Vec<rgb::Rgb<nannou::color::encoding::Srgb, u8>>,
1718
limits: ((f32, f32), (f32, f32)),

src/abstraction/search.rs

+78-42
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ pub trait Heuristic<TS, S, A, C, DC>
6161
where
6262
TS: TransitionSystem<S, A, C, DC>,
6363
S: Hash + Eq + Clone,
64-
C: Eq + PartialOrd + Ord + Add<DC, Output = C> + Copy + Default + LimitValues,
64+
C: Eq
65+
+ PartialOrd
66+
+ Ord
67+
+ Add<DC, Output = C>
68+
+ Sub<C, Output = DC>
69+
+ Copy
70+
+ Default
71+
+ LimitValues,
6572
{
6673
/// Returns the heuristic value for the given state,
6774
/// or None if the goal state is not reachable from that state.
@@ -73,7 +80,14 @@ pub trait MinimalHeuristic<TS, S, A, C, DC>
7380
where
7481
TS: TransitionSystem<S, A, C, DC>,
7582
S: State + Hash + Eq + Clone,
76-
C: Eq + PartialOrd + Ord + Add<DC, Output = C> + Copy + Default + LimitValues,
83+
C: Eq
84+
+ PartialOrd
85+
+ Ord
86+
+ Add<DC, Output = C>
87+
+ Sub<C, Output = DC>
88+
+ Copy
89+
+ Default
90+
+ LimitValues,
7791
{
7892
fn build(transition_system: Arc<TS>, task: Arc<Task<S, C>>) -> Self;
7993
}
@@ -215,20 +229,20 @@ pub enum ConflictType {
215229
#[derive(Debug)]
216230
pub struct Conflict<S, A, C, DC>
217231
where
218-
C: Ord + LimitValues,
232+
C: Ord + LimitValues + Sub<C, Output = DC> + Copy,
219233
DC: Ord + Default,
220234
{
221-
pub moves: A2<Move<S, A, C>>,
235+
pub moves: A2<Move<S, A, C, DC>>,
222236
pub type_: ConflictType,
223237
pub overcost: DC,
224238
}
225239

226240
impl<S, A, C, DC> Conflict<S, A, C, DC>
227241
where
228-
C: Ord + LimitValues,
242+
C: Ord + LimitValues + Sub<C, Output = DC> + Copy,
229243
DC: Ord + Default,
230244
{
231-
pub fn new(moves: A2<Move<S, A, C>>) -> Self {
245+
pub fn new(moves: A2<Move<S, A, C, DC>>) -> Self {
232246
Self {
233247
moves,
234248
type_: ConflictType::NonCardinal,
@@ -239,7 +253,7 @@ where
239253

240254
impl<S, A, C, DC> PartialEq for Conflict<S, A, C, DC>
241255
where
242-
C: Ord + Copy + LimitValues,
256+
C: Ord + Copy + LimitValues + Sub<C, Output = DC>,
243257
DC: Ord + Default,
244258
{
245259
fn eq(&self, other: &Self) -> bool {
@@ -257,14 +271,14 @@ where
257271

258272
impl<S, A, C, DC> Eq for Conflict<S, A, C, DC>
259273
where
260-
C: Ord + Copy + LimitValues,
274+
C: Ord + Copy + LimitValues + Sub<C, Output = DC>,
261275
DC: Ord + Default,
262276
{
263277
}
264278

265279
impl<S, A, C, DC> PartialOrd for Conflict<S, A, C, DC>
266280
where
267-
C: Ord + Copy + LimitValues,
281+
C: Ord + Copy + LimitValues + Sub<C, Output = DC>,
268282
DC: Ord + Default,
269283
{
270284
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
@@ -274,7 +288,7 @@ where
274288

275289
impl<S, A, C, DC> Ord for Conflict<S, A, C, DC>
276290
where
277-
C: Ord + Copy + LimitValues,
291+
C: Ord + Copy + LimitValues + Sub<C, Output = DC>,
278292
DC: Ord + Default,
279293
{
280294
fn cmp(&self, other: &Self) -> Ordering {
@@ -306,26 +320,26 @@ pub trait LimitValues {
306320

307321
/// Defines a time interval (start <= end).
308322
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
309-
pub struct Interval<C>
323+
pub struct Interval<C, DC>
310324
where
311-
C: PartialEq + Eq + PartialOrd + Ord + LimitValues,
325+
C: PartialEq + Eq + PartialOrd + Ord + LimitValues + Sub<C, Output = DC> + Copy,
312326
{
313327
pub start: C,
314328
pub end: C,
315329
}
316330

317-
impl<C> Default for Interval<C>
331+
impl<C, DC> Default for Interval<C, DC>
318332
where
319-
C: PartialEq + Eq + PartialOrd + Ord + LimitValues,
333+
C: PartialEq + Eq + PartialOrd + Ord + LimitValues + Sub<C, Output = DC> + Copy,
320334
{
321335
fn default() -> Self {
322336
Self::new(C::min_value(), C::max_value())
323337
}
324338
}
325339

326-
impl<C> Interval<C>
340+
impl<C, DC> Interval<C, DC>
327341
where
328-
C: PartialEq + Eq + PartialOrd + Ord + LimitValues,
342+
C: PartialEq + Eq + PartialOrd + Ord + LimitValues + Sub<C, Output = DC> + Copy,
329343
{
330344
pub fn new(start: C, end: C) -> Self {
331345
Self { start, end }
@@ -338,6 +352,10 @@ where
338352
pub fn contains(&self, other: &Self) -> bool {
339353
self.start <= other.start && other.end <= self.end
340354
}
355+
356+
pub fn length(&self) -> DC {
357+
self.end - self.start
358+
}
341359
}
342360

343361
/// The types of constraints that can be imposed on agents in a search algorithm.
@@ -351,22 +369,24 @@ pub enum ConstraintType {
351369

352370
/// Defines a constraint that can be imposed on a given agent in a search algorithm.
353371
#[derive(Debug, Clone)]
354-
pub struct Constraint<S, C>
372+
pub struct Constraint<S, C, DC>
355373
where
356-
C: PartialEq + Eq + PartialOrd + Ord + LimitValues,
374+
C: PartialEq + Eq + PartialOrd + Ord + LimitValues + Sub<C, Output = DC> + Copy,
375+
DC: PartialEq + Eq + PartialOrd + Ord,
357376
{
358377
pub agent: usize,
359378
pub state: S,
360379
pub next: Option<S>,
361-
pub interval: Interval<C>,
380+
pub interval: Interval<C, DC>,
362381
pub type_: ConstraintType,
363382
}
364383

365-
impl<S, C> Constraint<S, C>
384+
impl<S, C, DC> Constraint<S, C, DC>
366385
where
367-
C: PartialEq + Eq + PartialOrd + Ord + LimitValues,
386+
C: PartialEq + Eq + PartialOrd + Ord + LimitValues + Sub<C, Output = DC> + Copy,
387+
DC: PartialEq + Eq + PartialOrd + Ord,
368388
{
369-
pub fn new_state_constraint(agent: usize, state: S, interval: Interval<C>) -> Self {
389+
pub fn new_state_constraint(agent: usize, state: S, interval: Interval<C, DC>) -> Self {
370390
Self {
371391
agent,
372392
state,
@@ -375,7 +395,12 @@ where
375395
type_: ConstraintType::State,
376396
}
377397
}
378-
pub fn new_action_constraint(agent: usize, state: S, next: S, interval: Interval<C>) -> Self {
398+
pub fn new_action_constraint(
399+
agent: usize,
400+
state: S,
401+
next: S,
402+
interval: Interval<C, DC>,
403+
) -> Self {
379404
Self {
380405
agent,
381406
state,
@@ -386,29 +411,37 @@ where
386411
}
387412
}
388413

389-
impl<S, C> PartialEq for Constraint<S, C>
414+
impl<S, C, DC> PartialEq for Constraint<S, C, DC>
390415
where
391-
C: PartialEq + Eq + PartialOrd + Ord + LimitValues,
416+
C: PartialEq + Eq + PartialOrd + Ord + LimitValues + Sub<C, Output = DC> + Copy,
417+
DC: PartialEq + Eq + PartialOrd + Ord,
392418
{
393419
fn eq(&self, other: &Self) -> bool {
394420
self.interval == other.interval
395421
}
396422
}
397423

398-
impl<S, C> Eq for Constraint<S, C> where C: PartialEq + Eq + PartialOrd + Ord + LimitValues {}
424+
impl<S, C, DC> Eq for Constraint<S, C, DC>
425+
where
426+
C: PartialEq + Eq + PartialOrd + Ord + LimitValues + Sub<C, Output = DC> + Copy,
427+
DC: PartialEq + Eq + PartialOrd + Ord,
428+
{
429+
}
399430

400-
impl<S, C> PartialOrd for Constraint<S, C>
431+
impl<S, C, DC> PartialOrd for Constraint<S, C, DC>
401432
where
402-
C: PartialEq + Eq + PartialOrd + Ord + LimitValues,
433+
C: PartialEq + Eq + PartialOrd + Ord + LimitValues + Sub<C, Output = DC> + Copy,
434+
DC: PartialEq + Eq + PartialOrd + Ord,
403435
{
404436
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
405437
Some(self.cmp(other))
406438
}
407439
}
408440

409-
impl<S, C> Ord for Constraint<S, C>
441+
impl<S, C, DC> Ord for Constraint<S, C, DC>
410442
where
411-
C: PartialEq + Eq + PartialOrd + Ord + LimitValues,
443+
C: PartialEq + Eq + PartialOrd + Ord + LimitValues + Sub<C, Output = DC> + Copy,
444+
DC: PartialEq + Eq + PartialOrd + Ord,
412445
{
413446
fn cmp(&self, other: &Self) -> Ordering {
414447
self.interval.cmp(&other.interval)
@@ -417,19 +450,21 @@ where
417450

418451
/// Set of constraints that can be imposed on agents in a search algorithm.
419452
#[derive(Debug)]
420-
pub struct ConstraintSet<S, C>
453+
pub struct ConstraintSet<S, C, DC>
421454
where
422455
S: State + Eq + Hash + Clone,
423-
C: PartialEq + Eq + PartialOrd + Ord + LimitValues + Copy,
456+
C: PartialEq + Eq + PartialOrd + Ord + LimitValues + Copy + Sub<C, Output = DC>,
457+
DC: PartialEq + Eq + PartialOrd + Ord + Copy,
424458
{
425-
pub state_constraints: FxHashMap<S, Vec<Constraint<S, C>>>,
426-
pub action_constraints: FxHashMap<(S, S), Vec<Constraint<S, C>>>,
459+
pub state_constraints: FxHashMap<S, Vec<Constraint<S, C, DC>>>,
460+
pub action_constraints: FxHashMap<(S, S), Vec<Constraint<S, C, DC>>>,
427461
}
428462

429-
impl<S, C> Default for ConstraintSet<S, C>
463+
impl<S, C, DC> Default for ConstraintSet<S, C, DC>
430464
where
431465
S: State + Eq + Hash + Clone,
432-
C: PartialEq + Eq + PartialOrd + Ord + LimitValues + Copy,
466+
C: PartialEq + Eq + PartialOrd + Ord + LimitValues + Copy + Sub<C, Output = DC>,
467+
DC: PartialEq + Eq + PartialOrd + Ord + Copy,
433468
{
434469
fn default() -> Self {
435470
Self {
@@ -439,12 +474,13 @@ where
439474
}
440475
}
441476

442-
impl<S, C> ConstraintSet<S, C>
477+
impl<S, C, DC> ConstraintSet<S, C, DC>
443478
where
444479
S: State + Eq + Hash + Clone,
445-
C: PartialEq + Eq + PartialOrd + Ord + LimitValues + Copy,
480+
C: PartialEq + Eq + PartialOrd + Ord + LimitValues + Copy + Sub<C, Output = DC>,
481+
DC: PartialEq + Eq + PartialOrd + Ord + Copy,
446482
{
447-
pub fn add(&mut self, constraint: &Arc<Constraint<S, C>>) {
483+
pub fn add(&mut self, constraint: &Arc<Constraint<S, C, DC>>) {
448484
match constraint.type_ {
449485
ConstraintType::State => {
450486
self.state_constraints
@@ -464,11 +500,11 @@ where
464500
}
465501
}
466502

467-
pub fn get_state_constraints(&self, state: &S) -> Option<&Vec<Constraint<S, C>>> {
503+
pub fn get_state_constraints(&self, state: &S) -> Option<&Vec<Constraint<S, C, DC>>> {
468504
self.state_constraints.get(state)
469505
}
470506

471-
pub fn get_action_constraints(&self, from: &S, to: &S) -> Option<&Vec<Constraint<S, C>>> {
507+
pub fn get_action_constraints(&self, from: &S, to: &S) -> Option<&Vec<Constraint<S, C, DC>>> {
472508
self.action_constraints.get(&(from.clone(), to.clone()))
473509
}
474510

@@ -525,4 +561,4 @@ where
525561
}
526562
}
527563

528-
pub type LandmarkSet<S, C> = Vec<Arc<Constraint<S, C>>>;
564+
pub type LandmarkSet<S, C, DC> = Vec<Arc<Constraint<S, C, DC>>>;

src/abstraction/transition.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::slice;
1+
use std::{ops::Sub, slice};
22

33
use tuple::A2;
44

@@ -16,7 +16,7 @@ pub trait State {
1616
/// The reverse transitions must also be described to allow using the reverse search as a heuristic.
1717
pub trait TransitionSystem<S, A, C, DC>
1818
where
19-
C: Ord + LimitValues,
19+
C: Ord + LimitValues + Sub<C, Output = DC> + Copy,
2020
{
2121
fn actions_from(&self, state: &S) -> slice::Iter<A>;
2222

@@ -31,7 +31,7 @@ where
3131
fn can_wait_at(&self, state: &S) -> bool;
3232

3333
/// Returns true if the two moves lead to a collision.
34-
fn conflict(&self, moves: A2<&Move<S, A, C>>) -> bool;
34+
fn conflict(&self, moves: A2<&Move<S, A, C, DC>>) -> bool;
3535
}
3636

3737
/// Definition of a callback that can be used to apply actions to a transition system.
@@ -85,22 +85,22 @@ where
8585

8686
/// Definition of a move in a transition system.
8787
#[derive(Debug, Clone)]
88-
pub struct Move<S, A, C>
88+
pub struct Move<S, A, C, DC>
8989
where
90-
C: Ord + LimitValues,
90+
C: Ord + LimitValues + Sub<C, Output = DC> + Copy,
9191
{
9292
pub agent: usize,
9393
pub from: S,
9494
pub to: S,
9595
pub action: Option<A>,
96-
pub interval: Interval<C>,
96+
pub interval: Interval<C, DC>,
9797
}
9898

99-
impl<S, A, C> Move<S, A, C>
99+
impl<S, A, C, DC> Move<S, A, C, DC>
100100
where
101-
C: Ord + LimitValues,
101+
C: Ord + LimitValues + Sub<C, Output = DC> + Copy,
102102
{
103-
pub fn new(agent: usize, from: S, to: S, action: Option<A>, interval: Interval<C>) -> Self {
103+
pub fn new(agent: usize, from: S, to: S, action: Option<A>, interval: Interval<C, DC>) -> Self {
104104
Self {
105105
agent,
106106
from,

0 commit comments

Comments
 (0)