Skip to content

Instantly share code, notes, and snippets.

@db48x
Created July 30, 2021 10:56
Show Gist options
  • Save db48x/e5427620a874c3622b6b6d73e48b6fbf to your computer and use it in GitHub Desktop.
Save db48x/e5427620a874c3622b6b6d73e48b6fbf to your computer and use it in GitHub Desktop.
Jul 30 03:45:08.613 INFO Steepest Descent, max_iters: 10
Jul 30 03:45:08.613 INFO iter: 0, cost: 174.77841665711315, best_cost: 174.77841665711315, cost_func_count: 1, grad_func_count: 1, jacobian_func_count: 0, hessian_func_count: 0, modify_func_count: 0, time: 0.000049513000000000005
Jul 30 03:45:08.613 INFO iter: 1, cost: 65.02249040964746, best_cost: 65.02249040964746, cost_func_count: 2, grad_func_count: 2, jacobian_func_count: 0, hessian_func_count: 0, modify_func_count: 0, time: 0.00006891
Jul 30 03:45:08.613 INFO iter: 2, cost: 17.830063056602913, best_cost: 17.830063056602913, cost_func_count: 3, grad_func_count: 3, jacobian_func_count: 0, hessian_func_count: 0, modify_func_count: 0, time: 0.000035076
Jul 30 03:45:08.613 INFO iter: 3, cost: 7.958180134215115, best_cost: 7.958180134215115, cost_func_count: 4, grad_func_count: 4, jacobian_func_count: 0, hessian_func_count: 0, modify_func_count: 0, time: 0.00007735600000000001
Jul 30 03:45:08.613 INFO iter: 4, cost: 3.8463517943209946, best_cost: 3.8463517943209946, cost_func_count: 5, grad_func_count: 5, jacobian_func_count: 0, hessian_func_count: 0, modify_func_count: 0, time: 0.000044524
Jul 30 03:45:08.614 INFO iter: 5, cost: 1.4063704889435558, best_cost: 1.4063704889435558, cost_func_count: 6, grad_func_count: 6, jacobian_func_count: 0, hessian_func_count: 0, modify_func_count: 0, time: 0.00008306600000000001
Jul 30 03:45:08.614 INFO iter: 6, cost: 1.2885260656147053, best_cost: 1.2885260656147053, cost_func_count: 7, grad_func_count: 7, jacobian_func_count: 0, hessian_func_count: 0, modify_func_count: 0, time: 0.000065113
Jul 30 03:45:08.614 INFO iter: 7, cost: 1.14948200947968, best_cost: 1.14948200947968, cost_func_count: 8, grad_func_count: 8, jacobian_func_count: 0, hessian_func_count: 0, modify_func_count: 0, time: 0.000054953
Jul 30 03:45:08.614 INFO iter: 8, cost: 1.0361985264927551, best_cost: 1.0361985264927551, cost_func_count: 9, grad_func_count: 9, jacobian_func_count: 0, hessian_func_count: 0, modify_func_count: 0, time: 0.000067857
Jul 30 03:45:08.614 INFO iter: 9, cost: 0.9349732270182578, best_cost: 0.9349732270182578, cost_func_count: 10, grad_func_count: 10, jacobian_func_count: 0, hessian_func_count: 0, modify_func_count: 0, time: 0.0000688
let fleet_x = f64::from(p.translation.x);
let fleet_y = f64::from(p.translation.y);
let fleet_v = f64::from(f.v);
let orbit_radius = f64::from(orbit.radius);
let orbit_speed = f64::from(orbit.speed);
let orbit_anomaly = f64::from(orbit.true_anomaly) + f64::atan2(dest.y.into(), dest.x.into());
use argmin::prelude::{ArgminOp, ArgminSlogLogger, ObserverMode, Error, Executor};
use argmin::solver::gradientdescent::SteepestDescent;
use argmin::solver::linesearch::MoreThuenteLineSearch;
struct Func {
fleet_x: f64,
fleet_y: f64,
fleet_v: f64,
orbit_radius: f64,
orbit_speed: f64,
orbit_anomaly: f64,
}
impl ArgminOp for Func {
type Param = Vec<f64>;
type Output = f64;
type Hessian = Vec<f64>;
type Jacobian = Vec<f64>;
type Float = f64;
fn apply(&self, x: &Self::Param) -> Result<Self::Output, Error> {
let t = x[0];
let θ = x[1];
let planet_pos = DVec2::new(self.orbit_radius * f64::cos(self.orbit_speed * t + self.orbit_anomaly),
self.orbit_radius * f64::sin(self.orbit_speed * t + self.orbit_anomaly));
let ship_pos = DVec2::new(self.fleet_x + self.fleet_v * t * f64::cos(θ),
self.fleet_y + self.fleet_v * t * f64::sin(θ));
Ok(ship_pos.distance(planet_pos))
}
fn gradient(&self, x: &Self::Param) -> Result<Self::Param, Error> {
let t = x[0];
let θ = x[1];
Ok(vec!{
(2.0*(self.orbit_radius*f64::cos(t*self.orbit_speed+self.orbit_anomaly)-t*self.fleet_v*f64::cos(θ)-self.fleet_x)*((-self.orbit_radius*self.orbit_speed*f64::sin(t*self.orbit_speed+self.orbit_anomaly))-self.fleet_v*f64::cos(θ))+2.0*(self.orbit_radius*self.orbit_speed*f64::cos(t*self.orbit_speed+self.orbit_anomaly)-self.fleet_v*f64::sin(θ))*(self.orbit_radius*f64::sin(t*self.orbit_speed+self.orbit_anomaly)-t*self.fleet_v*f64::sin(θ)-self.fleet_y))/(2.0*f64::sqrt((self.orbit_radius*f64::sin(t*self.orbit_speed+self.orbit_anomaly)-t*self.fleet_v*f64::sin(θ)-self.fleet_y).powf(2.0)+(self.orbit_radius*f64::cos(t*self.orbit_speed+self.orbit_anomaly)-t*self.fleet_v*f64::cos(θ)-self.fleet_x).powf(2.0))),
(2.0*t*self.fleet_v*f64::sin(θ)*(self.orbit_radius*f64::cos(t*self.orbit_speed+self.orbit_anomaly)-t*self.fleet_v*f64::cos(θ)-self.fleet_x)-2.0*t*self.fleet_v*f64::cos(θ)*(self.orbit_radius*f64::sin(t*self.orbit_speed+self.orbit_anomaly)-t*self.fleet_v*f64::sin(θ)-self.fleet_y))/(2.0*f64::sqrt((self.orbit_radius*f64::sin(t*self.orbit_speed+self.orbit_anomaly)-t*self.fleet_v*f64::sin(θ)-self.fleet_y).powf(2.0)+(self.orbit_radius*f64::cos(t*self.orbit_speed+self.orbit_anomaly)-t*self.fleet_v*f64::cos(θ)-self.fleet_x).powf(2.0)))
})
}
}
let init_param: Vec<f64> = vec![0.0, 0.0];
let solver = SteepestDescent::new(MoreThuenteLineSearch::new());
let solution = Executor::new(Func { fleet_x, fleet_y, fleet_v, orbit_radius, orbit_speed, orbit_anomaly },
solver, init_param)
.add_observer(ArgminSlogLogger::term(), ObserverMode::Always)
.max_iters(10)
.run()
.expect("solver failed‽");
let heading = solution.state().best_param[1] as f32;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment