@@ -9,12 +9,13 @@ Description:
9
9
10
10
import (
11
11
"fmt"
12
+ "strings"
13
+ "testing"
14
+
12
15
"github.com/MatProGo-dev/MatProInterface.go/optim"
13
16
"github.com/MatProGo-dev/MatProInterface.go/problem"
14
17
"github.com/MatProGo-dev/SymbolicMath.go/symbolic"
15
18
"gonum.org/v1/gonum/mat"
16
- "strings"
17
- "testing"
18
19
)
19
20
20
21
/*
@@ -1019,3 +1020,95 @@ func TestOptimizationProblem_Check2(t *testing.T) {
1019
1020
}
1020
1021
}
1021
1022
}
1023
+
1024
+ /*
1025
+ TestOptimizationProblem_IsLinear1
1026
+ Description:
1027
+
1028
+ Tests the IsLinear function with a simple problem
1029
+ that has a constant objective and a single, linear constraint.
1030
+ */
1031
+ func TestOptimizationProblem_IsLinear1 (t * testing.T ) {
1032
+ // Constants
1033
+ p1 := problem .NewProblem ("TestOptimizationProblem_IsLinear1" )
1034
+ v1 := p1 .AddVariable ()
1035
+ c1 := v1 .LessEq (1.0 )
1036
+ k1 := symbolic .K (1.0 )
1037
+
1038
+ p1 .Constraints = append (p1 .Constraints , c1 )
1039
+
1040
+ // Create good objective
1041
+ p1 .Objective = * problem .NewObjective (
1042
+ k1 , problem .SenseFind ,
1043
+ )
1044
+
1045
+ // Algorithm
1046
+ isLinear := p1 .IsLinear ()
1047
+ if ! isLinear {
1048
+ t .Errorf ("expected the problem to be linear; received non-linear" )
1049
+ }
1050
+ }
1051
+
1052
+ /*
1053
+ TestOptimizationProblem_IsLinear2
1054
+ Description:
1055
+
1056
+ Tests the IsLinear function with a simple problem
1057
+ that has a linear objective containing 3 variables and two lienar constraints,
1058
+ each containing one variable.
1059
+ */
1060
+ func TestOptimizationProblem_IsLinear2 (t * testing.T ) {
1061
+ // Constants
1062
+ p1 := problem .NewProblem ("TestOptimizationProblem_IsLinear2" )
1063
+ vv1 := p1 .AddVariableVector (3 )
1064
+ c1 := vv1 .AtVec (0 ).LessEq (1.0 )
1065
+ c2 := vv1 .AtVec (1 ).LessEq (1.0 )
1066
+
1067
+ // Add constraints
1068
+ p1 .Constraints = append (p1 .Constraints , c1 )
1069
+ p1 .Constraints = append (p1 .Constraints , c2 )
1070
+
1071
+ // Create good objective
1072
+ p1 .Objective = * problem .NewObjective (
1073
+ vv1 .Transpose ().Multiply (symbolic .OnesVector (3 )),
1074
+ problem .SenseMinimize ,
1075
+ )
1076
+
1077
+ // Algorithm
1078
+ isLinear := p1 .IsLinear ()
1079
+ if ! isLinear {
1080
+ t .Errorf ("expected the problem to be linear; received non-linear" )
1081
+ }
1082
+ }
1083
+
1084
+ /*
1085
+ TestOptimizationProblem_IsLinear3
1086
+ Description:
1087
+
1088
+ Tests the IsLinear function with a simple problem
1089
+ that has a quadratic objective containing 3 variables and two lienar constraints,
1090
+ each containing one variable.
1091
+ */
1092
+ func TestOptimizationProblem_IsLinear3 (t * testing.T ) {
1093
+ // Constants
1094
+ p1 := problem .NewProblem ("TestOptimizationProblem_IsLinear3" )
1095
+ vv1 := p1 .AddVariableVector (3 )
1096
+ c1 := vv1 .AtVec (0 ).LessEq (1.0 )
1097
+ c2 := vv1 .AtVec (1 ).LessEq (1.0 )
1098
+
1099
+ // Add constraints
1100
+ p1 .Constraints = append (p1 .Constraints , c1 )
1101
+ p1 .Constraints = append (p1 .Constraints , c2 )
1102
+
1103
+ // Create good objective
1104
+ p1 .Objective = * problem .NewObjective (
1105
+ vv1 .Transpose ().Multiply (vv1 ),
1106
+ problem .SenseMaximize ,
1107
+ )
1108
+
1109
+ // Algorithm
1110
+ isLinear := p1 .IsLinear ()
1111
+ if isLinear {
1112
+ t .Errorf ("expected the problem to be non-linear; received linear" )
1113
+ }
1114
+ }
0 commit comments