Skip to content

Commit 31a40a6

Browse files
committed
Created a simple String method for the problem and test it
1 parent dc646b5 commit 31a40a6

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

problem/optimization_problem.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,3 +994,43 @@ func (op *OptimizationProblem) MakeNotWellDefinedError() ope.NotWellDefinedError
994994
ErrorSource: op.Check(),
995995
}
996996
}
997+
998+
/*
999+
String
1000+
Description:
1001+
1002+
Creates a string for the problem.
1003+
*/
1004+
func (op *OptimizationProblem) String() string {
1005+
// Create string for the objective
1006+
objString := fmt.Sprintf("\n%v\n\t%v\n", op.Objective.Sense, op.Objective.Expression)
1007+
1008+
// Create the constraints string
1009+
constraintsString := "subject to\n"
1010+
nVectorConstraints := 0
1011+
nMatrixConstraints := 0
1012+
for ii, constraint := range op.Constraints {
1013+
switch constraint.(type) {
1014+
case symbolic.ScalarConstraint:
1015+
constraintsString += fmt.Sprintf(
1016+
"\tConstraint #%v. %v\n",
1017+
ii,
1018+
constraint,
1019+
)
1020+
case symbolic.VectorConstraint:
1021+
nVectorConstraints += 1
1022+
case symbolic.MatrixConstraint:
1023+
nMatrixConstraints += 1
1024+
}
1025+
}
1026+
1027+
// Add a text summary of the vector and matrix constraints
1028+
// TODO(Kwesi): Create a String() method for vector and matrix constraints.
1029+
constraintsString += fmt.Sprintf(
1030+
"\t\tin addition to %v vector constraints and %v matrix constraints.",
1031+
nVectorConstraints,
1032+
nMatrixConstraints,
1033+
)
1034+
1035+
return objString + constraintsString
1036+
}

testing/problem/optimization_problem_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3149,3 +3149,56 @@ func TestOptimizationProblem_CopyVariable1(t *testing.T) {
31493149
)
31503150
}
31513151
}
3152+
3153+
/*
3154+
TestOptimizationProblem_String1
3155+
Description:
3156+
3157+
Tests that a small optimization problem with all scalar constraints gets represented
3158+
as a string with:
3159+
- Minimize sense of objective
3160+
- The objective expression is completely contained in the string
3161+
- the string describes that there are 0 vector constraints and 0 matrix constraints
3162+
*/
3163+
func TestOptimizationProblem_String1(t *testing.T) {
3164+
// Create Optimization Problem
3165+
p := problem.NewProblem("TestOptimizationProblem_String1")
3166+
3167+
N := 2
3168+
x := p.AddVariableVector(N)
3169+
c := symbolic.OnesVector(N)
3170+
objExpr := x.Transpose().Multiply(c)
3171+
p.SetObjective(objExpr, problem.SenseMinimize)
3172+
3173+
p.Constraints = append(p.Constraints, x.AtVec(0).LessEq(1.2))
3174+
p.Constraints = append(p.Constraints, x.AtVec(1).GreaterEq(3.14))
3175+
3176+
// Create String
3177+
pAsString := fmt.Sprintf("%s", p)
3178+
3179+
// Check that the string has "Minimize" in it
3180+
if !strings.Contains(pAsString, "Minimize") {
3181+
t.Errorf(
3182+
"Problem string does not contain the string \"Minimize\".",
3183+
)
3184+
}
3185+
3186+
if !strings.Contains(pAsString, objExpr.String()) {
3187+
t.Errorf(
3188+
"Problem string does not contain the expression in the objective %s",
3189+
objExpr,
3190+
)
3191+
}
3192+
3193+
if !strings.Contains(pAsString, "0 vector constraints") {
3194+
t.Errorf(
3195+
"Problem string does not contain \"0 vector constraints\".",
3196+
)
3197+
}
3198+
3199+
if !strings.Contains(pAsString, "0 matrix constraints") {
3200+
t.Errorf(
3201+
"Problem string does not contain \"0 matrix constraints\".",
3202+
)
3203+
}
3204+
}

0 commit comments

Comments
 (0)