首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >假设我们知道一个多边形的顶点,并想得到它的半空间表示A*x <= b

假设我们知道一个多边形的顶点,并想得到它的半空间表示A*x <= b
EN

Stack Overflow用户
提问于 2020-07-20 20:22:39
回答 1查看 412关注 0票数 1

我想得到半空间表示A*x <= b,给定一个多边形的顶点,无论是python还是Matlab。

假设vertices = [2 -2; 2 2; -10 2; -10 -2];是顶点,我使用了两个不同的库,给出了两个不同的答案,不确定它们为什么给出了不同的答案。

  • 使用https://github.com/stephane-caron/pypoman, 从pypoman导入compute_polytope_halfspaces顶点=map(数组,[2,- 2,2,-10,2,-10,-2]) A,b=compute_polytope_halfspaces(顶点) print(A) print(b) 产出: A=[ -0.00000000e+00 -1.00000000e+00 4.93432455e-17 1.00000000e+00] b=2.10.2.2。
  • 使用多参数工具箱3 (http://people.ee.ethz.ch/~mpt/3/) (Matlab) 顶点=2-2;2;-102;-10-2;Xc =多面体(顶点); 输出: >> Xc.A ans = 0 -0.4472 0.4472 -0.0000 0 0.4472 -0.0995 -0.0000 >> Xc.b ans = 0.8944 0.8944 0.8944 0.9950

任何事情都能帮助我们理解为什么会发生这种事

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-06 01:45:41

使用package polytope,可以从多边形的顶点计算凸多边形的半空间表示,如下所示:

从顶点表示到半空间表示

代码语言:javascript
运行
复制
"""How to compute the halfspace representation of a convex polytope.

The polytope is given in vertex representation,
and this script shows how to convert the vertex representation
to a halfspace representation.
"""
import numpy as np
import polytope

vertices = np.array([[2, -2], [2, 2], [-10, 2], [-10, -2]])
poly = polytope.qhull(vertices)  # convex hull
    # `poly` is an instance of the class `polytope.polytope.Polytope`,
    # which is for representing convex polytopes.
    # Nonconvex polytopes can be represented too,
    # using the class `polytope.polytope.Region`.
print('Halfspace representation of convex polytope:')
print('matrix `A`:')
print(poly.A)
print('vector `b`:')
print(poly.b)
# print(poly)  # another way of printing
    # a polytope's halfspace representation

问题中所给出的半空间表示表示相同的多面体。

这个问题使用了两个不同的软件包,每个软件包都给出了不同的半空间表示。正如下面的代码所检查的,这两个半空间表示表示相同的多个表位。代码还显示,它们等于上面使用package polytope获得的答案。

代码语言:javascript
运行
复制
"""Showing that the question's halfspace representations are the same polytope.

Any polytope has infinitely many minimal halfspace representations.
The representations below happen to be minimal, and equal up to scaling and
permutation of rows of `A` and elements of `b`.
"""
import numpy as np
import polytope

# halfspace representation computed using `polytope`
# from the vertex representation given in the question
vertices = np.array([[2, -2], [2, 2], [-10, 2], [-10, -2]])
poly = polytope.qhull(vertices)
# first halfspace representation given in the question
A = np.array([
    [-0.00000000e+00, -1.00000000e+00],
    [-1.00000000e+00, -0.00000000e+00],
    [4.93432455e-17, 1.00000000e+00],
    [1.00000000e+00, -0.00000000e+00]])
b = np.array([2., 10., 2., 2.])
question_poly_1 = polytope.Polytope(A, b)
# second halfspace representation given in the question
A = np.array([
    [0.0, -0.4472],
    [0.4472, -0.0000],
    [0.0, 0.4472],
    [-0.0995, -0.0000]])
b = np.array([0.8944, 0.8944, 0.8944, 0.9950])
question_poly_2 = polytope.Polytope(A, b)
# check that all the above halfspace representations
# represent the same polytope
assert poly == question_poly_1, (poly, question_poly_1)
assert poly == question_poly_2, (poly, question_poly_2)

上面的Python代码与polytope 0.2.3版一起工作。

可以使用包安装程序polytopePackage索引(PyPI)安装包pip

代码语言:javascript
运行
复制
pip install polytope
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63003243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档