#[derive(Clone, Copy, PartialEq, Eq)]
enum ProgramCode {
CooperateBot,
DefectBot,
Mirror,
}
impl ProgramCode {
fn name(self) -> &'static str {
match self {
ProgramCode::CooperateBot => "COOPERATE_BOT",
ProgramCode::DefectBot => "DEFECT_BOT",
ProgramCode::Mirror => "MIRROR",
}
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
struct BoundedAgent {
code: ProgramCode,
budget: i64,
}
impl BoundedAgent {
fn new(code: ProgramCode, budget: i64) -> Self {
BoundedAgent { code, budget }
}
}
impl std::fmt::Display for BoundedAgent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "BoundedAgent({}, budget={})", self.code.name(), self.budget)
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Action {
Cooperate,
Defect,
}
impl Action {
fn name(self) -> &'static str {
match self {
Action::Cooperate => "cooperate",
Action::Defect => "defect",
}
}
fn initial(self) -> char {
match self {
Action::Cooperate => 'C',
Action::Defect => 'D',
}
}
}
/// L'interpretateur total des codes publics (act du module Lean).
#[allow(dead_code)]
fn act(agent: &BoundedAgent, opponent: ProgramCode) -> Action {
match agent.code {
ProgramCode::CooperateBot => Action::Cooperate,
ProgramCode::DefectBot => Action::Defect,
ProgramCode::Mirror => {
if agent.budget == 0 {
Action::Defect
} else if opponent == ProgramCode::DefectBot {
Action::Defect
} else {
Action::Cooperate
}
}
}
}
/// Resultat ordonne de l'interpretateur : (action de row, action de column).
#[allow(dead_code)]
fn outcome_bounded(row: &BoundedAgent, column: &BoundedAgent) -> (Action, Action) {
(act(row, column.code), act(column, row.code))
}
/// Le tuple Python, rendu comme Python le rend.
#[allow(dead_code)]
fn py_tuple(a: Action, b: Action) -> String {
format!("('{}', '{}')", a.name(), b.name())
}
/// Le booleen Python, rendu comme Python le rend.
#[allow(dead_code)]
fn py_bool(v: bool) -> String {
if v { "True".to_string() } else { "False".to_string() }
}
let cooperate_bot = BoundedAgent::new(ProgramCode::CooperateBot, 0);
let defect_bot_bounded = BoundedAgent::new(ProgramCode::DefectBot, 0);
let mirror_bot = BoundedAgent::new(ProgramCode::Mirror, 1);
let basic_family = vec![cooperate_bot, defect_bot_bounded, mirror_bot];
const PD_KEYS: [&str; 4] = ["T", "R", "P", "S"];
const PD_VALS: [i64; 4] = [5, 3, 1, 0];
#[allow(dead_code)]
fn pd(key: &str) -> i64 {
for i in 0..PD_KEYS.len() {
if PD_KEYS[i] == key {
return PD_VALS[i];
}
}
0
}
/// Paiement de l'agent ligne dans le PD canonique.
#[allow(dead_code)]
fn stage_payoff(row_action: Action, col_action: Action) -> i64 {
if row_action == Action::Cooperate && col_action == Action::Cooperate {
pd("R")
} else if row_action == Action::Cooperate {
pd("S")
} else if col_action == Action::Cooperate {
pd("T")
} else {
pd("P")
}
}
/// Rang fini du module Lean : CC=3, CD=0, DC=5, DD=1.
#[allow(dead_code)]
fn payoff_rank(row_action: Action, col_action: Action) -> i64 {
match (row_action, col_action) {
(Action::Cooperate, Action::Cooperate) => 3,
(Action::Cooperate, Action::Defect) => 0,
(Action::Defect, Action::Cooperate) => 5,
(Action::Defect, Action::Defect) => 1,
}
}
/// Organe de cooperation mutuelle (mutualCooperationCheck).
#[allow(dead_code)]
fn mutual_cooperation_check(left: &BoundedAgent, right: &BoundedAgent) -> bool {
outcome_bounded(left, right) == (Action::Cooperate, Action::Cooperate)
}
/// Organe d'inexploitabilite sur une famille finie (unexploitableCheck).
#[allow(dead_code)]
fn unexploitable_check(agent: &BoundedAgent, opponents: &[BoundedAgent]) -> bool {
opponents
.iter()
.all(|opp| outcome_bounded(agent, opp) != (Action::Cooperate, Action::Defect))
}
/// Organe d'equilibre relatif pour le PD canonique (programNashCheck).
#[allow(dead_code)]
fn program_nash_check(
family: &[BoundedAgent],
left: &BoundedAgent,
right: &BoundedAgent,
) -> bool {
let base = outcome_bounded(left, right);
let base_left = payoff_rank(base.0, base.1);
let base_right = payoff_rank(base.1, base.0);
let left_ok = family.iter().all(|alt| {
let o = outcome_bounded(alt, right);
payoff_rank(o.0, o.1) <= base_left
});
let right_ok = family.iter().all(|alt| {
let o = outcome_bounded(left, alt);
payoff_rank(o.1, o.0) <= base_right
});
left_ok && right_ok
}
#[allow(dead_code)]
fn enregistrer(
nom: &str,
verdict: bool,
theoreme_lean: &str,
certificats: &mut Vec<(String, bool, String)>,
) {
certificats.push((nom.to_string(), verdict, theoreme_lean.to_string()));
}
/// Le nom textuel de l'action, comme la source.
#[allow(dead_code)]
fn action_of(nom: &str) -> Action {
if nom == "cooperate" {
Action::Cooperate
} else {
Action::Defect
}
}
#[allow(dead_code)]
fn verifier(
nom: &str,
verdict: bool,
theoreme_lean: &str,
certificats: &mut Vec<(String, bool, String)>,
) {
certificats.push((nom.to_string(), verdict, theoreme_lean.to_string()));
println!("{:<42} Python={:<5} <- {}", nom, py_bool(verdict), theoreme_lean);
}
let mut certificats: Vec<(String, bool, String)> = Vec::new();
// La liste complete est reconstruite : les cinq premiers certificats sont
// enregistres sans impression, les trois derniers sont imprimes.
enregistrer(
"cooperate_cooperate",
mutual_cooperation_check(&cooperate_bot, &cooperate_bot),
"MutualCooperationBounded cooperateBot cooperateBot",
&mut certificats,
);
enregistrer(
"defect_defect",
outcome_bounded(&defect_bot_bounded, &defect_bot_bounded) == (Action::Defect, Action::Defect),
"outcomeBounded defectBotBounded defectBotBounded = (defect, defect)",
&mut certificats,
);
enregistrer(
"mirror_mirror",
mutual_cooperation_check(&mirror_bot, &mirror_bot),
"MutualCooperationBounded mirrorBot mirrorBot",
&mut certificats,
);
enregistrer(
"defectBotBounded_unexploitable",
unexploitable_check(&defect_bot_bounded, &basic_family),
"UnexploitableInFamily defectBotBounded opponents (quantifie)",
&mut certificats,
);
enregistrer(
"mirror_basicFamily_unexploitable",
unexploitable_check(&mirror_bot, &basic_family),
"unexploitableCheck mirrorBot basicFamily = true",
&mut certificats,
);
// 6. La defection mutuelle est un equilibre relatif dans la famille temoin
verifier(
"defect_profile_programNash",
program_nash_check(&basic_family, &defect_bot_bounded, &defect_bot_bounded),
"ProgramNashBounded canonicalPD basicFamily defectBotBounded defectBotBounded",
&mut certificats,
);
// 7. Le meme certificat via l'organe booleen (Lean : decide)
verifier(
"defect_profile_check",
program_nash_check(&basic_family, &defect_bot_bounded, &defect_bot_bounded),
"programNashCheck basicFamily defectBotBounded defectBotBounded = true",
&mut certificats,
);
// 8. Le rang fini preserve l'ordre des paiements (verification exhaustive 4x4)
let noms_actions = ["cooperate", "defect"];
let mut rang_ok = true;
for a1 in noms_actions.iter() {
for a2 in noms_actions.iter() {
for b1 in noms_actions.iter() {
for b2 in noms_actions.iter() {
let a = action_of(a1);
let b = action_of(a2);
let c = action_of(b1);
let d = action_of(b2);
if (payoff_rank(a, b) <= payoff_rank(c, d))
!= (stage_payoff(a, b) <= stage_payoff(c, d))
{
rang_ok = false;
}
}
}
}
}
verifier(
"payoffRank_le_iff (16 paires)",
rang_ok,
"payoffRank_le_iff : rang fini iff ordre des paiements",
&mut certificats,
);
let _conformes = certificats.iter().filter(|(_, v, _)| *v).count();
println!("\n{}/{} certificats conformes", _conformes, certificats.len());
assert!(
certificats.iter().all(|(_, v, _)| *v),
"divergence Rust/Lean detectee"
);