Пример #1
0
        public void Parse(Stream stream)
        {
            var reader    = new StreamReader(stream);
            var length    = GetNextInteger(reader);
            var width     = GetNextInteger(reader);
            var height    = GetNextInteger(reader);
            var maxWeight = GetNextInteger(reader);

            Bin   = new PackingShape(width, height, length);
            Items = new List <PackingItem>();
            while (true)
            {
                try {
                    var id     = GetNextInteger(reader);
                    var pieces = GetNextInteger(reader);
                    length = GetNextInteger(reader);
                    width  = GetNextInteger(reader);
                    height = GetNextInteger(reader);
                    var weight   = GetNextInteger(reader);
                    var stack    = GetNextInteger(reader);
                    var material = GetNextInteger(reader);
                    var rotate   = GetNextInteger(reader);
                    var tilt     = GetNextInteger(reader);
                    for (var i = 0; i < pieces; i++)
                    {
                        Items.Add(new PackingItem(width, height, length, Bin, weight, material));
                    }
                } catch (InvalidOperationException) {
                    break;
                }
            }
        }
Пример #2
0
 public PackingItem(int width, int height, int depth, PackingShape targetBin)
     : this()
 {
     this.Width     = width;
     this.Height    = height;
     this.Depth     = depth;
     this.TargetBin = (PackingShape)targetBin.Clone();
 }
        public override double EvaluateMove(Permutation permutation, Swap2Move move, PackingShape binShape, ReadOnlyItemList <PackingItem> items, bool useStackingConstraints)
        {
            // uses full evaluation
            Swap2Manipulator.Apply(permutation, move.Index1, move.Index2);
            var solution = DecoderParameter.ActualValue.Decode(permutation, binShape, items, useStackingConstraints);

            return(SolutionEvaluatorParameter.ActualValue.Evaluate(solution));
        }
Пример #4
0
 public CuboidDiagonal(PackingPosition myPosition, PackingShape myShape)
 {
     x1 = myPosition.X;
     y1 = myPosition.Y;
     z1 = myPosition.Z;
     x2 = myPosition.X + (myPosition.Rotated ? myShape.Depth : myShape.Width) - 1;
     y2 = myPosition.Y + myShape.Height - 1;
     z2 = myPosition.Z + (myPosition.Rotated ? myShape.Width : myShape.Depth) - 1;
 }
 public override Solution Decode(IntegerVector intVec, PackingShape binShape, IList <PackingItem> items, bool useStackingConstraints)
 {
     // TODO
     if (useStackingConstraints)
     {
         throw new NotSupportedException("Stacking constraints are not supported by the Bottom-Left IntegerVector Decoder");
     }
     return(base.Decode(intVec, binShape, items, useStackingConstraints: false));
 }
Пример #6
0
 public PackingItem(int width, int height, int depth, PackingShape targetBin, double weight, int material)
     : this()
 {
     this.Width     = width;
     this.Height    = height;
     this.Depth     = depth;
     this.Weight    = weight;
     this.Material  = material;
     this.TargetBin = (PackingShape)targetBin.Clone();
 }
        public virtual Solution Decode(IntegerVector intVec, PackingShape binShape, IList <PackingItem> items, bool useStackingConstraints)
        {
            var      sequenceMatrix = IntegerVectorProblem.GenerateSequenceMatrix(intVec);
            Solution result         = CreateSolution(binShape, useStackingConstraints);

            //Fill bins according to grouping vector
            IList <int> remainingIDs = new List <int>();

            foreach (var sequence in sequenceMatrix)
            {
                remainingIDs = remainingIDs.Concat(sequence).ToList();
                result.Bins.Add(CreatePacking(result, ref remainingIDs, items, useStackingConstraints));
            }
            result.UpdateBinPackings();

            //Try to put remaining items in existing bins
            var temp = new List <int>(remainingIDs);

            foreach (int id in temp)
            {
                foreach (BinPacking3D bp in result.Bins)
                {
                    var position = FindPositionForItem(bp, items[id], useStackingConstraints);
                    if (position != null)
                    {
                        bp.PackItem(id, items[id], position);
                        remainingIDs.Remove(id);
                        break;
                    }
                }
            }

            //Put still remaining items in new bins
            while (remainingIDs.Count > 0)
            {
                result.Bins.Add(CreatePacking(result, ref remainingIDs, items, useStackingConstraints));
            }
            result.UpdateBinPackings();

            // gkronber: original implementation by Helm also updates the encoded solution (TODO)
            // var newSolution = new int[intVec.Length];
            // int binIndex = 0;
            // foreach (var bp in result.BinPackings) {
            //   foreach (var entry in bp.ItemPositions)
            //     newSolution[entry.Key] = binIndex;
            //   binIndex++;
            // }
            // solution.GroupingVector = new IntegerVector(newSolution);

            return(result);
        }
        public Solution Decode(Permutation permutation, PackingShape binShape, IList <PackingItem> items, bool useStackingConstraints)
        {
            Solution    result       = new Solution(binShape, useExtremePoints: true, stackingConstraints: useStackingConstraints);
            IList <int> remainingIDs = new List <int>(permutation);

            while (remainingIDs.Count > 0)
            {
                var bp = new BinPacking3D(binShape);
                bp.ExtremePointBasedPacking(ref remainingIDs, items, stackingConstraints: useStackingConstraints);
                result.Bins.Add(bp);
            }
            result.UpdateBinPackings();
            return(result);
        }
