alec 1.3.7

Adaptive Lazy Evolving Compression - Smart codec for IoT sensor data with 90% compression ratio
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
// ALEC - Adaptive Lazy Evolving Compression
// Copyright (c) 2025 David Martin Venti
//
// Dual-licensed under AGPL-3.0 and Commercial License.
// See LICENSE file for details.

//! Demo Preload Generator for ALEC
//!
//! This example generates pre-trained context files (preloads) for common
//! sensor types. These preloads allow ALEC to achieve optimal compression
//! from the very first byte, eliminating the "cold start" problem.
//!
//! # Generated Preloads
//!
//! - `demo_temperature_v1.alec-context` - Indoor temperature sensor (20-25°C)
//! - `demo_humidity_v1.alec-context` - Humidity sensor with daily cycle (40-60%)
//! - `demo_counter_v1.alec-context` - Monotonic counter with occasional resets
//!
//! # Usage
//!
//! ```bash
//! cargo run --example generate_preloads
//! ```
//!
//! # Using Generated Preloads
//!
//! ```rust,no_run
//! use alec::context::Context;
//! use alec::Encoder;
//! use std::path::Path;
//!
//! // Load a pre-trained context
//! let context = Context::load_from_file(
//!     Path::new("contexts/demo/demo_temperature_v1.alec-context")
//! ).expect("Failed to load preload");
//!
//! // Create encoder with pre-trained context - ready for optimal compression!
//! let mut encoder = Encoder::with_context(context);
//! ```

use alec::context::{
    Context, ContextConfig, EvolutionConfig, Pattern, PreloadDictEntry, PreloadFile,
    PreloadPredictionModel, PreloadPredictionType, PreloadStatistics,
};
use alec::protocol::RawData;
use std::path::Path;

/// Configuration for demo preload generation
struct PreloadConfig {
    sensor_type: &'static str,
    min_value: f64,
    max_value: f64,
    typical_value: f64,
    noise_amplitude: f64,
    training_samples: usize,
    prediction_type: PreloadPredictionType,
    prediction_coefficients: Vec<f64>,
    period_samples: u32,
}

fn main() {
    println!("=== ALEC Demo Preload Generator ===\n");

    let output_dir = Path::new("contexts/demo");

    // Ensure output directory exists
    std::fs::create_dir_all(output_dir).expect("Failed to create output directory");

    // Generate all demo preloads
    generate_temperature_preload(output_dir);
    generate_humidity_preload(output_dir);
    generate_counter_preload(output_dir);

    println!("\n=== Verification ===\n");

    // Verify all generated preloads
    verify_preload(output_dir.join("demo_temperature_v1.alec-context"));
    verify_preload(output_dir.join("demo_humidity_v1.alec-context"));
    verify_preload(output_dir.join("demo_counter_v1.alec-context"));

    println!("\n=== Done ===");
    println!("\nPreloads are ready in: {}", output_dir.display());
    println!("\nUsage example:");
    println!("  let ctx = Context::load_from_file(Path::new(\"contexts/demo/demo_temperature_v1.alec-context\"))?;");
}

/// Generate temperature sensor preload
///
/// Simulates an indoor temperature sensor:
/// - Range: 20.0 - 25.0 °C
/// - Typical: 22.5 °C
/// - Noise: ±0.1 °C
/// - Slow drift over time
fn generate_temperature_preload(output_dir: &Path) {
    println!("Generating: demo_temperature_v1.alec-context");

    let config = PreloadConfig {
        sensor_type: "temperature",
        min_value: 20.0,
        max_value: 25.0,
        typical_value: 22.5,
        noise_amplitude: 0.1,
        training_samples: 10000,
        prediction_type: PreloadPredictionType::MovingAverage,
        prediction_coefficients: vec![3.0], // 3-sample moving average
        period_samples: 0,
    };

    // Create context with evolution disabled for consistent training
    let ctx_config = ContextConfig {
        evolution: EvolutionConfig {
            enabled: false,
            ..Default::default()
        },
        ..Default::default()
    };
    let mut ctx = Context::with_config(ctx_config);

    // Register common delta patterns for temperature
    // Temperature typically changes by small amounts
    let delta_patterns: Vec<i16> = vec![
        0, // No change (most common)
        1, -1, // ±0.01°C (scaled)
        2, -2, // ±0.02°C
        5, -5, // ±0.05°C
        10, -10, // ±0.1°C
    ];

    for delta in delta_patterns {
        let bytes = delta.to_le_bytes().to_vec();
        let _ = ctx.register_pattern(Pattern::new(bytes));
    }

    // Simulate temperature readings with slow drift and noise
    let mut value = config.typical_value;
    let mut drift_direction = 1.0;

    for i in 0..config.training_samples {
        // Add small noise
        let noise = (simple_random(i as u64) - 0.5) * 2.0 * config.noise_amplitude;

        // Slow drift (changes direction at boundaries)
        if value >= config.max_value - 0.5 {
            drift_direction = -1.0;
        } else if value <= config.min_value + 0.5 {
            drift_direction = 1.0;
        }
        let drift = drift_direction * 0.001;

        value = (value + drift + noise).clamp(config.min_value, config.max_value);

        ctx.observe(&RawData::new(value, i as u64));
    }

    // Build preload with custom statistics
    let preload = build_preload(&ctx, &config);

    // Save preload
    let path = output_dir.join("demo_temperature_v1.alec-context");
    preload
        .save_to_file(&path)
        .expect("Failed to save temperature preload");

    println!("  - Saved: {}", path.display());
    println!("  - Training samples: {}", config.training_samples);
    println!("  - Patterns: {}", ctx.pattern_count());
}

