@@ -119,28 +119,6 @@ class Model(_Graph):
119119 >>> model = flow_shop_scheduling(processing_times=processing_times)
120120 """
121121
122- objective : typing .Optional [ArraySymbol ]
123- """Objective to be minimized.
124-
125- Examples:
126- This example prints the value of the objective of a model representing
127- the simple polynomial, :math:`y = i^2 - 4i`, for a state with value
128- :math:`i=2.0`.
129-
130- >>> from dwave.optimization import Model
131- ...
132- >>> model = Model()
133- >>> i = model.integer(lower_bound=-5, upper_bound=5)
134- >>> c = model.constant(4)
135- >>> y = i**2 - c*i
136- >>> model.minimize(y)
137- >>> with model.lock():
138- ... model.states.resize(1)
139- ... i.set_state(0, 2.0)
140- ... print(f"Objective = {model.objective.state(0)}")
141- Objective = -4.0
142- """
143-
144122 states : States
145123 """States of the model.
146124
@@ -154,9 +132,37 @@ class Model(_Graph):
154132 """
155133
156134 def __init__ (self ):
157- self .objective = None
135+ self ._objective = None
158136 self .states = States (self )
159137
138+ @property
139+ def objective (self ) -> typing .Optional [ArraySymbol ]:
140+ """Objective to be minimized.
141+
142+ Examples:
143+ This example prints the value of the objective of a model representing
144+ the simple polynomial, :math:`y = i^2 - 4i`, for a state with value
145+ :math:`i=2.0`.
146+
147+ >>> from dwave.optimization import Model
148+ ...
149+ >>> model = Model()
150+ >>> i = model.integer(lower_bound=-5, upper_bound=5)
151+ >>> c = model.constant(4)
152+ >>> y = i**2 - c*i
153+ >>> model.minimize(y)
154+ >>> with model.lock():
155+ ... model.states.resize(1)
156+ ... i.set_state(0, 2.0)
157+ ... print(f"Objective = {model.objective.state(0)}")
158+ Objective = -4.0
159+ """
160+ return self ._objective
161+
162+ @objective .setter
163+ def objective (self , value : ArraySymbol ):
164+ self .minimize (value )
165+
160166 def binary (self , shape : typing .Optional [_ShapeLike ] = None ) -> BinaryVariable :
161167 r"""Create a binary symbol as a decision variable.
162168
@@ -463,7 +469,7 @@ def lock(self) -> contextlib.AbstractContextManager:
463469 def minimize (self , value : ArraySymbol ):
464470 # inherit the docstring from _Graph
465471 super ().minimize (value )
466- self .objective = value
472+ self ._objective = value
467473
468474 # dev note: the typing is underspecified, but it would be quite complex to fully
469475 # specify the linear/quadratic so let's leave it alone for now.
0 commit comments