Skip to content

Commit

Permalink
Move States into a new states.pyx
Browse files Browse the repository at this point in the history
  • Loading branch information
arcondello committed Dec 2, 2024
1 parent a6f62d2 commit 36d39e5
Show file tree
Hide file tree
Showing 12 changed files with 649 additions and 544 deletions.
2 changes: 1 addition & 1 deletion docs/reference/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Model Methods
States Class
------------

.. currentmodule:: dwave.optimization.model
.. currentmodule:: dwave.optimization.states

.. autoclass:: States

Expand Down
36 changes: 1 addition & 35 deletions dwave/optimization/model.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ cdef class Model:
Objective = -4.0
"""

cdef readonly States states
cdef readonly object states
"""States of the model.
:ref:`States <intro_optimization_states>` represent assignments of values
Expand All @@ -83,35 +83,6 @@ cdef class Model:
cdef object _data_sources


cdef class States:
"""The states/solutions of the model."""

cdef void attach_states(self, vector[cppState]) noexcept
cdef vector[cppState] detach_states(self)
cpdef resolve(self)
cpdef Py_ssize_t size(self) except -1

cdef Model _model(self)

# In order to not create a circular reference, we only hold a weakref
# to the model from the states. This introduces some overhead, but it
# makes sure that the Model is promptly garbage collected
cdef readonly object _model_ref

# The state(s) of the model kept as a ragged vector-of-vectors (each
# cppState is a vector).
# Accessors should check the length of the state when accessing!
cdef vector[cppState] _states

# The number of views into the states that exist. The model cannot
# be unlocked while there are unsafe views
cdef Py_ssize_t _view_count

# Object that contains or will contain the information needed to construct states
cdef readonly object _future
cdef readonly object _result_hook


cdef class Symbol:
# Inheriting nodes must call this method from their __init__()
cdef void initialize_node(self, Model model, cppNode* node_ptr) noexcept
Expand Down Expand Up @@ -151,8 +122,3 @@ cdef class ArraySymbol(Symbol):
# a pointer to Node* and we can theoretically dynamic cast each time.
# But again, it's cheap and it simplifies things.
cdef cppArrayNode* array_ptr


cdef class StateView:
cdef readonly Py_ssize_t index # which state we're accessing
cdef readonly ArraySymbol symbol
27 changes: 0 additions & 27 deletions dwave/optimization/model.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -112,33 +112,6 @@ class Model:
def unlock(self): ...


class States:
def __init__(self, model: Model): ...
def __len__(self) -> int: ...
def clear(self): ...

def from_file(
self,
file: typing.Union[typing.BinaryIO, collections.abc.ByteString, str],
*,
replace: bool = True,
check_header: bool = True,
) -> Model: ...

def from_future(self, future: object, result_hook: collections.abc.Callable): ...
def initialize(self): ...

def into_file(
self,
file: typing.Union[typing.BinaryIO, collections.abc.ByteString, str],
): ...

def resize(self, n: int): ...
def resolve(self): ...
def size(self) -> int: ...
def to_file(self) -> typing.BinaryIO: ...


class Symbol:
def __init__(self, *args, **kwargs) -> typing.NoReturn: ...
def equals(self, other: Symbol) -> bool: ...
Expand Down
Loading

0 comments on commit 36d39e5

Please sign in to comment.