Skip to content

Commit

Permalink
solver interface run_ci
Browse files Browse the repository at this point in the history
  • Loading branch information
thowell committed Feb 1, 2022
1 parent e2abc77 commit 2ac8579
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 111 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DirectTrajectoryOptimization"
uuid = "a9f42406-efe7-414c-8b71-df971cc98041"
authors = ["thowell <[email protected]>"]
version = "0.1.2"
version = "0.1.3"

[deps]
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
Expand Down
2 changes: 1 addition & 1 deletion examples/acrobot/acrobot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ bnds = [bnd1, [bndt for t = 2:T-1]..., bndT]
cons = [Constraint() for t = 1:T]

# ## problem
p = ProblemData(obj, dyn, cons, bnds, options=Options())
p = solver(dyn, obj, cons, bnds, options=Options{Float64}())

@variables z[1:p.nlp.num_var]

Expand Down
2 changes: 1 addition & 1 deletion examples/car/car.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ conT = Constraint(obs, nx, 0, nw, idx_ineq=collect(1:1))
cons = [[cont for t = 1:T-1]..., conT]

# ## problem
p = ProblemData(obj, dyn, cons, bnds, options=Options())
p = solver(dyn, obj, cons, bnds)

# ## initialize
x_interpolation = linear_interpolation(x1, xT, T)
Expand Down
5 changes: 1 addition & 4 deletions src/DirectTrajectoryOptimization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ include("constraints.jl")
include("bounds.jl")
include("general_constraint.jl")
include("dynamics.jl")
include("solver.jl")
include("data.jl")
include("solver.jl")
include("moi.jl")
include("utils.jl")

Expand All @@ -27,9 +27,6 @@ export Bound, Bounds, Constraint, Constraints, GeneralConstraint
# dynamics
export Dynamics

# problem
export ProblemData

# solver
export Solver, Options, initialize_states!, initialize_controls!, solve!, get_trajectory

Expand Down
2 changes: 1 addition & 1 deletion src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ end

Constraints{T} = Vector{Constraint{T}} where T

