为什么我总是得到 0 的 X 和 Y 值。找到最优解但总是 x=0 y=0?

问题描述 投票:0回答:1

No matter what i did the result is always the same. Im not very good with this type of programming. Can someone please help me. 

解决方案始终是最优的,但结果是相同的。谢谢你。

using Google.OrTools.Sat;
using System;
using System.Windows.Forms;

class YourClass
{
    public void YourMethod(List<YourRectangleClass> smallRectangles, YourRectangleClass BigRect)
    {
        int numRectangles = smallRectangles.Count();
        CpModel model = new CpModel();
        IntVar[] x1 = new IntVar[numRectangles];
        IntVar[] y1 = new IntVar[numRectangles];
        IntVar[] width = new IntVar[numRectangles];
        IntVar[] height = new IntVar[numRectangles];
        IntervalVar[] rectanglesX = new IntervalVar[numRectangles];
        IntervalVar[] rectanglesY = new IntervalVar[numRectangles];
        IntVar BigRectArea = model.NewIntVar(0, BigRect.UWidth() * BigRect.UHeight(), "area");
        uint TotalPieceArea = 0;

        for (int i = 0; i < numRectangles; i++)
        {
            x1[i] = model.NewIntVar(0, smallRectangles[i].UWidth(), $"X_{i}");
            y1[i] = model.NewIntVar(0, smallRectangles[i].UHeight(), $"Y_{i}");
            TotalPieceArea += (uint)(smallRectangles[i].UWidth() * smallRectangles[i].UHeight());
            width[i] = model.NewIntVar(0, BigRect.UWidth(), $"Width_{i}");
            height[i] = model.NewIntVar(0, BigRect.UHeight(), $"Height_{i}");
`

`
            // Create IntervalVar for the rectangle with derived bounds
            rectanglesX[i] = model.NewIntervalVar(x1[i], width[i], x1[i] + smallRectangles[i].UWidth(), $"RectX_{i}");
            rectanglesY[i] = model.NewIntervalVar(y1[i], height[i], y1[i] + smallRectangles[i].UHeight(), $"RectY_{i}");
        }

        for (int i = 0; i < numRectangles; i++)
        {
            model.AddNoOverlap2D().AddRectangle(rectanglesX[i], rectanglesY[i]);
        }

        // Minimize the area not used
        model.Minimize(BigRectArea - (int)TotalPieceArea);

        CpSolver solver = new CpSolver();
        CpSolverStatus status = solver.Solve(model);

        if (status == CpSolverStatus.Optimal)
        {
            for (int j = 0; j < numRectangles; j++)
            {
                smallRectangles[j].X = Convert.ToDouble(solver.Value(x1[j]));
                smallRectangles[j].Y = Convert.ToDouble(solver.Value(y1[j]));
            }
        }
        else
        {
            MessageBox.Show("No optimal solution found TILE.");
        }
    }
}
optimization or-tools tile
1个回答
0
投票

bigRectArea
是无约束的,
TotalPieceArea
是常数。所以简单地说,最好的解决方案是
bigRectArea == TotalPieceArea

然后为每个矩形添加 1 个 NoOverlap2D 约束。所以每个矩形都是不受约束的。

如果您添加启用日志记录(https://github.com/google/or-tools/blob/main/ortools/sat/docs/troubleshooting.md#enable-logging),您会看到所有约束在预求解期间被删除。

在这种情况下,求解器会将所有变量修复为 0 并退出。

© www.soinside.com 2019 - 2024. All rights reserved.