-
Notifications
You must be signed in to change notification settings - Fork 15
Set up simulation framework #664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
48ab8a7
Create draft for simulation data types
Nils1729 886525f
WIP: Simulate unloading of vehicles
Nils1729 3840093
WIP add debug helpers and make simulation work
Nils1729 9c90b75
WIP add typings and lint
Nils1729 fe0e5d2
Validate events
Nils1729 8c78f97
Fix never parameter in activity and behavior functions
Nils1729 82f73d1
Validate activities and behaviors
Nils1729 e70d878
Introduce pseudo randomness for deterministic uuids
Nils1729 238f094
Add migrations for simulation
Nils1729 9e1279a
Rename SimulationActivity.terminate to onTerminate
Nils1729 ea96dbe
Remove simulatedRegionId from VehicleArrivedEvent
Nils1729 e1071d0
Slight improvement to debugging buttons
Nils1729 ff63b98
Merge branch 'dev' into feature/661-set-up-simulation-framework
Nils1729 fd3a924
Fix typo
Nils1729 7f70a01
Validate state in AddBehaviorAction
Nils1729 df72ca4
Incorporate suggestions from review
Nils1729 5e3ecc9
Fix randomness
Nils1729 f32df1f
Add generic DelayEventActivity
Nils1729 5e17077
Fix DelayEventActivity
Nils1729 eaca4fa
Please linter
Nils1729 0c94f05
Remove debug UI
Nils1729 6aa58e4
Merge branch 'dev' into feature/661-set-up-simulation-framework
Nils1729 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,93 +1,67 @@ | ||
| /* eslint-disable no-bitwise */ | ||
| import { | ||
| ArrayMaxSize, | ||
| ArrayMinSize, | ||
| IsArray, | ||
| IsInt, | ||
| Max, | ||
| Min, | ||
| ValidateIf, | ||
| } from 'class-validator'; | ||
| import { IsInt, Min, ValidateIf } from 'class-validator'; | ||
| import hash from 'hash.js'; | ||
| import { v4 } from 'uuid'; | ||
| import { getCreate } from '../../models/utils'; | ||
| import type { ExerciseState } from '../../state'; | ||
| import type { Mutable, UUID } from '../../utils'; | ||
| import { IsLiteralUnion, IsValue } from '../../utils/validators'; | ||
|
|
||
| // 4 unsigned 32-bit integers | ||
| type Sfc32state = readonly [number, number, number, number]; | ||
|
|
||
| export class RandomState { | ||
| @IsValue('randomState' as const) | ||
| readonly type = 'randomState'; | ||
|
|
||
| @IsLiteralUnion({ sfc32: true }) | ||
| readonly algo = 'sfc32'; | ||
|
|
||
| @ValidateIf((o) => o.algo === 'sfc32') | ||
| @IsArray() | ||
| @ArrayMinSize(4) | ||
| @ArrayMaxSize(4) | ||
| @IsInt({ each: true }) | ||
| @Min(0, { each: true }) | ||
| @Max(4294967295 /* UINT32_MAX */, { each: true }) | ||
| readonly sfc32state!: Sfc32state; | ||
| @IsLiteralUnion({ 'sha256-id-ctr': true }) | ||
| readonly algo = 'sha256-id-ctr'; | ||
|
|
||
| /** | ||
| * @deprecated Use {@link create} instead | ||
| */ | ||
| constructor(state: Sfc32state) { | ||
| this.sfc32state = state; | ||
| } | ||
| @ValidateIf((o) => o.algo === 'sha256-id-ctr') | ||
| @IsInt() | ||
| @Min(0) | ||
| readonly counter: number = 0; | ||
|
|
||
| static readonly create = getCreate(this); | ||
| } | ||
|
|
||
| export function seededRandomState() { | ||
| return RandomState.create( | ||
| Array.from({ length: 4 }).map(() => | ||
| Math.trunc(Math.random() * 4294967295) | ||
| ) as unknown as Sfc32state | ||
| ); | ||
| return RandomState.create(); | ||
| } | ||
|
|
||
| export function nextBool( | ||
| draftState: Mutable<ExerciseState>, | ||
| probability: number = 0.5 | ||
| ): boolean { | ||
| return nextInt(draftState, 4294967296) / 4294967296 < probability; | ||
| return nextInt(draftState, 4294967295) / 4294967296 < probability; | ||
| } | ||
|
|
||
| export function nextUUID(draftState: Mutable<ExerciseState>): UUID { | ||
| return v4({ | ||
| random: Array.from({ length: 16 }).map(() => nextInt(draftState, 256)), | ||
| random: advance(draftState), | ||
| }); | ||
| } | ||
|
|
||
| // Returns 0 to UINT32_MAX | ||
| export function nextInt( | ||
| draftState: Mutable<ExerciseState>, | ||
| upperBound: number | ||
| ): number { | ||
| return Math.trunc(advance(draftState) % upperBound); | ||
| const state = advance(draftState) | ||
| .slice(4) | ||
|
lukasrad02 marked this conversation as resolved.
Outdated
|
||
| .map((b, i) => Math.trunc(b * 256 ** i)) | ||
| .reduce((a, b) => a | b); | ||
| return Math.trunc(state % upperBound); | ||
| } | ||
|
|
||
| function advance(draftState: Mutable<ExerciseState>): number { | ||
| // Returns 32 numbers from 0-255 | ||
|
lukasrad02 marked this conversation as resolved.
Outdated
|
||
| function advance(draftState: Mutable<ExerciseState>): number[] { | ||
| const state = draftState.randomState; | ||
| let [a, b, c, d] = state.sfc32state; | ||
|
|
||
| // Adapted from https://github.com/bryc/code/blob/master/jshash/PRNGs.md#sfc32 | ||
| a = Math.trunc(a); | ||
| b = Math.trunc(b); | ||
| c = Math.trunc(c); | ||
| d = Math.trunc(d); | ||
| const t = Math.trunc(Math.trunc(a + b) + d); | ||
| d = Math.trunc(d + 1); | ||
| a = b ^ (b >>> 9); | ||
| b = Math.trunc(c + (c << 3)); | ||
| c = (c << 21) | (c >>> 11); | ||
| c = Math.trunc(c + t); | ||
| const result = t >>> 0; | ||
| state.sfc32state = [a, b, c, d]; | ||
| const result = hash | ||
| .sha256() | ||
| .update(draftState.id) | ||
| .update(state.counter.toString()) | ||
| .digest() | ||
| .map((b) => b & 0xff); | ||
|
|
||
| state.counter++; | ||
| return result; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.