solverforge-solver 0.18.0

Solver engine 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
//! Canonical execution of prepared construction nodes.
//!
//! This module consumes only frozen graph data and solve-owned prepared
//! sources.  It does not rediscover a model, open a declaration callback, or
//! assemble a second phase tree. Every list branch delegates to the one
//! shared construction kernel, including the distinct Clarke-Wright
//! savings/merge/completion path.

use std::fmt;

use solverforge_config::ConstructionHeuristicConfig;
use solverforge_core::domain::PlanningSolution;
use solverforge_core::score::{ParseableScore, Score};
use solverforge_scoring::Director;

use crate::builder::context::{ListConstructionKernelError, RuntimeListElement, SourceElement};
use crate::heuristic::selector::nearby_list_change::CrossEntityDistanceMeter;
use crate::phase::construction::{
    build_scalar_group_construction, record_scalar_assignment_remaining,
    scalar_group_work_remaining, FrozenRuntimeListConstructionSlot,
    FrozenScalarOrMixedConstruction, ScalarOrMixedSlotOrder,
};
use crate::phase::Phase;
use crate::scope::{ProgressCallback, SolverScope, StepControlPolicy};

use super::list_construction::{
    execute_runtime_list_cheapest_insertion, execute_runtime_list_clarke_wright,
    execute_runtime_list_k_opt, execute_runtime_list_regret_insertion,
    execute_runtime_list_round_robin,
};
use super::{
    ConstructionExecution, DefaultConstructionStageExecutionRecord,
    DefaultRuntimeConstructionExecution, PreparedConstruction, PreparedDefaultRuntime,
    PreparedListSlot, PreparedRuntimeExecution, ResolvedConstructionExecutionOutcome,
    ResolvedConstructionExecutionStep, RuntimeInstantiationError,
};

