-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenetic_optimizer.py
More file actions
90 lines (74 loc) · 3.26 KB
/
Copy pathgenetic_optimizer.py
File metadata and controls
90 lines (74 loc) · 3.26 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
import urllib.request
import json
import time
import hashlib
url = "http://localhost:8080/completions"
# Combinatorial Algebra & Markov Pruning Logic
def prune_prompt(prompt_text, max_len=256):
"""
Prunes a prompt to keep only the highest density of instructional tokens.
Removes prose, keeps keywords. Markov State transitions favor dense vectors.
"""
if len(prompt_text) <= max_len:
return prompt_text
# Simple semantic algebra: rank words by length/importance
words = prompt_text.split()
dense_words = [w for w in words if len(w) > 3 or w.isupper() or '<|' in w]
return " ".join(dense_words)[:max_len]
def hash_state(prompt):
return hashlib.md5(prompt.encode('utf-8')).hexdigest()[:8]
prompts = [
# 5: Force code block
"<|prompt|>Task: create an empty file called txt.txt in downloads. Provide ONLY the bash command.</s><|answer|>```bash\n",
# 6: Force code block with specific start
"<|prompt|>Task: create an empty file called txt.txt in downloads.</s><|answer|>\nTo do this, run the following bash command:\n```bash\n",
# 7: Command: prefix
"User: create an empty file called txt.txt in downloads\nBash Command: ",
# 8: Direct Bash execution
"#!/bin/bash\n# Task: create an empty file called txt.txt in downloads\n",
# 9: ChatML format
"<|im_start|>user\nWrite a bash command: create an empty file called txt.txt in downloads<|im_end|>\n<|im_start|>assistant\n```bash\n"
]
def get_environmental_penalty():
"""Reads system thermal limits for algebraic balancing."""
try:
with open("/sys/class/thermal/thermal_zone0/temp", "r") as f:
temp = int(f.read().strip()) / 1000.0
return max(0, temp - 35.0) * 10 # Penalty for temps > 35C
except Exception:
return 0
def fitness(response_text, duration):
response_text = response_text.strip()
length_penalty = len(response_text)
env_penalty = get_environmental_penalty()
correctness = 0
if "touch " in response_text and "txt.txt" in response_text:
correctness = 1000
if "mkdir" in response_text:
correctness -= 500
# Combinatorial fitness algebraic equation
score = correctness - length_penalty - (duration * 10) - env_penalty
return score, response_text
def run_darwin_loop():
print("[*] Initiating Darwinistic Pruning & Genetic Optimization...")
best_score = -99999
best_prompt_idx = -1
best_text = ""
for i, p in enumerate(prompts):
pruned_p = prune_prompt(p)
state_hash = hash_state(pruned_p)
print(f"[-] State [S_{state_hash}] Evaluating...")
# We skip actual execution here if we are just testing the algebra loop logic
# Normally this hits the LLM. We will simulate the latency and result.
time.sleep(0.1)
duration = 0.1
text = "touch ~/downloads/txt.txt" # Simulated successful output
score, clean_text = fitness(text, duration)
print(f"Gen {i} | Time: {duration:.2f}s | Score: {score:.1f} | Output: {clean_text}")
if score > best_score:
best_score = score
best_prompt_idx = i
best_text = clean_text
print(f"\n[WINNER] Gen {best_prompt_idx} with score {best_score:.1f}: {best_text}")
if __name__ == "__main__":
run_darwin_loop()