43 #ifndef ILQGAMES_UTILS_LOG_H 44 #define ILQGAMES_UTILS_LOG_H 46 #include <ilqgames/utils/operating_point.h> 47 #include <ilqgames/utils/strategy.h> 48 #include <ilqgames/utils/types.h> 49 #include <ilqgames/utils/uncopyable.h> 57 std::string DefaultExperimentName();
66 const std::vector<Strategy>& strategies,
67 const std::vector<float>& total_costs,
68 Time cumulative_runtime,
bool was_converged) {
69 operating_points_.push_back(operating_point);
70 strategies_.push_back(strategies);
71 total_player_costs_.push_back(total_costs);
72 cumulative_runtimes_.push_back(cumulative_runtime);
73 was_converged_.push_back(was_converged);
78 for (
size_t ii = 0; ii < log.NumIterates(); ii++) {
79 AddSolverIterate(log.operating_points_[ii], log.strategies_[ii],
80 log.total_player_costs_[ii],
81 log.cumulative_runtimes_[ii], log.was_converged_[ii]);
87 void ClearAllButFirstIterate() {
88 constexpr
size_t kOneIterate = 1;
90 CHECK_GE(operating_points_.size(), kOneIterate);
91 operating_points_.resize(kOneIterate, operating_points_.front());
92 strategies_.resize(kOneIterate);
93 total_player_costs_.resize(kOneIterate);
94 cumulative_runtimes_.resize(kOneIterate);
95 was_converged_.resize(kOneIterate);
99 bool WasConverged()
const {
return was_converged_.back(); }
100 bool WasConverged(
size_t idx)
const {
return was_converged_[idx]; }
101 Time InitialTime()
const {
102 return (NumIterates() > 0) ? operating_points_[0].t0 : 0.0;
104 Time FinalTime()
const {
105 return (NumIterates() > 0) ? IndexToTime(operating_points_[0].xs.size() - 1)
108 PlayerIndex NumPlayers()
const {
return strategies_[0].size(); }
109 size_t NumIterates()
const {
return operating_points_.size(); }
110 std::vector<float> TotalCosts()
const {
return total_player_costs_.back(); }
112 const std::vector<Strategy>& InitialStrategies()
const {
113 return strategies_.front();
116 return operating_points_.front();
118 const std::vector<Strategy>& FinalStrategies()
const {
119 return strategies_.back();
122 return operating_points_.back();
125 VectorXf InterpolateState(
size_t iterate, Time t)
const;
126 float InterpolateState(
size_t iterate, Time t, Dimension dim)
const;
127 VectorXf InterpolateControl(
size_t iterate, Time t, PlayerIndex player)
const;
128 float InterpolateControl(
size_t iterate, Time t, PlayerIndex player,
129 Dimension dim)
const;
131 std::vector<MatrixXf> Ps(
size_t iterate,
size_t time_index)
const;
132 std::vector<VectorXf> alphas(
size_t iterate,
size_t time_index)
const;
133 MatrixXf P(
size_t iterate,
size_t time_index, PlayerIndex player)
const;
134 VectorXf alpha(
size_t iterate,
size_t time_index, PlayerIndex player)
const;
136 VectorXf State(
size_t iterate,
size_t time_index)
const {
137 return operating_points_[iterate].xs[time_index];
139 float State(
size_t iterate,
size_t time_index, Dimension dim)
const {
140 return operating_points_[iterate].xs[time_index](dim);
142 VectorXf Control(
size_t iterate,
size_t time_index,
143 PlayerIndex player)
const {
144 return operating_points_[iterate].us[time_index][player];
146 float Control(
size_t iterate,
size_t time_index, PlayerIndex player,
147 Dimension dim)
const {
148 return operating_points_[iterate].us[time_index][player](dim);
151 std::vector<MatrixXf> Ps(
size_t iterate, Time t)
const {
152 return Ps(iterate, TimeToIndex(t));
154 std::vector<VectorXf> alphas(
size_t iterate, Time t)
const {
155 return alphas(iterate, TimeToIndex(t));
157 MatrixXf P(
size_t iterate, Time t, PlayerIndex player)
const {
158 return P(iterate, TimeToIndex(t), player);
160 VectorXf alpha(
size_t iterate, Time t, PlayerIndex player)
const {
161 return alpha(iterate, TimeToIndex(t), player);
165 size_t TimeToIndex(Time t)
const {
166 return static_cast<size_t>(
167 std::max<Time>(constants::kSmallNumber, t - InitialTime()) /
172 Time IndexToTime(
size_t idx)
const {
173 return InitialTime() + time::kTimeStep *
static_cast<Time
>(idx);
177 bool Save(
bool only_last_trajectory =
false,
178 const std::string& experiment_name = DefaultExperimentName())
const;
183 std::vector<OperatingPoint> operating_points_;
184 std::vector<std::vector<Strategy>> strategies_;
185 std::vector<std::vector<float>> total_player_costs_;
186 std::vector<Time> cumulative_runtimes_;
187 std::vector<bool> was_converged_;
191 bool SaveLogs(
const std::vector<SolverLog>& logs,
192 bool only_last_trajectory =
true,
193 const std::string& experiment_name = DefaultExperimentName());
194 bool SaveLogs(
const std::vector<std::shared_ptr<const SolverLog>>& logs,
195 bool only_last_trajectory =
true,
196 const std::string& experiment_name = DefaultExperimentName());