Advertisement
SpaceRiver

Untitled

Feb 22nd, 2023
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using SimpleDb;
  5. using System;
  6.  
  7. namespace Algorithms {
  8.    
  9.     public abstract class Algorithm {
  10.         protected Profile Profile;
  11.  
  12.         protected List<Box> Boxes;
  13.         protected Dimensions ContainerDimensions;
  14.         protected string? ExtraPreferences;
  15.         protected int ProblemId;
  16.  
  17.         protected Solution Solution;
  18.  
  19.         public Algorithm(Profile profile) {
  20.             Boxes = null;
  21.             ContainerDimensions = null;
  22.             ExtraPreferences = null;
  23.             Solution = null;
  24.             Profile = profile;
  25.         }
  26.  
  27.         public abstract Solution Solve(Problem problem);
  28.  
  29.         protected List<Dimensions> GetRotations(Dimensions dimensions) {
  30.             List<Dimensions> rotations = new List<Dimensions>();
  31.             rotations.Add(dimensions);
  32.             rotations.Add(new Dimensions(dimensions.Height, dimensions.Width, dimensions.Length));
  33.             rotations.Add(new Dimensions(dimensions.Length, dimensions.Height, dimensions.Width));
  34.             return rotations;
  35.         }
  36.  
  37.         public Solution GetSolution(Problem problem) {
  38.             if (Boxes == null || ContainerDimensions == null) {
  39.                 throw new Exception("Boxes or ContainerDimensions not set");
  40.             } else {
  41.                 if (Solution == null) {
  42.                     Solution = Solve(problem);
  43.                 }
  44.                 return Solution;
  45.             }
  46.         }
  47.     }
  48.  
  49.     public class UniformAlgorithm : Algorithm {
  50.         public UniformAlgorithm(Profile profile) : base(profile) { }
  51.  
  52.  
  53.         public override Solution Solve(Problem Problem) {
  54.             Boxes = Problem.Boxes;
  55.             ContainerDimensions = Problem.ContainerDimensions;
  56.             ExtraPreferences = Problem.ExtraPreferences;
  57.             ProblemId = Problem.Id;
  58.             if (!CheckUniform()) {
  59.                 throw new Exception("Boxes are not uniform");
  60.             }
  61.             // try fit boxes in container without rotation
  62.             Dimensions firstBoxDimensions = Boxes[0].Dimensions;
  63.             List<Dimensions> rotations = GetRotations(firstBoxDimensions);
  64.             foreach (Dimensions rotation in rotations) {
  65.                 if(Fits(rotation)) {
  66.                     return ArrangeBoxes(rotation);
  67.                 }
  68.             }
  69.             throw new Exception("Boxes do not fit in container");
  70.         }
  71.  
  72.         private Solution ArrangeBoxes(Dimensions boxDimensions) {
  73.             int x = Convert.ToInt32(ContainerDimensions.Width / boxDimensions.Width);
  74.             int y = Convert.ToInt32(ContainerDimensions.Height / boxDimensions.Height);
  75.             int z = Convert.ToInt32(ContainerDimensions.Length / boxDimensions.Length);
  76.             int boxCount = Boxes.Count;
  77.             List<BoxPlacement> boxPlacements = new List<BoxPlacement>();
  78.             for (int i = 0; i < boxCount; i++) {
  79.                 int xIndex = i % x;
  80.                 int yIndex = (i / x) % y;
  81.                 int zIndex = i / (x * y);
  82.                 Position position = new Position();
  83.                 position.x = xIndex * boxDimensions.Width;
  84.                 position.y = yIndex * boxDimensions.Height;
  85.                 position.z = zIndex * boxDimensions.Length;
  86.                 BoxPlacement boxPlacement = new BoxPlacement();
  87.                 Box box = Boxes[i];
  88.                 box.Dimensions = boxDimensions;
  89.                 boxPlacement.Box = box;
  90.                 boxPlacement.Position = position;
  91.                 boxPlacements.Add(boxPlacement);
  92.             }
  93.             Solution solution = new Solution();
  94.             solution.Placements = boxPlacements;
  95.             solution.ProblemId = ProblemId;
  96.             solution.ProfileId = Profile.Id;
  97.             return solution;
  98.         }
  99.         private bool Fits(Dimensions boxDimensions) {
  100.             int x = Convert.ToInt32(ContainerDimensions.Width / boxDimensions.Width);
  101.             int y = Convert.ToInt32(ContainerDimensions.Height / boxDimensions.Height);
  102.             int z = Convert.ToInt32(ContainerDimensions.Length / boxDimensions.Length);
  103.             int maxBoxes = x * y * z;
  104.             int boxCount = Boxes.Count;
  105.             return maxBoxes >= boxCount;
  106.         }
  107.  
  108.         // checks all the boxes are the same size
  109.         private bool CheckUniform() {
  110.             if (Boxes.Count == 0) {
  111.                 return false;
  112.             }
  113.             Dimensions firstBoxDimensions = Boxes[0].Dimensions;
  114.             foreach (Box box in Boxes) {
  115.                 if (box.Dimensions.Height != firstBoxDimensions.Height || box.Dimensions.Width != firstBoxDimensions.Width || box.Dimensions.Length != firstBoxDimensions.Length) {
  116.                     return false;
  117.                 }
  118.             }
  119.             return true;
  120.         }
  121.     }
  122.     public class UniformOrderedAlgorithm : Algorithm {
  123.         public UniformOrderedAlgorithm(Profile profile) : base(profile) { }
  124.  
  125.         public override Solution Solve(Problem problem) {
  126.             Algorithm algorithm = new UniformAlgorithm(Profile);
  127.             // reverse the order
  128.             // order boxes based on ORderInList, where the biggest numbers will be first
  129.             problem.Boxes.Sort((x, y) => y.OrderInList.CompareTo(x.OrderInList));
  130.             Solution solution = algorithm.Solve(problem);
  131.             return solution;
  132.         }
  133.     }
  134.  
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement