Skip to content

Conversation

ocots
Copy link
Member

@ocots ocots commented Jun 7, 2025

No description provided.

Copy link
Contributor

github-actions bot commented Jun 7, 2025

Breakage test results
Date: 2025-06-28 22:47:52

Name Latest Stable
OptimalControl.jl compat: v0.4.5 compat: v0.2.5

@ocots
Copy link
Member Author

ocots commented Jun 7, 2025

@jbcaillau Just for fun. Reordering the problem before parsing.

@ocots ocots marked this pull request as draft June 7, 2025 21:12
@jbcaillau
Copy link
Member

@ocots what is it supposed to do 😅?

note that PRAGMA is inactivated for :fun

@jbcaillau
Copy link
Member

ps. you mean just for fun or just for :fun?

@ocots
Copy link
Member Author

ocots commented Jun 8, 2025

For fun. 😁

@jbcaillau jbcaillau marked this pull request as ready for review June 8, 2025 08:37
@ocots
Copy link
Member Author

ocots commented Jun 8, 2025

julia> e = quote
    tf  R, variable
    t  [t0, tf], time
    x = (r, v, m)  R³, state
    u  R, control

    x(t0) == [r0, v0, m0]
    m(tf) == mf,         (1)
    0  u(t)  1
    r(t)  r0
    0  v(t)  vmax
    PRAGMA(1+1)

    (t) == F0(x(t)) + u(t) * F1(x(t))

    r(tf)  max
end;

julia> code = CTParser.order(e)

quote
    (tf  R, variable)
    (t  [t0, tf], time)
    (u  R, control)
    x = ((r, v, m)  R³, state)
    x(t0) == [r0, v0, m0]
    (m(tf) == mf, 1)
    0  u(t)  1
    r(t)  r0
    0  v(t)  vmax
    PRAGMA(1 + 1)
    (t) == F0(x(t)) + u(t) * F1(x(t))
    r(tf)  max
end

and

# Wrong order
julia> e = quote
    u  R, control
    x(t0) == [r0, v0, m0]
    r(tf)  max
    x = (r, v, m)  R³, state
    t  [t0, tf], time
    m(tf) == mf,         (1)
    0  u(t)  1
    r(t)  r0
    0  v(t)  vmax
    (t) == F0(x(t)) + u(t) * F1(x(t))
    PRAGMA(1+1)
    tf  R, variable
end;

julia> code = CTParser.order(e)

quote
    (tf  R, variable)
    (t  [t0, tf], time)
    (u  R, control)
    x = ((r, v, m)  R³, state)
    x(t0) == [r0, v0, m0]
    (m(tf) == mf, 1)
    0  u(t)  1
    r(t)  r0
    0  v(t)  vmax
    (t) == F0(x(t)) + u(t) * F1(x(t))
    PRAGMA(1 + 1)
    r(tf)  max
end

Copy link
Member

@jbcaillau jbcaillau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

@ocots
Copy link
Member Author

ocots commented Jun 8, 2025

But I haven't introduced it in the @def macro. There are no tests neither. I am not sure it is working.

One pass more :-)

It is not ready for merge.

@jbcaillau
Copy link
Member

@ocots what about this PR?

@ocots
Copy link
Member Author

ocots commented Jun 12, 2025

@ocots what about this PR?

In stand by

@ocots
Copy link
Member Author

ocots commented Jun 23, 2025

@jbcaillau What do you prefer?

julia> o = CTParser.@def begin
    u ∈ R, control
    x(t0) == [r0, v0, m0]
    r(tf) → max
    x = (r, v, m) ∈ R³, state
    t ∈ [t0, tf], time
    m(tf) == mf,         (1)
    0 ≤ u(t) ≤ 1
    r(t) ≥ r0
    0 ≤ v(t) ≤ vmax
    ẋ(t) == F0(x(t)) + u(t) * F1(x(t))
    PRAGMA(1+1)
    tf ∈ R, variable    
end

Abstract definition:

    tf ∈ R, variable
    t ∈ [t0, tf], time
    u ∈ R, control
    x = ((r, v, m) ∈ R³, state)
    x(t0) == [r0, v0, m0]
    m(tf) == mf, 1
    0 ≤ u(t) ≤ 1
    r(t) ≥ r0
    0 ≤ v(t) ≤ vmax
    ẋ(t) == F0(x(t)) + u(t) * F1(x(t))
    PRAGMA(1 + 1)
    r(tf) → max

The (autonomous) optimal control problem is of the form:

    minimize  J(x, u, tf) = g(x(0), x(tf), tf)

    subject to

        ẋ(t) = f(x(t), u(t), tf), t in [0, tf] a.e.,

        ϕ₋ ≤ ϕ(x(0), x(tf), tf) ≤ ϕ₊, 
        x₋ ≤ x(t) ≤ x₊, 
        u₋ ≤ u(t) ≤ u₊, 

    where x(t) = (r(t), v(t), m(t)) ∈ R³, u(t) ∈ R and tf ∈ R.

Declarations (* required):

