ilqgames
A new real-time solver for large-scale differential games.
player_cost.h
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 // Container to store all the cost functions for a single player, and keep track
40 // of which variables (x, u1, u2, ..., uN) they correspond to.
41 //
42 ///////////////////////////////////////////////////////////////////////////////
43 
44 #ifndef ILQGAMES_COST_PLAYER_COST_H
45 #define ILQGAMES_COST_PLAYER_COST_H
46 
47 #include <ilqgames/constraint/constraint.h>
48 #include <ilqgames/cost/cost.h>
49 #include <ilqgames/utils/operating_point.h>
50 #include <ilqgames/utils/quadratic_cost_approximation.h>
51 #include <ilqgames/utils/types.h>
52 
53 #include <unordered_map>
54 
55 namespace ilqgames {
56 
57 class PlayerCost {
58  public:
59  ~PlayerCost() {}
60 
61  // Provide default values for all constructor values. If num_time_steps is
62  // positive use that to initialize the lambdas.
63  explicit PlayerCost(const std::string& name = "",
64  float state_regularization = 0.0,
65  float control_regularization = 0.0)
66  : name_(name),
67  state_regularization_(state_regularization),
68  control_regularization_(control_regularization),
69  cost_structure_(CostStructure::SUM),
70  time_of_extreme_cost_(0) {}
71 
72  // Add new state and control costs for this player.
73  void AddStateCost(const std::shared_ptr<Cost>& cost);
74  void AddControlCost(PlayerIndex idx, const std::shared_ptr<Cost>& cost);
75 
76  // Add new state and control constraints. For now, they are only equality
77  // constraints but later they should really be inequality constraints and
78  // there should be some logic for maintaining sets of active constraints.
79  void AddStateConstraint(const std::shared_ptr<Constraint>& constraint);
80  void AddControlConstraint(PlayerIndex idx,
81  const std::shared_ptr<Constraint>& constraint);
82 
83  // Evaluate this cost at the current time, state, and controls, or
84  // integrate over an entire trajectory. The "Offset" here indicates that
85  // state costs will be evaluated at the next time step.
86  float Evaluate(Time t, const VectorXf& x,
87  const std::vector<VectorXf>& us) const;
88  float Evaluate(const OperatingPoint& op, Time time_step) const;
89  float Evaluate(const OperatingPoint& op) const;
90  float EvaluateOffset(Time t, Time next_t, const VectorXf& next_x,
91  const std::vector<VectorXf>& us) const;
92 
93  // Quadraticize this cost at the given time, time step, state, and controls.
94  QuadraticCostApproximation Quadraticize(
95  Time t, const VectorXf& x, const std::vector<VectorXf>& us) const;
96 
97  // Return empty cost quadraticization except for control costs.
98  QuadraticCostApproximation QuadraticizeControlCosts(
99  Time t, const VectorXf& x, const std::vector<VectorXf>& us) const;
100 
101  // Set whether this is a time-additive, max-over-time, or min-over-time cost.
102  // At each specific time, all costs are accumulated with the given operation.
103  enum CostStructure { SUM, MAX, MIN };
104  void SetTimeAdditive() { cost_structure_ = SUM; }
105  void SetMaxOverTime() { cost_structure_ = MAX; }
106  void SetMinOverTime() { cost_structure_ = MIN; }
107  bool IsTimeAdditive() const { return cost_structure_ == SUM; }
108  bool IsMaxOverTime() const { return cost_structure_ == MAX; }
109  bool IsMinOverTime() const { return cost_structure_ == MIN; }
110 
111  // Keep track of the time of extreme costs.
112  size_t TimeOfExtremeCost() { return time_of_extreme_cost_; }
113  void SetTimeOfExtremeCost(size_t kk) { time_of_extreme_cost_ = kk; }
114 
115  // Accessors.
116  const PtrVector<Cost>& StateCosts() const { return state_costs_; }
117  const PlayerPtrMultiMap<Cost>& ControlCosts() const { return control_costs_; }
118  const PtrVector<Constraint>& StateConstraints() const {
119  return state_constraints_;
120  }
121  const PlayerPtrMultiMap<Constraint>& ControlConstraints() const {
122  return control_constraints_;
123  }
124  bool IsConstrained() const {
125  return !state_constraints_.empty() || !control_constraints_.empty();
126  }
127 
128  private:
129  // Name to be used with error msgs.
130  const std::string name_;
131 
132  // State costs and control costs.
133  PtrVector<Cost> state_costs_;
134  PlayerPtrMultiMap<Cost> control_costs_;
135 
136  // State and control constraints
137  PtrVector<Constraint> state_constraints_;
138  PlayerPtrMultiMap<Constraint> control_constraints_;
139 
140  // Regularization on costs.
141  const float state_regularization_;
142  const float control_regularization_;
143 
144  // Ternary variable whether this objective is time-additive, max-over-time, or
145  // min-over-time.
146  CostStructure cost_structure_;
147 
148  // Keep track of the time of extreme costs. This will depend upon the current
149  // operating point, and it will only be meaningful if the cost structure is an
150  // extremum over time.
151  size_t time_of_extreme_cost_;
152 }; //\class PlayerCost
153 
154 } // namespace ilqgames
155 
156 #endif