dwavesystems / dwavesystems/dwave-optimization

Consider adding more detailed state size and symbol break down

Open
#583 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
C++
Stars
31
Forks
36
Avg merge
16h 55m
Merged PRs (30d)
8

Description

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 .

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.