#nullable enable using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.ExceptionServices; using System.Threading; using OpenNest.Engine.Jobs; using OpenNest.Engine.NestingEngines.Circles; using OpenNest.Engine.NestingEngines.Irregular; using OpenNest.Engine.NestingEngines.Raster; using OpenNest.Engine.NestingEngines.Rectangles; namespace OpenNest.Engine.NestingEngines.Default; /// /// The engine used when the caller names none. It runs each candidate engine on the whole job, /// checks every result with and returns the best one: a valid /// layout before an invalid one, then the fewest unplaced parts, then the lowest /// ; remaining ties keep candidate order. /// /// Candidates are Irregular, then Rectangles, then Circles, then Raster. Neither of the first two /// wins every job: Rectangles packs each part as its box, which is often best for plain plates and /// is a cheap, valid fallback for the rest. Circles bids only when every part is round, and /// otherwise returns everything unplaced at once. Raster is a fast irregular nest whose plan comes /// long before Irregular's; it is last in tie-break order, so it wins only by a better score. /// A candidate that throws or returns no result is skipped; when every candidate throws, the /// exception of the first in tie-break order is rethrown. Cancellation stops the search. /// Candidates' plate commits are not forwarded; the chosen result's commits are reported once it /// is selected. /// /// Candidates' whole-job plans () are scored the same way /// and reported only when one beats every plan reported so far, so a later candidate never shows a /// worse plan after a better one. A candidate result that beats them is reported too, and the /// chosen result is always the last plan reported. /// /// Rectangles, the quick and always-valid fallback, runs first, then Raster, then Circles, then /// Irregular, so a caller that stops the search early, such as , /// already holds a valid plan (an irregular one within seconds), and Irregular can search until /// cancelled /// () last. The choice is made after all have /// run, in candidate (tie-break) order, so the run order never changes it. /// /// Deterministic: candidates run one after another and the choice uses no clock. /// /// Setting the OPENNEST_NEST_LOG environment variable to a directory writes a /// of each solve there; it changes nothing about the result. /// public sealed class DefaultNestingEngine : INestingEngine { private readonly IReadOnlyList> candidates; private readonly IReadOnlyList runOrder; public DefaultNestingEngine() : this( new Func[] { () => new IrregularNestingEngine(), () => new RectanglesNestingEngine(), () => new CirclesNestingEngine(), () => new RasterNestingEngine(), }, new[] { 1, 3, 2, 0 }) { } /// Test seam: candidate engines in tie-break order, run in that order. internal DefaultNestingEngine(IReadOnlyList> candidates) : this(candidates, Enumerable.Range(0, candidates?.Count ?? 0).ToArray()) { } /// Test seam: candidate engines in tie-break order, and the order to run them in /// (a permutation of their indexes); the run order affects progress, never the choice. /// internal DefaultNestingEngine( IReadOnlyList> candidates, IReadOnlyList runOrder ) { ArgumentNullException.ThrowIfNull(candidates); ArgumentNullException.ThrowIfNull(runOrder); if (candidates.Count == 0) throw new ArgumentException( "At least one candidate engine is required.", nameof(candidates) ); if ( runOrder.Count != candidates.Count || runOrder.Distinct().Count() != candidates.Count || runOrder.Any(i => i < 0 || i >= candidates.Count) ) throw new ArgumentException( "The run order must list every candidate index once.", nameof(runOrder) ); this.candidates = candidates; this.runOrder = runOrder; } /// Candidate indexes in the order they run. internal IReadOnlyList RunOrder => runOrder; /// Where each solve's goes; null writes none. internal string? TraceDirectory { get; init; } = Environment.GetEnvironmentVariable(NestTrace.DirectoryVariable); public NestJobResult Solve( NestJob job, IProgress? progress = null, CancellationToken token = default ) { ArgumentNullException.ThrowIfNull(job); token.ThrowIfCancellationRequested(); using var trace = NestTrace.Start(TraceDirectory, job); var names = new string[candidates.Count]; var forward = progress == null ? null : new PlanFilter(job, progress, token); var scores = new CandidateScore?[candidates.Count]; ExceptionDispatchInfo? firstFailure = null; var firstFailureOrder = int.MaxValue; foreach (var order in runOrder) { token.ThrowIfCancellationRequested(); NestJobResult? result; if (forward != null) forward.Order = order; var timer = Stopwatch.StartNew(); try { var engine = candidates[order](); names[order] = engine.GetType().Name; trace?.Write("candidate.start", new { engine = names[order], order }); result = engine.Solve(job, forward, token); } catch (Exception ex) { trace?.Write("candidate.failed", new { engine = names[order], order, ms = timer.ElapsedMilliseconds, error = ex.ToString() }); trace?.Summarize($"{names[order]}: failed after {timer.ElapsedMilliseconds} ms: {ex.Message}"); // A canceled solve is rethrown by the token checks around each candidate. if (order < firstFailureOrder) { firstFailure = ExceptionDispatchInfo.Capture(ex); firstFailureOrder = order; } continue; } if (result == null) { trace?.Write("candidate.noResult", new { engine = names[order], order, ms = timer.ElapsedMilliseconds }); continue; } var solved = timer.ElapsedMilliseconds; var scored = CandidateScore.Of(job, result, order); scores[order] = scored; if (trace != null) TraceCandidate(trace, job, names[order], scored, solved, timer.ElapsedMilliseconds - solved); forward?.Offer(scored); } // Choose in tie-break order, whatever order the candidates ran in. CandidateScore? best = null; foreach (var scored in scores) if (scored != null && (best == null || scored.IsBetterThan(best))) best = scored; token.ThrowIfCancellationRequested(); if (best == null) { firstFailure?.Throw(); throw new InvalidOperationException("No candidate engine returned a result."); } forward?.Finish(best); CandidateProgress.ReportCommits(best.Result, progress); if (trace != null) TraceChoice(trace, names, scores, best); return best.Result; } private static void TraceCandidate(NestTrace trace, NestJob job, string engine, CandidateScore score, long solveMs, long scoreMs) { var utilization = score.Valid ? trace.Utilization(score.Result) : 0; trace.Write("candidate.finished", new { engine, order = score.Order, solveMs, scoreMs, valid = score.Valid, violations = score.Valid ? null : NestLayoutCheck.Violations(job, score.Result).Take(20).ToArray(), unplaced = score.Unplaced, cost = score.Cost, offcut = score.Offcut, utilization, stopReason = score.Result.StopReason, sheets = score.Valid ? trace.Sheets(score.Result) : null, }); trace.Summarize($"{engine}: {solveMs} ms, {(score.Valid ? "valid" : "INVALID")}, " + $"{score.Result.Plates.Count} sheet(s), {score.Unplaced} unplaced, cost {score.Cost:0.##}, " + $"utilization {utilization:P1}"); } private static void TraceChoice(NestTrace trace, string[] names, CandidateScore?[] scores, CandidateScore best) { CandidateScore? second = null; foreach (var scored in scores) if (scored != null && !ReferenceEquals(scored, best) && (second == null || scored.IsBetterThan(second))) second = scored; var priced = second != null && best.Valid && second.Valid; var costMargin = priced ? second!.Cost - best.Cost : (double?)null; var offcutMargin = priced ? best.Offcut - second!.Offcut : (double?)null; // Costs this close tie, as CandidateScore ranks them. var costDecides = priced && System.Math.Abs(second!.Cost - best.Cost) > 1e-9 * System.Math.Max(1, System.Math.Abs(best.Cost)); // Given the runner-up's place in the tie-break order, the best still wins only on its score. var reason = second == null ? "only result" : best.Valid != second.Valid ? "only valid layout" : best.Unplaced != second.Unplaced ? "fewer unplaced" : costDecides ? "lower cost" : (best with { Order = second.Order }).IsBetterThan(second) ? "same cost, larger offcut" : "tie, candidate order"; var runnerUp = second == null ? null : names[second.Order]; trace.Write("default.chosen", new { engine = names[best.Order], reason, runnerUp, costMargin, offcutMargin, }); trace.Summarize($"Chosen: {names[best.Order]} ({reason}{(runnerUp == null ? "" : $" over {runnerUp}")})"); trace.Complete(); } /// /// Forwards candidate progress except plate commits, and replaces candidates' raw plan reports /// with Default's own best plan so far by . /// private sealed class PlanFilter(NestJob job, IProgress inner, CancellationToken token) : IProgress { private CandidateScore? shown; /// Candidate order of the plans now arriving, for score ties. public int Order { get; set; } public void Report(NestJobProgress value) { if (value == null || value.Stage == NestJobStage.PlateCommitted) return; if (value.Stage != NestJobStage.PlanImproved) { inner.Report(value); return; } if (value.Plan == null) return; CandidateScore plan; try { plan = CandidateScore.Of(job, value.Plan, Order); } catch (Exception) when (!token.IsCancellationRequested) { // A plan that cannot be scored is not shown; throwing would fail the candidate. return; } Offer(plan); } /// Reports the plan when it beats every plan reported so far; a valid plan that /// does not is reported as an alternative. public void Offer(CandidateScore plan) { if (shown != null && !plan.IsBetterThan(shown)) { if (plan.Valid && !CandidateProgress.SameLayout(plan.Result, shown.Result)) CandidateProgress.ReportAlternative(plan.Result, inner); return; } shown = plan; NestTrace.Current?.Write("default.betterPlan", new { order = plan.Order, valid = plan.Valid, unplaced = plan.Unplaced, cost = plan.Cost, sheets = plan.Result.Plates.Count, }); CandidateProgress.ReportPlan(plan.Result, inner); } /// Makes the chosen result the last plan reported. A candidate's plan can outscore /// every returned result (its engine judged a later plan better), so this may follow it. public void Finish(CandidateScore chosen) { if (shown != null && CandidateProgress.SameLayout(shown.Result, chosen.Result)) return; shown = chosen; CandidateProgress.ReportPlan(chosen.Result, inner); } } }