Why am I always getting 0 values for X and Y . Optimal Solution is found but always x=0 y=0?

49 Views Asked by At

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.

The solution is always optimal but the results are the same. Thank you.

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.");
        }
    }
}
1

There are 1 best solutions below

0
Laurent Perron On

bigRectArea is unconstrained, TotalPieceArea is a constant. So trivially, the best solution is bigRectArea == TotalPieceArea.

Then you add 1 NoOverlap2D constraint per rectangle. So each rectangle is unconstrained.

If you add enabled the logging (https://github.com/google/or-tools/blob/main/ortools/sat/docs/troubleshooting.md#enable-logging), you would have seen that all constraints are removed during presolve.

In that case, the solver will fix all variables to 0 and call it quit.