Spaces:
Running
Running
File size: 16,586 Bytes
603b5b4 e85a399 cb9b449 c64cc6d 603b5b4 cb9b449 e85a399 779968d 8caaf20 779968d a6d7d68 779968d 8caaf20 a6d7d68 0e3a627 779968d 603b5b4 e048104 9292daf 888bb2a 779968d e85a399 779968d e85a399 14a60d6 779968d 603b5b4 779968d 9292daf 888bb2a 779968d e85a399 14a60d6 779968d 603b5b4 779968d 603b5b4 e048104 603b5b4 779968d c64cc6d 779968d c64cc6d 779968d e85a399 888bb2a e85a399 888bb2a e85a399 779968d 888bb2a e85a399 779968d 3adb9d3 0244a95 779968d e85a399 3adb9d3 c64cc6d 19d8323 779968d 19d8323 3adb9d3 14a60d6 603b5b4 3adb9d3 e85a399 b9bbaec e85a399 552eeb7 19d8323 552eeb7 19d8323 552eeb7 b9bbaec 19d8323 e85a399 19d8323 e85a399 19d8323 c64cc6d 19d8323 3adb9d3 e85a399 3adb9d3 cb9b449 603b5b4 3adb9d3 40aa7d5 3adb9d3 40aa7d5 3adb9d3 779968d | 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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | import type { ReactNode } from "react";
import AlgorithmControls from "./AlgorithmControls";
import { useCallback, useEffect, useRef, useState } from "react";
import ReactMarkdown from "react-markdown";
import { InputField, Tabs, Card } from "@elvis/ui";
import { type SettingsUi, type TrajectoryValues } from "./types.ts";
import usageMarkdown from "./usage.md?raw";
import IterationHistoryPlot from "./IterationHistoryPlot.tsx";
const UNIVARIATE_FUNCTION_OPTIONS = {
"--Custom--": "x^2",
"Quadratic": "x^2",
"Local Minima": "x^2 - 0.1cos(20x)",
"Plateau": "x^4",
}
const BIVARIATE_FUNCTION_OPTIONS = {
"--Custom--": "x^2 + 3y^2",
"Quadratic": "x^2 + 3y^2",
"Small": "(0.05x)^2 + 3(0.05y)^2",
"Ackley": "-20exp(-0.2 sqrt(0.5(x^2 + y^2))) - exp(0.5(cos(2 pi x) + cos(2 pi y))) + e + 20",
"Rasteringin": "20 + (x^2 - 10cos(2 pi x)) + (y^2 - 10cos(2 pi y))",
"Rosenbrock": "(1 - x)^2 + 100(y - x^2)^2",
}
interface SidebarProps {
comparison: ReactNode,
onTabChange: (tab: string) => void,
settings: SettingsUi,
setSettings: (settings: SettingsUi) => void,
onRandomInitialPoint: () => void,
trajectoryValues?: TrajectoryValues | null,
onReset?: () => void,
onNextStep?: () => void,
onPrevStep?: () => void,
onAnimate?: () => void,
onPause?: () => void,
isAnimating?: boolean,
currentIteration: number,
totalIterations: number,
showGradientField: boolean,
onShowGradientFieldChange: (show: boolean) => void,
}
export default function Sidebar({
comparison,
onTabChange,
settings,
setSettings,
onRandomInitialPoint,
trajectoryValues,
onReset,
onNextStep,
onPrevStep,
onAnimate,
onPause,
isAnimating = false,
currentIteration,
totalIterations,
showGradientField,
onShowGradientFieldChange,
}: SidebarProps) {
const tabs = ["Settings", "Optimize", "Compare", "Usage"] as const;
const [activeTab, setActiveTab] = useState<(typeof tabs)[number]>("Settings");
const [functionOption, setFunctionOption] = useState<string>("--Custom--");
function updateSettings(key: keyof SettingsUi, value: string) {
setSettings({ ...settings, [key]: value });
}
function handleProblemSettingChange(value: string) {
const [mode, option] = value.split("::") as [SettingsUi["mode"], string];
const selectedOption = option ?? "--Custom--";
const expr = mode === "Bivariate"
? BIVARIATE_FUNCTION_OPTIONS[selectedOption as keyof typeof BIVARIATE_FUNCTION_OPTIONS]
: UNIVARIATE_FUNCTION_OPTIONS[selectedOption as keyof typeof UNIVARIATE_FUNCTION_OPTIONS];
setFunctionOption(selectedOption);
setSettings({ ...settings, mode, functionExpr: expr });
}
function getCurrentValue<T>(values?: T[] | null): T | null {
return values?.[currentIteration] ?? null;
}
const currentX = getCurrentValue(trajectoryValues?.x);
const currentY = getCurrentValue(trajectoryValues?.y);
// univariate only
const currentDerivative = getCurrentValue(trajectoryValues?.derivative);
const currentSecondDerivative = getCurrentValue(trajectoryValues?.secondDerivative);
// bivariate only
const currentZ = getCurrentValue(trajectoryValues?.z);
const currentGradient = getCurrentValue(trajectoryValues?.gradient);
const currentHessian = getCurrentValue(trajectoryValues?.hessian);
const holdIntervalRef = useRef<number | null>(null);
const stopHolding = useCallback(() => {
if (holdIntervalRef.current !== null) {
window.clearInterval(holdIntervalRef.current);
holdIntervalRef.current = null;
}
}, []);
const startHolding = useCallback((action?: () => void) => {
if (!action || holdIntervalRef.current !== null) return;
action();
holdIntervalRef.current = window.setInterval(action, 200);
}, []);
useEffect(() => {
function handleKeyDown(event: KeyboardEvent) {
if (activeTab !== "Optimize") return;
const target = event.target as HTMLElement | null;
if (target?.matches("input, textarea, select, [contenteditable='true']")) return;
if (event.repeat) return;
if (event.key === "ArrowLeft") {
event.preventDefault();
startHolding(onPrevStep);
} else if (event.key === "ArrowRight") {
event.preventDefault();
startHolding(onNextStep);
}
}
function handleKeyUp(event: KeyboardEvent) {
if (event.key === "ArrowLeft" || event.key === "ArrowRight") stopHolding();
}
window.addEventListener("keydown", handleKeyDown);
window.addEventListener("keyup", handleKeyUp);
window.addEventListener("blur", stopHolding);
return () => {
stopHolding();
window.removeEventListener("keydown", handleKeyDown);
window.removeEventListener("keyup", handleKeyUp);
window.removeEventListener("blur", stopHolding);
};
}, [activeTab, onNextStep, onPrevStep, startHolding, stopHolding]);
return (
<Card className="flex flex-col h-full p-4 gap-2 min-w-0 min-h-0 overflow-auto">
<div className="w-full [&>div]:w-full [&>div>button]:w-1/4 [&>div>button]:px-1">
<Tabs tabs={tabs} activeTab={activeTab} onChange={(tab) => { setActiveTab(tab); onTabChange(tab); }} />
</div>
{/* Tab content */}
<div className="flex flex-1 flex-col gap-4 min-h-0 min-w-0 overflow-auto">
{activeTab === "Settings" && (
<>
<div className="grid grid-cols-[6rem_minmax(0,1fr)] items-center gap-0.5">
<label className="text-sm font-semibold whitespace-nowrap pr-0.25">Objective</label>
<div className="min-w-0">
<select
value={`${settings.mode}::${functionOption}`}
onChange={(event) => handleProblemSettingChange(event.target.value)}
className="h-10 w-full rounded border border-stone-300 bg-white px-2 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-stone-400"
>
<optgroup label="Univariate" className="font-semibold">
{Object.keys(UNIVARIATE_FUNCTION_OPTIONS).map((option) => (
<option key={`Univariate::${option}`} value={`Univariate::${option}`}>
{option}
</option>
))}
</optgroup>
<optgroup label="Bivariate" className="font-semibold">
{Object.keys(BIVARIATE_FUNCTION_OPTIONS).map((option) => (
<option key={`Bivariate::${option}`} value={`Bivariate::${option}`}>
{option}
</option>
))}
</optgroup>
</select>
</div>
</div>
<InputField
label=""
value={settings.functionExpr}
onChange={(value) => updateSettings("functionExpr", value)}
readonly={functionOption !== "--Custom--"}
rows={3}
/>
{settings.mode === "Bivariate" && (
<fieldset className="grid grid-cols-[6rem_minmax(0,1fr)] items-center gap-0.5">
<legend className="text-sm font-medium whitespace-nowrap pr-0.25">Gradient field</legend>
<div className="flex items-center gap-4 text-sm">
<label className="flex cursor-pointer items-center gap-1.5">
<input
type="radio"
name="gradient-field"
checked={showGradientField}
onChange={() => onShowGradientFieldChange(true)}
/>
Show
</label>
<label className="flex cursor-pointer items-center gap-1.5">
<input
type="radio"
name="gradient-field"
checked={!showGradientField}
onChange={() => onShowGradientFieldChange(false)}
/>
Hide
</label>
</div>
</fieldset>
)}
<AlgorithmControls settings={settings} setSettings={setSettings} onRandomInitialPoint={onRandomInitialPoint} />
</>
)}
{activeTab === "Optimize" && (
<>
<IterationHistoryPlot mode={settings.mode} trajectoryValues={trajectoryValues} currentIteration={currentIteration} />
<div className="hidden lg:flex flex-none flex-col gap-1 min-w-0">
<div className="flex items-center justify-between gap-2 text-sm font-semibold">
<span>Current values</span>
<span className="whitespace-nowrap font-normal text-stone-500">
Iteration {currentIteration} of {totalIterations}
</span>
</div>
{ settings.mode === "Univariate" && (
<>
<div className="grid grid-cols-[6rem_minmax(0,1fr)] items-center gap-2">
<label className="text-right text-sm font-medium whitespace-nowrap">X</label>
<input
type="text"
value={currentX !== null ? currentX.toFixed(4) : ""}
readOnly
className="w-full rounded border border-stone-300 bg-stone-50 px-2 py-1 text-sm"
/>
</div>
<div className="grid grid-cols-[6rem_minmax(0,1fr)] items-center gap-2">
<label className="text-right text-sm font-medium whitespace-nowrap">Y</label>
<input
type="text"
value={currentY !== null ? currentY.toFixed(4) : ""}
readOnly
className="w-full rounded border border-stone-300 bg-stone-50 px-2 py-1 text-sm"
/>
</div>
<div className="grid grid-cols-[6rem_minmax(0,1fr)] items-center gap-2">
<label className="text-right text-sm font-medium whitespace-nowrap">1st derivative</label>
<input
type="text"
value={currentDerivative !== null ? currentDerivative.toFixed(4) : ""}
readOnly
className="w-full rounded border border-stone-300 bg-stone-50 px-2 py-1 text-sm"
/>
</div>
<div className="grid grid-cols-[6rem_minmax(0,1fr)] items-center gap-2">
<label className="text-right text-sm font-medium whitespace-nowrap">2nd derivative</label>
<input
type="text"
value={currentSecondDerivative !== null ? currentSecondDerivative.toFixed(4) : ""}
readOnly
className="w-full rounded border border-stone-300 bg-stone-50 px-2 py-1 text-sm"
/>
</div>
</>
)}
{ settings.mode === "Bivariate" && (
<>
<div className="grid grid-cols-[6rem_minmax(0,1fr)] items-center gap-2">
<label className="text-right text-sm font-medium whitespace-nowrap">(X, Y)</label>
<input
type="text"
value={currentX !== null && currentY !== null ? `${currentX.toFixed(4)}, ${currentY.toFixed(4)}` : ""}
readOnly
className="w-full rounded border border-stone-300 bg-stone-50 px-2 py-1 text-sm"
/>
</div>
<div className="grid grid-cols-[6rem_minmax(0,1fr)] items-center gap-2">
<label className="text-right text-sm font-medium whitespace-nowrap">Z</label>
<input
type="text"
value={currentZ !== null ? currentZ.toFixed(4) : ""}
readOnly
className="w-full rounded border border-stone-300 bg-stone-50 px-2 py-1 text-sm"
/>
</div>
<div className="grid grid-cols-[6rem_minmax(0,1fr)] items-center gap-2">
<label className="text-right text-sm font-medium whitespace-nowrap">Gradient</label>
<input
type="text"
value={currentGradient !== null ? `[${currentGradient.map(v => v.toFixed(4)).join(", ")}]` : ""}
readOnly
className="w-full rounded border border-stone-300 bg-stone-50 px-2 py-1 text-sm"
/>
</div>
<div className="grid grid-cols-[6rem_minmax(0,1fr)] items-center gap-2">
<label className="text-right text-sm font-medium whitespace-nowrap">Hessian</label>
<input
type="text"
value={currentHessian !== null ? `[${currentHessian.map(row => `[${row.map(v => v.toFixed(4)).join(", ")}]`).join(", ")}]` : ""}
readOnly
className="w-full rounded border border-stone-300 bg-stone-50 px-2 py-1 text-sm"
/>
</div>
</>
)}
</div>
<div className="-mt-2 flex flex-col gap-1">
<div className="flex gap-2">
<button
type="button"
onPointerDown={() => startHolding(onNextStep)}
onPointerUp={stopHolding}
onPointerCancel={stopHolding}
onPointerLeave={stopHolding}
disabled={!onNextStep}
className="h-8 flex-1 rounded border border-gray-300 bg-orange-200 px-3 py-1 text-sm text-stone-700 hover:bg-orange-300 disabled:cursor-not-allowed disabled:opacity-50"
>
Next
</button>
<button
type="button"
onPointerDown={() => startHolding(onPrevStep)}
onPointerUp={stopHolding}
onPointerCancel={stopHolding}
onPointerLeave={stopHolding}
className="h-8 flex-1 rounded border border-gray-300 bg-orange-200 px-3 py-1 text-sm text-stone-700 hover:bg-orange-300"
>
Previous
</button>
<button
type="button"
onClick={onReset}
className="h-8 flex-1 rounded border border-gray-300 bg-orange-200 px-3 py-1 text-sm text-stone-700 hover:bg-orange-300"
>
Reset
</button>
</div>
<div className="grid grid-cols-3 gap-2">
<button
type="button"
onClick={onAnimate}
disabled={isAnimating || !onAnimate}
className="h-8 rounded border border-gray-300 bg-orange-200 px-3 py-1 text-sm text-stone-700 hover:bg-orange-300 disabled:cursor-not-allowed disabled:opacity-50"
>
Animate
</button>
<button
type="button"
onClick={onPause}
disabled={!isAnimating}
className="h-8 rounded border border-gray-300 bg-stone-100 px-3 py-1 text-sm text-stone-700 hover:bg-stone-200 disabled:cursor-not-allowed disabled:opacity-50"
>
Pause
</button>
</div>
</div>
</>
)}
{activeTab === "Compare" && comparison}
{activeTab === "Usage" && (
<ReactMarkdown
components={{
h1: ({ children }) => <h1 className="text-2xl font-bold mt-0 -mb-2">{children}</h1>,
h2: ({ children }) => <h2 className="text-xl font-semibold mt-0 -mb-2">{children}</h2>,
h3: ({ children }) => <h3 className="text-lg font-semibold mt-0 -mb-2">{children}</h3>,
p: ({ children }) => <p className="leading-6 mb-3 last:mb-0">{children}</p>,
ul: ({ children }) => <ul className="-mt-6 mb-0 list-disc pl-4 [&_p]:mb-0">{children}</ul>,
ol: ({ children }) => <ol className="-mt-5 mb-0 list-decimal pl-4 [&_p]:mb-0">{children}</ol>,
li: ({ children }) => <li className="mb-0">{children}</li>,
}}
>
{usageMarkdown}
</ReactMarkdown>
)}
</div>
</Card>
);
}
|