43 #include <ilqgames/utils/make_directory.h> 44 #include <ilqgames/utils/operating_point.h> 45 #include <ilqgames/utils/solver_log.h> 46 #include <ilqgames/utils/strategy.h> 47 #include <ilqgames/utils/types.h> 48 #include <ilqgames/utils/uncopyable.h> 50 #include <glog/logging.h> 54 #include <sys/types.h> 63 VectorXf SolverLog::InterpolateState(
size_t iterate, Time t)
const {
64 const OperatingPoint& op = operating_points_[iterate];
67 const size_t lo = TimeToIndex(t);
68 const size_t hi = std::min(lo + 1, op.xs.size() - 1);
71 const float frac = (t - IndexToTime(lo)) / time::kTimeStep;
72 return (1.0 - frac) * op.xs[lo] + frac * op.xs[hi];
75 float SolverLog::InterpolateState(
size_t iterate, Time t, Dimension dim)
const {
76 const OperatingPoint& op = operating_points_[iterate];
79 const size_t lo = TimeToIndex(t);
80 const size_t hi = std::min(lo + 1, op.xs.size() - 1);
83 const float frac = (t - IndexToTime(lo)) / time::kTimeStep;
84 return (1.0 - frac) * op.xs[lo](dim) + frac * op.xs[hi](dim);
87 VectorXf SolverLog::InterpolateControl(
size_t iterate, Time t,
88 PlayerIndex player)
const {
89 const OperatingPoint& op = operating_points_[iterate];
92 const size_t lo = TimeToIndex(t);
93 const size_t hi = std::min(lo + 1, op.xs.size() - 1);
96 const float frac = (t - IndexToTime(lo)) / time::kTimeStep;
97 return (1.0 - frac) * op.us[lo][player] + frac * op.us[hi][player];
100 float SolverLog::InterpolateControl(
size_t iterate, Time t, PlayerIndex player,
101 Dimension dim)
const {
102 const OperatingPoint& op = operating_points_[iterate];
105 const size_t lo = TimeToIndex(t);
106 const size_t hi = std::min(lo + 1, op.xs.size() - 1);
109 const float frac = (t - IndexToTime(lo)) / time::kTimeStep;
110 return (1.0 - frac) * op.us[lo][player](dim) + frac * op.us[hi][player](dim);
113 bool SolverLog::Save(
bool only_last_trajectory,
114 const std::string& experiment_name)
const {
116 const std::string dir_name =
117 std::string(ILQGAMES_LOG_DIR) +
"/" + experiment_name;
118 if (!MakeDirectory(dir_name))
return false;
119 LOG(INFO) <<
"Saving to directory: " << dir_name;
122 if (only_last_trajectory) start = operating_points_.size() - 1;
124 for (
size_t ii = start; ii < operating_points_.size(); ii++) {
125 const auto& op = operating_points_[ii];
126 const std::string sub_dir_name = dir_name +
"/" + std::to_string(ii);
127 if (!MakeDirectory(sub_dir_name))
return false;
131 file.open(sub_dir_name +
"/t0.txt");
132 file << op.t0 << std::endl;
136 file.open(sub_dir_name +
"/xs.txt");
137 for (
const auto& x : op.xs) {
138 file << x.transpose() << std::endl;
143 file.open(sub_dir_name +
"/costs.txt");
144 for (
const auto& c : total_player_costs_[ii]) {
145 file << c << std::endl;
150 file.open(sub_dir_name +
"/cumulative_runtimes.txt");
151 file << cumulative_runtimes_[ii] << std::endl;
155 std::vector<std::ofstream> files(NumPlayers());
156 for (
size_t jj = 0; jj < files.size(); jj++) {
157 files[jj].open(sub_dir_name +
"/u" + std::to_string(jj) +
".txt");
159 for (
size_t kk = 0; kk < op.us.size(); kk++) {
160 CHECK_EQ(files.size(), op.us[kk].size());
161 for (
size_t jj = 0; jj < files.size(); jj++) {
162 files[jj] << op.us[kk][jj].transpose() << std::endl;
165 for (
size_t jj = 0; jj < files.size(); jj++) {
173 inline std::vector<MatrixXf> SolverLog::Ps(
size_t iterate,
174 size_t time_index)
const {
175 std::vector<MatrixXf> Ps(strategies_[iterate].size());
176 for (PlayerIndex ii = 0; ii < Ps.size(); ii++)
177 Ps[ii] = P(iterate, time_index, ii);
181 inline std::vector<VectorXf> SolverLog::alphas(
size_t iterate,
182 size_t time_index)
const {
183 std::vector<VectorXf> alphas(strategies_[iterate].size());
184 for (PlayerIndex ii = 0; ii < alphas.size(); ii++)
185 alphas[ii] = alpha(iterate, time_index, ii);
189 inline MatrixXf SolverLog::P(
size_t iterate,
size_t time_index,
190 PlayerIndex player)
const {
191 return strategies_[iterate][player].Ps[time_index];
194 inline VectorXf SolverLog::alpha(
size_t iterate,
size_t time_index,
195 PlayerIndex player)
const {
196 return strategies_[iterate][player].alphas[time_index];
199 std::string DefaultExperimentName() {
201 std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
202 std::string name = std::string(std::ctime(&date));
203 std::transform(name.begin(), name.end(), name.begin(),
204 [](
char ch) {
return (ch ==
' ' || ch ==
':') ?
'_' : ch; });
205 return std::regex_replace(name, std::regex(
"( |\n)+$"),
"");
208 bool SaveLogs(
const std::vector<SolverLog>& logs,
bool only_last_trajectory,
209 const std::string& experiment_name) {
210 const std::string dir_name =
211 std::string(ILQGAMES_LOG_DIR) +
"/" + experiment_name;
212 if (!MakeDirectory(dir_name))
return false;
214 for (
size_t ii = 0; ii < logs.size(); ii++) {
215 const auto& log = logs[ii];
217 if (!log.Save(only_last_trajectory,
218 experiment_name +
"/" + std::to_string(ii)))
225 bool SaveLogs(
const std::vector<std::shared_ptr<const SolverLog>>& logs,
226 bool only_last_trajectory,
const std::string& experiment_name) {
227 const std::string dir_name =
228 std::string(ILQGAMES_LOG_DIR) +
"/" + experiment_name;
229 if (!MakeDirectory(dir_name))
return false;
231 for (
size_t ii = 0; ii < logs.size(); ii++) {
232 const auto& log = logs[ii];
234 if (!log->Save(only_last_trajectory,
235 experiment_name +
"/" + std::to_string(ii)))