如何解决二维装箱算法 ASP.Net MVC 根问题
我正在制作一个带有二维垃圾箱包装系统的网站。我正在使用一种我在网上找到的算法,名为 2D-Bin Pack Binary Search。问题是我需要从客户那里获取宽度和长度。并且客户可能想要订购包含相同产品和子类别等的另一产品。因此公司也需要为第二个产品使用相同的材料。我有材料的标准宽度值。如果我的宽度 x 长度大于标准,我会为第一个产品优化它,没有问题。但是当我尝试将第二个产品添加到我的购物车时,其值也大于标准值,我仍然得到 0x0 pos 值。对不起,我的语法等等,但让我向您展示代码。
var standart = db.Sepet.Where(x => x.MusteriId == musteriid && x.UrunId == urun && x.KategoriId == kategori && x.OzellikId == ozellik).FirstOrDefault();
double isMetrekare = 0;
isMetrekare = ((standart.standarten) * (Boy)) / 10000;
double GidenKategoriFiyat = (isMetrekare * sonkategorifiyat);
double GidenBaskiFiyat = (isMetrekare * sonbaskifiyat);
double toplamfiyat = (GidenBaskiFiyat) + (GidenKategoriFiyat);
double karmarji = (toplamfiyat * 40) / 100;
double sonGidenFiyat = ((karmarji) + (toplamfiyat));
Sepet sepet1 = new Sepet();
sepet1.en = En;
sepet1.boy = Boy;
sepet1.standarten = standart.standarten;
sepet1.urunfiyat = 0;
sepet1.x_pozisyon = 0;
sepet1.y_pozisyon = 0;
sepet1.UrunId = urun;
sepet1.KategoriId = kategori;
sepet1.OzellikId = ozellik;
sepet1.BaskiId = baski;
sepet1.MusteriAdresId = DrpMusteriAdres;
sepet1.MusteriId = musteriid;
db.Sepet.Add(sepet1);
db.SaveChanges();
//////////#region1
this.containerWidth = standart.standarten;
var sepettbl1 = db.Sepet.Where(x => x.MusteriId == musteriid && x.UrunId == urun && x.KategoriId == kategori && x.OzellikId == ozellik).ToList();
_Boxes = new List<Box>();
foreach (var item in sepettbl1)
{
Box Box = new Box();
Box.length = item.boy;
Box.width = item.en;
Box.volume = (item.en * item.boy);
Box.sepetid = item.SepetId;
Debug.WriteLine(item.SepetId);
_Boxes.Add(Box);
}
// Sort Boxes into descending order based on volume
_Boxes = _Boxes.OrderByDescending(x => x.volume).ToList();
// Initialize root node
var positionx = "";
var positionz = "";
this.rootNode = new Node { length = containerLength,width = containerWidth };
Pack();
foreach (var Box in _Boxes)
{
var xlist = db.Sepet.Where(x => x.MusteriId == musteriid && x.UrunId == urun && x.KategoriId == kategori && x.OzellikId == ozellik && x.SepetId == Box.sepetid).ToList();
foreach (var girdi in xlist)
{
positionx = Box.position != null ? Box.position.pos_x.ToString() : String.Empty;
positionz = Box.position != null ? Box.position.pos_z.ToString() : String.Empty;
try
{
girdi.x_pozisyon = Convert.Todouble(positionx);
girdi.y_pozisyon = Convert.Todouble(positionz);
}
catch(Exception x)
{
Debug.WriteLine(x.Message);
}
girdi.en = Box.width;
girdi.boy = Box.length;
girdi.standarten = standart.standarten;
girdi.urunfiyat = ((((Box.volume / 10000) * sonbaskifiyat) + ((Box.volume / 10000) * sonkategorifiyat)) / 10000) + (((((Box.volume / 10000) * sonbaskifiyat) + ((Box.volume / 10000) * sonkategorifiyat)) / 10000) * 40) / 100;
girdi.UrunId = urun;
girdi.KategoriId = kategori;
girdi.OzellikId = ozellik;
girdi.BaskiId = baski;
girdi.MusteriAdresId = DrpMusteriAdres;
girdi.MusteriId = musteriid;
db.SaveChanges();
continue;
}
}
//}
}
我从开始到 region1 所做的是从客户那里获取宽度和长度,然后将其添加到没有位置的数据库中。 在 region1 之后,我从数据库中获取了最后一个包含的所有数据,我使用我的算法来比较大小和体积,从而为它们中的每一个提供它们的位置值。我猜这适用于较小的值,然后是标准宽度或 rootnode 的体积。我应该说我无法理解算法究竟是如何工作的。让我分享算法的代码。
internal class Node
{
public Node rightNode;
public Node bottomNode;
public double pos_x;
public double pos_z;
public double width;
public double length;
public bool isOccupied;
}
internal class Box
{
public double length;
public double width;
public double volume;
public Node position;
public int sepetid;
}
private double containerWidth = 500;
private double containerLength = 5000;
private List<Box> _Boxes;
private Node rootNode;
private void Pack()
{
foreach (var Box in _Boxes)
{
var node = FindNode(rootNode,Box.width,Box.length);
if (node != null)
{
// Split rectangles
Box.position = SplitNode(node,Box.length);
}
}
}
private Node FindNode(Node rootNode,double BoxWidth,double BoxLength)
{
if (rootNode.isOccupied)
{
var nextNode = FindNode(rootNode.bottomNode,BoxWidth,BoxLength);
if (nextNode == null)
{
nextNode = FindNode(rootNode.rightNode,BoxLength);
}
return nextNode;
}
else if (BoxWidth <= rootNode.width && BoxLength <= rootNode.length)
{
return rootNode;
}
else
{
return null;
}
}
private Node SplitNode(Node node,double BoxLength)
{
node.isOccupied = true;
node.bottomNode = new Node { pos_z = node.pos_z,pos_x = node.pos_x + BoxWidth,length = node.length,width = node.width - BoxWidth };
node.rightNode = new Node { pos_z = node.pos_z + BoxLength,pos_x = node.pos_x,length = node.length - BoxLength,width = BoxWidth };
return node;
}
我希望这足以帮助我,如果您愿意提供帮助,我可以提供更多代码。请不要讽刺我,因为我还是个学生,这是我的第一个专业作品。如果你能帮上忙,我很高兴。谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。