예제 #1
0
    def test_parameters(self):
        p = Parameter(name='p')
        self.assertEqual(p.name(), "p")
        self.assertEqual(p.size, (1,1))

        p = Parameter(4, 3, sign="positive")
        with self.assertRaises(Exception) as cm:
            p.value = 1
        self.assertEqual(str(cm.exception), "Invalid dimensions (1,1) for Parameter value.")

        val = -np.ones((4,3))
        val[0,0] = 2

        p = Parameter(4, 3, sign="positive")
        with self.assertRaises(Exception) as cm:
            p.value = val
        self.assertEqual(str(cm.exception), "Invalid sign for Parameter value.")

        p = Parameter(4, 3, sign="negative")
        with self.assertRaises(Exception) as cm:         
            p.value = val
        self.assertEqual(str(cm.exception), "Invalid sign for Parameter value.")

        # No error for unknown sign.
        p = Parameter(4, 3)
        p.value = val
예제 #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()
예제 #3
0
파일: tests.py 프로젝트: 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