function Constraint(f::Function, nx::Int, nu::Int, nw::Int; idx_ineq=collect(1:0), eval_hess=false)
function Constraint(f::Function, nx::Int, nu::Int; nw::Int=0, idx_ineq=collect(1:0), eval_hess=false)
#TODO: option to load/save methods
@variables x[1:nx], u[1:nu], w[1:nw]
val = f(x, u, w)
Expand Down
44 changes: 0 additions & 44 deletions src/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,48 +232,4 @@ function duals!(λ_dyn::Vector{Vector{T}}, λ_stage::Vector{Vector{T}}, λ_gen::
λ_stage[t] .= @views λ[idx]
end
λ_gen .= λ[idx_gen]
end

struct ProblemData{T} <: MOI.AbstractNLPEvaluator
nlp::NLPData{T}
s_data::SolverData
end

function ProblemData(obj::Objective{T}, dyn::Vector{Dynamics{T}}, cons::Constraints{T}, bnds::Bounds{T};
eval_hess=false,
general_constraint=GeneralConstraint(),
options=Options(),
w=[[zeros(nw) for nw in dimensions(dyn)[3]]..., zeros(0)]) where T

trajopt = TrajectoryOptimizationData(obj, dyn, cons, bnds, w=w)
nlp = NLPData(trajopt, general_constraint=general_constraint, eval_hess=eval_hess)
s_data = SolverData(nlp, options=options)

ProblemData(nlp, s_data)
end

function initialize_states!(p::ProblemData, x)
for (t, xt) in enumerate(x)
n = length(xt)
for i = 1:n
MOI.set(p.s_data.solver, MOI.VariablePrimalStart(), p.s_data.z[p.nlp.idx.x[t][i]], xt[i])
end
end
end

function initialize_controls!(p::ProblemData, u)
for (t, ut) in enumerate(u)
m = length(ut)
for j = 1:m
MOI.set(p.s_data.solver, MOI.VariablePrimalStart(), p.s_data.z[p.nlp.idx.u[t][j]], ut[j])
end
end
end

function get_trajectory(p::ProblemData)
return p.nlp.trajopt.x, p.nlp.trajopt.u[1:end-1]
end

function solve!(p::ProblemData)
MOI.optimize!(p.s_data.solver)
end
2 changes: 1 addition & 1 deletion src/general_constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct GeneralConstraint{T}
idx_ineq::Vector{Int}
end

function GeneralConstraint(f::Function, nz::Int, nw::Int; idx_ineq=collect(1:0), eval_hess=false)
function GeneralConstraint(f::Function, nz::Int; nw::Int=0, idx_ineq=collect(1:0), eval_hess=false)
#TODO: option to load/save methods
@variables z[1:nz], w[1:nw]
val = f(z, w)
Expand Down
2 changes: 1 addition & 1 deletion src/objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct Cost{T}
hess_cache::Vector{T}
end

function Cost(f::Function, nx::Int, nu::Int, nw::Int; eval_hess=false)
function Cost(f::Function, nx::Int, nu::Int; nw::Int=0, eval_hess=false)
#TODO: option to load/save methods
@variables x[1:nx], u[1:nu], w[1:nw]
val = f(x, u, w)
Expand Down
75 changes: 38 additions & 37 deletions src/solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,46 @@
# timing_statistics = :no
end

# struct Solver{T}
# p::Problem{T}
# nlp_bounds::Vector{MOI.NLPBoundsPair}
# block_data::MOI.NLPBlockData
# solver::Ipopt.Optimizer
# z::Vector{MOI.VariableIndex}
# end
struct Solver{T} <: MOI.AbstractNLPEvaluator
nlp::NLPData{T}
s_data::SolverData
end

function solver(dyn::Vector{Dynamics{T}}, obj::Objective{T}, cons::Constraints{T}, bnds::Bounds{T};
options=Options{T}(),
w=[[zeros(nw) for nw in dimensions(dyn)[3]]..., zeros(0)],
eval_hess=false,
general_constraint=GeneralConstraint()) where T

# function Solver(trajopt::TrajectoryOptimizationProblem; eval_hess=false, options=Options())
# p = Problem(trajopt, eval_hess=eval_hess)

# nlp_bounds = MOI.NLPBoundsPair.(p.con_bnds...)
# block_data = MOI.NLPBlockData(nlp_bounds, p, true)

# # instantiate NLP solver
# solver = Ipopt.Optimizer()
trajopt = TrajectoryOptimizationData(obj, dyn, cons, bnds, w=w)
nlp = NLPData(trajopt, general_constraint=general_constraint, eval_hess=eval_hess)
s_data = SolverData(nlp, options=options)

# # set NLP solver options
# for name in fieldnames(typeof(options))
# solver.options[String(name)] = getfield(options, name)
# end

# z = MOI.add_variables(solver, p.num_var)

# for i = 1:p.num_var
# MOI.add_constraint(solver, z[i], MOI.LessThan(p.var_bnds[2][i]))
# MOI.add_constraint(solver, z[i], MOI.GreaterThan(p.var_bnds[1][i]))
# end

# MOI.set(solver, MOI.NLPBlock(), block_data)
# MOI.set(solver, MOI.ObjectiveSense(), MOI.MIN_SENSE)

# return Solver(p, nlp_bounds, block_data, solver, z)
# end
Solver(nlp, s_data)
end

# function initialize!(s::Solver, z)
# for i = 1:s.p.num_var
# MOI.set(s.solver, MOI.VariablePrimalStart(), s.z[i], z[i])
# end
# end
function initialize_states!(p::Solver, x)
for (t, xt) in enumerate(x)
n = length(xt)
for i = 1:n
MOI.set(p.s_data.solver, MOI.VariablePrimalStart(), p.s_data.z[p.nlp.idx.x[t][i]], xt[i])
end
end
end

function initialize_controls!(p::Solver, u)
for (t, ut) in enumerate(u)
m = length(ut)
for j = 1:m
MOI.set(p.s_data.solver, MOI.VariablePrimalStart(), p.s_data.z[p.nlp.idx.u[t][j]], ut[j])
end
end
end

function get_trajectory(p::Solver)
return p.nlp.trajopt.x, p.nlp.trajopt.u[1:end-1]
end

function solve!(p::Solver)
MOI.optimize!(p.s_data.solver)
end
4 changes: 2 additions & 2 deletions test/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
ct = (x, u, w) -> [-ones(nx) - x; x - ones(nx)]
cT = (x, u, w) -> x

cont = Constraint(ct, nx, nu, nw, idx_ineq=collect(1:2nx))
conT = Constraint(cT, nx, 0, 0)
cont = Constraint(ct, nx, nu, nw=nw, idx_ineq=collect(1:2nx))
conT = Constraint(cT, nx, 0, nw=0)

cons = [[cont for t = 1:T-1]..., conT]
nc = DTO.num_con(cons)
Expand Down
10 changes: 5 additions & 5 deletions test/hessian_lagrangian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@
# objective
ot = (x, u, w) -> 0.1 * dot(x[3:4], x[3:4]) + 0.1 * dot(u, u)
oT = (x, u, w) -> 0.1 * dot(x[3:4], x[3:4])
objt = Cost(ot, nx, nu, nw, eval_hess=true)
objT = Cost(oT, nx, 0, nw, eval_hess=true)
objt = Cost(ot, nx, nu, nw=nw, eval_hess=true)
objT = Cost(oT, nx, 0, nw=nw, eval_hess=true)
obj = [[objt for t = 1:T-1]..., objT]

# constraints
Expand All @@ -107,14 +107,14 @@

ct = (x, u, w) -> [-5.0 * ones(nu) - cos.(u) .* sum(x.^2); cos.(x) .* tan.(u) - 5.0 * ones(nx)]
cT = (x, u, w) -> sin.(x.^3.0)
cont = Constraint(ct, nx, nu, nw, idx_ineq=collect(1:(nu + nx)), eval_hess=true)
conT = Constraint(cT, nx, 0, nw, eval_hess=true)
cont = Constraint(ct, nx, nu, nw=nw, idx_ineq=collect(1:(nu + nx)), eval_hess=true)
conT = Constraint(cT, nx, 0, nw=nw, eval_hess=true)
cons = [[cont for t = 1:T-1]..., conT]

# data
# trajopt = DTO.TrajectoryOptimizationData(obj, dyn, cons, bnds)
# nlp = DTO.NLPData(trajopt, eval_hess=true)
p = ProblemData(obj, dyn, cons, bnds, eval_hess=true)
p = solver(dyn, obj, cons, bnds, eval_hess=true)

# Lagrangian
function lagrangian(z)
Expand Down
4 changes: 2 additions & 2 deletions test/objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
nw = 0
ot = (x, u, w) -> dot(x, x) + 0.1 * dot(u, u)
oT = (x, u, w) -> 10.0 * dot(x, x)
ct = Cost(ot, nx, nu, nw)
cT = Cost(oT, nx, 0, nw)
ct = Cost(ot, nx, nu, nw=nw)
cT = Cost(oT, nx, 0, nw=nw)
obj = [[ct for t = 1:T-1]..., cT]

J = [0.0]
Expand Down
21 changes: 10 additions & 11 deletions test/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@
# objective
ot = (x, u, w) -> 0.1 * dot(x[3:4], x[3:4]) + 0.1 * dot(u, u)
oT = (x, u, w) -> 0.1 * dot(x[3:4], x[3:4])
ct = Cost(ot, nx, nu, nw)
cT = Cost(oT, nx, 0, nw)
ct = Cost(ot, nx, nu, nw=nw)
cT = Cost(oT, nx, 0, nw=nw)
obj = [[ct for t = 1:T-1]..., cT]

# constraints
Expand All @@ -109,9 +109,8 @@

cons = [Constraint() for t = 1:T]


# problem
p = ProblemData(obj, dyn, cons, bnds)
p = solver(dyn, obj, cons, bnds)

# initialize
initialize_states!(p, x_interpolation)
Expand Down Expand Up @@ -179,8 +178,8 @@ end
# ## objective
ot = (x, u, w) -> 0.1 * dot(x, x) + 0.1 * dot(u, u)
oT = (x, u, w) -> 0.1 * dot(x, x)
ct = Cost(ot, nx, nu, nw)
cT = Cost(oT, nx, 0, nw)
ct = Cost(ot, nx, nu, nw=nw)
cT = Cost(oT, nx, 0, nw=nw)
obj = [[ct for t = 1:T-1]..., cT]

# ## constraints
Expand All @@ -192,7 +191,7 @@ end
cons = [Constraint() for t = 1:T]

# ## problem
p = ProblemData(obj, dyn, cons, bnds, options=Options())
p = solver(dyn, obj, cons, bnds)

# ## initialize
x_interpolation = linear_interpolation(x1, xT, T)
Expand Down Expand Up @@ -240,8 +239,8 @@ end
# ## objective
ot = (x, u, w) -> 0.1 * dot(x, x) + 0.1 * dot(u, u)
oT = (x, u, w) -> 0.1 * dot(x, x)
ct = Cost(ot, nx, nu, nw, eval_hess=eval_hess)
cT = Cost(oT, nx, 0, nw, eval_hess=eval_hess)
ct = Cost(ot, nx, nu, nw=nw, eval_hess=eval_hess)
cT = Cost(oT, nx, 0, nw=nw, eval_hess=eval_hess)
obj = [[ct for t = 1:T-1]..., cT]

# ## constraints
Expand All @@ -252,10 +251,10 @@ end

cons = [Constraint() for t = 1:T]

gc = GeneralConstraint((z, w) -> z[(end-1):end] - xT, nx * T + nu * (T-1), 0, eval_hess=true)
gc = GeneralConstraint((z, w) -> z[(end-1):end] - xT, nx * T + nu * (T-1), nw=0, eval_hess=true)

# ## problem
p = ProblemData(obj, dyn, cons, bnds,
p = solver(dyn, obj, cons, bnds,
general_constraint=gc,
eval_hess=true,
options=Options())
Expand Down

0 comments on commit 2ac8579

Please sign in to comment.