File size: 10,719 Bytes
2ca80ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test Material-Field Engine with Reference Queries

Demonstrates system behavior with fixed reference queries:
- Simple factual prompts
- Common misconceptions
- Basic formula recall
- Date recall

Shows how elastic modulus mode affects exclusion behavior under a fixed substrate.
"""

import sys
from pathlib import Path

if hasattr(sys.stdout, "reconfigure"):
    try:
        sys.stdout.reconfigure(encoding="utf-8", errors="replace")
    except Exception:
        pass

sys.path.insert(0, str(Path(__file__).parent))

from material_field_engine import (
    VerifiedSubstrate, Vector2D, MaterialFieldEngine
)


def test_science_fact(mode='multiplicative', sigma=0.5):
    """
    Query: 'What do plants need for photosynthesis?'
    Expected: Sunlight, water, carbon dioxide
    """
    print("\n" + "=" * 80)
    print("SCIENCE: What do plants need for photosynthesis?")
    print("=" * 80)
    print(f"Mode: {mode}, Οƒ={sigma}")

    # Substrate: Verified science facts
    substrate = VerifiedSubstrate(
        elastic_modulus_mode=mode,
        elastic_modulus_sigma=sigma
    )

    # Verified answers (embeddings simulated as 2D for demo)
    substrate.add_verified_state(Vector2D(x=0.90, y=0.88, properties=None))  # "sunlight, water, CO2"

    # Candidates:
    # - Correct answer (close to substrate)
    # - Partially correct (medium distance)
    # - Common misconception (far from substrate)
    # - Creative but wrong (far, different direction)
    candidates = [
        (0.90, 0.88),  # Correct: sunlight, water, CO2
        (0.70, 0.65),  # Partial: mentions sunlight but incomplete
        (0.40, 0.35),  # Misconception: "plants eat soil"
        (0.15, 0.80),  # Creative wrong: "moonlight and air"
    ]

    engine = MaterialFieldEngine(
        substrate,
        lambda_min=0.35,
        lambda_max=1.00,
        inference_steps=8
    )

    engine.initialize_candidates(candidates)

    print("\nCandidate Knowledge States:")
    labels = [
        "Correct (sunlight+water+CO2)",
        "Partial (sunlight mentioned)",
        "Misconception (plants eat soil)",
        "Creative wrong (moonlight)"
    ]

    for i, (v, label) in enumerate(zip(engine.candidate_vectors, labels)):
        dist = v.distance_to(substrate.states[0])
        print(f"  {i}. {label:<30} | E={v.properties.elastic_modulus:.3f} | "
              f"Οƒ_y={v.properties.yield_strength:.3f} | dist={dist:.3f}")

    results = engine.run_inference()

    print("\nResult:")
    if results['final_output']:
        winner_idx = next(i for i, c in enumerate(candidates)
                         if (c[0], c[1]) == (results['final_output'].x, results['final_output'].y))
        print(f"  Selected: {labels[winner_idx]}")
        print(f"  E={results['final_output'].properties.elastic_modulus:.3f}")
    else:
        print("  ABSTAINED (no candidate met grounding threshold)")

    print(f"  Excluded: {results['total_excluded']}")
    # Intentionally omit interpretive flags in stdout.


def test_geography_fact(mode='multiplicative', sigma=0.5):
    """
    Query: 'What is the capital of Texas?'
    Expected: Austin
    """
    print("\n" + "=" * 80)
    print("GEOGRAPHY: What is the capital of Texas?")
    print("=" * 80)
    print(f"Mode: {mode}, Οƒ={sigma}")

    substrate = VerifiedSubstrate(
        elastic_modulus_mode=mode,
        elastic_modulus_sigma=sigma
    )

    # Verified answer
    substrate.add_verified_state(Vector2D(x=0.85, y=0.82, properties=None))  # "Austin"

    # Candidates:
    candidates = [
        (0.85, 0.82),  # Correct: Austin
        (0.75, 0.70),  # Common error: Houston (largest city)
        (0.40, 0.35),  # Wrong: Dallas
        (0.20, 0.15),  # Very wrong: Los Angeles (not even in Texas!)
    ]

    engine = MaterialFieldEngine(
        substrate,
        lambda_min=0.35,
        lambda_max=1.00,
        inference_steps=8
    )

    engine.initialize_candidates(candidates)

    print("\nCandidate Answers:")
    labels = [
        "Correct (Austin)",
        "Common error (Houston - largest city)",
        "Wrong (Dallas)",
        "Very wrong (Los Angeles)"
    ]

    for i, (v, label) in enumerate(zip(engine.candidate_vectors, labels)):
        dist = v.distance_to(substrate.states[0])
        print(f"  {i}. {label:<38} | E={v.properties.elastic_modulus:.3f} | "
              f"dist={dist:.3f}")

    results = engine.run_inference()

    print("\nResult:")
    if results['final_output']:
        winner_idx = next(i for i, c in enumerate(candidates)
                         if (c[0], c[1]) == (results['final_output'].x, results['final_output'].y))
        print(f"  Selected: {labels[winner_idx]}")
    else:
        print("  ABSTAINED")
    print(f"  Excluded: {results['total_excluded']}")


def test_math_concept(mode='multiplicative', sigma=0.5):
    """
    Query: 'What is the area formula for a rectangle?'
    Expected: length Γ— width
    """
    print("\n" + "=" * 80)
    print("MATH: What is the area formula for a rectangle?")
    print("=" * 80)
    print(f"Mode: {mode}, Οƒ={sigma}")

    substrate = VerifiedSubstrate(
        elastic_modulus_mode=mode,
        elastic_modulus_sigma=sigma
    )

    # Verified formula
    substrate.add_verified_state(Vector2D(x=0.92, y=0.90, properties=None))  # "length Γ— width"

    candidates = [
        (0.92, 0.90),  # Correct: length Γ— width
        (0.70, 0.68),  # Confused with perimeter: 2(l+w)
        (0.45, 0.42),  # Wrong: length + width
        (0.25, 0.20),  # Very wrong: confusing with volume
    ]

    engine = MaterialFieldEngine(
        substrate,
        lambda_min=0.40,
        lambda_max=1.10,
        inference_steps=8
    )

    engine.initialize_candidates(candidates)

    print("\nCandidate Formulas:")
    labels = [
        "Correct (length Γ— width)",
        "Perimeter confusion (2(l+w))",
        "Wrong operation (length + width)",
        "Volume confusion (3D thinking)"
    ]

    for i, (v, label) in enumerate(zip(engine.candidate_vectors, labels)):
        print(f"  {i}. {label:<35} | E={v.properties.elastic_modulus:.3f}")

    results = engine.run_inference()

    print("\nResult:")
    if results['final_output']:
        winner_idx = next(i for i, c in enumerate(candidates)
                         if (c[0], c[1]) == (results['final_output'].x, results['final_output'].y))
        print(f"  Selected: {labels[winner_idx]}")
    else:
        print("  ABSTAINED")
    print(f"  Excluded: {results['total_excluded']}")


def test_historical_fact(mode='multiplicative', sigma=0.5):
    """
    Query: 'When did Christopher Columbus reach the Americas?'
    Expected: 1492
    """
    print("\n" + "=" * 80)
    print("HISTORY: When did Columbus reach the Americas?")
    print("=" * 80)
    print(f"Mode: {mode}, Οƒ={sigma}")

    substrate = VerifiedSubstrate(
        elastic_modulus_mode=mode,
        elastic_modulus_sigma=sigma
    )

    # Verified date
    substrate.add_verified_state(Vector2D(x=0.88, y=0.86, properties=None))  # "1492"

    candidates = [
        (0.88, 0.86),  # Correct: 1492
        (0.75, 0.70),  # Close: 1490s range
        (0.50, 0.45),  # Common error: 1776 (confusing with US independence)
        (0.30, 0.25),  # Wrong century: 1500s
        (0.10, 0.12),  # Very wrong: 1942 (digit confusion)
    ]

    engine = MaterialFieldEngine(
        substrate,
        lambda_min=0.35,
        lambda_max=1.00,
        inference_steps=8
    )

    engine.initialize_candidates(candidates)

    print("\nCandidate Dates:")
    labels = [
        "Correct (1492)",
        "Approximate (1490s)",
        "Confusion (1776 - US independence)",
        "Wrong century (1500s)",
        "Digit swap (1942)"
    ]

    for i, (v, label) in enumerate(zip(engine.candidate_vectors, labels)):
        print(f"  {i}. {label:<38} | E={v.properties.elastic_modulus:.3f}")

    results = engine.run_inference()

    print("\nResult:")
    if results['final_output']:
        winner_idx = next(i for i, c in enumerate(candidates)
                         if (c[0], c[1]) == (results['final_output'].x, results['final_output'].y))
        print(f"  Selected: {labels[winner_idx]}")
    else:
        print("  ABSTAINED")
    print(f"  Excluded: {results['total_excluded']}")


def compare_modes():
    """Compare how different elastic modulus modes affect fixed reference queries"""
    print("\n" + "=" * 80)
    print("MODE COMPARISON: How does mode selection affect reference queries?")
    print("=" * 80)

    modes = [
        ('cosine', 0.5, "Cosine (direction only)"),
        ('multiplicative', 0.4, "Multiplicative Οƒ=0.4 (tight - good for facts)"),
        ('multiplicative', 0.7, "Multiplicative Οƒ=0.7 (loose - allows exploration)"),
    ]

    for mode, sigma, description in modes:
        print(f"\n{'─' * 80}")
        print(f"Testing with: {description}")
        print(f"{'─' * 80}")

        test_science_fact(mode, sigma)


if __name__ == "__main__":
    print("""
╔══════════════════════════════════════════════════════════════════════════════╗
β•‘                                                                              β•‘
β•‘                    Testing with Reference Queries                            β•‘
β•‘                                                                              β•‘
β•‘  Demonstrates material-field governance with simple example queries.        β•‘
β•‘  Shows how elastic modulus mode affects factual recall vs. exploration.     β•‘
β•‘                                                                              β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
""")

    # Default: use multiplicative mode with a fixed sigma for repeatability
    mode = 'multiplicative'
    sigma = 0.45

    if len(sys.argv) > 1:
        if sys.argv[1] == 'compare':
            compare_modes()
            sys.exit(0)
        elif sys.argv[1] == 'cosine':
            mode = 'cosine'
        elif sys.argv[1] == 'tight':
            sigma = 0.3
        elif sys.argv[1] == 'loose':
            sigma = 0.7

    print(f"\nUsing mode: {mode}, Οƒ={sigma}")
    print("  (Run with 'compare' to see all modes side-by-side)\n")

    # Run all example tests
    test_science_fact(mode, sigma)
    test_geography_fact(mode, sigma)
    test_math_concept(mode, sigma)
    test_historical_fact(mode, sigma)