No WS4 command here uses shell magic: every one goes through this helper, which is also how you would drive the command line from any script.

This one is a command-line session.

[1]:
import subprocess


def run(command: str) -> None:
    """Run a WS4 command, printing what it printed."""
    result = subprocess.run(command, shell=True, text=True, capture_output=True)
    print((result.stdout or result.stderr).strip())
    result.check_returncode()

Route products to mills

The sample estate ships a road network with two mills at opposite ends, so the least-haul-cost destination differs across the estate. This notebook routes a schedule and reads the accounts the routing produces, then constrains the haul cost in a search.

The network

One access vertex per grid row, a road along them, a spur to destination.MILL1 at the west end and one to destination.MILL2 at the east end. examples/estate/route.yaml names the four tables and defines the route accounts: haul and volume per mill, and a total haul.

[2]:
from pathlib import Path

import pandas as pd

work = Path("tmp/examples/routes")
work.mkdir(parents=True, exist_ok=True)
schedule = pd.DataFrame({"block_id": [1, 2, 9, 10, 57, 58, 63, 64], "treatment": ["CC"] * 8, "offset": [0] * 8})
(work / "schedule.csv").write_text(schedule.to_csv(index=False))
[2]:
87
[3]:
run("ws4 simulate --dataset tmp/examples/estate/compiled --schedule tmp/examples/routes/schedule.csv"
    " --years 60 --topology examples/estate/topology.csv"
    " --route-config examples/estate/route.yaml --out tmp/examples/routes/run")
Simulation complete → tmp/examples/routes/run
[4]:
routed = pd.read_csv(work / "run" / "route_values.csv")
print(routed.groupby("destination")[["volume", "length_m", "haul_cost"]].sum().to_string())
print()
print(routed[["block_id", "destination", "volume", "length_m", "haul_cost"]].to_string(index=False))
                        volume  length_m  haul_cost
destination
destination.MILL1  3423.514286    8000.0        0.4
destination.MILL2  5370.300000   12000.0        0.6

 block_id       destination      volume  length_m  haul_cost
        1 destination.MILL1 1918.714286    4000.0       0.20
        2 destination.MILL1 1504.800000    4000.0       0.20
        9 destination.MILL2 1272.000000    4000.0       0.20
       10 destination.MILL2 1932.000000    4000.0       0.20
       57 destination.MILL2  243.000000    1000.0       0.05
       58 destination.MILL2  294.300000    1000.0       0.05
       63 destination.MILL2  684.000000    1000.0       0.05
       64 destination.MILL2  945.000000    1000.0       0.05

Every load went to the cheaper mill for its block: the choice is the sum of the segment haul rates along the route, so a block near the middle can be closer to one mill in distance and still cheaper to send to the other.

[5]:
values = pd.read_csv(work / "run" / "account_values.csv")
print(values[(values.offset == 0) & values.account.str.startswith(("route.", "product.Mill"))].to_string(index=False))
 offset             account       value
      0 product.Mill1.Yield 3423.514286
      0 product.Mill2.Yield 5370.300000
      0    route.Mill1.haul  684.702857
      0    route.Mill2.haul  749.115000
      0    route.Total.haul 1433.817857

Where to look next

  • docs/howto/route-products.rst walks through the tables and the route configuration.

  • docs/reference/roads.rst specifies the network, the routing and the route outputs.