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
//! HEXACO personality model implementation.
//!
//! The HEXACO model is a six-factor model of human personality:
//! - Honesty-Humility (H)
//! - Emotionality (E) - similar to Neuroticism in Big Five
//! - eXtraversion (X)
//! - Agreeableness (A)
//! - Conscientiousness (C)
//! - Openness to Experience (O)
//!
//! Each factor ranges from -1.0 to 1.0, where:
//! - Negative values indicate low trait presence
//! - Zero indicates average trait level
//! - Positive values indicate high trait presence

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

/// HEXACO personality factors.
///
/// All factors are stored as f32 values in the range -1.0 to 1.0.
/// These represent stable personality traits that rarely change
/// after early adulthood.
///
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Hexaco {
    /// Openness to experience: curiosity, creativity, preference for novelty.
    /// Range: -1.0 (closed) to 1.0 (open)
    openness: f32,

    /// Conscientiousness: organization, dependability, self-discipline.
    /// Range: -1.0 (disorganized) to 1.0 (conscientious)
    conscientiousness: f32,

    /// Extraversion: sociability, assertiveness, positive emotionality.
    /// Range: -1.0 (introverted) to 1.0 (extraverted)
    extraversion: f32,

    /// Agreeableness: cooperation, trust, compliance.
    /// Range: -1.0 (antagonistic) to 1.0 (agreeable)
    agreeableness: f32,

    /// Neuroticism (Emotionality): emotional instability, anxiety, moodiness.
    /// Range: -1.0 (stable) to 1.0 (neurotic)
    neuroticism: f32,

    /// Honesty-Humility: sincerity, fairness, lack of greed.
    /// Range: -1.0 (manipulative) to 1.0 (honest/humble)
    honesty_humility: f32,
}

impl Hexaco {
    /// Creates a new Hexaco with all factors at the neutral point (0.0).
    ///
    #[must_use]
    pub fn new() -> Self {
        Hexaco {
            openness: 0.0,
            conscientiousness: 0.0,
            extraversion: 0.0,
            agreeableness: 0.0,
            neuroticism: 0.0,
            honesty_humility: 0.0,
        }
    }

    /// Creates a Hexaco from a PersonalityProfile preset.
    ///
    /// The profile's 0.0-1.0 values are converted to -1.0 to 1.0 range.
    ///
    /// # Arguments
    ///
    /// * `profile` - The personality profile preset to use
    ///
    #[must_use]
    pub fn from_profile(profile: PersonalityProfile) -> Self {
        // Convert 0-1 range to -1 to 1 range: value * 2 - 1
        Hexaco {
            openness: profile.openness() * 2.0 - 1.0,
            conscientiousness: profile.conscientiousness() * 2.0 - 1.0,
            extraversion: profile.extraversion() * 2.0 - 1.0,
            agreeableness: profile.agreeableness() * 2.0 - 1.0,
            neuroticism: profile.neuroticism() * 2.0 - 1.0,
            honesty_humility: profile.honesty_humility() * 2.0 - 1.0,
        }
    }

    /// Creates a Hexaco with all factors set to the specified value.
    ///
    /// The value is clamped to the valid range -1.0 to 1.0.
    ///
    /// # Arguments
    ///
    /// * `value` - The value to set for all factors
    ///
    #[must_use]
    pub fn uniform(value: f32) -> Self {
        let clamped = value.clamp(-1.0, 1.0);
        Hexaco {
            openness: clamped,
            conscientiousness: clamped,
            extraversion: clamped,
            agreeableness: clamped,
            neuroticism: clamped,
            honesty_humility: clamped,
        }
    }

    // Builder methods

    /// Sets the openness factor.
    #[must_use]
    pub fn with_openness(mut self, value: f32) -> Self {
        self.openness = value.clamp(-1.0, 1.0);
        self
    }

    /// Sets the conscientiousness factor.
    #[must_use]
    pub fn with_conscientiousness(mut self, value: f32) -> Self {
        self.conscientiousness = value.clamp(-1.0, 1.0);
        self
    }

    /// Sets the extraversion factor.
    #[must_use]
    pub fn with_extraversion(mut self, value: f32) -> Self {
        self.extraversion = value.clamp(-1.0, 1.0);
        self
    }

    /// Sets the agreeableness factor.
    #[must_use]
    pub fn with_agreeableness(mut self, value: f32) -> Self {
        self.agreeableness = value.clamp(-1.0, 1.0);
        self
    }

    /// Sets the neuroticism factor.
    #[must_use]
    pub fn with_neuroticism(mut self, value: f32) -> Self {
        self.neuroticism = value.clamp(-1.0, 1.0);
        self
    }

