solverforge-scoring 0.8.8

Incremental constraint scoring for SolverForge
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
453
454
455
456
457
458
459
460
461
use std::collections::HashMap;
use std::hash::Hash;
use std::marker::PhantomData;
use std::slice;

use solverforge_core::score::Score;
use solverforge_core::{ConstraintRef, ImpactType};

use crate::api::constraint_set::IncrementalConstraint;
use crate::stream::collection_extract::{ChangeSource, TrackedCollectionExtract};
use crate::stream::filter::UniFilter;
use crate::stream::{ExistenceMode, FlattenExtract};

#[derive(Debug, Clone)]
struct ASlot<K, Sc>
where
    Sc: Score,
{
    key: Option<K>,
    bucket_pos: usize,
    contribution: Sc,
}

impl<K, Sc> Default for ASlot<K, Sc>
where
    Sc: Score,
{
    fn default() -> Self {
        Self {
            key: None,
            bucket_pos: 0,
            contribution: Sc::zero(),
        }
    }
}

pub struct IncrementalExistsConstraint<S, A, P, B, K, EA, EP, KA, KB, FA, FP, Flatten, W, Sc>
where
    Sc: Score,
{
    constraint_ref: ConstraintRef,
    impact_type: ImpactType,
    mode: ExistenceMode,
    extractor_a: EA,
    extractor_parent: EP,
    key_a: KA,
    key_b: KB,
    filter_a: FA,
    filter_parent: FP,
    flatten: Flatten,
    weight: W,
    is_hard: bool,
    a_source: ChangeSource,
    parent_source: ChangeSource,
    a_slots: Vec<ASlot<K, Sc>>,
    a_indices_by_key: HashMap<K, Vec<usize>>,
    b_key_counts: HashMap<K, usize>,
    _phantom: PhantomData<(fn() -> S, fn() -> A, fn() -> P, fn() -> B)>,
}

impl<S, A, P, B, K, EA, EP, KA, KB, FA, FP, Flatten, W, Sc>
    IncrementalExistsConstraint<S, A, P, B, K, EA, EP, KA, KB, FA, FP, Flatten, W, Sc>
