diff --git a/src/Nest/Core/Container.ts b/src/Nest/Core/Container.ts index e91c1a105..988ac5d89 100644 --- a/src/Nest/Core/Container.ts +++ b/src/Nest/Core/Container.ts @@ -356,7 +356,7 @@ export class Container //#region -------------------------File------------------------- //对象从文件中读取数据,初始化自身 - ReadFile(file: NestFiler, parts: Part[], preParts: Part[]) + ReadFile(file: NestFiler, parts: Part[]) { this.ParentId = file.Read(); this.ChildrenIndex = file.Read(); @@ -365,9 +365,8 @@ export class Container this.PlacedParts = []; for (let i = 0; i < count; i++) { - let isPrePart = file.Read(); let index = file.Read() as number; - let part = isPrePart ? preParts[index] : parts[index]; + let part = parts[index]; part.StateIndex = file.Read(); part.PlacePosition = file.Read(); this.PlacedParts.push(part); @@ -398,7 +397,6 @@ export class Container file.Write(parts.length); for (let p of parts) { - file.Write(p.IsPrePlace); file.Write(p.Id); file.Write(p.StateIndex); file.Write(p.PlacePosition); diff --git a/src/Nest/Core/Individual.ts b/src/Nest/Core/Individual.ts index d719e83ed..3463b142c 100644 --- a/src/Nest/Core/Individual.ts +++ b/src/Nest/Core/Individual.ts @@ -22,7 +22,6 @@ import { PlaceType } from "./PlaceType"; export class Individual { constructor(public Parts?: Part[], - public PreParts?: Part[], public mutationRate = 0.5, private bin?: Path, private OddmentsBins: Path[] = [], @@ -47,7 +46,7 @@ export class Individual this.OddmentsContainers = this.OddmentsBins.map(path => new Container(path, type ?? RandomIndex(3), this.ComparePointKeys[RandomIndex(2)])); this.InitHoleContainers(); - let parts = this.PreParts.concat(this.Parts); + let parts = this.Parts.concat(); //网洞优先 parts = this.Parts.filter(p => !this.HoleContainers.some(hole => { @@ -71,7 +70,7 @@ export class Individual let container = new Container(this.bin, type ?? RandomIndex(3), this.ComparePointKeys[RandomIndex(2)]); if (this.mutationRate > 0.5) //大板优先,可以提高收敛速度 { - let maxP = parts.reduce((preP, curP) => preP.State.Contour.Area > curP.State.Contour.Area ? preP : curP); + let maxP = MaxPart(parts); let PutGroupF = (): boolean => { @@ -120,7 +119,7 @@ export class Individual this.Containers = []; this.HoleContainers = []; - let parts = this.PreParts.concat(this.Parts); + let parts = this.Parts.concat(); while (parts.length > 0) { if (this.Containers.length > Math.max(0, bestCount))//提前结束,已经超过最佳用板量 @@ -151,7 +150,7 @@ export class Individual if (this.mutationRate > 0.5) //大板优先,可以提高收敛速度 { - let maxP = parts.reduce((preP, curP) => preP.State.Contour.Area > curP.State.Contour.Area ? preP : curP); + let maxP = MaxPart(parts); let PutGroupF = (): boolean => { @@ -247,7 +246,7 @@ export class Individual Clone() { - let p = new Individual(this.Parts.map(p => p.Clone()), this.PreParts.map(p => p.Clone()), this.mutationRate, this.bin, this.OddmentsBins, this.ComparePointKeys); + let p = new Individual(this.Parts.map(p => p.Clone()), this.mutationRate, this.bin, this.OddmentsBins, this.ComparePointKeys); p.Mutate(); return p; } @@ -261,8 +260,8 @@ export class Individual Mutate() { this.MutateParts(this.Parts); - if (this.PreParts.length) - this.MutateParts(this.PreParts); + + this.Parts = this.Parts.filter(p => p.IsPrePlace).concat(this.Parts.filter(p => !p.IsPrePlace)); return this; } @@ -324,13 +323,7 @@ export class Individual this.Parts[i].Mutate(); } - if (this.PreParts.length) - for (let i = this.PreParts.length - 1; i > 0; i--) - { - const j = Math.floor(Math.random() * (i + 1)); - [this.PreParts[i], this.PreParts[j]] = [this.PreParts[j], this.PreParts[i]]; - this.PreParts[i].Mutate(); - } + this.Parts = this.Parts.filter(p => p.IsPrePlace).concat(this.Parts.filter(p => !p.IsPrePlace)); } //#region -------------------------File------------------------- @@ -344,17 +337,17 @@ export class Individual let count = file.Read() as number; this.Containers = []; for (let i = 0; i < count; i++) - this.Containers.push(new Container().ReadFile(file, this.Parts, this.PreParts)); + this.Containers.push(new Container().ReadFile(file, this.Parts)); count = file.Read(); this.HoleContainers = []; for (let i = 0; i < count; i++) - this.HoleContainers.push(new Container().ReadFile(file, this.Parts, this.PreParts)); + this.HoleContainers.push(new Container().ReadFile(file, this.Parts)); count = file.Read(); this.OddmentsContainers = []; for (let i = 0; i < count; i++) - this.OddmentsContainers.push(new Container().ReadFile(file, this.Parts, this.PreParts)); + this.OddmentsContainers.push(new Container().ReadFile(file, this.Parts)); } //对象将自身数据写入到文件. @@ -376,3 +369,10 @@ export class Individual } //#endregion } + +function MaxPart(parts: Part[]): Part +{ + return parts.reduce((preP, curP) => preP.IsPrePlace === curP.IsPrePlace ? + (preP.State.Contour.Area > curP.State.Contour.Area ? preP : curP) + : (preP.IsPrePlace > curP.IsPrePlace ? preP : curP)); +} diff --git a/src/Nest/Core/NestDatabase.ts b/src/Nest/Core/NestDatabase.ts index 6607723db..9e77625af 100644 --- a/src/Nest/Core/NestDatabase.ts +++ b/src/Nest/Core/NestDatabase.ts @@ -15,8 +15,7 @@ export class NestDatabase OddmentsBins: Path[];//余料容器列表 Paths: Path[]; //所有的Path都在这里 - PreParts: Part[];//优先排料的零件 - Parts: Part[]; //所有的零件(不再是所有的零件了) + Parts: Part[]; //所有的零件 ComparePointKeys: string[] = DefaultComparePointKeys;//用来决定零件靠边模式 //#region -------------------------File------------------------- @@ -35,16 +34,6 @@ export class NestDatabase this.Bin = this.Paths[file.Read()]; PathGeneratorSingle.paths = this.Paths; - count = file.Read(); - this.PreParts = []; - for (let i = 0; i < count; i++) - { - let part = new Part(); - part.ReadFile(file); - part.IsPrePlace = true; - this.PreParts.push(part); - } - count = file.Read(); this.Parts = []; for (let i = 0; i < count; i++) @@ -77,10 +66,6 @@ export class NestDatabase file.Write(this.Bin.Id); - file.Write(this.PreParts.length); - for (let part of this.PreParts) - part.WriteFile(file); - file.Write(this.Parts.length); for (let part of this.Parts) part.WriteFile(file); diff --git a/src/Nest/Core/OptimizeMachine.ts b/src/Nest/Core/OptimizeMachine.ts index 298849f44..e81c2ffc0 100644 --- a/src/Nest/Core/OptimizeMachine.ts +++ b/src/Nest/Core/OptimizeMachine.ts @@ -25,7 +25,6 @@ export class OptimizeMachine Bin: Path; //默认的容器 OddmentsBins: Path[];//余料容器列表 - PreParts: Part[]; //优先开料的零件 Parts: Part[]; //所有的零件(不再是所有的零件了) ComparePointKeys: string[] = DefaultComparePointKeys;//用来决定零件靠边模式 @@ -56,20 +55,6 @@ export class OptimizeMachine // this.PartCount[part.Id] = count === undefined ? 1 : (count + 1); // } } - PutPreParts(parts: Part[]) - { - if (globalThis.document) parts = parts.slice(); - arrayRemoveIf(parts, p => p.RotatedStates.length === 0); - this.PreParts = parts; - - // //计算重复的零件(暂时不用) - // for (let part of parts) - // { - // let count = this.PartCount[part.Id]; - // this.PartCount[part.Id] = count === undefined ? 1 : (count + 1); - // } - } - callBack: (i: Individual) => Promise; @@ -81,11 +66,12 @@ export class OptimizeMachine this._IsSuspend = false; NestCache.Clear(); this.Parts.sort((p1, p2) => p2.State.Contour.Area - p1.State.Contour.Area); - this._Individuals = [new Individual(this.Parts, this.PreParts, 0.8, this.Bin, this.OddmentsBins, this.ComparePointKeys)]; + this.Parts = this.Parts.filter(p => p.IsPrePlace).concat(this.Parts.filter(p => !p.IsPrePlace)); + this._Individuals = [new Individual(this.Parts, 0.8, this.Bin, this.OddmentsBins, this.ComparePointKeys)]; for (let i = 1; i < this.Config.PopulationCount; i++) { let parts = this.Parts.map(p => p.Clone()); - let inv = new Individual(parts, this.PreParts.map(p => p.Clone()), 0.8, this.Bin, this.OddmentsBins, this.ComparePointKeys); + let inv = new Individual(parts.map(p => p.Clone()), 0.8, this.Bin, this.OddmentsBins, this.ComparePointKeys); if (i < 5) inv.BigMutate(); this._Individuals.push(inv); diff --git a/src/Nest/Core/OptimizeWorker.worker.ts b/src/Nest/Core/OptimizeWorker.worker.ts index 2719c96d3..d9e0d1f5a 100644 --- a/src/Nest/Core/OptimizeWorker.worker.ts +++ b/src/Nest/Core/OptimizeWorker.worker.ts @@ -16,7 +16,6 @@ ctx.addEventListener("message", async (event) => m.OddmentsBins = db.OddmentsBins; m.ComparePointKeys = db.ComparePointKeys; m.PutParts(db.Parts); - m.PutPreParts(db.PreParts); m.callBack = async (inv) => { diff --git a/src/Nest/Core/Part.ts b/src/Nest/Core/Part.ts index 821dff61e..06dcca9a6 100644 --- a/src/Nest/Core/Part.ts +++ b/src/Nest/Core/Part.ts @@ -52,7 +52,7 @@ export class Part PlaceIndex: number;//放置的大板索引 //#endregion - IsPrePlace = false;//需要优先放置(这个值由程序内部设置) + IsPrePlace = false;//需要优先放置 GroupMap: { [key: number]: Part[]; } = {}; get State(): PartState //零件当前的状态 @@ -297,6 +297,7 @@ export class Part { let part = new Part(); part.Id = this.Id; + part.IsPrePlace = this.IsPrePlace; part.UserData = this.UserData; part.RotatedStates = this.RotatedStates; part.StateIndex = this.StateIndex; @@ -316,6 +317,8 @@ export class Part ReadFile(file: NestFiler) { this.Id = file.Read(); + this.IsPrePlace = file.Read(); + let count = file.Read() as number; this.RotatedStates = []; for (let i = 0; i < count; i++) @@ -356,6 +359,8 @@ export class Part WriteFile(file: NestFiler) { file.Write(this.Id); + file.Write(this.IsPrePlace); + file.Write(this.RotatedStates.length); for (let state of this.RotatedStates) state.WriteFile(file); diff --git a/src/Nest/Test/PlaceUtil.ts b/src/Nest/Test/PlaceUtil.ts index 2bced2cba..b9789ae47 100644 --- a/src/Nest/Test/PlaceUtil.ts +++ b/src/Nest/Test/PlaceUtil.ts @@ -389,13 +389,11 @@ export async function InitParts(binPath = DefaultBin): Promise } //注册id - for (let i = 0; i < parts.length; i++) - parts[i].Id = i; + parts.forEach((p, i) => p.Id = i); let db = new NestDatabase(); db.Paths = PathGeneratorSingle.paths; db.Parts = parts; - db.PreParts = []; db.Bin = DefaultBin; db.OddmentsBins = oddpaths; db.ComparePointKeys = gravity; diff --git a/src/Nest/Test/TestYH2.tsx b/src/Nest/Test/TestYH2.tsx index 78849a792..3c85fc73c 100644 --- a/src/Nest/Test/TestYH2.tsx +++ b/src/Nest/Test/TestYH2.tsx @@ -15,7 +15,6 @@ export class Command_TestYH2 implements Command m.Bin = db.Bin; m.OddmentsBins = db.OddmentsBins; m.PutParts(db.Parts); - m.PutPreParts(db.PreParts); m.callBack = async (inv) => { Place(inv, db.Parts, db.Bin);