File size: 25,408 Bytes
2b9a95b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
"""
CausalSCM - Abstract base class for Structural Causal Models.

Each experiment implements its own SCM subclass that defines:
- Environment variable sampling
- Causal relationships
- Effect computation

Key principle: SCM writes effects into DroneSheet middleware.
This is the ONLY way SCM affects the game.

Enhanced features:
- Explicit causal variable and structure equation registration
- do-intervention support
- Automatic sampling with topological ordering
- Multi-format export (text, graph, DOT, JSON Schema)
"""

from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import Dict, Any, Optional, List, Tuple, Callable, Set
import random
import copy

# Handle both development (api.middleware) and Docker (middleware) imports
try:
    from ...middleware.drone_state import EnvironmentEffects
    from ...middleware.drone_sheet import DroneSheet
except ImportError:
    from middleware.drone_state import EnvironmentEffects
    from middleware.drone_sheet import DroneSheet


# ============================================================
# Core Data Structures for Explicit Causal Structure
# ============================================================

@dataclass
class CausalVariable:
    """
    Represents a variable in the causal model.

    Attributes:
        name: Unique identifier for the variable
        var_type: One of 'exogenous', 'latent', 'observed', 'derived'
        parents: List of parent variable names
        description: Human-readable description
        domain: Optional (min, max) range for the variable
    """
    name: str
    var_type: str = 'observed'  # 'exogenous', 'latent', 'observed', 'derived'
    parents: List[str] = field(default_factory=list)
    description: str = ''
    domain: Optional[Tuple[float, float]] = None


@dataclass
class StructuralEquation:
    """
    Represents a structural equation in the causal model.

    The function takes a dict of parent values and returns the variable value.

    Attributes:
        target: Name of the variable this equation computes
        function: Callable that takes parent values dict and returns computed value
        description: Human-readable description of the equation
    """
    target: str
    function: Callable[[Dict[str, Any]], Any]
    description: str = ''


@dataclass
class EnvironmentState:
    """
    Complete environment state from SCM sampling.

    Separates visible and latent (hidden) variables.
    """
    # Variables visible to agent
    visible: Dict[str, float] = field(default_factory=dict)

    # Latent variables (hidden from agent)
    latent: Dict[str, float] = field(default_factory=dict)

    # Derived variables (computed from visible/latent)
    derived: Dict[str, float] = field(default_factory=dict)

    def get(self, key: str, default: float = 0.0) -> float:
        """Get any variable by name."""
        if key in self.visible:
            return self.visible[key]
        if key in self.latent:
            return self.latent[key]
        if key in self.derived:
            return self.derived[key]
        return default

    def all_variables(self) -> Dict[str, float]:
        """Get all variables (for logging)."""
        result = {}
        result.update(self.visible)
        result.update(self.latent)
        result.update(self.derived)
        return result

    @property
    def weather_pattern(self) -> float:
        """Convenience accessor for common latent variable."""
        return self.latent.get('weather_pattern', 0.5)


