const T: i64 = 5; const R: i64 = 3; const P: i64 = 1; const S: i64 = 0;
const MAX_DEPTH: i64 = 3;
const STEP_BUDGET: i64 = 1000;
#[derive(Clone, Copy, PartialEq)]
enum Act { C, D }
impl Act {
fn name(self) -> &'static str {
match self {
Act::C => "C",
Act::D => "D",
}
}
}
fn payoff_self(my_action: Act, other_action: Act) -> (i64, i64) {
match (my_action, other_action) {
(Act::C, Act::C) => (R, R),
(Act::C, Act::D) => (S, T),
(Act::D, Act::C) => (T, S),
(Act::D, Act::D) => (P, P),
}
}
enum Bot {
Cooperate,
Defect,
Fair,
Cupod { hist: Vec<&'static str> },
Prudent,
}
impl Bot {
fn name(&self) -> &'static str {
match self {
Bot::Cooperate => "CooperateBot_toy",
Bot::Defect => "DefectBot_toy",
Bot::Fair => "FairBot_toy",
Bot::Cupod { .. } => "CUPOD_toy",
Bot::Prudent => "PrudentBot_toy",
}
}
fn act(&mut self, other_source: &str) -> Act {
match self {
Bot::Cooperate => Act::C,
Bot::Defect => Act::D,
Bot::Fair => if other_source.contains("return \"C\"") { Act::C } else { Act::D },
Bot::Cupod { hist } => {
// Continuous Update of PD : coopere tant que l autre a coopere au tour precedent
if hist.is_empty() {
hist.push("C"); // premiere rencontre : ouverture cooperative
return Act::C;
}
if !other_source.is_empty() && other_source.contains("return \"D\"") && hist[hist.len() - 1] == "D" {
// defection repetee de l autre -> defection
hist.push("D");
return Act::D;
}
hist.push("C");
Act::C
}
Bot::Prudent => {
// Prudent : defaut D sauf si preuve de cooperation dans la source
if other_source.contains("return \"C\"") && other_source.to_lowercase().contains("defect") {
// l autre a une strategie mixte documentee -> ouverture prudente
Act::C
} else {
Act::D
}
}
}
}
}
fn _sources_of() -> Vec<(&'static str, &'static str)> {
vec![
("CooperateBot_toy", "return \"C\""),
("DefectBot_toy", "return \"D\""),
("FairBot_toy", "if 'return \"C\"' in other_source: return \"C\"\nreturn \"D\""),
("CUPOD_toy", "if not _hist: _hist.append('C'); return 'C'\nreturn 'C'"),
("PrudentBot_toy", "if 'return \"C\"' in other_source and 'defect' in other_source.lower(): return \"C\"\nreturn \"D\""),
]
}
fn _src_of(sources: &[(&'static str, &'static str)], name: &str) -> &'static str {
for (n, s) in sources {
if *n == name {
return s;
}
}
""
}
struct Metrics {
states_explored: i64,
elapsed_seconds: f64,
depth_reached: i64,
}
fn simulate_payoff(bot_a: &mut Bot, bot_b: &mut Bot, sources: &[(&'static str, &'static str)],
max_depth: i64, step_cap: i64) -> (Act, Act, (i64, i64), &'static str, Metrics) {
let src_a = _src_of(sources, bot_a.name());
let src_b = _src_of(sources, bot_b.name());
let mut states_explored: i64 = 0;
let mut prev: Option<(Act, Act)> = None;
let t0 = std::time::Instant::now();
let mut a = bot_a.act(src_b);
let mut b = bot_b.act(src_a);
states_explored += 2;
for _ in 0..std::cmp::min(max_depth - 1, step_cap - states_explored) {
if states_explored >= step_cap {
let m = Metrics {
states_explored,
elapsed_seconds: t0.elapsed().as_secs_f64(),
depth_reached: std::cmp::min(max_depth, states_explored / 2),
};
return (Act::D, Act::D, (P, P), "timeout", m);
}
a = bot_a.act(src_b);
b = bot_b.act(src_a);
states_explored += 2;
if let Some((prev_a, _)) = prev {
if a == b && b == prev_a {
break;
}
}
prev = Some((a, b));
}
let m = Metrics {
states_explored,
elapsed_seconds: t0.elapsed().as_secs_f64(),
depth_reached: std::cmp::min(max_depth, states_explored / 2),
};
(a, b, payoff_self(a, b), "ok", m)
}
fn py_float(x: f64) -> String {
if x.is_finite() && x == x.trunc() { format!("{:.1}", x) } else { format!("{}", x) }
}
fn py_tuple_f(t: (f64, f64)) -> String {
format!("({}, {})", py_float(t.0), py_float(t.1))
}
fn effective_payoff(payoff_tuple: (i64, i64), states: i64, epsilon: f64) -> (f64, f64) {
// Payoff net = payoff_brut - epsilon_xx * states_explored, par joueur.
// Defaut backward-compatible : un seul epsilon, facteurs 1.0/1.0
// (bot_a/bot_b absents a cet appel).
let factor_a = 1.0_f64;
let factor_b = 1.0_f64;
let cost_a = epsilon * factor_a * states as f64;
let cost_b = epsilon * factor_b * states as f64;
(payoff_tuple.0 as f64 - cost_a, payoff_tuple.1 as f64 - cost_b)
}
// Runner en bloc : aucune variable de type Bot ne persiste au top-level (evcxr).
{
let sources = _sources_of();
println!("{:>8} | {:>14} | {:>14} | {:>14} | {:>14} | {:>14} | {:>14} | {:>14}", "epsilon", "C,C", "C,D", "D,C", "D,D", "F,C", "P,C", "U,C");
println!("{}", "-".repeat(110));
// Un seul CUPOD partage pour les 5 epsilon : la memoire persiste
// (3 appels/duel x 5 eps = 15 pousses) -- la colonne U,C reste egale a C,C.
let mut cupod = Bot::Cupod { hist: Vec::new() };
for eps in [0.0_f64, 0.05, 0.1, 0.5, 1.0] {
let mut row: Vec<String> = Vec::new();
for duel in ["C,C", "C,D", "D,C", "D,D", "F,C", "P,C", "U,C"] {
// chaque duel sauf CUPOD recoit des instances fraiches
let (payoff, states) = match duel {
"C,C" => { let mut a = Bot::Cooperate; let mut b = Bot::Cooperate; let r = simulate_payoff(&mut a, &mut b, &sources, MAX_DEPTH, STEP_BUDGET); (r.2, r.4.states_explored) }
"C,D" => { let mut a = Bot::Cooperate; let mut b = Bot::Defect; let r = simulate_payoff(&mut a, &mut b, &sources, MAX_DEPTH, STEP_BUDGET); (r.2, r.4.states_explored) }
"D,C" => { let mut a = Bot::Defect; let mut b = Bot::Cooperate; let r = simulate_payoff(&mut a, &mut b, &sources, MAX_DEPTH, STEP_BUDGET); (r.2, r.4.states_explored) }
"D,D" => { let mut a = Bot::Defect; let mut b = Bot::Defect; let r = simulate_payoff(&mut a, &mut b, &sources, MAX_DEPTH, STEP_BUDGET); (r.2, r.4.states_explored) }
"F,C" => { let mut a = Bot::Fair; let mut b = Bot::Cooperate; let r = simulate_payoff(&mut a, &mut b, &sources, MAX_DEPTH, STEP_BUDGET); (r.2, r.4.states_explored) }
"P,C" => { let mut a = Bot::Prudent; let mut b = Bot::Cooperate; let r = simulate_payoff(&mut a, &mut b, &sources, MAX_DEPTH, STEP_BUDGET); (r.2, r.4.states_explored) }
_ => { let mut b = Bot::Cooperate; let r = simulate_payoff(&mut cupod, &mut b, &sources, MAX_DEPTH, STEP_BUDGET); (r.2, r.4.states_explored) }
};
let eff = effective_payoff(payoff, states, eps);
row.push(py_tuple_f(eff));
}
let cells_line = row.iter().map(|c| format!("{:>14}", c)).collect::<Vec<_>>().join(" | ");
println!("{:>8.2} | {}", eps, cells_line);
}
println!("\nCles de lecture : C=CooperateBot, D=DefectBot, F=FairBot (lit source), P=PrudentBot (preuve documentee), U=CUPOD (memoire interne).");
println!(" Chaque duel est execute contre CooperateBot_toy (adversaire toujours cooperatif).");
println!("Observation COHERENTE AVEC l intuition : a cout nul, (C,C) domine (D,D).");
println!("A cout eleve, le cout du raisonnement peut inverser l equilibre observe.");
};