Exemplo n.º 1
0
def testLinear(args):
    """Test 1d problem with linear constraints and linear objective"""
    sys = OneDcase()
    N = 10
    t0 = 0.0
    tf = 2.0
    prob = TrajOptCollocProblem(sys, N, t0, tf)
    prob.xbd = [np.array([-1e20, -1e20, -1e20]), np.array([1e20, 1e20, 1e20])]
    prob.ubd = [np.array([-1e20]), np.array([1e20])]
    prob.x0bd = [np.array([0, 0, -1e20]), np.array([0, 0, 1e20])]
    prob.xfbd = [np.array([1, 0, -1e20]), np.array([1, 0, 1e20])]
    lqr = LqrObj(R=np.ones(1))
    prob.add_lqr_obj(lqr)
    A = np.zeros(5)
    A[1] = 1
    A[2] = 1  # so it basically does nothing
    linPntObj = LinearPointObj(0, A, 3, 1, 0)
    prob.add_obj(linPntObj)
    # add linear constraint that x is increasing
    A = np.zeros(5)
    A[1] = 1
    lb = np.zeros(1)
    ub = np.ones(1)
    linPntCon = LinearPointConstr(-1, A, lb, ub)
    prob.add_constr(linPntCon, True)
    # we want mid point to be close to 0.8
    wantState = np.array([0.8, 0])
    pntObj = PointObj(N, wantState)
    prob.addObj(pntObj)
    prob.pre_process()  # construct the problem
    # construct a solver for the problem
    cfg = OptConfig(args.backend, print_level=5)
    slv = OptSolver(prob, cfg)
    rst = slv.solve_rand()
    print(rst.flag, rst.sol)
    if rst.flag == 1:
        # parse the solution
        sol = prob.parse_sol(rst.sol.copy())
        show_sol(sol)
Exemplo n.º 2
0
def main():
    sys = OrderTwoModel()
    N = 10
    t0 = 0.0
    tf = 3.0
    prob1 = TrajOptCollocProblem(sys, N, t0, [0.1, tf])  # maybe I need to give tips on choosing times
    prob2 = TrajOptCollocProblem(sys, N, [0.1, tf], [0.11, tf])
    prob3 = TrajOptCollocProblem(sys, N, [0.2, tf], [0.21, tf])
    xlb = -1e20 * np.ones(6)
    xub = 1e20 * np.ones(6)
    ulb = -np.ones(2)
    uub = -ulb
    x0, xf = np.array([[0., 0.], [1.0, 0.5]])
    x0lb = np.concatenate((x0, -1e20 * np.ones(4)))
    x0ub = np.concatenate((x0, 1e20 * np.ones(4)))

    xflb = np.concatenate((xf, -1e20 * np.ones(4)))
    xfub = np.concatenate((xf, 1e20 * np.ones(4)))
    prob1.xbd = [xlb, xub]
    prob1.ubd = [ulb, uub]
    prob1.x0bd = [x0lb, x0ub]
    prob1.xfbd = [xlb, xub]
    prob2.xbd = [xlb, xub]
    prob2.ubd = [ulb, uub]
    prob2.x0bd = [xlb, xub]
    prob2.xfbd = [xlb, xub]
    prob3.xbd = [xlb, xub]
    prob3.ubd = [ulb, uub]
    prob3.x0bd = [xlb, xub]
    prob3.xfbd = [xflb, xfub]

    # set bounds constraints for prob1 and prob2 at final
    a = np.zeros((2, 9))  # 1 + 6 + 2
    np.fill_diagonal(a[:, 1:3], 1.0)
    prob1.add_constr(LinearPointConstr(-1, a, np.array([0.2, 0.2]), np.array([0.2, 1e20])))
    prob2.add_constr(LinearPointConstr(-1, a, np.array([0.8, -1e20]), np.array([0.8, 0.3])))

    # define objective function, #TODO: change to time optimal
    obj = LqrObj(R=0.01 * np.ones(2))
    prob1.add_obj(obj, path=True)
    prob2.add_obj(obj, path=True)
    prob3.add_obj(obj, path=True)
    # optimize time
    obj_a = np.zeros(prob3.tfind + 1)
    obj_a[-1] = 1.0
    prob3.add_obj(LinearObj(obj_a))
    # add constraints to some phases
    prob = TrajOptMultiPhaseCollocProblem([prob1, prob2, prob3], addx=None)
    constr1 = ConnectConstr(0, 6)
    constr2 = ConnectConstr(1, 6)
    prob.add_connect_constr(constr1)
    prob.add_connect_constr(constr2)

    # ready to solve
    prob.pre_process()
    # cfg = OptConfig(backend='snopt', deriv_check=1, print_file='tmp.out')
    cfg = OptConfig(print_level=5)
    slv = OptSolver(prob, cfg)
    rst = slv.solve_rand()
    print(rst.flag)
    if rst.flag == 1:
        sol = prob.parse_sol(rst)
        show_sol(sol)