From 3f891398c71f776fd44546d94764d0816a3a0ac7 Mon Sep 17 00:00:00 2001 From: ChenX Date: Fri, 13 Dec 2019 16:51:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=AE=97=E6=B3=95:=E6=8F=90?= =?UTF-8?q?=E9=AB=98=E9=9A=8F=E6=9C=BA=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Nest/Container.ts | 2 ++ src/Nest/Converter/ConverBoard2Part.ts | 9 ++++++- src/Nest/Individual.ts | 37 +++++++++++++++----------- src/Nest/OptimizeMachine.ts | 14 +++++++--- src/Nest/Random.ts | 3 +-- src/Nest/Test/TestYH3.tsx | 2 +- 6 files changed, 45 insertions(+), 22 deletions(-) diff --git a/src/Nest/Container.ts b/src/Nest/Container.ts index c6e28dcce..628f83b7c 100644 --- a/src/Nest/Container.ts +++ b/src/Nest/Container.ts @@ -122,6 +122,7 @@ export class Container if (area < minArea || ((equaln(area, minArea, 1)) && (p.x < translate.x || (p.x === translate.x && p.y < translate.y)))) + // && (p.y < translate.y || (p.y === translate.y && p.x < translate.x)))) // && (this.BinPath.Size.x > this.BinPath.Size.y ? p.x < translate.x : p.y < translate.y))) { translate = p; @@ -138,6 +139,7 @@ export class Container if (area < minArea || ((equaln(area, minArea, 1)) && (p.x < translate.x || (p.x === translate.x && p.y < translate.y)))) + // && (p.y < translate.y || (p.y === translate.y && p.x < translate.x)))) // && (this.BinPath.Size.x > this.BinPath.Size.y ? p.x < translate.x : p.y < translate.y))) { translate = p; diff --git a/src/Nest/Converter/ConverBoard2Part.ts b/src/Nest/Converter/ConverBoard2Part.ts index 8c810c174..ff8b16a55 100644 --- a/src/Nest/Converter/ConverBoard2Part.ts +++ b/src/Nest/Converter/ConverBoard2Part.ts @@ -98,6 +98,10 @@ export function ConverBoard2Part(board: Board, knifRadius = 3.5): Part let [, pts] = Curves2Points(board.ContourCurve as Polyline, true, knifRadius); arrayRemoveDuplicateBySort(pts, (p1, p2) => equalv2(p1, p2, 1e-2)); path = new Path(pts); + + let area = path.BoundingBox.area - path.Area; + if (area < 15000 && pts.length > 6) + path = NestCache.CreatePath(board.Width, board.Height, knifRadius); } part.Init2(path, DefaultBin, Rotations[board.BoardProcessOption.lines]); for (let m of board.BoardModeling) @@ -106,7 +110,10 @@ export function ConverBoard2Part(board: Board, knifRadius = 3.5): Part { let [, pts] = Curves2Points(m.shape.Outline.Curve, false, knifRadius); part.UserData.push(m.shape.Outline.Curve.Clone()); - part.AppendHole(new Path(pts)); + + let path = new Path(pts); + if (path.Area > 15000) + part.AppendHole(path); } } diff --git a/src/Nest/Individual.ts b/src/Nest/Individual.ts index fd33c8da5..287a408b2 100644 --- a/src/Nest/Individual.ts +++ b/src/Nest/Individual.ts @@ -35,7 +35,7 @@ export class Individual */ Mutate() { - if (this.mutationRate > 0.6) + if (this.mutationRate > 0.5) for (let i = 0; i < this.Parts.length; i++) { let rand = Math.random(); @@ -47,25 +47,32 @@ export class Individual [this.Parts[i], this.Parts[j]] = [this.Parts[j], this.Parts[i]]; } if (rand < this.mutationRate) - { this.Parts[i].Mutate(); - break; - } } - - //洗牌 - let rand = Math.random(); - if (rand < 0.5) + else { - let index = RandomIndex(this.Parts.length - 2); - let count = Math.ceil(RandomIndex(this.Parts.length - 2 - index) * this.mutationRate * 2) || 1; - let parts = this.Parts.splice(index, count); - this.Parts.push(...parts); + //洗牌 + let rand = Math.random(); + if (rand < 0.5) + { + let index = RandomIndex(this.Parts.length - 2); + let count = Math.ceil(RandomIndex(this.Parts.length - 2 - index) * this.mutationRate * 2) || 1; + let parts = this.Parts.splice(index, count); + this.Parts.push(...parts); + this.Parts[index].Mutate(); + } + else + for (let i = 0; i < this.Parts.length; i++) + { + let rand = Math.random(); + if (rand < this.mutationRate) + { + this.Parts[i].Mutate(); + i += 5; + } + } } - let index = RandomIndex(this.Parts.length); - this.Parts[index].Mutate(); - if (this.mutationRate > 0.2) this.mutationRate -= 0.004; return this; diff --git a/src/Nest/OptimizeMachine.ts b/src/Nest/OptimizeMachine.ts index 365eaf11c..15f07afac 100644 --- a/src/Nest/OptimizeMachine.ts +++ b/src/Nest/OptimizeMachine.ts @@ -57,7 +57,14 @@ export class OptimizeMachine { let parts = this.Parts.map(p => p.Clone()); if (i < 3) - ShuffleArray(parts); + { + for (let i = parts.length - 1; i > 0; i--) + { + const j = Math.floor(Math.random() * (i + 1)); + [parts[i], parts[j]] = [parts[j], parts[i]]; + parts[i].Mutate(); + } + } this._Individuals.push(new Individual(parts, 0.8)); } //2.执行 @@ -82,7 +89,7 @@ export class OptimizeMachine if (globalThis.document || !clipperCpp.lib) await Sleep(0); let p = this._Individuals[i]; - if (this.calcCount < 2000 || this.calcCount % 1000 === 0) + if (this.calcCount < 1000 || this.calcCount % 1000 === 0) p.Evaluate(this.Bin, this.best, true, i % 2); else p.Evaluate(this.Bin, this.best); @@ -118,8 +125,9 @@ export class OptimizeMachine //自然选择 this._Individuals.splice(-10);//杀死它 - for (let i = 0; i < 5; i++) + for (let i = 0; i < 4; i++) this._Individuals.push(bestP.Clone()); + this._Individuals.push(this.bestP.Clone()); for (let i = 0; i < 3; i++) this._Individuals.push(this._Individuals[1].Clone()); for (let i = 0; i < 2; i++) diff --git a/src/Nest/Random.ts b/src/Nest/Random.ts index 160736ddb..eb2573fb5 100644 --- a/src/Nest/Random.ts +++ b/src/Nest/Random.ts @@ -2,8 +2,7 @@ import { FixIndex } from "./Util"; export function RandomIndex(count: number, exclude?: number): number { - let r = Math.random(); - let index = Math.floor(r * count); + let index = Math.floor(Math.random() * count); if (index === count) index = 0; if (index === exclude) index = FixIndex(index + 1, count); return index; diff --git a/src/Nest/Test/TestYH3.tsx b/src/Nest/Test/TestYH3.tsx index 3a9241ffa..37b901510 100644 --- a/src/Nest/Test/TestYH3.tsx +++ b/src/Nest/Test/TestYH3.tsx @@ -202,7 +202,7 @@ export class Command_TestYHWorker implements Command } }, "应用优化"); }; - for (let i = 0; i < navigator.hardwareConcurrency - 2; i++)// || + for (let i = 0; i < navigator.hardwareConcurrency / 2; i++)// || { let w = new Worker; workers.push(w);