@@ -3149,3 +3149,103 @@ 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+ }
3205+
3206+ /*
3207+ TestOptimizationProblem_String2
3208+ Description:
3209+
3210+ Tests that a small optimization problem with all scalar constraints gets represented
3211+ as a string with:
3212+ - Maximize sense of objective
3213+ - the string describes that there are 2 vector constraints and 1 matrix constraints
3214+ */
3215+ func TestOptimizationProblem_String2 (t * testing.T ) {
3216+ // Create Optimization Problem
3217+ p := problem .NewProblem ("TestOptimizationProblem_String1" )
3218+
3219+ N := 2
3220+ x := p .AddVariableVector (N )
3221+ y := p .AddVariableMatrix (N , N , 0.0 , 200.0 , symbolic .Continuous )
3222+ c := symbolic .OnesVector (N )
3223+ objExpr := x .Transpose ().Multiply (c )
3224+ p .SetObjective (objExpr , problem .SenseMaximize )
3225+
3226+ p .Constraints = append (p .Constraints , x .LessEq (c ))
3227+ p .Constraints = append (p .Constraints , x .GreaterEq (symbolic .ZerosVector (N )))
3228+ p .Constraints = append (p .Constraints , y .Eq (symbolic .ZerosMatrix (N , N )))
3229+
3230+ // Create String
3231+ pAsString := fmt .Sprintf ("%s" , p )
3232+
3233+ // Check that the string has "Maximize" in it
3234+ if ! strings .Contains (pAsString , "Maximize" ) {
3235+ t .Errorf (
3236+ "Problem string does not contain the string \" Maximize\" ." ,
3237+ )
3238+ }
3239+
3240+ if ! strings .Contains (pAsString , "2 vector constraints" ) {
3241+ t .Errorf (
3242+ "Problem string does not contain \" 2 vector constraints\" ." ,
3243+ )
3244+ }
3245+
3246+ if ! strings .Contains (pAsString , "1 matrix constraints" ) {
3247+ t .Errorf (
3248+ "Problem string does not contain \" 1 matrix constraints\" ." ,
3249+ )
3250+ }
3251+ }
0 commit comments