Пример #9
0
        public int CompareTo(PackingShape other)
        {
            //Using "Clustered-Area-Height"-comparison as descr

            int result = (this.Width * this.Depth).CompareTo(other.Width * other.Depth);

            if (result == 0)
            {
                result = this.Volume.CompareTo(other.Volume);
            }
            if (result == 0)
            {
                result = this.Height.CompareTo(other.Height);
            }
            return(result);
        }
        public static Solution Apply(Permutation permutation, PackingShape binShape, IList <PackingItem> items, bool useStackingConstraints)
        {
            Solution    result       = new Solution(binShape, useExtremePoints: true, stackingConstraints: useStackingConstraints);
            IList <int> remainingIDs = new List <int>(permutation);
            var         bpg          = new BinPacking3D(binShape);

            bpg.ExtremePointBasedPacking(ref remainingIDs, items, stackingConstraints: useStackingConstraints);
            result.Bins.Add(bpg);
            foreach (int ID in remainingIDs)
            {
                var item         = items[ID];
                var points       = GetResidualSpaceAllPoints(result, item);
                var sortedPoints = points.OrderBy(x => x.Item3);
                var packed       = false;
                foreach (var p in sortedPoints)
                {
                    packed = p.Item1.PackItemIfFeasible(ID, item, p.Item2, useStackingConstraints);
                    if (packed)
                    {
                        break;
                    }
                }
                if (!packed)
                {
                    // pack item in a new bin
                    var bp            = new BinPacking3D(binShape);
                    var positionFound = bp.FindExtremePointForItem(item, false, useStackingConstraints);
                    if (positionFound != null)
                    {
                        bp.PackItem(ID, item, positionFound);
                    }
                    else
                    {
                        throw new InvalidOperationException("Item " + ID + " cannot be packed in an empty bin.");
                    }
                    result.Bins.Add(bp);
                }
            }
            result.UpdateBinPackings();
            return(result);
        }
Пример #11
0
        public static Solution Apply(Permutation permutation, PackingShape binShape, IList <PackingItem> items, bool useStackingConstraints)
        {
            Solution    result       = new Solution(binShape, useExtremePoints: true, stackingConstraints: useStackingConstraints);
            IList <int> remainingIDs = new List <int>(permutation);
            var         bpg          = new BinPacking3D(binShape);

            bpg.ExtremePointBasedPacking(ref remainingIDs, items, stackingConstraints: useStackingConstraints);
            result.Bins.Add(bpg);
            foreach (int ID in remainingIDs)
            {
                var sortedBins = result.Bins.OrderBy(x => x.FreeVolume);
                var item       = items[ID];
                var posFound   = false;
                foreach (var bp in sortedBins)
                {
                    var pos = bp.FindExtremePointForItem(item, false, useStackingConstraints);
                    posFound = pos != null;
                    if (posFound)
                    {
                        bp.PackItem(ID, item, pos);
                        break;
                    }
                }
                if (!posFound)
                {
                    var bp  = new BinPacking3D(binShape);
                    var pos = bp.FindExtremePointForItem(item, false, useStackingConstraints);
                    if (pos == null)
                    {
                        throw new InvalidOperationException("Item " + ID + " cannot be packed in empty bin.");
                    }
                    bp.PackItem(ID, item, pos);
                    result.Bins.Add(bp);
                }
            }
            result.UpdateBinPackings();
            return(result);
        }
