Skip to content

Conversation

@RobbieKiwi
Copy link
Contributor

@RobbieKiwi RobbieKiwi commented Dec 23, 2025

Closes #236

Changes proposed in this Pull Request

Allow the objective cost function to contain a constant. While the constant is not needed for optimization, it can make life easier in some cases (see linked issue for discussion).

It was previously forbidden to have a constant in the objective cost function, I assume this is because most solvers expect the ocf to only contain variables.

Now the objective can contain a constant if it is explicitly allowed with

model.add_objective(..., allow_constant=True)

Otherwise adding an objective with a constant will result in an error.

A bit of refactoring was required. To avoid having to repeat the same logic on multiple code paths and touch too much of the core solve logic, I made a decorator which strips the constant term before calling the Model.solve method and then adds it back again after.

@strip_and_replace_constant_objective
def solve(
    self,
    ...

Checklist

  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release_notes.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support constant values in objective

1 participant