Skip to content

Consider adding more detailed state size and symbol break down #583

Description

@p-ortmann

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}")

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions