refactor: DFS solver → matrix-based linear algebra (MathNet.Numerics)

- Rewrite ProductionSolver: system of linear equations A×x=b via LU/QR decomposition
- Add ResourceClassifier: classify resources as target/intermediate/raw
- Add RecipeMatrixBuilder: build coefficient matrix and RHS vector
- Add DI container (Microsoft.Extensions.DependencyInjection) in CLI
- Fix resource flow calculation (RecipeRate is total, not per-machine)
- Fix recipes.json: moduleCategory → machineCategory (recipe #19)
- Update tests: 30 tests covering solver, classifier, matrix builder
- Update README: document matrix model, architecture, principles
This commit is contained in:
2026-07-02 14:46:14 +03:00
parent a8c9808062
commit 1c735b124a
9 changed files with 746 additions and 274 deletions
+132 -22
View File
@@ -5,16 +5,15 @@ using Xunit;
namespace FactorioCalc.Tests;
/// <summary>
/// Regression tests for critical bugs fixed in ProductionSolver.
/// Tests for edge cases and correctness of the matrix-based solver.
/// </summary>
public class SolverBugFixTests
{
// --- Bug #1: mainProduct should filter by target resourceId, not .First() ---
// --- Multi-product recipes (e.g. oil refining) ---
[Fact]
public void Solve_MultiProductRecipe_UsesCorrectProduct()
{
// Simulate a recipe that produces multiple products (like oil refining)
var resources = new Dictionary<int, Resource>
{
{ 1, new Resource(1, "Crude Oil") },
@@ -32,22 +31,25 @@ public class SolverBugFixTests
{
1, new Recipe(1, "Oil Refining", "advanced-crafting", 4.0, "advanced-crafting",
new[] { new Ingredient(1, 1) },
new[] { new Product(3, 1), new Product(2, 1) }) // Heavy Oil first, Light Oil second
new[] { new Product(3, 1), new Product(2, 1) })
},
};
var repo = new TestRepository(recipes, resources, machines, new Dictionary<int, Module>());
var solver = new ProductionSolver(repo);
// Target Light Oil (resourceId=2) — it's the SECOND product in the list
// Target Light Oil — it's the SECOND product
var targets = new[] { new ProductionTarget(2, 10) };
var result = solver.Solve(targets);
Assert.Single(result.Executions);
// Should not throw — the solver correctly finds the matching product
// Heavy Oil should appear as a byproduct in resource flows
Assert.True(result.ResourceFlows.TryGetValue(3, out var heavyOilFlow));
Assert.True(heavyOilFlow > 0, "Heavy Oil should be produced as byproduct");
}
// --- Bug #2: DFS visited should not block recalculation for double targets ---
// --- Double targets aggregate correctly via matrix ---
[Fact]
public void Solve_DoubleTargetsForResource_AggregatesDemand()
@@ -80,24 +82,21 @@ public class SolverBugFixTests
var repo = new TestRepository(recipes, resources, machines, new Dictionary<int, Module>());
var solver = new ProductionSolver(repo);
// Two targets that both need Iron Plate: Steel Plate (needs 2/sec × 2 iron plate) + direct 5/sec
var targets = new[]
{
new ProductionTarget(9, 2), // Steel Plate → needs 4 Iron Plate/sec
new ProductionTarget(7, 5), // Iron Plate direct → needs 5 more/sec
new ProductionTarget(7, 5), // Iron Plate direct → 5 more/sec
};
var result = solver.Solve(targets);
// Should have both Steel Plate and Iron Plate executions
Assert.Equal(2, result.Executions.Count);
var ironPlateExec = result.Executions.First(e => e.RecipeId == 1);
// Iron Plate should account for BOTH demands (4 from steel + 5 direct = 9 total)
Assert.True(ironPlateExec.MachineCount >= 9,
$"Expected at least 9 machines for Iron Plate (demand=9/sec), got {ironPlateExec.MachineCount}");
}
// --- Bug #3: effectiveSpeed should not go to zero with heavy productivity modules ---
// --- Heavy productivity modules don't cause division by zero ---
[Fact]
public void Solve_HeavyProductivityModules_DoesNotDivideByZero()
@@ -120,7 +119,7 @@ public class SolverBugFixTests
},
};
// Extreme productivity modules: -20% speed × 4 slots = -80% total speed
// Extreme: -80% total speed
var extremeModules = new[]
{
new Module(1, "Prod Mod 3", ModuleType.Productivity, -0.20, 0.30, -0.15),
@@ -132,17 +131,17 @@ public class SolverBugFixTests
var moduleDict = new Dictionary<int, Module>();
for (var i = 0; i < extremeModules.Length; i++)
moduleDict[i + 1] = extremeModules[i];
var repo = new TestRepository(recipes, resources, machines, moduleDict);
var solver = new ProductionSolver(repo);
var targets = new[] { new ProductionTarget(7, 10) };
// Should NOT throw DivideByZeroException
// Should NOT throw
var result = solver.SolveWithModules(targets, extremeModules);
Assert.Single(result.Executions);
var exec = result.Executions.First();
// Speed should be clamped to minimum (0.05), not negative or zero
Assert.True(exec.EffectiveSpeed >= 0.05, $"EffectiveSpeed {exec.EffectiveSpeed} below minimum");
Assert.True(exec.MachineCount > 0 && exec.MachineCount < int.MaxValue,
$"MachineCount {exec.MachineCount} is unreasonable");
@@ -170,20 +169,131 @@ public class SolverBugFixTests
};
var prodModule = new Module(1, "Prod Mod 1", ModuleType.Productivity, -0.10, 0.10, -0.05);
var repo = new TestRepository(recipes, resources, machines, new Dictionary<int, Module> { { 1, prodModule } });
var repo = new TestRepository(recipes, resources, machines,
new Dictionary<int, Module> { { 1, prodModule } });
var solver = new ProductionSolver(repo);
var targets = new[] { new ProductionTarget(7, 10) };
var resultNoModules = solver.Solve(targets);
var resultWithModules = solver.SolveWithModules(targets, new[] { prodModule });
var execNoModules = resultNoModules.Executions.First();
var execWithModules = resultWithModules.Executions.First();
// With productivity, we need fewer machines because output per cycle is higher
// (even though speed is lower, the +10% output compensates)
Assert.True(execWithModules.EffectiveProductivity > 0, "Productivity bonus should be positive");
}
// --- Matrix solver correctness: verify exact rates ---
[Fact]
public void Solve_SimpleChain_ExactRates()
{
// Iron Plate: 1 ore → 1 plate (3s), speed=0.5
// Steel Plate: 2 plates + 1 coal → 1 steel (5s), speed=0.5
// Target: 1 steel/sec
// → Steel recipe rate = 1 cycle/sec
// → needs 2 iron plate/sec
// → Iron Plate recipe rate = 2 cycles/sec
// → needs 2 iron ore/sec
var resources = new Dictionary<int, Resource>
{
{ 1, new Resource(1, "Iron Ore") },
{ 7, new Resource(7, "Iron Plate") },
{ 9, new Resource(9, "Steel Plate") },
{ 5, new Resource(5, "Coal") },
};
var machines = new Dictionary<int, Machine>
{
{ 3, new Machine(3, "Smelter", 0.5, 3.0, 2, new[] { "smelting" }) },
};
var recipes = new Dictionary<int, Recipe>
{
{
1, new Recipe(1, "Iron Plate", "smelting", 3.0, "smelting",
new[] { new Ingredient(1, 1) },
new[] { new Product(7, 1) })
},
{
2, new Recipe(2, "Steel Plate", "smelting", 5.0, "smelting",
new[] { new Ingredient(7, 2), new Ingredient(5, 1) },
new[] { new Product(9, 1) })
},
};
var repo = new TestRepository(recipes, resources, machines, new Dictionary<int, Module>());
var solver = new ProductionSolver(repo);
var targets = new[] { new ProductionTarget(9, 1) };
var result = solver.Solve(targets);
var steelExec = result.Executions.First(e => e.RecipeId == 2);
var ironExec = result.Executions.First(e => e.RecipeId == 1);
// Steel Plate rate should be ~1 cycle/sec
Assert.InRange(steelExec.RecipeRate, 0.99, 1.01);
// Iron Plate rate should be ~2 cycles/sec
Assert.InRange(ironExec.RecipeRate, 1.99, 2.01);
}
// --- ResourceClassifier tests ---
[Fact]
public void ResourceClassifier_IdentifiesRawAndCraftable()
{
var resources = new Dictionary<int, Resource>
{
{ 1, new Resource(1, "Iron Ore") },
{ 7, new Resource(7, "Iron Plate") },
};
var recipes = new Dictionary<int, Recipe>
{
{
1, new Recipe(1, "Iron Plate", "smelting", 3.0, "smelting",
new[] { new Ingredient(1, 1) },
new[] { new Product(7, 1) })
},
};
var machines = new Dictionary<int, Machine>();
var repo = new TestRepository(recipes, resources, machines, new Dictionary<int, Module>());
var classifier = new ResourceClassifier();
classifier.Classify(repo.Recipes, new[] { new ProductionTarget(7, 10) });
Assert.True(classifier.Craftable.Contains(7));
Assert.True(classifier.Raw.Contains(1));
Assert.True(classifier.Targets.Contains(7));
}
// --- RecipeMatrixBuilder tests ---
[Fact]
public void RecipeMatrixBuilder_BuildsSquareMatrix()
{
var resources = new Dictionary<int, Resource>
{
{ 1, new Resource(1, "Iron Ore") },
{ 7, new Resource(7, "Iron Plate") },
};
var recipes = new Dictionary<int, Recipe>
{
{
1, new Recipe(1, "Iron Plate", "smelting", 3.0, "smelting",
new[] { new Ingredient(1, 1) },
new[] { new Product(7, 1) })
},
};
var machines = new Dictionary<int, Machine>();
var repo = new TestRepository(recipes, resources, machines, new Dictionary<int, Module>());
var classifier = new ResourceClassifier();
classifier.Classify(repo.Recipes, new[] { new ProductionTarget(7, 10) });
var builder = new RecipeMatrixBuilder();
builder.Build(repo.Recipes, new[] { new ProductionTarget(7, 10) }, classifier);
// Single craftable resource, single recipe → 1×1 matrix
Assert.Equal(1, builder.Matrix.RowCount);
Assert.Equal(1, builder.Matrix.ColumnCount);
Assert.Equal(10, builder.RightHandSide[0]); // target = 10/sec
Assert.Equal(1, builder.Matrix[0, 0]); // +1 Iron Plate per cycle
}
}