#r "nuget: Microsoft.Z3"
#load "Z3NativeLoader.cs"
using Microsoft.Z3;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
// Bibliotheque native de Z3 hors Windows et macOS Intel (voir Z3NativeLoader.cs)
Z3NativeLoader.Register(typeof(Context).Assembly);
// Contexte Z3 partage par toutes les cellules (meme moteur C++ que z3-solver Python).
var ctx = new Context();
Console.WriteLine($"Moteur : Microsoft.Z3 {Microsoft.Z3.Version.FullVersion}");
// --- Chargement natif du cache (16b) + partition par cours (16d) : la base commune.
string MealBase() {
var cwd = new DirectoryInfo(Directory.GetCurrentDirectory());
if (cwd.Name == "Z3-API") return Path.Combine(cwd.FullName, "data", "meals");
for (var d = cwd; d != null; d = d.Parent) {
var c = Path.Combine(d.FullName, "MyIA.AI.Notebooks", "SymbolicAI", "SMT", "Z3-API", "data", "meals");
if (Directory.Exists(c)) return c;
}
throw new FileNotFoundException("Serie Z3-API introuvable depuis " + cwd.FullName);
}
var cachePath = Path.Combine(MealBase(), "mealplan_cache.json");
if (!File.Exists(cachePath)) throw new FileNotFoundException($"Cache absent : {cachePath} (executez 16b d'abord).");
var constituants = new List<string>();
var plats = new List<(string Title, double[] Vec, string[] Cats)>();
using (var doc = JsonDocument.Parse(File.ReadAllText(cachePath))) {
foreach (var e in doc.RootElement.GetProperty("constituants").EnumerateArray())
constituants.Add(e.GetString()!);
foreach (var r in doc.RootElement.GetProperty("recipes").EnumerateArray()) {
var t = (r.GetProperty("title").GetString() ?? "").Trim();
plats.Add((t[..Math.Min(40, t.Length)],
r.GetProperty("vec").EnumerateArray().Select(v => v.GetDouble()).ToArray(),
r.GetProperty("cats").EnumerateArray().Select(c => c.GetString()!).ToArray()));
}
}
int C = constituants.Count;
int R = plats.Count; // mesure effective au chargement -- jamais recopiee d'une prose ancienne
int[][] vint = Enumerable.Range(0, C)
.Select(c => plats.Select(p => (int)Math.Round(p.Vec[c])).ToArray()).ToArray(); // Math.Round = banker (ToEven), comme round() Python
int Pq(int c, double q) { // quantile TRONQUE (pas interpole) ; int() Python = troncature vers zero
var vals = plats.Select(p => p.Vec[c]).OrderBy(v => v).ToList();
return (int)vals[(int)(q * (vals.Count - 1))];
}
int NMENUS = 7, NPLATS = 5;
int loE = NPLATS * Pq(0, 0.20), hiE = NPLATS * Pq(0, 0.80);
int loP = NPLATS * Pq(1, 0.30), hiS = Math.Max(1, NPLATS * Pq(4, 0.70));
// restr : (indexConstituant, lo, hi), -1 = pas de borne de ce cote.
var restr = new (int cc, int lo, int hi)[] { (0, loE, hiE), (1, loP, -1), (4, -1, hiS) };
var COURSES = new[] { "Entree", "Plat principal", "Accompagnement", "Pain", "Dessert" };
var COURSE_CATS = new HashSet<string>[] {
new HashSet<string> { "appetizers", "soups", "salads", "salad", "soup" },
new HashSet<string> { "main dish", "meats", "beef", "poultry", "seafood", "fish", "pasta", "casseroles", "chili", "pork", "chicken", "stews" },
new HashSet<string> { "vegetables", "vegetarian", "sauces", "sauce", "side dishes", "rice", "potatoes" },
new HashSet<string> { "breads", "bread", "muffins", "rolls", "biscuits" },
new HashSet<string> { "desserts", "cakes", "cake", "cookies", "chocolate", "fruits", "pies", "candy", "pastries" },
};
int CourseOf(string[] cats) {
foreach (var cat in cats)
for (int k = 0; k < 5; k++) if (COURSE_CATS[k].Contains(cat.ToLowerInvariant())) return k;
return 1;
}
var pool = Enumerable.Range(0, 5).Select(_ => new List<int>()).ToList();
for (int r = 0; r < R; r++) pool[CourseOf(plats[r].Cats)].Add(r);
const int SEL = 4, PROT = 1, GLU = 2, LIP = 3; // alias de constituants (indices C)
Console.WriteLine($"Cache : R={R} recettes, C={C} constituants. Pools : "
+ string.Join(", ", Enumerable.Range(0, 5).Select(k => $"{COURSES[k]}={pool[k].Count}")) + ".");
Console.WriteLine($"Theoreme : {NMENUS} menus x {NPLATS} plats. Bornes patient dynamiques : "
+ $"energie[{loE},{hiE}], proteines>={loP}, sel<={hiS}.");