behaviorsim-rs 0.7.0

Domain-agnostic specification for modeling individual psychology and social dynamics
Documentation
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Entity model configuration.
//!
//! This module defines entity-type-specific configuration. Different entity types
//! (Human, Animal) have different feature requirements.

use crate::enums::Species;
use serde::{Deserialize, Serialize};

/// Configuration for an entity model.
///
/// This enables or disables features based on entity type complexity.
///
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EntityModelConfig {
    /// Whether personality modeling is enabled.
    personality_enabled: bool,

    /// Whether mental health (ITS) tracking is enabled.
    mental_health_enabled: bool,

    /// Time scale for psychological processing.
    /// 1.0 = human baseline, higher = faster subjective time.
    time_scale: f32,

    /// Minimum interaction frequency required for proximal process effects.
    /// Effects are blocked when frequency is below this threshold.
    /// Default: 0.3
    proximal_process_frequency_threshold: f64,

    /// Minimum interaction complexity required for proximal process effects.
    /// Effects are blocked when complexity is below this threshold.
    /// Default: 0.3
    proximal_process_complexity_threshold: f64,
}

/// Default proximal process frequency threshold.
pub const DEFAULT_PROXIMAL_FREQUENCY_THRESHOLD: f64 = 0.3;

/// Default proximal process complexity threshold.
pub const DEFAULT_PROXIMAL_COMPLEXITY_THRESHOLD: f64 = 0.3;

impl EntityModelConfig {
    /// Creates a new EntityModelConfig with defaults.
    ///
    /// Use builder methods to customize.
    ///
    #[must_use]
    pub fn new() -> Self {
        EntityModelConfig {
            personality_enabled: false,
            mental_health_enabled: false,
            time_scale: 1.0,
            proximal_process_frequency_threshold: DEFAULT_PROXIMAL_FREQUENCY_THRESHOLD,
            proximal_process_complexity_threshold: DEFAULT_PROXIMAL_COMPLEXITY_THRESHOLD,
        }
    }

    /// Creates a configuration appropriate for a human entity.
    ///
    /// Personality and mental health are enabled.
    ///
    #[must_use]
    pub fn human_default() -> Self {
        EntityModelConfig {
            personality_enabled: true,
            mental_health_enabled: true,
            time_scale: 1.0,
            proximal_process_frequency_threshold: DEFAULT_PROXIMAL_FREQUENCY_THRESHOLD,
            proximal_process_complexity_threshold: DEFAULT_PROXIMAL_COMPLEXITY_THRESHOLD,
        }
    }

    /// Creates a configuration appropriate for the given species.
    ///
    /// # Arguments
    ///
    /// * `species` - The species to create configuration for
    ///
    #[must_use]
    pub fn for_species(species: &Species) -> Self {
        match species {
            Species::Human => EntityModelConfig::human_default(),
            // All non-human species use the animal simple config
            Species::Dog
            | Species::Cat
            | Species::Dolphin
            | Species::Horse
            | Species::Elephant
            | Species::Chimpanzee
            | Species::Crow
            | Species::Mouse
            | Species::Custom { .. } => EntityModelConfig::animal_simple(),
        }
    }

    /// Creates a configuration appropriate for a simple animal entity.
    ///
    /// Mental health tracking is disabled.
    ///
    #[must_use]
    pub fn animal_simple() -> Self {
        EntityModelConfig {
            personality_enabled: true,
            mental_health_enabled: false,
            time_scale: 1.0,
            proximal_process_frequency_threshold: DEFAULT_PROXIMAL_FREQUENCY_THRESHOLD,
            proximal_process_complexity_threshold: DEFAULT_PROXIMAL_COMPLEXITY_THRESHOLD,
        }
    }

    /// Creates a configuration appropriate for a high-complexity animal.
    ///
    /// Similar to human but with mental health disabled.
    #[must_use]
    pub fn animal_complex() -> Self {
        EntityModelConfig {
            personality_enabled: true,
            mental_health_enabled: false,
            time_scale: 1.0,
            proximal_process_frequency_threshold: DEFAULT_PROXIMAL_FREQUENCY_THRESHOLD,
            proximal_process_complexity_threshold: DEFAULT_PROXIMAL_COMPLEXITY_THRESHOLD,
        }
    }

    // Builder methods

    /// Enables or disables personality modeling.
    #[must_use]
    pub fn with_personality_enabled(mut self, enabled: bool) -> Self {
        self.personality_enabled = enabled;
        self
    }

    /// Enables or disables mental health tracking.
    #[must_use]
    pub fn with_mental_health_enabled(mut self, enabled: bool) -> Self {
        self.mental_health_enabled = enabled;
        self
    }

    /// Sets the time scale for psychological processing.
    #[must_use]
    pub fn with_time_scale(mut self, scale: f32) -> Self {
        self.time_scale = scale.max(0.01); // Minimum 0.01 to avoid division by zero
        self
    }

