44 #include <ilqgames/cost/cost.h> 45 #include <ilqgames/cost/player_cost.h> 46 #include <ilqgames/utils/operating_point.h> 47 #include <ilqgames/utils/quadratic_cost_approximation.h> 48 #include <ilqgames/utils/types.h> 50 #include <glog/logging.h> 51 #include <unordered_map> 59 template <
typename T,
typename F>
60 void AccumulateControlCostsBase(
const PlayerPtrMultiMap<T>& costs, Time t,
61 const std::vector<VectorXf>& us,
63 QuadraticCostApproximation* q, F f) {
65 for (
const auto& pair : costs) {
66 const PlayerIndex player = pair.first;
67 const auto& cost = pair.second;
70 auto iter = q->control.find(player);
71 if (iter == q->control.end()) {
72 auto inserted_pair = q->control.emplace(
73 player, SingleCostApproximation(us[player].size(), regularization));
77 CHECK(inserted_pair.second);
80 iter = inserted_pair.first;
83 f(*cost, t, us[player], &(iter->second.hess), &(iter->second.grad));
88 void AccumulateControlCosts(
const PlayerPtrMultiMap<Cost>& costs, Time t,
89 const std::vector<VectorXf>& us,
91 QuadraticCostApproximation* q) {
92 auto f = [](
const Cost& cost, Time t,
const VectorXf& u, MatrixXf* hess,
93 VectorXf* grad) { cost.Quadraticize(t, u, hess, grad); };
94 AccumulateControlCostsBase(costs, t, us, regularization, q, f);
97 void AccumulateControlConstraints(
98 const PlayerPtrMultiMap<Constraint>& constraints, Time t,
99 const std::vector<VectorXf>& us,
float regularization,
100 QuadraticCostApproximation* q) {
101 auto f = [](
const Constraint& constraint, Time t,
const VectorXf& u,
103 VectorXf* grad) { constraint.Quadraticize(t, u, hess, grad); };
104 AccumulateControlCostsBase(constraints, t, us, regularization, q, f);
109 void PlayerCost::AddStateCost(
const std::shared_ptr<Cost>& cost) {
110 state_costs_.emplace_back(cost);
113 void PlayerCost::AddControlCost(PlayerIndex idx,
114 const std::shared_ptr<Cost>& cost) {
115 control_costs_.emplace(idx, cost);
118 void PlayerCost::AddStateConstraint(
119 const std::shared_ptr<Constraint>& constraint) {
120 state_constraints_.emplace_back(constraint);
123 void PlayerCost::AddControlConstraint(
124 PlayerIndex idx,
const std::shared_ptr<Constraint>& constraint) {
125 control_constraints_.emplace(idx, constraint);
128 float PlayerCost::Evaluate(Time t,
const VectorXf& x,
129 const std::vector<VectorXf>& us)
const {
130 float total_cost = 0.0;
133 for (
const auto& cost : state_costs_) total_cost += cost->Evaluate(t, x);
136 for (
const auto& pair : control_costs_) {
137 const PlayerIndex& player = pair.first;
138 const auto& cost = pair.second;
140 total_cost += cost->Evaluate(t, us[player]);
146 float PlayerCost::Evaluate(
const OperatingPoint& op, Time time_step)
const {
149 cost = constants::kInfinity;
150 else if (IsMaxOverTime())
151 cost = -constants::kInfinity;
153 for (
size_t kk = 0; kk < op.xs.size(); kk++) {
154 const Time t = op.t0 + time_step *
static_cast<float>(kk);
155 const float instantaneous_cost = Evaluate(t, op.xs[kk], op.us[kk]);
157 if (IsTimeAdditive())
158 cost += instantaneous_cost;
159 else if (IsMinOverTime())
160 cost = std::min(cost, instantaneous_cost);
162 cost = std::max(cost, instantaneous_cost);
168 float PlayerCost::Evaluate(
const OperatingPoint& op)
const {
169 float total_cost = 0.0;
170 for (
size_t kk = 0; kk < op.xs.size(); kk++) total_cost += Evaluate(op, kk);
175 float PlayerCost::EvaluateOffset(Time t, Time next_t,
const VectorXf& next_x,
176 const std::vector<VectorXf>& us)
const {
177 float total_cost = 0.0;
180 for (
const auto& cost : state_costs_)
181 total_cost += cost->Evaluate(next_t, next_x);
184 for (
const auto& pair : control_costs_) {
185 const PlayerIndex& player = pair.first;
186 const auto& cost = pair.second;
188 total_cost += cost->Evaluate(t, us[player]);
194 QuadraticCostApproximation PlayerCost::Quadraticize(
195 Time t,
const VectorXf& x,
const std::vector<VectorXf>& us)
const {
196 QuadraticCostApproximation q(x.size(), state_regularization_);
199 for (
const auto& cost : state_costs_)
200 cost->Quadraticize(t, x, &q.state.hess, &q.state.grad);
203 AccumulateControlCosts(control_costs_, t, us, control_regularization_, &q);
207 for (
const auto& constraint : state_constraints_)
208 constraint->Quadraticize(t, x, &q.state.hess, &q.state.grad);
211 AccumulateControlConstraints(control_constraints_, t, us,
212 control_regularization_, &q);
217 QuadraticCostApproximation PlayerCost::QuadraticizeControlCosts(
218 Time t,
const VectorXf& x,
const std::vector<VectorXf>& us)
const {
219 QuadraticCostApproximation q(x.size(), state_regularization_);
222 AccumulateControlCosts(control_costs_, t, us, control_regularization_, &q);