@@ -268,11 +268,7 @@ def objective(self) -> Objective:
268268 def objective (
269269 self , obj : Objective | LinearExpression | QuadraticExpression
270270 ) -> Objective :
271- if not isinstance (obj , Objective ):
272- expr = obj
273- else :
274- expr = obj .expression
275- self .add_objective (expr = expr , overwrite = True , allow_constant = False )
271+ self .add_objective (expr = obj , overwrite = True , allow_constant = False )
276272 return self ._objective
277273
278274 @property
@@ -782,23 +778,46 @@ def add_constraints(
782778 self .constraints .add (constraint )
783779 return constraint
784780
781+ @overload
782+ def add_objective (
783+ self ,
784+ expr : Objective ,
785+ sense : None = None ,
786+ overwrite : bool = False ,
787+ allow_constant : bool = False ,
788+ ) -> None : ...
789+
790+ @overload
785791 def add_objective (
786792 self ,
787793 expr : Variable
788794 | LinearExpression
789795 | QuadraticExpression
790796 | Sequence [tuple [ConstantLike , VariableLike ]],
797+ sense : Literal ["min" , "max" ] | None = None ,
798+ overwrite : bool = False ,
799+ allow_constant : bool = False ,
800+ ) -> None : ...
801+
802+ def add_objective (
803+ self ,
804+ expr : Variable
805+ | LinearExpression
806+ | QuadraticExpression
807+ | Sequence [tuple [ConstantLike , VariableLike ]]
808+ | Objective ,
809+ sense : Literal ["min" , "max" ] | None = None ,
791810 overwrite : bool = False ,
792- sense : str = "min" ,
793811 allow_constant : bool = False ,
794812 ) -> None :
795813 """
796814 Add an objective function to the model.
797815
798816 Parameters
799817 ----------
800- expr : linopy.LinearExpression, linopy.QuadraticExpression
818+ expr : linopy.Variable, linopy. LinearExpression, linopy.QuadraticExpression, Objective
801819 Expression describing the objective function.
820+ sense: "min" or "max", the sense to optimize for. Defaults to min. Cannot be set if passing Objective directly
802821 overwrite : False, optional
803822 Whether to overwrite the existing objective. The default is False.
804823 allow_constant: bool, optional
@@ -814,15 +833,20 @@ def add_objective(
814833 "Objective already defined."
815834 " Set `overwrite` to True to force overwriting."
816835 )
817- if isinstance (expr , Variable ):
818- expr = 1 * expr
819836
820- if not allow_constant and expr .has_constant :
837+ if isinstance (expr , Objective ):
838+ assert sense is None , "Cannot set sense if objective object is passed"
839+ objective = expr
840+ assert objective .model == self
841+ else :
842+ sense = sense or "min"
843+ objective = Objective (expression = expr , model = self , sense = sense )
844+
845+ if not allow_constant and objective .expression .has_constant :
821846 raise ConstantObjectiveError (
822847 "Objective contains constant term. Either remove constants from the expression with expr.drop_constants() or use model.add_objective(..., allow_constant=True)." ,
823848 )
824849
825- objective = Objective (expression = expr , model = self , sense = sense )
826850 self ._objective = objective
827851
828852 def remove_variables (self , name : str ) -> None :
0 commit comments