/// Executes one explicitly prepared construction node.
///
/// The caller supplies the mutable per-solve prepared execution. A reached
/// list stage binds its source index by compact catalog entry exactly once,
/// validates current assignments, then uses the exact unassigned set as its
/// no-work authority. It never clones payloads, rebuilds the source-key map,
/// or invokes an unreached declaration callback.
/// `Default` is deliberately not accepted here: default construction is a
/// staged graph transition and is resolved by the runtime runner only after
/// each preceding boundary has completed.
pub(crate) fn execute_prepared_construction<S, V, DM, IDM, Extension, D, ProgressCb>(
    execution: &mut PreparedRuntimeExecution<S, V, DM, IDM, Extension>,
    construction: &PreparedConstruction<S, V, DM, IDM>,
    required_only: bool,
    solver_scope: &mut SolverScope<'_, S, D, ProgressCb>,
) -> Result<ConstructionExecution, RuntimeInstantiationError>
where
    S: PlanningSolution + Clone + Send + Sync + 'static,
    S::Score: Score + Copy + Ord + ParseableScore,
    V: Clone + PartialEq + Into<usize> + Send + Sync + fmt::Debug + 'static,
    DM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter<S>,
    IDM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter<S>,
    D: Director<S>,
    ProgressCb: ProgressCallback<S>,
{
    let control_policy = StepControlPolicy::for_required_construction(required_only);
    match construction {
        PreparedConstruction::ScalarOrMixed {
            config,
            schedule,
            scalar_slots,
            list_slots,
            slot_order,
        } => execute_with_construction_termination(
            config,
            control_policy,
            solver_scope,
            |solver_scope| {
                if control_policy.should_terminate_construction(solver_scope) {
                    return Ok(false);
                }
                let mut active_list_slots = Vec::with_capacity(list_slots.len());
                for (old_index, prepared_slot) in list_slots.iter().copied().enumerate() {
                    let unassigned = execution
                        .current_list_source_work(prepared_slot, solver_scope.working_solution())?;
                    if !unassigned.is_empty() {
                        active_list_slots.push((old_index, prepared_slot));
                    }
                }
                let mut compact_list_indexes = vec![None; list_slots.len()];
                let frozen_list_slots = active_list_slots
                    .iter()
                    .enumerate()
                    .map(|(new_index, (old_index, prepared_slot))| {
                        compact_list_indexes[*old_index] = Some(new_index);
                        let (slot, source_index) = execution.bound_list_source(*prepared_slot);
                        FrozenRuntimeListConstructionSlot {
                            slot: slot.clone(),
                            source_index,
                        }
                    })
                    .collect::<Vec<_>>();
                let compact_slot_order = slot_order
                    .iter()
                    .filter_map(|entry| match entry {
                        ScalarOrMixedSlotOrder::Scalar { .. } => Some(*entry),
                        ScalarOrMixedSlotOrder::List {
                            list_index,
                            construction_slot_index,
                        } => compact_list_indexes[*list_index].map(|list_index| {
                            ScalarOrMixedSlotOrder::List {
                                list_index,
                                construction_slot_index: *construction_slot_index,
                            }
                        }),
                    })
                    .collect::<Vec<_>>();
                if scalar_slots.is_empty() && frozen_list_slots.is_empty() {
                    return Ok(false);
                }
                Ok(FrozenScalarOrMixedConstruction::new(
                    *schedule,
                    config.clone(),
                    scalar_slots.clone(),
                    frozen_list_slots,
                    compact_slot_order,
                )
                .solve(solver_scope))
            },
        ),
        PreparedConstruction::RoundRobin { config, slots } => {
            execute_with_construction_termination(
                config,
                control_policy,
                solver_scope,
                |solver_scope| {
                    execute_list_slots(
                        execution,
                        slots,
                        control_policy,
                        solver_scope,
                        execute_runtime_list_round_robin,
                    )
                },
            )
        }
        PreparedConstruction::CheapestInsertion { config, slots } => {
            execute_with_construction_termination(
                config,
                control_policy,
                solver_scope,
                |solver_scope| {
                    execute_list_slots(
                        execution,
                        slots,
                        control_policy,
                        solver_scope,
                        execute_runtime_list_cheapest_insertion,
                    )
                },
            )
        }
        PreparedConstruction::RegretInsertion { config, slots } => {
            execute_with_construction_termination(
                config,
                control_policy,
                solver_scope,
                |solver_scope| {
                    execute_list_slots(
                        execution,
                        slots,
                        control_policy,
                        solver_scope,
                        execute_runtime_list_regret_insertion,
                    )
                },
            )
        }
        PreparedConstruction::ClarkeWright { config, slots } => {
            execute_with_construction_termination(
                config,
                control_policy,
                solver_scope,
                |solver_scope| {
                    execute_list_slots(
                        execution,
                        slots,
                        control_policy,
                        solver_scope,
                        execute_runtime_list_clarke_wright,
                    )
                },
            )
        }
        PreparedConstruction::KOpt { config, slots } => execute_with_construction_termination(
            config,
            control_policy,
            solver_scope,
            |solver_scope| {
                for slot in slots {
                    if control_policy.should_terminate_construction(solver_scope) {
                        return Ok(false);
                    }
                    execute_runtime_list_k_opt(slot, config.k, control_policy, solver_scope);
                }
                Ok(!slots.is_empty())
            },
        ),
        PreparedConstruction::GroupedScalar {
            config,
            group_index,
            group,
            scalar_bindings,
        } => execute_with_construction_termination(
            config,
            control_policy,
            solver_scope,
            |solver_scope| {
                if control_policy.should_terminate_construction(solver_scope) {
                    return Ok(false);
                }
                if !scalar_group_work_remaining(group, solver_scope.working_solution()) {
                    return Ok(false);
                }
                record_scalar_assignment_remaining(group, solver_scope);
                let mut phase = build_scalar_group_construction(
                    Some(config),
                    *group_index,
                    group.clone(),
                    scalar_bindings.clone(),
                    required_only,
                );
                phase.solve(solver_scope);
                record_scalar_assignment_remaining(group, solver_scope);
                Ok(true)
            },
        ),
    }
}

