Exemplo n.º 1
0
def vdp_dae(method):
  ocp = Ocp(T=10)

  # Define 2 states
  x1 = ocp.state()
  x2 = ocp.state()

  z = ocp.algebraic()

  # Define 1 control
  u = ocp.control(order=0)

  # Specify ODE
  ocp.set_der(x1, z * x1 - x2 + u)
  ocp.set_der(x2, x1)
  ocp.add_alg(z-(1 - x2**2))

  # Lagrange objective
  ocp.add_objective(ocp.integral(x1**2 + x2**2 + u**2))

  # Path constraints
  ocp.subject_to(-1 <= (u <= 1))
  ocp.subject_to(x1 >= -0.25)

  # Initial constraints
  ocp.subject_to(ocp.at_t0(x1) == 0)
  ocp.subject_to(ocp.at_t0(x2) == 1)

  # Pick an NLP solver backend
  ocp.solver('ipopt')

  # Pick a solution method
  ocp.method(method)
  return (ocp, x1, x2, u)
Exemplo n.º 2
0
    def test_dae_casadi(self):
        # cross check with dae_colloation

        xref = 0.1  # chariot reference

        l = 1.  #- -> crane, + -> pendulum
        m = 1.
        M = 1.
        g = 9.81

        ocp = Ocp(T=5)

        x = ocp.state()
        y = ocp.state()
        w = ocp.state()
        dx = ocp.state()
        dy = ocp.state()
        dw = ocp.state()

        xa = ocp.algebraic()
        u = ocp.control()

        ocp.set_der(x, dx)
        ocp.set_der(y, dy)
        ocp.set_der(w, dw)
        ddx = (w - x) * xa / m
        ddy = g - y * xa / m
        ddw = ((x - w) * xa - u) / M
        ocp.set_der(dx, ddx)
        ocp.set_der(dy, ddy)
        ocp.set_der(dw, ddw)
        ocp.add_alg((x - w) * (ddx - ddw) + y * ddy + dy * dy + (dx - dw)**2)

        ocp.add_objective(
            ocp.at_tf((x - xref) * (x - xref) + (w - xref) * (w - xref) +
                      dx * dx + dy * dy))
        ocp.add_objective(
            ocp.integral((x - xref) * (x - xref) + (w - xref) * (w - xref)))

        ocp.subject_to(-2 <= (u <= 2))

        ocp.subject_to(ocp.at_t0(x) == 0)
        ocp.subject_to(ocp.at_t0(y) == l)
        ocp.subject_to(ocp.at_t0(w) == 0)
        ocp.subject_to(ocp.at_t0(dx) == 0)
        ocp.subject_to(ocp.at_t0(dy) == 0)
        ocp.subject_to(ocp.at_t0(dw) == 0)
        #ocp.subject_to(xa>=0,grid='integrator_roots')

        ocp.set_initial(y, l)
        ocp.set_initial(xa, 9.81)

        # Pick an NLP solver backend
        # NOTE: default scaling strategy of MUMPS leads to a singular matrix error
        ocp.solver(
            'ipopt', {
                "ipopt.linear_solver": "mumps",
                "ipopt.mumps_scaling": 0,
                "ipopt.tol": 1e-12
            })

        # Pick a solution method
        method = DirectCollocation(N=50)
        ocp.method(method)

        # Solve
        sol = ocp.solve()

        assert_array_almost_equal(
            sol.sample(xa, grid='integrator', refine=1)[1][0],
            9.81011622448889)
        assert_array_almost_equal(
            sol.sample(xa, grid='integrator', refine=1)[1][1],
            9.865726317147214)
        assert_array_almost_equal(
            sol.sample(xa, grid='integrator')[1][0], 9.81011622448889)
        assert_array_almost_equal(
            sol.sample(xa, grid='integrator')[1][1], 9.865726317147214)
        assert_array_almost_equal(
            sol.sample(xa, grid='control')[1][0], 9.81011622448889)
        assert_array_almost_equal(
            sol.sample(xa, grid='control')[1][1], 9.865726317147214)