|
31 | 31 | # timing_statistics = :no |
32 | 32 | end |
33 | 33 |
|
34 | | -# struct Solver{T} |
35 | | -# p::Problem{T} |
36 | | -# nlp_bounds::Vector{MOI.NLPBoundsPair} |
37 | | -# block_data::MOI.NLPBlockData |
38 | | -# solver::Ipopt.Optimizer |
39 | | -# z::Vector{MOI.VariableIndex} |
40 | | -# end |
| 34 | +struct Solver{T} <: MOI.AbstractNLPEvaluator |
| 35 | + nlp::NLPData{T} |
| 36 | + s_data::SolverData |
| 37 | +end |
| 38 | + |
| 39 | +function solver(dyn::Vector{Dynamics{T}}, obj::Objective{T}, cons::Constraints{T}, bnds::Bounds{T}; |
| 40 | + options=Options{T}(), |
| 41 | + w=[[zeros(nw) for nw in dimensions(dyn)[3]]..., zeros(0)], |
| 42 | + eval_hess=false, |
| 43 | + general_constraint=GeneralConstraint()) where T |
41 | 44 |
|
42 | | -# function Solver(trajopt::TrajectoryOptimizationProblem; eval_hess=false, options=Options()) |
43 | | -# p = Problem(trajopt, eval_hess=eval_hess) |
44 | | - |
45 | | -# nlp_bounds = MOI.NLPBoundsPair.(p.con_bnds...) |
46 | | -# block_data = MOI.NLPBlockData(nlp_bounds, p, true) |
47 | | - |
48 | | -# # instantiate NLP solver |
49 | | -# solver = Ipopt.Optimizer() |
| 45 | + trajopt = TrajectoryOptimizationData(obj, dyn, cons, bnds, w=w) |
| 46 | + nlp = NLPData(trajopt, general_constraint=general_constraint, eval_hess=eval_hess) |
| 47 | + s_data = SolverData(nlp, options=options) |
50 | 48 |
|
51 | | -# # set NLP solver options |
52 | | -# for name in fieldnames(typeof(options)) |
53 | | -# solver.options[String(name)] = getfield(options, name) |
54 | | -# end |
55 | | - |
56 | | -# z = MOI.add_variables(solver, p.num_var) |
57 | | - |
58 | | -# for i = 1:p.num_var |
59 | | -# MOI.add_constraint(solver, z[i], MOI.LessThan(p.var_bnds[2][i])) |
60 | | -# MOI.add_constraint(solver, z[i], MOI.GreaterThan(p.var_bnds[1][i])) |
61 | | -# end |
62 | | - |
63 | | -# MOI.set(solver, MOI.NLPBlock(), block_data) |
64 | | -# MOI.set(solver, MOI.ObjectiveSense(), MOI.MIN_SENSE) |
65 | | - |
66 | | -# return Solver(p, nlp_bounds, block_data, solver, z) |
67 | | -# end |
| 49 | + Solver(nlp, s_data) |
| 50 | +end |
68 | 51 |
|
69 | | -# function initialize!(s::Solver, z) |
70 | | -# for i = 1:s.p.num_var |
71 | | -# MOI.set(s.solver, MOI.VariablePrimalStart(), s.z[i], z[i]) |
72 | | -# end |
73 | | -# end |
| 52 | +function initialize_states!(p::Solver, x) |
| 53 | + for (t, xt) in enumerate(x) |
| 54 | + n = length(xt) |
| 55 | + for i = 1:n |
| 56 | + MOI.set(p.s_data.solver, MOI.VariablePrimalStart(), p.s_data.z[p.nlp.idx.x[t][i]], xt[i]) |
| 57 | + end |
| 58 | + end |
| 59 | +end |
74 | 60 |
|
| 61 | +function initialize_controls!(p::Solver, u) |
| 62 | + for (t, ut) in enumerate(u) |
| 63 | + m = length(ut) |
| 64 | + for j = 1:m |
| 65 | + MOI.set(p.s_data.solver, MOI.VariablePrimalStart(), p.s_data.z[p.nlp.idx.u[t][j]], ut[j]) |
| 66 | + end |
| 67 | + end |
| 68 | +end |
75 | 69 |
|
| 70 | +function get_trajectory(p::Solver) |
| 71 | + return p.nlp.trajopt.x, p.nlp.trajopt.u[1:end-1] |
| 72 | +end |
| 73 | + |
| 74 | +function solve!(p::Solver) |
| 75 | + MOI.optimize!(p.s_data.solver) |
| 76 | +end |
0 commit comments