arco 0.6.0

Automated Research into Computational Ontologies — a platform for discovering the conditions under which computation emerges
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! Scientific cycle orchestrator.
//!
//! Per the Mathematical Constitution:
//!     The scientific cycle is: Generate → Calibrate → Observe →
//!     Hypothesize → Predict → Test → Revise. Each cycle produces
//!     a Research Record.
//!
//! # Quick start
//!
//! ```rust,no_run
//! use arco::cycle::{CycleConfig, run_cycle};
//! use arco::substrates::graph::{
//!     BinaryGraphUniverse, generate_standard_hypotheses,
//! };
//! use rand::{rngs::StdRng, SeedableRng};
//!
//! let mut rng = StdRng::seed_from_u64(42);
//! let config = CycleConfig {
//!     n_train: 20,
//!     n_test: 5,
//!     ..CycleConfig::default()
//! };
//! let universe = BinaryGraphUniverse::new(3, "compound", &mut rng, config.n_train + config.n_test);
//! let mut hypotheses = generate_standard_hypotheses();
//! let record = run_cycle(&universe, &config, &mut hypotheses, None);
//! println!("{}", record.summary());
//! ```

use std::time::Instant;

use rand::RngExt;
use rand::SeedableRng;
use rand::rngs::StdRng;
use rayon::iter::IndexedParallelIterator;
use rayon::iter::IntoParallelRefIterator;
use rayon::iter::IntoParallelRefMutIterator;
use rayon::iter::ParallelIterator;

use crate::calibration::CalibrationConfig;
use crate::calibration::{calibrate, generate_trajectories};
use crate::hypotheses::{Hypothesis, surviving_hypotheses};
use crate::metrics::NsbCardinality;
use crate::metrics::{Estimator, MetricConfig, memory, persistence, storage};
use crate::record::{ClassificationMetrics, HypothesisRecord, ResearchRecord, UniverseResult};
use crate::rules::Rule;
use crate::types::BooleanTester;
use crate::types::TestEnsembles;
use crate::universe::InformationUniverse;

// ===================================================================
// Cycle Configuration
// ===================================================================

/// Configuration for a scientific cycle run.
///
/// All parameters have sensible defaults. The cycle is reproducible
/// given the same config and universe.
#[derive(Debug, Clone)]
pub struct CycleConfig {
    /// Number of training universes.
    pub n_train: usize,
    /// Number of held-out test universes.
    pub n_test: usize,
    /// Ensemble size per universe.
    pub n_ensemble: usize,
    /// Timesteps per trajectory.
    pub steps: usize,
    /// Maximum timescale for storage/memory.
    pub max_delta: usize,
    /// Number of shuffles for bias correction.
    pub n_shuffles: usize,
    /// Number of null universes for calibration.
    pub n_null_universes: usize,
    /// Random seed for reproducibility.
    pub seed: u64,
    /// The MI estimator
    pub estimator: Estimator,
    /// NSB estimator cardinality.
    pub cardinality: NsbCardinality,
}

impl Default for CycleConfig {
    fn default() -> Self {
        Self {
            n_train: 300,
            n_test: 100,
            n_ensemble: 10,
            steps: 60,
            max_delta: 15,
            n_shuffles: 10,
            n_null_universes: 30,
            seed: 42,
            estimator: Estimator::Plugin,
            cardinality: NsbCardinality::Observed,
        }
    }
}

// ===================================================================
// Scientific Cycle
// ===================================================================

