Battery Grammar To Optimizer

Source: examples/optimization/battery_grammar_to_optimizer.py

Introduction

Inspect the packaged rectangular battery optimization benchmark.

Technical Implementation

This page is generated from the top-of-file module docstring and the example source code. The full script is included below for direct inspection.

 1from __future__ import annotations
 2
 3import design_research_problems as derp
 4
 5
 6def _format_config(problem: object, variables: object) -> str:
 7    """Return the rounded ``SxP`` label for one battery design vector.
 8
 9    Args:
10        problem: Optimization problem exposing ``decode_candidate``.
11        variables: Candidate design vector to decode.
12
13    Returns:
14        Short ``SxP`` battery-pack label.
15    """
16    state = problem.decode_candidate(variables)
17    return f"{state.series_count}S{state.parallel_count}P"
18
19
20def main() -> None:
21    """Print a seeded start and the solved rectangular battery design."""
22    problem = derp.get_problem("battery_pack_18650_series_parallel_cost_min")
23
24    print("Rectangular battery packaged benchmark")
25    print(problem.metadata.title)
26
27    try:
28        initial = problem.generate_initial_solution(seed=1)
29        initial_components = problem.objective_components(initial)
30        initial_violation = problem.max_constraint_violation(initial)
31        result = problem.solve()
32        solved_components = problem.objective_components(result.x)
33        solved_violation = problem.max_constraint_violation(result.x)
34    except derp.MissingOptionalDependencyError as exc:
35        print(exc)
36        return
37
38    print(
39        "initial",
40        f"config={_format_config(problem, initial)}",
41        f"cost_usd={initial_components['cost_usd']:.2f}",
42        f"voltage_v={initial_components['voltage_v']:.2f}",
43        f"capacity_ah={initial_components['capacity_ah']:.2f}",
44        f"current_limit_a={initial_components['current_limit_a']:.2f}",
45        f"violation={initial_violation:.3g}",
46    )
47    print("solve", result.message)
48    print(
49        "solved",
50        f"config={_format_config(problem, result.x)}",
51        f"cost_usd={solved_components['cost_usd']:.2f}",
52        f"voltage_v={solved_components['voltage_v']:.2f}",
53        f"capacity_ah={solved_components['capacity_ah']:.2f}",
54        f"current_limit_a={solved_components['current_limit_a']:.2f}",
55        f"violation={solved_violation:.3g}",
56    )
57
58
59if __name__ == "__main__":
60    main()

Expected Results

Run Command

PYTHONPATH=src python3 examples/optimization/battery_grammar_to_optimizer.py

Run the command shown below from repository root. Output should summarize the problem setup, a baseline solution, or diagnostic values relevant to this example.