Commit f49d32f3 authored by Erik Strand's avatar Erik Strand

Compute loss

Numbers aren't obviously wrong
parent 69acc484
......@@ -23,11 +23,29 @@ struct LaunchObjective {
}
void operator()(VectorX<Scalar> const& velocity, Scalar& loss, VectorX<Scalar>& gradient) {
// prepare initial conditions
initial_conditions_.segment<dim_>(3 * dim_) = velocity;
// run the simulation
nbody_.initialize(dim_, masses_, initial_conditions_, delta_t_);
for (uint32_t i = 0; i < n_steps_; ++i) {
nbody_.step();
}
// compute loss
Vector2<Scalar> const goal = Vector2<Scalar>(0, 1);
Vector2<Scalar> const difference = nbody_.state().segment<dim_>(dim_) - goal;
loss = difference.squaredNorm();
// compute gradient of loss
// loss = difference^T * difference
// d_loss = 2 * difference^T * d_difference
gradient = 2 * difference.transpose() * nbody_.state_jacobian().block<dim_, dim_>(dim_, 3 * dim_);
// print stuff
std::cout << "loss = " << loss << "\n";
std::cout << "loss gradient\n";
std::cout << gradient << '\n';
std::cout << "state jacobian\n";
std::cout << nbody_.state_jacobian() << '\n';
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment