|
|
|
@ -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));
|
|
|
|
|
}
|
|
|
|
|