where
    S: 'static,
    A: Clone + 'static,
    P: Clone + 'static,
    B: Clone + 'static,
    K: Eq + Hash + Clone,
    EA: TrackedCollectionExtract<S, Item = A>,
    EP: TrackedCollectionExtract<S, Item = P>,
    KA: Fn(&A) -> K,
    KB: Fn(&B) -> K,
    FA: UniFilter<S, A>,
    FP: UniFilter<S, P>,
    Flatten: FlattenExtract<P, Item = B>,
    W: Fn(&A) -> Sc,
    Sc: Score,
{
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        constraint_ref: ConstraintRef,
        impact_type: ImpactType,
        mode: ExistenceMode,
        extractor_a: EA,
        extractor_parent: EP,
        key_a: KA,
        key_b: KB,
        filter_a: FA,
        filter_parent: FP,
        flatten: Flatten,
        weight: W,
        is_hard: bool,
    ) -> Self {
        let a_source = extractor_a.change_source();
        let parent_source = extractor_parent.change_source();
        Self {
            constraint_ref,
            impact_type,
            mode,
            extractor_a,
            extractor_parent,
            key_a,
            key_b,
            filter_a,
            filter_parent,
            flatten,
            weight,
            is_hard,
            a_source,
            parent_source,
            a_slots: Vec::new(),
            a_indices_by_key: HashMap::new(),
            b_key_counts: HashMap::new(),
            _phantom: PhantomData,
        }
    }

    #[inline]
    fn compute_score(&self, a: &A) -> Sc {
        let base = (self.weight)(a);
        match self.impact_type {
            ImpactType::Penalty => -base,
            ImpactType::Reward => base,
        }
    }

    #[inline]
    fn matches_existence(&self, key: &K) -> bool {
        let count = self.b_key_counts.get(key).copied().unwrap_or(0);
        match self.mode {
            ExistenceMode::Exists => count > 0,
            ExistenceMode::NotExists => count == 0,
        }
    }

    fn rebuild_b_counts(&mut self, solution: &S) {
        self.b_key_counts.clear();
        for parent in self.extractor_parent.extract(solution) {
            if !self.filter_parent.test(solution, parent) {
                continue;
            }
            for item in self.flatten.extract(parent) {
                *self.b_key_counts.entry((self.key_b)(item)).or_insert(0) += 1;
            }
        }
    }

    fn remove_a_from_bucket(&mut self, idx: usize, key: &K, bucket_pos: usize) {
        let mut remove_key = false;
        if let Some(bucket) = self.a_indices_by_key.get_mut(key) {
            let removed = bucket.swap_remove(bucket_pos);
            debug_assert_eq!(removed, idx);
            if bucket_pos < bucket.len() {
                let moved_idx = bucket[bucket_pos];
                self.a_slots[moved_idx].bucket_pos = bucket_pos;
            }
            remove_key = bucket.is_empty();
        }
        if remove_key {
            self.a_indices_by_key.remove(key);
        }
    }

    fn retract_a(&mut self, idx: usize) -> Sc {
        if idx >= self.a_slots.len() {
            return Sc::zero();
        }
        let slot = self.a_slots[idx].clone();
        let Some(key) = slot.key.clone() else {
            return Sc::zero();
        };
        self.remove_a_from_bucket(idx, &key, slot.bucket_pos);
        self.a_slots[idx] = ASlot::default();
        -slot.contribution
    }

    fn insert_a(&mut self, solution: &S, idx: usize) -> Sc {
        let entities_a = self.extractor_a.extract(solution);
        if idx >= entities_a.len() {
            return Sc::zero();
        }
        if self.a_slots.len() < entities_a.len() {
            self.a_slots.resize(entities_a.len(), ASlot::default());
        }

        let a = &entities_a[idx];
        if !self.filter_a.test(solution, a) {
            self.a_slots[idx] = ASlot::default();
            return Sc::zero();
        }

        let key = (self.key_a)(a);
        let bucket = self.a_indices_by_key.entry(key.clone()).or_default();
        let bucket_pos = bucket.len();
        bucket.push(idx);

        let contribution = if self.matches_existence(&key) {
            self.compute_score(a)
        } else {
            Sc::zero()
        };

        self.a_slots[idx] = ASlot {
            key: Some(key),
            bucket_pos,
            contribution,
        };
        contribution
    }

    fn reevaluate_key(&mut self, solution: &S, key: &K) -> Sc {
        let Some(indices) = self.a_indices_by_key.get(key).cloned() else {
            return Sc::zero();
        };
        let entities_a = self.extractor_a.extract(solution);
        let mut total = Sc::zero();
        let exists = self.matches_existence(key);

        for idx in indices {
            let a = &entities_a[idx];
            let new_contribution = if exists {
                self.compute_score(a)
            } else {
                Sc::zero()
            };
            let old_contribution = self.a_slots[idx].contribution;
            self.a_slots[idx].contribution = new_contribution;
            total = total + (new_contribution - old_contribution);
        }

        total
    }

    fn update_key_counts(
        &mut self,
        solution: &S,
        key_multiset: &HashMap<K, usize>,
        insert: bool,
    ) -> Sc {
        let mut total = Sc::zero();

        for (key, count) in key_multiset {
            if insert {
                *self.b_key_counts.entry(key.clone()).or_insert(0) += *count;
            } else {
                let mut remove_key = false;
                if let Some(entry) = self.b_key_counts.get_mut(key) {
                    *entry = entry.saturating_sub(*count);
                    remove_key = *entry == 0;
                }
                if remove_key {
                    self.b_key_counts.remove(key);
                }
            }
        }

        for key in key_multiset.keys() {
            total = total + self.reevaluate_key(solution, key);
        }

        total
    }

    fn parent_key_multiset(&self, solution: &S, idx: usize) -> HashMap<K, usize> {
        let parents = self.extractor_parent.extract(solution);
        if idx >= parents.len() {
            return HashMap::new();
        }
        let parent = &parents[idx];
        if !self.filter_parent.test(solution, parent) {
            return HashMap::new();
        }

        let mut multiset = HashMap::new();
        for item in self.flatten.extract(parent) {
            *multiset.entry((self.key_b)(item)).or_insert(0) += 1;
        }
        multiset
    }

    fn initialize_a_state(&mut self, solution: &S) -> Sc {
        self.a_slots.clear();
        self.a_indices_by_key.clear();

        let len = self.extractor_a.extract(solution).len();
        self.a_slots.resize(len, ASlot::default());

        let mut total = Sc::zero();
        for idx in 0..len {
            total = total + self.insert_a(solution, idx);
        }
        total
    }

    fn full_match_count(&self, solution: &S) -> usize {
        let mut key_counts = HashMap::<K, usize>::new();
        for parent in self.extractor_parent.extract(solution) {
            if !self.filter_parent.test(solution, parent) {
                continue;
            }
            for item in self.flatten.extract(parent) {
                *key_counts.entry((self.key_b)(item)).or_insert(0) += 1;
            }
        }

        self.extractor_a
            .extract(solution)
            .iter()
            .filter(|a| {
                self.filter_a.test(solution, a)
                    && match self.mode {
                        ExistenceMode::Exists => {
                            key_counts.get(&(self.key_a)(a)).copied().unwrap_or(0) > 0
                        }
                        ExistenceMode::NotExists => {
                            key_counts.get(&(self.key_a)(a)).copied().unwrap_or(0) == 0
                        }
                    }
            })
            .count()
    }
}

