|
2 | 2 |
|
3 | 3 | ```@meta |
4 | 4 | CollapsedDocStrings = false |
| 5 | +Draft = false |
5 | 6 | ``` |
6 | 7 |
|
7 | 8 | In this tutorial, we explain the `Flow` function, in particular to compute flows from an optimal control problem. |
@@ -462,7 +463,7 @@ where $ p^0 = -1 $ since we are in the normal case, and where $c(x) = x - l_b$. |
462 | 463 | u(x) = 0. |
463 | 464 | ``` |
464 | 465 |
|
465 | | -From the maximizing condition, along a boundary arc, we have $p(t) = 0$. Differentiating, we obtain $\dot{p}(t) = 2 x(t) - \mu(t) = 0$. Hence, along a boundary arc, the dual variable $\mu$ is given in feedback form by: |
| 466 | +From the maximisation condition, along a boundary arc, we have $p(t) = 0$. Differentiating, we obtain $\dot{p}(t) = 2 x(t) - \mu(t) = 0$. Hence, along a boundary arc, the dual variable $\mu$ is given in feedback form by: |
466 | 467 |
|
467 | 468 | ```math |
468 | 469 | \mu(x) = 2x. |
@@ -503,3 +504,85 @@ p0 = -0.982237546583301 |
503 | 504 | xf, pf = f(t0, x0, p0, tf) |
504 | 505 | xf |
505 | 506 | ``` |
| 507 | + |
| 508 | +## Jump on the costate |
| 509 | + |
| 510 | +Let consider the following problem: |
| 511 | + |
| 512 | +```@example main |
| 513 | +t0=0 |
| 514 | +tf=1 |
| 515 | +x0=[0, 1] |
| 516 | +l = 1/9 |
| 517 | +@def ocp begin |
| 518 | + t ∈ [ t0, tf ], time |
| 519 | + x ∈ R², state |
| 520 | + u ∈ R, control |
| 521 | + x(t0) == x0 |
| 522 | + x(tf) == [0, -1] |
| 523 | + x₁(t) ≤ l, (x_con) |
| 524 | + ẋ(t) == [x₂(t), u(t)] |
| 525 | + 0.5∫(u(t)^2) → min |
| 526 | +end |
| 527 | +nothing # hide |
| 528 | +``` |
| 529 | + |
| 530 | +The pseudo-Hamiltonian of this problem is |
| 531 | + |
| 532 | +```math |
| 533 | + H(x, p, u, \mu) = p_1\, x_2 + p_2\, u + 0.5\, p^0 u^2 + \mu\, c(x), |
| 534 | +``` |
| 535 | + |
| 536 | +where $ p^0 = -1 $ since we are in the normal case, and where the constraint is $c(x) = l - x_1 \ge 0$. Along a boundary arc, when $c(x(t)) = 0$, we have $x_1(t) = l$, so $\dot{x}_1(t) = x_2(t) = 0$. Differentiating again, we obtain $\dot{x}_2(t) = u(t) = 0$ (the constraint is of order 2). Hence, along a boundary arc, the control in feedback form is: |
| 537 | + |
| 538 | + |
| 539 | +```math |
| 540 | +u(x, p) = 0. |
| 541 | +``` |
| 542 | + |
| 543 | +From the maximisation condition, along a boundary arc, we have $p_2(t) = 0$. Differentiating, we obtain $\dot{p}_2(t) = -p_1(t) = 0$. Differentiating again, we obtain $\dot{p}_1(t) = \mu(t) = 0$. Hence, along a boundary arc, the Lagrange multiplier $\mu$ is given in feedback form by: |
| 544 | + |
| 545 | +```math |
| 546 | +\mu(x, p) = 0. |
| 547 | +``` |
| 548 | + |
| 549 | +Outside a boundary arc, the maximisation condition gives $u(x, p) = p_2$. A deeper analysis of the problem shows that the optimal solution has 3 arcs, the first and the third ones are interior to the constraint. The second arc is a boundary arc, that is $x_1(t) = l$ along the second arc. We denote by $t_1$ and $t_2$ the two switching times. We have $t_1 = 3l = 1/3$ and $t_2 = 1 - 3l = 2/3$, since $l=1/9$. The initial costate solution is $p(0) = [-18, -6]$. |
| 550 | + |
| 551 | +!!! danger "Important" |
| 552 | + |
| 553 | + The costate is discontinuous at $t_1$ and $t_2$ with a jump of $18$. |
| 554 | + |
| 555 | +Let us compute the solution concatenating the flows with the jumps. |
| 556 | + |
| 557 | +```@example main |
| 558 | +t1 = 3l |
| 559 | +t2 = 1 - 3l |
| 560 | +p0 = [-18, -6] |
| 561 | +
|
| 562 | +fs = Flow(ocp, |
| 563 | + (x, p) -> p[2] # control along regular arc |
| 564 | + ) |
| 565 | +fc = Flow(ocp, |
| 566 | + (x, p) -> 0, # control along boundary arc |
| 567 | + (x, u) -> l-x[1], # state constraint |
| 568 | + (x, p) -> 0 # Lagrange multiplier |
| 569 | + ) |
| 570 | +
|
| 571 | +ν = 18 # jump value of p1 at t1 and t2 |
| 572 | +
|
| 573 | +f = fs * (t1, [ν, 0], fc) * (t2, [ν, 0], fs) |
| 574 | +
|
| 575 | +xf, pf = f(t0, x0, p0, tf) # xf should be [0, -1] |
| 576 | +``` |
| 577 | + |
| 578 | +Let us solve the problem with a direct method to compare with the solution from the flow. |
| 579 | + |
| 580 | +```@example main |
| 581 | +using NLPModelsIpopt |
| 582 | +
|
| 583 | +direct_sol = solve(ocp) |
| 584 | +plot(direct_sol; label="direct") |
| 585 | +
|
| 586 | +flow_sol = f((t0, tf), x0, p0) |
| 587 | +plot!(flow_sol; label="flow") |
| 588 | +``` |
0 commit comments