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
//! Partition-and-resolve control loop (the SGPlan core, adapted to numeric
//! STRIPS — see docs/sgplan6-spec.md §5,§9).
//!
//! Each outer iteration:
//! Phase A (PARALLEL, coarse): solve every group's subgoal from the INITIAL
//! state independently — one `ffdp` subplanner per group, run concurrently.
//! Phase B (sequential, validated compose): replay each group's subplan on the
//! evolving real state; if it no longer applies, re-solve from the current
//! state with the full data-parallel subplanner. Reject a step that breaks an
//! already-achieved sibling's subgoal.
//! Resolution: on any stuck/conflict, MERGE the offending group with a neighbor
//! (coarsen granularity) and retry. When merging collapses to one group the
//! subproblem IS the whole problem, i.e. a monolithic `ffdp` fallback — so
//! `sgp` is solvable exactly when `ffdp` is.
use crate::packed::{PackedTask, State};
use crate::par;
use crate::search::{solve_subgoal, solve_subgoal_avoiding};
use crate::partition::{interaction_partition, merge_at, merge_with_neighbor, Subgoal};
#[derive(Clone, Copy)]
pub struct Stats {
pub init_groups: usize,
pub final_groups: usize,
pub merges: usize,
pub fallback: bool, // collapsed to a single (monolithic) group
}
pub enum Solved {
Plan(Vec<usize>, Stats),
Unsolvable,
}
/// Does op-sequence `ops` apply from `state` and achieve `g`? (cheap replay).
fn replay_ok(task: &PackedTask, state: &State, ops: &[usize], g: &Subgoal) -> bool {
let mut s = state.clone();
for &oi in ops {
if !task.op_applicable(oi, &s) {
return false;
}
s = task.apply(oi, &s);
}
task.goal_met_with(&s, &g.pos, &g.num)
}
pub fn solve(
task: &PackedTask,
threads: usize,
cfg: crate::search::SearchCfg,
mutex_groups: &[Vec<u32>],
) -> Solved {
let init = task.initial();
// Seed from the goal-interaction graph over guidance variables (mutex groups);
// falls back to the finest partition when no groups are supplied.
let mut groups = interaction_partition(task, mutex_groups);
let init_groups = groups.len();
let mut merges = 0usize;
loop {
let monolithic = groups.len() == 1;
// Phase A — coarse parallel: solve each group from the initial state.
// One thread per subplanner when there are many groups (coarse
// parallelism); all threads for the single monolithic fallback.
let sub_threads = if monolithic { threads } else { 1 };
let subplans: Vec<Option<Vec<usize>>> = par::par_map(&groups, threads, |g| {
if g.is_empty() {
Some(Vec::new())
} else {
solve_subgoal(task, &init, &g.pos, &g.num, sub_threads, cfg)
}
});
// A group unsolvable in isolation → it likely needs a sibling's effects
// first; merge and retry (or, if monolithic, genuinely unsolvable).
if let Some(i) = subplans.iter().position(|s| s.is_none()) {
if monolithic {
return Solved::Unsolvable;
}
merge_with_neighbor(&mut groups, i);
merges += 1;
continue;
}
// Phase B — sequential validated composition on the evolving state.
let mut state = init.clone();
let mut plan: Vec<usize> = Vec::new();
let mut done = vec![false; groups.len()];
// (stuck group, the specific sibling it broke if any)
let mut conflict: Option<(usize, Option<usize>)> = None;
for i in 0..groups.len() {
if task.goal_met_with(&state, &groups[i].pos, &groups[i].num) {
done[i] = true;
continue; // already achieved (e.g. by a sibling's subplan)
}
// Protect already-achieved siblings: forbid ops that would delete one
// of their goal facts (the ESPC hard-constraint / ∞-penalty form). If
// the subgoal is solvable under that protection it provably can't break
// a sibling; only if it's infeasible do we relax and let the conflict
// check below trigger a merge.
let protected: crate::hash::FxHashSet<u32> = (0..i)
.filter(|&j| done[j])
.flat_map(|j| groups[j].pos.iter().copied())
.collect();
let forbidden: Vec<bool> = if protected.is_empty() {
Vec::new()
} else {
(0..task.n_ops)
.map(|oi| task.del.slice(oi).iter().any(|f| protected.contains(f)))
.collect()
};
let pre = subplans[i].as_ref().unwrap();
let ops = if protected.is_empty() && replay_ok(task, &state, pre, &groups[i]) {
pre.clone() // no siblings to protect yet — reuse the from-init plan
} else {
let protected_solve = solve_subgoal_avoiding(
task,
&state,
&groups[i].pos,
&groups[i].num,
&forbidden,
threads,
cfg,
);
match protected_solve.or_else(|| {
solve_subgoal(task, &state, &groups[i].pos, &groups[i].num, threads, cfg)
}) {
Some(o) => o,
None => {
conflict = Some((i, None));
break;
}
}
};
// apply, but reject if it breaks an already-achieved sibling
let mut ns = state.clone();
for &oi in &ops {
ns = task.apply(oi, &ns);
}
let breaker = (0..i)
.find(|&j| done[j] && !task.goal_met_with(&ns, &groups[j].pos, &groups[j].num));
if let Some(j) = breaker {
conflict = Some((i, Some(j)));
break;
}
state = ns;
plan.extend(ops);
done[i] = true;
}
if conflict.is_none() && task.goal_met_with(&state, &task.goal_pos, &task.goal_num) {
return Solved::Plan(
plan,
Stats {
init_groups,
final_groups: groups.len(),
merges,
fallback: monolithic,
},
);
}
// Resolve: coalesce the actual conflicting pair (semantic merge); else the
// stuck group with a neighbor.
if monolithic {
// single group already = whole goal but compose didn't satisfy it:
// treat as unsolvable (matches ffdp on the full problem).
return Solved::Unsolvable;
}
let last = groups.len() - 1;
match conflict {
Some((i, Some(j))) => merge_at(&mut groups, i, j),
Some((i, None)) => merge_with_neighbor(&mut groups, i),
None => merge_with_neighbor(&mut groups, last),
};
merges += 1;
}
}