Пример #12
0
        private Tuple <Solution, double, SortingMethod?, FittingMethod?> GetBest(PackingShape bin, IList <PackingItem> items, SortingMethod[] sortings, FittingMethod[] fittings, CancellationToken token)
        {
            SortingMethod?bestSorting  = null;
            FittingMethod?bestFitting  = null;
            var           best         = double.NaN;
            Solution      bestSolution = null;

            foreach (var fit in fittings)
            {
                foreach (var sort in sortings)
                {
                    var result = Optimize(bin, items, sort, fit, DeltaParameter.Value.Value, Problem.UseStackingConstraints, Problem.SolutionEvaluator, token);
                    if (double.IsNaN(result.Item2) || double.IsInfinity(result.Item2))
                    {
                        continue;
                    }
                    if (double.IsNaN(best) ||
                        Problem.Maximization && result.Item2 > best ||
                        !Problem.Maximization && result.Item2 < best)
                    {
                        bestSolution = result.Item1;
                        best         = result.Item2;
                        bestSorting  = sort;
                        bestFitting  = fit;
                    }
                    if (token.IsCancellationRequested)
                    {
                        return(Tuple.Create(bestSolution, best, bestSorting, bestFitting));
                    }
                }
            }
            if (double.IsNaN(best))
            {
                return(null);
            }
            return(Tuple.Create(bestSolution, best, bestSorting, bestFitting));
        }
Пример #13
0
 private bool Encloses(PackingPosition checkedPosition, PackingShape checkedShape)
 {
     return(Encloses(new CuboidDiagonal(this), new CuboidDiagonal(checkedPosition, checkedShape)));
 }
 public abstract double EvaluateMove(TSol encodedSolutionCandidate, TMove move, PackingShape binShape, ReadOnlyItemList <PackingItem> items, bool useStackingConstraints);