    /// Sets the proximal process frequency threshold.
    #[must_use]
    pub fn with_proximal_frequency_threshold(mut self, threshold: f64) -> Self {
        self.proximal_process_frequency_threshold = threshold.clamp(0.0, 1.0);
        self
    }

    /// Sets the proximal process complexity threshold.
    #[must_use]
    pub fn with_proximal_complexity_threshold(mut self, threshold: f64) -> Self {
        self.proximal_process_complexity_threshold = threshold.clamp(0.0, 1.0);
        self
    }

    // Accessors

    /// Returns true if personality modeling is enabled.
    #[must_use]
    pub fn personality_enabled(&self) -> bool {
        self.personality_enabled
    }

    /// Returns true if mental health (ITS) tracking is enabled.
    #[must_use]
    pub fn mental_health_enabled(&self) -> bool {
        self.mental_health_enabled
    }

    /// Returns the time scale for psychological processing.
    ///
    /// Higher values mean faster subjective time (more psychological
    /// change per unit of real time).
    #[must_use]
    pub fn time_scale(&self) -> f32 {
        self.time_scale
    }

    /// Returns the proximal process frequency threshold.
    ///
    /// Context effects are blocked when interaction frequency is below this.
    #[must_use]
    pub fn proximal_frequency_threshold(&self) -> f64 {
        self.proximal_process_frequency_threshold
    }

    /// Returns the proximal process complexity threshold.
    ///
    /// Context effects are blocked when interaction complexity is below this.
    #[must_use]
    pub fn proximal_complexity_threshold(&self) -> f64 {
        self.proximal_process_complexity_threshold
    }

    /// Checks whether proximal process criteria are met.
    ///
    /// Returns true if both frequency and complexity meet or exceed thresholds.
    #[must_use]
    pub fn check_proximal_process_gate(&self, frequency: f64, complexity: f64) -> bool {
        frequency >= self.proximal_process_frequency_threshold
            && complexity >= self.proximal_process_complexity_threshold
    }

    // Mutators

    /// Sets whether personality is enabled.
    pub fn set_personality_enabled(&mut self, enabled: bool) {
        self.personality_enabled = enabled;
    }

    /// Sets whether mental health is enabled.
    pub fn set_mental_health_enabled(&mut self, enabled: bool) {
        self.mental_health_enabled = enabled;
    }

    /// Sets the time scale.
    pub fn set_time_scale(&mut self, scale: f32) {
        self.time_scale = scale.max(0.01);
    }

    /// Sets the proximal process frequency threshold.
    pub fn set_proximal_frequency_threshold(&mut self, threshold: f64) {
        self.proximal_process_frequency_threshold = threshold.clamp(0.0, 1.0);
    }

    /// Sets the proximal process complexity threshold.
    pub fn set_proximal_complexity_threshold(&mut self, threshold: f64) {
        self.proximal_process_complexity_threshold = threshold.clamp(0.0, 1.0);
    }
}

