Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 14, 2025

Overview

This PR implements the GetOptimalObjectiveValue function as requested in issue #25. The function evaluates the objective function of an optimization problem at a given solution point by leveraging the existing FindValueOfExpression function.

Implementation

The new function has the following signature:

func GetOptimalObjectiveValue(sol Solution) (float64, error)

The function works by:

  1. Retrieving the optimization problem from the solution using GetProblem()
  2. Extracting the objective expression from problem.Objective.Expression
  3. Using the existing FindValueOfExpression function to substitute all variables in the objective expression with their values from the solution
  4. Returning the computed objective value

Example Usage

// Create an optimization problem
p := problem.NewProblem("Example")
x := p.AddVariable()
y := p.AddVariable()

// Set objective: minimize 5*x + 3*y
obj := x.Multiply(symbolic.K(5.0)).Plus(y.Multiply(symbolic.K(3.0)))
p.SetObjective(obj, problem.SenseMinimize)

// Create a solution where x=2, y=4
sol := solution.DummySolution{
    Values: map[uint64]float64{
        x.ID: 2.0,
        y.ID: 4.0,
    },
    Status: solution_status.OPTIMAL,
    Problem: p,
}

// Calculate the objective value at this solution point
objValue, err := solution.GetOptimalObjectiveValue(&sol)
// Returns: 22.0 (which is 5*2 + 3*4)

Error Handling

The function includes proper error handling for:

  • Solutions without an associated problem
  • Problems without a defined objective
  • Variables missing from the solution (propagated from FindValueOfExpression)

Testing

Added four comprehensive test cases covering:

  • Simple linear objectives with multiple variables
  • Constant objective functions
  • Complex expressions with multiple operations
  • Error cases (nil problem reference)

All existing tests continue to pass, confirming no regressions were introduced.

Closes #25

Original prompt

Can you attempt to create the GetOptimalObjectiveValue function by using the FindValueOfExpression function that is currently built for solution.Solution interfaces? Your solution should follow the instructions and signature listed in issue #25.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Add GetOptimalObjectiveValue using FindValueOfExpression Implement GetOptimalObjectiveValue function to evaluate objective at solution point Oct 14, 2025
Copilot AI requested a review from kwesiRutledge October 14, 2025 03:02
Copilot finished work on behalf of kwesiRutledge October 14, 2025 03:02
…27)

* Initial plan

* Change FindValueOfExpression signature to return symbolic.Expression

Co-authored-by: kwesiRutledge <[email protected]>

* Add helper function to reduce code duplication in tests

Co-authored-by: kwesiRutledge <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: kwesiRutledge <[email protected]>
@kwesiRutledge kwesiRutledge marked this pull request as ready for review October 14, 2025 23:16
@codecov
Copy link

codecov bot commented Oct 14, 2025

Codecov Report

❌ Patch coverage is 64.70588% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.33%. Comparing base (0214315) to head (3023d9b).
⚠️ Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
solution/solution.go 64.70% 4 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #26      +/-   ##
==========================================
+ Coverage   86.76%   87.33%   +0.57%     
==========================================
  Files          27       35       +8     
  Lines        3687     4185     +498     
==========================================
+ Hits         3199     3655     +456     
- Misses        440      474      +34     
- Partials       48       56       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kwesiRutledge kwesiRutledge merged commit ea9d244 into main Oct 15, 2025
4 of 5 checks passed
@kwesiRutledge kwesiRutledge deleted the copilot/create-get-optimal-objective-value-function branch October 15, 2025 00:51
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.

Create a Method for Extracting the Objective Value at the Solution Point

2 participants