mapf 0.3.0

Base traits and utilities for multi-agent planning
Documentation
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
/*
 * Copyright (C) 2023 Open Source Robotics Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

use super::*;
use crate::error::NoError;
use num::traits::Zero;

/// The `Weighted` trait describes how to calculate the cost of an action.
pub trait Weighted<State, Action> {
    /// How is cost represented. E.g. f32, f64, or u64
    type Cost;

    /// What kind of error can happen if a bad state or activity is provided
    type WeightedError;

    /// Calculate the cost for performing `action` which transitions
    /// `from_state` to `to_state`.
    ///
    /// When cost returns `None` that implies the state is too expensive for
    /// consideration.
    fn cost(
        &self,
        from_state: &State,
        action: &Action,
        to_state: &State,
    ) -> Result<Option<Self::Cost>, Self::WeightedError>;

    /// Calculate the cost of an initial state.
    fn initial_cost(&self, for_state: &State) -> Result<Option<Self::Cost>, Self::WeightedError>;
}

/// The `CostModifier` trait can be used with `.map` to modify the output of a
/// `Weighted` trait.
pub trait CostModifier<State, Action, Cost> {
    /// What kind of error can happen if a bad input is provided
    type CostModifierError;

    /// Modified the incoming cost. The output of this function will be used
    /// as the cost instead of the input.
    fn modify_cost(
        &self,
        from_state: &State,
        action: &Action,
        to_state: &State,
        original_cost: Cost,
    ) -> Result<Option<Cost>, Self::CostModifierError>;

    /// Modify the cost of an initial state. By default this does not modify
    /// anything. Initial state costs are usually zero anyway so this modifier
    /// is typically not needed.
    fn modify_initial_cost(
        &self,
        _: &State,
        original_cost: Cost,
    ) -> Result<Option<Cost>, Self::CostModifierError> {
        Ok(Some(original_cost))
    }
}

/// Implements CostModifier for simple proportional scaling of cost calculation.
///
/// Apply this to a Weighted property using `.map(ScaleWeight(scale))`.
pub struct ScaleWeight<Cost: std::ops::Mul<Cost, Output = Cost> + Clone>(pub Cost);
impl<State, Action, Cost> CostModifier<State, Action, Cost> for ScaleWeight<Cost>
where
    Cost: std::ops::Mul<Cost, Output = Cost> + Clone,
{
    type CostModifierError = NoError;
    fn modify_cost(
        &self,
        _: &State,
        _: &Action,
        _: &State,
        original_cost: Cost,
    ) -> Result<Option<Cost>, Self::CostModifierError> {
        Ok(Some(original_cost * self.0.clone()))
    }
}

impl<Base, Prop, Action> Weighted<Base::State, Action> for Incorporated<Base, Prop>
where
    Base: Domain,
    Prop: Weighted<Base::State, Action>,
    Prop::WeightedError: Into<Base::Error>,
{
    type Cost = Prop::Cost;
    type WeightedError = Base::Error;
    fn cost(
        &self,
        from_state: &Base::State,
        action: &Action,
        to_state: &Base::State,
    ) -> Result<Option<Self::Cost>, Self::WeightedError> {
        self.prop
            .cost(from_state, action, to_state)
            .map_err(Into::into)
    }

    fn initial_cost(
        &self,
        for_state: &Base::State,
    ) -> Result<Option<Self::Cost>, Self::WeightedError> {
        self.prop.initial_cost(for_state).map_err(Into::into)
    }
}

impl<Base, Prop, Action> Weighted<Base::State, Action> for Chained<Base, Prop>
where
    Base: Domain + Weighted<Base::State, Action>,
    Base::WeightedError: Into<Base::Error>,
    Prop: Weighted<Base::State, Action, Cost = Base::Cost>,
    Prop::WeightedError: Into<Base::Error>,
    Base::Cost: std::ops::Add<Base::Cost, Output = Base::Cost>,
{
    type Cost = Base::Cost;
    type WeightedError = Base::Error;
    fn cost(
        &self,
        from_state: &Base::State,
        action: &Action,
        to_state: &Base::State,
    ) -> Result<Option<Self::Cost>, Self::WeightedError> {
        let base_cost = self
            .base
            .cost(from_state, action, to_state)
            .map_err(Into::into)?;
        let prop_cost = self
            .prop
            .cost(from_state, action, to_state)
            .map_err(Into::into)?;

        let base_cost = match base_cost {
            Some(c) => c,
            None => return Ok(None),
        };
        let prop_cost = match prop_cost {
            Some(c) => c,
            None => return Ok(None),
        };

        Ok(Some(base_cost + prop_cost))
    }

    fn initial_cost(
        &self,
        for_state: &Base::State,
    ) -> Result<Option<Self::Cost>, Self::WeightedError> {
        let base_cost = self.base.initial_cost(for_state).map_err(Into::into)?;
        let prop_cost = self.prop.initial_cost(for_state).map_err(Into::into)?;

        let base_cost = match base_cost {
            Some(c) => c,
            None => return Ok(None),
        };
        let prop_cost = match prop_cost {
            Some(c) => c,
            None => return Ok(None),
        };

        Ok(Some(base_cost + prop_cost))
    }
}

impl<Base, Prop, Action> Weighted<Base::State, Action> for Mapped<Base, Prop>
where
    Base: Domain + Weighted<Base::State, Action>,
    Base::WeightedError: Into<Base::Error>,
    Prop: CostModifier<Base::State, Action, Base::Cost>,
    Prop::CostModifierError: Into<Base::Error>,
{
    type Cost = Base::Cost;
    type WeightedError = Base::Error;
    fn cost(
        &self,
        from_state: &Base::State,
        action: &Action,
        to_state: &Base::State,
    ) -> Result<Option<Self::Cost>, Self::WeightedError> {
        let base_cost = match self
            .base
            .cost(from_state, action, to_state)
            .map_err(Into::into)?
        {
            Some(base_cost) => base_cost,
            None => return Ok(None),
        };

        self.prop
            .modify_cost(from_state, action, to_state, base_cost)
            .map_err(Into::into)
    }

    fn initial_cost(
        &self,
        for_state: &Base::State,
    ) -> Result<Option<Self::Cost>, Self::WeightedError> {
        let base_cost = match self.base.initial_cost(for_state).map_err(Into::into)? {
            Some(base_cost) => base_cost,
            None => return Ok(None),
        };

        self.prop
            .modify_initial_cost(for_state, base_cost)
            .map_err(Into::into)
    }
}

impl<Base, Lifter, Prop, Action> Weighted<Base::State, Action> for Lifted<Base, Lifter, Prop>
where
    Base: Domain,
    Base::State: Clone,
    Action: Clone,
    Lifter: ProjectState<Base::State> + ActionMap<Base::State, Action>,
    Lifter::ActionMapError: Into<Base::Error>,
    Lifter::ProjectionError: Into<Base::Error>,
    Prop: Weighted<Lifter::ProjectedState, Lifter::ToAction>,
    Prop::WeightedError: Into<Base::Error>,
    Prop::Cost: std::ops::Add<Prop::Cost, Output = Prop::Cost> + Zero,
{
    type Cost = Prop::Cost;
    type WeightedError = Base::Error;
    fn cost(
        &self,
        from_state: &Base::State,
        action: &Action,
        to_state: &Base::State,
    ) -> Result<Option<Self::Cost>, Self::WeightedError> {
        let from_state_proj = match self.lifter.project(from_state).map_err(Into::into)? {
            Some(s) => s,
            None => return Ok(None),
        };

        let to_state_proj = match self.lifter.project(to_state).map_err(Into::into)? {
            Some(s) => s,
            None => return Ok(None),
        };

        let mut cost = Prop::Cost::zero();
        let actions = self
            .lifter
            .map_action(from_state.clone(), action.clone())
            .into_iter();
        for action in actions {
            let action = action.map_err(Into::into)?;
            let additional_cost = match self
                .prop
                .cost(&from_state_proj, &action, &to_state_proj)
                .map_err(Into::into)?
            {
                Some(c) => c,
                None => return Ok(None),
            };

            cost = cost + additional_cost;
        }

        Ok(Some(cost))
    }

    fn initial_cost(
        &self,
        for_state: &Base::State,
    ) -> Result<Option<Self::Cost>, Self::WeightedError> {
        let for_state_proj = match self.lifter.project(for_state).map_err(Into::into)? {
            Some(s) => s,
            None => return Ok(None),
        };

        self.prop.initial_cost(&for_state_proj).map_err(Into::into)
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use crate::error::NoError;
    use approx::assert_relative_eq;

    pub(crate) type Point = nalgebra::Point2<f64>;

    pub(crate) trait Mobile {
        fn position(&self) -> Point;
        fn distance_traveled(&self, from_other: &Self) -> f64;
    }

    pub(crate) trait BatteryPowered {
        fn battery_level(&self) -> f64;
    }

    #[derive(Clone, Copy)]
    pub(crate) struct TestState {
        pub(crate) position: Point,
        pub(crate) battery: f64,
    }

    impl From<TestState> for Point {
        fn from(value: TestState) -> Self {
            value.position
        }
    }

    pub(crate) struct Battery(f64);
    impl From<TestState> for Battery {
        fn from(value: TestState) -> Self {
            Battery(value.battery)
        }
    }

    impl Mobile for TestState {
        fn position(&self) -> Point {
            self.position
        }

        fn distance_traveled(&self, from_other: &Self) -> f64 {
            (self.position - from_other.position).norm()
        }
    }

    impl Mobile for Point {
        fn position(&self) -> Point {
            *self
        }

        fn distance_traveled(&self, from_other: &Self) -> f64 {
            (*self - *from_other).norm()
        }
    }

    impl BatteryPowered for TestState {
        fn battery_level(&self) -> f64 {
            self.battery
        }
    }

    impl BatteryPowered for Battery {
        fn battery_level(&self) -> f64 {
            self.0
        }
    }

    struct DistanceWeight(f64 /* cost per meter */);
    impl<State: Mobile, Action> Weighted<State, Action> for DistanceWeight {
        type Cost = f64;
        type WeightedError = NoError;
        fn cost(
            &self,
            from_state: &State,
            _: &Action,
            to_state: &State,
        ) -> Result<Option<Self::Cost>, Self::WeightedError> {
            Ok(Some(to_state.distance_traveled(from_state) * self.0))
        }

        fn initial_cost(&self, _: &State) -> Result<Option<Self::Cost>, Self::WeightedError> {
            Ok(Some(0.0))
        }
    }

    struct BatteryLossWeight(f64 /* cost per battery loss */);
    impl<State: BatteryPowered, Action> Weighted<State, Action> for BatteryLossWeight {
        type Cost = f64;
        type WeightedError = NoError;
        fn cost(
            &self,
            from_state: &State,
            _: &Action,
            to_state: &State,
        ) -> Result<Option<Self::Cost>, Self::WeightedError> {
            if to_state.battery_level() < 0.0 {
                return Ok(None);
            }

            Ok(Some(
                (from_state.battery_level() - to_state.battery_level()) * self.0,
            ))
        }

        fn initial_cost(&self, _: &State) -> Result<Option<Self::Cost>, Self::WeightedError> {
            Ok(Some(0.0))
        }
    }

    #[test]
    fn test_cost_calculation() {
        let domain = DefineTrait::<TestState>::new()
            .with(
                DefineTrait::<TestState>::new()
                    .with(DistanceWeight(0.1))
                    .map(ScaleWeight(2.0)),
            )
            .chain(
                DefineTrait::<TestState>::new()
                    .with(BatteryLossWeight(10.0))
                    .map(ScaleWeight(3.0)),
            );

        let from_state = TestState {
            position: Point::new(0.0, 0.0),
            battery: 1.0,
        };
        let to_state = TestState {
            position: Point::new(10.0, 0.0),
            battery: 0.5,
        };

        let cost = domain.cost(&from_state, &(), &to_state).unwrap().unwrap();
        assert_relative_eq!(cost, 10.0 * 0.1 * 2.0 + 0.5 * 10.0 * 3.0);
    }

    #[test]
    fn test_lifted_weight_calculation() {
        let domain = DefineTrait::<TestState>::new()
            .lift(
                DefineDomainMap::for_subspace(StateInto::<Point>::new()),
                DefineTrait::<Point>::new()
                    .with(DistanceWeight(0.1))
                    .map(ScaleWeight(2.0)),
            )
            .chain_lift(
                DefineDomainMap::for_subspace(StateInto::<Battery>::new()),
                DefineTrait::<Battery>::new()
                    .with(BatteryLossWeight(10.0))
                    .map(ScaleWeight(3.0)),
            );

        let from_state = TestState {
            position: Point::new(0.0, 0.0),
            battery: 1.0,
        };
        let to_state = TestState {
            position: Point::new(10.0, 0.0),
            battery: 0.5,
        };

        let cost = domain.cost(&from_state, &(), &to_state).unwrap().unwrap();
        assert_relative_eq!(cost, 10.0 * 0.1 * 2.0 + 0.5 * 10.0 * 3.0);
    }
}