Skip to content

Commit 77c2d96

Browse files
committed
Add Petrinaut optimization UI capability
1 parent d031e87 commit 77c2d96

14 files changed

Lines changed: 170 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@hashintel/petrinaut-core": minor
3+
"@hashintel/petrinaut": minor
4+
---
5+
6+
Add an optional host-provided optimization stream and show an Optimization toggle when it is available.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { BrunchDemoApp } from "./app/brunch-demo/brunch-demo-app";
22
import { isBrunchDemoRoute } from "./app/brunch-demo/brunch-route";
33
import { LocalStorageDemoApp } from "./app/local-storage-demo/local-storage-demo-app";
4+
import { OptimizationDemoApp } from "./app/optimization-demo/optimization-demo-app";
5+
import { isOptimizationDemoRoute } from "./app/optimization-demo/optimization-route";
46

57
export const DemoApp = () => {
68
if (isBrunchDemoRoute()) {
79
return <BrunchDemoApp />;
810
}
911

12+
if (isOptimizationDemoRoute()) {
13+
return <OptimizationDemoApp />;
14+
}
15+
1016
return <LocalStorageDemoApp />;
1117
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { PetrinautOptimizationContext } from "@hashintel/petrinaut/react";
2+
3+
import type { PetrinautOptimization } from "@hashintel/petrinaut-core";
4+
import type { FC, PropsWithChildren } from "react";
5+
6+
const wait = (durationMs: number) =>
7+
new Promise<void>((resolve) => window.setTimeout(resolve, durationMs));
8+
9+
const fakeOptimization: PetrinautOptimization = {
10+
async *optimize() {
11+
for (let value = 0; value < Number.MAX_SAFE_INTEGER; value += 1) {
12+
yield value;
13+
await wait(1_000);
14+
}
15+
},
16+
};
17+
18+
export const FakeOptimizationProvider: FC<PropsWithChildren> = ({
19+
children,
20+
}) => (
21+
<PetrinautOptimizationContext value={fakeOptimization}>
22+
{children}
23+
</PetrinautOptimizationContext>
24+
);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { LocalStorageDemoApp } from "../local-storage-demo/local-storage-demo-app";
2+
import { FakeOptimizationProvider } from "./fake-optimization-provider";
3+
4+
export const OptimizationDemoApp = () => (
5+
<FakeOptimizationProvider>
6+
<LocalStorageDemoApp />
7+
</FakeOptimizationProvider>
8+
);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const isOptimizationDemoRoute = (): boolean => {
2+
const path = window.location.pathname.replace(/\/+$/u, "") || "/";
3+
4+
return path === "/optimization";
5+
};

apps/petrinaut-website/vercel.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
{
1212
"source": "/brunch",
1313
"destination": "/"
14+
},
15+
{
16+
"source": "/optimization",
17+
"destination": "/"
1418
}
1519
],
1620
"functions": {

libs/@hashintel/petrinaut-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export type {
7878
PetrinautCommands,
7979
PetrinautMutations,
8080
} from "./instance";
81+
export type { PetrinautOptimization } from "./optimization";
8182
export { createPetrinautActions } from "./actions";
8283
export type {
8384
CreatePetrinautActionsOptions,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Host-provided optimization capability for Petrinaut.
3+
*
4+
* The initial contract intentionally has no inputs: experiment properties and
5+
* optimization configuration will be added as that integration is defined.
6+
*/
7+
export type PetrinautOptimization = {
8+
optimize(): AsyncIterable<number>;
9+
};

libs/@hashintel/petrinaut/docs/experiments.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Experiments live under the **Simulate** [global mode](drawing-a-net.md#global-mo
2222
| **Time step (dt)** | `1` | Same meaning as in single-run simulations (see [Simulation](simulation.md#time-step-dt)). |
2323
| **Max time (seconds)** | `180` | Each run advances until simulation time reaches this value, then completes. |
2424

25+
When Petrinaut is embedded with an optimization provider, the **Experiment** section also shows an **Optimization** toggle. The toggle is hidden when the host does not provide optimization, including on the default Petrinaut demo route. The demo site's `/optimization` route supplies a fake provider for previewing this surface. Optimization configuration and execution will be added in a later release.
26+
2527
The model used is a snapshot of the current net at the time you press **Run**. Editing the net afterwards does not change runs that have already started.
2628

2729
> Currently, an experiment can only run against one scenario at a time. To compare scenarios, create one experiment per scenario.

libs/@hashintel/petrinaut/src/react/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export {
1717
NetManagementContext,
1818
type NetManagement,
1919
} from "./net-management-context";
20+
export { PetrinautOptimizationContext } from "./optimization-context";
21+
export type { PetrinautOptimization } from "./optimization-context";
2022
export { ExperimentsContext, isExperimentActive } from "./experiments/context";
2123
export type {
2224
CreateExperimentInput,

0 commit comments

Comments
 (0)