/// Generate humidity sensor preload
///
/// Simulates a humidity sensor with daily cycle:
/// - Range: 40.0 - 60.0 %
/// - Typical: 50.0 %
/// - Daily cycle: ±5%
/// - Noise: ±2%
fn generate_humidity_preload(output_dir: &Path) {
    println!("Generating: demo_humidity_v1.alec-context");

    let config = PreloadConfig {
        sensor_type: "humidity",
        min_value: 40.0,
        max_value: 60.0,
        typical_value: 50.0,
        noise_amplitude: 2.0,
        training_samples: 10000,
        prediction_type: PreloadPredictionType::Periodic,
        prediction_coefficients: vec![],
        period_samples: 86400, // 1 day in seconds
    };

    let ctx_config = ContextConfig {
        evolution: EvolutionConfig {
            enabled: false,
            ..Default::default()
        },
        ..Default::default()
    };
    let mut ctx = Context::with_config(ctx_config);

    // Register common delta patterns for humidity
    let delta_patterns: Vec<i16> = vec![
        0, // No change
        10, -10, // ±0.1%
        20, -20, // ±0.2%
        50, -50, // ±0.5%
        100, -100, // ±1.0%
    ];

    for delta in delta_patterns {
        let bytes = delta.to_le_bytes().to_vec();
        let _ = ctx.register_pattern(Pattern::new(bytes));
    }

    // Simulate humidity with daily sinusoidal cycle
    let samples_per_day = 86400; // 1 sample per second
    let cycle_amplitude = 5.0;

    for i in 0..config.training_samples {
        // Daily cycle (simplified: use sample index as time)
        let day_progress = (i % samples_per_day) as f64 / samples_per_day as f64;
        let cycle_value = (day_progress * 2.0 * std::f64::consts::PI).sin() * cycle_amplitude;

        // Add noise
        let noise = (simple_random(i as u64) - 0.5) * 2.0 * config.noise_amplitude;

        let value =
            (config.typical_value + cycle_value + noise).clamp(config.min_value, config.max_value);

        ctx.observe(&RawData::new(value, i as u64));
    }

    let preload = build_preload(&ctx, &config);

    let path = output_dir.join("demo_humidity_v1.alec-context");
    preload
        .save_to_file(&path)
        .expect("Failed to save humidity preload");

    println!("  - Saved: {}", path.display());
    println!("  - Training samples: {}", config.training_samples);
    println!("  - Patterns: {}", ctx.pattern_count());
}

