use std::collections::BTreeSet;
#[allow(dead_code)]
struct Af {
args: Vec<&'static str>,
attacks: BTreeSet<(&'static str, &'static str)>,
}
#[allow(dead_code)]
impl Af {
fn attackers(&self, x: &str) -> BTreeSet<&'static str> {
self.attacks.iter().filter(|(_, b)| *b == x).map(|(a, _)| *a).collect()
}
}
fn py_bool(b: bool) -> &'static str {
if b { "True" } else { "False" }
}
fn is_conflict_free(af: &Af, s: &BTreeSet<&str>) -> bool {
for (a, b) in &af.attacks {
if s.contains(a) && s.contains(b) {
return false;
}
}
true
}
fn defeats(af: &Af, s: &BTreeSet<&str>, x: &str) -> bool {
s.iter().any(|a| af.attacks.contains(&(*a, x)))
}
fn defends(af: &Af, s: &BTreeSet<&str>, x: &str) -> bool {
af.attackers(x).iter().all(|b| defeats(af, s, b))
}
fn is_admissible(af: &Af, s: &BTreeSet<&str>) -> bool {
is_conflict_free(af, s) && s.iter().all(|a| defends(af, s, a))
}
fn powerset(args: &[&'static str]) -> Vec<Vec<&'static str>> {
fn rec(args: &[&'static str], start: usize, k: usize, cur: &[&'static str], out: &mut Vec<Vec<&'static str>>) {
if k == 0 {
out.push(cur.to_vec());
return;
}
for i in start..=(args.len() - k) {
let mut next = cur.to_vec();
next.push(args[i]);
rec(args, i + 1, k - 1, &next, out);
}
}
let mut out = Vec::new();
for r in 0..=args.len() {
rec(args, 0, r, &[], &mut out);
}
out
}
fn grounded(af: &Af) -> BTreeSet<&'static str> {
let mut s: BTreeSet<&'static str> = BTreeSet::new();
let mut changed = true;
while changed {
changed = false;
for x in &af.args {
if !s.contains(x) && defends(af, &s, x) {
s.insert(x);
changed = true;
}
}
}
s
}
fn preferred(af: &Af) -> Vec<BTreeSet<&'static str>> {
let adm: Vec<BTreeSet<&'static str>> = powerset(&af.args)
.into_iter()
.map(|v| v.into_iter().collect())
.filter(|s| is_admissible(af, s))
.collect();
let mut out = Vec::new();
for a in &adm {
let maximal = !adm.iter().any(|b| a.is_subset(b) && b != a);
if maximal {
out.push(a.clone());
}
}
out
}
fn stable(af: &Af) -> Vec<BTreeSet<&'static str>> {
let mut out = Vec::new();
for v in powerset(&af.args) {
let s: BTreeSet<&'static str> = v.into_iter().collect();
if is_admissible(af, &s) && af.args.iter().all(|x| s.contains(x) || defeats(af, &s, x)) {
out.push(s);
}
}
out
}
/// Affichage compact d'un ensemble d'arguments : {a,d} (le vide rend {}).
fn fmt(e: &BTreeSet<&str>) -> String {
format!("{{{}}}", e.iter().copied().collect::<Vec<_>>().join(","))
}
/// S ∪ S+ : la portée de S — les arguments acceptés par S ou attaqués par S.
fn range_of(af: &Af, s: &BTreeSet<&'static str>) -> BTreeSet<&'static str> {
let mut r: BTreeSet<&'static str> = s.clone();
for (a, b) in &af.attacks {
if s.contains(a) {
r.insert(b);
}
}
r
}
/// Extensions semi-stables : les admissibles dont la portée S ∪ S+ est MAXIMALE POUR
/// L'INCLUSION — la portée d'aucun autre admissible ne la contient strictement (Caminada 2006).
/// Ce n'est pas « la portée de plus grande taille » : voir le contre-exemple ci-dessous, où {1}
/// (portée de 4 arguments) et {2,3} (portée de 5) sont semi-stables.
fn semi_stable(af: &Af) -> Vec<BTreeSet<&'static str>> {
let adm: Vec<BTreeSet<&'static str>> = powerset(&af.args)
.into_iter()
.map(|v| v.into_iter().collect())
.filter(|s| is_admissible(af, s))
.collect();
let rng: Vec<BTreeSet<&'static str>> = adm.iter().map(|s| range_of(af, s)).collect();
let mut out = Vec::new();
for (i, s) in adm.iter().enumerate() {
// maximale pour l'inclusion : aucune autre portée ne la contient strictement
let domine = rng.iter().enumerate().any(|(j, r)| j != i && rng[i].is_subset(r) && rng[i] != *r);
if !domine {
out.push(s.clone());
}
}
out
}
fn fmt_ext_list(items: &[String]) -> String {
format!("[{}]", items.iter().map(|e| format!("'{}'", e)).collect::<Vec<_>>().join(", "))
}
// Retour sur l'AF canonique af1 — là où la sémantique stable n'existe pas.
let af1 = Af {
args: vec!["c", "b", "a", "d"],
attacks: [("a", "b"), ("b", "a"), ("c", "c")].iter().copied().collect(),
};
let s1 = stable(&af1);
let p1 = preferred(&af1);
let ss1 = semi_stable(&af1);
println!("=== af1 : le défaut et sa réparation ===");
let stable_str = if s1.is_empty() {
"AUCUNE".to_string()
} else {
fmt_ext_list(&s1.iter().map(fmt).collect::<Vec<_>>())
};
println!(" stable : {}", stable_str);
let ss_strs: Vec<String> = ss1.iter().map(fmt).collect();
println!(" semi-stable : {} ({} ext.)", fmt_ext_list(&ss_strs), ss1.len());
println!();
println!("semi-stable existe là où stable n'existe pas : {}", py_bool(!ss1.is_empty() && s1.is_empty()));
let ss_set: BTreeSet<BTreeSet<&str>> = ss1.iter().cloned().collect();
let p_set: BTreeSet<BTreeSet<&str>> = p1.iter().cloned().collect();
println!("sur af1, semi-stable == preferred : {}", py_bool(ss_set == p_set));
println!();
// Contre-exemple : deux portées INCOMPARABLES, donc deux semi-stables.
let af_cx = Af {
args: vec!["0", "1", "2", "3", "4", "5"],
attacks: [
("0", "0"), ("0", "4"),
("1", "2"), ("1", "3"), ("1", "5"),
("2", "0"), ("2", "1"), ("2", "4"),
("4", "0"), ("4", "3"), ("4", "4"),
("5", "4"), ("5", "5"),
].iter().copied().collect(),
};
let ss_cx = semi_stable(&af_cx);
let mut attendu_cx: BTreeSet<BTreeSet<&str>> = BTreeSet::new();
attendu_cx.insert(["1"].iter().copied().collect());
attendu_cx.insert(["2", "3"].iter().copied().collect());
println!("=== contre-exemple : deux portées incomparables, deux semi-stables ===");
println!(" admissible -> portée S ∪ S+");
let adm_cx: Vec<BTreeSet<&'static str>> = powerset(&af_cx.args)
.into_iter().map(|v| v.into_iter().collect()).filter(|s| is_admissible(&af_cx, s)).collect();
for s in &adm_cx {
println!(" {:<10} -> {}", fmt(s), fmt(&range_of(&af_cx, s)));
}
let ss_set_cx: BTreeSet<BTreeSet<&str>> = ss_cx.iter().cloned().collect();
let mut ss_cx_trie: Vec<String> = ss_cx.iter().map(fmt).collect();
ss_cx_trie.sort();
println!(" semi-stable : {} ({} ext.)", fmt_ext_list(&ss_cx_trie), ss_cx.len());
let cx_ok = ss_set_cx == attendu_cx;
println!(" attendu ['{{1}}', '{{2,3}}'] vs calculé {} : {}", fmt_ext_list(&ss_cx_trie), if cx_ok { "CONFORME" } else { "ECART" });
assert!(cx_ok, "semi-stable doit rendre {{1}} et {{2,3}} : la maximalité est pour l'inclusion, pas pour la taille");