Slow but steady progress on the mbh

This commit is contained in:
Connor
2021-09-26 00:01:50 -06:00
parent 49eff02cd0
commit 26ddb43a5d
8 changed files with 420 additions and 191 deletions

View File

@@ -1,86 +1,243 @@
using Dates
export mbh
"""
Generates n pareto-distributed random numbers
Generates pareto-distributed random numbers
This is usually around one to two percent, but sometimes up to ten or so (fat tails)
"""
function pareto(α::Float64, n::Int)
s = rand((-1,1), (n,3))
r = rand(Float64, (n,3))
ϵ = 1e-10
return (s./ϵ) * (α - 1.0) ./ (ϵ ./ (ϵ .+ r)).^(-α)
function pareto(shape::Tuple{Int, Int}, α::Float64=1.01)
s = rand((-1,1), shape)
r = rand(Float64, shape)
ϵ = 1e-10
return 1 .+ (s./ϵ) * (α - 1.0) ./ (ϵ ./ (ϵ .+ r)).^(-α)
end
function pareto(shape::Int, α::Float64=1.01)
s = rand((-1,1), shape)
r = rand(Float64, shape)
ϵ = 1e-10
return 1 .+ (s./ϵ) * (α - 1.0) ./ (ϵ ./ (ϵ .+ r)).^(-α)
end
function pareto(α::Float64=1.01)
s = rand((-1,1))
r = rand(Float64)
ϵ = 1e-10
return 1 + (s./ϵ) * (α - 1.0) ./ (ϵ ./ (ϵ .+ r)).^(-α)
end
function pareto_add(α::Float64=1.01)
s = rand((-1,1))
r = rand(Float64)
ϵ = 1e-10
return (s./ϵ) * (α - 1.0) ./ (ϵ ./ (ϵ .+ r)).^(-α)
end
"""
Perturbs the monotonic basin hopping decision vector
TODO: This needs to be updated
Returns a random date between two dates
"""
function perturb(x::AbstractMatrix, n::Int)
ans = x + pareto(1.01, n)
map!(elem -> elem > 1.0 ? 1.0 : elem, ans, ans)
map!(elem -> elem < -1.0 ? -1.0 : elem, ans, ans)
return ans
function gen_date(date_range::Tuple{DateTime, DateTime})
l0, lf = date_range
l0 + Dates.Millisecond(floor(rand()*(lf-l0).value))
end
function new_x(n::Int)
2.0 * rand(Float64, (n,3)) .- 1.
"""
Returns a random amount of time in a range
"""
function gen_period(date_range::Tuple{DateTime, DateTime})
l0, lf = date_range
Dates.Millisecond(floor(rand()*(lf-l0).value))
end
function mbh(flybys::Vector{Planet})
end
function mbh(start::AbstractVector,
final::AbstractVector,
craft::Sc,
μ::AbstractFloat,
t0::AbstractFloat,
tf::AbstractFloat,
n::Int;
search_patience_lim::Int=2000,
drill_patience_lim::Int=40,
tol=1e-6,
verbose=false)
archive = []
i = 0
x_current = Result(false, 1e8*ones(n,3))
while i < search_patience_lim
i += 1
drill_impatience = 0
if verbose print("\r\t", "search: ", i, " drill: ", drill_impatience, " ") end
x_star = nlp_solve(start, final, craft, μ, t0, tf, new_x(n), tol=tol)
# If x_star is converged and better, set new x_current
if x_star.converged && mass_est(x_star.zero) < mass_est(x_current.zero)
x_current = x_star
end
# If x_star is converged, drill down, otherwise, start over
if x_star.converged
while drill_impatience < drill_patience_lim
x_star = nlp_solve(start, final, craft, μ, t0, tf, perturb(x_current.zero,n), tol=tol)
if x_star.converged && mass_est(x_star.zero) < mass_est(x_current.zero)
x_current = x_star
drill_impatience = 0
else
if verbose print("\r\t", "search: ", i, " drill: ", drill_impatience, " ") end
drill_impatience += 1
end
end
push!(archive, x_current)
end
"""
Perturbs a valid mission with pareto-distributed variables, generating a mission guess
"""
function perturb(mission::Mission)
new_launch_date = mission.launch_date + Dates.Second(floor(7day * pareto_add()))
new_launch_v∞ = mission.launch_v∞ .* pareto(3)
new_phases = Vector{Phase}()
for phase in mission.phases
new_v∞_in = phase.v∞_in .* pareto(3)
new_δ = phase.δ * pareto()
new_tof = phase.tof * pareto()
new_thrust_profile = phase.thrust_profile .* pareto(size(phase.thrust_profile))
push!(new_phases, Phase(phase.planet, new_v∞_in, new_δ, new_tof, new_thrust_profile))
end
if verbose println() end
# TODO: Mission_Guess.validate()
Mission_Guess(mission.sc, mission.start_mass, new_launch_date, new_launch_v∞, new_phases)
end
if archive == [] error("there were no converged paths") end
"""
Generates a new randomized guess for the mission decision variables
"""
function mission_guess( flybys::Vector{Body},
sc::Sc,
start_mass::Float64,
launch_window::Tuple{DateTime, DateTime},
max_C3_out::Float64,
max_v∞_in_mag::Float64,
latest_arrival::DateTime,
primary::Body=Sun )
# TODO: Eventually I can calculate n more intelligently
n = 20
current_best_mass = 1e8
best = archive[1]
for candidate in archive
if mass_est(candidate.zero) < current_best_mass
current_best_mass = mass_est(candidate.zero)
best = candidate
# Determine the launch conditions
launch_date = gen_date(launch_window)
launch_v∞_normalized = rand(-1:0.0001:1, 3)
launch_v∞ = rand(0:0.0001:max_C3_out) * launch_v∞_normalized/norm(launch_v∞_normalized)
# Determine the leg lengths
num_phases = length(flybys) - 1
total_tof = 100year
max_tof = (latest_arrival - launch_date).value / 1000
tofs = rand(30day : hour : 0.7max_tof, num_phases)
total_tof = sum(tofs)
while total_tof > max_tof
tofs = rand(30day : hour : 0.7max_tof, num_phases)
total_tof = sum(tofs)
end
# For each phase, determine the v∞_in and δ
phases = Vector{Phase}()
for i in 1:num_phases
flyby = flybys[i+1]
v∞_normalized = rand(-1:0.0001:1, 3)
v∞ = rand(0:0.0001:10) * v∞_normalized/norm(v∞_normalized)
δ = rand(0:0.0001:2π)
periapsis = (flyby.μ/(v∞ v∞)) * ( 1/sin(δ/2) - 1 )
while periapsis < flyby.r + 100.
δ = rand(0:0.0001:2π)
periapsis = (flyby.μ/(v∞ v∞)) * ( 1/sin(δ/2) - 1 )
end
thrusts = rand(-1:0.0001:1,(n,3))
push!(phases, Phase(flyby, v∞, δ, tofs[i], thrusts))
end
# Finally, determine the arrival v∞
arrival_v∞_normalized = rand(-1:0.0001:1, 3)
arrival_v∞ = rand(0:0.0001:max_v∞_in_mag) * arrival_v∞_normalized/norm(arrival_v∞_normalized)
# And we can construct a mission guess object with these values
Mission_Guess( sc, start_mass, launch_date, launch_v∞, phases )
end
"""
A convenience function for calculating mass usage given a certain thrust profile
"""
function mass_consumption(sc::Sc, phase::Phase)
weighted_thrusting_time = 0.0
n = size(phase.thrust_profile)[1]
for i in 1:size(phase.thrust_profile,1)
weighted_thrusting_time += norm(phase.thrust_profile[i,:]) * phase.tof/n
end
return weighted_thrusting_time*sc.mass_flow_rate
end
"""
This attempts to determine v∞_out from v∞_in and the turning angle, assuming we are staying in the
plane of the three planets
"""
function calc_turn(p1::Body, p2::Body, p3::Body, v∞_in::Vector{Float64}, δ::Float64)
end
"""
Sequentially calls the NLP solver to attempt to solve based on the initial guess
"""
function inner_loop_solve(guess::Mission_Guess)
v∞_out = guess.launch_v∞
time = utc2et(Dates.format(guess.launch_date,"yyyy-mm-ddTHH:MM:SS"))
current_planet = Earth
start = [spkssb(Earth.id, time, "ECLIPJ2000"); 0.0] + [ zeros(3); guess.launch_v∞; guess.start_mass ]
corrected_phases = Vector{Phase}()
for i in 1:length(guess.phases)
phase = guess.phases[i]
time += phase.tof
goal = spkssb(phase.planet.id, time, "ECLIPJ2000") + [zeros(3); phase.v∞_in]
result = solve_phase( start, goal, guess.sc, phase.tof, phase.thrust_profile)
converged(result) || return Bad_Mission() # Drop if it's not working
corrected_phase = Phase(phase.planet, phase.v∞_in, phase.δ, phase.tof, result.zero)
push!(corrected_phases, corrected_phase)
mass_used = mass_consumption(guess.sc, corrected_phase)
if i != length(guess.phases)
v∞_out = calc_turn(current_planet, phase.planet, guess.phases[i+1].planet, phase.v∞_in, phase.δ)
current_planet = phase.planet
planet_state = [spkssb(current_planet.id, time, "ECLIPJ2000"); 0.0]
start = planet_state + [ zeros(3); v∞_out; start_mass - mass_used ]
end
end
return best, archive
return Mission(guess.sc, guess.start_mass, guess.launch_date, guess.launch_v∞, corrected_phases)
end
"""
The cost function for the mission
TODO: This will probably move and eventually be passed as an argument
"""
function cost(mission::Mission, max_C3::Float64, max_v∞::Float64)
mass_used = 0.0
for phase in mission.phases mass_used += mass_used(sc, phase) end
mass_percent = mass_used/mission.sc.dry_mass
C3_percent = ( mission.launch_v∞ mission.launch_v∞ ) / max_C3
v∞_percent = norm(mission.phases[end].v∞_in) / max_v∞
return 3mass_percent + C3_percent + v∞_percent
end
function mbh( flybys::Vector{Body},
sc::Sc,
start_mass::Float64,
launch_window::Pair{Date},
max_C3::Float64,
max_v∞::Float64,
latest_arrival::Date,
primary::Body=Sun;
search_patience::Int=1_000,
drill_patience::Int=50)
# Let's pseudo-code this bitch
#
# First, we need a function (mission_guess below) that randomly generates the:
# - Launch date
# - Launch v∞_out
# - for each phase:
# - tof
# - v∞_in
# - turning angle
#
# Also need a function (inner_loop_solve below) that takes the generated decision vector guess and
# attempts to correct it with the NLP solver. It will either converge or not.
#
# Also need a costing function (may be provided potentially)
#
# Also need a perturb function
#
guess = mission_guess(flybys, sc, start_mass, launch_window, max_C3, max_v∞, latest_arrival, primary)
inner_loop_solve(guess)
# search_count = 0
# x_current = "Bad"
# while search_count < search_patience
# search_count += 1
# drill_count = 0
# x_star = inner_loop_solve(mission_guess())
# if x_star.converged
# if cost(x_star) < cost(x_current)
# x_current = x_star
# while drill_count < drill_patience
# x_star = inner_loop_solve(perturb(x_current))
# if x_star.converged and cost(x_star) < cost(x_current)
# x_current = x_star
# drill_count = 0
# else
# drill_count += 1
# end
# end
# push!(archive, x_current)
# end
# end
#
#
end

View File

@@ -31,8 +31,7 @@ function solve_phase( start::Vector{Float64},
end
result = nlsolve(f!, atanh.(x0), ftol=tol, autodiff=:forward, iterations=num_iters)
converged(result) || throw(Convergence_Error())
result.zero = tanh.(result.zero)
if converged(result) result.zero = tanh.(result.zero) end
return result

View File

@@ -68,4 +68,4 @@ end
"""
Convenience function for propagating a state with no thrust
"""
prop(x::Vector{Float64}, t::Float64, p::Body=Sun) = prop(zeros(1000,3), x, no_thrust, t, p)[1]
prop(x::Vector{Float64}, t::Float64, p::Body=Sun) = prop(zeros(1000,3), [x;1e10], no_thrust, t, p)[1]