direct-evolution-together 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- direct_evolution_together/__init__.py +1 -0
- direct_evolution_together/admin.py +11 -0
- direct_evolution_together/apps.py +39 -0
- direct_evolution_together/asgi.py +9 -0
- direct_evolution_together/cli.py +200 -0
- direct_evolution_together/config.nims +4 -0
- direct_evolution_together/cppn.py +431 -0
- direct_evolution_together/direct_evolution_together.nimble +10 -0
- direct_evolution_together/migrations/0001_initial.py +27 -0
- direct_evolution_together/migrations/__init__.py +0 -0
- direct_evolution_together/models.py +25 -0
- direct_evolution_together/mummy_ws.nim +1360 -0
- direct_evolution_together/settings.py +134 -0
- direct_evolution_together/templates/direct_evolution_together/admin_reset.html +162 -0
- direct_evolution_together/templates/direct_evolution_together/index.html +3145 -0
- direct_evolution_together/urls.py +16 -0
- direct_evolution_together/views.py +305 -0
- direct_evolution_together/ws_bridge.py +201 -0
- direct_evolution_together/ws_server.py +46 -0
- direct_evolution_together-0.0.1.dist-info/METADATA +57 -0
- direct_evolution_together-0.0.1.dist-info/RECORD +24 -0
- direct_evolution_together-0.0.1.dist-info/WHEEL +4 -0
- direct_evolution_together-0.0.1.dist-info/entry_points.txt +2 -0
- direct_evolution_together-0.0.1.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import nimporter_plus # noqa: F401 - registers Nim import hook
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from django.contrib import admin
|
|
2
|
+
|
|
3
|
+
from direct_evolution_together.models import ChatMessage
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@admin.register(ChatMessage)
|
|
7
|
+
class ChatMessageAdmin(admin.ModelAdmin):
|
|
8
|
+
list_display = ("room", "username", "message", "created_at")
|
|
9
|
+
list_filter = ("room",)
|
|
10
|
+
search_fields = ("username", "message")
|
|
11
|
+
ordering = ("-created_at", "-id")
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
from django.apps import AppConfig
|
|
5
|
+
from django.conf import settings
|
|
6
|
+
from django.db.backends.signals import connection_created
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _enable_sqlite_wal(sender, connection, **kwargs) -> None:
|
|
10
|
+
if connection.vendor == "sqlite":
|
|
11
|
+
with connection.cursor() as cursor:
|
|
12
|
+
cursor.execute("PRAGMA journal_mode=WAL;")
|
|
13
|
+
cursor.execute("PRAGMA synchronous=NORMAL;")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class DirectEvolutionTogetherConfig(AppConfig):
|
|
17
|
+
default_auto_field = "django.db.models.BigAutoField"
|
|
18
|
+
name = "direct_evolution_together"
|
|
19
|
+
|
|
20
|
+
def ready(self) -> None:
|
|
21
|
+
connection_created.connect(_enable_sqlite_wal, dispatch_uid="det_sqlite_wal")
|
|
22
|
+
is_runserver = "runserver" in sys.argv or os.environ.get("DET_START_WS") == "1"
|
|
23
|
+
if not is_runserver:
|
|
24
|
+
return
|
|
25
|
+
# Under StatReloader, only start Mummy in the serving child process (RUN_MAIN=true),
|
|
26
|
+
# or immediately when --noreload is used.
|
|
27
|
+
if os.environ.get("RUN_MAIN") == "true" or "--noreload" in sys.argv:
|
|
28
|
+
from direct_evolution_together.ws_bridge import start_ws_server
|
|
29
|
+
|
|
30
|
+
start_ws_server(
|
|
31
|
+
host=settings.WS_HOST,
|
|
32
|
+
port=settings.WS_PORT,
|
|
33
|
+
pop_size=settings.POPULATION_SIZE,
|
|
34
|
+
min_votes=settings.MIN_VOTES,
|
|
35
|
+
max_votes=settings.MAX_VOTES_PER_USER,
|
|
36
|
+
mut_sigma=settings.MUTATION_SIGMA,
|
|
37
|
+
elite_frac=settings.ELITE_FRAC,
|
|
38
|
+
crossover_rate=settings.CROSSOVER_RATE,
|
|
39
|
+
)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""ASGI entrypoint for running Django HTTP views under Uvicorn."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
from django.core.asgi import get_asgi_application
|
|
6
|
+
|
|
7
|
+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "direct_evolution_together.settings")
|
|
8
|
+
|
|
9
|
+
application = get_asgi_application()
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import os
|
|
3
|
+
import shutil
|
|
4
|
+
import socket
|
|
5
|
+
import subprocess
|
|
6
|
+
import sys
|
|
7
|
+
import threading
|
|
8
|
+
import time
|
|
9
|
+
import webbrowser
|
|
10
|
+
|
|
11
|
+
NIM_INSTALL_GUIDE = """\
|
|
12
|
+
direct-evolution-together runs its WebSocket server on Nim + Mummy
|
|
13
|
+
(we intentionally ship pure Nim source with no precompiled binaries!).
|
|
14
|
+
|
|
15
|
+
To install Nim & Nimble packages:
|
|
16
|
+
1. Install Nim:
|
|
17
|
+
- macOS: brew install nim
|
|
18
|
+
- Linux / Unix: curl https://nim-lang.org/choosenim/init.sh -sSf | sh
|
|
19
|
+
- Any platform: https://nim-lang.org/install.html
|
|
20
|
+
2. Install required Nim packages:
|
|
21
|
+
nimble install -y mummy nimpy
|
|
22
|
+
3. Re-run:
|
|
23
|
+
direct-evolution-together
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def check_nim_toolchain(*, auto_install_deps: bool = False) -> tuple[bool, str]:
|
|
28
|
+
"""Verify Nim, Nimble, mummy, and nimpy are available for compiling Mummy."""
|
|
29
|
+
if shutil.which("nim") is None or shutil.which("nimble") is None:
|
|
30
|
+
return False, "Missing `nim` or `nimble` in PATH.\n\n" + NIM_INSTALL_GUIDE
|
|
31
|
+
|
|
32
|
+
missing = []
|
|
33
|
+
for pkg in ("mummy", "nimpy"):
|
|
34
|
+
res = subprocess.run(
|
|
35
|
+
["nimble", "path", pkg],
|
|
36
|
+
capture_output=True,
|
|
37
|
+
text=True,
|
|
38
|
+
check=False,
|
|
39
|
+
)
|
|
40
|
+
if res.returncode != 0:
|
|
41
|
+
missing.append(pkg)
|
|
42
|
+
|
|
43
|
+
if missing and auto_install_deps:
|
|
44
|
+
install_res = subprocess.run(
|
|
45
|
+
["nimble", "install", "-y", *missing],
|
|
46
|
+
check=False,
|
|
47
|
+
)
|
|
48
|
+
if install_res.returncode == 0:
|
|
49
|
+
missing = []
|
|
50
|
+
|
|
51
|
+
if missing:
|
|
52
|
+
pkg_list = " ".join(missing)
|
|
53
|
+
return (
|
|
54
|
+
False,
|
|
55
|
+
f"Nim is installed, but required Nimble package(s) are missing: {pkg_list}\n"
|
|
56
|
+
f"Run `nimble install -y {pkg_list}` (or pass `--install-nim-deps`) and try again.\n\n"
|
|
57
|
+
+ NIM_INSTALL_GUIDE,
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
return True, "Nim toolchain OK (nim, nimble, mummy, nimpy)."
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def open_browser_when_ready(host: str, port: int, timeout: float = 10.0) -> threading.Thread:
|
|
64
|
+
"""Open the local Web UI URL in the default browser once the HTTP port is accepting connections."""
|
|
65
|
+
browse_host = "127.0.0.1" if host in ("0.0.0.0", "::", "") else host
|
|
66
|
+
url = f"http://{browse_host}:{port}/"
|
|
67
|
+
|
|
68
|
+
def _worker() -> None:
|
|
69
|
+
deadline = time.monotonic() + timeout
|
|
70
|
+
while time.monotonic() < deadline:
|
|
71
|
+
try:
|
|
72
|
+
with socket.create_connection((browse_host, port), timeout=0.25):
|
|
73
|
+
webbrowser.open(url, new=2)
|
|
74
|
+
return
|
|
75
|
+
except OSError:
|
|
76
|
+
time.sleep(0.1)
|
|
77
|
+
|
|
78
|
+
thread = threading.Thread(target=_worker, name="det-browser-open", daemon=True)
|
|
79
|
+
thread.start()
|
|
80
|
+
return thread
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def main() -> None:
|
|
84
|
+
"""Run auto-migrations, verify Nim toolchain, and launch Django + Mummy."""
|
|
85
|
+
from direct_evolution_together.settings import load_dotenv
|
|
86
|
+
|
|
87
|
+
load_dotenv()
|
|
88
|
+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "direct_evolution_together.settings")
|
|
89
|
+
|
|
90
|
+
parser = argparse.ArgumentParser(
|
|
91
|
+
prog="direct-evolution-together",
|
|
92
|
+
description="Launch direct-evolution-together (Django web UI + Nim Mummy WebSockets + PyTorch CPPN)",
|
|
93
|
+
)
|
|
94
|
+
parser.add_argument("--host", default=os.getenv("HOST", "127.0.0.1"))
|
|
95
|
+
parser.add_argument("--port", type=int, default=int(os.getenv("PORT", "8000")))
|
|
96
|
+
parser.add_argument("--ws-host", default=os.getenv("MUMMY_WS_HOST", "127.0.0.1"))
|
|
97
|
+
parser.add_argument("--ws-port", type=int, default=int(os.getenv("MUMMY_WS_PORT", "8001")))
|
|
98
|
+
parser.add_argument(
|
|
99
|
+
"--population-size",
|
|
100
|
+
type=int,
|
|
101
|
+
default=int(os.getenv("MUMMY_POP_SIZE", "30")),
|
|
102
|
+
help="Fixed population size for public rooms and default for private rooms (3-64, default: 30)",
|
|
103
|
+
)
|
|
104
|
+
parser.add_argument(
|
|
105
|
+
"--max-votes-per-user",
|
|
106
|
+
type=int,
|
|
107
|
+
default=int(os.getenv("MUMMY_MAX_VOTES", "5")),
|
|
108
|
+
help="Max active votes per user per round in public rooms and default for private rooms (1-20, default: 5)",
|
|
109
|
+
)
|
|
110
|
+
parser.add_argument(
|
|
111
|
+
"--mutation-sigma",
|
|
112
|
+
type=float,
|
|
113
|
+
default=float(os.getenv("MUMMY_MUT_SIGMA", "0.35")),
|
|
114
|
+
help="Constant GA mutation sigma for public rooms and default for private rooms (0.05-1.0, default: 0.35)",
|
|
115
|
+
)
|
|
116
|
+
parser.add_argument(
|
|
117
|
+
"--elite-frac",
|
|
118
|
+
type=float,
|
|
119
|
+
default=float(os.getenv("MUMMY_ELITE_FRAC", "0.10")),
|
|
120
|
+
help="Elite fraction carried over per generation for public rooms and default for private rooms (0.0-0.5, default: 0.10)",
|
|
121
|
+
)
|
|
122
|
+
parser.add_argument(
|
|
123
|
+
"--crossover-rate",
|
|
124
|
+
type=float,
|
|
125
|
+
default=float(os.getenv("MUMMY_CROSSOVER_RATE", "0.75")),
|
|
126
|
+
help="Crossover probability for breeding for public rooms and default for private rooms (0.0-1.0, default: 0.75)",
|
|
127
|
+
)
|
|
128
|
+
parser.add_argument("--reload", action="store_true", help="Enable Django dev autoreloader")
|
|
129
|
+
parser.add_argument("--noreload", action="store_true", help="Disable Django autoreloader (default)")
|
|
130
|
+
parser.add_argument("--no-browser", action="store_true", help="Do not automatically open the Web UI in a browser")
|
|
131
|
+
parser.add_argument("--check", action="store_true", help="Check Nim/Mummy readiness and exit")
|
|
132
|
+
parser.add_argument(
|
|
133
|
+
"--install-nim-deps",
|
|
134
|
+
action="store_true",
|
|
135
|
+
help="Automatically run `nimble install -y mummy nimpy` if missing",
|
|
136
|
+
)
|
|
137
|
+
args = parser.parse_args()
|
|
138
|
+
|
|
139
|
+
is_reloader_child = os.environ.get("RUN_MAIN") == "true"
|
|
140
|
+
if not is_reloader_child:
|
|
141
|
+
ok, message = check_nim_toolchain(auto_install_deps=args.install_nim_deps)
|
|
142
|
+
if not ok:
|
|
143
|
+
print(message, file=sys.stderr)
|
|
144
|
+
raise SystemExit(1)
|
|
145
|
+
if args.check:
|
|
146
|
+
print(message)
|
|
147
|
+
return
|
|
148
|
+
|
|
149
|
+
pop_size = max(3, min(64, args.population_size))
|
|
150
|
+
max_votes = max(1, min(20, args.max_votes_per_user))
|
|
151
|
+
mut_sigma = max(0.05, min(1.0, float(args.mutation_sigma)))
|
|
152
|
+
elite_frac = max(0.0, min(0.5, float(args.elite_frac)))
|
|
153
|
+
crossover_rate = max(0.0, min(1.0, float(args.crossover_rate)))
|
|
154
|
+
os.environ["MUMMY_WS_HOST"] = args.ws_host
|
|
155
|
+
os.environ["MUMMY_WS_PORT"] = str(args.ws_port)
|
|
156
|
+
os.environ["MUMMY_POP_SIZE"] = str(pop_size)
|
|
157
|
+
os.environ["MUMMY_MAX_VOTES"] = str(max_votes)
|
|
158
|
+
os.environ["MUMMY_MUT_SIGMA"] = f"{mut_sigma:.2f}"
|
|
159
|
+
os.environ["MUMMY_ELITE_FRAC"] = f"{elite_frac:.2f}"
|
|
160
|
+
os.environ["MUMMY_CROSSOVER_RATE"] = f"{crossover_rate:.2f}"
|
|
161
|
+
|
|
162
|
+
import django
|
|
163
|
+
from django.core.management import call_command, execute_from_command_line
|
|
164
|
+
|
|
165
|
+
use_noreload = (not args.reload) or args.noreload
|
|
166
|
+
|
|
167
|
+
if not is_reloader_child:
|
|
168
|
+
os.environ["DET_START_WS"] = "0"
|
|
169
|
+
django.setup()
|
|
170
|
+
call_command("migrate", interactive=False, verbosity=0)
|
|
171
|
+
print(
|
|
172
|
+
f"direct-evolution-together · Web UI: http://{args.host}:{args.port}/ "
|
|
173
|
+
f"| WebSocket (Nim + Mummy): ws://{args.ws_host}:{args.ws_port}",
|
|
174
|
+
flush=True,
|
|
175
|
+
)
|
|
176
|
+
if use_noreload:
|
|
177
|
+
from direct_evolution_together.ws_bridge import start_ws_server
|
|
178
|
+
|
|
179
|
+
start_ws_server(
|
|
180
|
+
host=args.ws_host,
|
|
181
|
+
port=args.ws_port,
|
|
182
|
+
pop_size=pop_size,
|
|
183
|
+
max_votes=max_votes,
|
|
184
|
+
mut_sigma=mut_sigma,
|
|
185
|
+
elite_frac=elite_frac,
|
|
186
|
+
crossover_rate=crossover_rate,
|
|
187
|
+
)
|
|
188
|
+
if not args.no_browser:
|
|
189
|
+
open_browser_when_ready(args.host, args.port)
|
|
190
|
+
|
|
191
|
+
os.environ["DET_START_WS"] = "1"
|
|
192
|
+
cmd = [__file__, "runserver", f"{args.host}:{args.port}"]
|
|
193
|
+
if use_noreload:
|
|
194
|
+
cmd.append("--noreload")
|
|
195
|
+
sys.argv = cmd
|
|
196
|
+
execute_from_command_line(cmd)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
if __name__ == "__main__":
|
|
200
|
+
main()
|