/// Execute the full ARCO scientific cycle.
///
/// # Type parameters
///
/// - `U: InformationUniverse` — The universe type. The cycle is fully
///   generic over the state, rule, observation, and schedule types.
///
/// # Steps
///
/// 1. **Generate**: Call `universe.generate_rules()` to sample rule sets.
/// 2. **Calibrate**: Compute thresholds from destructive null
///    universes using `universe.null_rules()`.
/// 3. **Observe**: Generate ensembles via `generate_trajectories`
///    and compute storage, memory, and persistence for each
///    training universe.
/// 4. **Hypothesize & Test**: For each hypothesis, evaluate its
///    condition on test rule sets, compute the predicted metric
///    directly, and check against the calibrated threshold.
/// 5. **Revise**: Check failure conditions, optionally verify
///    boolean functions, compile the research record.
///
/// # Parameters
///
/// * `universe` — The Information Universe to study.
/// * `config` — Experimental parameters.
/// * `hypotheses` — Mutable slice of hypotheses to test.
/// * `boolean_tester` — Optional function that verifies boolean
///   functions implemented by rule sets. Pass `None` to skip.
///
/// # Returns
///
/// A [`ResearchRecord`] with all experimental data.
pub fn run_cycle<U: InformationUniverse>(
    universe: &U,
    config: &CycleConfig,
    hypotheses: &mut [Hypothesis<U::Rule>],
    boolean_tester: Option<&BooleanTester<U>>,
) -> ResearchRecord<U> {
    let t0 = Instant::now();
    let mut record = ResearchRecord::new(env!("CARGO_PKG_VERSION"));

    // Store config for reproducibility
    record
        .config
        .insert("n_train".to_string(), config.n_train.to_string());
    record
        .config
        .insert("n_test".to_string(), config.n_test.to_string());
    record
        .config
        .insert("n_ensemble".to_string(), config.n_ensemble.to_string());
    record
        .config
        .insert("steps".to_string(), config.steps.to_string());
    record
        .config
        .insert("max_delta".to_string(), config.max_delta.to_string());
    record
        .config
        .insert("n_shuffles".to_string(), config.n_shuffles.to_string());
    record.config.insert(
        "n_null_universes".to_string(),
        config.n_null_universes.to_string(),
    );
    record
        .config
        .insert("seed".to_string(), config.seed.to_string());
    record
        .config
        .insert("estimator".to_string(), config.estimator.name().to_string());
    record.config.insert(
        "cardinality".to_string(),
        config.cardinality.name().to_string(),
    );

    let mut rng = StdRng::seed_from_u64(config.seed);
    let state_space = universe.state_space();
    let schedule = universe.schedule();
    let observer = universe.observation();

    // ================================================================
    // STEP 1: GENERATE
    // ================================================================
    let mut train_subsets: Vec<(Vec<U::Rule>, f64)> = Vec::with_capacity(config.n_train);
    let mut test_subsets: Vec<(Vec<U::Rule>, f64)> = Vec::with_capacity(config.n_test);

    for _ in 0..config.n_train {
        train_subsets.push(universe.generate_rules(&mut rng));
    }
    for _ in 0..config.n_test {
        test_subsets.push(universe.generate_rules(&mut rng));
    }

    // ================================================================
    // STEP 2: CALIBRATE
    // ================================================================
    let met_config = MetricConfig {
        estimator: config.estimator,
        cardinality: config.cardinality.clone(),
        max_delta: config.max_delta,
        n_shuffles: config.n_shuffles,
        seed: config.seed,
    };
    let ca_config = CalibrationConfig {
        metric: met_config,
        ..CalibrationConfig::default()
    };
    let calibration = calibrate(
        universe,
        config.n_null_universes,
        config.n_ensemble,
        config.steps,
        &ca_config,
    );

    record
        .thresholds
        .insert("persistence".to_string(), calibration.persistence_threshold);
    record
        .thresholds
        .insert("storage".to_string(), calibration.storage_threshold);
    record
        .thresholds
        .insert("memory".to_string(), calibration.memory_threshold);

    // ================================================================
    // STEP 3: OBSERVE
    // ================================================================
    let mut results: Vec<UniverseResult> = (0..train_subsets.len())
        .map(|i| UniverseResult {
            universe_id: i,
            structured_ratio: 0.0,
            n_rules: 0,
            rule_names: vec![],
            persistence: 0.0,
            storage: 0.0,
            memory: 0.0,
        })
        .collect();

    results
        .par_iter_mut()
        .zip(train_subsets.par_iter())
        .enumerate()
        .for_each(|(i, (result, (rules, ratio)))| {
            let local_seed = config.seed + i as u64 * 137;
            let mut local_rng = StdRng::seed_from_u64(local_seed);

            let n_pool = state_space.len();
            let n_ens = config.n_ensemble.min(n_pool);
            let mut init_indices: Vec<usize> = (0..n_pool).collect();
            for j in 0..n_ens {
                let k = local_rng.random_range(j..n_pool);
                init_indices.swap(j, k);
            }
            let initial_states: Vec<U::State> = init_indices
                .iter()
                .take(n_ens)
                .map(|&idx| state_space[idx].clone())
                .collect();

            let ensemble = generate_trajectories(
                &initial_states,
                rules,
                observer,
                schedule,
                config.steps,
                local_seed,
            );

            *result = UniverseResult {
                universe_id: i,
                structured_ratio: *ratio,
                n_rules: rules.len(),
                rule_names: rules.iter().map(|r| r.name().to_string()).collect(),
                persistence: persistence(&ensemble, 1, &ca_config.metric),
                storage: storage(&ensemble, &ca_config.metric),
                memory: memory(&ensemble, &ca_config.metric),
            };
        });

    record.results = results;

    // ================================================================
    // STEP 4: HYPOTHESIZE & TEST
    // ================================================================
    let mut test_ensembles: TestEnsembles<U> =
        (0..test_subsets.len()).map(|_| Vec::new()).collect();

    test_ensembles
        .par_iter_mut()
        .zip(test_subsets.par_iter())
        .enumerate()
        .for_each(|(i, (ensemble_out, (rules, _ratio)))| {
            let local_seed = config.seed + 10000 + i as u64 * 137;
            let mut local_rng = StdRng::seed_from_u64(local_seed);

            let n_pool = state_space.len();
            let n_ens = config.n_ensemble.min(n_pool);
            let mut init_indices: Vec<usize> = (0..n_pool).collect();
            for j in 0..n_ens {
                let k = local_rng.random_range(j..n_pool);
                init_indices.swap(j, k);
            }
            let initial_states: Vec<U::State> = init_indices
                .iter()
                .take(n_ens)
                .map(|&idx| state_space[idx].clone())
                .collect();

            *ensemble_out = generate_trajectories(
                &initial_states,
                rules,
                observer,
                schedule,
                config.steps,
                local_seed,
            );
        });

    let mut classes = Vec::with_capacity(hypotheses.len());
    for h in hypotheses.iter_mut() {
        let threshold = record
            .thresholds
            .get(&h.property_name)
            .copied()
            .unwrap_or(0.0);

        let class = hypothesis_classification_metrics::<U>(
            h,
            &test_subsets,
            &test_ensembles,
            threshold,
            &ca_config.metric,
        );
        classes.push(class);
    }

    record.hypotheses = hypotheses
        .iter()
        .zip(classes.iter())
        .map(|(h, c)| {
            let mut r = HypothesisRecord::from(h);
            r.classification_metrics = *c;
            r
        })
        .collect();

    // ================================================================
    // STEP 5: BOOLEAN VERIFICATION (OPTIONAL)
    // ================================================================
    if let Some(tester) = boolean_tester {
        for (rules, _ratio) in train_subsets.iter() {
            let verified = tester(rules);
            for (gate, count) in verified {
                *record.boolean_verifications.entry(gate).or_insert(0) += count;
            }
        }
    }

    // ================================================================
    // STEP 6: FAILURE CONDITION CHECK
    // ================================================================
    let nand_count = record
        .boolean_verifications
        .get("NAND")
        .copied()
        .unwrap_or(0);

    if record.n_storage() == 0 {
        record
            .failure_conditions
            .push("F-1 (NULL): No storage universes found.".to_string());
    }

    if nand_count == 0 && boolean_tester.is_some() {
        record
            .failure_conditions
            .push("F-1 (NULL): NAND not verified.".to_string());
    }

    let surviving = surviving_hypotheses(hypotheses);
    if surviving.is_empty() && !hypotheses.is_empty() {
        record
            .failure_conditions
            .push("F-6 (DISCONFIRMATION): No hypotheses survived.".to_string());
    }

    record.elapsed_seconds = t0.elapsed().as_secs_f64();
    record
}