    /// Sets the honesty-humility factor.
    #[must_use]
    pub fn with_honesty_humility(mut self, value: f32) -> Self {
        self.honesty_humility = value.clamp(-1.0, 1.0);
        self
    }

    // Accessors

    /// Returns the openness factor.
    #[must_use]
    pub fn openness(&self) -> f32 {
        self.openness
    }

    /// Returns the conscientiousness factor.
    #[must_use]
    pub fn conscientiousness(&self) -> f32 {
        self.conscientiousness
    }

    /// Returns the extraversion factor.
    #[must_use]
    pub fn extraversion(&self) -> f32 {
        self.extraversion
    }

    /// Returns the agreeableness factor.
    #[must_use]
    pub fn agreeableness(&self) -> f32 {
        self.agreeableness
    }

    /// Returns the neuroticism factor.
    ///
    /// Note: In HEXACO, this is called "Emotionality". Use `emotionality()`
    /// for HEXACO-standard naming.
    #[must_use]
    pub fn neuroticism(&self) -> f32 {
        self.neuroticism
    }

    /// Returns the emotionality factor (HEXACO standard name for neuroticism).
    ///
    /// Emotionality measures emotional reactivity, sensitivity, and anxiety.
    /// Higher values indicate stronger emotional responses to events.
    #[must_use]
    pub fn emotionality(&self) -> f32 {
        self.neuroticism
    }

    /// Returns the honesty-humility factor.
    #[must_use]
    pub fn honesty_humility(&self) -> f32 {
        self.honesty_humility
    }

    // Mutators

    /// Sets the openness factor directly.
    pub fn set_openness(&mut self, value: f32) {
        self.openness = value.clamp(-1.0, 1.0);
    }

    /// Sets the conscientiousness factor directly.
    pub fn set_conscientiousness(&mut self, value: f32) {
        self.conscientiousness = value.clamp(-1.0, 1.0);
    }

    /// Sets the extraversion factor directly.
    pub fn set_extraversion(&mut self, value: f32) {
        self.extraversion = value.clamp(-1.0, 1.0);
    }

    /// Sets the agreeableness factor directly.
    pub fn set_agreeableness(&mut self, value: f32) {
        self.agreeableness = value.clamp(-1.0, 1.0);
    }

    /// Sets the neuroticism factor directly.
    pub fn set_neuroticism(&mut self, value: f32) {
        self.neuroticism = value.clamp(-1.0, 1.0);
    }

    /// Sets the honesty-humility factor directly.
    pub fn set_honesty_humility(&mut self, value: f32) {
        self.honesty_humility = value.clamp(-1.0, 1.0);
    }
}