/// Applies the configured construction termination overlay and captures its
/// outcome before the overlay is restored. This is the one place that turns a
/// child kernel's `ran` bit into an exact execution result, so callers never
/// mistake phase-local termination for an ordinary no-work result.
fn execute_with_construction_termination<S, D, ProgressCb>(
    config: &ConstructionHeuristicConfig,
    control_policy: StepControlPolicy,
    solver_scope: &mut SolverScope<'_, S, D, ProgressCb>,
    work: impl FnOnce(&mut SolverScope<'_, S, D, ProgressCb>) -> Result<bool, RuntimeInstantiationError>,
) -> Result<ConstructionExecution, RuntimeInstantiationError>
where
    S: PlanningSolution,
    S::Score: ParseableScore,
    D: Director<S>,
    ProgressCb: ProgressCallback<S>,
{
    solver_scope.with_phase_termination(config.termination.as_ref(), |solver_scope| {
        if control_policy.should_terminate_construction(solver_scope) {
            return Ok(ConstructionExecution::skipped_terminated());
        }
        let ran = work(solver_scope)?;
        if ran {
            Ok(ConstructionExecution::executed())
        } else if control_policy.should_terminate_construction(solver_scope) {
            Ok(ConstructionExecution::skipped_terminated())
        } else {
            Ok(ConstructionExecution::skipped_no_work())
        }
    })
}

/// Executes the staged omitted-construction profile through the same prepared
/// dispatcher as an explicitly configured construction phase.
///
/// Each boundary is resolved against the current working solution. Structural
/// list declarations were registered before phase execution, but a reached
/// source-backed child binds and validates its source before deciding whether
/// work remains. Stage resolution can therefore choose Clarke-Wright,
/// assignment, or K-opt without rebuilding model metadata. In particular, the
/// post-construction K-opt boundary observes routes only after preceding
/// construction stages.
pub(crate) fn execute_prepared_default_construction<S, V, DM, IDM, Extension, D, ProgressCb>(
    execution: &mut PreparedRuntimeExecution<S, V, DM, IDM, Extension>,
    default: &PreparedDefaultRuntime<S, V, DM, IDM>,
    solver_scope: &mut SolverScope<'_, S, D, ProgressCb>,
) -> Result<DefaultRuntimeConstructionExecution, RuntimeInstantiationError>
where
    S: PlanningSolution + Clone + Send + Sync + 'static,
    S::Score: Score + Copy + Ord + ParseableScore,
    V: Clone + PartialEq + Into<usize> + Send + Sync + fmt::Debug + 'static,
    DM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter<S>,
    IDM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter<S>,
    D: Director<S>,
    ProgressCb: ProgressCallback<S>,
{
    let mut record = DefaultRuntimeConstructionExecution::default();
    for stage in crate::runtime::compiler::DefaultPreconstructionStage::ORDERED {
        let resolved = super::super::defaults::resolve_default_preconstruction_stage(
            &default.bindings,
            stage,
            solver_scope.working_solution(),
        );
        record.push(execute_resolved_default_construction_plan(
            execution,
            default,
            &resolved,
            solver_scope,
        )?);
    }

    let resolved = super::super::defaults::resolve_default_postconstruction_kopt(
        &default.bindings,
        solver_scope.working_solution(),
    );
    record.push(execute_resolved_default_construction_plan(
        execution,
        default,
        &resolved,
        solver_scope,
    )?);
    Ok(record)
}

fn execute_resolved_default_construction_plan<S, V, DM, IDM, Extension, D, ProgressCb>(
    execution: &mut PreparedRuntimeExecution<S, V, DM, IDM, Extension>,
    default: &PreparedDefaultRuntime<S, V, DM, IDM>,
    resolved: &crate::runtime::compiler::ResolvedDefaultConstructionPlan<S, V, DM, IDM>,
    solver_scope: &mut SolverScope<'_, S, D, ProgressCb>,
) -> Result<DefaultConstructionStageExecutionRecord, RuntimeInstantiationError>
where
    S: PlanningSolution + Clone + Send + Sync + 'static,
    S::Score: Score + Copy + Ord + ParseableScore,
    V: Clone + PartialEq + Into<usize> + Send + Sync + fmt::Debug + 'static,
    DM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter<S>,
    IDM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter<S>,
    D: Director<S>,
    ProgressCb: ProgressCallback<S>,
{
    let mut steps = Vec::with_capacity(resolved.steps.len());
    for step in &resolved.steps {
        let construction =
            execution.prepare_resolved_construction(default.phase_index, &step.construction)?;
        let execution = execute_prepared_construction(
            execution,
            &construction,
            step.required_only,
            solver_scope,
        )?;
        steps.push(ResolvedConstructionExecutionStep {
            kind: step.kind,
            required_only: step.required_only,
            target: step.target.clone(),
            list_policies: step.list_policies.clone(),
            outcome: execution.outcome,
        });
    }
    let outcome = if steps
        .iter()
        .any(|step| step.outcome == ResolvedConstructionExecutionOutcome::Executed)
    {
        ResolvedConstructionExecutionOutcome::Executed
    } else if steps
        .iter()
        .any(|step| step.outcome == ResolvedConstructionExecutionOutcome::SkippedTerminated)
    {
        ResolvedConstructionExecutionOutcome::SkippedTerminated
    } else {
        ResolvedConstructionExecutionOutcome::SkippedNoWork
    };
    Ok(DefaultConstructionStageExecutionRecord {
        stage: resolved.stage,
        outcome,
        steps,
    })
}

/// Executes one configured list construction family in declaration order.
///
/// The callback type is monomorphized at the call site.  It is not a phase
/// factory or trait-object adapter: every invocation passes the exact
/// `RuntimeListSlot` and its borrowed prepared source to one concrete kernel.
fn execute_list_slots<S, V, DM, IDM, Extension, D, ProgressCb>(
    execution: &mut PreparedRuntimeExecution<S, V, DM, IDM, Extension>,
    slots: &[PreparedListSlot],
    control_policy: StepControlPolicy,
    solver_scope: &mut SolverScope<'_, S, D, ProgressCb>,
    execute: fn(
        &crate::builder::context::RuntimeListSlot<S, V, DM, IDM>,
        &crate::builder::context::RuntimeListSourceIndex<
            crate::builder::context::RuntimeListElement<V>,
        >,
        &[SourceElement<RuntimeListElement<V>>],
        StepControlPolicy,
        &mut SolverScope<'_, S, D, ProgressCb>,
    ) -> Result<(), ListConstructionKernelError>,
) -> Result<bool, RuntimeInstantiationError>
where
    S: PlanningSolution + Clone + Send + Sync + 'static,
    S::Score: Score + Copy + Ord + ParseableScore,
    V: Clone + PartialEq + Into<usize> + Send + Sync + fmt::Debug + 'static,
    DM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter<S>,
    IDM: Clone + Send + Sync + fmt::Debug + CrossEntityDistanceMeter<S>,
    D: Director<S>,
    ProgressCb: ProgressCallback<S>,
{
    let mut ran = false;
    for prepared_slot in slots {
        if control_policy.should_terminate_construction(solver_scope) {
            break;
        }
        let unassigned =
            execution.current_list_source_work(*prepared_slot, solver_scope.working_solution())?;
        if unassigned.is_empty() {
            continue;
        }
        let (slot, source_index) = execution.bound_list_source(*prepared_slot);
        execute(
            slot,
            source_index,
            &unassigned,
            control_policy,
            solver_scope,
        )
        .map_err(|error| RuntimeInstantiationError {
            phase_index: prepared_slot.phase_index,
            kind: super::RuntimeInstantiationErrorKind::SourceRefresh {
                target: slot.identity(),
                error,
            },
        })?;
        ran = true;
    }
    Ok(ran)
}