-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathCOSolution.cs
1532 lines (1424 loc) · 73.7 KB
/
COSolution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using SC.ObjectModel.Additionals;
using SC.ObjectModel.Elements;
using SC.ObjectModel.Interfaces;
using SC.ObjectModel.IO.Json;
using SC.Toolbox;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Xml;
namespace SC.ObjectModel
{
/// <summary>
/// Defines a solution to the problem
/// </summary>
public class COSolution : IXmlSerializable
{
/// <summary>
/// Creates a new solution
/// </summary>
/// <param name="instance">The instance this solution belongs to</param>
/// <param name="tetris">Defines whether to use tetris style in this solution</param>
/// <param name="meritType">The merit-type to use when using heuristics</param>
internal COSolution(Instance instance, bool tetris, MeritFunctionType meritType)
{
InstanceLinked = instance;
ContainedPieces = new HashSet<VariablePiece>();
OffloadPieces = new HashSet<VariablePiece>(instance.Pieces);
Orientations = new int[instance.PiecesWithVirtuals.Count()];
OrientedPieces = new ComponentsSet[instance.PiecesWithVirtuals.Count()];
Positions = new MeshPoint[instance.PiecesWithVirtuals.Count()];
Containers = new Container[instance.PiecesWithVirtuals.Count()];
InitMetaInfo();
InitFlagHandling();
ContainerContent = instance.Containers.Select(c => new HashSet<VariablePiece>()).ToArray();
ExploitedVolume = 0;
ExploitedVolumeOfContainers = new double[instance.Containers.Count];
ExploitedWeightOfContainers = new double[instance.Containers.Count];
MaterialsPerContainer = new int[instance.Containers.Count, Enum.GetValues(typeof(MaterialClassification)).Length];
}
/// <summary>
/// The ID of the solution
/// </summary>
public int ID { get; set; }
/// <summary>
/// The instance this solution belongs to
/// </summary>
public Instance InstanceLinked { get; set; }
#region Core information
/// <summary>
/// Contains all pieces that are allocated to a container
/// </summary>
public HashSet<VariablePiece> ContainedPieces { get; set; }
/// <summary>
/// Contains all pieces not allocated to a container
/// </summary>
public HashSet<VariablePiece> OffloadPieces { get; set; }
/// <summary>
/// The items contained in the different containers
/// </summary>
public HashSet<VariablePiece>[] ContainerContent { get; set; }
/// <summary>
/// The orientations used per piece
/// </summary>
public int[] Orientations { get; set; }
/// <summary>
/// The pre-oriented pieces used
/// </summary>
public ComponentsSet[] OrientedPieces { get; set; }
/// <summary>
/// The positions of the pieces used
/// </summary>
public MeshPoint[] Positions { get; set; }
/// <summary>
/// The containers used per piece
/// </summary>
public Container[] Containers { get; set; }
/// <summary>
/// Used to store the currently used order of containers.
/// </summary>
public List<Container> ConstructionContainerOrder;
/// <summary>
/// Used to store the currently used orientations per piece.
/// </summary>
public int[][] ConstructionOrientationOrder;
#endregion
#region Solution manipulation
/// <summary>
/// Adds a piece to a container at a position and in an orientation
/// </summary>
/// <param name="container">The container to add the piece to</param>
/// <param name="piece">The piece to add to the solution</param>
/// <param name="orientation">The orientation to use for the piece</param>
/// <param name="position">The position of the piece inside the container</param>
public void Add(Container container, VariablePiece piece, int orientation, MeshPoint position)
{
ExploitedVolume += TetrisMode ? piece.Volume : piece.Original.BoundingBox.Volume;
ExploitedVolumeOfContainers[container.VolatileID] += TetrisMode ? piece.Volume : piece.Original.BoundingBox.Volume;
ExploitedWeightOfContainers[container.VolatileID] += piece.Weight;
MaterialsPerContainer[container.VolatileID, (int)piece.Material.MaterialClass]++;
ContainedPieces.Add(piece);
OffloadPieces.Remove(piece);
ContainerContent[container.VolatileID].Add(piece);
Orientations[piece.VolatileID] = orientation;
Positions[piece.VolatileID] = position;
Containers[piece.VolatileID] = container;
OrientedPieces[piece.VolatileID] = piece[orientation];
AddPieceFlags(container, piece);
}
/// <summary>
/// Removes a specific piece from the solution
/// </summary>
/// <param name="container">The container to remove the piece from</param>
/// <param name="piece">The piece to remove</param>
/// <returns>The position at which the piece was inserted</returns>
public MeshPoint Remove(Container container, VariablePiece piece)
{
ExploitedVolume -= TetrisMode ? piece.Volume : piece.Original.BoundingBox.Volume;
ExploitedVolumeOfContainers[container.VolatileID] -= TetrisMode ? piece.Volume : piece.Original.BoundingBox.Volume;
ExploitedWeightOfContainers[container.VolatileID] -= piece.Weight;
MaterialsPerContainer[container.VolatileID, (int)piece.Material.MaterialClass]--;
ContainedPieces.Remove(piece);
OffloadPieces.Add(piece);
ContainerContent[container.VolatileID].Remove(piece);
MeshPoint position = Positions[piece.VolatileID];
Containers[piece.VolatileID] = null;
Positions[piece.VolatileID] = null;
Orientations[piece.VolatileID] = 0;
OrientedPieces[piece.VolatileID] = null;
RemovePieceFlags(container, piece);
return position;
}
/// <summary>
/// Clears the content of one container
/// </summary>
/// <param name="container">The container to clear</param>
public void RemoveContainer(Container container)
{
ExploitedVolume -= ContainerContent[container.VolatileID].Sum(p => { return TetrisMode ? p.Volume : p.Original.BoundingBox.Volume; });
ExploitedVolumeOfContainers[container.VolatileID] = 0;
ExploitedWeightOfContainers[container.VolatileID] = 0;
foreach (var piece in ContainerContent[container.VolatileID])
{
MaterialsPerContainer[container.VolatileID, (int)piece.Material.MaterialClass]--;
ContainedPieces.Remove(piece);
OffloadPieces.Add(piece);
Containers[piece.VolatileID] = null;
Positions[piece.VolatileID] = null;
Orientations[piece.VolatileID] = 0;
OrientedPieces[piece.VolatileID] = null;
RemovePieceFlags(container, piece);
}
ContainerContent[container.VolatileID].Clear();
}
/// <summary>
/// Clears the solution
/// </summary>
public void Clear()
{
EPCounter = 0;
ContainedPieces.Clear();
OffloadPieces = new HashSet<VariablePiece>(InstanceLinked.Pieces);
foreach (var container in InstanceLinked.Containers)
{
ExploitedVolumeOfContainers[container.VolatileID] = 0;
ExploitedWeightOfContainers[container.VolatileID] = 0;
for (int i = 0; i < Enum.GetValues(typeof(MaterialClassification)).Length; i++)
MaterialsPerContainer[container.VolatileID, i] = 0;
ContainerContent[container.VolatileID].Clear();
ExtremePoints[container.VolatileID].Clear();
if (ResidualSpace != null)
{
ResidualSpace.Clear();
}
}
GenerateDefaultEPs();
foreach (var piece in InstanceLinked.Pieces)
{
Orientations[piece.VolatileID] = 0;
OrientedPieces[piece.VolatileID] = null;
Positions[piece.VolatileID] = null;
Containers[piece.VolatileID] = null;
}
switch (MeritType)
{
case MeritFunctionType.MFV:
break;
case MeritFunctionType.MMPSXY:
case MeritFunctionType.LPXY:
{
foreach (var container in InstanceLinked.Containers)
{
PackingMaxX[container.VolatileID] = 0;
PackingMaxY[container.VolatileID] = 0;
}
}
break;
case MeritFunctionType.MRSU:
break;
case MeritFunctionType.MEDXYZ:
break;
case MeritFunctionType.MEDXY:
break;
case MeritFunctionType.None:
default:
break;
}
LevelPackingC = 0;
ExploitedVolume = 0.0;
ClearFlagHandling();
}
#endregion
#region Cloning
/// <summary>
/// Clones the current solution (does not clone meta-information)
/// </summary>
/// <returns>A clone of this solution</returns>
public COSolution Clone(bool unofficial = true)
{
COSolution clone = InstanceLinked.CreateSolution(TetrisMode, MeritType, unofficial);
clone.InstanceLinked = InstanceLinked;
clone.ID = ID;
clone.ContainedPieces = new HashSet<VariablePiece>(ContainedPieces);
clone.OffloadPieces = new HashSet<VariablePiece>(OffloadPieces);
clone.MaterialsPerContainer = new int[InstanceLinked.Containers.Count, Enum.GetValues(typeof(MaterialClassification)).Length];
foreach (var container in InstanceLinked.Containers)
{
foreach (var piece in ContainerContent[container.VolatileID])
{
clone.ContainerContent[container.VolatileID].Add(piece);
clone.MaterialsPerContainer[container.VolatileID, (int)piece.Material.MaterialClass]++;
}
}
foreach (var piece in InstanceLinked.Pieces)
{
clone.Orientations[piece.VolatileID] = Orientations[piece.VolatileID];
clone.OrientedPieces[piece.VolatileID] = OrientedPieces[piece.VolatileID];
clone.Positions[piece.VolatileID] = (Positions[piece.VolatileID] != null) ? Positions[piece.VolatileID].Clone() : null;
clone.Containers[piece.VolatileID] = Containers[piece.VolatileID];
}
clone.ExploitedVolume = ExploitedVolume;
clone.ExploitedVolumeOfContainers = ExploitedVolumeOfContainers.ToArray();
clone.ExploitedWeightOfContainers = ExploitedWeightOfContainers.ToArray();
clone.ExtremePoints = ExtremePoints.Select(c => c.ToList()).ToArray();
// Add info about the virtual pieces
clone.GenerateVirtualPieceInfo();
// Copy meta information
clone.EndPointsBoundingBoxInner = EndPointsBoundingBoxInner.Select(p => p.Clone()).ToArray();
clone.EndPointsBoundingBoxOuter = EndPointsBoundingBoxOuter.Select(p => p.Clone()).ToArray();
clone.EndPointsComponentInner = EndPointsComponentInner.Select(p => p.Clone()).ToArray();
clone.EndPointsComponentOuter = EndPointsComponentOuter.Select(p => p.Clone()).ToArray();
clone.PushedPosition = PushedPosition.Select(p => p.Clone()).ToArray();
clone.EndPointsDelta = EndPointsDelta.Select(p => p.Clone()).ToArray();
clone.PiecesByVolatileID = PiecesByVolatileID.ToArray();
clone.ContainerByVolatileID = ContainerByVolatileID.ToArray();
if (MeritType == MeritFunctionType.MMPSXY)
{
clone.PackingMaxX = PackingMaxX.ToArray();
clone.PackingMaxY = PackingMaxY.ToArray();
}
if (MeritType == MeritFunctionType.MRSU)
{
clone.ResidualSpace = ResidualSpace.Select(c => c.Clone()).ToList();
}
clone.EPCounter = EPCounter;
clone.LevelPackingC = LevelPackingC;
// Copy construction information
if (ConstructionContainerOrder != null)
clone.ConstructionContainerOrder = ConstructionContainerOrder.ToList();
if (ConstructionOrientationOrder != null)
clone.ConstructionOrientationOrder = ConstructionOrientationOrder.Select(p => p.ToArray()).ToArray();
// Return it
return clone;
}
#endregion
#region Auxiliary information
/// <summary>
/// The merit-function type in use
/// </summary>
private MeritFunctionType MeritType = MeritFunctionType.None;
/// <summary>
/// Indicates whether we look at the pieces in all available detail or only at their bounding boxes
/// </summary>
private bool TetrisMode = true;
/// <summary>
/// Fast access field for exploited volume
/// </summary>
public double ExploitedVolume;
/// <summary>
/// Fast accessible information about the exploited volume per container
/// </summary>
public double[] ExploitedVolumeOfContainers = null;
/// <summary>
/// Fast accessible information about the loaded weight per container
/// </summary>
public double[] ExploitedWeightOfContainers = null;
/// <summary>
/// Fast accessible information about the number of items with the correpsonding material per container
/// </summary>
public int[,] MaterialsPerContainer = null;
/// <summary>
/// Stores the inner points of the bounding-boxes of the pieces
/// </summary>
public MeshPoint[] EndPointsBoundingBoxInner = null;
/// <summary>
/// Stores the outer points of the bounding-boxes of the pieces
/// </summary>
public MeshPoint[] EndPointsBoundingBoxOuter = null;
/// <summary>
/// Stores the inner points of the components
/// </summary>
public MeshPoint[] EndPointsComponentInner = null;
/// <summary>
/// Stores the outer points of the components
/// </summary>
public MeshPoint[] EndPointsComponentOuter = null;
/// <summary>
/// Stores the pushed positions of the pieces
/// </summary>
public MeshPoint[] PushedPosition = null;
/// <summary>
/// Stores the push-delta-information of the pieces
/// </summary>
public MeshPoint[] EndPointsDelta = null;
/// <summary>
/// Stores the pieces belonging to their respective volatile IDs
/// </summary>
public Piece[] PiecesByVolatileID = null;
/// <summary>
/// Stores the container belonging to their respective volatile IDs
/// </summary>
public Container[] ContainerByVolatileID = null;
/// <summary>
/// Stores the current size of the packing regarding x
/// </summary>
public double[] PackingMaxX = null;
/// <summary>
/// Stores the current size of the packing regarding y
/// </summary>
public double[] PackingMaxY = null;
/// <summary>
/// Stores the residual space currently available at the different insertion points
/// </summary>
public List<MeshPoint> ResidualSpace = null;
/// <summary>
/// Stores the currently available EPs
/// </summary>
public List<MeshPoint>[] ExtremePoints = null;
/// <summary>
/// Counts the added EPs
/// </summary>
private int EPCounter = 0;
/// <summary>
/// The C constant for the level packing merit function
/// </summary>
private double LevelPackingC = 0;
/// <summary>
/// Initiates the meta-information for the underlying instance
/// </summary>
private void InitMetaInfo()
{
int pieceID = 0;
int componentID = 0;
int containerID = 0;
// Generate piece info
PiecesByVolatileID = new Piece[InstanceLinked.PiecesWithVirtuals.Count()];
foreach (var piece in InstanceLinked.PiecesWithVirtuals)
{
foreach (var component in piece.Original.Components)
{
foreach (var orientation in MeshConstants.ORIENTATIONS)
{
piece[orientation][component.ID].VolatileID = componentID;
}
component.VolatileID = componentID++;
}
PiecesByVolatileID[pieceID] = piece;
piece.VolatileID = pieceID++;
}
// Generate container info
ContainerByVolatileID = new Container[InstanceLinked.Containers.Count];
foreach (var container in InstanceLinked.Containers.OrderBy(c => c.ID))
{
ContainerByVolatileID[containerID] = container;
container.VolatileID = containerID++;
}
if (MeritType == MeritFunctionType.MRSU)
{
ResidualSpace = new List<MeshPoint>();
}
// Generate default EPs
GenerateDefaultEPs();
// Generate merit-info
switch (MeritType)
{
case MeritFunctionType.None:
case MeritFunctionType.MFV:
break;
case MeritFunctionType.MMPSXY:
{
PackingMaxX = new double[InstanceLinked.Containers.Count];
PackingMaxY = new double[InstanceLinked.Containers.Count];
}
break;
case MeritFunctionType.LPXY:
{
PackingMaxX = new double[InstanceLinked.Containers.Count];
PackingMaxY = new double[InstanceLinked.Containers.Count];
LevelPackingC = Math.Max(InstanceLinked.Containers.Max(c => c.Mesh.Length), InstanceLinked.Containers.Max(c => c.Mesh.Width)) + 1;
}
break;
case MeritFunctionType.MRSU:
default:
break;
}
// Init endpoint-info
EndPointsBoundingBoxInner = new MeshPoint[pieceID];
EndPointsBoundingBoxOuter = new MeshPoint[pieceID];
PushedPosition = new MeshPoint[pieceID];
EndPointsDelta = new MeshPoint[pieceID];
for (int i = 0; i < pieceID; i++)
{
EndPointsBoundingBoxInner[i] = new MeshPoint() { ParentPiece = PiecesByVolatileID[i], VertexID = 1 };
EndPointsBoundingBoxOuter[i] = new MeshPoint() { ParentPiece = PiecesByVolatileID[i], VertexID = 8 };
PushedPosition[i] = new MeshPoint();
EndPointsDelta[i] = new MeshPoint();
}
if (TetrisMode)
{
EndPointsComponentInner = new MeshPoint[componentID];
EndPointsComponentOuter = new MeshPoint[componentID];
for (int i = 0; i < componentID; i++)
{
EndPointsComponentInner[i] = new MeshPoint();
EndPointsComponentOuter[i] = new MeshPoint();
}
}
else
{
EndPointsComponentInner = null;
EndPointsComponentOuter = null;
}
// Add info about the virtual pieces
GenerateVirtualPieceInfo();
}
#endregion
#region Flag handling
/// <summary>
/// Stores all information about how many flags of a type and value are contained in a container.
/// </summary>
private MultiKeyDictionary<int, int, int>[] _flagCountPerContainer;
/// <summary>
/// Stores all information about which values of the different flag types are contained in which container.
/// </summary>
private MultiKeyDictionary<int, HashSet<int>>[] _flagTypesPerContainer;
/// <summary>
/// Inits flag handling for this solution.
/// </summary>
private void InitFlagHandling()
{
_flagCountPerContainer = new MultiKeyDictionary<int, int, int>[InstanceLinked.Containers.Count];
_flagTypesPerContainer = new MultiKeyDictionary<int, HashSet<int>>[InstanceLinked.Containers.Count];
foreach (var container in InstanceLinked.Containers)
{
_flagCountPerContainer[container.VolatileID] = new MultiKeyDictionary<int, int, int>(defaultIfNotPresent: true);
_flagTypesPerContainer[container.VolatileID] = new MultiKeyDictionary<int, HashSet<int>>(defaultIfNotPresent: true, defaultValueIfNotPresent: () => new HashSet<int>());
}
}
/// <summary>
/// Clears all flag handling information from this solution.
/// </summary>
private void ClearFlagHandling()
{
foreach (var container in InstanceLinked.Containers)
{
_flagCountPerContainer[container.VolatileID].Clear();
_flagTypesPerContainer[container.VolatileID].Clear();
}
}
/// <summary>
/// Updates the flag information implied by adding the piece to the given container.
/// </summary>
/// <param name="container">The container the piece is added to.</param>
/// <param name="piece">The piece that is added to the container.</param>
private void AddPieceFlags(Container container, VariablePiece piece)
{
foreach (var (flag, value) in piece.GetFlags())
{
_flagCountPerContainer[container.VolatileID][flag, value]++;
_flagTypesPerContainer[container.VolatileID][flag].Add(value);
}
}
/// <summary>
/// Updates the flag information implied by removing the piece from the given container.
/// </summary>
/// <param name="container">The container the piece is removed from.</param>
/// <param name="piece">The piece that is remove </param>
private void RemovePieceFlags(Container container, VariablePiece piece)
{
foreach (var (flag, value) in piece.GetFlags())
{
int contained = --_flagCountPerContainer[container.VolatileID][flag, value];
if (contained <= 0)
_flagTypesPerContainer[container.VolatileID][flag].Remove(value);
}
}
/// <summary>
/// Returns a set of all flag values of the given flag type contained in the given container.
/// </summary>
/// <param name="container">The container to check the contained flag types for.</param>
/// <param name="flagType">The flag type to check.</param>
/// <returns>A set containing all flag values of a given flag type contained in the given container.</returns>
public HashSet<int> GetFlagInfoTypesContained(Container container, int flagType) => _flagTypesPerContainer[container.VolatileID][flagType];
/// <summary>
/// Returns the number of pieces contained in the given container for a given flag value of a given flag type.
/// </summary>
/// <param name="container">The container to check.</param>
/// <param name="flagType">The type of the flag to check.</param>
/// <param name="flagValue">The flag value to lookup the number of pieces for.</param>
/// <returns>The number of pieces contained in the given container that have the given flag value for the given flag type.</returns>
public int GetFlagInfoPiecesContained(Container container, int flagType, int flagValue) => _flagCountPerContainer[container.VolatileID][flagType, flagValue];
#endregion
#region Extreme point handling
/// <summary>
/// Generates the default extreme-points for the given solution.
/// </summary>
private void GenerateDefaultEPs()
{
// Init EP info fields
ExtremePoints = new List<MeshPoint>[InstanceLinked.Containers.Count];
// Generate default EPs
foreach (var container in InstanceLinked.Containers)
{
ExtremePoints[container.VolatileID] = new List<MeshPoint>();
AddEP(container, new MeshPoint() { X = 0, Y = 0, Z = 0 });
}
// Generate EPs for virtual pieces and already contained pieces
foreach (var container in InstanceLinked.Containers)
foreach (var virtualPiece in container.VirtualPieces)
GenerateEPsForPiece(container, virtualPiece, virtualPiece.FixedPosition, virtualPiece.FixedOrientation);
foreach (var piece in ContainedPieces)
GenerateEPsForPiece(Containers[piece.VolatileID], piece, Positions[piece.VolatileID], Orientations[piece.VolatileID]);
// Generate EPs for slants
foreach (var container in InstanceLinked.Containers)
foreach (var slant in container.Slants.Where(s => s.NormalVector.X <= 0 || s.NormalVector.Y <= 0 || s.NormalVector.Z <= 0))
foreach (var intersection in slant.ContainerIntersections)
AddEP(container, new MeshPoint() { X = intersection.X, Y = intersection.Y, Z = intersection.Z });
}
/// <summary>
/// Generates extreme-points for a given piece at a given position and orientation.
/// </summary>
/// <param name="container">The container to generate the extreme-points in.</param>
/// <param name="piece">The piece to generate the extreme-points for.</param>
/// <param name="position">The position of the piece.</param>
/// <param name="orientation">The orientation of the piece.</param>
private void GenerateEPsForPiece(Container container, Piece piece, MeshPoint position, int orientation)
{
if (TetrisMode)
{
foreach (var component in piece[orientation].Components)
{
// Init extreme points to defaults
MeshPoint ep11 = new MeshPoint()
{
X = position.X + component.RelPosition.X + component.Length,
Y = 0,
Z = position.Z + component.RelPosition.Z
}; // Taking endpoint regarding x and projecting along the y-axis
MeshPoint ep12 = new MeshPoint()
{
X = position.X + component.RelPosition.X + component.Length,
Y = position.Y + component.RelPosition.Y,
Z = 0
}; // Taking endpoint regarding x and projecting along the z-axis
MeshPoint ep21 = new MeshPoint()
{
X = 0,
Y = position.Y + component.RelPosition.Y + component.Width,
Z = position.Z + component.RelPosition.Z
}; // Taking endpoint regarding y and projecting along the x-axis
MeshPoint ep22 = new MeshPoint()
{
X = position.X + component.RelPosition.X,
Y = position.Y + component.RelPosition.Y + component.Width,
Z = 0
}; // Taking endpoint regarding y and projecting along the z-axis
MeshPoint ep31 = new MeshPoint()
{
X = 0,
Y = position.Y + component.RelPosition.Y,
Z = position.Z + component.RelPosition.Z + component.Height
}; // Taking endpoint regarding z and projecting along the x-axis
MeshPoint ep32 = new MeshPoint()
{
X = position.X + component.RelPosition.X,
Y = 0,
Z = position.Z + component.RelPosition.Z + component.Height
}; // Taking endpoint regarding z and projecting along the y-axis
// Add the EPs
AddEP(container, ep11);
AddEP(container, ep12);
AddEP(container, ep21);
AddEP(container, ep22);
AddEP(container, ep31);
AddEP(container, ep32);
}
}
else
{
// Init extreme points to defaults
MeshPoint ep11 = new MeshPoint()
{
X = position.X + piece[orientation].BoundingBox.Length,
Y = 0,
Z = position.Z
}; // Taking endpoint regarding x and projecting along the y-axis
MeshPoint ep12 = new MeshPoint()
{
X = position.X + piece[orientation].BoundingBox.Length,
Y = position.Y,
Z = 0
}; // Taking endpoint regarding x and projecting along the z-axis
MeshPoint ep21 = new MeshPoint()
{
X = 0,
Y = position.Y + piece[orientation].BoundingBox.Width,
Z = position.Z
}; // Taking endpoint regarding y and projecting along the x-axis
MeshPoint ep22 = new MeshPoint()
{
X = position.X,
Y = position.Y + piece[orientation].BoundingBox.Width,
Z = 0
}; // Taking endpoint regarding y and projecting along the z-axis
MeshPoint ep31 = new MeshPoint()
{
X = 0,
Y = position.Y,
Z = position.Z + piece[orientation].BoundingBox.Height
}; // Taking endpoint regarding z and projecting along the x-axis
MeshPoint ep32 = new MeshPoint()
{
X = position.X,
Y = 0,
Z = position.Z + piece[orientation].BoundingBox.Height
}; // Taking endpoint regarding z and projecting along the y-axis
// Add the EPs
AddEP(container, ep11);
AddEP(container, ep12);
AddEP(container, ep21);
AddEP(container, ep22);
AddEP(container, ep31);
AddEP(container, ep32);
}
}
/// <summary>
/// Prepares all the necessary information about the virtual pieces
/// </summary>
private void GenerateVirtualPieceInfo()
{
// Generate detailed virtual piece positions
foreach (var container in InstanceLinked.Containers)
{
foreach (var virtualPiece in container.VirtualPieces)
{
Positions[virtualPiece.VolatileID] = virtualPiece.FixedPosition;
Containers[virtualPiece.VolatileID] = container;
Orientations[virtualPiece.VolatileID] = virtualPiece.FixedOrientation;
OrientedPieces[virtualPiece.VolatileID] = virtualPiece[virtualPiece.FixedOrientation];
EndPointsBoundingBoxInner[virtualPiece.VolatileID].X = virtualPiece.FixedPosition.X;
EndPointsBoundingBoxOuter[virtualPiece.VolatileID].X = virtualPiece.FixedPosition.X + virtualPiece[virtualPiece.FixedOrientation].BoundingBox[8].X;
EndPointsBoundingBoxInner[virtualPiece.VolatileID].Y = virtualPiece.FixedPosition.Y;
EndPointsBoundingBoxOuter[virtualPiece.VolatileID].Y = virtualPiece.FixedPosition.Y + virtualPiece[virtualPiece.FixedOrientation].BoundingBox[8].Y;
EndPointsBoundingBoxInner[virtualPiece.VolatileID].Z = virtualPiece.FixedPosition.Z;
EndPointsBoundingBoxOuter[virtualPiece.VolatileID].Z = virtualPiece.FixedPosition.Z + virtualPiece[virtualPiece.FixedOrientation].BoundingBox[8].Z;
if (TetrisMode)
{
foreach (var component in virtualPiece[virtualPiece.FixedOrientation].Components)
{
EndPointsComponentInner[component.VolatileID].X = virtualPiece.FixedPosition.X + component[1].X;
EndPointsComponentOuter[component.VolatileID].X = virtualPiece.FixedPosition.X + component[8].X;
EndPointsComponentInner[component.VolatileID].Y = virtualPiece.FixedPosition.Y + component[1].Y;
EndPointsComponentOuter[component.VolatileID].Y = virtualPiece.FixedPosition.Y + component[8].Y;
EndPointsComponentInner[component.VolatileID].Z = virtualPiece.FixedPosition.Z + component[1].Z;
EndPointsComponentOuter[component.VolatileID].Z = virtualPiece.FixedPosition.Z + component[8].Z;
}
}
}
}
}
/// <summary>
/// Removes infeasible EPs
/// </summary>
/// <param name="container">The container to prone</param>
/// <param name="exhaustive">Exhaustiveness of prone</param>
public void ProneEPs(Container container, bool exhaustive)
{
if (!exhaustive)
{
ExtremePoints[container.VolatileID] = ExtremePoints[container.VolatileID].Distinct().ToList();
}
else
{
throw new NotImplementedException();
}
}
/// <summary>
/// Adds the specified EPs
/// </summary>
/// <param name="container">The container to add the EPs to</param>
/// <param name="eps">The EPs to add</param>
public void AddEPs(Container container, IEnumerable<MeshPoint> eps)
{
if (MeritType == MeritFunctionType.MRSU)
{
MeshPoint[] epArray = eps.ToArray();
foreach (var ep in epArray)
{
ep.VolatileID = EPCounter++;
ResidualSpace.Add(new MeshPoint() { X = container.Mesh.Length - ep.X, Y = container.Mesh.Width - ep.Y, Z = container.Mesh.Height - ep.Z });
}
ExtremePoints[container.VolatileID].AddRange(epArray);
}
else
{
ExtremePoints[container.VolatileID].AddRange(eps);
}
}
/// <summary>
/// Adds a single specified EP
/// </summary>
/// <param name="container">The container to add the EP to</param>
/// <param name="ep">The EP to add</param>
public void AddEP(Container container, MeshPoint ep)
{
if (MeritType == MeritFunctionType.MRSU)
{
ep.VolatileID = EPCounter++;
ResidualSpace.Add(new MeshPoint() { X = container.Mesh.Length - ep.X, Y = container.Mesh.Width - ep.Y, Z = container.Mesh.Height - ep.Z });
}
ExtremePoints[container.VolatileID].Add(ep);
}
/// <summary>
/// Removes the specified EPs
/// </summary>
/// <param name="container">The container to remove the EPs from</param>
/// <param name="ep">The EPs to remove</param>
public void RemoveEP(Container container, MeshPoint ep)
{
ExtremePoints[container.VolatileID].Remove(ep);
}
/// <summary>
/// Clears all EPs from the specified container
/// </summary>
/// <param name="container">The container to clear</param>
public void ClearEPs(Container container)
{
ExtremePoints[container.VolatileID].Clear();
}
#endregion
#region Enhanced piece positioning functionality
/// <summary>
/// Adds the piece at the given position in the given orientation to the container
/// </summary>
/// <param name="container">The container to add the piece to</param>
/// <param name="piece">The piece to add</param>
/// <param name="orientation">The orientation to use</param>
/// <param name="point">The position of the piece</param>
public void PositionPiece(Container container, VariablePiece piece, int orientation, MeshPoint point)
{
this.Add(container, piece, orientation, point);
// Update endpoints
EndPointsBoundingBoxInner[piece.VolatileID].X = point.X;
EndPointsBoundingBoxInner[piece.VolatileID].Y = point.Y;
EndPointsBoundingBoxInner[piece.VolatileID].Z = point.Z;
EndPointsBoundingBoxOuter[piece.VolatileID].X = point.X + this.OrientedPieces[piece.VolatileID].BoundingBox.Length;
EndPointsBoundingBoxOuter[piece.VolatileID].Y = point.Y + this.OrientedPieces[piece.VolatileID].BoundingBox.Width;
EndPointsBoundingBoxOuter[piece.VolatileID].Z = point.Z + this.OrientedPieces[piece.VolatileID].BoundingBox.Height;
if (EndPointsComponentInner != null)
{
foreach (var component in piece[orientation].Components)
{
EndPointsComponentInner[component.VolatileID].X = point.X + component.RelPosition.X;
EndPointsComponentInner[component.VolatileID].Y = point.Y + component.RelPosition.Y;
EndPointsComponentInner[component.VolatileID].Z = point.Z + component.RelPosition.Z;
EndPointsComponentOuter[component.VolatileID].X = point.X + component.RelPosition.X + component.Length;
EndPointsComponentOuter[component.VolatileID].Y = point.Y + component.RelPosition.Y + component.Width;
EndPointsComponentOuter[component.VolatileID].Z = point.Z + component.RelPosition.Z + component.Height;
}
}
// Update meta-info
switch (MeritType)
{
case MeritFunctionType.MMPSXY:
case MeritFunctionType.LPXY:
{
if (EndPointsBoundingBoxOuter[piece.VolatileID].X > PackingMaxX[container.VolatileID])
{
PackingMaxX[container.VolatileID] = EndPointsBoundingBoxOuter[piece.VolatileID].X;
}
if (EndPointsBoundingBoxOuter[piece.VolatileID].Y > PackingMaxY[container.VolatileID])
{
PackingMaxY[container.VolatileID] = EndPointsBoundingBoxOuter[piece.VolatileID].Y;
}
}
break;
case MeritFunctionType.MRSU:
{
foreach (var ep in ExtremePoints[container.VolatileID])
{
if (ep.Z >= point.Z && ep.Z < point.Z + piece[orientation].BoundingBox.Height)
{
if (ep.X <= point.X && point.Y <= ep.Y && ep.Y <= point.Y + piece[orientation].BoundingBox.Width)
{
ResidualSpace[ep.VolatileID].X = Math.Min(ResidualSpace[ep.VolatileID].X, point.X - ep.X);
}
if (ep.Y <= point.Y && point.X <= ep.X && ep.X <= point.X + piece[orientation].BoundingBox.Length)
{
ResidualSpace[ep.VolatileID].Y = Math.Min(ResidualSpace[ep.VolatileID].Y, point.Y - ep.Y);
}
}
if (ep.Z <= point.Z &&
point.X <= ep.X && ep.X <= point.X + piece[orientation].BoundingBox.Length &&
point.Y <= ep.Y && ep.Y <= point.Y + piece[orientation].BoundingBox.Width)
{
ResidualSpace[ep.VolatileID].Z = Math.Min(ResidualSpace[ep.VolatileID].Z, point.Z - ep.Z);
}
}
}
break;
case MeritFunctionType.None:
case MeritFunctionType.MFV:
default:
break;
}
}
/// <summary>
/// Adds the piece at the given position in the given orientation to the container
/// </summary>
/// <param name="container">The container to add the piece to</param>
/// <param name="piece">The piece to add</param>
/// <param name="box">The box which defines the insertion origin or anchor</param>
/// <param name="vertexID">The anchor to use of the specified box</param>
/// <param name="orientation">The orientation to use</param>
/// <param name="point">The position of the piece</param>
public void PositionPiece(Container container, VariablePiece piece, MeshCube box, int vertexID, int orientation, MeshPoint point)
{
MeshPoint insertionPoint = new MeshPoint()
{
X = point.X - ((box == null) ? piece[orientation].BoundingBox[vertexID].X : box[vertexID].X),
Y = point.Y - ((box == null) ? piece[orientation].BoundingBox[vertexID].Y : box[vertexID].Y),
Z = point.Z - ((box == null) ? piece[orientation].BoundingBox[vertexID].Z : box[vertexID].Z)
};
// Update endpoints
this.Add(container, piece, orientation, insertionPoint);
EndPointsBoundingBoxInner[piece.VolatileID].X = insertionPoint.X;
EndPointsBoundingBoxInner[piece.VolatileID].Y = insertionPoint.Y;
EndPointsBoundingBoxInner[piece.VolatileID].Z = insertionPoint.Z;
EndPointsBoundingBoxOuter[piece.VolatileID].X = insertionPoint.X + this.OrientedPieces[piece.VolatileID].BoundingBox.Length;
EndPointsBoundingBoxOuter[piece.VolatileID].Y = insertionPoint.Y + this.OrientedPieces[piece.VolatileID].BoundingBox.Width;
EndPointsBoundingBoxOuter[piece.VolatileID].Z = insertionPoint.Z + this.OrientedPieces[piece.VolatileID].BoundingBox.Height;
if (EndPointsComponentInner != null)
{
foreach (var component in piece[orientation].Components)
{
EndPointsComponentInner[component.VolatileID].X = insertionPoint.X + component.RelPosition.X;
EndPointsComponentInner[component.VolatileID].Y = insertionPoint.Y + component.RelPosition.Y;
EndPointsComponentInner[component.VolatileID].Z = insertionPoint.Z + component.RelPosition.Z;
EndPointsComponentOuter[component.VolatileID].X = insertionPoint.X + component.RelPosition.X + component.Length;
EndPointsComponentOuter[component.VolatileID].Y = insertionPoint.Y + component.RelPosition.Y + component.Width;
EndPointsComponentOuter[component.VolatileID].Z = insertionPoint.Z + component.RelPosition.Z + component.Height;
}
}
// Update meta-info
switch (MeritType)
{
case MeritFunctionType.MMPSXY:
case MeritFunctionType.LPXY:
{
if (EndPointsBoundingBoxOuter[piece.VolatileID].X > PackingMaxX[container.VolatileID])
{
PackingMaxX[container.VolatileID] = EndPointsBoundingBoxOuter[piece.VolatileID].X;
}
if (EndPointsBoundingBoxOuter[piece.VolatileID].Y > PackingMaxY[container.VolatileID])
{
PackingMaxY[container.VolatileID] = EndPointsBoundingBoxOuter[piece.VolatileID].Y;
}
}
break;
case MeritFunctionType.MRSU:
{
foreach (var component in piece[orientation].Components)
{
foreach (var ep in ExtremePoints[container.VolatileID])
{
if (ep.Z >= insertionPoint.Z && ep.Z < insertionPoint.Z + component.Height)
{
if (ep.X <= insertionPoint.X && insertionPoint.Y <= ep.Y && ep.Y <= insertionPoint.Y + component.Width)
{
ResidualSpace[ep.VolatileID].X = Math.Min(ResidualSpace[ep.VolatileID].X, insertionPoint.X - ep.X);
}
if (ep.Y <= insertionPoint.Y && insertionPoint.X <= ep.X && ep.X <= insertionPoint.X + component.Length)
{
ResidualSpace[ep.VolatileID].Y = Math.Min(ResidualSpace[ep.VolatileID].Y, insertionPoint.Y - ep.Y);
}
}
if (ep.Z <= insertionPoint.Z &&
insertionPoint.X <= ep.X && ep.X <= insertionPoint.X + component.Length &&
insertionPoint.Y <= ep.Y && ep.Y <= insertionPoint.Y + component.Width)
{
ResidualSpace[ep.VolatileID].Z = Math.Min(ResidualSpace[ep.VolatileID].Z, insertionPoint.Z - ep.Z);
}
}
}
}
break;
case MeritFunctionType.None:
case MeritFunctionType.MFV:
default:
break;
}
}
/// <summary>
/// Repositions a piece inside a container
/// </summary>
/// <param name="piece">The piece to reposition</param>
/// <param name="x">The x-value of the new position</param>
/// <param name="y">The y-value of the new position</param>
/// <param name="z">The z-value of the new position</param>
public void RepositionPiece(VariablePiece piece, double x, double y, double z)
{
this.Positions[piece.VolatileID].X = x;
this.Positions[piece.VolatileID].Y = y;
this.Positions[piece.VolatileID].Z = z;
EndPointsBoundingBoxInner[piece.VolatileID].X = x;
EndPointsBoundingBoxInner[piece.VolatileID].Y = y;
EndPointsBoundingBoxInner[piece.VolatileID].Z = z;