ilqgames
A new real-time solver for large-scale differential games.
player_cost_cache.cpp
1 /*
2  * Copyright (c) 2019, The Regents of the University of California (Regents).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: David Fridovich-Keil ( dfk@eecs.berkeley.edu )
35  */
36 
37 ///////////////////////////////////////////////////////////////////////////////
38 //
39 // Storage utility for inspecting player costs corresponding to a log.
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 
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>
49 
50 #include <glog/logging.h>
51 #include <memory>
52 #include <unordered_map>
53 #include <vector>
54 
55 namespace ilqgames {
56 
57 PlayerCostCache::PlayerCostCache(const std::shared_ptr<const SolverLog>& log,
58  const std::vector<PlayerCost>& player_costs)
59  : log_(log) {
60  CHECK_NOTNULL(log.get());
61 
62  // Populate costs separately for each player.
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];
67 
68  // Cycle through each separate cost.
69  // Start with state costs.
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)
74  << "Player " << ii
75  << " has duplicate cost with name: " << cost->Name();
76 
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);
81 
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);
85  }
86  }
87  }
88 
89  // Now handle control costs.
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)
96  << "Player " << ii
97  << " has duplicate cost with name: " << cost->Name();
98 
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);
103 
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));
107  }
108  }
109  }
110 
111  // Handle constraints.
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)
116  << "Player " << ii
117  << " has duplicate constraint with name: " << constraint->Name();
118 
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);
123 
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);
127  }
128  }
129  }
130 
131  // Now handle control constraints.
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)
138  << "Player " << ii
139  << " has duplicate constraint with name: " << constraint->Name();
140 
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);
145 
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));
149  }
150  }
151  }
152  }
153 }
154 
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());
159 
160  // Access the approprate time-indexed list of costs.
161  const auto& costs = evaluated_player_costs_[player].at(name)[iterate];
162 
163  // Interpolate this list.
164  const size_t lo = log_->TimeToIndex(t);
165  const size_t hi = std::min(lo + 1, time::kNumTimeSteps - 1);
166 
167  const float frac = (t - log_->IndexToTime(lo)) / time::kTimeStep;
168  return (1.0 - frac) * costs[lo] + frac * costs[hi];
169 }
170 
171 } // namespace ilqgames