ilqgames
A new real-time solver for large-scale differential games.
control_sliders.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 // Static variables shared by all GUI windows.
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 #ifndef ILQGAMES_GUI_CONTROL_SLIDERS_H
44 #define ILQGAMES_GUI_CONTROL_SLIDERS_H
45 
46 #include <ilqgames/utils/solver_log.h>
47 
48 #include <memory>
49 #include <vector>
50 
51 namespace ilqgames {
52 
54  public:
55  ~ControlSliders() {}
56  ControlSliders(const std::vector<
57  std::vector<std::shared_ptr<const ilqgames::SolverLog>>>&
58  logs_for_each_problem)
59  : interpolation_time_(0.0),
60  solver_iterate_(0),
61  log_index_(0),
62  max_log_index_(0),
63  logs_for_each_problem_(logs_for_each_problem) {
64  for (const auto& logs : logs_for_each_problem_) {
65  for (const auto& log : logs) CHECK_NOTNULL(log.get());
66  }
67 
68  // Compute max log index.
69  for (const auto& logs : logs_for_each_problem_) {
70  if (logs.size() > static_cast<size_t>(max_log_index_) + 1)
71  max_log_index_ = logs.size() - 1;
72  }
73  }
74 
75  // Render all the sliders in a separate window.
76  void Render();
77 
78  // Accessors.
79  size_t NumProblems() const { return logs_for_each_problem_.size(); }
80  const std::vector<std::vector<std::shared_ptr<const SolverLog>>>&
81  LogsForEachProblem() const {
82  return logs_for_each_problem_;
83  }
84  std::vector<std::shared_ptr<const SolverLog>> LogForEachProblem() const {
85  std::vector<std::shared_ptr<const SolverLog>> logs(NumProblems());
86  for (size_t ii = 0; ii < NumProblems(); ii++)
87  logs[ii] = logs_for_each_problem_[ii][LogIndex(ii)];
88  return logs;
89  }
90 
91  Time InterpolationTime(size_t problem_idx) const {
92  CHECK_LT(problem_idx, NumProblems());
93 
94  const auto& logs = logs_for_each_problem_[problem_idx];
95  const int log_idx = LogIndex(problem_idx);
96  return std::max(std::min(static_cast<Time>(interpolation_time_),
97  logs[log_idx]->FinalTime()),
98  logs[log_idx]->InitialTime());
99  }
100  int SolverIterate(size_t problem_idx) const {
101  CHECK_LT(problem_idx, NumProblems());
102 
103  const auto& logs = logs_for_each_problem_[problem_idx];
104  const int log_idx = LogIndex(problem_idx);
105  return std::min(solver_iterate_,
106  static_cast<int>(logs[log_idx]->NumIterates() - 1));
107  }
108  int LogIndex(size_t problem_idx) const {
109  CHECK_LT(problem_idx, NumProblems());
110 
111  const auto& logs = logs_for_each_problem_[problem_idx];
112  return std::min(log_index_, static_cast<int>(logs.size() - 1));
113  }
114  int MaxLogIndex() const { return max_log_index_; }
115 
116  private:
117  // Time at which to interpolate each trajectory.
118  float interpolation_time_;
119 
120  // Solver iterate to display.
121  int solver_iterate_;
122 
123  // Log index to render for receding horizon problems.
124  int log_index_;
125 
126  // Keep track of the max number of log indices across all problems.
127  int max_log_index_;
128 
129  // List of all logs we might want to inspect, indexed by problem, then by
130  // receding horizon invocation.
131  const std::vector<std::vector<std::shared_ptr<const ilqgames::SolverLog>>>
132  logs_for_each_problem_;
133 }; // class ControlSliders
134 
135 } // namespace ilqgames
136 
137 #endif