-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_sim.rs
285 lines (248 loc) · 8.58 KB
/
example_sim.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#![allow(clippy::unwrap_used, clippy::expect_used)]
//! Taken from official [tutorial](https://mosaik.readthedocs.io/en/3.3.3/tutorials/examplesim.html)
//! This includes the `example_model.py` and the `simulator_mosaik.py` of the Python tutorial.
use clap::Parser;
use log::{error, info, warn};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::{collections::HashMap, sync::LazyLock};
use mosaik_api::{
run_simulation,
tcp::ConnectionDirection,
types::{
Attr, CreateResult, EntityId, InputData, Meta, ModelDescription, OutputData, OutputRequest,
SimulatorType, Time,
},
MosaikApi,
};
static META: LazyLock<Meta> = LazyLock::new(|| {
Meta::new(
SimulatorType::Hybrid,
HashMap::from([(
"ExampleModel".to_string(),
ModelDescription {
public: true,
params: &["init_val"],
attrs: &["delta", "val"],
trigger: Some(&["delta"]),
any_inputs: None,
persistent: None,
},
)]),
Some(vec!["print_something".to_string()]),
)
});
/// Rust implementation of the Python [example_model.py](https://mosaik.readthedocs.io/en/3.3.3/tutorials/examplesim.html#the-model)
#[derive(Debug, Serialize, Deserialize)]
pub struct Model {
val: f64,
delta: f64,
}
impl Model {
pub fn new(init_val: f64) -> Self {
Self {
val: init_val,
delta: 1.0,
}
}
pub fn get_val(&self) -> f64 {
self.val
}
pub fn get_delta(&self) -> f64 {
self.delta
}
pub fn set_delta(&mut self, delta: f64) {
self.delta = delta;
}
pub fn step(&mut self) {
self.val += self.delta;
}
}
/// Rust implementation of the Python [simulator_mosaik.py](https://mosaik.readthedocs.io/en/3.3.3/tutorials/examplesim.html#the-simulator-class)
pub struct ExampleSim {
// init values of the original example
eid_prefix: String,
entities: Map<EntityId, Value>,
time: Time,
// some more values
step_size: u64,
time_resolution: f64,
}
impl Default for ExampleSim {
// This is like the __init__ in the original example
fn default() -> Self {
Self {
eid_prefix: "Model_".to_string(),
step_size: 1,
time: 0,
time_resolution: 1.0,
entities: Map::new(), // Maps EIDs to model instances/entities
}
}
}
impl MosaikApi for ExampleSim {
fn init(
&mut self,
_sid: String,
time_resolution: f64,
sim_params: Map<String, Value>,
) -> Result<&'static Meta, String> {
if (time_resolution - 1.0f64).abs() >= f64::EPSILON {
return Err(format!(
"ExampleSim only supports time_resolution=1., but {time_resolution} was set."
));
}
self.time_resolution = time_resolution;
for (key, value) in sim_params {
match (key.as_str(), value) {
("eid_prefix", Value::String(eid_prefix)) => {
info!("EID prefix is set to: {}", eid_prefix);
self.eid_prefix = eid_prefix;
}
("step_size", Value::Number(step_size)) => {
if let Some(step_size) = step_size.as_u64() {
info!("Step size is set to: {}", step_size);
self.step_size = step_size;
} else {
let e = format!("Step size is not a valid number: {step_size:?}");
error!("Error in default_init: {}", e);
return Err(e);
}
}
_ => {
warn!("Init: Unknown parameter: {}", key);
}
}
}
Ok(&META)
}
fn create(
&mut self,
num: usize,
model_name: String,
model_params: Map<Attr, Value>,
) -> Result<Vec<CreateResult>, String> {
let next_eid = self.entities.len();
let mut result: Vec<CreateResult> = Vec::new();
let init_val = model_params
.get("init_val")
.and_then(serde_json::Value::as_f64)
.unwrap_or(0.0);
for i in next_eid..(next_eid + num) {
let model_instance =
serde_json::to_value(Model::new(init_val)).map_err(|e| e.to_string())?;
let eid = format!("{}{}", self.eid_prefix, i);
self.entities.insert(eid.clone(), model_instance);
let dict = CreateResult::new(eid, model_name.clone());
result.push(dict);
}
// entities must have length of num
Ok(result)
}
fn step(
&mut self,
time: Time,
inputs: InputData,
_max_advance: Time,
) -> Result<Option<Time>, String> {
self.time = time;
// NOTE this code is implemented as on https://mosaik.readthedocs.io/en/latest/tutorials/examplesim.html#step
// but it seems to contain a bug. The delta is overridden by each loop before it's written to the model_instance.
// Check for new delta and do step for each model instance:
for (eid, model_value) in &mut self.entities {
let mut model_instance: Model =
Model::deserialize(model_value.clone()).map_err(|e| e.to_string())?;
if let Some(attrs) = inputs.get(eid) {
let mut new_delta = 0.0;
for value in attrs.values() {
new_delta = value.values().map(|v| v.as_f64().unwrap()).sum();
}
model_instance.delta = new_delta;
}
model_instance.step();
*model_value = serde_json::to_value(&model_instance).map_err(|e| e.to_string())?;
}
Ok(Some(time + 1)) // Step size is 1 second
}
fn get_data(&self, outputs: OutputRequest) -> Result<OutputData, String> {
let mut data: HashMap<EntityId, HashMap<Attr, Value>> = HashMap::new();
for (eid, attrs) in &outputs {
let instance = self.entities.get(eid);
let instance: Option<Model> =
instance.and_then(|i| serde_json::from_value(i.clone()).ok());
if let Some(instance) = instance {
let mut values: HashMap<String, Value> = HashMap::new();
for attr in attrs {
if attr == "val" {
values.insert(attr.clone(), instance.get_val().into());
} else if attr == "delta" {
values.insert(attr.clone(), instance.get_delta().into());
}
}
data.insert(eid.clone(), values);
}
}
Ok(OutputData {
requests: data,
time: None,
})
}
fn setup_done(&self) -> Result<(), String> {
// Nothing to do
Ok(())
}
fn stop(&self) {
// Nothing to do
}
fn extra_method(
&mut self,
method: &str,
args: &Vec<Value>,
kwargs: &Map<String, Value>,
) -> Result<Value, String> {
match method {
"print_something" => {
self.print_something(args, kwargs);
Ok(Value::String("Printed something!".to_string()))
}
_ => Err(format!(
"Method '{method}' not found with args: {args:?} and kwargs: {kwargs:?}"
)),
}
}
}
impl ExampleSim {
pub fn print_something(&self, args: &Vec<Value>, kwargs: &Map<String, Value>) {
println!(
"This is a print statement from an extra method! With args: {args:?} and kwargs: {kwargs:?}",
);
}
}
#[derive(Parser, Debug)]
struct Args {
/// The local address mosaik connects to, or none if we connect to them
#[clap(short, long)]
addr: Option<String>,
}
pub fn main() {
//get the address if there is one
let args = Args::parse();
env_logger::init();
let address = match args.addr {
//case if we connect us to mosaik
Some(addr) => {
ConnectionDirection::ConnectToAddress(addr.parse().expect("Address is not parseable."))
}
//case if mosaik connects to us
None => {
let addr = "127.0.0.1:3456";
ConnectionDirection::ListenOnAddress(addr.parse().expect("Address is not parseable."))
}
};
//initialize the simulator.
let simulator = ExampleSim::default();
//start build_connection in the library.
if let Err(e) = run_simulation(address, simulator) {
panic!("Error running ExampleSim: {e:?}");
}
}