impl Default for EntityModelConfig {
    fn default() -> Self {
        EntityModelConfig::human_default()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_creates_default_config() {
        let config = EntityModelConfig::new();
        assert!(!config.personality_enabled());
        assert!(!config.mental_health_enabled());
    }

    #[test]
    fn human_default_has_personality_and_mental_health() {
        let config = EntityModelConfig::human_default();
        assert!(config.personality_enabled());
        assert!(config.mental_health_enabled());
    }

    #[test]
    fn human_default_has_time_scale_one() {
        let config = EntityModelConfig::human_default();
        assert!((config.time_scale() - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn animal_simple_lacks_mental_health() {
        let config = EntityModelConfig::animal_simple();
        assert!(!config.mental_health_enabled());
        assert!(config.personality_enabled());
    }

    #[test]
    fn animal_complex_lacks_mental_health() {
        let config = EntityModelConfig::animal_complex();
        assert!(!config.mental_health_enabled());
    }

    #[test]
    fn with_personality_enabled_works() {
        let config = EntityModelConfig::new().with_personality_enabled(true);
        assert!(config.personality_enabled());

        let config2 = EntityModelConfig::human_default().with_personality_enabled(false);
        assert!(!config2.personality_enabled());
    }

    #[test]
    fn with_mental_health_enabled_works() {
        let config = EntityModelConfig::new().with_mental_health_enabled(true);
        assert!(config.mental_health_enabled());
    }

    #[test]
    fn with_time_scale_works() {
        let config = EntityModelConfig::new().with_time_scale(6.7);
        assert!((config.time_scale() - 6.7).abs() < f32::EPSILON);
    }

    #[test]
    fn time_scale_has_minimum() {
        let config = EntityModelConfig::new().with_time_scale(-5.0);
        assert!(config.time_scale() >= 0.01);
    }

    #[test]
    fn mutators_work() {
        let mut config = EntityModelConfig::new();

        config.set_personality_enabled(true);
        assert!(config.personality_enabled());

        config.set_mental_health_enabled(true);
        assert!(config.mental_health_enabled());

        config.set_time_scale(2.0);
        assert!((config.time_scale() - 2.0).abs() < f32::EPSILON);
    }

    #[test]
    fn default_is_human() {
        let config = EntityModelConfig::default();
        assert!(config.mental_health_enabled());
    }

    #[test]
    fn clone_and_equality() {
        let config1 = EntityModelConfig::human_default();
        let config2 = config1.clone();
        assert_eq!(config1, config2);
    }

    #[test]
    fn debug_format() {
        let config = EntityModelConfig::new();
        let debug = format!("{:?}", config);
        assert!(debug.contains("EntityModelConfig"));
    }

    // --- Proximal process threshold tests ---

    #[test]
    fn default_proximal_process_thresholds() {
        let config = EntityModelConfig::human_default();
        assert!((config.proximal_frequency_threshold() - 0.3).abs() < f64::EPSILON);
        assert!((config.proximal_complexity_threshold() - 0.3).abs() < f64::EPSILON);
    }

    #[test]
    fn with_proximal_frequency_threshold() {
        let config = EntityModelConfig::new().with_proximal_frequency_threshold(0.5);
        assert!((config.proximal_frequency_threshold() - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn with_proximal_complexity_threshold() {
        let config = EntityModelConfig::new().with_proximal_complexity_threshold(0.6);
        assert!((config.proximal_complexity_threshold() - 0.6).abs() < f64::EPSILON);
    }

    #[test]
    fn proximal_thresholds_clamped() {
        let config = EntityModelConfig::new()
            .with_proximal_frequency_threshold(1.5)
            .with_proximal_complexity_threshold(-0.5);

        assert!((config.proximal_frequency_threshold() - 1.0).abs() < f64::EPSILON);
        assert!((config.proximal_complexity_threshold() - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn check_proximal_process_gate_both_pass() {
        let config = EntityModelConfig::new()
            .with_proximal_frequency_threshold(0.3)
            .with_proximal_complexity_threshold(0.3);

        assert!(config.check_proximal_process_gate(0.5, 0.5));
        assert!(config.check_proximal_process_gate(0.3, 0.3)); // Exactly at threshold
    }

    #[test]
    fn check_proximal_process_gate_frequency_fails() {
        let config = EntityModelConfig::new()
            .with_proximal_frequency_threshold(0.3)
            .with_proximal_complexity_threshold(0.3);

        assert!(!config.check_proximal_process_gate(0.2, 0.5));
    }

    #[test]
    fn check_proximal_process_gate_complexity_fails() {
        let config = EntityModelConfig::new()
            .with_proximal_frequency_threshold(0.3)
            .with_proximal_complexity_threshold(0.3);

        assert!(!config.check_proximal_process_gate(0.5, 0.2));
    }

    #[test]
    fn check_proximal_process_gate_both_fail() {
        let config = EntityModelConfig::new()
            .with_proximal_frequency_threshold(0.3)
            .with_proximal_complexity_threshold(0.3);

        assert!(!config.check_proximal_process_gate(0.1, 0.1));
    }

    #[test]
    fn set_proximal_thresholds() {
        let mut config = EntityModelConfig::new();

        config.set_proximal_frequency_threshold(0.4);
        config.set_proximal_complexity_threshold(0.5);

        assert!((config.proximal_frequency_threshold() - 0.4).abs() < f64::EPSILON);
        assert!((config.proximal_complexity_threshold() - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn set_proximal_thresholds_clamped() {
        let mut config = EntityModelConfig::new();

        config.set_proximal_frequency_threshold(2.0);
        config.set_proximal_complexity_threshold(-1.0);

        assert!((config.proximal_frequency_threshold() - 1.0).abs() < f64::EPSILON);
        assert!((config.proximal_complexity_threshold() - 0.0).abs() < f64::EPSILON);
    }

    // --- for_species tests ---

    #[test]
    fn for_species_human_returns_human_default() {
        let config = EntityModelConfig::for_species(&Species::Human);
        assert!(config.mental_health_enabled());
    }

    #[test]
    fn for_species_dog_returns_animal_simple() {
        let config = EntityModelConfig::for_species(&Species::Dog);
        assert!(!config.mental_health_enabled());
    }

    #[test]
    fn for_species_cat_returns_animal_simple() {
        let config = EntityModelConfig::for_species(&Species::Cat);
        assert!(!config.mental_health_enabled());
    }

    #[test]
    fn for_species_dolphin_returns_animal_simple() {
        let config = EntityModelConfig::for_species(&Species::Dolphin);
        assert!(!config.mental_health_enabled());
    }

    #[test]
    fn for_species_chimpanzee_returns_animal_simple() {
        let config = EntityModelConfig::for_species(&Species::Chimpanzee);
        assert!(!config.mental_health_enabled());
    }

    #[test]
    fn for_species_mouse_returns_animal_simple() {
        let config = EntityModelConfig::for_species(&Species::Mouse);
        assert!(!config.mental_health_enabled());
    }

    #[test]
    fn for_species_custom_returns_animal_simple() {
        let custom = Species::custom("Parrot", 60, 5, 0.6);
        let config = EntityModelConfig::for_species(&custom);
        assert!(!config.mental_health_enabled());
    }
}