-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterse_fields.py
199 lines (166 loc) · 6.94 KB
/
terse_fields.py
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
from typing import Protocol, Callable, Union, Any, TypeVar
from enum import Enum
from dataclasses import dataclass
import hashlib
import cmath
#------------------------------------------------------------------------------
# Type Definitions
#------------------------------------------------------------------------------
"""
Type Definitions for Morphological Source Code (MSC).
MSC is built upon T (Types), V (Values), and C (Computations) acting in
mutually transforming, Noetherian symmetry-preserving roles.
"""
T = TypeVar('T')
V = TypeVar('V', bound=Union[int, float, str, bool, list, dict, tuple, set, object, Callable, type])
C = TypeVar('C', bound=Callable[..., Any])
#------------------------------------------------------------------------------
# Enums and Data Classes for Symmetries and Manifolds
#------------------------------------------------------------------------------
class Symmetry(Enum):
TRANSLATION = "Translation"
ROTATION = "Rotation"
PHASE = "Phase"
class Conservation(Enum):
INFORMATION = "Information"
COHERENCE = "Coherence"
BEHAVIORAL = "Behavioral"
@dataclass
class State:
type_space: T
value_space: V
computation_space: C
symmetry: Symmetry
conservation: Conservation
#------------------------------------------------------------------------------
# Protocols - Structures for Feedback and Dynamics
#------------------------------------------------------------------------------
class Field(Protocol):
"""
Defines a dynamic field space, leveraging symmetries and manifold mappings.
"""
def interact(self, state: State) -> State:
...
class Gauge:
"""
Manages the field's influence on type, value, and computation manifolds.
"""
def __init__(self, local: State, global_: State, emergent: State):
self.fields = [local, global_, emergent]
def apply_transformation(self, state: State) -> State:
transformed_state = state
for field in self.fields:
transformed_state = self._combine_states(transformed_state, field)
return transformed_state
def _combine_states(self, state_a: State, state_b: State) -> State:
# Apply computation from state_b to the value space of state_a
new_value = [state_b.computation_space(val) for val in state_a.value_space]
return State(
type_space=state_a.type_space,
value_space=new_value,
computation_space=state_a.computation_space,
symmetry=state_a.symmetry,
conservation=state_b.conservation,
)
#------------------------------------------------------------------------------
# Core System - Deamon-Core
#------------------------------------------------------------------------------
class MorphologicalKernel:
"""
Central to running feedback-driven transformations.
Interprets configuration space in accordance with Noetherian symmetries.
"""
def __init__(self):
self.state_history = []
def run(self, initial_state: State, gauge: Gauge, steps: int) -> State:
current_state = initial_state
for _ in range(steps):
current_state = gauge.apply_transformation(current_state)
self.state_history.append(current_state)
return current_state
def __repr__(self):
return f"Kernel with {len(self.state_history)} state transitions."
#------------------------------------------------------------------------------
# Example Usage
#------------------------------------------------------------------------------
def visualize_state_history(state_history):
"""
Visualizes the evolution of the state transformations over time.
This function takes the state history from the MorphologicalKernel's execution
and generates a simple line plot representing the "value space" at each
transformation step. This is a simplistic visualization to help illustrate
how the value space evolves, a key concept in understanding transformations
in this framework.
Parameters:
- state_history: A list of State objects created during the kernel's run.
Each State object represents the system's configuration at a specific point
in time.
Returns:
- Matplotlib Figure showcasing the value space over time.
Raises:
- ValueError: If the state_history is not provided or is empty.
"""
if not state_history:
raise ValueError("state_history cannot be empty!")
values = [state.value_space for state in state_history]
#plt.plot(values)
#plt.title('Evolution of Value Space')
#plt.xlabel('Step')
#plt.ylabel('Value Space')
#plt.grid(True)
#plt.show()
def main():
"""
Main Execution and Example of Morphological Kernel.
This function outlines the setup and execution process for the Morphological Kernel.
It showcases how initial states and Gauge configurations are used to propagate system
transformations through the invocation of the kernel's `run` method. Additionally,
it provides a demonstration of visualizing the resulting state evolution.
Steps included:
1. Definition of the initial state as a combination of type, value, and computation
spaces, decorated with symmetry and conservation laws.
2. Setup of Gauge states: local, global, and emergent, each providing specific
transformation rules for manipulating system configurations.
3. Initialization and execution of the Morphological Kernel, running a series of
transformations over the specified steps.
4. Display of the final state and visualization of the state history to illustrate
the cumulative impact of transformation steps.
Outputs:
- Terminal output of the final state configuration after running the kernel.
- A visual plot showing Value Space evolution for ease of conceptual understanding.
"""
initial_state = State(
type_space=lambda x: x,
value_space=[0],
computation_space=lambda x: x,
symmetry=Symmetry.TRANSLATION,
conservation=Conservation.INFORMATION
)
local_gauge = State(
type_space=lambda x: x,
value_space=[1],
computation_space=lambda x: x + 1,
symmetry=Symmetry.ROTATION,
conservation=Conservation.COHERENCE
)
global_gauge = State(
type_space=lambda x: x,
value_space=[4],
computation_space=lambda x: 2 * x,
symmetry=Symmetry.PHASE,
conservation=Conservation.BEHAVIORAL
)
emergent_gauge = State(
type_space=lambda x: x,
value_space=[0],
computation_space=lambda x: x,
symmetry=Symmetry.TRANSLATION,
conservation=Conservation.INFORMATION
)
gauge = Gauge(local=local_gauge, global_=global_gauge, emergent=emergent_gauge)
kernel = MorphologicalKernel()
final_state = kernel.run(initial_state, gauge, steps=10)
print(f"Final state: {final_state}")
visualize_state_history(kernel.state_history)
if __name__ == '__main__':
main()