Consider adding more detailed state size and symbol break down

Open
#583 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
55/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
cpp, python

Research direction

Start with the Model.iter_symbols(), num_nodes(), and state_size() entry points shown in the issue, then compare them with the provided breakdown_model example. Done means exposing counts and state sizes grouped by symbol type while preserving the existing aggregate values; validate the behavior using the build_example_model example.

Written by the indexing model from the issue text.

Description

enhancement

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}")
Dominant language
C++
Stars
31
Forks
36
Avg merge
1d 9h
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dwavesystems/dwave-optimization

All issues in dwavesystems/dwave-optimization

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.