Update for meeting with Bosanac
This commit is contained in:
110
julia/src/inner_loop/nlp_solver.jl
Normal file
110
julia/src/inner_loop/nlp_solver.jl
Normal file
@@ -0,0 +1,110 @@
|
||||
using SNOW
|
||||
|
||||
export solve_mission
|
||||
|
||||
struct Result
|
||||
converged::Bool
|
||||
info::Symbol
|
||||
sol::Matrix{Float64}
|
||||
end
|
||||
|
||||
"""
|
||||
This function will take an initial guess to a mission profile and leverage Ipopt to solve the mission
|
||||
"""
|
||||
function solve_mission( guess::Mission_Guess;
|
||||
tol=1e-8,
|
||||
verbose::Bool=false )
|
||||
# First we need to define a function that propagates the mission all the way through
|
||||
# The function takes in a constraints vector (g) and an input vector (x) and spits out
|
||||
# the objective function. In my case, the objective will be constant.
|
||||
#
|
||||
|
||||
# First we define our starting point
|
||||
x0 = Vector(guess)
|
||||
n = size(guess.phases[1].thrust_profile)[1]
|
||||
flybys = [ p.planet for p in guess.phases ]
|
||||
|
||||
# And our NLP
|
||||
function optimizer!(g,x)
|
||||
# Establish initial conditions
|
||||
mass = guess.start_mass
|
||||
v∞_out = x[2:4]
|
||||
current_planet = Earth
|
||||
launch_date = Dates.unix2datetime(x[1])
|
||||
time = utc2et(Dates.format(launch_date,"yyyy-mm-ddTHH:MM:SS"))
|
||||
start = state(current_planet, time, v∞_out, mass)
|
||||
|
||||
# Now, for each phase we must require:
|
||||
# - That the ending state matches (unless final leg)
|
||||
# - That the v∞_out == v∞_in (unless final leg)
|
||||
# - That the minimum height is acceptable (unless final leg)
|
||||
# - That the ending position matches (if final leg)
|
||||
# - That the ending mass is acceptable (if final leg)
|
||||
i = 0
|
||||
for flyby in flybys
|
||||
phase_params = x[ 5 + (3n+7) * i : 5 + (3n+7) * (i+1) - 1 ]
|
||||
v∞_in = phase_params[1:3]
|
||||
v∞_out = phase_params[4:6]
|
||||
tof = phase_params[7]
|
||||
thrusts = reshape(phase_params[8:end], (n,3))
|
||||
current_planet = flyby
|
||||
time += tof
|
||||
goal = state(current_planet, time, v∞_in)
|
||||
final = prop(reshape(thrusts, (n,3)), start, guess.sc, tof)[2]
|
||||
if flyby != flybys[end]
|
||||
g[1+8i:6+8i] .= final[1:6] .- goal[1:6]
|
||||
g[7+8i] = norm(v∞_out) - norm(v∞_in)
|
||||
δ = acos( ( v∞_in ⋅ v∞_out ) / ( norm(v∞_in) * norm(v∞_out) ) )
|
||||
g[8+8i] = (current_planet.μ/(v∞_in ⋅ v∞_in)) * ( 1/sin(δ/2) - 1 )
|
||||
else
|
||||
g[8length(flybys)+1:8length(flybys)+3] .= final[1:3] .- goal[1:3]
|
||||
g[8length(flybys)+4] = final[7]
|
||||
end
|
||||
start = state(current_planet, time, v∞_out, final[7])
|
||||
i += 1
|
||||
return 1.0
|
||||
end
|
||||
end
|
||||
g = zeros(8length(flybys)+4)
|
||||
optimizer!(g,x0)
|
||||
return g
|
||||
|
||||
|
||||
|
||||
# n = size(x0)[1]
|
||||
|
||||
# function f!(g,x)
|
||||
# try
|
||||
# g[1:6] .= prop(reshape(x,(n,3)), start, craft, tof, primary)[2][1:6]
|
||||
# return 1.
|
||||
# catch e
|
||||
# if isa(e,LaGuerreConway_Error) || isa(e, Mass_Error) || isa(e, PropOne_Error)
|
||||
# return 10_000.
|
||||
# else
|
||||
# rethrow
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
|
||||
# lower_x = -1 * ones(3n)
|
||||
# upper_x = ones(3n)
|
||||
# num_constraints = 6
|
||||
# filename = "ipopt_"*string(rand(UInt))
|
||||
# ipopt_options = Dict("constr_viol_tol" => tol,
|
||||
# "acceptable_constr_viol_tol" => 1e-4,
|
||||
# "max_iter" => 10_000,
|
||||
# "max_cpu_time" => 30.,
|
||||
# "print_level" => 0,
|
||||
# "output_file" => filename)
|
||||
# options = Options(solver=IPOPT(ipopt_options),
|
||||
# derivatives=ForwardAD())
|
||||
# x, _, info = minimize(f!, vec(x0), num_constraints, lower_x, upper_x, final[1:6], final[1:6], options)
|
||||
# rm(filename)
|
||||
# if info in [:Solve_Succeeded, :Solved_To_Acceptable_Level]
|
||||
# return Result(true, info, reshape(x,(n,3)))
|
||||
# else
|
||||
# if verbose println(info) end
|
||||
# return Result(false, info, x0)
|
||||
# end
|
||||
|
||||
end
|
||||
@@ -1,60 +0,0 @@
|
||||
using SNOW
|
||||
|
||||
export solve_phase
|
||||
|
||||
struct Result
|
||||
converged::Bool
|
||||
info::Symbol
|
||||
sol::Matrix{Float64}
|
||||
end
|
||||
|
||||
"""
|
||||
This function will take a single phase (so an initial state, and a final state) and an initial guess
|
||||
to the thrust profile and use an NLP solver to find the nearest thrust profile to that initial guess
|
||||
that satisfies the final state condition
|
||||
"""
|
||||
function solve_phase( start::Vector{Float64},
|
||||
final::Vector{Float64},
|
||||
craft::Sc,
|
||||
tof::Float64,
|
||||
x0::Matrix{Float64},
|
||||
primary::Body=Sun;
|
||||
tol=1e-8,
|
||||
verbose::Bool=false )
|
||||
n = size(x0)[1]
|
||||
|
||||
function f!(g,x)
|
||||
try
|
||||
g[1:6] .= prop(reshape(x,(n,3)), start, craft, tof, primary)[2][1:6]
|
||||
return 1.
|
||||
catch e
|
||||
if isa(e,LaGuerreConway_Error) || isa(e, Mass_Error) || isa(e, PropOne_Error)
|
||||
return 10_000.
|
||||
else
|
||||
rethrow
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
lower_x = -1 * ones(3n)
|
||||
upper_x = ones(3n)
|
||||
num_constraints = 6
|
||||
filename = "ipopt_"*string(rand(UInt))
|
||||
ipopt_options = Dict("constr_viol_tol" => tol,
|
||||
"acceptable_constr_viol_tol" => 1e-4,
|
||||
"max_iter" => 10_000,
|
||||
"max_cpu_time" => 30.,
|
||||
"print_level" => 0,
|
||||
"output_file" => filename)
|
||||
options = Options(solver=IPOPT(ipopt_options),
|
||||
derivatives=ForwardAD())
|
||||
x, _, info = minimize(f!, vec(x0), num_constraints, lower_x, upper_x, final[1:6], final[1:6], options)
|
||||
rm(filename)
|
||||
if info in [:Solve_Succeeded, :Solved_To_Acceptable_Level]
|
||||
return Result(true, info, reshape(x,(n,3)))
|
||||
else
|
||||
if verbose println(info) end
|
||||
return Result(false, info, x0)
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user