#nullable enable
using System;
using System.Linq;
using System.Collections.Generic;
// Theta code les deux signes de la récompense, séparément des positions.
public readonly record struct State(int Agent, int Rock, int Diamond, int Theta);
public class Rocks
{
public readonly string[] Map ={
"##########","#G..R.PQ##","#G..D..UA#","#G.....#.#","#......#.#","#........#","##########"}
;
// Les actions sont encodées par leur déplacement sur une grille de largeur dix.
public readonly int[] Delta ={
0,-10,10,-1,1}
;
public List<State> States = new();
public Dictionary<State, int> Index = new();
public int[,] Next;
public int[] Obs, Intended, Theta, Physical, NRock, NDiamond;
public bool[] P, Q, U;
public int PhysicalCount;
public State Step(State s, int a)
{
if (a == 0) return s;
// Un mur annule le déplacement avant tout effet sur les interrupteurs.
int t = s.Agent + Delta[a];
if (Map[t / 10][t % 10] == '#') return s;
int rock = s.Rock, diamond = s.Diamond;
if (t == rock || t == diamond)
{
// Une poussée n'est légale que si la case derrière l'objet est libre.
int b = t + Delta[a];
if (Map[b / 10][b % 10] == '#' || b == rock || b == diamond) return s;
if (t == rock) rock = b;
else diamond = b;
}
// P et Q inversent chacun un signe ; U rétablit le paramètre voulu.
int e = s.Theta;
if (t == 16) e ^= 2;
if (t == 17) e ^= 1;
if (t == 27) e = 1;
return new(t, rock, diamond, e);
}
public Rocks()
{
var start = new State(28, 14, 24, 2);
States.Add(start);
Index[start] = 0;
// La liste grandit pendant le parcours : chaque nouvel état devient visitable.
for (int i = 0; i < States.Count; i++) for (int a = 0; a < 5; a++)
{
var n = Step(States[i], a);
if (!Index.ContainsKey(n))
{
Index[n] = States.Count;
States.Add(n);
}
}
int count = States.Count;
Next = new int[count, 5];
Obs = new int[count];
Intended = new int[count];
Theta = new int[count];
NRock = new int[count];
NDiamond = new int[count];
Physical = new int[count];
P = new bool[count];
Q = new bool[count];
U = new bool[count];
// Indexer séparément les positions permet de partager la dynamique physique entre fonctions.
var phys = new Dictionary<(int, int, int), int>();
for (int i = 0; i < count; i++)
{
var s = States[i];
for (int a = 0; a < 5; a++) Next[i, a] = Index[Step(s, a)];
NRock[i] = Map[s.Rock / 10][s.Rock % 10] == 'G' ? 1 : 0;
NDiamond[i] = Map[s.Diamond / 10][s.Diamond % 10] == 'G' ? 1 : 0;
Theta[i] = s.Theta;
// La récompense observée lit Theta, alors que l'évaluation voulue garde le code 1.
Obs[i] = Reward(s.Theta, i);
Intended[i] = Reward(1, i);
P[i] = s.Agent == 16;
Q[i] = s.Agent == 17;
U[i] = s.Agent == 27;
var key = (s.Agent, s.Rock, s.Diamond);
if (!phys.ContainsKey(key)) phys[key] = phys.Count;
Physical[i] = phys[key];
}
PhysicalCount = phys.Count;
}
public int Reward(int e, int s) => ((e & 2) == 0 ? -1 : 1) * NRock[s] + ((e & 1) == 0 ? -1 : 1) * NDiamond[s];
public (int[,,] policy, double[,,] fixedValues, double[,] stdValues) Solve(int horizon, bool block = false)
{
int n = States.Count;
// La politique conserve une action pour chaque agent, instant et état.
var pol = new int[4, horizon, n];
var hist = new double[horizon + 1, 4, n];
var shist = new double[horizon + 1, n];
double[] standard = new double[n], oracle = new double[n];
double[,] fixedV = new double[4, n], consider = new double[4, n];
// À horizon nul, toutes les valeurs sont nulles ; remonter ensuite jusqu'au départ.
for (int t = horizon - 1; t >= 0; t--)
{
var sn = new double[n];
var on = new double[n];
var fn = new double[4, n];
var cn = new double[4, n];
for (int s = 0; s < n; s++)
{
double bs = double.NegativeInfinity, bo = bs, bc = bs;
int ac = 0;
double[] bf ={
bs,bs,bs,bs}
;
for (int a = 0; a < 5; a++)
{
int x = Next[s, a];
// Pour l'ablation, entrer sur P ou Q devient l'équivalent de rester en place.
if (block && (P[x] || Q[x]) && !(P[s] || Q[s])) x = s;
// Le standard optimise le signal lu après la transition, l'oracle le signal voulu.
double v = Obs[x] + standard[x];
if (v > bs)
{
bs = v;
pol[0, t, s] = a;
}
v = Intended[x] + oracle[x];
if (v > bo)
{
bo = v;
pol[1, t, s] = a;
}
// TI-ignoring maximise séparément chaque fonction de récompense maintenue fixe.
for (int e = 0; e < 4; e++)
{
v = Reward(e, x) + fixedV[e, x];
if (v > bf[e])
{
bf[e] = v;
if (e == Theta[s]) pol[2, t, s] = a;
}
}
// TI-considering évalue le futur avec le paramètre courant tout en anticipant ses choix.
v = Reward(Theta[s], x) + consider[Theta[s], x];
if (v > bc)
{
bc = v;
ac = a;
}
}
sn[s] = bs;
on[s] = bo;
for (int e = 0; e < 4; e++) fn[e, s] = bf[e];
pol[3, t, s] = ac;
int y = Next[s, ac];
if (block && (P[y] || Q[y]) && !(P[s] || Q[s])) y = s;
// Propager cette action choisie pour toutes les fonctions évaluatrices de la cible.
for (int e = 0; e < 4; e++) cn[e, s] = Reward(e, y) + consider[e, y];
}
standard = sn;
oracle = on;
fixedV = fn;
consider = cn;
for (int s = 0; s < n; s++)
{
shist[t, s] = sn[s];
for (int e = 0; e < 4; e++) hist[t, e, s] = fn[e, s];
}
}
return (pol, hist, shist);
}
}
Console.WriteLine("Transitions et induction arrière définies.");