File size: 2,876 Bytes
e369f76
 
 
f59b79f
e369f76
 
ce0550f
f59b79f
ce0550f
 
f59b79f
 
 
 
 
e369f76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f59b79f
 
 
 
 
 
 
 
 
105b3fb
f59b79f
 
 
 
 
 
 
 
 
 
e369f76
 
 
 
 
f59b79f
 
 
e369f76
f59b79f
 
 
 
 
 
105b3fb
f59b79f
 
 
e369f76
 
 
 
f59b79f
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
import gradio as gr
import json
import os
import matplotlib.pyplot as plt
from simulation import RFTSimulation
from utils import export_json, seed_everything

# Detect Colab
IN_COLAB = 'COLAB_GPU' in os.environ or 'Google' in os.environ.get('COLAB_CLOUD_PLATFORM', '')
if IN_COLAB:
    from IPython.display import Image, display

# -----------------------------
# Core Simulation Function
# -----------------------------

def run_simulation_and_plot(num_agents, steps, mutation_rate, drift_rate, seed=None):
    if seed is not None:
        seed_everything(seed)

    simulation = RFTSimulation(num_agents, steps, mutation_rate, drift_rate, save_plots=True)
    all_agents_history, coherence_list, stability_list = simulation.run()

    final_agent_states = []
    for i, agent in enumerate(simulation.agents):
        final_agent_states.append({
            'id': i + 1,
            'phi': agent.phi,
            'tau_eff': agent.tau_eff,
            'tier': agent.tier,
            'fitness': agent.fitness
        })

    json_filename = "final_agent_states.json"
    export_json(final_agent_states, json_filename)

    plot_filenames = [
        'phi_plot.png',
        'tau_plot.png',
        'fitness_plot.png',
        'coherence_plot.png',
        'stability_plot.png'
    ]

    return (*plot_filenames, json_filename)

# -----------------------------
# PREMIUM UI (Always Unlocked)
# -----------------------------

with gr.Blocks(title="RFT Agent Simulation Engine — Premium") as iface:

    gr.Markdown("# 🔥 RFT Agent Simulation Engine — Premium Edition")
    gr.Markdown("Full‑power simulation engine with extended limits, advanced controls, and unrestricted exports.")

    with gr.Group():
        gr.Markdown("### Premium Simulation Controls")

        num_agents = gr.Slider(1, 200, value=10, step=1, label="Number of Agents (Premium)")
        steps = gr.Slider(10, 20000, value=500, step=10, label="Steps (Premium)")
        mutation_rate = gr.Slider(0.001, 1.0, value=0.05, step=0.001, label="Mutation Rate")
        drift_rate = gr.Slider(0.001, 1.0, value=0.02, step=0.001, label="Drift Rate")
        seed = gr.Number(value=42, label="Random Seed")

    outputs = [
        gr.Image(label="Phi Plot"),
        gr.Image(label="Tau Effective Plot"),
        gr.Image(label="Fitness Plot"),
        gr.Image(label="Coherence Plot"),
        gr.Image(label="Stability Plot"),
        gr.File(label="Exported Final Agent States (JSON)")
    ]

    run_button = gr.Button("🚀 Run Premium Simulation")

    run_button.click(
        run_simulation_and_plot,
        inputs=[num_agents, steps, mutation_rate, drift_rate, seed],
        outputs=outputs
    )


# -----------------------------
# Launch Logic
# -----------------------------

if IN_COLAB:
    print("Running in Colab development mode (skipping Gradio launch).")
else:
    iface.launch()