File size: 5,459 Bytes
17b7ba4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Budget Controller for fair comparison in Decomp+SR pipeline.



Ensures all SR methods use identical:

- Time budget (wall-clock limit)

- Complexity budget (max nodes, max depth)

- Operator set

"""

from __future__ import annotations

import time
import signal
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional
from contextlib import contextmanager


@dataclass
class BudgetConfig:
    """Configuration for fair comparison budgets."""

    # Time budget
    time_budget_sec: float = 60.0

    # Complexity budget
    max_nodes: int = 50
    max_depth: int = 10

    # Operator set (must be identical for all methods)
    operators: List[str] = field(default_factory=lambda: [
        'add', 'sub', 'mul', 'div', 'sin', 'cos', 'exp'
    ])

    def to_dict(self) -> Dict[str, Any]:
        return {
            'time_budget_sec': self.time_budget_sec,
            'max_nodes': self.max_nodes,
            'max_depth': self.max_depth,
            'operators': self.operators.copy(),
        }


class TimeoutError(Exception):
    """Raised when operation exceeds time budget."""
    pass


class BudgetController:
    """

    Controls time and complexity budgets for fair comparison.



    Usage:

        controller = BudgetController(config)

        with controller.time_limit():

            result = sr_method.fit(t, y)

        controller.check_complexity(result.expression)

    """

    def __init__(self, config: Optional[BudgetConfig] = None):
        self.config = config or BudgetConfig()
        self._start_time: Optional[float] = None
        self._timed_out: bool = False

    @contextmanager
    def time_limit(self):
        """Context manager for time-limited execution."""
        self._start_time = time.time()
        self._timed_out = False

        def handler(signum, frame):
            self._timed_out = True
            raise TimeoutError(f"Exceeded time budget of {self.config.time_budget_sec}s")

        # Set alarm (Unix only)
        old_handler = signal.signal(signal.SIGALRM, handler)
        signal.alarm(int(self.config.time_budget_sec) + 1)

        try:
            yield
        except TimeoutError:
            self._timed_out = True
            raise
        finally:
            signal.alarm(0)
            signal.signal(signal.SIGALRM, old_handler)

    def elapsed_time(self) -> float:
        """Get elapsed time since time_limit started."""
        if self._start_time is None:
            return 0.0
        return time.time() - self._start_time

    def check_complexity(self, expression: str) -> Dict[str, Any]:
        """

        Check if expression meets complexity budget.



        Returns:

            Dict with 'nodes', 'depth', 'within_budget' fields.

        """
        nodes = self._count_nodes(expression)
        depth = self._count_depth(expression)

        return {
            'nodes': nodes,
            'depth': depth,
            'within_budget': (nodes <= self.config.max_nodes and
                            depth <= self.config.max_depth),
            'max_nodes': self.config.max_nodes,
            'max_depth': self.config.max_depth,
        }

    def _count_nodes(self, expr: str) -> int:
        """Count nodes in expression (simple approximation)."""
        if not expr:
            return 0
        # Count operators and operands
        import re
        tokens = re.findall(r'[a-zA-Z_][a-zA-Z0-9_]*|\d+\.?\d*|[+\-*/()]', expr)
        return len([t for t in tokens if t not in '()'])

    def _count_depth(self, expr: str) -> int:
        """Count max nesting depth in expression."""
        if not expr:
            return 0
        max_depth = 0
        current = 0
        for c in expr:
            if c == '(':
                current += 1
                max_depth = max(max_depth, current)
            elif c == ')':
                current -= 1
        return max_depth

    def get_gplearn_params(self) -> Dict[str, Any]:
        """Get GPlearn-compatible parameters from budget."""
        return {
            'function_set': self._convert_operators_gplearn(),
            'init_depth': (2, min(6, self.config.max_depth)),
            'max_samples': 1.0,
        }

    def get_pysr_params(self) -> Dict[str, Any]:
        """Get PySR-compatible parameters from budget."""
        return {
            'binary_operators': ['+', '-', '*', '/'],
            'unary_operators': ['sin', 'cos', 'exp'],
            'maxsize': self.config.max_nodes,
            'timeout_in_seconds': int(self.config.time_budget_sec),
        }

    def _convert_operators_gplearn(self) -> List[str]:
        """Convert operator names to GPlearn format."""
        mapping = {
            'add': 'add', '+': 'add',
            'sub': 'sub', '-': 'sub',
            'mul': 'mul', '*': 'mul',
            'div': 'div', '/': 'div',
            'sin': 'sin', 'cos': 'cos',
            'exp': 'exp', 'log': 'log',
            'sqrt': 'sqrt', 'abs': 'abs',
        }
        result = []
        for op in self.config.operators:
            if op.lower() in mapping:
                gp_op = mapping[op.lower()]
                if gp_op not in result:
                    result.append(gp_op)
        return result

    @property
    def timed_out(self) -> bool:
        return self._timed_out