Skip to main content

DecayConstraintState

Struct DecayConstraintState 

Source
pub struct DecayConstraintState {
    pub node_id: String,
    pub satisfied: DecayCounter,
    pub violations: DecayCounter,
    pub half_life: f64,
}
Expand description

A time-decaying constraint state.

Fields§

§node_id: String§satisfied: DecayCounter

Satisfied constraints (decay counter)

§violations: DecayCounter

Violations (decay counter)

§half_life: f64

Half-life in seconds

Implementations§

Source§

impl DecayConstraintState

Source

pub fn new(node_id: &str, half_life_secs: f64) -> Self

Examples found in repository?
examples/experiments.rs (line 79)
72fn experiment_3_decay() {
73    println!("=== Experiment 3: Time-Decay Constraint CRDT ===\n");
74
75    let ns = 1_000_000_000u64;
76    
77    // Simulate a fleet node over 1 hour
78    let half_life = 300.0; // 5 minutes
79    let mut state = decay::DecayConstraintState::new("forgemaster", half_life);
80    
81    // Steady state: 100 satisfied per minute, 2 violations per minute
82    for t in 0..60 {
83        let time = t as u64 * 60 * ns;
84        state.record_satisfied(100.0, time);
85        state.record_violations(2.0, time);
86    }
87    
88    println!("  After 60 min steady state:");
89    println!("    Satisfaction rate at t=60: {:.1}%", state.satisfaction_rate(60 * 60 * ns) * 100.0);
90    
91    // Burst of violations at minute 55
92    let mut state2 = decay::DecayConstraintState::new("oracle1", half_life);
93    for t in 0..55 {
94        let time = t as u64 * 60 * ns;
95        state2.record_satisfied(100.0, time);
96        state2.record_violations(2.0, time);
97    }
98    // Burst: 50 violations at minute 55
99    state2.record_violations(50.0, 55 * 60 * ns);
100    state2.record_satisfied(100.0, 55 * 60 * ns);
101    for t in 56..60 {
102        let time = t as u64 * 60 * ns;
103        state2.record_satisfied(100.0, time);
104        state2.record_violations(2.0, time);
105    }
106    
107    println!("\n  After burst (50 violations at min 55):");
108    println!("    Satisfaction rate at t=60: {:.1}%", state2.satisfaction_rate(60 * 60 * ns) * 100.0);
109    println!("    Violation weight at t=60: {:.1}", state2.violation_weight(60 * 60 * ns));
110    
111    // Half-life comparison
112    println!("\n  Half-life sensitivity:");
113    for &hl in &[30.0, 60.0, 300.0, 3600.0] {
114        let mut s = decay::DecayConstraintState::new("test", hl);
115        for t in 0..60 {
116            s.record_violations(5.0, t as u64 * 60 * ns);
117        }
118        let weight = s.violation_weight(60 * 60 * ns);
119        println!("    half_life={:>4.0}s: violation_weight={:.1}", hl, weight);
120    }
121    println!();
122}
Source

pub fn record_satisfied(&mut self, count: f64, time_ns: u64)

Record satisfied constraints.

Examples found in repository?
examples/experiments.rs (line 84)
72fn experiment_3_decay() {
73    println!("=== Experiment 3: Time-Decay Constraint CRDT ===\n");
74
75    let ns = 1_000_000_000u64;
76    
77    // Simulate a fleet node over 1 hour
78    let half_life = 300.0; // 5 minutes
79    let mut state = decay::DecayConstraintState::new("forgemaster", half_life);
80    
81    // Steady state: 100 satisfied per minute, 2 violations per minute
82    for t in 0..60 {
83        let time = t as u64 * 60 * ns;
84        state.record_satisfied(100.0, time);
85        state.record_violations(2.0, time);
86    }
87    
88    println!("  After 60 min steady state:");
89    println!("    Satisfaction rate at t=60: {:.1}%", state.satisfaction_rate(60 * 60 * ns) * 100.0);
90    
91    // Burst of violations at minute 55
92    let mut state2 = decay::DecayConstraintState::new("oracle1", half_life);
93    for t in 0..55 {
94        let time = t as u64 * 60 * ns;
95        state2.record_satisfied(100.0, time);
96        state2.record_violations(2.0, time);
97    }
98    // Burst: 50 violations at minute 55
99    state2.record_violations(50.0, 55 * 60 * ns);
100    state2.record_satisfied(100.0, 55 * 60 * ns);
101    for t in 56..60 {
102        let time = t as u64 * 60 * ns;
103        state2.record_satisfied(100.0, time);
104        state2.record_violations(2.0, time);
105    }
106    
107    println!("\n  After burst (50 violations at min 55):");
108    println!("    Satisfaction rate at t=60: {:.1}%", state2.satisfaction_rate(60 * 60 * ns) * 100.0);
109    println!("    Violation weight at t=60: {:.1}", state2.violation_weight(60 * 60 * ns));
110    
111    // Half-life comparison
112    println!("\n  Half-life sensitivity:");
113    for &hl in &[30.0, 60.0, 300.0, 3600.0] {
114        let mut s = decay::DecayConstraintState::new("test", hl);
115        for t in 0..60 {
116            s.record_violations(5.0, t as u64 * 60 * ns);
117        }
118        let weight = s.violation_weight(60 * 60 * ns);
119        println!("    half_life={:>4.0}s: violation_weight={:.1}", hl, weight);
120    }
121    println!();
122}
Source

