The current aggregate values for num_nodes() and state_size() are useful, but they do not make it easy to identify which types of nodes are responsible for the count.
It'd be convenient to have access to the state size and counts by node type.
Below is an example implementation .
from __future__ import annotations
from collections import Counter
from typing import Any
from dwave.optimization import Model
def breakdown_model(model: Model) -> dict[str, Any]:
"""Return a structural breakdown of a D-Wave nonlinear optimization model."""
type_counts: Counter[str] = Counter()
type_state_sizes: Counter[str] = Counter()
for sym in model.iter_symbols():
kind = type(sym).__name__
type_counts[kind] += 1
type_state_sizes[kind] += sym.state_size()
return {
"num_nodes": model.num_nodes(),
"state_size": model.state_size(),
"counts_by_type": dict(type_counts),
"state_size_by_type": dict(type_state_sizes),
}
def build_example_model() -> Model:
model = Model()
x = model.integer((5, 4), lower_bound=0, upper_bound=10)
c = model.constant(1)
y = x + c
model.add_constraint(y.sum() <= 100)
model.minimize(y.sum())
return model
if __name__ == "__main__":
model = build_example_model()
breakdown = breakdown_model(model)
for key, value in breakdown.items():
print(f"{key}: {value}")
The current aggregate values for
num_nodes()andstate_size()are useful, but they do not make it easy to identify which types of nodes are responsible for the count.It'd be convenient to have access to the state size and counts by node type.
Below is an example implementation .