首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >[python]pycddlib使用案例

[python]pycddlib使用案例

作者头像
云未归来
发布2025-07-22 14:28:20
发布2025-07-22 14:28:20
4400
代码可运行
举报
运行总次数:0
代码可运行

测试版本:

pycddlib==3.0.2

Examples

The following examples presume:

代码语言:javascript
代码运行次数:0
运行
复制
>>> import cdd
>>> from pprint import pprint

Checking and Removing Redundancies

代码语言:javascript
代码运行次数:0
运行
复制
>>> array = [[2, 1, 2, 3], [0, 1, 2, 3], [3, 0, 1, 2], [0, -2, -4, -6]]
>>> mat = cdd.matrix_from_array(array, rep_type=cdd.RepType.INEQUALITY)
>>> cdd.redundant(mat, 0) is None
True
>>> cdd.redundant(mat, 1)
[5.0, -3.0, 0.0]
>>> cdd.redundant(mat, 2)
[8.0, -4.0, 0.0]
>>> cdd.redundant(mat, 3)
[6.4999..., -3.0, 0.0]
>>> cdd.redundant_rows(mat)
{0}
>>> cdd.matrix_canonicalize(mat)
({1, 3}, {0}, [None, 0, 1, None])
>>> pprint(mat.array)
[[0.0, 1.0, 2.0, 3.0], [3.0, 0.0, 1.0, 2.0]]
>>> mat.lin_set
{0}

Solving Linear Programs

代码语言:javascript
代码运行次数:0
运行
复制
>>> array = [
...     [4 / 3, -2, -1],  # 0 <= 4/3-2x-y
...     [2 / 3, 0, -1],  # 0 <= 2/3-y
...     [0, 1, 0],  # 0 <= x
...     [0, 0, 1],  # 0 <= y
...     [0, 3, 4],  # obj func: 3x+4y
... ]
>>> lp = cdd.linprog_from_array(array, obj_type=cdd.LPObjType.MAX)
>>> cdd.linprog_solve(lp)
>>> lp.status == cdd.LPStatusType.OPTIMAL
True
>>> lp.obj_value
3.666666...
>>> lp.primal_solution
[0.333333..., 0.666666...]
>>> lp.dual_solution
[(0, 1.5), (1, 2.5)]

Calculating Extreme Points / Rays

This is the sampleh1.ine example that comes with cddlib.

代码语言:javascript
代码运行次数:0
运行
复制
>>> array = [[2, -1, -1, 0], [0, 1, 0, 0], [0, 0, 1, 0]]
>>> mat = cdd.matrix_from_array(array, rep_type=cdd.RepType.INEQUALITY)
>>> poly = cdd.polyhedron_from_matrix(mat)
>>> ext = cdd.copy_generators(poly)
>>> ext.rep_type
<RepType.GENERATOR: 2>
>>> pprint(ext.array) 
[[1.0, 0.0, 0.0, 0.0],
 [1.0, 2.0, 0.0, 0.0],
 [1.0, 0.0, 2.0, 0.0],
 [0.0, 0.0, 0.0, 1.0]]
>>> ext.lin_set # note: first row is 0, so fourth row is 3
{3}

Matrix Rank

代码语言:javascript
代码运行次数:0
运行
复制
>>> # row 3 = row 1 - row 2
>>> # col 2 = 2 * col 1
>>> array = [[1, 2, 0, 1], [1, 2, 1, 3], [0, 0, -1, -2]]
>>> mat = cdd.matrix_from_array(array)
>>> row_basis, col_basis, rank = cdd.matrix_rank(mat)
>>> row_basis
{0, 1}
>>> col_basis
{0, 2}
>>> rank
2

Matrix Adjacencies

代码语言:javascript
代码运行次数:0
运行
复制
>>> # H-representation of a square
>>> # 0 <= 1 + x1 (face 0)
>>> # 0 <= 1 + x2 (face 1)
>>> # 0 <= 1 - x1 (face 2)
>>> # 0 <= 1 - x2 (face 3)
>>> #
>>> #   +---(3)---+
>>> #   |         |
>>> #   |         |
>>> #  (0)       (2)
>>> #   |         |
>>> #   |         |
>>> #   +---(1)---+
>>> array = [[1, 1, 0], [1, 0, 1], [1, -1, 0], [1, 0, -1]]
>>> mat = cdd.matrix_from_array(array, rep_type=cdd.RepType.INEQUALITY)
>>> cdd.matrix_adjacency(mat)
[{1, 3}, {0, 2}, {1, 3}, {0, 2}]
代码语言:javascript
代码运行次数:0
运行
复制
>>> # V-representation of a square
>>> #
>>> #   1-----3
>>> #   |     |
>>> #   |     |
>>> #   0-----2
>>> array = [[1, -1, -1], [1, -1, 1], [1, 1, -1], [1, 1, 1]]
>>> mat = cdd.matrix_from_array(array, rep_type=cdd.RepType.GENERATOR)
>>> cdd.matrix_adjacency(mat)
[{1, 2}, {0, 3}, {0, 3}, {1, 2}]

Polyhedron Adjacencies and Incidences

