Spaces:
Running on Zero
Running on Zero
File size: 5,121 Bytes
9936912 | 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 | """Generate verified SFT examples for continuous-time LTI stability."""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import numpy as np
SYSTEM_PROMPT = (
"You are a concise control-systems engineering assistant. "
"State assumptions, show the decisive calculation, and provide executable code "
"when requested."
)
def format_number(value: complex) -> str:
"""Format a real or complex eigenvalue without numerical noise."""
real = float(np.real(value))
imag = float(np.imag(value))
if abs(imag) < 1e-10:
return f"{real:.6g}"
sign = "+" if imag >= 0 else "-"
return f"{real:.6g} {sign} {abs(imag):.6g}j"
def make_record(index: int, matrix: np.ndarray, seed: int) -> dict:
eigenvalues = np.linalg.eigvals(matrix)
stable = bool(np.all(np.real(eigenvalues) < 0.0))
matrix_text = repr(matrix.tolist())
eigenvalue_text = ", ".join(format_number(value) for value in eigenvalues)
conclusion = (
"All eigenvalues have strictly negative real parts; therefore, the origin "
"is asymptotically stable"
if stable
else "At least one eigenvalue has a positive real part; therefore, the origin "
"is unstable"
)
prompt = (
"For the continuous-time autonomous LTI system x_dot = A x with "
f"A = {matrix_text}, determine asymptotic stability from the eigenvalues. "
"Provide minimal NumPy code that verifies the conclusion."
)
answer = (
f"The eigenvalues are {eigenvalue_text}. {conclusion}.\n\n"
"```python\n"
"import numpy as np\n\n"
f"A = np.array({matrix_text}, dtype=float)\n"
"eigenvalues = np.linalg.eigvals(A)\n"
"stable = np.all(np.real(eigenvalues) < 0.0)\n"
"print(eigenvalues)\n"
"print(\"asymptotically stable:\", stable)\n"
"```"
)
return {
"messages": [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": prompt},
{"role": "assistant", "content": answer},
],
"metadata": {
"id": f"lti_eigenvalue_stability_{index:04d}",
"domain": "state_space",
"family": "continuous_lti_eigenvalue_stability",
"tool": "python_numpy",
"source_type": "programmatically_generated",
"generator": "scripts/generate_stability_sft.py",
"seed": seed,
"verification": ["numpy.linalg.eigvals"],
"ground_truth": {
"A": matrix.tolist(),
"eigenvalues": [
[float(np.real(value)), float(np.imag(value))]
for value in eigenvalues
],
"asymptotically_stable": stable,
},
"status": "draft",
},
}
def generate(count: int, seed: int) -> list[dict]:
"""Generate a roughly balanced set of stable and unstable examples."""
rng = np.random.default_rng(seed)
stable_target = count // 2
unstable_target = count - stable_target
stable_count = 0
unstable_count = 0
seen: set[tuple[int, ...]] = set()
records: list[dict] = []
while len(records) < count:
integer_matrix = rng.integers(-4, 5, size=(2, 2))
key = tuple(int(value) for value in integer_matrix.flat)
if key in seen:
continue
seen.add(key)
matrix = integer_matrix.astype(float)
eigenvalues = np.linalg.eigvals(matrix)
real_parts = np.real(eigenvalues)
# Avoid marginal or numerically ambiguous examples in this first dataset.
if np.any(np.abs(real_parts) < 0.25):
continue
stable = bool(np.all(real_parts < 0.0))
if stable and stable_count >= stable_target:
continue
if not stable and unstable_count >= unstable_target:
continue
records.append(make_record(len(records) + 1, matrix, seed))
stable_count += int(stable)
unstable_count += int(not stable)
return records
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--count", type=int, default=6)
parser.add_argument("--seed", type=int, default=42)
parser.add_argument(
"--output",
type=Path,
default=Path("data/sft_draft/generated_stability.jsonl"),
)
args = parser.parse_args()
if args.count < 2:
parser.error("--count must be at least 2")
records = generate(args.count, args.seed)
args.output.parent.mkdir(parents=True, exist_ok=True)
with args.output.open("w", encoding="utf-8") as handle:
for record in records:
handle.write(json.dumps(record, ensure_ascii=False) + "\n")
stable_count = sum(
record["metadata"]["ground_truth"]["asymptotically_stable"]
for record in records
)
print(f"wrote: {args.output}")
print(f"records: {len(records)}")
print(f"stable: {stable_count}")
print(f"unstable: {len(records) - stable_count}")
if __name__ == "__main__":
main()
|