43 #include <ilqgames/cost/player_cost.h> 44 #include <ilqgames/dynamics/multi_player_flat_system.h> 45 #include <ilqgames/utils/operating_point.h> 46 #include <ilqgames/utils/player_cost_cache.h> 47 #include <ilqgames/utils/solver_log.h> 48 #include <ilqgames/utils/types.h> 50 #include <glog/logging.h> 52 #include <unordered_map> 57 PlayerCostCache::PlayerCostCache(
const std::shared_ptr<const SolverLog>& log,
58 const std::vector<PlayerCost>& player_costs)
60 CHECK_NOTNULL(log.get());
63 evaluated_player_costs_.resize(player_costs.size());
64 for (PlayerIndex ii = 0; ii < player_costs.size(); ii++) {
65 const auto& player_cost = player_costs[ii];
66 auto& evaluated_costs = evaluated_player_costs_[ii];
70 for (
const auto& cost : player_cost.StateCosts()) {
71 auto e = evaluated_costs.emplace(cost->Name(),
72 std::vector<std::vector<float>>());
73 LOG_IF(WARNING, !e.second)
75 <<
" has duplicate cost with name: " << cost->Name();
77 auto& entry = e.first->second;
78 entry.resize(log->NumIterates());
79 for (
size_t jj = 0; jj < log->NumIterates(); jj++) {
80 entry[jj].resize(time::kNumTimeSteps);
82 for (
size_t kk = 0; kk < time::kNumTimeSteps; kk++) {
83 const VectorXf x = log->State(jj, kk);
84 entry[jj][kk] = cost->Evaluate(log->IndexToTime(kk), x);
90 for (
const auto& cost_pair : player_cost.ControlCosts()) {
91 const auto other_player = cost_pair.first;
92 const auto& cost = cost_pair.second;
93 auto e = evaluated_costs.emplace(cost->Name(),
94 std::vector<std::vector<float>>());
95 LOG_IF(WARNING, !e.second)
97 <<
" has duplicate cost with name: " << cost->Name();
99 auto& entry = e.first->second;
100 entry.resize(log->NumIterates());
101 for (
size_t jj = 0; jj < log->NumIterates(); jj++) {
102 entry[jj].resize(time::kNumTimeSteps);
104 for (
size_t kk = 0; kk < time::kNumTimeSteps; kk++) {
105 entry[jj][kk] = cost->Evaluate(log->IndexToTime(kk),
106 log->Control(jj, kk, other_player));
112 for (
const auto& constraint : player_cost.StateConstraints()) {
113 auto e = evaluated_costs.emplace(constraint->Name(),
114 std::vector<std::vector<float>>());
115 LOG_IF(WARNING, !e.second)
117 <<
" has duplicate constraint with name: " << constraint->Name();
119 auto& entry = e.first->second;
120 entry.resize(log->NumIterates());
121 for (
size_t jj = 0; jj < log->NumIterates(); jj++) {
122 entry[jj].resize(time::kNumTimeSteps);
124 for (
size_t kk = 0; kk < time::kNumTimeSteps; kk++) {
125 const VectorXf x = log->State(jj, kk);
126 entry[jj][kk] = constraint->Evaluate(log->IndexToTime(kk), x);
132 for (
const auto& constraint_pair : player_cost.ControlConstraints()) {
133 const auto other_player = constraint_pair.first;
134 const auto& constraint = constraint_pair.second;
135 auto e = evaluated_costs.emplace(constraint->Name(),
136 std::vector<std::vector<float>>());
137 LOG_IF(WARNING, !e.second)
139 <<
" has duplicate constraint with name: " << constraint->Name();
141 auto& entry = e.first->second;
142 entry.resize(log->NumIterates());
143 for (
size_t jj = 0; jj < log->NumIterates(); jj++) {
144 entry[jj].resize(time::kNumTimeSteps);
146 for (
size_t kk = 0; kk < time::kNumTimeSteps; kk++) {
147 entry[jj][kk] = constraint->Evaluate(
148 log->IndexToTime(kk), log->Control(jj, kk, other_player));
155 float PlayerCostCache::Interpolate(
size_t iterate, Time t, PlayerIndex player,
156 const std::string& name)
const {
157 CHECK_LT(iterate, log_->NumIterates());
158 CHECK_LT(player, evaluated_player_costs_.size());
161 const auto& costs = evaluated_player_costs_[player].at(name)[iterate];
164 const size_t lo = log_->TimeToIndex(t);
165 const size_t hi = std::min(lo + 1, time::kNumTimeSteps - 1);
167 const float frac = (t - log_->IndexToTime(lo)) / time::kTimeStep;
168 return (1.0 - frac) * costs[lo] + frac * costs[hi];