Пример #15
0
        private static Tuple <Solution, double> Optimize(PackingShape bin, IList <PackingItem> items, SortingMethod sorting, FittingMethod fitting, double delta, bool stackingConstraints, IEvaluator evaluator, CancellationToken token)
        {
            Permutation sorted = null;

            switch (sorting)
            {
            case SortingMethod.Given:
                sorted = new Permutation(PermutationTypes.Absolute, Enumerable.Range(0, items.Count).ToArray());
                break;

            case SortingMethod.VolumeHeight:
                sorted = new Permutation(PermutationTypes.Absolute,
                                         items.Select((v, i) => new { Index = i, Item = v })
                                         .OrderByDescending(x => x.Item.Depth * x.Item.Width * x.Item.Height)
                                         .ThenByDescending(x => x.Item.Height)
                                         .Select(x => x.Index).ToArray());
                break;

            case SortingMethod.HeightVolume:
                sorted = new Permutation(PermutationTypes.Absolute,
                                         items.Select((v, i) => new { Index = i, Item = v })
                                         .OrderByDescending(x => x.Item.Height)
                                         .ThenByDescending(x => x.Item.Depth * x.Item.Width * x.Item.Height)
                                         .Select(x => x.Index).ToArray());
                break;

            case SortingMethod.AreaHeight:
                sorted = new Permutation(PermutationTypes.Absolute,
                                         items.Select((v, i) => new { Index = i, Item = v })
                                         .OrderByDescending(x => x.Item.Depth * x.Item.Width)
                                         .ThenByDescending(x => x.Item.Height)
                                         .Select(x => x.Index).ToArray());
                break;

            case SortingMethod.HeightArea:
                sorted = new Permutation(PermutationTypes.Absolute,
                                         items.Select((v, i) => new { Index = i, Item = v })
                                         .OrderByDescending(x => x.Item.Height)
                                         .ThenByDescending(x => x.Item.Depth * x.Item.Width)
                                         .Select(x => x.Index).ToArray());
                break;

            case SortingMethod.ClusteredAreaHeight:
                double clusterRange = bin.Width * bin.Depth * delta;
                sorted = new Permutation(PermutationTypes.Absolute,
                                         items.Select((v, i) => new { Index = i, Item = v, ClusterId = (int)(Math.Ceiling(v.Width * v.Depth / clusterRange)) })
                                         .GroupBy(x => x.ClusterId)
                                         .Select(x => new { Cluster = x.Key, Items = x.OrderByDescending(y => y.Item.Height).ToList() })
                                         .OrderByDescending(x => x.Cluster)
                                         .SelectMany(x => x.Items)
                                         .Select(x => x.Index).ToArray());
                break;

            case SortingMethod.ClusteredHeightArea:
                double clusterRange2 = bin.Height * delta;
                sorted = new Permutation(PermutationTypes.Absolute,
                                         items.Select((v, i) => new { Index = i, Item = v, ClusterId = (int)(Math.Ceiling(v.Height / clusterRange2)) })
                                         .GroupBy(x => x.ClusterId)
                                         .Select(x => new { Cluster = x.Key, Items = x.OrderByDescending(y => y.Item.Depth * y.Item.Width).ToList() })
                                         .OrderByDescending(x => x.Cluster)
                                         .SelectMany(x => x.Items)
                                         .Select(x => x.Index).ToArray());
                break;

            default: throw new ArgumentException("Unknown sorting method: " + sorting);
            }

            ExtremePointPermutationDecoderBase decoder = null;

            switch (fitting)
            {
            case FittingMethod.FirstFit:
                decoder = new ExtremePointPermutationDecoder();
                break;

            case FittingMethod.FreeVolumeBestFit:
                decoder = new FreeVolumeBestFitExtremePointPermutationDecoder();
                break;

            case FittingMethod.ResidualSpaceBestFit:
                decoder = new ResidualSpaceBestFitExtremePointPermutationDecoder();
                break;

            default: throw new ArgumentException("Unknown fitting method: " + fitting);
            }

            var sol = decoder.Decode(sorted, bin, items, stackingConstraints);
            var fit = evaluator.Evaluate(sol);

            return(Tuple.Create(sol, fit));
        }
 protected abstract Solution CreateSolution(PackingShape binShape, bool useStackingConstraints);
 public override Solution Decode(Permutation permutation, PackingShape binShape, IList <PackingItem> items, bool useStackingConstraints)
 {
     return(Apply(permutation, binShape, items, useStackingConstraints));
 }
Пример #18
0
 public CuboidDiagonal(PackingShape myShape) : this(new PackingPosition(0, 0, 0, 0), myShape)
 {
 }
Пример #19
0
 protected PackingShape(PackingShape original, Cloner cloner)
     : base(original, cloner)
 {
     RegisterEvents();
 }
Пример #20
0
 private bool Overlaps(PackingPosition myPosition, PackingPosition checkedPosition, PackingShape checkedShape)
 {
     return(Overlaps(new CuboidDiagonal(myPosition, this), new CuboidDiagonal(checkedPosition, checkedShape)));
 }
Пример #21
0
 public abstract Solution Decode(Permutation permutation, PackingShape binShape, IList <PackingItem> items, bool useStackingConstraints);
Пример #22
0
 public override bool Encloses(PackingPosition checkedPosition, PackingShape <PackingPosition> checkedShape)
 {
     return(Encloses(checkedPosition, (PackingShape)checkedShape));
 }
Пример #23
0
 protected override Solution CreateSolution(PackingShape binShape, bool useStackingConstraints)
 {
     return(new Solution(binShape, useExtremePoints: true, stackingConstraints: useStackingConstraints));
 }
Пример #24
0
 public override bool Overlaps(PackingPosition myPosition, PackingPosition checkedPosition, PackingShape <PackingPosition> checkedShape)
 {
     return(Overlaps(myPosition, checkedPosition, (PackingShape)checkedShape));
 }