╭──────────┬────────┬────────┬──────────┬─────────────┬───────────┬────────────╮
│ variable │ times* │ state* │ control* │ constraints │ dynamics* │ objective* │
├──────────┼────────┼────────┼──────────┼─────────────┼───────────┼────────────┤
│    V     │   V    │   V    │    V     │      V      │     V     │     V      │
╰──────────┴────────┴────────┴──────────┴─────────────┴───────────┴────────────╯

or

julia> o = CTParser.@def begin
    u ∈ R, control
    x(t0) == [r0, v0, m0]
    r(tf) → max
    x = (r, v, m) ∈ R³, state
    t ∈ [t0, tf], time
    m(tf) == mf,         (1)
    0 ≤ u(t) ≤ 1
    r(t) ≥ r0
    0 ≤ v(t) ≤ vmax
    ẋ(t) == F0(x(t)) + u(t) * F1(x(t))
    PRAGMA(1+1)
    tf ∈ R, variable    
end

Abstract definition:

    u ∈ R, control
    x(t0) == [r0, v0, m0]
    r(tf) → max
    x = ((r, v, m) ∈ R³, state)
    t ∈ [t0, tf], time
    m(tf) == mf, 1
    0 ≤ u(t) ≤ 1
    r(t) ≥ r0
    0 ≤ v(t) ≤ vmax
    ẋ(t) == F0(x(t)) + u(t) * F1(x(t))
    PRAGMA(1 + 1)
    tf ∈ R, variable

The (autonomous) optimal control problem is of the form:

    minimize  J(x, u, tf) = g(x(0), x(tf), tf)

    subject to

        ẋ(t) = f(x(t), u(t), tf), t in [0, tf] a.e.,

        ϕ₋ ≤ ϕ(x(0), x(tf), tf) ≤ ϕ₊, 
        x₋ ≤ x(t) ≤ x₊, 
        u₋ ≤ u(t) ≤ u₊, 

    where x(t) = (r(t), v(t), m(t)) ∈ R³, u(t) ∈ R and tf ∈ R.

Declarations (* required):

╭──────────┬────────┬────────┬──────────┬─────────────┬───────────┬────────────╮
│ variable │ times* │ state* │ control* │ constraints │ dynamics* │ objective* │
├──────────┼────────┼────────┼──────────┼─────────────┼───────────┼────────────┤
│    V     │   V    │   V    │    V     │      V      │     V     │     V      │
╰──────────┴────────┴────────┴──────────┴─────────────┴───────────┴────────────╯

@jbcaillau
Copy link
Member

@ocots i don't get the point 😅 is the point to reorder the definition so that it makes sense?

@ocots ocots marked this pull request as draft June 23, 2025 20:39
@ocots
Copy link
Member Author

ocots commented Jun 23, 2025

@ocots i don't get the point 😅 is the point to reorder the definition so that it makes sense?

:-)

Actually, in the first case I store in the definition, the reordered code while in the second case I store the original one.

The idea is actually to reorder the code to help parsing but not sure it is a good idea...

@ocots
Copy link
Member Author

ocots commented Jun 23, 2025

Not enough robust for the moment. This does not work:

        oo = @def begin
            λ  R^2, variable
            tf = λ₂
            t  [0, tf], time
            x  R, state
            u  R, control
            (t) == u(t)
            tf  min 
        end

because I replace the aliases after variable, time, state and control.

julia> e = quote
           λ ∈ R^2, variable
           tf = λ₂
           t ∈ [0, tf], time
           x ∈ R, state
           u ∈ R, control
           ẋ(t) == u(t)
           tf → min 
       end
quote
    #= REPL[4]:2 =#
    (λ ∈ R ^ 2, variable)
    #= REPL[4]:3 =#
    tf = λ₂
    #= REPL[4]:4 =#
    (t ∈ [0, tf], time)
    #= REPL[4]:5 =#
    (x ∈ R, state)
    #= REPL[4]:6 =#
    (u ∈ R, control)
    #= REPL[4]:7 =#
    ẋ(t) == u(t)
    #= REPL[4]:8 =#
    tf → min
end

julia> code = CTParser.reorder(e)
quote
    (λ ∈ R ^ 2, variable)
    (t ∈ [0, tf], time)
    (x ∈ R, state)
    (u ∈ R, control)
    tf = λ₂
    ẋ(t) == u(t)
    tf → min
end

julia> o = eval(CTParser.def_fun(code))
Line 2: (t ∈ [0, tf], time)
ERROR: UndefVarError: `tf` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
 [1] top-level scope
   @ ~/Research/logiciels/dev/control-toolbox/CTParser.jl/src/onepass.jl:109

caused by: UndefVarError: `tf` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
 [1] top-level scope
   @ ~/Research/logiciels/dev/control-toolbox/CTParser.jl/src/onepass.jl:106

@jbcaillau
Copy link
Member

@ocots indeed. order matters (for aliases, yes). looks error prone to me 😬

@jbcaillau
Copy link
Member

so you insist 🙃?

@ocots
Copy link
Member Author

ocots commented Jun 24, 2025

so you insist 🙃?

Yes to have something which works! Even if we throw it to trash.

I have made something less strict that works always when you reorder something that already works. But to be sure, if the parsing is ok without reordering, I do not reorder. I do it only if needed. So it is a little help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants