-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealms.py
More file actions
228 lines (188 loc) · 9.35 KB
/
realms.py
File metadata and controls
228 lines (188 loc) · 9.35 KB
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
"""
Universal Binary Principle (UBP) Framework v3.2+ - Realms Module
Author: Euan Craig, New Zealand
Date: 20 August 2025
This module defines the concept of 'Realms' within the UBP framework,
which are distinct computational environments or domains of reality, each
governed by specific UBP constants, CRVs, and computational geometries.
Realms provide contextual filters and operational parameters for OffBits
and Bitfields.
"""
import numpy as np
from typing import Dict, Any, Tuple, Optional, List
from dataclasses import dataclass, field
# Corrected import: UBPConstants is in system_constants, not core_v2
# We will primarily use constants from the UBPConfig object for consistency.
from ubp_config import get_config, UBPConfig, RealmConfig
@dataclass
class Realm:
"""
Represents a specific computational realm within the UBP framework.
Each realm has its own set of base parameters, CRVs, and rules.
"""
name: str
config: RealmConfig
is_active: bool = True
current_crv: float = field(init=False)
current_wavelength: float = field(init=False)
meta: Dict[str, Any] = field(default_factory=dict)
_ubp_global_config: UBPConfig = field(init=False) # Store a reference to the global UBPConfig
def __post_init__(self):
# Get the global UBPConfig instance to access system constants consistently
self._ubp_global_config = get_config()
# Initialize current_crv and current_wavelength from config
self.current_crv = self.config.main_crv
# Calculate wavelength using the speed of light from the global UBPConfig
if self.current_crv > self._ubp_global_config.constants.EPSILON_UBP:
self.current_wavelength = self._ubp_global_config.constants.SPEED_OF_LIGHT / self.current_crv
else:
self.current_wavelength = float('inf') # Or some other appropriate value
self.meta.update({
"platonic_solid": self.config.platonic_solid,
"coordination_number": self.config.coordination_number,
"spatial_coherence_baseline": self.config.spatial_coherence,
"temporal_coherence_baseline": self.config.temporal_coherence,
"nrci_baseline": self.config.nrci_baseline,
"lattice_type": self.config.lattice_type,
"optimization_factor": self.config.optimization_factor
})
print(f"Realm '{self.name}' initialized. Main CRV: {self.current_crv:.2e} Hz")
def activate(self):
"""Activates the realm, making it available for computations."""
self.is_active = True
print(f"Realm '{self.name}' activated.")
def deactivate(self):
"""Deactivates the realm."""
self.is_active = False
print(f"Realm '{self.name}' deactivated.")
def update_crv(self, new_crv: float, reason: str = "manual_update"):
"""
Updates the current CRV for the realm. This could be dynamic based on
CRV database selections or system conditions.
"""
self.current_crv = new_crv
# Re-calculate wavelength using constants from the global UBPConfig
if new_crv > self._ubp_global_config.constants.EPSILON_UBP:
self.current_wavelength = self._ubp_global_config.constants.SPEED_OF_LIGHT / new_crv
else:
self.current_wavelength = float('inf') # Or some other appropriate value
self.meta["last_crv_update_reason"] = reason
print(f"Realm '{self.name}' CRV updated to {self.current_crv:.2e} Hz (Reason: {reason})")
def get_realm_parameters(self) -> Dict[str, Any]:
"""Returns a dictionary of current operational parameters for the realm."""
params = {
"name": self.name,
"is_active": self.is_active,
"current_crv": self.current_crv,
"current_wavelength": self.current_wavelength,
}
params.update(self.meta)
return params
def __str__(self):
return (f"Realm(name='{self.name}', active={self.is_active}, "
f"CRV={self.current_crv:.2e} Hz, Wavelength={self.current_wavelength:.2e} m)")
class RealmManager:
"""
Manages the collection of available realms, their activation status,
and provides methods for selecting the most appropriate realm for
a given computational task.
"""
def __init__(self):
self.realms: Dict[str, Realm] = {}
self.ubp_config = get_config() # Get the global UBPConfig instance once
self._initialize_realms_from_config()
print(f"RealmManager initialized with {len(self.realms)} realms.")
def _initialize_realms_from_config(self):
"""Initializes realms based on the UBPConfig."""
for realm_name, realm_cfg in self.ubp_config.realms.items():
self.add_realm(realm_cfg)
def add_realm(self, realm_config: RealmConfig):
"""Adds a new realm to the manager using its configuration."""
if realm_config.name.lower() in self.realms:
print(f"Warning: Realm '{realm_config.name}' already exists. Overwriting.")
# Pass the RealmConfig directly to Realm as its 'config' field
self.realms[realm_config.name.lower()] = Realm(name=realm_config.name, config=realm_config)
print(f"Added realm: {realm_config.name}")
def get_realm(self, name: str) -> Optional[Realm]:
"""Retrieves a realm by its name."""
return self.realms.get(name.lower())
def get_active_realms(self) -> List[Realm]:
"""Returns a list of all currently active realms."""
return [realm for realm in self.realms.values() if realm.is_active]
def select_optimal_realm(self, data_characteristics: Dict[str, Any]) -> Optional[Realm]:
"""
Selects the most optimal realm based on data characteristics.
This is a placeholder for a more sophisticated selection algorithm
that would consider CRV resonance, computational load, etc.
"""
# For now, a simplified selection based on frequency, prioritizing active realms
target_freq = data_characteristics.get('frequency', 0.0)
best_realm = None
min_freq_diff = float('inf')
active_realms = self.get_active_realms()
if not active_realms:
print("No active realms available for selection.")
return None
# Simple greedy selection based on closest main_crv to target_freq
for realm in active_realms:
diff = abs(realm.current_crv - target_freq)
if diff < min_freq_diff:
min_freq_diff = diff
best_realm = realm
if best_realm:
print(f"Selected optimal realm: {best_realm.name} (closest CRV to {target_freq:.2e} Hz)")
else:
print("Could not select an optimal realm.")
return best_realm
def __str__(self):
return f"RealmManager(num_realms={len(self.realms)}, active_realms={len(self.get_active_realms())})"
if __name__ == "__main__":
print("--- Testing Realms Module ---")
# Ensure config is initialized (e.g., in development mode for smaller dimensions)
from ubp_config import get_config
get_config(environment="development")
realm_manager = RealmManager()
print(realm_manager)
quantum_realm = realm_manager.get_realm("quantum")
if quantum_realm:
print(f"\nQuantum Realm details: {quantum_realm}")
print(f" CRV: {quantum_realm.current_crv}")
print(f" Wavelength: {quantum_realm.current_wavelength}")
print(f" Platonic Solid: {quantum_realm.meta['platonic_solid']}")
# Test updating CRV
quantum_realm.update_crv(1.0e13, "dynamic_adjustment")
print(f"Updated Quantum Realm: {quantum_realm}")
em_realm = realm_manager.get_realm("electromagnetic")
if em_realm:
print(f"\nEM Realm details: {em_realm}")
em_realm.deactivate()
print(f"EM Realm deactivated: {em_realm.is_active}")
print(f"\nActive realms: {[r.name for r in realm_manager.get_active_realms()]}")
# Test optimal realm selection
print("\n--- Testing Realm Selection ---")
# Scenario 1: Target frequency matches Quantum realm
data_chars_1 = {"frequency": 1.0e13, "complexity": 0.7}
selected_realm_1 = realm_manager.select_optimal_realm(data_chars_1)
if selected_realm_1:
print(f"Selected realm for data_chars_1: {selected_realm_1.name}")
assert selected_realm_1.name == "quantum"
# Scenario 2: Target frequency matches Optical realm
data_chars_2 = {"frequency": 5.0e14, "noise_level": 0.05}
selected_realm_2 = realm_manager.select_optimal_realm(data_chars_2)
if selected_realm_2:
print(f"Selected realm for data_chars_2: {selected_realm_2.name}")
assert selected_realm_2.name == "optical"
# Scenario 3: No active realms matching
if em_realm:
em_realm.deactivate() # Ensure it's deactivated
# Deactivate all active realms for this test
for r in realm_manager.get_active_realms():
r.deactivate()
data_chars_3 = {"frequency": 1.0e9} # Example for a deactivated realm
selected_realm_3 = realm_manager.select_optimal_realm(data_chars_3)
print(f"Selected realm for data_chars_3 (all realms deactivated): {selected_realm_3}")
assert selected_realm_3 is None
# Re-activate a realm for further testing
if quantum_realm:
quantum_realm.activate()
print("\n✅ Realms module test completed successfully!")