pub fn record_violations(&mut self, count: f64, time_ns: u64)

Record violations.

Examples found in repository?
examples/experiments.rs (line 85)
72fn experiment_3_decay() {
73    println!("=== Experiment 3: Time-Decay Constraint CRDT ===\n");
74
75    let ns = 1_000_000_000u64;
76    
77    // Simulate a fleet node over 1 hour
78    let half_life = 300.0; // 5 minutes
79    let mut state = decay::DecayConstraintState::new("forgemaster", half_life);
80    
81    // Steady state: 100 satisfied per minute, 2 violations per minute
82    for t in 0..60 {
83        let time = t as u64 * 60 * ns;
84        state.record_satisfied(100.0, time);
85        state.record_violations(2.0, time);
86    }
87    
88    println!("  After 60 min steady state:");
89    println!("    Satisfaction rate at t=60: {:.1}%", state.satisfaction_rate(60 * 60 * ns) * 100.0);
90    
91    // Burst of violations at minute 55
92    let mut state2 = decay::DecayConstraintState::new("oracle1", half_life);
93    for t in 0..55 {
94        let time = t as u64 * 60 * ns;
95        state2.record_satisfied(100.0, time);
96        state2.record_violations(2.0, time);
97    }
98    // Burst: 50 violations at minute 55
99    state2.record_violations(50.0, 55 * 60 * ns);
100    state2.record_satisfied(100.0, 55 * 60 * ns);
101    for t in 56..60 {
102        let time = t as u64 * 60 * ns;
103        state2.record_satisfied(100.0, time);
104        state2.record_violations(2.0, time);
105    }
106    
107    println!("\n  After burst (50 violations at min 55):");
108    println!("    Satisfaction rate at t=60: {:.1}%", state2.satisfaction_rate(60 * 60 * ns) * 100.0);
109    println!("    Violation weight at t=60: {:.1}", state2.violation_weight(60 * 60 * ns));
110    
111    // Half-life comparison
112    println!("\n  Half-life sensitivity:");
113    for &hl in &[30.0, 60.0, 300.0, 3600.0] {
114        let mut s = decay::DecayConstraintState::new("test", hl);
115        for t in 0..60 {
116            s.record_violations(5.0, t as u64 * 60 * ns);
117        }
118        let weight = s.violation_weight(60 * 60 * ns);
119        println!("    half_life={:>4.0}s: violation_weight={:.1}", hl, weight);
120    }
121    println!();
122}
Source

pub fn satisfaction_rate(&mut self, time_ns: u64) -> f64

Get satisfaction rate at a given time.

