This is the fifty first part of the ILP series. For your convenience you can find other parts in the table of contents in Part 1 – Boolean algebra
This is the fifth part of the ML series. For your convenience you can find other parts in the table of contents in Part 1 – Linear regression in MXNet
Today we are going to implement linear regression with ILP. Let’s go!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
var solver = new CplexMilpSolver(10); var samples = new object[][] { new object[]{5.1,3.5,1.4,0.2,"setosa"}, new object[]{4.9,3,1.4,0.2,"setosa"}, new object[]{4.7,3.2,1.3,0.2,"setosa"}, new object[]{4.6,3.1,1.5,0.2,"setosa"}, new object[]{5,3.6,1.4,0.2,"setosa"}, new object[]{5.4,3.9,1.7,0.4,"setosa"}, new object[]{4.6,3.4,1.4,0.3,"setosa"}, new object[]{5,3.4,1.5,0.2,"setosa"}, new object[]{4.4,2.9,1.4,0.2,"setosa"}, new object[]{4.9,3.1,1.5,0.1,"setosa"}, new object[]{5.4,3.7,1.5,0.2,"setosa"}, new object[]{4.8,3.4,1.6,0.2,"setosa"}, new object[]{4.8,3,1.4,0.1,"setosa"}, new object[]{4.3,3,1.1,0.1,"setosa"}, new object[]{5.8,4,1.2,0.2,"setosa"}, new object[]{5.7,4.4,1.5,0.4,"setosa"}, new object[]{5.4,3.9,1.3,0.4,"setosa"}, new object[]{5.1,3.5,1.4,0.3,"setosa"}, new object[]{5.7,3.8,1.7,0.3,"setosa"}, new object[]{5.1,3.8,1.5,0.3,"setosa"}, new object[]{5.4,3.4,1.7,0.2,"setosa"}, new object[]{5.1,3.7,1.5,0.4,"setosa"}, new object[]{4.6,3.6,1,0.2,"setosa"}, new object[]{5.1,3.3,1.7,0.5,"setosa"}, new object[]{4.8,3.4,1.9,0.2,"setosa"}, new object[]{5,3,1.6,0.2,"setosa"}, new object[]{5,3.4,1.6,0.4,"setosa"}, new object[]{5.2,3.5,1.5,0.2,"setosa"}, new object[]{5.2,3.4,1.4,0.2,"setosa"}, new object[]{4.7,3.2,1.6,0.2,"setosa"}, new object[]{4.8,3.1,1.6,0.2,"setosa"}, new object[]{5.4,3.4,1.5,0.4,"setosa"}, new object[]{5.2,4.1,1.5,0.1,"setosa"}, new object[]{5.5,4.2,1.4,0.2,"setosa"}, new object[]{4.9,3.1,1.5,0.1,"setosa"}, new object[]{5,3.2,1.2,0.2,"setosa"}, new object[]{5.5,3.5,1.3,0.2,"setosa"}, new object[]{4.9,3.1,1.5,0.1,"setosa"}, new object[]{4.4,3,1.3,0.2,"setosa"}, new object[]{5.1,3.4,1.5,0.2,"setosa"}, new object[]{5,3.5,1.3,0.3,"setosa"}, new object[]{4.5,2.3,1.3,0.3,"setosa"}, new object[]{4.4,3.2,1.3,0.2,"setosa"}, new object[]{5,3.5,1.6,0.6,"setosa"}, new object[]{5.1,3.8,1.9,0.4,"setosa"}, new object[]{4.8,3,1.4,0.3,"setosa"}, new object[]{5.1,3.8,1.6,0.2,"setosa"}, new object[]{4.6,3.2,1.4,0.2,"setosa"}, new object[]{5.3,3.7,1.5,0.2,"setosa"}, new object[]{5,3.3,1.4,0.2,"setosa"}, new object[]{7,3.2,4.7,1.4,"versicolor"}, new object[]{6.4,3.2,4.5,1.5,"versicolor"}, new object[]{6.9,3.1,4.9,1.5,"versicolor"}, new object[]{5.5,2.3,4,1.3,"versicolor"}, new object[]{6.5,2.8,4.6,1.5,"versicolor"}, new object[]{5.7,2.8,4.5,1.3,"versicolor"}, new object[]{6.3,3.3,4.7,1.6,"versicolor"}, new object[]{4.9,2.4,3.3,1,"versicolor"}, new object[]{6.6,2.9,4.6,1.3,"versicolor"}, new object[]{5.2,2.7,3.9,1.4,"versicolor"}, new object[]{5,2,3.5,1,"versicolor"}, new object[]{5.9,3,4.2,1.5,"versicolor"}, new object[]{6,2.2,4,1,"versicolor"}, new object[]{6.1,2.9,4.7,1.4,"versicolor"}, new object[]{5.6,2.9,3.6,1.3,"versicolor"}, new object[]{6.7,3.1,4.4,1.4,"versicolor"}, new object[]{5.6,3,4.5,1.5,"versicolor"}, new object[]{5.8,2.7,4.1,1,"versicolor"}, new object[]{6.2,2.2,4.5,1.5,"versicolor"}, new object[]{5.6,2.5,3.9,1.1,"versicolor"}, new object[]{5.9,3.2,4.8,1.8,"versicolor"}, new object[]{6.1,2.8,4,1.3,"versicolor"}, new object[]{6.3,2.5,4.9,1.5,"versicolor"}, new object[]{6.1,2.8,4.7,1.2,"versicolor"}, new object[]{6.4,2.9,4.3,1.3,"versicolor"}, new object[]{6.6,3,4.4,1.4,"versicolor"}, new object[]{6.8,2.8,4.8,1.4,"versicolor"}, new object[]{6.7,3,5,1.7,"versicolor"}, new object[]{6,2.9,4.5,1.5,"versicolor"}, new object[]{5.7,2.6,3.5,1,"versicolor"}, new object[]{5.5,2.4,3.8,1.1,"versicolor"}, new object[]{5.5,2.4,3.7,1,"versicolor"}, new object[]{5.8,2.7,3.9,1.2,"versicolor"}, new object[]{6,2.7,5.1,1.6,"versicolor"}, new object[]{5.4,3,4.5,1.5,"versicolor"}, new object[]{6,3.4,4.5,1.6,"versicolor"}, new object[]{6.7,3.1,4.7,1.5,"versicolor"}, new object[]{6.3,2.3,4.4,1.3,"versicolor"}, new object[]{5.6,3,4.1,1.3,"versicolor"}, new object[]{5.5,2.5,4,1.3,"versicolor"}, new object[]{5.5,2.6,4.4,1.2,"versicolor"}, new object[]{6.1,3,4.6,1.4,"versicolor"}, new object[]{5.8,2.6,4,1.2,"versicolor"}, new object[]{5,2.3,3.3,1,"versicolor"}, new object[]{5.6,2.7,4.2,1.3,"versicolor"}, new object[]{5.7,3,4.2,1.2,"versicolor"}, new object[]{5.7,2.9,4.2,1.3,"versicolor"}, new object[]{6.2,2.9,4.3,1.3,"versicolor"}, new object[]{5.1,2.5,3,1.1,"versicolor"}, new object[]{5.7,2.8,4.1,1.3,"versicolor"}, new object[]{6.3,3.3,6,2.5,"virginica"}, new object[]{5.8,2.7,5.1,1.9,"virginica"}, new object[]{7.1,3,5.9,2.1,"virginica"}, new object[]{6.3,2.9,5.6,1.8,"virginica"}, new object[]{6.5,3,5.8,2.2,"virginica"}, new object[]{7.6,3,6.6,2.1,"virginica"}, new object[]{4.9,2.5,4.5,1.7,"virginica"}, new object[]{7.3,2.9,6.3,1.8,"virginica"}, new object[]{6.7,2.5,5.8,1.8,"virginica"}, new object[]{7.2,3.6,6.1,2.5,"virginica"}, new object[]{6.5,3.2,5.1,2,"virginica"}, new object[]{6.4,2.7,5.3,1.9,"virginica"}, new object[]{6.8,3,5.5,2.1,"virginica"}, new object[]{5.7,2.5,5,2,"virginica"}, new object[]{5.8,2.8,5.1,2.4,"virginica"}, new object[]{6.4,3.2,5.3,2.3,"virginica"}, new object[]{6.5,3,5.5,1.8,"virginica"}, new object[]{7.7,3.8,6.7,2.2,"virginica"}, new object[]{7.7,2.6,6.9,2.3,"virginica"}, new object[]{6,2.2,5,1.5,"virginica"}, new object[]{6.9,3.2,5.7,2.3,"virginica"}, new object[]{5.6,2.8,4.9,2,"virginica"}, new object[]{7.7,2.8,6.7,2,"virginica"}, new object[]{6.3,2.7,4.9,1.8,"virginica"}, new object[]{6.7,3.3,5.7,2.1,"virginica"}, new object[]{7.2,3.2,6,1.8,"virginica"}, new object[]{6.2,2.8,4.8,1.8,"virginica"}, new object[]{6.1,3,4.9,1.8,"virginica"}, new object[]{6.4,2.8,5.6,2.1,"virginica"}, new object[]{7.2,3,5.8,1.6,"virginica"}, new object[]{7.4,2.8,6.1,1.9,"virginica"}, new object[]{7.9,3.8,6.4,2,"virginica"}, new object[]{6.4,2.8,5.6,2.2,"virginica"}, new object[]{6.3,2.8,5.1,1.5,"virginica"}, new object[]{6.1,2.6,5.6,1.4,"virginica"}, new object[]{7.7,3,6.1,2.3,"virginica"}, new object[]{6.3,3.4,5.6,2.4,"virginica"}, new object[]{6.4,3.1,5.5,1.8,"virginica"}, new object[]{6,3,4.8,1.8,"virginica"}, new object[]{6.9,3.1,5.4,2.1,"virginica"}, new object[]{6.7,3.1,5.6,2.4,"virginica"}, new object[]{6.9,3.1,5.1,2.3,"virginica"}, new object[]{5.8,2.7,5.1,1.9,"virginica"}, new object[]{6.8,3.2,5.9,2.3,"virginica"}, new object[]{6.7,3.3,5.7,2.5,"virginica"}, new object[]{6.7,3,5.2,2.3,"virginica"}, new object[]{6.3,2.5,5,1.9,"virginica"}, new object[]{6.5,3,5.2,2,"virginica"}, new object[]{6.2,3.4,5.4,2.3,"virginica"}, new object[]{5.9,3,5.1,1.8,"virginica"} }; var weights = Enumerable.Range(0, 5).Select(x => solver.Create(string.Format("w_{0}", x), Domain.AnyReal)).ToArray(); foreach (var w in weights) { w.Set(ConstraintType.LessOrEqual, solver.FromConstant(10000)).Set(ConstraintType.GreaterOrEqual, solver.FromConstant(-10000)); } var b = solver.Create("b", Domain.AnyReal); b.Set(ConstraintType.LessOrEqual, solver.FromConstant(10000)).Set(ConstraintType.GreaterOrEqual, solver.FromConstant(-10000)); var predicted = samples.Select(s => { var features = new double[] { double.Parse(s[1].ToString()), double.Parse(s[2].ToString()), double.Parse(s[3].ToString()), s[4].ToString() == "setosa" ? 1.0 : 0.0, s[4].ToString() == "virginica" ? 1.0 : 0.0 }; return solver.Operation(OperationType.Addition, Enumerable.Range(0, features.Length).Select(x => solver.FromConstant(features[x]).Operation(OperationType.Multiplication, weights[x])).ToArray()).Operation(OperationType.Addition, b); }).ToArray(); var distances = Enumerable.Range(0, samples.Length).Select(i => { var target = double.Parse(samples[i][0].ToString()); return predicted[i].Operation(OperationType.Subtraction, solver.FromConstant(target)).Operation(OperationType.AbsoluteValue); }).ToArray(); // Sum of distances var error = solver.Operation(OperationType.Addition, distances.Select(d => d).ToArray()); // Sum of squared distances - not supported //var error = solver.Operation(OperationType.Addition, distances.Select(d => d.Operation(OperationType.Multiplication, d)).ToArray()); // Average of distances //var error = solver.Operation(OperationType.Addition, distances.Select(d => d).ToArray()).Operation(OperationType.Division, solver.FromConstant(samples.Length)); // Sum of approximated squared distances //var error = solver.Operation(OperationType.Addition, distances.Select(d => { // var casted = solver.CreateAnonymous(Domain.PositiveOrZeroInteger); // casted.Set(ConstraintType.LessOrEqual, d); // casted.Operation(OperationType.Addition, solver.FromConstant(1)).Set(ConstraintType.GreaterOrEqual, d); // return casted.Operation(OperationType.Multiplication, casted); //}).ToArray()); solver.AddGoal("Goal", error.Operation(OperationType.Negation)); solver.Solve(); foreach (var w in weights) { Console.WriteLine("W: " + solver.GetValue(w)); } Console.WriteLine("B: " + solver.GetValue(b)); Console.WriteLine("Error: " + solver.GetValue(error)); |
We start with defining the samples set. Next, we create real variables for linear regression coefficients. We add arbitrary bounds. Next, we calculate the predicted value in an obvious way.
Next, we calculate distances and error.
The error is the hard part. We can easily calculate sum of distances or approximated average of them, or even sum of approximated squared distances. But we don’t know how to multiply two real variables so we can’t calculate the real MSE.
Finally, we minimize te error and solve the problem. Here goes the solution:
1 2 3 4 5 6 7 8 |
Total (root+branch&cut) = 0.44 sec. (250.99 ticks) W: 0.464135021097045 W: 0.915611814345992 W: -0.388185654008438 W: 0.868776371308019 W: -0.375949367088609 B: 1.2957805907173 Error: 35.9801687763713 |
As you can see, it is very fast (around 0.44 second). You can see the error and coefficients. Calculation time for sum of approximated squares is worse but not terrible: Total (root+branch&cut) = 8.28 sec. (7154.17 ticks)