// Les deux tests énumèrent respectivement les paires ordonnées et les rectangles stricts.
IEnumerable<(int, int)> Grid(int n, int m) =>
from a in Enumerable.Range(0, n)
from b in Enumerable.Range(0, m)
select (a, b);
(int, int) Meet((int, int) x, (int, int) y) =>
(Math.Min(x.Item1, y.Item1), Math.Min(x.Item2, y.Item2));
(int, int) Join((int, int) x, (int, int) y) =>
(Math.Max(x.Item1, y.Item1), Math.Max(x.Item2, y.Item2));
bool Leq((int, int) x, (int, int) y) => x.Item1 <= y.Item1 && x.Item2 <= y.Item2;
(int Count, List<((int, int), (int, int), double, double)> Violations) Supermodular(double[,] u)
{
var errors = new List<((int, int), (int, int), double, double)>();
foreach (var x in Grid(u.GetLength(0), u.GetLength(1)))
foreach (var y in Grid(u.GetLength(0), u.GetLength(1)))
{
var lo = Meet(x, y);
var hi = Join(x, y);
double lhs = u[lo.Item1, lo.Item2] + u[hi.Item1, hi.Item2];
double rhs = u[x.Item1, x.Item2] + u[y.Item1, y.Item2];
if (lhs < rhs - 1e-12)
errors.Add((x, y, lhs, rhs));
}
return (errors.Count, errors);
}
(int Count, List<((int, int), (int, int), double, double)> Violations) IncreasingDifferences(
double[,] u
)
{
var errors = new List<((int, int), (int, int), double, double)>();
int n = u.GetLength(0),
m = u.GetLength(1);
for (int a = 0; a < n; a++)
for (int a2 = a + 1; a2 < n; a2++)
for (int b = 0; b < m; b++)
for (int b2 = b + 1; b2 < m; b2++)
{
double high = u[a2, b2] - u[a, b2],
low = u[a2, b] - u[a, b];
if (high < low - 1e-12)
errors.Add(((a, b), (a2, b2), high, low));
}
return (errors.Count, errors);
}
var threshold = new double[,]
{
{ 0, 0, 0 },
{ 0, 2, 2 },
{ 0, 2, 2 },
};
var cournot = new double[10, 10];
for (int a = 0; a < 10; a++)
for (int b = 0; b < 10; b++)
cournot[a, b] = a * (10 - a - b);
foreach (var (name, u) in new[] { ("standard / modernisation", threshold), ("Cournot", cournot) })
Console.WriteLine(
$"{name} : supermodulaire={Supermodular(u).Count == 0}, différences croissantes={IncreasingDifferences(u).Count == 0}, violations=({Supermodular(u).Count}, {IncreasingDifferences(u).Count})"
);