solverforge_scoring/constraint/
incremental.rs1use crate::constraint::score_sum::checked;
6use std::fmt::Debug;
7use std::marker::PhantomData;
8
9use solverforge_core::score::Score;
10use solverforge_core::{ConstraintRef, ImpactType};
11
12use crate::api::analysis::{ConstraintJustification, DetailedConstraintMatch, EntityRef};
13use crate::api::constraint_set::IncrementalConstraint;
14
15pub struct IncrementalUniConstraint<S, A, E, F, W, Sc>
19where
20 Sc: Score,
21{
22 constraint_ref: ConstraintRef,
23 impact_type: ImpactType,
24 extractor: E,
25 filter: F,
26 weight: W,
27 is_hard: bool,
28 change_source: crate::stream::collection_extract::ChangeSource,
29 _phantom: PhantomData<(fn() -> S, fn() -> A, fn() -> Sc)>,
30}
31
32impl<S, A, E, F, W, Sc> IncrementalUniConstraint<S, A, E, F, W, Sc>
33where
34 S: Send + Sync + 'static,
35 A: Send + Sync + 'static,
36 E: crate::stream::collection_extract::CollectionExtract<S, Item = A>,
37 F: Fn(&S, &A) -> bool + Send + Sync,
38 W: Fn(&A) -> Sc + Send + Sync,
39 Sc: Score,
40{
41 pub fn new(
43 constraint_ref: ConstraintRef,
44 impact_type: ImpactType,
45 extractor: E,
46 filter: F,
47 weight: W,
48 is_hard: bool,
49 ) -> Self {
50 let change_source = extractor.change_source();
51 Self {
52 constraint_ref,
53 impact_type,
54 extractor,
55 filter,
56 weight,
57 is_hard,
58 change_source,
59 _phantom: PhantomData,
60 }
61 }
62
63 #[inline]
64 fn matches(&self, solution: &S, entity: &A) -> bool {
65 (self.filter)(solution, entity)
66 }
67
68 #[inline]
69 fn compute_delta(&self, entity: &A) -> Sc {
70 let base = (self.weight)(entity);
71 match self.impact_type {
72 ImpactType::Penalty => -base,
73 ImpactType::Reward => base,
74 }
75 }
76
77 #[inline]
78 fn compute_delta_overflowing(&self, entity: &A) -> (Sc, bool) {
79 let base = (self.weight)(entity);
80 match self.impact_type {
81 ImpactType::Penalty => base.overflowing_neg(),
82 ImpactType::Reward => (base, false),
83 }
84 }
85
86 #[inline]
87 fn reverse_delta_overflowing(&self, entity: &A) -> (Sc, bool) {
88 let base = (self.weight)(entity);
89 match self.impact_type {
90 ImpactType::Penalty => (base, false),
91 ImpactType::Reward => base.overflowing_neg(),
92 }
93 }
94}
95
96impl<S, A, E, F, W, Sc> IncrementalConstraint<S, Sc> for IncrementalUniConstraint<S, A, E, F, W, Sc>
97where
98 S: Send + Sync + 'static,
99 A: Debug + Send + Sync + 'static,
100 E: crate::stream::collection_extract::CollectionExtract<S, Item = A>,
101 F: Fn(&S, &A) -> bool + Send + Sync,
102 W: Fn(&A) -> Sc + Send + Sync,
103 Sc: Score,
104{
105 fn evaluate(&self, solution: &S) -> Sc {
106 let entities = self.extractor.extract(solution);
107 let mut total = Sc::zero();
108 for entity in entities {
109 if self.matches(solution, entity) {
110 total = total + self.compute_delta(entity);
111 }
112 }
113 total
114 }
115
116 fn match_count(&self, solution: &S) -> usize {
117 let entities = self.extractor.extract(solution);
118 entities
119 .iter()
120 .filter(|e| self.matches(solution, e))
121 .count()
122 }
123
124 fn initialize(&mut self, solution: &S) -> Sc {
125 self.evaluate(solution)
126 }
127
128 fn on_insert(&mut self, solution: &S, entity_index: usize, descriptor_index: usize) -> Sc {
129 checked(self.on_insert_overflowing(solution, entity_index, descriptor_index))
130 }
131
132 fn on_insert_overflowing(
133 &mut self,
134 solution: &S,
135 entity_index: usize,
136 descriptor_index: usize,
137 ) -> (Sc, bool) {
138 if !self.change_source.owns_descriptor(descriptor_index) {
139 return (Sc::zero(), false);
140 }
141 let entities = self.extractor.extract(solution);
142 if entity_index >= entities.len() {
143 return (Sc::zero(), false);
144 }
145 let entity = &entities[entity_index];
146 if self.matches(solution, entity) {
147 self.compute_delta_overflowing(entity)
148 } else {
149 (Sc::zero(), false)
150 }
151 }
152
153 fn on_retract(&mut self, solution: &S, entity_index: usize, descriptor_index: usize) -> Sc {
154 checked(self.on_retract_overflowing(solution, entity_index, descriptor_index))
155 }
156
157 fn on_retract_overflowing(
158 &mut self,
159 solution: &S,
160 entity_index: usize,
161 descriptor_index: usize,
162 ) -> (Sc, bool) {
163 if !self.change_source.owns_descriptor(descriptor_index) {
164 return (Sc::zero(), false);
165 }
166 let entities = self.extractor.extract(solution);
167 if entity_index >= entities.len() {
168 return (Sc::zero(), false);
169 }
170 let entity = &entities[entity_index];
171 if self.matches(solution, entity) {
172 self.reverse_delta_overflowing(entity)
173 } else {
174 (Sc::zero(), false)
175 }
176 }
177
178 fn reset(&mut self) {
179 }
181
182 fn name(&self) -> &str {
183 &self.constraint_ref.name
184 }
185
186 fn is_hard(&self) -> bool {
187 self.is_hard
188 }
189
190 fn constraint_ref(&self) -> &ConstraintRef {
191 &self.constraint_ref
192 }
193
194 fn get_matches<'a>(&'a self, solution: &S) -> Vec<DetailedConstraintMatch<'a, Sc>> {
195 let entities = self.extractor.extract(solution);
196 let cref = self.constraint_ref();
197 entities
198 .iter()
199 .filter(|e| self.matches(solution, e))
200 .map(|entity| {
201 let entity_ref =
202 EntityRef::new(entity).with_planning_id(self.extractor.planning_id(entity));
203 let justification = ConstraintJustification::new(vec![entity_ref]);
204 DetailedConstraintMatch::new(cref, self.compute_delta(entity), justification)
205 })
206 .collect()
207 }
208}
209
210impl<S, A, E, F, W, Sc: Score> std::fmt::Debug for IncrementalUniConstraint<S, A, E, F, W, Sc> {
211 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
212 f.debug_struct("IncrementalUniConstraint")
213 .field("name", &self.constraint_ref.name)
214 .field("impact_type", &self.impact_type)
215 .finish()
216 }
217}