dwavesystems / dwave-optimization

Enables the formulation of nonlinear models for industrial optimization problems.
https://docs.ocean.dwavesys.com/en/stable/docs_optimization/index.html#index-optimization
Apache License 2.0
7 stars 18 forks source link

Consider adding `Symbol.__str__()` overload #133

Open arcondello opened 1 month ago

arcondello commented 1 month ago

Currently we have Symbol.__repr__() implemented

In [1]: from dwave.optimization import Model

In [2]: model = Model()

In [3]: i = model.integer()

In [5]: repr(i)
Out[5]: '<dwave.optimization.symbols.IntegerVariable at 0x55ed9d929be0>'

However, we've had a few requests for something closer to dimod's CQM printing

In [6]: import dimod

In [7]: cqm = dimod.ConstrainedQuadraticModel()

In [8]: i = cqm.add_variable("INTEGER")

In [9]: j = cqm.add_variable("INTEGER")

In [17]: cqm.set_objective([(i, j, 1)])

In [18]: print(cqm)
Constrained quadratic model: 2 variables, 0 constraints, 3 biases

Objective
  Integer(1)*Integer(0)

Constraints

Bounds
  0.0 <= Integer(0) <= 9007199254740991.0
  0.0 <= Integer(1) <= 9007199254740991.0

Approaches Considered

I think don't want to print everything for every node, so maybe something like 1-nesting

Max(Sum(...), Constant(...))

or

Max(Sum, Constant)

or something like that.

We could also take inspiration from sympy which does print the whole thing

In [1]: from sympy import *

In [2]: x, y, z = symbols("x y z")

In [10]: out = x + y

In [11]: for _ in range(100):
    ...:     out = x*out + y
    ...: 

In [12]: out
Out[12]: x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x*(x + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) + y) 

Though how to handle say indexing gets a bit funny.

arcondello commented 1 month ago

This obviously interacts with https://github.com/dwavesystems/dwave-optimization/issues/17 as well.