/// Compute standard binary classification metrics for one hypothesis on
/// held-out test data.
///
/// The hypothesis condition determines the predicted class, while the
/// measured property relative to `threshold` determines the actual class:
///
/// - **predicted positive**: the universe satisfies the hypothesis condition
/// - **predicted negative**: the universe does not satisfy the hypothesis
///   condition
/// - **actual positive**: the measured property exceeds `threshold`
/// - **actual negative**: the measured property does not exceed `threshold`
///
/// The confusion matrix is therefore:
///
/// ```text
///                         Actual positive    Actual negative
/// Predicted positive          TP                  FP
/// Predicted negative          FN                  TN
/// ```
///
/// The returned metrics are:
///
/// - `accuracy`: `(TP + TN) / total`
/// - `precision`: `TP / (TP + FP)`
/// - `recall`: `TP / (TP + FN)`
/// - `specificity`: `TN / (TN + FP)`
/// - `balanced_accuracy`: `(recall + specificity) / 2`
/// - `coverage`: `(TP + FP) / total`
///
/// `coverage` measures how frequently the hypothesis condition is satisfied
/// in the held-out test data. It is not a classification-quality metric, but
/// is useful for interpreting the other metrics: a hypothesis with excellent
/// precision but very low coverage applies to only a small fraction of the
/// test universes.
///
/// Only held-out test subsets are considered. Each test subset is paired with
/// its corresponding test ensemble by position.
///
/// If a metric has a zero denominator, its value is returned as `0.0`.
fn hypothesis_classification_metrics<U: InformationUniverse>(
    hypothesis: &mut Hypothesis<U::Rule>,
    test_subsets: &[(Vec<U::Rule>, f64)],
    test_ensembles: &TestEnsembles<U>,
    threshold: f64,
    metric: &MetricConfig,
) -> ClassificationMetrics {
    let mut true_positive = 0usize;
    let mut false_positive = 0usize;
    let mut false_negative = 0usize;
    let mut true_negative = 0usize;

    for ((rules, _ratio), ensemble) in test_subsets.iter().zip(test_ensembles.iter()) {
        let predicted_positive = (hypothesis.condition_fn)(rules);

        let metric_value = match hypothesis.property_name.as_str() {
            "persistence" => persistence(ensemble, 1, metric),
            "storage" => storage(ensemble, metric),
            "memory" => memory(ensemble, metric),
            _ => 0.0,
        };

        let actual_positive = metric_value > threshold;

        match (predicted_positive, actual_positive) {
            (true, true) => true_positive += 1,
            (true, false) => false_positive += 1,
            (false, true) => false_negative += 1,
            (false, false) => true_negative += 1,
        }
    }

    let total = true_positive + false_positive + false_negative + true_negative;

    let accuracy = if total > 0 {
        (true_positive + true_negative) as f64 / total as f64
    } else {
        0.0
    };

    let precision_denominator = true_positive + false_positive;
    let precision = if precision_denominator > 0 {
        true_positive as f64 / precision_denominator as f64
    } else {
        0.0
    };

    let recall_denominator = true_positive + false_negative;
    let recall = if recall_denominator > 0 {
        true_positive as f64 / recall_denominator as f64
    } else {
        0.0
    };

    let specificity_denominator = true_negative + false_positive;
    let specificity = if specificity_denominator > 0 {
        true_negative as f64 / specificity_denominator as f64
    } else {
        0.0
    };

    let balanced_accuracy = (recall + specificity) / 2.0;

    let coverage_denominator = total;
    let coverage = if coverage_denominator > 0 {
        (true_positive + false_positive) as f64 / coverage_denominator as f64
    } else {
        0.0
    };

    // Update hypothesis
    hypothesis.accuracy = balanced_accuracy;
    let score = hypothesis.accuracy - 0.1 * hypothesis.complexity;
    hypothesis.score = score;

    ClassificationMetrics {
        accuracy,
        precision,
        recall,
        specificity,
        balanced_accuracy,
        coverage,
        score,
        true_positive,
        false_positive,
        false_negative,
        true_negative,
    }
}