cvxpy 解算器产生不同形状的解?

cvxpy solvers produce solutions of different shapes?

我正在尝试在后台使用 maxrect's get_maximal_rectangle, which uses cvxpy

使用 shapely 多边形:POLYGON ((0 0.95, 0 2, 2 2, 2 1.95, 0 0.95))

max_hull = shapely.wkt.loads('POLYGON ((0 0.95, 0 2, 2 2, 2 1.95, 0 0.95))') # this is a sample of a dynamically generated valid, simple, convex polygon
max_rect = get_maximal_rectangle(max_hull.exterior.coords[:-1])
print('max rectangle', max_rect)

这会产生错误

Either candidate conic solvers (['CVXOPT']) do not support the cones output by the problem (ExpCone, NonNeg, Zero), or there are not enough constraints in the problem.

所以我检查了 maxrect 的 github 问题。其他人报告了同样的问题。他们通过修改对 cvx solve 的调用来解决它。因此,我下载了源代码并修改了解决问题的调用:prob.solve(solver=cvxpy.CVXOPT, verbose=False, max_iters=1000, reltol=1e-9),一开始只是将其称为空 prob.solve(),但这会产生

Solver 'ECOS' failed. Try another solver, or solve with verbose=True for more information.

(在“Invoking solver ECOS to obtain a solution”之后 verbose 没有给我额外的细节)

所以我想使用 SCS。它实际上有效,但这里的解决方案值没有正确的形状(!?)。 get_maximal_rectangle 函数想要 return list(bottom_left[0]), list(top_right[0]),但这引发了

TypeError: 'numpy.float64' object is not iterable

如果我打印左下角并且 top_right 我看到 4 个值:

[-3.86442831e+01 -2.49322910e+06] [ 8.14325412e+02 -2.03083800e+06]

但是很难将它们视为坐标,因为 1. 它们是负数,所以它们没有刻在我的多边形中,并且 2. 求解器似乎应该 return 与 CVXOPT 具有相同的形状。

我成功定制了这个库以供现代使用。

    prob = cvxpy.Problem(obj, constraints)
    #prob.solve(solver=cvxpy.CVXOPT, verbose=False, max_iters=1000, reltol=1e-9)
    prob.solve()

    bottom_left = np.array(bl.value).T * scale
    top_right = np.array(tr.value).T * scale
    
    #return list(bottom_left[0]), list(top_right[0])
    return bottom_left, top_right

将结果转换回形状对象:

    pa = get_maximal_rectangle(maximum_plot.exterior.coords)
    return Polygon([(pa[0][0],pa[0][1]), (pa[0][0],pa[1][1]), (pa[1][0],pa[1][1]), (pa[1][0],pa[0][1])])