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
use Score;
use CollectionExtract;
use ;
use UniConstraintStream;
/// An entity that knows whether its planning variable is unassigned, which
/// enables [`UniConstraintStream::unassigned`] on streams over it.
///
/// `#[planning_entity]` implements it for an entity with exactly one
/// `Option<_>` planning variable: the entity is unassigned while that field is
/// `None`. Implement it by hand for an entity whose "unassigned" state is
/// something else, such as several optional variables or a sentinel value.
///
/// # Examples
///
/// ```
/// use solverforge_core::score::SoftScore;
/// use solverforge_scoring::api::constraint_set::IncrementalConstraint;
/// use solverforge_scoring::stream::{
/// source, ChangeSource, ConstraintFactory, UnassignedEntity,
/// };
///
/// /// A shift whose `employee` is `usize::MAX` until it is assigned.
/// #[derive(Clone, Debug)]
/// struct Shift {
/// employee: usize,
/// }
///
/// #[derive(Clone)]
/// struct Roster {
/// shifts: Vec<Shift>,
/// }
///
/// impl UnassignedEntity<Roster> for Shift {
/// fn is_unassigned(_roster: &Roster, shift: &Self) -> bool {
/// shift.employee == usize::MAX
/// }
/// }
///
/// fn shifts(roster: &Roster) -> &[Shift] {
/// roster.shifts.as_slice()
/// }
///
/// let constraint = ConstraintFactory::<Roster, SoftScore>::new()
/// .for_each(source(shifts as fn(&Roster) -> &[Shift], ChangeSource::Descriptor(0)))
/// .unassigned()
/// .penalize(SoftScore::of(1))
/// .named("unassigned shift");
///
/// let roster = Roster {
/// shifts: vec![Shift { employee: 0 }, Shift { employee: usize::MAX }],
/// };
/// assert_eq!(constraint.evaluate(&roster), SoftScore::of(-1));
/// ```