Build on the Last Value
Make each value depend on the one before it, as a price that drifts: five products are priced every second, and each price is the product's last price plus a small step.
Before You Start
-
The commands run from the root of a clone of the repository, where the files are in
docs/snippets/generate-data/. The Full Files block of this page holds the same files, for a reader who saves them by hand. -
First Simulation explains arrivals, tasks and the records a run writes.
previouslists the keys of the generator that reads the last value, andadd,minandmaxlist the generators that combine values.
Steps
Start Five Products
count: 5 starts five products from one arrival, and each is priced every second. Each keeps its own last payload, so previous reads the last value of the same field for the same product.
| docs/snippets/generate-data/build_on_the_last_value_builder.py:arrival | |
|---|---|
A Builder arrival has no count, so the file adds one arrival for each product.
Sampler(seed=42) makes the two draws. Each generator is named by the place of its draw in the blueprint, so the steps are the blueprint's. A product is priced every second, so there is no gap to draw.
Read the Last Value
previous with path: price reads the product's last price. default: 20 is the value before the first record. add adds a draw between -1 and 1, so the price drifts by up to 1 each second. The stock level adds a normal step to its last value, and min and max keep it between 0 and 500 units.
A Low-level script keeps each product's last values itself, in the dictionary last, because previous cannot be read outside a task. It draws the two steps from Sampler.generator for the places the blueprint uses, so the draws are the blueprint's.
Check the Result
Run the model for 30 simulated seconds and print its first 6 records. The five products are priced at 1 second, each near 20, and product-0 is priced again at 2 seconds within 1 of its first price.
This prints every record of the run as an [EVT] line with the same fields and values.
This prints every record of the run as an [EVT] line with the same fields and values.
The docs test runs the model for 5,000 simulated seconds: no price differs from the product's last price by more than 1, and the stock reaches both 0 and 500 but never leaves that range.