Examples found in repository?
examples/experiments.rs (line 89)
72fn experiment_3_decay() {
73    println!("=== Experiment 3: Time-Decay Constraint CRDT ===\n");
74
75    let ns = 1_000_000_000u64;
76    
77    // Simulate a fleet node over 1 hour
78    let half_life = 300.0; // 5 minutes
79    let mut state = decay::DecayConstraintState::new("forgemaster", half_life);
80    
81    // Steady state: 100 satisfied per minute, 2 violations per minute
82    for t in 0..60 {
83        let time = t as u64 * 60 * ns;
84        state.record_satisfied(100.0, time);
85        state.record_violations(2.0, time);
86    }
87    
88    println!("  After 60 min steady state:");
89    println!("    Satisfaction rate at t=60: {:.1}%", state.satisfaction_rate(60 * 60 * ns) * 100.0);
90    
91    // Burst of violations at minute 55
92    let mut state2 = decay::DecayConstraintState::new("oracle1", half_life);
93    for t in 0..55 {
94        let time = t as u64 * 60 * ns;
95        state2.record_satisfied(100.0, time);
96        state2.record_violations(2.0, time);
97    }
98    // Burst: 50 violations at minute 55
99    state2.record_violations(50.0, 55 * 60 * ns);
100    state2.record_satisfied(100.0, 55 * 60 * ns);
101    for t in 56..60 {
102        let time = t as u64 * 60 * ns;
103        state2.record_satisfied(100.0, time);
104        state2.record_violations(2.0, time);
105    }
106    
107    println!("\n  After burst (50 violations at min 55):");
108    println!("    Satisfaction rate at t=60: {:.1}%", state2.satisfaction_rate(60 * 60 * ns) * 100.0);
109    println!("    Violation weight at t=60: {:.1}", state2.violation_weight(60 * 60 * ns));
110    
111    // Half-life comparison
112    println!("\n  Half-life sensitivity:");
113    for &hl in &[30.0, 60.0, 300.0, 3600.0] {
114        let mut s = decay::DecayConstraintState::new("test", hl);
115        for t in 0..60 {
116            s.record_violations(5.0, t as u64 * 60 * ns);
117        }
118        let weight = s.violation_weight(60 * 60 * ns);
119        println!("    half_life={:>4.0}s: violation_weight={:.1}", hl, weight);
120    }
121    println!();
122}
Source

pub fn violation_weight(&mut self, time_ns: u64) -> f64

Current violation “weight” (how much recent violations matter).

Examples found in repository?
examples/experiments.rs (line 109)
72fn experiment_3_decay() {
73    println!("=== Experiment 3: Time-Decay Constraint CRDT ===\n");
74
75    let ns = 1_000_000_000u64;
76    
77    // Simulate a fleet node over 1 hour
78    let half_life = 300.0; // 5 minutes
79    let mut state = decay::DecayConstraintState::new("forgemaster", half_life);
80    
81    // Steady state: 100 satisfied per minute, 2 violations per minute
82    for t in 0..60 {
83        let time = t as u64 * 60 * ns;
84        state.record_satisfied(100.0, time);
85        state.record_violations(2.0, time);
86    }
87    
88    println!("  After 60 min steady state:");
89    println!("    Satisfaction rate at t=60: {:.1}%", state.satisfaction_rate(60 * 60 * ns) * 100.0);
90    
91    // Burst of violations at minute 55
92    let mut state2 = decay::DecayConstraintState::new("oracle1", half_life);
93    for t in 0..55 {
94        let time = t as u64 * 60 * ns;
95        state2.record_satisfied(100.0, time);
96        state2.record_violations(2.0, time);
97    }
98    // Burst: 50 violations at minute 55
99    state2.record_violations(50.0, 55 * 60 * ns);
100    state2.record_satisfied(100.0, 55 * 60 * ns);
101    for t in 56..60 {
102        let time = t as u64 * 60 * ns;
103        state2.record_satisfied(100.0, time);
104        state2.record_violations(2.0, time);
105    }
106    
107    println!("\n  After burst (50 violations at min 55):");
108    println!("    Satisfaction rate at t=60: {:.1}%", state2.satisfaction_rate(60 * 60 * ns) * 100.0);
109    println!("    Violation weight at t=60: {:.1}", state2.violation_weight(60 * 60 * ns));
110    
111    // Half-life comparison
112    println!("\n  Half-life sensitivity:");
113    for &hl in &[30.0, 60.0, 300.0, 3600.0] {
114        let mut s = decay::DecayConstraintState::new("test", hl);
115        for t in 0..60 {
116            s.record_violations(5.0, t as u64 * 60 * ns);
117        }
118        let weight = s.violation_weight(60 * 60 * ns);
119        println!("    half_life={:>4.0}s: violation_weight={:.1}", hl, weight);
120    }
121    println!();
122}

Trait Implementations§

Source§

impl Clone for DecayConstraintState

Source§

fn clone(&self) -> DecayConstraintState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DecayConstraintState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for DecayConstraintState

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Merge for DecayConstraintState

Source§

fn merge(&mut self, other: &Self)

Merge another state into this one (in-place). Read more
Source§

fn merged(&self, other: &Self) -> Self
where Self: Sized,

Create a merged copy without modifying self.
Source§

fn subsumes(&self, other: &Self) -> bool
where Self: PartialEq,

Check if this state subsumes (is greater than or equal to) another. In lattice terms: self ≥ other.
Source§

impl PartialEq for DecayConstraintState

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for DecayConstraintState

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,