refactor: Redesign nesting engines with pipeline pattern and add exhaustive search

- Rename Result to PackResult to avoid confusion with Result<T>
- Add PackingRequest as immutable configuration replacing mutable engine state
- Add PackingStrategy enum (AdvancedFit, BestFit, Exhaustive)
- Implement pipeline pattern for composable packing steps
- Rewrite AdvancedFitEngine as stateless using pipeline
- Rewrite BestFitEngine as stateless
- Add ExhaustiveFitEngine with symmetry breaking for optimal solutions
  - Tries all bin assignments to find minimum bins
  - Falls back to AdvancedFit for >20 items
  - Configurable threshold via constructor
- Update IEngine/IEngineFactory interfaces for new pattern
- Add strategy parameter to MCP tools

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-01 15:16:40 -05:00
parent 6e8469be4b
commit b19ecf3610
22 changed files with 898 additions and 351 deletions

View File

@@ -1,8 +1,13 @@
namespace CutList.Core.Nesting
namespace CutList.Core.Nesting
{
public class MultiBinEngine : IEngine
/// <summary>
/// Engine that coordinates packing across multiple bin types with different sizes.
/// Uses priority ordering to determine which bin types to fill first.
/// </summary>
public class MultiBinEngine
{
private readonly IEngineFactory _engineFactory;
private readonly List<MultiBin> _bins;
public MultiBinEngine() : this(new EngineFactory())
{
@@ -14,16 +19,14 @@
_bins = new List<MultiBin>();
}
private readonly List<MultiBin> _bins;
/// <summary>
/// Gets the read-only collection of bins.
/// Gets the read-only collection of bin types.
/// Use SetBins() to configure bins for packing.
/// </summary>
public IReadOnlyList<MultiBin> Bins => _bins.AsReadOnly();
/// <summary>
/// Sets the bins to use for packing.
/// Sets the bin types to use for packing.
/// </summary>
public void SetBins(IEnumerable<MultiBin> bins)
{
@@ -34,26 +37,48 @@
}
}
/// <summary>
/// The spacing/kerf between items.
/// </summary>
public double Spacing { get; set; }
public Result Pack(List<BinItem> items)
/// <summary>
/// The packing strategy to use for each bin type.
/// </summary>
public PackingStrategy Strategy { get; set; } = PackingStrategy.AdvancedFit;
/// <summary>
/// Packs items across all configured bin types.
/// </summary>
public PackResult Pack(List<BinItem> items)
{
var bins = _bins
var sortedBinTypes = _bins
.Where(b => b.Length > 0)
.OrderBy(b => b.Priority)
.ThenBy(b => b.Length)
.ToList();
var result = new Result();
var result = new PackResult();
var remainingItems = new List<BinItem>(items);
foreach (var bin in bins)
{
var engine = _engineFactory.CreateEngine(bin.Length, Spacing, bin.Quantity);
var r = engine.Pack(remainingItems);
var engine = _engineFactory.CreateEngine(Strategy);
result.AddBins(r.Bins);
remainingItems = r.ItemsNotUsed.ToList();
foreach (var binType in sortedBinTypes)
{
if (remainingItems.Count == 0)
break;
var request = new PackingRequest(
items: remainingItems,
stockLength: binType.Length,
spacing: Spacing,
maxBinCount: binType.Quantity
);
var packResult = engine.Pack(request);
result.AddBins(packResult.Bins);
remainingItems = packResult.ItemsNotUsed.ToList();
}
result.AddItemsNotUsed(remainingItems);
@@ -61,4 +86,4 @@
return result;
}
}
}
}