From fc3de972182be2367565188b69383ae4b2cb84e9 Mon Sep 17 00:00:00 2001 From: ChenX Date: Fri, 31 May 2024 10:45:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96:=E4=BF=AE=E5=A4=8D=E5=BC=82?= =?UTF-8?q?=E5=BD=A2=E4=BC=98=E5=8C=96=E7=AE=97=E6=B3=95NotPutCache?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Nest/Core/Container.ts | 26 ++++++++++++++++++-------- src/Nest/Core/OptimizeMachine.ts | 12 ++++++++++-- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/Nest/Core/Container.ts b/src/Nest/Core/Container.ts index c1fd618a4..4d66a93e3 100644 --- a/src/Nest/Core/Container.ts +++ b/src/Nest/Core/Container.ts @@ -51,11 +51,26 @@ export class Container constructor(public BinPath?: Path, private _PlaceType = PlaceType.Box, public ComparePosFnKey = "xy") { if (BinPath) + { this.StatusKey = this.BinPath.Id.toString() + "," + this._PlaceType + "," + ComparePosFnKey; + this.UpdateCurNotPuts(); + } + this.CompartePoint = ComparePoint(ComparePosFnKey); } + private UpdateCurNotPuts() + { + this._CurNotPuts = NestCache.GetNoPut(this.StatusKey); + if (!this._CurNotPuts) + { + this._CurNotPuts = new Set; + NestCache.SetNoPut(this.StatusKey, this._CurNotPuts); + this._NotPuts.push(this._CurNotPuts); + } + } + get UseRatio(): number { return this.PlacedBox.area / this.BinPath.Area; @@ -72,6 +87,7 @@ export class Container } private _NotPuts: Set[] = [];//已经无法放置的pathId + private _CurNotPuts: Set; private PrePut(part: Part): PartPlacedContainerState { @@ -100,8 +116,6 @@ export class Container } //快速退出 - let noSet = NestCache.GetNoPut(this.StatusKey); - if (noSet) this._NotPuts.push(noSet); for (let set of this._NotPuts) { if (set.has(part.State.Contour.Id)) @@ -114,12 +128,7 @@ export class Container let finalNfp = this.GetNFPs(part, binNfp); if (!finalNfp || finalNfp.length === 0) { - if (noSet) noSet.add(part.State.Contour.Id); - else - { - noSet = new Set([part.State.Contour.Id]); - NestCache.SetNoPut(this.StatusKey, noSet); - } + this._CurNotPuts.add(part.State.Contour.Id); return; } @@ -324,6 +333,7 @@ export class Container private AppendPart(part: Part, calc = true): void { this.StatusKey += "," + part.State.Contour.Id; + this.UpdateCurNotPuts(); this.PlacedParts.push(part); this.PlacedArea += part.State.Contour.Area; let m = { x: part.PlacePosition.x * 1e-4, y: part.PlacePosition.y * 1e-4 }; diff --git a/src/Nest/Core/OptimizeMachine.ts b/src/Nest/Core/OptimizeMachine.ts index fa7a56da1..92aba7a16 100644 --- a/src/Nest/Core/OptimizeMachine.ts +++ b/src/Nest/Core/OptimizeMachine.ts @@ -91,6 +91,10 @@ export class OptimizeMachine { console.time("1"); if (this.Parts.length === 0) return; + + //默认100次sleep一次 1500->70 2500->40 3500->10 4500->1 + let sleepCount = Math.max(130 - (30 * Math.ceil(this.Parts.length / 1000)), 1); + //开始自然选择 while (!this._IsSuspend) //实验停止信号 { @@ -101,7 +105,7 @@ export class OptimizeMachine if (globalThis.document || !clipperCpp.lib) await Sleep(0); let p = this._Individuals[i]; - const useContainerTemplate = i >= this._Individuals.length - 5; // 最后的5个个体,使用容器模板模式,来优化排料 + const useContainerTemplate = i === 1 || i === 4 || i === 10 || i === 20 || i === 50; // 最后的5个个体,使用容器模板模式,来优化排料 if (this.calcCount < 1000 || this.calcCount % 1000 === 0) p.Evaluate(this.best, true, useContainerTemplate, i % 3); else @@ -113,12 +117,16 @@ export class OptimizeMachine this.best = p.Fitness; await this.callBack(p); } + + if (this.Parts.length > 2000) + Sleep(0); + if (goBack) p.mutationRate = 0.6; } this.calcCount += this._Individuals.length; - if (this.calcCount % 100 === 0) + if (this.calcCount % sleepCount === 0) { await Sleep(0); if (false)//debug time