Skip to content

Run your first trade study

In this tutorial, you’ll compare battery and charger combinations for two workshop outage durations. You’ll validate the study and read its feasibility results.

1. Validate the example

Install Paretorium and download the examples. Run the following from the folder containing studies/:

python -m paretorium studies.example_backup --check

Expect Validated workshop_backup. The runner checked the model inputs and dependencies for each compatible concept without drawing samples.

2. Run the study

python -m paretorium studies.example_backup \
  --samples 100 --seed 42 --summary-only --out runs/example-backup.json

Open runs/example-backup.json. Under rejected, find the large battery with the basic charger. Under summaries, find the three compatible concepts evaluated in each of the two outage scenarios. Compare the feasibility_rate for the small battery with the regulated charger between short_outage and long_outage: it meets the reserve limit in fewer long-outage samples.

3. Find the declarations

In the source below, find the charger_rating rule and the two Scenario declarations. They are where the rejection and the two durations came from. When you’re ready to write your own module, follow Author a study. To see this run as a chart and a table, continue to Create your first tradespace.

"""Editable example: screen backup power systems for a small workshop.

All numbers are illustrative, not equipment specifications.
"""

from collections.abc import Mapping

from paretorium import (
    CompatibilityRule,
    Concept,
    Decision,
    DesignSpace,
    Metric,
    Model,
    Option,
    Parameter,
    ParameterCase,
    Port,
    Scenario,
    Study,
    Triangular,
)


def demand(_concept: Concept, inputs: Mapping[str, float]) -> dict[str, float]:
    return {"demand_wh": inputs["load_w"] * inputs["duration_h"]}


def supply(concept: Concept, inputs: Mapping[str, float]) -> dict[str, float]:
    capacity, mass, price = {
        "small": (850, 9, 430),
        "large": (1500, 18, 710),
    }[concept["battery"]]
    charger_price = {"basic": 0, "regulated": 140}[concept["charger"]]
    return {
        "usable_wh": capacity * inputs["usable_fraction"],
        "mass_kg": mass,
        "cost_usd": price + charger_price,
    }


def balance(
    _concept: Concept, inputs: Mapping[str, float]
) -> dict[str, float]:
    return {"reserve_wh": inputs["usable_wh"] - inputs["demand_wh"]}


study = Study(
    id="workshop_backup",
    design_space=DesignSpace(
        decisions=(
            Decision("battery", (Option("small"), Option("large"))),
            Decision("charger", (Option("basic"), Option("regulated"))),
        ),
        rules=(
            CompatibilityRule(
                "charger_rating",
                lambda c: (
                    c["battery"] != "large" or c["charger"] == "regulated"
                ),
                "The basic charger is not rated for the large battery.",
            ),
        ),
    ),
    parameters=(
        Parameter(
            "load_w",
            "Average load",
            "W",
            Triangular(180, 220, 270),
            source="Illustrative demand range.",
        ),
        Parameter("duration_h", "Backup duration", "h", 2),
        Parameter(
            "usable_fraction",
            "Usable capacity fraction",
            "1",
            0.72,
            cases=(ParameterCase({"charger": "regulated"}, 0.88),),
        ),
    ),
    # Order doesn't matter: the graph puts demand and supply before balance.
    models=(
        Model(
            "balance",
            (Port("demand_wh", "Wh"), Port("usable_wh", "Wh")),
            (Port("reserve_wh", "Wh"),),
            balance,
        ),
        Model(
            "demand",
            (Port("load_w", "W"), Port("duration_h", "h")),
            (Port("demand_wh", "Wh"),),
            demand,
        ),
        Model(
            "supply",
            (Port("usable_fraction", "1"),),
            (
                Port("usable_wh", "Wh"),
                Port("mass_kg", "kg"),
                Port("cost_usd", "USD"),
            ),
            supply,
        ),
    ),
    metrics=(
        Metric("cost_usd", "USD", objective="min"),
        Metric("mass_kg", "kg", objective="min"),
        Metric("reserve_wh", "Wh", objective="max", minimum=0),
    ),
    scenarios=(
        Scenario("short_outage"),
        Scenario("long_outage", overrides={"duration_h": 3.5}),
    ),
)