Skip to content

Commit 13ee85f

Browse files
Add GetProblem method to Solution interface and DummySolution implementation
Co-authored-by: kwesiRutledge <[email protected]>
1 parent 79f9779 commit 13ee85f

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

solution/dummy_solution.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package solution
22

3-
import solution_status "github.com/MatProGo-dev/MatProInterface.go/solution/status"
3+
import (
4+
"github.com/MatProGo-dev/MatProInterface.go/problem"
5+
solution_status "github.com/MatProGo-dev/MatProInterface.go/solution/status"
6+
)
47

58
type DummySolution struct {
69
Values map[uint64]float64
@@ -11,6 +14,9 @@ type DummySolution struct {
1114
// Whether or not the solution is within the optimality threshold
1215
Status solution_status.SolutionStatus
1316

17+
// The optimization problem that this solution is for
18+
Problem *problem.OptimizationProblem
19+
1420
// The optimality gap returned from the solver. For many solvers, this is
1521
// the gap between the best possible solution with integer relaxation and
1622
// the best integer solution found so far.
@@ -28,3 +34,7 @@ func (ds *DummySolution) GetValueMap() map[uint64]float64 {
2834
func (ds *DummySolution) GetStatus() solution_status.SolutionStatus {
2935
return ds.Status
3036
}
37+
38+
func (ds *DummySolution) GetProblem() *problem.OptimizationProblem {
39+
return ds.Problem
40+
}

solution/solution.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package solution
33
import (
44
"fmt"
55

6+
"github.com/MatProGo-dev/MatProInterface.go/problem"
67
solution_status "github.com/MatProGo-dev/MatProInterface.go/solution/status"
78
"github.com/MatProGo-dev/SymbolicMath.go/symbolic"
89
)
@@ -20,6 +21,9 @@ type Solution interface {
2021
// GetStatus
2122
//
2223
GetStatus() solution_status.SolutionStatus
24+
25+
// GetProblem returns the optimization problem that this solution is for
26+
GetProblem() *problem.OptimizationProblem
2327
}
2428

2529
func ExtractValueOfVariableWithID(s Solution, idx uint64) (float64, error) {

testing/solution/solution_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"strings"
55
"testing"
66

7+
"github.com/MatProGo-dev/MatProInterface.go/problem"
78
"github.com/MatProGo-dev/MatProInterface.go/solution"
89
solution_status "github.com/MatProGo-dev/MatProInterface.go/solution/status"
910
"github.com/MatProGo-dev/SymbolicMath.go/symbolic"
@@ -317,3 +318,62 @@ func TestSolution_FindValueOfExpression5(t *testing.T) {
317318
)
318319
}
319320
}
321+
322+
/*
323+
TestSolution_GetProblem1
324+
Description:
325+
326+
This function tests whether we can retrieve the problem from a solution.
327+
*/
328+
func TestSolution_GetProblem1(t *testing.T) {
329+
// Constants
330+
p := problem.NewProblem("TestProblem1")
331+
v1 := p.AddVariable()
332+
333+
tempSol := solution.DummySolution{
334+
Values: map[uint64]float64{
335+
v1.ID: 2.1,
336+
},
337+
Objective: 2.3,
338+
Status: solution_status.OPTIMAL,
339+
Problem: p,
340+
}
341+
342+
// Algorithm
343+
retrievedProblem := tempSol.GetProblem()
344+
345+
// Verify the problem is the same
346+
if retrievedProblem != p {
347+
t.Errorf("Expected GetProblem to return the same problem pointer")
348+
}
349+
350+
if retrievedProblem.Name != "TestProblem1" {
351+
t.Errorf("Expected problem name to be 'TestProblem1'; received %v", retrievedProblem.Name)
352+
}
353+
}
354+
355+
/*
356+
TestSolution_GetProblem2
357+
Description:
358+
359+
This function tests whether GetProblem returns nil when no problem is set.
360+
*/
361+
func TestSolution_GetProblem2(t *testing.T) {
362+
// Constants
363+
tempSol := solution.DummySolution{
364+
Values: map[uint64]float64{
365+
0: 2.1,
366+
},
367+
Objective: 2.3,
368+
Status: solution_status.OPTIMAL,
369+
Problem: nil,
370+
}
371+
372+
// Algorithm
373+
retrievedProblem := tempSol.GetProblem()
374+
375+
// Verify the problem is nil
376+
if retrievedProblem != nil {
377+
t.Errorf("Expected GetProblem to return nil when no problem is set")
378+
}
379+
}

0 commit comments

Comments
 (0)