예제 #1
0
def inflate_region(obstacle_pts, A_bounds, b_bounds, start, require_containment=False, iter_limit=np.inf):
    A_bounds = np.array(A_bounds)
    b_bounds = np.array(b_bounds)
    d = np.array(start)
    dim = A_bounds.shape[1]
    C = 0.01 * np.eye(dim)
    best_vol = 1e-100
    results = {'p_history': [], 'e_history': []}
    iters = 1

    while True:
        A, b, infeas_start = compute_obstacle_planes(obstacle_pts, C, d)
        # print "number of hyperplanes:", len(b)
        A = np.vstack((A, A_bounds))
        b = np.hstack((b, b_bounds))

        if require_containment:
            if np.all(A.dot(start) <= b) or iters == 1 or infeas_start:
                results['p_history'].append({'A': A, 'b': b})
            else:
                A = results['p_history'][-1]['A']
                b = results['p_history'][-1]['b']
                # print "Breaking early because start point is no longer contained in polytope"
                break
        else:
            results['p_history'].append({'A': A, 'b': b})

        iters += 1

        if iters > iter_limit:
            # print "iter limit reached"
            break

        try:
            C, d = lownerjohn_inner(A, b)
        except mosek.fusion.SolutionError:
            print "Breaking early beause ellipsoid maximization failed"
            break

        C = np.array(C)
        d = np.array(d)

        vol = np.linalg.det(C)
        results['e_history'].append({'C': C, 'd': d})

        if abs(vol - best_vol) / best_vol < 2e-2:
            break
        best_vol = vol
    return A, b, C, d, results
예제 #2
0
from scipy.io import loadmat, savemat
from irispy.mosek_ellipsoid.lownerjohn_ellipsoid import lownerjohn_inner

"""
MATLAB wrapper to the python lownerjohn_inner function (the MATLAB interface to Mosek Fusion fails after any call to 'clear java', so we can use this interface instead).

usage:
    python -m irispy.mosek.matlab_wrapper fname

where fname is a .mat file containing matrix A and vector b. Results will be stored in the same file.

Example usage from MATLAB:

    function [C, d] = py_lownerjohn_inner(A, b)
      fname = tempname();
      save(fname, 'A', 'b');
      system(['python -m irispy.mosek.matlab_wrapper ', fname]);
      load(fname, 'C', 'd');
      d = reshape(d, [], 1);
    end
"""

fname = sys.argv[1]

matvars = loadmat(fname, mat_dtype=True)
A = matvars['A']
b = matvars['b'].reshape((-1))

[C, d] = lownerjohn_inner(A, b);

savemat(fname, {'C': C, 'd': d})