48 #include <ilqgames/examples/receding_horizon_simulator.h> 49 #include <ilqgames/solver/ilq_solver.h> 50 #include <ilqgames/solver/problem.h> 51 #include <ilqgames/solver/solution_splicer.h> 52 #include <ilqgames/utils/solver_log.h> 53 #include <ilqgames/utils/strategy.h> 54 #include <ilqgames/utils/types.h> 56 #include <glog/logging.h> 63 using clock = std::chrono::system_clock;
65 std::vector<std::shared_ptr<const SolverLog>> RecedingHorizonSimulator(
66 Time final_time, Time planner_runtime, GameSolver* solver) {
67 CHECK_NOTNULL(solver);
70 std::vector<std::shared_ptr<const SolverLog>> logs;
74 auto solver_call_time = clock::now();
76 logs.push_back(solver->Solve(&success));
79 std::chrono::duration<Time>(clock::now() - solver_call_time).count();
81 VLOG(1) <<
"Solved initial problem in " << elapsed_time <<
" seconds, with " 82 << logs.back()->NumIterates() <<
" iterations.";
83 const auto& dynamics = solver->GetProblem().Dynamics();
86 SolutionSplicer splicer(*logs.front());
90 VectorXf x(solver->GetProblem().InitialState());
91 Time t = splicer.CurrentOperatingPoint().t0;
96 constexpr Time kExtraTime = 0.25;
99 if (t >= final_time ||
100 !splicer.ContainsTime(t + planner_runtime + time::kTimeStep))
103 x = solver->GetProblem().Dynamics()->Integrate(
104 t - kExtraTime, t, x, splicer.CurrentOperatingPoint(),
105 splicer.CurrentStrategies());
108 solver->GetProblem().OverwriteSolution(splicer.CurrentOperatingPoint(),
109 splicer.CurrentStrategies());
112 solver->GetProblem().SetUpNextRecedingHorizon(x, t, planner_runtime);
114 solver_call_time = clock::now();
115 logs.push_back(solver->Solve(&success, planner_runtime));
117 std::chrono::duration<Time>(clock::now() - solver_call_time).count();
119 CHECK_LE(elapsed_time, planner_runtime);
120 VLOG(1) <<
"t = " << t <<
": Solved warm-started problem in " 121 << elapsed_time <<
" seconds.";
125 if (t >= final_time || !splicer.ContainsTime(t))
break;
128 x = solver->GetProblem().Dynamics()->Integrate(
129 t - elapsed_time, t, x, splicer.CurrentOperatingPoint(),
130 splicer.CurrentStrategies());
133 if (logs.back()->WasConverged()) splicer.Splice(*logs.back());