代码语言:javascript
代码运行次数:0
运行
复制
>>> # H-representation of a square
>>> # 0 <= 1 + x1 (face 0)
>>> # 0 <= 1 + x2 (face 1)
>>> # 0 <= 1 - x1 (face 2)
>>> # 0 <= 1 - x2 (face 3)
>>> array = [[1, 1, 0], [1, 0, 1], [1, -1, 0], [1, 0, -1]]
>>> mat = cdd.matrix_from_array(array, rep_type=cdd.RepType.INEQUALITY)
>>> poly = cdd.polyhedron_from_matrix(mat)
>>> gen = cdd.copy_generators(poly)
>>> gen.rep_type
<RepType.GENERATOR: 2>
>>> pprint(gen.array, width=40)
[[1.0, 1.0, -1.0],
 [1.0, 1.0, 1.0],
 [1.0, -1.0, 1.0],
 [1.0, -1.0, -1.0]]
>>> gen.lin_set
set()
>>> # V-representation of this square
>>> #
>>> #   2---(3)---1
>>> #   |         |
>>> #   |         |
>>> #  (0)       (2)
>>> #   |         |
>>> #   |         |
>>> #   3---(1)---0
>>> #
>>> # vertex 0 is adjacent to vertices 1 and 3
>>> # vertex 1 is adjacent to vertices 0 and 2
>>> # vertex 2 is adjacent to vertices 1 and 3
>>> # vertex 3 is adjacent to vertices 0 and 2
>>> cdd.copy_adjacency(poly)
[{1, 3}, {0, 2}, {1, 3}, {0, 2}]
>>> # vertex 0 is the intersection of faces (1) and (2)
>>> # vertex 1 is the intersection of faces (2) and (3)
>>> # vertex 2 is the intersection of faces (0) and (3)
>>> # vertex 3 is the intersection of faces (0) and (1)
>>> cdd.copy_incidence(poly)
[{1, 2}, {2, 3}, {0, 3}, {0, 1}]
>>> # face (0) is adjacent to faces (1) and (3)
>>> # face (1) is adjacent to faces (0) and (2)
>>> # face (2) is adjacent to faces (1) and (3)
>>> # face (3) is adjacent to faces (0) and (2)
>>> cdd.copy_input_adjacency(poly)
[{1, 3}, {0, 2}, {1, 3}, {0, 2}, set()]
>>> # face (0) intersects with vertices 2 and 3
>>> # face (1) intersects with vertices 0 and 3
>>> # face (2) intersects with vertices 0 and 1
>>> # face (3) intersects with vertices 1 and 2
>>> cdd.copy_input_incidence(poly)
[{2, 3}, {0, 3}, {0, 1}, {1, 2}, set()]
>>> # add a vertex, and construct new polyhedron
>>> cdd.matrix_append_to(gen, cdd.matrix_from_array([[1, 0, 2]]))
>>> vpoly = cdd.polyhedron_from_matrix(gen)
>>> vmat = cdd.copy_inequalities(vpoly)
>>> vmat.rep_type
<RepType.INEQUALITY: 1>
>>> pprint(vmat.array)
[[1.0, 0.0, 1.0],
 [2.0, 1.0, -1.0],
 [1.0, 1.0, 0.0],
 [2.0, -1.0, -1.0],
 [1.0, -1.0, 0.0]]
>>> vmat.lin_set
set()
>>> # so now we have:
>>> # 0 <= 1 + x2
>>> # 0 <= 2 + x1 - x2
>>> # 0 <= 1 + x1
>>> # 0 <= 2 - x1 - x2
>>> # 0 <= 1 - x1
>>> #
>>> # graphical depiction of vertices and faces:
>>> #
>>> #        4
>>> #       / \
>>> #      /   \
>>> #    (1)   (3)
>>> #    /       \
>>> #   2         1
>>> #   |         |
>>> #   |         |
>>> #  (2)       (4)
>>> #   |         |
>>> #   |         |
>>> #   3---(0)---0
>>> #
>>> # for each face, list adjacent faces
>>> cdd.copy_adjacency(vpoly)
[{2, 4}, {2, 3}, {0, 1}, {1, 4}, {0, 3}]
>>> # for each face, list adjacent vertices
>>> cdd.copy_incidence(vpoly)
[{0, 3}, {2, 4}, {2, 3}, {1, 4}, {0, 1}]
>>> # for each vertex, list adjacent vertices
>>> cdd.copy_input_adjacency(vpoly)
[{1, 3}, {0, 4}, {3, 4}, {0, 2}, {1, 2}]
>>> # for each vertex, list adjacent faces
>>> cdd.copy_input_incidence(vpoly)
[{0, 4}, {3, 4}, {1, 2}, {0, 2}, {1, 3}]

Fourier and Block Elimination

The next example is taken from Wikipedia.

代码语言:javascript
代码运行次数:0
运行
复制
>>> array = [
...     [10, -2, 5, -4],  # 2x-5y+4z<=10
...     [9, -3, 6, -3],  # 3x-6y+3z<=9
...     [-7, 1, -5, 2],  # -x+5y-2z<=-7
...     [12, 3, -2, -6],  # -3x+2y+6z<=12
... ]
>>> mat = cdd.matrix_from_array(array, rep_type=cdd.RepType.INEQUALITY)
>>> cdd.fourier_elimination(mat).array
[[-1.0, 0.0, -1.25], [-1.0, -1.0, -1.0], [-1.5, 1.0, -2.833333...]]
>>> cdd.block_elimination(mat, {3}).array
[[-4.0, 0.0, -5.0], [-1.5, -1.5, -1.5], [-9.0, 6.0, -17.0]]

案例来自:Examples — pycddlib 3.0.2 documentation

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-06-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 测试版本:
  • Examples
    • Checking and Removing Redundancies
    • Solving Linear Programs
    • Calculating Extreme Points / Rays
    • Matrix Rank
    • Matrix Adjacencies
    • Polyhedron Adjacencies and Incidences
    • Fourier and Block Elimination
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档