Ejemplo n.º 1
0
# Finds the separating hyperplane between two polyhedra.
# Data from Section 8.2.2: Separating polyhedra in 2D in http://cvxr.com/cvx/examples/

import convex_sets as cs
from cvxpy import numpy as np
import cvxpy
import matplotlib.pyplot as plt

n = 2
m = 2 * n
A1 = np.matrix("1 1; 1 -1; -1 1; -1 -1")
A2 = np.matrix("1 0; -1 0; 0 1; 0 -1")
b1 = 2 * np.ones((m, 1))
b2 = np.matrix("5; -3; 4; -2")

poly1 = cs.Polyhedron(A1, b1)
poly2 = cs.Polyhedron(A2, b2)

# Separating hyperplane.
normal, offset = cs.sep_hyp(poly1, poly2)

# Plotting
t = np.linspace(-3, 6, 100)
p = -normal[0] * t / normal[1] + offset / normal[1]
plt.fill([-2, 0, 2, 0], [0, 2, 0, -2], 'b', [3, 5, 5, 3], [2, 2, 4, 4], 'r')
plt.axis([-3, 6, -3, 6])
plt.axes().set_aspect('equal', 'box')
plt.plot(t, p)
plt.title('Separating 2 polyhedra by a hyperplane')
plt.show()
Ejemplo n.º 2
0
# Finds the separating hyperplane between two polyhedra.
# Data from Section 8.2.2: Separating polyhedra in 2D in http://cvxr.com/cvx/examples/

import convex_sets as cs
from cvxpy import numpy as np
import cvxpy
import matplotlib.pyplot as plt

n = 2
m = 2*n
A1 = np.matrix("1 1; 1 -1; -1 1; -1 -1")
A2 = np.matrix("1 0; -1 0; 0 1; 0 -1")
b1 = 2*np.ones((m,1))
b2 = np.matrix("5; -3; 4; -2")

poly1 = cs.Polyhedron(A1, b1)
poly2 = cs.Polyhedron(A2, b2)

# Separating hyperplane.
normal,offset = cs.sep_hyp(poly1, poly2)

# Plotting
t = np.linspace(-3,6,100);
p = -normal[0]*t/normal[1] + offset/normal[1]
plt.fill([-2, 0, 2, 0],[0,2,0,-2],'b', [3,5,5,3],[2,2,4,4],'r')
plt.axis([-3, 6, -3, 6])
plt.axes().set_aspect('equal', 'box')
plt.plot(t,p)
plt.title('Separating 2 polyhedra by a hyperplane')
plt.show()
Ejemplo n.º 3
0
Archivo: tests.py Proyecto: BvanP/cvxpy
# Problems involving polyhedra.

import convex_sets as cs
from cvxpy import numpy as np
import cvxpy

n = 2
m = 2*n
A1 = np.matrix("1 1; 1 -1; -1 1; -1 -1")
A2 = np.matrix("1 0; -1 0; 0 1; 0 -1")
b1 = 2*np.ones((m,1))
b2 = np.matrix("5; -3; 4; -2")

poly1 = cs.Polyhedron(A1, b1)
poly2 = cs.Polyhedron(A2, b2)

assert cs.contains(poly1, [1,1])
# TODO distance should be an expression, i.e. norm2(poly1 - poly2)
print cs.dist(poly1, poly2)
elem = cs.proj(poly1, poly2)
assert cs.contains(poly1, elem)
assert cs.dist(poly1, elem) < 1e-6

hull = cs.ConvexHull([b1, b2])
print cs.contains(hull, b1)
print cs.contains(hull, 0.3*b1 + 0.7*b2)

print cs.dist(poly1, 5*hull[0:2] + 2)
print cs.dist(poly1, np.matrix("1 5; -1 3")*poly2 + [1,5])
assert cs.dist(poly1, np.matrix("1 0; 0 1")*poly2 + [1,5]) - cs.dist(poly2, poly1 - [1,5]) == 0
Ejemplo n.º 4
0
Archivo: tests.py Proyecto: r0k3/cvxpy
# Problems involving polyhedra.

import convex_sets as cs
from cvxpy import numpy as np
import cvxpy

n = 2
m = 2 * n
A1 = np.matrix("1 1; 1 -1; -1 1; -1 -1")
A2 = np.matrix("1 0; -1 0; 0 1; 0 -1")
b1 = 2 * np.ones((m, 1))
b2 = np.matrix("5; -3; 4; -2")

poly1 = cs.Polyhedron(A1, b1)
poly2 = cs.Polyhedron(A2, b2)

assert cs.contains(poly1, [1, 1])
# TODO distance should be an expression, i.e. norm2(poly1 - poly2)
print cs.dist(poly1, poly2)
elem = cs.proj(poly1, poly2)
assert cs.contains(poly1, elem)
assert cs.dist(poly1, elem) < 1e-6

hull = cs.ConvexHull([b1, b2])
print cs.contains(hull, b1)
print cs.contains(hull, 0.3 * b1 + 0.7 * b2)

print cs.dist(poly1, 5 * hull[0:2] + 2)
print cs.dist(poly1, np.matrix("1 5; -1 3") * poly2 + [1, 5])
assert cs.dist(poly1,
               np.matrix("1 0; 0 1") * poly2 + [1, 5]) - cs.dist(