Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion solution/dummy_solution.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package solution

import solution_status "github.com/MatProGo-dev/MatProInterface.go/solution/status"
import (
"github.com/MatProGo-dev/MatProInterface.go/problem"
solution_status "github.com/MatProGo-dev/MatProInterface.go/solution/status"
)

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

// The optimization problem that this solution is for
Problem *problem.OptimizationProblem

// The optimality gap returned from the solver. For many solvers, this is
// the gap between the best possible solution with integer relaxation and
// the best integer solution found so far.
Expand All @@ -28,3 +34,7 @@ func (ds *DummySolution) GetValueMap() map[uint64]float64 {
func (ds *DummySolution) GetStatus() solution_status.SolutionStatus {
return ds.Status
}

func (ds *DummySolution) GetProblem() *problem.OptimizationProblem {
return ds.Problem
}
4 changes: 4 additions & 0 deletions solution/solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package solution
import (
"fmt"

"github.com/MatProGo-dev/MatProInterface.go/problem"
solution_status "github.com/MatProGo-dev/MatProInterface.go/solution/status"
"github.com/MatProGo-dev/SymbolicMath.go/symbolic"
)
Expand All @@ -20,6 +21,9 @@ type Solution interface {
// GetStatus
//
GetStatus() solution_status.SolutionStatus

// GetProblem returns the optimization problem that this solution is for
GetProblem() *problem.OptimizationProblem
}

func ExtractValueOfVariableWithID(s Solution, idx uint64) (float64, error) {
Expand Down
60 changes: 60 additions & 0 deletions testing/solution/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"
"testing"

"github.com/MatProGo-dev/MatProInterface.go/problem"
"github.com/MatProGo-dev/MatProInterface.go/solution"
solution_status "github.com/MatProGo-dev/MatProInterface.go/solution/status"
"github.com/MatProGo-dev/SymbolicMath.go/symbolic"
Expand Down Expand Up @@ -317,3 +318,62 @@ func TestSolution_FindValueOfExpression5(t *testing.T) {
)
}
}

/*
TestSolution_GetProblem1
Description:

This function tests whether we can retrieve the problem from a solution.
*/
func TestSolution_GetProblem1(t *testing.T) {
// Constants
p := problem.NewProblem("TestProblem1")
v1 := p.AddVariable()

tempSol := solution.DummySolution{
Values: map[uint64]float64{
v1.ID: 2.1,
},
Objective: 2.3,
Status: solution_status.OPTIMAL,
Problem: p,
}

// Algorithm
retrievedProblem := tempSol.GetProblem()

// Verify the problem is the same
if retrievedProblem != p {
t.Errorf("Expected GetProblem to return the same problem pointer")
}

if retrievedProblem.Name != "TestProblem1" {
t.Errorf("Expected problem name to be 'TestProblem1'; received %v", retrievedProblem.Name)
}
}

/*
TestSolution_GetProblem2
Description:

This function tests whether GetProblem returns nil when no problem is set.
*/
func TestSolution_GetProblem2(t *testing.T) {
// Constants
tempSol := solution.DummySolution{
Values: map[uint64]float64{
0: 2.1,
},
Objective: 2.3,
Status: solution_status.OPTIMAL,
Problem: nil,
}

// Algorithm
retrievedProblem := tempSol.GetProblem()

// Verify the problem is nil
if retrievedProblem != nil {
t.Errorf("Expected GetProblem to return nil when no problem is set")
}
}