impl Default for Hexaco {
    fn default() -> Self {
        Hexaco::new()
    }
}

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

    #[test]
    fn new_creates_neutral_factors() {
        let hexaco = Hexaco::new();
        assert!((hexaco.openness() - 0.0).abs() < f32::EPSILON);
        assert!((hexaco.conscientiousness() - 0.0).abs() < f32::EPSILON);
        assert!((hexaco.extraversion() - 0.0).abs() < f32::EPSILON);
        assert!((hexaco.agreeableness() - 0.0).abs() < f32::EPSILON);
        assert!((hexaco.neuroticism() - 0.0).abs() < f32::EPSILON);
        assert!((hexaco.honesty_humility() - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn from_profile_balanced_is_neutral() {
        let hexaco = Hexaco::from_profile(PersonalityProfile::Balanced);
        // Balanced profile has 0.5 for all factors, which maps to 0.0 in -1 to 1 range
        assert!((hexaco.openness() - 0.0).abs() < f32::EPSILON);
        assert!((hexaco.conscientiousness() - 0.0).abs() < f32::EPSILON);
        assert!((hexaco.extraversion() - 0.0).abs() < f32::EPSILON);
        assert!((hexaco.agreeableness() - 0.0).abs() < f32::EPSILON);
        assert!((hexaco.neuroticism() - 0.0).abs() < f32::EPSILON);
        assert!((hexaco.honesty_humility() - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn from_profile_anxious_has_high_neuroticism() {
        let hexaco = Hexaco::from_profile(PersonalityProfile::Anxious);
        // Anxious profile has 0.8 neuroticism, which maps to 0.6 in -1 to 1 range
        assert!(hexaco.neuroticism() > 0.5);
    }

    #[test]
    fn from_profile_leader_characteristics() {
        let hexaco = Hexaco::from_profile(PersonalityProfile::Leader);
        assert!(hexaco.extraversion() > 0.5);
        assert!(hexaco.conscientiousness() > 0.5);
        assert!(hexaco.neuroticism() < -0.3);
    }

    #[test]
    fn uniform_sets_all_factors() {
        let hexaco = Hexaco::uniform(0.5);
        assert!((hexaco.openness() - 0.5).abs() < f32::EPSILON);
        assert!((hexaco.conscientiousness() - 0.5).abs() < f32::EPSILON);
        assert!((hexaco.extraversion() - 0.5).abs() < f32::EPSILON);
        assert!((hexaco.agreeableness() - 0.5).abs() < f32::EPSILON);
        assert!((hexaco.neuroticism() - 0.5).abs() < f32::EPSILON);
        assert!((hexaco.honesty_humility() - 0.5).abs() < f32::EPSILON);
    }

    #[test]
    fn uniform_clamps_values() {
        let high = Hexaco::uniform(2.0);
        assert!((high.openness() - 1.0).abs() < f32::EPSILON);

        let low = Hexaco::uniform(-2.0);
        assert!((low.openness() - (-1.0)).abs() < f32::EPSILON);
    }

    #[test]
    fn builder_methods_work() {
        let hexaco = Hexaco::new()
            .with_openness(0.7)
            .with_conscientiousness(0.5)
            .with_extraversion(-0.3)
            .with_agreeableness(0.2)
            .with_neuroticism(-0.5)
            .with_honesty_humility(0.8);

        assert!((hexaco.openness() - 0.7).abs() < f32::EPSILON);
        assert!((hexaco.conscientiousness() - 0.5).abs() < f32::EPSILON);
        assert!((hexaco.extraversion() - (-0.3)).abs() < f32::EPSILON);
        assert!((hexaco.agreeableness() - 0.2).abs() < f32::EPSILON);
        assert!((hexaco.neuroticism() - (-0.5)).abs() < f32::EPSILON);
        assert!((hexaco.honesty_humility() - 0.8).abs() < f32::EPSILON);
    }

    #[test]
    fn builder_methods_clamp_values() {
        let hexaco = Hexaco::new().with_openness(2.0).with_neuroticism(-2.0);

        assert!((hexaco.openness() - 1.0).abs() < f32::EPSILON);
        assert!((hexaco.neuroticism() - (-1.0)).abs() < f32::EPSILON);
    }

    #[test]
    fn setters_work() {
        let mut hexaco = Hexaco::new();

        hexaco.set_openness(0.7);
        assert!((hexaco.openness() - 0.7).abs() < f32::EPSILON);

        hexaco.set_conscientiousness(0.5);
        assert!((hexaco.conscientiousness() - 0.5).abs() < f32::EPSILON);

        hexaco.set_extraversion(-0.3);
        assert!((hexaco.extraversion() - (-0.3)).abs() < f32::EPSILON);

        hexaco.set_agreeableness(0.2);
        assert!((hexaco.agreeableness() - 0.2).abs() < f32::EPSILON);

        hexaco.set_neuroticism(-0.5);
        assert!((hexaco.neuroticism() - (-0.5)).abs() < f32::EPSILON);

        hexaco.set_honesty_humility(0.8);
        assert!((hexaco.honesty_humility() - 0.8).abs() < f32::EPSILON);
    }

    #[test]
    fn setters_clamp_values() {
        let mut hexaco = Hexaco::new();

        hexaco.set_openness(5.0);
        assert!((hexaco.openness() - 1.0).abs() < f32::EPSILON);

        hexaco.set_neuroticism(-5.0);
        assert!((hexaco.neuroticism() - (-1.0)).abs() < f32::EPSILON);
    }

    #[test]
    fn default_is_neutral() {
        let hexaco = Hexaco::default();
        assert!((hexaco.openness() - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn clone_and_equality() {
        let hexaco1 = Hexaco::new().with_openness(0.5);
        let hexaco2 = hexaco1.clone();
        assert_eq!(hexaco1, hexaco2);
    }

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

    #[test]
    fn from_profile_rebel_characteristics() {
        let hexaco = Hexaco::from_profile(PersonalityProfile::Rebel);
        // Rebel has low agreeableness (0.2) and low honesty_humility (0.3)
        assert!(hexaco.agreeableness() < -0.5);
        assert!(hexaco.honesty_humility() < -0.3);
    }

    #[test]
    fn from_profile_introverted_characteristics() {
        let hexaco = Hexaco::from_profile(PersonalityProfile::Introverted);
        // Introverted has low extraversion (0.2)
        assert!(hexaco.extraversion() < -0.5);
    }

    #[test]
    fn emotionality_is_alias_for_neuroticism() {
        let hexaco = Hexaco::new().with_neuroticism(0.7);
        assert!((hexaco.emotionality() - 0.7).abs() < f32::EPSILON);
        assert!((hexaco.emotionality() - hexaco.neuroticism()).abs() < f32::EPSILON);
    }
}