/// Generate counter sensor preload
///
/// Simulates a monotonic counter:
/// - Range: 0 - 1,000,000
/// - Behavior: Increment by 1, occasional reset to 0
/// - Prediction: Linear (slope=1)
fn generate_counter_preload(output_dir: &Path) {
    println!("Generating: demo_counter_v1.alec-context");

    let config = PreloadConfig {
        sensor_type: "counter",
        min_value: 0.0,
        max_value: 1_000_000.0,
        typical_value: 0.0,
        noise_amplitude: 0.0,
        training_samples: 10000,
        prediction_type: PreloadPredictionType::Linear,
        prediction_coefficients: vec![1.0, 0.0], // slope=1, intercept=0 (predict next = current + 1)
        period_samples: 0,
    };

    let ctx_config = ContextConfig {
        evolution: EvolutionConfig {
            enabled: false,
            ..Default::default()
        },
        ..Default::default()
    };
    let mut ctx = Context::with_config(ctx_config);

    // Register common patterns for counter
    // Delta of 1 is most common
    let patterns: Vec<Vec<u8>> = vec![
        1i64.to_le_bytes().to_vec(), // Delta +1 (most common)
        0i64.to_le_bytes().to_vec(), // No change (rare)
        vec![0xFF; 8],               // Reset marker pattern
    ];

    for pattern in patterns {
        let _ = ctx.register_pattern(Pattern::new(pattern));
    }

    // Simulate counter with occasional resets
    let mut counter: i64 = 0;
    let reset_probability = 0.001; // 0.1% chance of reset per sample

    for i in 0..config.training_samples {
        // Occasional reset
        if simple_random(i as u64 + 1000) < reset_probability {
            counter = 0;
        } else {
            counter += 1;
        }

        ctx.observe(&RawData::new(counter as f64, i as u64));
    }

    let preload = build_preload(&ctx, &config);

    let path = output_dir.join("demo_counter_v1.alec-context");
    preload
        .save_to_file(&path)
        .expect("Failed to save counter preload");

    println!("  - Saved: {}", path.display());
    println!("  - Training samples: {}", config.training_samples);
    println!("  - Patterns: {}", ctx.pattern_count());
}

/// Build a PreloadFile from a trained context and configuration
fn build_preload(ctx: &Context, config: &PreloadConfig) -> PreloadFile {
    // Collect dictionary entries from context
    let mut dictionary: Vec<PreloadDictEntry> = Vec::new();
    for (&code, pattern) in ctx.patterns_iter() {
        if code <= u16::MAX as u32 {
            dictionary.push(PreloadDictEntry {
                pattern: pattern.data.clone(),
                code: code as u16,
                frequency: pattern.frequency as u32,
            });
        }
    }
    dictionary.sort_by_key(|e| e.code);

    // Build statistics
    let statistics = PreloadStatistics {
        mean: config.typical_value,
        variance: config.noise_amplitude.powi(2),
        min_observed: config.min_value,
        max_observed: config.max_value,
        min_expected: config.min_value - config.noise_amplitude,
        max_expected: config.max_value + config.noise_amplitude,
        recent_values: vec![config.typical_value; 3],
    };

    // Build prediction model
    let prediction = PreloadPredictionModel {
        model_type: config.prediction_type,
        coefficients: config.prediction_coefficients.clone(),
        period_samples: config.period_samples,
    };

    PreloadFile {
        format_version: 1,
        context_version: ctx.version(),
        sensor_type: config.sensor_type.to_string(),
        created_timestamp: std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0),
        training_samples: config.training_samples as u64,
        dictionary,
        statistics,
        prediction,
    }
}

/// Simple deterministic pseudo-random number generator (0.0 - 1.0)
fn simple_random(seed: u64) -> f64 {
    // Simple LCG for reproducible results
    let a: u64 = 6364136223846793005;
    let c: u64 = 1442695040888963407;
    let m: u64 = u64::MAX;
    let result = seed.wrapping_mul(a).wrapping_add(c) % m;
    result as f64 / m as f64
}

/// Verify a preload file is valid
fn verify_preload<P: AsRef<Path>>(path: P) {
    let path = path.as_ref();
    print!("Verifying: {} ... ", path.display());

    match PreloadFile::load_from_file(path) {
        Ok(preload) => {
            // Check file is valid
            assert!(
                !preload.sensor_type.is_empty(),
                "Sensor type should not be empty"
            );
            assert!(preload.format_version == 1, "Format version should be 1");
            assert!(preload.training_samples > 0, "Should have training samples");

            // Check statistics are reasonable
            assert!(
                preload.statistics.min_observed <= preload.statistics.max_observed,
                "Min should be <= max"
            );
            assert!(
                preload.statistics.mean >= preload.statistics.min_observed
                    && preload.statistics.mean <= preload.statistics.max_observed,
                "Mean should be within observed range"
            );

            println!("OK");
            println!("    Sensor type: {}", preload.sensor_type);
            println!("    Context version: {}", preload.context_version);
            println!("    Training samples: {}", preload.training_samples);
            println!("    Dictionary entries: {}", preload.dictionary.len());
            println!("    Prediction model: {:?}", preload.prediction.model_type);
        }
        Err(e) => {
            println!("FAILED");
            println!("    Error: {}", e);
        }
    }
}