Ejemplo n.º 1
0
def test_dynamics_integration():
    dyn = LinearSystemDynamics(TESTA, TESTB)
    ctrl = ConstantController(dyn, np.array([0, 0]))
    xs, us = dyn.simulate(DEFAULT_ENV.start, ctrl,
                          np.linspace(0, 10, 100))
    #enable to see plot of dynamics
    fig = plt.figure(1)
    ax = fig.add_subplot()
    DEFAULT_ENV.plot_path(xs, ax=ax)
    ax.set_xlim(0, 100)
    ax.set_ylim(0, 100)
    plt.show()
Ejemplo n.º 2
0
def test_mip_initalization_default():
    planner = MIPPlanner(DEFAULT_ENV, LinearSystemDynamics(TESTA, TESTB),
                         T=200,
                         h_k=0.001)
    n_regions = len(DEFAULT_ENV.get_cvx_ineqs()[0])
    n_timesteps = planner.T
    # active_set = np.zeros((n_timesteps, n_regions), dtype=bool)
    # active_set[:100, 0] = True
    # active_set[100:, 2] = True
    # active_set[120:, 3] = True
    # active_set_vals = active_set.copy()
    active_set = None
    active_set_vals = None
    x, u, obj, active_set, inactive_set_val, rt = planner.optimize_path(
        active_set, active_set_vals)

    x_inited, u_inited, obj_inited, as_inited,\
    is_val_inited, rt_inited = \
        planner.optimize_path(np.zeros_like(active_set, dtype=bool),
                              inactive_set_val,
                              initial_soln=(x, u))
    # ax = START_ENV.plot_path(x)
    # START_ENV.plot_path(x_inited, ax)
    # plt.show()
    np.testing.assert_allclose(x, x_inited)
    np.testing.assert_allclose(u, u_inited)
    np.testing.assert_allclose(inactive_set_val, is_val_inited)
    np.testing.assert_allclose([obj], [obj_inited])
    assert rt_inited < rt
Ejemplo n.º 3
0
def test_new_gen_env():
    from env_gen import load_folder_and_plot
    from obstacle_environment import ObstacleEnvironment2D
    from mip_planner import MIPPlanner, TESTA, TESTB
    from core.dynamics.linear_system_dynamics import LinearSystemDynamics
    import matplotlib.pyplot as plt

    START = (15, 10, 0, 0)
    END = (90, 90, 0, 0)
    OBSTACLES = None
    x_b = (0, 100)
    y_b = (0, 100)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    a, b = load_folder_and_plot(ax, (START[0], START[1]), (END[0], END[1]),
                                "../envs/2", x_b, y_b)  # read and plot
    START_ENV = ObstacleEnvironment2D(START, END, OBSTACLES, a, b)
    planner = MIPPlanner(START_ENV, LinearSystemDynamics(TESTA, TESTB),
                         T=50,
                         h_k=0.01, presolve=0)
    planner.set_optim_timelimit(180)
    wp, _, _, _, _, _ = planner.optimize_path()
    START_ENV.plot_path(wp, ax)
    ax.set_xlim(-50, 200)
    ax.set_ylim(-50, 200)
    fig.show()
Ejemplo n.º 4
0
def test_mip_planner():
    planner = MIPPlanner(DEFAULT_ENV, LinearSystemDynamics(TESTA, TESTB),
                         T=200,
                         h_k=0.001)
    x, u, _, _, _, _ = planner.optimize_path()

    np.testing.assert_allclose(x[0], DEFAULT_ENV.start)
    np.testing.assert_allclose(x[-1], DEFAULT_ENV.end)
    assert x[70][1] < 5 # bottom right corner
Ejemplo n.º 5
0
def test_mip_discrete_regions():
    planner = MIPPlanner(DEFAULT_ENV, LinearSystemDynamics(TESTA, TESTB),
                         T=200,
                         h_k=0.001)
    n_regions = len(DEFAULT_ENV.get_cvx_ineqs()[0])
    n_timesteps = planner.T
    active_set = np.zeros((n_timesteps, n_regions), dtype=bool)
    active_set[:100, 0] = True
    active_set[100:, 2] = True
    active_set[120:, 3] = True
    active_set_vals = active_set

    x, u, _, _, _, _ = planner.optimize_path(active_set, active_set_vals)
    np.testing.assert_allclose(x[0], DEFAULT_ENV.start)
    np.testing.assert_allclose(x[-1], DEFAULT_ENV.end)
    assert x[90][0] < 10 and x[90][1] > 80 # top left corner
Ejemplo n.º 6
0
t = 200
time_steps = 10
x_b = (0, 100)
y_b = (0, 100)
fig = plt.figure()
ax = fig.add_subplot(111)
a, b = load_folder_and_plot(ax, (START[0], START[1]), (END[0], END[1]),
                            "envs/10", x_b, y_b)  # read and plot
paths = generate_path_list(a, b, START, END)
print(paths)
# pick a path
p = paths[np.random.randint(len(paths))]
print(p)
START_ENV = ObstacleEnvironment2D(START, END, OBSTACLES, a, b)
planner = MIPPlanner(START_ENV,
                     LinearSystemDynamics(TESTA, TESTB),
                     T=t,
                     h_k=0.001,
                     presolve=2)
planner.set_optim_timelimit(10)
last_inactive_set = np.zeros((t, len(a)), dtype=bool)
for row in range(0, int(t / time_steps)):
    last_inactive_set[row][p[0]] = True
    last_inactive_set[180 + row][p[-1]] = True
curr_reg = 0
wp = None
ob = None
runtime = 0
for step in range(0, time_steps):
    print("STEP " + str(step))
    active_set = np.zeros((t, len(a)), dtype=bool)
Ejemplo n.º 7
0
import numpy as np
from obstacle_environment import ObstacleEnvironment2D
from core.dynamics.linear_system_dynamics import LinearSystemDynamics
import gurobipy as gp
from gurobipy import GRB

TESTA = np.array([
    [0, 0, 1, 0],
    [0, 0, 0, 1],
    [-1, 0, 0, 0],
    [0, 1, 0, 0],
])

TESTB = np.array([[0, 0], [0, 0], [1, 0], [0, 1]])

DEFAULT_DYN = LinearSystemDynamics(TESTA, TESTB)


class MIPPlanner:
    def __init__(self,
                 env: ObstacleEnvironment2D,
                 dyn,
                 T=200,
                 h_k=1e-3,
                 presolve=0):
        self.presolve = presolve  # 1 low 2 high ; higher = faster but more aggresive
        self.env = env
        self.dyn = dyn
        self.T = T
        self.h_k = h_k
        self.last_x = None