Obtaining the Maximum and Minimum and their
Orders, Ordering and the Histogram of an Array
of Real Numbers with a MILP Model
José Barahona da Fonseca
Department of Electrical Engineering, Faculty of Sciences and Technology, New University of Lisbon, 2829-516 Monte de Caparica, Portugal
Abstract. In a previous work we solved the same problem with real
numbers with limited precision, but we did not consider the possibility of repetitions. In this paper we used a new, simpler and more efficient approach where the coherence between the original set and the ordered set is guaranteed by an auxiliary indexed binary variable and two sets of constraints that implement the restriction to a binary variable, a linearization technique developed in a previous work [1]. We also obtained the order in the original set of each element of the ordered set, the maximum(s) and minimum(s) and their orders with another auxiliary indexed binary variable and two sets of constraints and obtained the histogram of the original set of real numbers. Then we presented some examples of computational experiments that show that this new approach is more efficient in terms of runtime and memory usage. Finally we point as the improvement of the histogram calculation as the near future work.
1 Introduction
Obtaining the maximum and minimum and ordering an array is very simple to do with a procedural programming language. Nevertheless it is very difficult to make it within a mathematical program since we cannot make attributions. To our knowledge our work is the first proposal to order and obtain the maximum and minimum of an array of real numbers and obtain their orders with a MILP model. In a previous work we have already solved this problem for a set of integers [2] with a much more complex MILP model. In this work with a simpler and more elegant formulation we have no limitations of the precision of real numbers. Nevertheless our new MILP model still consumes a lot of memory but it is faster than our previous model.
2 Description of the MILP model
Next we will describe our MILP model to obtain the maximums and minimums and their orders and order an array and obtain their histogram. To guarantee that each original real number a(i) exists in the ordered array b(j) we need an indexed auxiliary binary variable a_b(i,j) that must fulfil the sets of constraints (1) and (2).
∀ i ,
∑
ja
b(
i, j)≥ 1
(1)∀ j ,
∑
ia
b(
i , j)≥ 1
(2) In a nonlinear model we could write the implication a_b(I,j)=1a(i)=b(j) as the set of nonlinear constraints (3).∀ i , j , a
b(
i , j)a (i)=a
b(
i , j)b ( j)
(3) The set of nonlinear constraints (3) that express that each value b(j) must exist in the original set a(i) is equivalent to the conjunction of the two sets of linear constraints (4) and (5), since A≥B AND A≤B A=B and two sets of constraints of a mathematical program may be considered as the logical AND of these sets of constraints.∀ i , j ,
(
1−a
b(
i , j))
Z max
min+
a(i)≥ b( j)
(4)∀ i , j ,(1−a
b(
i , j))max
min+
b ( j)≥ a(i)
(5) To prevent the trivial solution of all a_b(i,j)=1 we must minimize the number of ones in a_b saving this number in obj4 obtained by equation (6) and then adding this variable to the objective variable obj that will be minimized.obj
4=
∑
i , j
a
b(
i, j)(6)
The maximum is obtained by the set of constraints (7) and the minimum by the set of constraints (8). Then we prevent trivial solutions adding to the objective variable, obj, maximum-minimum, this way we minimize the maximum and maximize the minimum and guarantee that they correspond to values of a(i).
∀ i , maximum ≥ a (i)(7)
∀ i , minimum≤ a (i)(8)
Next we will show how we obtain the order of maximums and minimums allowing their repetition as many times as we want. There exists an indexed binary variable ord_max(i) that is 1 when a(i)=maximum. In a nonlinear model we could define ord_max(i) by the set of equality constraints (9) and then we maximize the number of ones in ord_max to prevent trivial solutions.
∀ i ,∨d
max(i)a(i)=
¿
d
max(i)maximum (9)
Again we linearize (9) by two sets of linear constraints (10) and (11).
∀ i , j ,(1−ord
max(i))max
min+
maximum ≥ a(i)
(11) Then we obtain the number of ones in ord_max(i), obj2, with (12) and then we subtract obj2 from the objective variable, obj, and then minimize obj, this way maximizing the number of ones in ord_max(i) to prevent trivial solutions of all zeros in ord_max(i).obj
2=
∑
i
ord
max(i)(12)
Similarly to obtain the order of the minimums we define another indexed binary variable ord_min(i) that is one when a(i)=minimum. This binary variable is defined by the sets of constraints (13) and (14) and again to prevent trivial solutions of all zeros we define another variable obj3 with (15) that will contain the number of ones in ord_min and then subtract this variable from the objective variable and finally we maximize obj3 minimizing the objective variable.
∀ i , j ,(1−ord
min(i))max
min+
a(i)≥ minimum
(13)∀ i , j ,(1−ord
min(
i))max
min+
minimum ≥ a(i)
(14)obj
3=
∑
i
ord
min(i)(15)
To guarantee that b(j) is ordered we consider all pair of indexes and impose the desired relation through the set of constraints (16). When order=1, b(j) is ordered in increasing order and when order=0 in decreasing order.
∀ i , j :
(
i order+(1−order ) j
)
>
(
j order +(1−order) i
)
, b (i) ≥ b ( j)(16)
To obtain the histogram we need a new auxiliary binary indexed variable ord_k1(i,j) that is defined by the two sets of constraints (17) and (18). These two sets of constraints guarantee that when a(i)=a(j) then ord_k1(i,j)=1. Now the histogram, hist(i), the number of repetitions of each original element a(i), is obtained summing in j, ord_k1(i,j) through (19). Again to prevent the trivial solution of all zeros in ord_k1 we save the number of ones in obj5 through (20) and then maximize it subtracting obj5 from the objective variable obj and then minimizing it.∀ i , j ,
(
1−ord
k 1(
i))
max
min+
a (i)≥ a ( j)(17)
∀ i , j ,
(
1−ord
k 1(
j))
max
min+
a ( j)≥ a (i)(18)
hist (i)=
∑
jord
k 1(
i, j)(19)
obj
5=
∑
i , jord
k 1(
i , j)(20)
Since we will minimize the objective variable, obj, it will be calculated by (21).
obj=maximum−minimum−obj
2−
obj
3−
obj
5+
obj
4(21)
In appendix A we show the implementation of our MILP model with GAMS software and in appendix B we show the output of a run of the model for a set of 80 real numbers with repetitions that took about 5 minutes in a 32 bits PC with a 1.2GHz clock. The set of real numbers was generated randomly with limited precision to create repetitions. Nevertheless the MILP model works well with unlimited precision, setting the parameter flag_round=0, but in this situation it would be needed a very big set to arise repetitions.
3 Conclusion and future work
We showed that our MILP model is very elegant and efficient. To our knowledge this is the first proposal to solve these problems with a MILP model. Nevertheless we believe that with a reformulation of the model we will have even better runtimes and lower memory consumption. This will be our near future work.
imes, without title.
References
1. J. Barahona da Fonseca., “Solving Any Nonlinear Problem with a MILP
Model”, Proceedings of Escape-17 Conference, Bucharest, Romania, 2007
2. J. Barahona da Fonseca, « Ordering and Obtaining the Maximum and Minimum of a Set of Integers , Proceedings of CEAM-VF 2011, 2011.
Appendix A. GAMS code of our MILP model
set i/1*80/; alias (j,i); *loop(k, parameter v(i),k1,k1_i; scalar s /0.05/; scalar s2 /1/; scalar max_v /20/; scalar flag_round /1/ n_d_places /2/;
variable a(i), b(i), maximum, minimum,obj, obj2, obj3, obj4, obj5, hist(i); v(i)=s2*(uniform(-max_v*s,max_v*s)) * (1-flag_round) + s2*round((uniform(-max_v*s,max_v*s))*10**n_d_places)/10**n_d_places * flag_round ; a.fx(i)=v(i)*(v(i) ne 0) + 0.01*(v(i) eq 0);
scalar order /1/;
* order=1 Increasing Order, order=0 Decreasing Order binary variable a_b(i,j), ord_min(i), ord_max(i), ord_k1(i,j); *CONSTRAINTS:
constr_b(i,j)$((ord(i)*order + (1-order)*ord(j)) > (ord(j)*order + (1-order)*ord(i)) ).. b(i)=g=b(j); constr_a_b.. sum(i, a(i))=e=sum(i, b(i));
constr_a_b2(i,j).. (1-a_b(i,j))*1000+a(i)=g=b(j); constr_a_b21(i,j).. (1-a_b(i,j))*1000+b(j)=g=a(i); constr_a_b3(i).. sum(j, a_b(i,j))=g=1;
constr_a_b4(j).. sum(i, a_b(i,j))=g=1; calc_obj4.. obj4=e=sum((i,j), a_b(i,j));
calc_ord_max(i).. (1-ord_max(i))*1000+a(i)=g=maximum; calc_ord_max2(i).. a(i)=l=(1-ord_max(i))*1000+maximum; calc_obj2.. obj2=e=sum(i, ord_max(i));
calc_ord_min(i).. (1-ord_min(i))*1000+a(i)=g=minimum; calc_ord_min2(i).. a(i)=l=(1-ord_min(i))*1000+minimum; calc_obj3.. obj3=e=sum(i, ord_min(i));
calc_ord_k1(i,j).. (1-ord_k1(i,j))*1000+a(i)=g=a(j); calc_ord_k12(i,j).. a(i)=l=(1-ord_k1(i,j))*1000+a(j); calc_obj5.. obj5=e=sum( (i,j), ord_k1(i,j) ); calc_hist(i).. hist(i)=e=sum(j, ord_k1(i,j)); calc_maximum(i).. maximum=g=a(i); calc_minimum(i).. minimum=l=a(i);
calc_obj.. obj=e=maximum-minimum-obj2-obj3-obj5+obj4;
Model OrdMaxMin /all/;
Solve OrdMaxMin using MIP minimizing obj;
display a.l, b.l, maximum.l, ord_max.l, minimum.l, ord_min.l, ord_k1.l, hist.l;
Appendix B. Output of a run of our MILP model
---- 114 VARIABLE a.L 1 0.690, 2 -0.400, 3 -0.550, 4 0.710, 5 0.010, 6 0.160 7 0.520, 8 0.280, 9 -0.500, 10 -0.130, 11 -0.300, 12 -0.700 13 0.660, 14 0.330, 15 -0.390, 16 0.010, 17 0.740, 18 -0.430 19 0.450, 20 -0.070, 21 -0.760, 22 -0.910, 23 -0.640, 24 0.120 25 -0.400, 26 0.510, 27 -0.430, 28 -0.790, 29 0.090, 30 0.580 31 -0.650, 32 0.500, 33 -0.930, 34 0.240, 35 -0.280, 36 -0.510 37 0.870, 38 0.570, 39 -0.750, 40 -0.860, 41 -0.990, 42 0.010 43 -0.650, 44 -0.370, 45 0.930, 46 -0.260, 47 0.540, 48 0.830 49 0.470, 50 0.150, 51 -0.990, 52 0.040, 53 -0.550, 54 -0.450 55 0.870, 56 -0.730, 57 -0.250, 58 0.900, 59 -0.400, 60 -0.200 61 -0.230, 62 -0.620, 63 0.190, 64 -0.910, 65 0.890, 66 0.210 67 0.190, 68 0.010, 69 0.310, 70 -0.750, 71 -0.540, 72 0.550 73 -0.600, 74 -0.610, 75 0.290, 76 -0.830, 77 -0.130, 78 0.390 79 -0.690, 80 0.390
---- 114 VARIABLE b.L 1 -0.990, 2 -0.990, 3 -0.930, 4 -0.910, 5 -0.910, 6 -0.860 7 -0.830, 8 -0.790, 9 -0.760, 10 -0.750, 11 -0.750, 12 -0.730 13 -0.700, 14 -0.690, 15 -0.650, 16 -0.650, 17 -0.640, 18 -0.620 19 -0.610, 20 -0.600, 21 -0.550, 22 -0.550, 23 -0.540, 24 -0.510 25 -0.500, 26 -0.450, 27 -0.430, 28 -0.430, 29 -0.400, 30 -0.400 31 -0.400, 32 -0.390, 33 -0.370, 34 -0.300, 35 -0.280, 36 -0.260 37 -0.250, 38 -0.230, 39 -0.200, 40 -0.130, 41 -0.130, 42 -0.070 43 0.010, 44 0.010, 45 0.010, 46 0.010, 47 0.040, 48 0.090 49 0.120, 50 0.150, 51 0.160, 52 0.190, 53 0.190, 54 0.210 55 0.240, 56 0.280, 57 0.290, 58 0.310, 59 0.330, 60 0.390 61 0.390, 62 0.450, 63 0.470, 64 0.500, 65 0.510, 66 0.520 67 0.540, 68 0.550, 69 0.570, 70 0.580, 71 0.660, 72 0.690 73 0.710, 74 0.740, 75 0.830, 76 0.870, 77 0.870, 78 0.890 79 0.900, 80 0.930 ---- 114 VARIABLE maximum.L = 0.930 ---- 114 VARIABLE ord_max.L 45 1.000 ---- 114 VARIABLE minimum.L = -0.990 ---- 114 VARIABLE ord_min.L 41 1.000, 51 1.000 ---- 114 VARIABLE hist.L
1 1.000, 2 3.000, 3 2.000, 4 1.000, 5 4.000, 6 1.000 7 1.000, 8 1.000, 9 1.000, 10 2.000, 11 1.000, 12 1.000 13 1.000, 14 1.000, 15 1.000, 16 4.000, 17 1.000, 18 2.000 19 1.000, 20 1.000, 21 1.000, 22 2.000, 23 1.000, 24 1.000 25 3.000, 26 1.000, 27 2.000, 28 1.000, 29 1.000, 30 1.000 31 2.000, 32 1.000, 33 1.000, 34 1.000, 35 1.000, 36 1.000 37 2.000, 38 1.000, 39 2.000, 40 1.000, 41 2.000, 42 4.000 43 2.000, 44 1.000, 45 1.000, 46 1.000, 47 1.000, 48 1.000 49 1.000, 50 1.000, 51 2.000, 52 1.000, 53 2.000, 54 1.000 55 2.000, 56 1.000, 57 1.000, 58 1.000, 59 3.000, 60 1.000 61 1.000, 62 1.000, 63 2.000, 64 2.000, 65 1.000, 66 1.000 67 2.000, 68 4.000, 69 1.000, 70 2.000, 71 1.000, 72 1.000 73 1.000, 74 1.000, 75 1.000, 76 1.000, 77 2.000, 78 2.000