impl<S, A, P, B, K, EA, EP, KA, KB, FA, FP, Flatten, W, Sc> IncrementalConstraint<S, Sc>
    for IncrementalExistsConstraint<S, A, P, B, K, EA, EP, KA, KB, FA, FP, Flatten, W, Sc>
where
    S: Send + Sync + 'static,
    A: Clone + Send + Sync + 'static,
    P: Clone + Send + Sync + 'static,
    B: Clone + Send + Sync + 'static,
    K: Eq + Hash + Clone + Send + Sync,
    EA: TrackedCollectionExtract<S, Item = A> + Send + Sync,
    EP: TrackedCollectionExtract<S, Item = P> + Send + Sync,
    KA: Fn(&A) -> K + Send + Sync,
    KB: Fn(&B) -> K + Send + Sync,
    FA: UniFilter<S, A> + Send + Sync,
    FP: UniFilter<S, P> + Send + Sync,
    Flatten: FlattenExtract<P, Item = B> + Send + Sync,
    W: Fn(&A) -> Sc + Send + Sync,
    Sc: Score,
{
    fn evaluate(&self, solution: &S) -> Sc {
        let mut counts = HashMap::<K, usize>::new();
        for parent in self.extractor_parent.extract(solution) {
            if !self.filter_parent.test(solution, parent) {
                continue;
            }
            for item in self.flatten.extract(parent) {
                *counts.entry((self.key_b)(item)).or_insert(0) += 1;
            }
        }

        let mut total = Sc::zero();
        for a in self.extractor_a.extract(solution) {
            if !self.filter_a.test(solution, a) {
                continue;
            }
            let key = (self.key_a)(a);
            let matches = match self.mode {
                ExistenceMode::Exists => counts.get(&key).copied().unwrap_or(0) > 0,
                ExistenceMode::NotExists => counts.get(&key).copied().unwrap_or(0) == 0,
            };
            if matches {
                total = total + self.compute_score(a);
            }
        }
        total
    }

    fn match_count(&self, solution: &S) -> usize {
        self.full_match_count(solution)
    }

    fn initialize(&mut self, solution: &S) -> Sc {
        self.reset();
        self.rebuild_b_counts(solution);
        self.initialize_a_state(solution)
    }

    fn on_insert(&mut self, solution: &S, entity_index: usize, descriptor_index: usize) -> Sc {
        let a_changed =
            matches!(self.a_source, ChangeSource::Descriptor(idx) if idx == descriptor_index);
        let parent_changed =
            matches!(self.parent_source, ChangeSource::Descriptor(idx) if idx == descriptor_index);
        let same_source = self.a_source == self.parent_source && a_changed && parent_changed;

        let mut total = Sc::zero();
        if same_source {
            let keys = self.parent_key_multiset(solution, entity_index);
            total = total + self.update_key_counts(solution, &keys, true);
            total = total + self.insert_a(solution, entity_index);
            return total;
        }

        if parent_changed {
            let keys = self.parent_key_multiset(solution, entity_index);
            total = total + self.update_key_counts(solution, &keys, true);
        }
        if a_changed {
            total = total + self.insert_a(solution, entity_index);
        }
        total
    }

    fn on_retract(&mut self, solution: &S, entity_index: usize, descriptor_index: usize) -> Sc {
        let a_changed =
            matches!(self.a_source, ChangeSource::Descriptor(idx) if idx == descriptor_index);
        let parent_changed =
            matches!(self.parent_source, ChangeSource::Descriptor(idx) if idx == descriptor_index);
        let same_source = self.a_source == self.parent_source && a_changed && parent_changed;

        let mut total = Sc::zero();
        if same_source {
            let keys = self.parent_key_multiset(solution, entity_index);
            total = total + self.retract_a(entity_index);
            total = total + self.update_key_counts(solution, &keys, false);
            return total;
        }

        if a_changed {
            total = total + self.retract_a(entity_index);
        }
        if parent_changed {
            let keys = self.parent_key_multiset(solution, entity_index);
            total = total + self.update_key_counts(solution, &keys, false);
        }
        total
    }

    fn reset(&mut self) {
        self.a_slots.clear();
        self.a_indices_by_key.clear();
        self.b_key_counts.clear();
    }

    fn name(&self) -> &str {
        &self.constraint_ref.name
    }

    fn is_hard(&self) -> bool {
        self.is_hard
    }

    fn constraint_ref(&self) -> ConstraintRef {
        self.constraint_ref.clone()
    }
}

#[derive(Debug, Clone, Copy, Default)]
pub struct SelfFlatten;

impl<T> FlattenExtract<T> for SelfFlatten
where
    T: Send + Sync,
{
    type Item = T;

    fn extract<'a>(&self, parent: &'a T) -> &'a [T] {
        slice::from_ref(parent)
    }
}