class CausalSCM(ABC):
    """
    Abstract base class for Structural Causal Models.

    Each SCM defines:
    1. What environment variables exist
    2. How they are sampled (causal structure)
    3. How they affect the drone (effects)

    Subclasses implement:
    - sample_environment(): Generate environment state
    - apply_effects(): Write effects to DroneSheet

    Enhanced features:
    - Explicit variable and equation registration via CausalVariable/StructuralEquation
    - do-intervention support for causal reasoning
    - Automatic sampling with topological ordering
    - Multi-format export (text, graph, DOT, JSON Schema)

    Usage:
        scm = MyExperimentSCM(config)
        env = scm.sample_environment()
        scm.apply_effects(drone_sheet, env)
        # DroneSheet now has effects applied

        # do-intervention
        intervened_scm = scm.do('weather_pattern', 0.9)
        env = intervened_scm.sample()
    """

    def __init__(self, config: Dict[str, Any]):
        """
        Initialize SCM with experiment configuration.

        Args:
            config: Experiment configuration dict
        """
        self.config = config
        # Legacy storage (for backward compatibility)
        self._variables: Dict[str, Dict[str, Any]] = {}
        self._causal_edges: List[Tuple[str, str]] = []
        # New enhanced storage
        self._causal_variables: Dict[str, CausalVariable] = {}
        self._equations: Dict[str, StructuralEquation] = {}
        self._interventions: Dict[str, Any] = {}
        self._topological_order: Optional[List[str]] = None

        # Parse SCM config section
        scm_config = config.get('scm', {})
        self._exogenous_config: Dict[str, Dict[str, Any]] = scm_config.get(
            'exogenous_variables', {}
        )
        self._scm_parameters: Dict[str, Any] = scm_config.get('parameters', {})

    # ============================================================
    # Variable and Equation Registration
    # ============================================================

    def register_variable(self, var: CausalVariable) -> None:
        """
        Register a causal variable in the SCM.

        Args:
            var: CausalVariable to register
        """
        self._causal_variables[var.name] = var
        # Also update legacy storage for backward compatibility
        self._variables[var.name] = {
            'name': var.name,
            'type': var.var_type,
            'description': var.description,
            'domain': var.domain,
            'parents': var.parents,
        }
        # Add causal edges from parents
        for parent in var.parents:
            edge = (parent, var.name)
            if edge not in self._causal_edges:
                self._causal_edges.append(edge)
        # Invalidate cached topological order
        self._topological_order = None

    def register_equation(self, eq: StructuralEquation) -> None:
        """
        Register a structural equation in the SCM.

        Args:
            eq: StructuralEquation to register
        """
        self._equations[eq.target] = eq

    def add_variable(
        self,
        name: str,
        var_type: str = 'observed',
        description: str = '',
        **kwargs
    ) -> None:
        """
        Register a variable in the SCM (legacy method).

        For new code, prefer register_variable() with CausalVariable.

        Args:
            name: Variable name
            var_type: 'latent', 'observed', or 'derived'
            description: Human-readable description
            **kwargs: Additional variable properties (parents, domain)
        """
        parents = kwargs.pop('parents', [])
        domain = kwargs.pop('domain', None)
        var = CausalVariable(
            name=name,
            var_type=var_type,
            parents=parents,
            description=description,
            domain=domain,
        )
        self.register_variable(var)

    def add_causal_edge(self, cause: str, effect: str) -> None:
        """
        Add a causal edge to the graph.

        Args:
            cause: Cause variable name
            effect: Effect variable name
        """
        edge = (cause, effect)
        if edge not in self._causal_edges:
            self._causal_edges.append(edge)
        self._topological_order = None

    # ============================================================
    # do-Intervention Support
    # ============================================================

    def do(self, var_name: str, value: Any) -> 'CausalSCM':
        """
        Perform do-intervention: do(X = x).

        Returns a new SCM instance with the intervention applied.
        When sampling, the intervened variable will be fixed to the given value.

        Args:
            var_name: Variable name to intervene on
            value: Value to set the variable to

        Returns:
            New CausalSCM instance with intervention applied
        """
        # Create a deep copy to avoid modifying the original
        intervened = copy.copy(self)
        intervened._interventions = dict(self._interventions)
        intervened._interventions[var_name] = value
        return intervened

    def clear_interventions(self) -> None:
        """Clear all interventions."""
        self._interventions = {}

    def get_interventions(self) -> Dict[str, Any]:
        """Get current interventions."""
        return dict(self._interventions)

    # ============================================================
    # Topological Ordering
    # ============================================================

    def _compute_topological_order(self) -> List[str]:
        """
        Compute topological ordering of variables using Kahn's algorithm.

        Returns:
            List of variable names in topological order
        """
        if self._topological_order is not None:
            return self._topological_order

        # Build adjacency list and in-degree count
        in_degree: Dict[str, int] = {}
        children: Dict[str, List[str]] = {}

        all_vars = set(self._causal_variables.keys())
        for var_name in all_vars:
            in_degree[var_name] = 0
            children[var_name] = []

        for cause, effect in self._causal_edges:
            if cause in all_vars and effect in all_vars:
                children[cause].append(effect)
                in_degree[effect] += 1

        # Find all nodes with no incoming edges
        queue = [v for v in all_vars if in_degree[v] == 0]
        result = []

        while queue:
            node = queue.pop(0)
            result.append(node)
            for child in children.get(node, []):
                in_degree[child] -= 1
                if in_degree[child] == 0:
                    queue.append(child)

        # Check for cycles
        if len(result) != len(all_vars):
            missing = all_vars - set(result)
            raise ValueError(f"Cycle detected in causal graph. Remaining nodes: {missing}")

        self._topological_order = result
        return result

    # ============================================================
    # Automatic Sampling
    # ============================================================

    def sample(self, exogenous_values: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
        """
        Sample all variables according to the causal structure.

        Executes structural equations in topological order.
        Respects any active interventions.

        Args:
            exogenous_values: Optional dict of exogenous variable values.
                              If not provided, _sample_exogenous() is called.

        Returns:
            Dict mapping variable names to sampled values
        """
        if not self._causal_variables:
            # Fall back to sample_environment for backward compatibility
            env = self.sample_environment()
            return env.all_variables()

        values: Dict[str, Any] = {}
        exogenous = exogenous_values or {}

        for var_name in self._compute_topological_order():
            var = self._causal_variables[var_name]

            # Check for intervention
            if var_name in self._interventions:
                values[var_name] = self._interventions[var_name]
                continue

            # Check for provided exogenous value
            if var_name in exogenous:
                values[var_name] = exogenous[var_name]
                continue

            # Sample exogenous variables
            if var.var_type == 'exogenous' or not var.parents:
                values[var_name] = self._sample_exogenous(var_name, var)
                continue

            # Execute structural equation
            if var_name in self._equations:
                eq = self._equations[var_name]
                parent_values = {p: values.get(p) for p in var.parents}
                values[var_name] = eq.function(parent_values)
            else:
                # No equation registered, use default sampling
                values[var_name] = self._sample_exogenous(var_name, var)

        return values

    def _sample_exogenous(self, var_name: str, var: CausalVariable) -> Any:
        """
        Sample an exogenous variable.

        First checks config for distribution specification, then falls back
        to domain-based uniform sampling.

        Args:
            var_name: Variable name
            var: CausalVariable definition

        Returns:
            Sampled value
        """
        # Check if config specifies distribution for this variable
        if var_name in self._exogenous_config:
            return self._sample_from_config(var_name, self._exogenous_config[var_name])

        # Fall back to domain-based sampling
        if var.domain is not None:
            return random.uniform(var.domain[0], var.domain[1])
        return random.random()

    def _sample_from_config(self, var_name: str, var_config: Dict[str, Any]) -> Any:
        """
        Sample a variable based on config specification.

        Supported distributions:
        - uniform: params {low, high}
        - normal: params {mean, std}
        - bernoulli: params {p}
        - constant: params {value}

        Args:
            var_name: Variable name (for error messages)
            var_config: Config dict with 'distribution' and 'params'

        Returns:
            Sampled value
        """
        dist = var_config.get('distribution', 'uniform')
        params = var_config.get('params', {})

        if dist == 'uniform':
            low = params.get('low', 0.0)
            high = params.get('high', 1.0)
            return random.uniform(low, high)
        elif dist == 'normal':
            mean = params.get('mean', 0.0)
            std = params.get('std', 1.0)
            return random.gauss(mean, std)
        elif dist == 'bernoulli':
            p = params.get('p', 0.5)
            return 1.0 if random.random() < p else 0.0
        elif dist == 'constant':
            return params.get('value', 0.0)
        else:
            raise ValueError(f"Unknown distribution '{dist}' for variable '{var_name}'")

    def get_parameter(self, name: str, default: Any = None) -> Any:
        """
        Get a parameter from SCM config.

        Checks scm.parameters first, then falls back to top-level config.

        Args:
            name: Parameter name
            default: Default value if not found

        Returns:
            Parameter value
        """
        if name in self._scm_parameters:
            return self._scm_parameters[name]
        return self.config.get(name, default)

    # ============================================================
    # Export Methods
    # ============================================================

    def describe(self) -> str:
        """
        Return human-readable description of SCM.

        Groups variables by type and shows causal structure.
        """
        lines = [
            f"SCM: {self.__class__.__name__}",
            f"Variables: {len(self._causal_variables) or len(self._variables)}",
            f"Causal edges: {len(self._causal_edges)}",
            "",
        ]

        # Group variables by type
        by_type: Dict[str, List[CausalVariable]] = {
            'exogenous': [],
            'latent': [],
            'observed': [],
            'derived': [],
        }

        if self._causal_variables:
            for var in self._causal_variables.values():
                if var.var_type in by_type:
                    by_type[var.var_type].append(var)
                else:
                    by_type['observed'].append(var)

            for var_type, vars_list in by_type.items():
                if vars_list:
                    lines.append(f"{var_type.title()} Variables:")
                    for var in vars_list:
                        domain_str = f" [{var.domain[0]}, {var.domain[1]}]" if var.domain else ""
                        lines.append(f"  - {var.name}{domain_str}: {var.description}")
                        if var.parents:
                            lines.append(f"    Parents: {', '.join(var.parents)}")
                    lines.append("")
        else:
            # Legacy format
            lines.append("Variables:")
            for name, info in self._variables.items():
                var_type = info.get('type', 'unknown')
                desc = info.get('description', '')
                lines.append(f"  - {name} ({var_type}): {desc}")
            lines.append("")

        lines.append("Causal Structure:")
        for cause, effect in self._causal_edges:
            lines.append(f"  {cause} -> {effect}")

        return "\n".join(lines)

    def get_causal_graph(self) -> Dict[str, Any]:
        """
        Return causal graph structure for visualization.

        Returns:
            Dict with 'nodes', 'edges', 'variables', and 'metadata'
        """
        if self._causal_variables:
            nodes = [
                {
                    'name': var.name,
                    'type': var.var_type,
                    'description': var.description,
                    'domain': var.domain,
                    'parents': var.parents,
                }
                for var in self._causal_variables.values()
            ]
        else:
            nodes = [
                {'name': name, **info}
                for name, info in self._variables.items()
            ]

        edges = [
            {'from': cause, 'to': effect}
            for cause, effect in self._causal_edges
        ]

        return {
            'nodes': nodes,
            'edges': edges,
            'metadata': {
                'scm_name': self.__class__.__name__,
                'num_variables': len(nodes),
                'num_edges': len(edges),
            }
        }

    def to_dot(self) -> str:
        """
        Export causal graph in DOT/Graphviz format.

        Returns:
            DOT format string
        """
        lines = [
            f"digraph {self.__class__.__name__} {{",
            "    rankdir=TB;",
            "    node [shape=ellipse];",
            "",
        ]

        # Add nodes with styling based on type
        if self._causal_variables:
            latent_vars = []
            observed_vars = []
            derived_vars = []

            for var in self._causal_variables.values():
                if var.var_type in ('latent', 'exogenous'):
                    latent_vars.append(var)
                elif var.var_type == 'derived':
                    derived_vars.append(var)
                else:
                    observed_vars.append(var)

            if latent_vars:
                lines.append("    // Latent/Exogenous variables (dashed)")
                for var in latent_vars:
                    label = f"{var.name}\\n({var.var_type})"
                    lines.append(f'    {var.name} [label="{label}", style=dashed];')
                lines.append("")

            if observed_vars:
                lines.append("    // Observed variables")
                for var in observed_vars:
                    lines.append(f'    {var.name} [label="{var.name}"];')
                lines.append("")

            if derived_vars:
                lines.append("    // Derived variables (filled)")
                for var in derived_vars:
                    lines.append(f'    {var.name} [label="{var.name}", style=filled, fillcolor=lightgray];')
                lines.append("")
        else:
            for name in self._variables.keys():
                lines.append(f'    {name};')
            lines.append("")

        # Add edges
        lines.append("    // Edges")
        for cause, effect in self._causal_edges:
            lines.append(f"    {cause} -> {effect};")

        lines.append("}")
        return "\n".join(lines)

    def to_json_schema(self) -> Dict[str, Any]:
        """
        Export SCM as JSON Schema format.

        Returns:
            Dict with full SCM specification
        """
        variables = {}

        if self._causal_variables:
            for var in self._causal_variables.values():
                var_spec = {
                    'type': var.var_type,
                    'exogenous': var.var_type == 'exogenous' or not var.parents,
                    'description': var.description,
                    'parents': var.parents,
                }
                if var.domain:
                    var_spec['domain'] = list(var.domain)
                if var.name in self._equations:
                    var_spec['equation'] = self._equations[var.name].description
                variables[var.name] = var_spec
        else:
            for name, info in self._variables.items():
                variables[name] = {
                    'type': info.get('type', 'observed'),
                    'exogenous': not info.get('parents', []),
                    'description': info.get('description', ''),
                    'parents': info.get('parents', []),
                }
                if info.get('domain'):
                    variables[name]['domain'] = list(info['domain'])

        return {
            'name': self.__class__.__name__,
            'variables': variables,
            'edges': [
                [cause, effect]
                for cause, effect in self._causal_edges
            ],
        }

    # ============================================================
    # Abstract Methods (must be implemented by subclasses)
    # ============================================================

    @abstractmethod
    def sample_environment(self, equipment: dict = None) -> EnvironmentState:
        """
        Sample environment state according to causal structure.

        This method should:
        1. Sample latent variables first
        2. Sample visible variables conditioned on latent
        3. Compute derived variables

        Args:
            equipment: Optional equipment choices from agent (e.g., flight_profile).
                       SCMs can use this to let agent influence environment variables.

        Returns:
            EnvironmentState with all variables
        """
        pass

    @abstractmethod
    def apply_effects(
        self,
        sheet: DroneSheet,
        env: EnvironmentState
    ) -> None:
        """
        Apply environment effects to DroneSheet.

        This is the ONLY way SCM affects the game!

        Implementation should:
        1. Compute effects from environment state
        2. Call sheet.apply_environment_effects(effects)

        Args:
            sheet: DroneSheet middleware to write to
            env: EnvironmentState from sample_environment()
        """
        pass


class BaseSCM(CausalSCM):
    """
    Base SCM implementation with minimal common functionality.

    Provides:
    - apply_effects() that delegates to abstract _compute_effects()
    - Helper method for linear interpolation

    Subclasses MUST implement:
    - sample_environment(): Define how to sample environment state
    - _compute_effects(): Define the specific causal mechanism

    This base class is intentionally minimal. All experiment-specific
    logic (latent variables, causal structure, etc.) belongs in subclasses.
    """

    def __init__(self, config: Dict[str, Any]):
        super().__init__(config)

    def apply_effects(
        self,
        sheet: DroneSheet,
        env: EnvironmentState
    ) -> None:
        """
        Apply effects by delegating to _compute_effects().

        This is the standard flow - subclasses implement _compute_effects().
        """
        effects = self._compute_effects(sheet, env)
        sheet.apply_environment_effects(effects)

    @abstractmethod
    def _compute_effects(
        self,
        sheet: DroneSheet,
        env: EnvironmentState
    ) -> EnvironmentEffects:
        """
        Compute environment effects for this specific causal mechanism.

        This is where each experiment defines its unique causal structure.
        Subclasses MUST implement this method.

        Args:
            sheet: DroneSheet to read drone state from (if needed)
            env: EnvironmentState with visible/latent/derived variables

        Returns:
            EnvironmentEffects to be applied to DroneSheet
        """
        pass

    # ========== Helper methods for subclasses ==========

    @staticmethod
    def _interpolate(
        t: float,
        value_at_0: float,
        value_at_1: float
    ) -> float:
        """
        Linear interpolation helper.

        Args:
            t: Interpolation parameter (0 to 1)
            value_at_0: Value when t=0
            value_at_1: Value when t=1

        Returns:
            Interpolated value
        """
        return value_at_0 * (1 - t) + value_at_1 * t