|
| 1 | +# Learned Model |
| 2 | + |
| 3 | +This is the design document for the Python learned model used in the `simple_planning_simulator` package. |
| 4 | + |
| 5 | +## Purpose / Use cases |
| 6 | + |
| 7 | +<!-- Required --> |
| 8 | +<!-- Things to consider: |
| 9 | + - Why did we implement this feature? --> |
| 10 | + |
| 11 | +This library creates an interface between models in Python and PSIM (C++). It is used to quickly deploy learned Python models in PSIM without a need for complex C++ implementation. |
| 12 | + |
| 13 | +## Design |
| 14 | + |
| 15 | +<!-- Required --> |
| 16 | +<!-- Things to consider: |
| 17 | + - How does it work? --> |
| 18 | + |
| 19 | +The idea behind this package is that the model we want to use for simulation consists of multiple sub-models (e.g., steering model, drive model, vehicle kinematics, etc.). These sub-models are implemented in Python and can be trainable. Each sub-model has string names for all of its inputs/outputs, which are used to create model interconnections automatically (see image below). This allows us to easily switch sub-models for better customization of the simulator. |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +## Assumptions / Known limits |
| 24 | + |
| 25 | +<!-- Required --> |
| 26 | + |
| 27 | +To use this package `python3` and `pybind11` need to be installed. The only assumption on Python sub-models is their interface. |
| 28 | + |
| 29 | +```python |
| 30 | +class PythonSubmodelInterface: |
| 31 | + |
| 32 | + def forward(self, action, state): # Required |
| 33 | + """ |
| 34 | + Calculate forward pass through the model and returns next_state. |
| 35 | + """ |
| 36 | + return list() |
| 37 | + |
| 38 | + def get_state_names(self): # Required |
| 39 | + """ |
| 40 | + Return list of string names of the model states (outputs). |
| 41 | + """ |
| 42 | + return list() |
| 43 | + |
| 44 | + def get_action_names(self): # Required |
| 45 | + """ |
| 46 | + Return list of string names of the model actions (inputs). |
| 47 | + """ |
| 48 | + return list() |
| 49 | + |
| 50 | + def reset(self): # Required |
| 51 | + """ |
| 52 | + Reset model. This function is called after load_params(). |
| 53 | + """ |
| 54 | + pass |
| 55 | + |
| 56 | + def load_params(self, path): # Required |
| 57 | + """ |
| 58 | + Load parameters of the model. |
| 59 | + Inputs: |
| 60 | + - path: Path to a parameter file to load by the model. |
| 61 | + """ |
| 62 | + pass |
| 63 | + |
| 64 | + def dtSet(self, dt): # Required |
| 65 | + """ |
| 66 | + Set dt of the model. |
| 67 | + Inputs: |
| 68 | + - dt: time step |
| 69 | + """ |
| 70 | + pass |
| 71 | +``` |
| 72 | + |
| 73 | +## API |
| 74 | + |
| 75 | +<!-- Required --> |
| 76 | +<!-- Things to consider: |
| 77 | + - How do you use the package / API? --> |
| 78 | + |
| 79 | +To successfully create a vehicle model an InterconnectedModel class needs to be set up correctly. |
| 80 | + |
| 81 | +### InterconnectedModel class |
| 82 | + |
| 83 | +#### `Constructor` |
| 84 | + |
| 85 | +The constructor takes no arguments. |
| 86 | + |
| 87 | +#### `void addSubmodel(std::tuple<std::string, std::string, std::string> model_descriptor)` |
| 88 | + |
| 89 | +Add a new sub-model to the model. |
| 90 | + |
| 91 | +Inputs: |
| 92 | + |
| 93 | +- model_descriptor: Describes what model should be used. The model descriptor contains three strings: |
| 94 | + - The first string is a path to a python module where the model is implemented. |
| 95 | + - The second string is a path to the file where model parameters are stored. |
| 96 | + - The third string is the name of the class that implements the model. |
| 97 | + |
| 98 | +Outputs: |
| 99 | + |
| 100 | +- None |
| 101 | + |
| 102 | +#### `void generateConnections(std::vector<char *> in_names, std::vector<char*> out_names)` |
| 103 | + |
| 104 | +Generate connections between sub-models and inputs/outputs of the model. |
| 105 | + |
| 106 | +Inputs: |
| 107 | + |
| 108 | +- in_names: String names for all of the model inputs in order. |
| 109 | +- out_names: String names for all of the model outputs in order. |
| 110 | + |
| 111 | +Outputs: |
| 112 | + |
| 113 | +- None |
| 114 | + |
| 115 | +#### `void initState(std::vector<double> new_state)` |
| 116 | + |
| 117 | +Set the initial state of the model. |
| 118 | + |
| 119 | +Inputs: |
| 120 | + |
| 121 | +- new_state: New state of the model. |
| 122 | + |
| 123 | +Outputs: |
| 124 | + |
| 125 | +- None |
| 126 | + |
| 127 | +#### `std::vector<double> updatePyModel(std::vector<double> psim_input)` |
| 128 | + |
| 129 | +Calculate the next state of the model by calculating the next state of all of the sub-models. |
| 130 | + |
| 131 | +Inputs: |
| 132 | + |
| 133 | +- psim_input: Input to the model. |
| 134 | + |
| 135 | +Outputs: |
| 136 | + |
| 137 | +- next_state: Next state of the model. |
| 138 | + |
| 139 | +#### `dtSet(double dt)` |
| 140 | + |
| 141 | +Set the time step of the model. |
| 142 | + |
| 143 | +Inputs: |
| 144 | + |
| 145 | +- dt: time step |
| 146 | + |
| 147 | +Outputs: |
| 148 | + |
| 149 | +- None |
| 150 | + |
| 151 | +### Example |
| 152 | + |
| 153 | +Firstly we need to set up the model. |
| 154 | + |
| 155 | +```C++ |
| 156 | +InterconnectedModel vehicle; |
| 157 | + |
| 158 | +// Example of model descriptors |
| 159 | +std::tuple<char*, char*, char*> model_descriptor_1 = { |
| 160 | + (char*)"path_to_python_module_with_model_class_1", |
| 161 | + (char*)nullptr, // If no param file is needed you can pass 'nullptr' |
| 162 | + (char*)"ModelClass1" |
| 163 | + }; |
| 164 | + |
| 165 | +std::tuple<char*, char*, char*> model_descriptor_2 = { |
| 166 | + (char*)"path_to_python_module_with_model_class_2", |
| 167 | + (char*)"/path_to/param_file", |
| 168 | + (char*)"ModelClass2" // Name of the python class. Needs to use the interface from 'Assumptions' |
| 169 | + }; |
| 170 | + |
| 171 | +// Create sub-models based on descriptors |
| 172 | +vehicle.addSubmodel(model_descriptor_1); |
| 173 | +vehicle.addSubmodel(model_descriptor_2); |
| 174 | + |
| 175 | +// Define STATE and INPUT names of the system |
| 176 | +std::vector<char*> state_names = {(char*)"STATE_NAME_1", (char*)"STATE_NAME_2"}; |
| 177 | +std::vector<char*> input_names = {(char*)"INPUT_NAME_1", (char*)"INPUT_NAME_2"}; |
| 178 | + |
| 179 | +// Automatically connect sub-systems with model input |
| 180 | +vehicle.generateConnections(input_names, state_names); |
| 181 | + |
| 182 | +// Set the time step of the model |
| 183 | +vehicle.dtSet(dt); |
| 184 | +``` |
| 185 | +
|
| 186 | +After the model is correctly set up, we can use it the following way. |
| 187 | +
|
| 188 | +```C++ |
| 189 | +// Example of an model input |
| 190 | +std::vector<double> vehicle_input = {0.0, 1.0}; // INPUT_NAME_1, INPUT_NAME_2 |
| 191 | +
|
| 192 | +// Example of an model state |
| 193 | +std::vector<double> current_state = {0.2, 0.5}; // STATE_NAME_1, STATE_NAME_2 |
| 194 | +
|
| 195 | +// Set model state |
| 196 | +vehicle.initState(current_state); |
| 197 | +
|
| 198 | +// Calculate the next state of the model |
| 199 | +std::vector<double> next_state = vehicle.updatePyModel(vehicle_input); |
| 200 | +``` |
| 201 | + |
| 202 | +## References / External links |
| 203 | + |
| 204 | +<!-- Optional --> |
| 205 | + |
| 206 | +## Related issues |
| 207 | + |
| 208 | +<!-- Required --> |
0 commit comments