Solving Any Nonlinear Problem with a Linear MILP

0 downloads 0 Views 352KB Size Report
knows the GAMS language syntax to simplify the presentation of our ... variable then we must add 1 to x in constr2_x_bin constraint and subtract 1 to ord(i_x) ... p = ∑ ( i_x, ( w3 (ord(i_x)-1)3 + w2 (ord(i_x)-1)2 + w1 (ord(i_x)-1) + w0 ) x_bin(i_x) ...... 18 1 4 30 3 17 28 7 25 22 5 10 32 11 24 34 13 19 6 31 2 9 16 29 27 33 12 20.
19th European Symposium on Computer Aided Process Engineering – ESCAPE19 J. Jeżowski and J. Thullie (Editors) © 2009 Elsevier B.V./Ltd. All rights reserved.

Solving Any Nonlinear Problem with a Linear MILP Model: from Examples to a Novel General Methodology José Barahona da Fonseca Department of Electrical Engineering, Faculty of Sciences and Technology, New University of Lisbon, Monte de Caparica, 2829-516 Caparica, Portugal, [email protected]

Abstract Although the author is an Electrical Engineer he got interested in optimization problems using the GAMS software. Rapidly he understood the limitations of the nonlinear solvers, like the necessity to have an initial feasible solution and the high probability of the solver being trapped in a local optimum and since 2004 he solved a set of complex nonlinear problems using MILP models. From the solution of the optimization of AGVs (Autonomous Guided Vehicles) Network [1] resulted techniques of implementation of logical functions over variables of the model only with algebraic expressions and the division of two model variables with the aid of auxiliary binary variables. From the solution of generation of optimal error correcting codes [2,9] resulted a first sketch of the automatic relaxation of a set of constraints, after improved in the solution of university timetabling with soft (that can be relaxed) and hard (that cannot be relaxed) constraints. We claim that our solution is the optimal solution to the well know problem of maximal constraint satisfaction. Finally from the solution of the generation of additive-multiplicative magic squares [3-5] resulted in a technique of multiplication of a set of variables of a MILP model. Obviously these latter techniques can be extended to implement any nonlinear operation over a set of variables of a MILP model. Finally we propose new ways to implement nonlinear solvers based on meta-heuristics and Neural Networks, since our techniques require relatively big computational resources for moderate size problems.

J. Barahona da Fonseca Keywords: Implementing Any Nonlinear Function over a Set of Variables of a MILP Model, Automatic Relaxation of a Set of Constraints of a MILP Model.

1. Introduction In this work we try to summarize the new mathematical programming techniques that we created to solve nonlinear problems with MILP models during the last four years of intense work and that for our knowledge are original in the literature. These original techniques resulted from the solution of nonlinear problems with linear MILP models. The main results of our paper are a technique that allows the solution of any nonlinear problem with a MILP model and automatic relaxation of soft constraints based on auxiliary indexed binary variable. We assume the reader knows the GAMS language syntax to simplify the presentation of our linearization techniques. Due to the limitations of nonlinear solvers we point as a possible evolution of our work the development of nonlinear solvers based on state of the art neural networks learning algorithms and metaheuristics optimization techniques.

2. Nonlinear Operations over a Set of Variables in a MILP Model In this section we will present the main result of our paper: a methodology to solve any nonlinear problem with a linear MILP model. The main idea behind this approach is to impose a binary indexed variable to have a value 1 for the index whose order is equal to the value of the model variable and then make a nonlinear operation over this index. For the general case of a MILP model with various variables we impose that a binary indexed variable to have a value 1 for the combination of indexes whose orders are equal to the values of the correspondent model variables and then make a nonlinear operation over these indexes. Later in this section we will show how we calculate the very nonlinear function Hamming distance between two words of a code using

Solving Nonlinear Problems with MILP Models the same idea of making nonlinear operations over the indexes, in this case the not equal operator, ne, and in this case making the indexes the main variables of the MILP model that represent the characters of each word of the code. Although some previous works have implemented particular nonlinear functions over a variable in a MILP model [6], to our knowledge this is the first proposal of the implementation of a general solution to the implementation of any nonlinear function over a set of variables of a MILP model. As a first example of application of our techniques let’s assume we want to calculate a third degree polynomial in x with a MILP model. In the following we will assume the reader knows the GAMS syntax and we will present the constraints in a GAMS like style. First we have to create a binary variable x_bin(i_x), where the index i_x has at least as much values as the variable x, such that: constr1_x_bin.. ∑( i_x, x_bin(i_x) ) = 1 constr2_x_bin.. ∑ ( i_x, ord(i_x) x_bin(i_x) ) = x These two constraints translate the value of variable x into the order of the index i_x, such that x_bin(i_x)=1 for order(i_x)=x. In some cases where the variable x may be zero or negative we must make a linear transformation over the correspondent index i_x. In this latter case the second constraint must be changed to: constr2_x_bin_general.. ∑ ( i_x, (a-b*ord(i_x)) x_bin(i_x) ) = x where a and b are positive scalars. Now, since we can make nonlinear operations over an index, we can calculate the third degree polynomial with the following simple equation: calc_3d_pol.. p = ∑ ( i_x, ( w3 ord(i_x)3 + w2 ord(i_x)2 + w1 ord(i_x) + w0 ) x_bin(i_x) )

J. Barahona da Fonseca where w0..w3 are predefined scalars that define the third degree polynomial. Since the binary variable x_bin(i_x) is one only for the value of the index i_x whose order corresponds to the value of the variable x the sumation over all possible values of the third degree polynomial results in the value of the polynomial of the variable x. If we want to obtain the first root we just add a constraint imposing p>=0 and minimize p. Then to obtain the second root we add a constraint x > r1 and solve the model. If we got a new solution r2 then we add a new constraint x >r2 or x < r2 until we got the last root. This way we can obtain the roots of any degree polynomial with a MILP model with few constraints. This approach although very simple and efficient has a main drawback: if the domain of x is very big we would have a very big MILP model. This can be solved multiplying the order of the index by a big scalar. Another marginal drawback is the ‘zero problem’, i.e. if the x domain contains the value zero then it must be added a constant to x. If for example x is an integer variable then we must add 1 to x in constr2_x_bin constraint and subtract 1 to ord(i_x) in the calc_3d_pol constraint: constr2_x_bin2.. ∑ ( i_x, ord(i_x) x_bin(i_x) ) = x + 1 calc_3d_pol2.. p = ∑ ( i_x, ( w3 (ord(i_x)-1)3 + w2 (ord(i_x)-1)2 + w1 (ord(i_x)-1) + w0 ) x_bin(i_x)

As a second example let’s consider the determination of the maximum of the function sin x/x which tends to 1 when x tends to 0 with a MILP model using an array binary variable, x_bin(i), with 9500 values. Next we show the MILP model that solves this very nonlinear problem using the described methodology and the GAMS software and the Cplex solver output. constr_bin.. ∑ (i, x_bin(i))=1; constr2_bin.. ∑ (i, 1/ord(i) * x_bin(i)) = x; calc_obj.. obj=∑ ∑ (i, sin(1/ord(i)) / (1/ord(i))*x_bin(i) );

Solving Nonlinear Problems with MILP Models Model secx /all/; solve secx using MIP maxizing obj; Next we show the output of the run of this MILP model using the GAMS software:

GAMS Rev 230 WIN-VIS 23.0.2 x86/MS Windows General Algebraic Modeling System Execution ----

27 VARIABLE x.L VARIABLE obj.L

----

27 VARIABLE x_bin.L

06/27/09 21:47:18 Page 6

= 1.052632E-4 = 1.000

9500 1.000

As we can see the Cplex solver solves the linear model in few seconds that represents the exact solution of a very nonlinear and difficult problem. Note that 1/9500=1.052632 10-4. From this example it is clear that the main limitation of our methodology is that to attain a great precision we will need a very big binary array variable which can turn the model very large. An alternative way to solve the problem of the maximum and minimum of an array of integer values is shown below, table ??. As we referred previously an artificious way to overcome this problem is to multiply the indexed binary variable that would represent the independent variable by a big scalar factor. In the model described in table 1 we show how we obtained a better resolution with only 95 values of the indexed variable and using a big scalar f. sets i /1*95/; scalar f /10000/; binary variable x_bin(i); positive variable x;

J. Barahona da Fonseca variable obj; constr_bin.. ∑(i, x_bin(i))=1; constr2_bin.. ∑(i, 1/(f*ord(i))*x_bin(i))=x; calc_obj.. obj=∑(i, sin(1/(f*ord(i))) /(1/(f*ord(i)))*x_bin(i) ); Model secx2 /all/; solve secx2 using MIP maxizing obj; GAMS Rev 230 WIN-VIS 23.0.2 x86/MS Windows General Algebraic Modeling System Execution ----

29 VARIABLE x.L VARIABLE obj.L

----

29 VARIABLE x_bin.L

07/08/09 22:57:43 Page 6

= 1.052632E-6 = 1.000

95 1.000 Table 1. Alternative version of the MILP model to maximize sin x / x using a big scalar to reduce the domain of variation of the index.

As an example of the solution of a more complex nonlinear problem next we will show how to solve the following system of two nonlinear equations, equations (1), with a MILP model.

 x 3 − y 3 = −19  3  x + y 3 = 35

(1)

whose solution is x=2, y=3. In table 2 we present the linear MILP model written with GAMS software syntax that solves this system of two nonlinear equations and in table 3 we present the output of the solution of this model using the Cplex solver. Note that to turn this an optimization problem we maximize the sum of the x_bin indexed binary variable that must be 1.

Solving Nonlinear Problems with MILP Models sets i /1*95/; binary variable x_bin(i), y_bin(i); variable x, y; variable obj; constr_bin.. ∑ (i, x_bin(i))=1; constr2_bin.. ∑ (i, ord(i)*x_bin(i))=x; constr_biny.. ∑ (i, y_bin(i))=1; constr2_biny.. ∑ (i, ord(i)*y_bin(i))=y; * x3-y3=-19 eq1_1.. ∑ (i, ord(i)**3*x_bin(i))- ∑ (i, ord(i)**3*y_bin(i))=-19; * x3+y3=35 eq1_2.. ∑ (i, ord(i)**3*x_bin(i))+ ∑ (i, ord(i)**3*y_bin(i))=35; calc_obj.. obj=∑ (i, x_bin(i) ); Model SysEqs /all/; solve SysEqs using MIP maximizing obj; Table 2. MILP model to solve a system of two nonlinear equations. GAMS Rev 230 WIN-VIS 23.0.2 x86/MS Windows General Algebraic Modeling System Execution ----

50 VARIABLE x.L

----

50 VARIABLE x_bin.L

=

2.000

=

3.000

07/08/09 23:38:03 Page 6

2 1.000 ----

50 VARIABLE y.L

----

50 VARIABLE y_bin.L

3 1.000 Table 3. Output of the MILP model described in table 2 using the Cplex solver.

To show how is implemented a nonlinear function of more than one variable with a MILP model

J. Barahona da Fonseca we will present the solution of a multiplicative semimagic square of order three. A multiplicative semimagic square is a square matrix where all lines and columns have the same product. In a multiplicative magic square we also impose that the two main diagonals must have the same product. Besides the very interesting problem of the multiplication of a set of variables with a linear MILP model this problem also implies the solution of a difficult problem: the implementation of the all different constraint over all elements of the matrix. This is also done translating each element of line l and column c, a(l,c), into an auxiliary binary variable whose third index will correspond to the value of the matrix and then impose the alldifferent constraint over this auxiliary binary variable. In table 4 we present the MILP model that solves this very complex nonlinear and combinatorial explosive problem. The matrix is defined as a free variable a(l,c) which is translated into a binary variable a_bin(l,c,v1) where the last index will have an order equal to a(l,c) for a_bin(l,c,v1)=1. This translation is made by the first two constraints. Finaly the all_different constraint is implemented by a set of constraints indexed by the value index v1. Basicly they say that all possible values appear in the matrix zero or one times. This way there will not exist repetitions in the original matrix. To make the multiplication of three variables of each line and column we must create a binary variable with indexes (l,,v1,v2,v3) such that the indexes v1, v2 and v3 will correspond to the values of the elements of line l, respectively. This second translation is implemented by the constraints constr_prodc(c) that impose that the binary variable only has one 1 for each column and the constraints calc_prodc_bin1(c), calc_prodc_bin2(c) and calc_prodc_bin3(c) impose that the order of indexes v1, v2 and v3 for which prodc_bin(c, v1,v2,v3)=1 will correspond to the elements of column

c,

respectively.

Similarly

constraints

constr_prodl(l)

and

calc_prodl_bin1(l),

calc_prodl_bin2(l) and calc_prodl_bin3(l) translate the values of the elements of each line l into the order of indexes v1, v2 and v3, respectively.

Solving Nonlinear Problems with MILP Models Finally to calculate the product of each line and column we just add the product of the indexes v1*v2*v3 times the binary variable over all possible combinations of (v1,v2,v3) maintaining constant the index c or l, respectively. Since the binary variable is 1 only for the combination of indexes (v1,v2,v3) correspondent to the values of each line and column this sum will result in the product of the elements of each line or column respectively. This methodology can be extended to any nonlinear problem to make any nonlinear operation over any set of variables of a MILP model.

sets l /1*3/; alias(c,c1,l1,l); sets v1 /1*60/; alias(v2, v3, v1); positive variable a(l,c), prodc(c), prodl(l); variable obj; binary variable a_bin(l,c,v1), prodc_bin(c,v1,v2,v3), prodl_bin(l,v1,v2,v3); constr_a_bin(l,c).. ∑(v1, a_bin(l,c,v1))=1; constr_a_bin2(l,c).. ∑ (v1, ord(v1)*a_bin(l,c,v1))=a(l,c); all_different_constr(v1).. ∑ ((l,c), a_bin(l,c,v1)) ≤ 1; constr_prodc(c).. ∑ ((v1,v2,v3), prodc_bin(c,v1,v2,v3))=1; constr_prodl(l).. ∑ ((v1,v2,v3), prodl_bin(l,v1,v2,v3))=1; calc_prodc_bin1(c).. ∑ ((v1,v2,v3), ord(v1)*prodc_bin(c, v1,v2,v3))=a('1',c); calc_prodc_bin2(c).. ∑ ((v1,v2,v3), ord(v2)*prodc_bin(c, v1,v2,v3))=a('2',c); calc_prodc_bin3(c).. ∑ ((v1,v2,v3), ord(v3)*prodc_bin(c, v1,v2,v3))=a('3',c); calc_prodl_bin1(l).. ∑ ((v1,v2,v3), ord(v1)*prodl_bin(l, v1,v2,v3))=a(l,'1'); calc_prodl_bin2(l).. ∑ ((v1,v2,v3), ord(v2)*prodl_bin(l, v1,v2,v3))=a(l,'2'); calc_prodl_bin3(l).. ∑ ((v1,v2,v3), ord(v3)*prodl_bin(l, v1,v2,v3))=a(l,'3');

J. Barahona da Fonseca calc_prodc(c).. prodc(c)=∑ ((v1,v2,v3),), ord(v1)*ord(v2)*ord(v3)*prodc_bin(c, v1,v2,v3)); calc_prodl(l).. prodl(l)=∑ ((v1,v2,v3), ord(v1)*ord(v2)*ord(v3)*prodl_bin(l, v1,v2,v3)); eq_constrs(c,c1).. prodc(c)=prodc(c1); eq_constrs2(l,l1).. prodl(l)=prodl(l1); eq_constrs3(l,c).. prodl(l)=prodc(c); calc_obj.. obj=∑ ((l,c),a(l,c)); model MultMgSq /all/; solve MultMgSq using MIP minimizing obj; Table 4. MILP model to generate a multiplicative semimagic square.

----

93 VARIABLE a.L 1

1 2 3 ----

18.000 4.000 3.000

2

3

1.000 6.000 36.000

12.000 9.000 2.000

93 VARIABLE prodc.L

1 216.000, 2 216.000, 3 216.000 ----

93 VARIABLE prodl.L

1 216.000, 2 216.000, 3 216.000 GAMS Rev 230 WIN-VIS 23.0.2 x86/MS Windows General Algebraic Modeling System Execution ----

105 VARIABLE a.L 1

1 2 3

2.000 3.000 36.000

2 9.000 4.000 6.000

3 12.000 18.000 1.000

07/14/09 23:24:06 Page 6

Solving Nonlinear Problems with MILP Models ----

105 VARIABLE prodc.L

1 216.000, 2 216.000, 3 216.000 ----

105 VARIABLE prodl.L

1 216.000, 2 216.000, 3 216.000 Table 5. Alternative outputs of the MILP model described in table 4 using the Cplex solver with different domains of the third index of the binary variable that represents the values of the matrix.

2.1. Alternative Approach to the Implementation of Nonlinear Operations over a Set of Variables with a MILP Model The first time we solved the problem of the calculation of a nonlinear function over a set of variables in a MILP model was about two years ago when we generate small multiplicative and additive-multiplicative magic squares [3-5] with a MILP model. In table 6 we show an alternative way to calculate the product of the elements (set of variables of the MILP model) of a line of the multiplicative semimagic square. qm is a binary variable in which the first two indexes correspond to the line and column and the third corresponds to the values of the elements of the multiplicative semimagic square. This second approach to the linearization of a nonlinear function over various variables has the disadvantage that we must previously translate each matrix element into the last index of a binary variable qm, but is surprisingly more efficient than the previous approach of linearization. constr_l1(i).. ∑ ( (k1,k2,k3), line(i,k1,k2,k3) ) = 1 constr_l2(i,k1,k2,k3).. Order* line(i,k1,k2,k3) ≤ qm(i,'e1',k1)+qm(i,'e2',k2)+qm(i,'e3',k3) constr_l3(i).. prod_line(i)= ∑ ( (k1,k2,k3), line(i, k1,k2,k3) ord(k1) ord(k2) ord(k3) ) Table 6. Fundamental constraints of the MILP model to generate a multiplicative semimagic square.

J. Barahona da Fonseca The matrix elements are previously translated into the third index of the binary variable qm. The first set of constraints simply impose that there is only one combination of indexes k1,k2,k3 associated to each line i for which the line binary variable assumes the value 1. Then the second set of constraints impose that the combination for which the line binary variable is one correspond to three ones of the qm binary variable associated to the values of matrix line i. This alternative approach to the linearization of a nonlinear problem generates a much greater MILP model because of the existence of an indexed set of constraints thar translate the variables into the indexes of a binary variable but surprisingly has much less runtimes. To further illustrate this second approach next we show a MILP model to maximize the function x2+y2+z2. Now we have an indexed constraint, translate_x_y_z_v1_v2_v3(v1,v2,v3), which turn the MILP model much greater, almost five times greater (404MB against 75MB for the previous MILP model), but surprisingly it has better runtime performance. sets v1 /1*100/; alias(v2,v3, v1); positive variable x,y,z; binary variable xyz(v1,v2,v3), x_bin(v1), y_bin(v1), z_bin(v1); variable obj; only_one.. ∑ ((v1,v2,v3), xyz(v1,v2,v3))=1; only_one_x_bin.. ∑ (v1, x_bin(v1))=1; only_one_y_bin.. ∑ (v1, y_bin(v1))=1; only_one_z_bin.. ∑ (v1, z_bin(v1))=1; translate_x_x_bin.. ∑ (v1, ord(v1)*x_bin(v1))=x; translate_y_y_bin.. ∑ (v1, ord(v1)*y_bin(v1))=y; translate_z_z_bin.. ∑ (v1, ord(v1)*z_bin(v1))=z; translate_x_y_z_v1_v2_v3(v1,v2,v3).. 3*xyz(v1,v2,v3 ≤ x_bin(v1)+y_bin(v2)+z_bin(v3); calc_obj.. obj=e=∑((v1,v2,v3), (ord(v1)**2+ord(v2)**2+ord(v3)**2) * xyz(v1,v2,v3)); constr_x.. x ≤ 5; constr_y.. y ≤ 6; constr_z.. z ≤ 4; model NLF2a /all/; solve NLF2a using MIP maximizing obj; Table 7. Linear MILP model to solve the maximization of x2+y2+z2.

Solving Nonlinear Problems with MILP Models GAMS Rev 230 WIN-VIS 23.0.2 x86/MS Windows General Algebraic Modeling System Execution ----

07/17/09 14:59:47 Page 6

68 VARIABLE xyz.L 4

5 .6

1.000

----

68 VARIABLE x.L

----

68 VARIABLE x_bin.L

=

5.000

=

6.000

=

4.000

5 1.000 ----

68 VARIABLE y.L

----

68 VARIABLE y_bin.L

6 1.000 ----

68 VARIABLE z.L

----

68 VARIABLE z_bin.L

4 1.000 ----

68 VARIABLE obj.L

=

77.000

Table 8. Output of the run of the previous MILP model using the Cplex solver. In appendix 4 we show the extension of this problem to four variables where we have had to reduce the domain of the indexes to get a solution without exhausting the memory, in appendix 5 we show the complete MILP model to obtain an multiplicative semimagic square of order 3 using this alternative methodology of linearization of a nonlinear problem, in appendix 6 we show a MILP model to solve a Sudoku puzzle of any order that makes intensive use of the alldifferent constraint and in appendix VII we show a MILP model to solve any Kakuro puzzle of any order. The latter MILP model is a little bit more complex and we will describe it in detail.

J. Barahona da Fonseca

******************************************************************************

Appendix A. GAMS Implementation of our Novel MILP model to Solve Any Kakuro Puzzle sets i /1*9/; sets j /1*12/; set seg /1*3/; set v /1*9/; binary variable a_bin(i,j, v); Parameter n_segs_l(i), n_segs_c(j), ord_min_c(j,seg), ord_min_l(i,seg), ord_max_l(i,seg), sum_l(i,seg), sum_c(j,seg);

ord_max_c(j,seg),

********************************************************************** * Next we will Define a 'Black Belt' Kakuro puzzle 12x9 that GAMS will solve in few seconds ********************************************************************** ********************************************************************** * First Step: we define the Number of 'Segments' of each Line ********************************************************************** n_segs_l('1')=3; n_segs_l('2')=2; n_segs_l('3')=2; n_segs_l('4')=3; n_segs_l('5')=3; n_segs_l('6')=3; n_segs_l('7')=2; n_segs_l('8')=2; n_segs_l('9')=3; ******************************************************* * Second Step: we define the Number of 'Segments' of each Column ******************************************************* n_segs_c('1')=2; n_segs_c('2')=1; n_segs_c('3')=2; n_segs_c('4')=2;

Solving Nonlinear Problems with MILP Models n_segs_c('5')=2; n_segs_c('6')=3; n_segs_c('7')=3; n_segs_c('8')=2; n_segs_c('9')=2; n_segs_c('10')=2; n_segs_c('11')=1; n_segs_c('12')=2; ******************************************************** * Third Step: we Define each Segment of Each Line and Its Sum: ******************************************************** ord_min_l('1','1')=1; ord_max_l('1','1')=3; sum_l('1','1')=7; ord_min_l('1','2')=5; ord_max_l('1','2')=7; sum_l('1','2')=16; ord_min_l('1','3')=9; ord_max_l('1','3')=12; sum_l('1','3')=28; ord_min_l('2','1')=1; ord_max_l('2','1')=3; sum_l('2','1')=24; ord_min_l('2','2')=5; ord_max_l('2','2')=12; sum_l('2','2')=40; ord_min_l('3','1')=2; ord_max_l('3','1')=4; sum_l('3','1')=23; ord_min_l('3','2')=8; ord_max_l('3','2')=12; sum_l('3','2')=26; ord_min_l('4','1')=1; ord_max_l('4','1')=5; sum_l('4','1')=34; ord_min_l('4','2')=7;

J. Barahona da Fonseca ord_max_l('4','2')=9; sum_l('4','2')=16; ord_min_l('4','3')=11; ord_max_l('4','3')=12; sum_l('4','3')=17; ord_min_l('5','1')=1; ord_max_l('5','1')=3; sum_l('5','1')=23; ord_min_l('5','2')=5; ord_max_l('5','2')=8; sum_l('5','2')=14; ord_min_l('5','3')=10; ord_max_l('5','3')=12; sum_l('5','3')=21; ord_min_l('6','1')=1; ord_max_l('6','1')=2; sum_l('6','1')=16; ord_min_l('6','2')=4; ord_max_l('6','2')=6; sum_l('6','2')=17; ord_min_l('6','3')=8; ord_max_l('6','3')=12; sum_l('6','3')=23; ord_min_l('7','1')=1; ord_max_l('7','1')=5; sum_l('7','1')=26; ord_min_l('7','2')=9; ord_max_l('7','2')=11; sum_l('7','2')=24; ord_min_l('8','1')=1; ord_max_l('8','1')=8; sum_l('8','1')=42; ord_min_l('8','2')=10; ord_max_l('8','2')=12; sum_l('8','2')=22;

Solving Nonlinear Problems with MILP Models

ord_min_l('9','1')=1; ord_max_l('9','1')=4; sum_l('9','1')=19; ord_min_l('9','2')=6; ord_max_l('9','2')=8; sum_l('9','2')=19; ord_min_l('9','3')=10; ord_max_l('9','3')=12; sum_l('9','3')=23; ******************************************************** * Fourth Step: we Define each Segment of Each Column and Its Sum: ******************************************************** ord_min_c('1','1')=1; ord_max_c('1','1')=2; sum_c('1','1')=13; ord_min_c('1','2')=4; ord_max_c('1','2')=9; sum_c('1','2')=31; ord_min_c('2','1')=1; ord_max_c('2','1')=9; sum_c('2','1')=45; ord_min_c('3','1')=1; ord_max_c('3','1')=5; sum_c('3','1')=31; ord_min_c('3','2')=7; ord_max_c('3','2')=9; sum_c('3','2')=23; ord_min_c('4','1')=3; ord_max_c('4','1')=4; sum_c('4','1')=16; ord_min_c('4','2')=6; ord_max_c('4','2')=9; sum_c('4','2')=30; ord_min_c('5','1')=1;

J. Barahona da Fonseca ord_max_c('5','1')=2; sum_c('5','1')=3; ord_min_c('5','2')=4; ord_max_c('5','2')=8; sum_c('5','2')=23; ord_min_c('6','1')=1; ord_max_c('6','1')=2; sum_c('6','1')=12; ord_min_c('6','2')=5; ord_max_c('6','2')=6; sum_c('6','2')=4; ord_min_c('6','3')=8; ord_max_c('6','3')=9; sum_c('6','3')=17; ord_min_c('7','1')=1; ord_max_c('7','1')=2; sum_c('7','1')=16; ord_min_c('7','2')=4; ord_max_c('7','2')=5; sum_c('7','2')=16; ord_min_c('7','3')=8; ord_max_c('7','3')=9; sum_c('7','3')=16; ord_min_c('8','1')=2; ord_max_c('8','1')=6; sum_c('8','1')=18; ord_min_c('8','2')=8; ord_max_c('8','2')=9; sum_c('8','2')=4; ord_min_c('9','1')=1; ord_max_c('9','1')=4; sum_c('9','1')=29; ord_min_c('9','2')=6; ord_max_c('9','2')=7; sum_c('9','2')=16;

Solving Nonlinear Problems with MILP Models ord_min_c('10','1')=1; ord_max_c('10','1')=3; sum_c('10','1')=23; ord_min_c('10','2')=5; ord_max_c('10','2')=9; sum_c('10','2')=34; ord_min_c('11','1')=1; ord_max_c('11','1')=9; sum_c('11','1')=45; ord_min_c('12','1')=1; ord_max_c('12','1')=6; sum_c('12','1')=39; ord_min_c('12','2')=8; ord_max_c('12','2')=9; sum_c('12','2')=16; * Now the Matrix of the Kakuro puzzle is defined... * ... But it Remains to 'Define the 'empty positions’ with a Set of Constraints * undefined_value_li_cj(v1) ).. a_bin('li','cj',v1)=e=0; ****************************** variable obj; equations at_most_one_value(i,j) all_different_line(i,v,seg) all_different_column(j,v,seg) constraint_sum_l(i,seg) constraint_sum_c(j,seg) calc_obj ***************************************************************** * The Following Constraints Define the Empty Positions: ***************************************************************** undefined_value_l1_c1(v) undefined_value_l1_c3(v) undefined_value_l1_c2(v) undefined_value_l2_c1(v) undefined_value_l3_c1(v) undefined_value_l4_c1(v)

J. Barahona da Fonseca undefined_value_l4_c2(v) undefined_value_l5_c1(v) undefined_value_l5_c2(v) undefined_value_l51_c2(v) undefined_value_l6_c2(v) undefined_value_l7_c1(v) undefined_value_l7_c2(v) undefined_value_l8_c1(v) undefined_value_l8_c2(v) undefined_value_l81_c2(v) undefined_value_l8_c3(v) undefined_value_l9_c1(v) undefined_value_l9_c2(v) undefined_value_l9_c3(v) ***************************************************************** calc_obj ; * Each position of the Matrix have 1 or 0 values (in the case of 'Empty' Position): at_most_one_value(i,j).. sum(v, a_bin(i,j,v))=l=1; *Each Line Segment (i,seg) cannot have repetitions: all_different_line(i,v,seg)$(ord(seg) le n_segs_l(i)).. sum(j$( (ord(j) ge ord_min_l(i,seg)) and (ord(j) a_bin(i,j,v))=l=1;

le

ord_max_l(i,seg))

),

*Each Line l, Segment seg, Must has a Sum Equal to sum_l(i,seg): constraint_sum_l(i,seg)$(ord(seg) le n_segs_l(i)).. sum(( v,j)$( (ord(j) ge ord_min_l(i,seg)) and (ord(j) le ord_max_l(i,seg)) ) , ord(v)*a_bin(i,j,v))=e=sum_l(i,seg); undefined_value_l1_c1(v1).. a_bin('1','4',v)=e=0; undefined_value_l1_c3(v1).. a_bin('1','8',v)=e=0; undefined_value_l1_c2(v1).. a_bin('2','4',v)=e=0; undefined_value_l2_c1(v1).. a_bin('3','1',v)=e=0; undefined_value_l3_c1(v1).. a_bin('3','5',v)=e=0; undefined_value_l4_c1(v1).. a_bin('3','6',v)=e=0; undefined_value_l4_c2(v1).. a_bin('3','7',v)=e=0; undefined_value_l5_c1(v1).. a_bin('4','6',v)=e=0; undefined_value_l5_c2(v1).. a_bin('4','10',v)=e=0; undefined_value_l51_c2(v1).. a_bin('5','4',v)=e=0; undefined_value_l6_c2(v1).. a_bin('5','9',v)=e=0; undefined_value_l7_c1(v1).. a_bin('6','3',v)=e=0; undefined_value_l7_c2(v1).. a_bin('6','7',v)=e=0; undefined_value_l8_c1(v1).. a_bin('7','6',v)=e=0; undefined_value_l8_c2(v1).. a_bin('7','7',v)=e=0;

Solving Nonlinear Problems with MILP Models undefined_value_l81_c2(v1).. a_bin('7','8',v)=e=0; undefined_value_l8_c3(v1).. a_bin('7','12',v)=e=0; undefined_value_l9_c1(v1).. a_bin('8','9',v)=e=0; undefined_value_l9_c2(v1).. a_bin('9','5',v)=e=0; undefined_value_l9_c3(v1).. a_bin('9','9',v)=e=0; *Each Column Segment (l,seg) cannot have repetitions: all_different_column(j,v,seg)$(ord(seg) le n_segs_c(j)).. sum(i$ ( (ord(i) ge ord_min_c(j,seg)) and (ord(i) le ord_max_c(j,seg)) ) , a_bin(i,j,v))=l=1; *Each Column c, Segment seg, Must has a Sum Equal to sum_c(c,seg): constraint_sum_c(j,seg)$( ord(seg) le n_segs_c(j) ).. sum(( v,i)$( (ord(i) ge ord_min_c(j,seg)) and (ord(i) le ord_max_c(j,seg)) ) , ord(v)*a_bin(i,j,v))=e=sum_c(j,seg); * To prevent a 'Trivial Solution' we must minimize the Sum of All Matrix Elements * where the 'Empty Positions' are forced to be 0: calc_obj.. obj=e=sum((i,j,v), a_bin(i,j,v)); model Kakuro /all/; option IterLim=1000000000; option ResLim=1000000000; option optcr=0; option optca=0; solve Kakuro using MIP maximizing obj; display a_bin.l, obj.l; This methodology of linearization can be easily extended to a greater number of variables and to more complex nonlinear functions. 2.1.? Linearization of a Very Complex and Very Nonlinear Function Next we will present a final example of application of our linearization techniques to the very complex and very nonlinear function

d (i ) ≥ e(i ),5e(i ) a (i ) =  d (i ) < e(i ),−10d (i )

J. Barahona da Fonseca With our linearization techniques we can solve virtually any nonlinear function with a MILP model and with an arbitrary number of variables, although the complexity of the MILP model increases with that number. Since we can combine logical expressions with arithmetic expressions we translate the definition of the function in the following expression

A(i)=sum( (j,k), ((ord(j) ge ord(k)) * 5 * ord(k) - (ord(j) < ord(k)) * 10 *ord(j)) * ord(j)) ) * de(i,j,k) where d(i)=ord(j) and e(i)=ord(k) for (I,j,k) such that de(I,j,k)=1. The values of d(i) and e(i) are translated into the binary variable de(I,j,k) through the similar constraints. First d(i) and e(i) are translated into the binary variables d_bin(I,j) and e_bin(I,k), respectively, through the following set of indexed constraints

Sum(j, d_bin(i,j))=1; Sum(j, d_bin(i,j) * ord(j))=d(i);

Sum(k, e_bin(i,k))=1; Sum(k, e_bin(i,k) * ord(j))=e(i);

And then the binary variables d_bin(I,j) and e_bin(I,k) are merged into the fundamental binary variable de(I,j,k) through the following constraints (i).. Sum((j,k), de(I,j,k) ) = 1; (i,j,k).. 2*de(i,j,k) ≤ d_bin(i,j) + e_bin(i,k) Calc_obj.. obj=sum( (i,j,k), de(i,j,k) ) Solve Function using MIP maximizing obj

Solving Nonlinear Problems with MILP Models Usually we also want to maximize or minimize another objective variable, obj2. In that case we just add obj2 to obj obtaining obj3 and maximize obj3 in the case we want to maximize obj2 and in the case we want to minimize obj2 we subtract it from obj and then again maximize obj3. This second approach to the linearization of a nonlineat function with more than one variable seems simpler than the previous approach but paradoxally it shows worse runtimes and higher memory resources consumption.

???????2.1.? Four MILP Models to Generate Optimal Error Correcting Codes The generation of optimal error correcting codes with a given Hamming distance that will determine its error correction capability and sometimes with other constraints like words with constant weight, i.e. with a constant number of symbols different from 0 and constant composition codes for ternary and n-ary codes. Since the Hamming distance constraint, constant weight constraint and constant composition constraints are very nonlinear constraints the problem of generating optimal error correcting codes is very nonlinear. Next we will show how we solved it with four alternative linear MILP models using some of the previous linearization techniques. In a linear model we cannot make nonlinear operations over the model variables but we can make any nonlinear operation over pairs of indexes. So we created a binary variable that for a code of length 3 would be word(a0,a1,a2) and the Hamming distance between two words would simply calculated by the following legal expression d_h(b0,b1,b2,c0,c1,c2)=(ord(b0) ne ord(c0)) + (ord(b1) ne ord(c1)) + (ord(b2) ne ord(c2)) This is a valid expression in GAMS that allows algebraic operations over the results of logic operations. And the weight of a word can be calculated by the following simple expression

w(b0,b1,b2)=(ord(b0) ne 1) + (ord(b1) ne 1) + (ord(b2) ne 1) And the indexed set of constraints that impose a constant weight w0 could be written

J. Barahona da Fonseca

Constant_weight(b0,b1,b2).. w0*word(b0,b1,b2) =e= w(b0,b1,b2)*word(b0,b1,b2)

Note that since w(b0,b1,b2) it is an indexed parameter the previous indexed set of constraints are linear constraints.

And finally for a constant composition code, e.g. a ternary code, we define three indexed parameters w0(b0,b1,b2)=(ord(b0) eq 1) + (ord(b1) eq 1) + (ord(b2) eq 1) w1(b0,b1,b2)=(ord(b0) eq 2) + (ord(b1) eq 2) + (ord(b2) eq 2) w2(b0,b1,b2)=(ord(b0) eq 3) + (ord(b1) eq 3) + (ord(b2) eq 3)

And then we create three indexed constraints for the respective type of symol: Constant_composition0(b0,b1,b2).. w0_c*word(b0,b1,b2) =e= w0(b0,b1,b2)*word(b0,b1,b2) Constant_composition1(b0,b1,b2).. w1_c*word(b0,b1,b2) =e= w1(b0,b1,b2)*word(b0,b1,b2) Constant_composition2(b0,b1,b2).. w2_c*word(b0,b1,b2) =e= w2(b0,b1,b2)*word(b0,b1,b2)

The first two MILP models that generate optimal error correcting codes differ in the manner it is imposed a minimum Hamming distance over all possible pairs of words. In the first MILP model the indexed set of constraints that impose a minimum Hamming distance d have a number of indexes equal to the double of the length of the code:

Fund_constr1(b0,b1,b2,c0,c1,c2)$(d_h(b0,b1,b2,c0,c1,c2) > 0).. d_h(b0,b1,b2,c0,c1,c2) + (1-word(b0,b1,b2)) * d + (1-word(c0,c1,c2)) * d ≥ d The dollar operator restricts the constraints to pairs of different words since two equal words have an Hamming distance equal to zero. Note that d is a constant parameter and so

Solving Nonlinear Problems with MILP Models (1-word(c0,c1,c2)) * d does not turn out the constraint in a nonlinear constraint. The meaning of 1-word(c0,c1,c2) is that the word (c0,c1,c2) does not exist. So when we multiply this term by d and add it to the left side of the constraint we are Relaxing the constraint guaranteeing that it is always true when either of the words (b0,b1,b2) or (c0,c1,c2) does not belong to the code. By the contrary when both words exist this constraint simplifies to

d_h(b0,b1,b2,c0,c1,c2) ≥ d So when both words exist this constraint impose that they are from each other at a Hamming distance greater or equal to d , and when either of the words does not exist it is guaranted that the constraint is true through the two relaxing terms. By other words this constraint imposes that the generated code must has a minimum Hamming distance d. The number of words is simply the number of 1’s of the binary variable word(b0,b1,b2) which can be calculated by Calc_n_w.. n_w=e=sum((b0,b1,b2), word(b0,b1,b2)) Finally since the optimal code has a maximum number of words we just tell to the Cplex solver to maximize n_w: Solve CODE using MIP maximizing n_w; This approach has the main drawback that it needs a number of indexes equal to the double of the length of the code. Since the GAMS software has the limitation of 20 indexes this means we only can generate codes with a maximum length 10. In the second MILP model we rewrite the fundamental constraint such that the model uses a number of indexes equal to the code length. The main idea behind it is that if a word belongs to the code then the number of other words of the code Hamming distance less than d must be Zero. This idea is implemented by the following set of indexed constraints:

Fund_constr1(b0,b1,b2).. sum((c0,c1,c2)$( ( (ord(c0) ne ord(b0)) + ( ord(c1) ne ord(b1)) +

J. Barahona da Fonseca ( ord(c2) ne ord(b2)) ) > 0), (( (ord(c0) ne ord(b0)) + ( ord(c1) ne ord(b1)) + ( ord(c2) ne ord(b2)) ) < d) * word(c0,c1,c2) ) ≤

N_Words * (1-word(b0,b1,b2))

N_Words is a constant with the maximum number of words. For example for a binary code of length 5, N_Words=25=32 and for the general case of a k-ary code, a code with an alphabet with k symbols, and length n we would have N_Words=kn. When the word (b0,b1,b2) belongs to the code, the second term of the inequality is zero and to be true the first term also must be zero. In the first term we sum the result of the inequality (( (ord(c0) ne ord(b0)) + ( ord(c1) ne ord(b1)) + ( ord(c2) ne ord(b2)) ) < d) * word(c0,c1,c2) over all words (c0,c1,c2) that are different from (b0,b1,b2). The multiplication by word(c0,c1,c2) guarantees that we only take into account the words (c0,c1,c2) that belong to the code. Again this may be done in a linear model since both ( (ord(c0) ne ord(b0)) + ( ord(c1) ne ord(b1)) + ( ord(c2) ne ord(b2)) and d are not model variables but parameter variables and the result of an algebraic operation over a set of logical operations over the correspondent indexes. By the contrary when the word (b0,b1,b2) does not belong to the code the constraint is Relaxed since the second term of the inequality assumes the value N_Words which guarantees that the constraint will be true for any word (b0,b1,b2) that does not belong to the code. Since the Hamming distance is symmetric we found a way to reduce to an half the number of fundamental indexed constraints substituting the $ argument from d_h(b0,b1,b2,c0,c1,c2) > 0 to value(b0,b1,b2) > value(c0,c1,c2) where the parameter value(b0,b1,b2) is defined by value(b0,b1,b2)=ord(b0)-1 + base * (ord(b1)-1) + base2 (ord(b2)-1). The third and fourth models result from another approach. We assume known the maximum number of words and maximize the minimum Hamming distance, d, of the generated optimal error correcting code.

Solving Nonlinear Problems with MILP Models The third model resulted from the first simply turning the minimum Hamming distance as the objective variable and fixing the numer of words as a parameter equal to the known optimal number of words. This way when the model reaches the maximum Hamming distance it is guaranteed that it generated an optimal error correcting code. Nevertheless this model shown very great runtimes and and very high memory resources consumption. Then we converted the second model to allow a constant number of words and a variable minimum Hamming distance. But now we face a great difficulty: since d_h_min is the ojective variable the following expression inside an sum operator is no more valid in a linear model: ( d_h(b0,b1,b2,c0,c1,c2) < d_h_min ) * word(c0,c1,c2) Next we will show how we solved this problem. The main idea is to create a fundamental indexed binary variable d_h_i(b0,b1,b2, i) that is 1 when

the word (b0,b1,b2) exists, i.e.

word(b0,b1,b2)=1, and i must be such that ord(i)=d_h_min, d_h_min being the minimum Hamming distance of the generated error correcting code, the objective variable we want to maximize. Then the fundamental constraint is written as Fund_constr4(b0,b1,b2).. Sum((c0,c1,c2,i)$(d_h(b0,b1,b2,c0,c1,c2) > 0), ( d_h(b0,b1,b2,c0,c1,c2) < ord(i) ) * d_h_i(c0,c1,c2,i) ≤ N_Words*(1-word(b0,b1,b2)) When the code has a length greater than 10 we must substitute the indexed parameter d_h(b0,b1,b2,c0,c1,c2) by its per extenso definition since the GAMS software only allows the use of 20 indexes. To maintain coherent the indexed binary variables word(b0,b1,b2) and d_h_i(b0,b1,b2,,i)

and

the variable d_h_min, the minimum Hamming distance of the code, which must be equal to ord(i) for all values of indexes such that d_h_i(b0,b1,b2,i)=1, we wrote the following sets of indexed constraints. We need one more indexed binary variable, d_bin(i), which is set to 1 when ord(i)=d_min through the following two simple constraints

J. Barahona da Fonseca calc_d_bin.. ∑(i, d_bin(i)*ord(i))=d_h_min; constr_d_bin.. ∑ (i, d_bin(i))=1; Next the following set of indexed constraints manage the zeros of the indexed binary variable d_h_i(b0,b1,b2,,i) , i.e. when the word does not belong to the code, word(b0,b1,b2)=0, OR d_bin(i)=0 THEN d_h_i(b0,b1,b2,i)=0 constr_word_d_bin(b0,b1,b2,,i).. 2*d_h_i(b0,b1,b2i) ≤ word(b0,b1,b2) + d_bin(i); And finally the following set of indexed constraints manages the ones of the indexed binary variable d_h_i(b0,b1,b2,,i), i.e. when the word belong to the code, word(b0,b1,b2)=1, AND d_bin(i)=1 THEN d_h_i (b0,b1,b2,i)=1 constr_word_d_bin2(b0,b1,b2,,i).. d_h_i(b0,b1,b2,,i) ≥ 1/2*(word(b0,b1,b2) + d_bin(i)) - 5*(1-d_bin(i))-5*(1-word(b0,b1,b2)); 2.1.2 Solution of the Travelling Salesman Problem with a MILP Model

Another example where we apply our techniques of implementation of a logical AND function between several variables is the travelling salesman problem. The main difficulty in solving this problem is to guarantee only one tour through all cities. For that purpose ??? we created an auxiliary binary variable arc_pos(c1, p1, c2, p2) which is one when there is an arc between cities c1 and c2 , arc(c1,c2)=1, and position(c1,p1)=1 and position(c2,p2)=1. sets c /1*20/; *city

sets p /1*20/; *position

alias(p1, p2, p);

alias (c1,c2,c3,c4,c5,c6,c7,c8,c9,c10, c);

Solving Nonlinear Problems with MILP Models

binary variable arc(c, c1), position(c,p), arc_pos(c1,p1,c2,p2);

variable d, d_p;

parameter distance(c,c1), N ;

N=card(c); distance(c,c1)=100+ 2**(ord(c)+ord(c1)); constr1_position(c).. sum(p, position(c,p))=e=1; constr2_position(p).. sum(c, position(c,p))=e=1; constr_arc_pos(c1,c2).. sum((p1,p2), arc_pos(c1,p1,c2,p2))=e=arc(c1,c2);

Main_Constraint(c1,p1,c2,p2)..

4*arc_pos(c1,p1,c2,p2)=l=arc(c1,c2)+position(c1,p1)+position(c2,p2)+( (ord(c1) NE ord(c2)) AND( (ord(p2) = (ord(p1)+1)) OR (ord(p2)=(ord(p1)-N+1))) );

all_cities_once(c).. sum(c1, arc(c,c1))=e=1; all_cities_once2(c).. sum(c1, arc(c1,c))=e=1; calc_obj.. d=e=sum((c,c1), distance(c,c1)*arc(c,c1)); Model TSP /all/; solve TSP using MIP miniizing d;

The main idea behind this very subtle MILP model is concentrated in the indexed sets of constraints constr_arc_pos(c1,c2) and Main_Constraint(c1,p1,c2,p2) that simultaneously maintain coherent the indexed binary variable arc_pos(c1,p1,c2,p2) with the indexed binary variables arc(c1,c2), position(c1,p1) and position(c2,p2) and guarantee

J. Barahona da Fonseca there is only one cycle or tour through the logical condition ( (ord(c1) NE ord(c2)) AND( (ord(p2) = (ord(p1)+1)) OR (ord(p2)=(ord(p1)-N+1))) ) that says that two successive cities in the tour either have a consecutive order or the first city is the last in the tour and the second city assumes the initial position in the tour. Then we find another way of solving the travelling salesman problem with an apparently simpler MILP model but surprisingly it showed worse runtime performance and much greater memory consumption. We present it in table ??. In a all connected graph, as is the example of the model, we will have n! solutions and the indexed binary variable will have nn instances. Even for only 10 cities we would consume more than 10G of memory which is impractical for today PCs. As a matter of fact we only could reach the solution for 7 cities and 77=0.82 M and for 8 cities (88≈17M) the solver ended the compilation and began to solve the model but stopped with an out of memory message in the presolve stage, so it seems that each instance of the indexed binary variable path consumes almost 100 bytes. sets c /1*5/; *city alias (c1,c2,c3,c4, c); binary variable arc(c, c1), path(c1,c2,c3,c4,c); variable d; parameter distance(c,c1),all_different3(c,c1,c2), all_different4(c,c1,c2,c3), all_different5(c,c1,c2,c3,c4), N; N=card(c); all_different5(c,c1,c2,c3,c4)=(ord(c) ne ord(c1))*(ord(c) ne ord(c2))*(ord(c) ne ord(c3))*(ord(c) ne ord(c4))*(ord(c1) ne ord(c2))*(ord(c1) ne ord(c3))*(ord(c1) ne ord(c4))*(ord(c2) ne ord(c3))*(ord(c2) ne ord(c4))*(ord(c3) ne ord(c4)); distance(c,c1)=100+ 4**abs(ord(c)-ord(c1)); equations calc_obj constr_path constr_path2 ;

Solving Nonlinear Problems with MILP Models

constr_path.. sum((c,c1,c2,c3,c4)$all_different5(c,c1,c2,c3,c4), path(c,c1,c2,c3,c4))=e=1; constr_path2.. sum((c,c1,c2,c3,c4)$(1-all_different5(c,c1,c2,c3,c4)), path(c,c1,c2,c3,c4))=e=0; calc_obj.. d=e=sum((c,c1,c2,c3,c4)$all_different5(c,c1,c2,c3,c4), (distance(c,c1)+distance(c1,c2)+distance(c2,c3)+ distance(c3,c4)+distance(c4,c) )*path(c,c1,c2,c3,c4 )); Model TSP /all/; solve TSP using MIP miniizing d; 2.2 Linearization of a Nonlinear Model with Real Variables If some variables are not integers then we must set i /1*10/; set j /v1*v130/; alias(k,j); binary variable b_bin(i,j), a_bin(i,j), ab_bin(i,j,k); parameter a_par(i), b_par(i); a_par(i)=1.2*ord(i); b_par(i)=1.3*ord(i); calc_a_bin1(i).. sum(j, a_bin(i,j))=e=1; calc_a_bin2(i).. sum(j, a_bin(i,j)*ord(j))=e=a(i)*10; calc_b_bin1(i).. sum(j, b_bin(i,j))=e=1; calc_b_bin2(i).. sum(j, b_bin(i,j)*ord(j))=e=b(i)*10; calc_ab_bin1(i).. sum((j,k), ab_bin(i,j,k))=e=1; calc_ab_bin2(i,j,k).. 2*ab_bin(i,j,k)=l=a_bin(i,j)+b_bin(i,k); calc_a(i).. a(i)=e=a_par(i);

J. Barahona da Fonseca calc_b(i).. b(i)=e=b_par(i); calc_obj(i).. obj ≥ sum((j,k),ord(j)*ord(k)*ab_bin(i,j,k)/100); Model Maximum /all/;

Solve Maximum using MIP minimizing obj; Table ??. MILP Model to obtain the maximum of the product of two indexed model variables, a(i)*b(i) With some little changes of this model we obtain the minimum of the product of the referred indexed variables: calc_obj(i).. obj ≤ sum((j,k),ord(j)*ord(k)*ab_bin(i,j,k)/100); Model Minimum /all/;

Solve Minimum using MIP maximizing obj; Table ??. Changes to the previous MILP model to obtain the minimum of the product of two indexed variables, a(i)*b(i).

2.3 Logical Implication Between Two Sets of Binary Variables The logical implication between two sets of binary variables, a(i) => b(i), can be implemented by an alternative simpler following set of constraints: a_implies_b(i).. a(i) ≤ b(i) since when a(i) is ‘1’ then b(i) must be ‘1’, but when a(i) is ‘0’, b(i) can be ‘0’ or ‘1’, which coincides to the logical implication function truth table.

2.2. Using Logical Implication to Obtain the Shortest Path in a Graph We did apply our solution of implementation of logical Implication to the generation of the shortest path in a given graph using a MILP model. To our knowledge this is the first published

Solving Nonlinear Problems with MILP Models solution of the shortest path problem based on the logical implication function. In the following example, Path(i,j) is a binary variable that says that the directed arc (i,j) belongs to the shortest path. Node 1 is the origin node and 13 the destination node. The parameter dist(i,j) contains the distance between node i and j. We solve the problem of nonexistent arcs attributing to them a very high value of dist(i,j) such that when we minimize the distance d they are never considered. * any node that belongs to the Shortest Path must have a successor, except the destination node, *node 13 constr4_path(i,j) $( (ord(j) ne 13) and (ord(i) ne ord(j)) ).. Path(i,j) ≤ ∑ (k $ ( ord(k) ne ord(j)), Path(j,k) ) calc_d.. d = ∑ ( (i,j) , dist(i,j) Path(i,j) ) Next we will explain how the first set of constraints translates into a equivalent logical predicate. Saying that each node i that belong to optimal path must have a successor can be expressed in formal logic as N

∑ Path(i, k) > 0 Path(j, i) ⇒ Node i Must Have a Successor ≡

k =1, k ≠ i

.

The total distance will be the sum of the binary variable Path over all possible arcs (i,j) that define the graph.

2.4 Generation of Optimal Error Correcting Codes or a New Way to Linearize the Generalized Exclusive OR Logical Function Although a typical electrical engineering application the solution of this very nonlinear problem with linear MILP models gave rise to a set of new and original techniques of mathematical programming, namely the linearization of the generalized exclusive OR function through the intensive use of the indexes. The main difficulty to generate optimal error correcting codes with a given minimum Hamming distance d , the code with a maximum number of words, with a MILP model is to calculate the Hamming distance between all pairs of words of the code, defined as the number of correspondent characters that are different, since this is a very nonlinear function. We will see that

J. Barahona da Fonseca our MILP model has only one constraint to calculate the number of words that will be maximized and a set of similar constraints that will impose that all possible pairs of different words belonging to the code must have a Hamming distance greater or equal to d. Obviously two equal words have a zero Hamming distance. We will show three MILP models to solve this problem and we will discuss the advantages and disadvantages of each one. Surprisingly we will show that the MILP model that presents the best runtime performance is the MILP model that consumes more computational resources. The logical function that is one when two input digital variables are different is called the Exclusive OR logical function or simply XOR. The generalized XOR is an extension of this logical function that admits any type of input variables. To calculate the Hamming distance between two words we must calculate the generalized XOR between each pair of correspondent characters and sum all these results. There are two manners to linearize this nonlinear function. The first is described in appendix 1 as a set of complex linear constraints and gave rise to our first model. Although small, this model is very difficult to be solved by the Cplex solver and results in very big runtimes even for small problem instances. The second, described in appendix 2, is based on the idea of the association of each index of a binary variable to a character of the word and the words belonging to the code will correspond to the combinations of indexes for which the binary variable has value 1. This latter approach gave rise to a much greater MILP model, but much more efficient since the Hamming distance between two words is calculated directly by a single nonlinear expression based on the values of the indexes and not by a set of complex and difficult to solve constraints. Note that making nonlinear operations over the indexes do not turn the model nonlinear, only over the variables. This is the main idea behind this second approach to optimal error correcting codes generation with MILP models.

Solving Nonlinear Problems with MILP Models Since the generalized XOR between two characters is equivalent to a function that tests if they are different, in GAMS software this done simply using the not equal operator ne. For example to impose that the minimum Hamming distance is 2, besides to calculate it, we must relax the constraint if one of the words or both words do not belong to the code. Since the code is defined by a binary variable word whose indexes correspond to the characters of each word, for a code with length 3 we just write:

Fund_constr(a1,a2,a3, b1,b2,b3)$( ( (a1 ne b1)+(a2 ne b2)+(a3 ne b3) ) >0).. (a1 ne b1)+(a2 ne b2)+(a3 ne b3) +3(1-word(a1,a2,a3))+3(1-word(b1,b2,b3)) ≥ 2

(1)

Note that in (1), (a1 ne b1)+(a2 ne b2)+(a3 ne b3) is just the Hamming distance between words (a1,a2,a3)

and

(b1,b2,b3).

This

½(word(a1,a2,a3)+word(b1,b2,b3)),

is

not a

necessary possible

to

multiply

this

linearization

term

by of

word(a1,a2,a3) AND word(b1,b2,b3), since when both word(a1,a2,a3) and word(b1,b2,b3) are 1, the last 2 terms vanishes and the resultant constraint just imposes that the Hamming distance between all pairs of words belonging to the code is greater or equal to 2, which is equivalent to impose a minimum Hamming distance 2. The two terms with the negation of word binary variable, i.e. (1-word(b1,b2,b3)) , prevent this constraint be considered when one of the words or both do not belong to the code, i.e. word(a1,a2,a3)=0 or word(b1,b2,b3)=0 or both cases, since we multiply both of these terms by 3 and this will guarantee that the constraint will always be true when any of the words do not belong to the code. Finally the GAMS software syntax restriction $ operator prevents that we only compare different words, which is equivalent that their Hamming distance d>0. This solution generates a much greater MILP model, nevertheless is much more efficient because the Hamming distance between two words is calculated over the indexes and not through the solution of a complex set of constraints. The main limitation of this second solution consists in the great consumption of indexes, since the fundamental constraint has a number of indexes equal to the double of code length. With the GAMS software this limitation would mean that the maximum generated code length would be 10.

J. Barahona da Fonseca With the last MILP model, described in appendix 3, we rewrote the fundamental constraint in such a way that it only has a number of indexes equal to the code length. Now with the GAMS software we could generate optimal codes with a maximum length 20, if we have enough resources. We do not use an auxiliary parameter to store the Hamming distance between all pairs of words because this parameter would have a number of indexes equal to the double of the length of the code. Nevertheless with our 2G RAM PC we only found possible to generate ternary constant composition optimal codes with length 15. The main problems arose in the compilation phase, since after that phase the monitored used memory reduced drastically. This technique of constraint relaxation is an original technique in the literature of mathematical programming and we describe it in detail later in this section. To reduce the great combinatorial search space we only generate ternary codes with constant weight w, i.e. where all words of the code have exactly w characters different from 0, and constant composition codes where all words have an exact number of w0 zeros, w1 ones and w2 twos. To impose a constant weight w we just compare each character with 0 and sum all comparisons and to prevent that this set of constraints be considered when the word do not belong to the code, this time we just multiply both terms of the equality constraint by the binary variable that defines the code word. For a code of length 3 we would have the following equality constraint

((a1 ne 0)+(a2 ne 0)+(a3 ne 0)) word(a1,a2,a3) = w word(a1,a2,a3)

(2)

When the word belongs to the code, i.e. word(a1,a2,a3)=1, the resultant constraint imposes that the weight of the word must be equal to w and when the word does not belong to the code the constraint is relaxed resulting in 0=0.

With the GAMS software we could had solved this problem in a simpler way using the restriction $ operator that allow us to define a subset of a given domain. We need an auxiliary parameter wp(a1,a2,a3) where are stored the weights of all possible words of the code and then impose that

Solving Nonlinear Problems with MILP Models $((wp(a1,a2,a3)=w)*(wp(b1,b2,b3)=w)). Multiplication in this latter term has the meaning of a logical AND of the two logical conditions:

Fund_constr(a1,a2,a3,b1,b2,b3) $ ( ( wp(a1,a2,a3)=w) * (wp(b1,b2,b3)=w) *((a1 ne b1)+(a2 ne b2)+(a3 ne b3)))>0)).. (a1 ne b1)+(a2 ne b2)+(a3 ne b3)+ 3 (1-word(a1,a2,a3)) + 3( 1-word(b1,b2,b3)) ≥ 2 (3) This way the set of auxiliary constraints (2) would not be necessary and the MILP model will be smaller and easier to be solved by the Cplex solver. To further reduce the number of constraints we substituted the condition that imposes different words by a relation of absolute value: (value(a1,a2,a3) > value(b1,b2,b3)) considering each word a base 3 number calculated as

value(b1,b2,b3)=ord(b1)+ord(b2)*3+ord(b3)*32

This way we reduced to an half the number of fundamental indexed constraints since we now compare each pair of different words only once. In the literature besides constant weight ternary codes studies there exist also a lot of results of still less combinatorial explosive error correcting codes, the constant composition codes, i.e. codes where all words have w0 zeros, w1 ones and w2 twos, expressed as [w0,w1,w2]. In table 9 we present the altered GAMS code only for a code of length 6 for clarity where we introduced restrictions correspondent to the constant composition distribution and in table 10 we present an example of an optimal constant composition ternary code with length 15 and minimum Hamming distance 3 confirming that A3(15,3, [12, 2,1])=105 [10]. Note this is a very difficult task since there exist 315=1.44 107 ternary words of length 15. In the GAMS code note that a condition on w2_p(b0..b5) on the number of 2s would be redundant since we already impose the number of zeros and ones. We use an altered version of the GAMS code presented in appendix 3. Now we sum the negation of the Hamming distance between two words being greater or equal to d, i.e.

J. Barahona da Fonseca being less than d, and if the word b0..b5 belongs to the code this sum is imposed to be zero, which is equivalent to say that there is no words belonging to the code at a Hamming distance less than d, and if this word do not belong to the code this set of fundamental constraints are relaxed by the multiplication of the negation of word(b0,b1,b2,b3,b4,b5) by a number greater than the total number of words of the code, in this case 729=36, since our code is ternary and has length 6. Although more computational intensive, this alternative solution ‘consumes’ only a number of indexes equal to the code length. Note that now the Hamming distance between any pair of words is not stored in a data structure since this would imply the consumption of a number of indexes equal to the double of the code length. By the contrary the Hamming distance between two words is always calculated by a long expression. 000000000001021 000000000010012 000000000000201 000000000102100 000000000210100 000000001000120 000000001002010 000000001200001 000000002000011 000000002101000 000000010001200 000000010012000 000000010020100 000000020000110 000000020110000 000000100020010 000000101000002 000000102000100 000000120000001 000000201001000 000001000010020 000001020001000 000001100000200 000001210000000 000002000100001 000002010000010 000002100001000

000010000000210 000010000021000 000010010000002 000010021000000 000010100000020 000010200100000 000011000200000 000012000010000 000020000000101 000020010100000 000021000000010 000100000002001 000100000100002 000100001020000 000100010000020 000100100200000 000100200000100 000101002000000 000120000001000 000200000011000 000200000100010 000200011000000 000201000000100 001000000001002 001000000120000 001000000200010 001000001000200

001000200000001 001001000002000 001010002000000 001020000010000 001102000000000 001200100000000 002000000001010 002000010000001 002000100100000 002001001000000 002010000000100 002100000010000 010000000000102 010000000100200 010000002010000 010000010200000 010000200000010 010001000020000 010002001000000 010010000002000 010020100000000 010100020000000 010200000000001 011000000000020 020000000001100 020000001100000 020000100010000

020001000000001 020110000000000 021000010000000 100000000020001 100000000100020 100000000201000 100000012000000 100000100002000 100000200010000 100001000000002 100002000000100 100020001000000 100100000000200 100210000000000 101000020000000 112000000000000 120000000000010 200000001010000 200000110000000 200001000100000 200010000000001 200100000000010 201000000000100 210000000001000

Table 9. Ternary code generated by our MILP model using the Cplex solver that confirms that the constant composition ternary optimal code with length 15, minimum Hamming distance 3 and all words with 12 zeros, 2 ones and one 1 has 105 words, i.e. A3(15,3, [12, 2,1])=105.

sets

Solving Nonlinear Problems with MILP Models b0 /0*2/; alias (b1,b2,b3,b4,b5, c0,c1,c2,c3,c4,c5, b0); Scalar d_h_min /3/ /3/ w0 w1 /2/; Parameter w0_p(b0,b1,b2,b3,b4,b5), w1_p(b0,b1,b2,b3,b4,b5); w0_p(b0,b1,b2,b3,b4,b5)=(ord(b0) eq 1) + (ord(b1) eq 1) + (ord(b2) eq 1) + (ord(b3) eq 1) + (ord(b4) eq 1) + (ord(b5) eq 1); w1_p(b0,b1,b2,b3,b4,b5)=(ord(b0) eq 2) + (ord(b1) eq 2) + (ord(b2) eq 2) + (ord(b3) eq 2) + (ord(b4) eq 2) + (ord(b5) eq 2); Variable n_p; Binary Variable word(b0,b1,b2,b3,b4,b5); calc_n_p.. n_p=e=∑ ( (b0,b1,b2,b3,b4,b5)$( (w0_p(b0,b1,b2,b3,b4,b5)=w0) * (w1_p(b0,b1,b2,b3,b4,b5)=w1) ), word(b0 b0,b1,b2,b3,b4,b5) ); fund_constr(b0,b1,b2,b3,b4,b5) $( (w0_p(b0 b0,b1,b2,b3,b4,b5)=w0) * (w1_p(b0,b1,b2,b3,b4,b5)=w1) ).. ∑( (c0,c1,c2,c3,c4,c5)$( (w0_p(c0,c1,c2,c3,c4,c5)=w0)* w1_p(c0,c1,c2,c3,c4,c5)=w1)* ( ( (ord(b0) ne ord(c0)) + (ord(b1) ne ord(c1)) + (ord(b2) ne ord(c2)) + (ord(b3) ne ord(c3)) + (ord(b4) ne ord(c4)) + (ord(b5) ne ord(c5)) )>0 ) ), *This last logical term means that the two words are different ( ( (ord(b0) ne ord(c0)) + (ord(b1) ne ord(c1)) + (ord(b2) ne ord(c2)) + (ord(b3) ne ord(c3)) + (ord(b4) ne ord(c4)) + (ord(b5) ne ord(c5)) ) < d_h_min)*word(c0,c1,c2,c3,c4,c5) ) ≤ 729*(1-word(b0,b1,b2,b3,b4,b5)); * If the word belongs to the code, 1-word(b0,b1,b2,b3,b4,b5)=0, so there must not exist any word at a Hamming distance less than d_min from this word Model OptCode /all/; Solve OptCode maximizing n_p using MIP; Table 10. Modified GAMS code to generate constant composition ternary codes that consumes a number of indexes equal to the code length.

3. Relaxation of a Set of Constraints by an Indexed Binary Variable When we want to restrict the following set of constraints a_less_than_b(i).. a(i) ≤ b(i) to the instances where a another binary variable c(i) is ‘1’, we cannot write, in a linear model, a_less_than_b(i) $ (c(i) = 1).. a(i) ≤ b(i)

J. Barahona da Fonseca since this will turn the model nonlinear, but we can write an equivalent set of constraints a_less_than_b(i).. a(i) ≤ b(i) + ( 1-c(i) ) max_a_minus_b Note that 1-c(i) means the negation of the binary or logical variable c(i), and when c(i)=0 the scalar constant max_a_minus_b is added to b(i) which guarantees that the constraint is always true, even when a(i) > b(i), i.e. the constraint is relaxed. This is equivalent to restrict the constraint to the cases where c(i)=1, for which the constant is not added. If we want to restrict an equality constraint a_equal_to_b(i).. a(i) = b(i) to the cases where another binary variable c(i)=1, we can solve the problem with two sets of constraints: a_less_than_b(i).. a(i) ≤ b(i) + ( 1-c(i) ) max_a_minus_b a_greater_than_b(i).. a(i) + ( 1-c(i) ) max_a_minus_b ≥ b(i) which are equivalent to the restricted equality constraint a_equal_to_b(i) $ ( c(i) = 1 ).. a(i) = b(i)

3.1. Simpler Equality Constraints Relaxation Based on the Indexes and on a Binary Variable In a work that we are just submitted to the IEEE Transactions on Information Theory [9] we impose the constraint of a constant weight w. The weight of a word is just the number of characters that are different from zero. In practical terms it is important to use constant weight codes because that implies a uniform power consumption in the coder and a uniform signal to noise ratio, and so a uniform probability of error in the transmission of all words. We only want to impose this weight constraint for words that belong to the code, i.e. for which the binary variable word(a1, .., an)=1. So the set of constraints that impose a weight w for a length 3 code is expressed in GAMS simplified syntax as Impose_w(a1,a2,a3)..word(a1,a2,a3)( (a1 ne 0)+(a2 ne 0)+(a3 ne 0) )=w word(a1,a2,a3) This is the simplest type of set of constraints relaxation by a binary variable that arises when we want to impose a set of equality constraints based on a computation over the indexes that define the set of constraints.

3.2. Automatic Relaxation of a Set of Constraints by a Set of Binary Variables

Solving Nonlinear Problems with MILP Models In some problems, like in university timetabling, there are sets of soft constraints with different priorities which can be relaxed, but we want to minimize the total number of relaxations. This can be done simply summing all binary variables associated to each set of soft constraints and multiplying this sum by a weight proportional to the relevance or priority of the set of soft constraints and finally summing these latter values to the objective variable, obj: Calc_obj1.. obj1 = ∑ (i, c1(i) ) Calc_obj2.. obj2 = ∑ (i, c2(i) ) Calc_obj.. obj_final = obj + w1 obj1 + w2 obj2 We applied these technique intensively in our work [11] where we make the university timetabling where there are hard constraints, constraints that cannot be relaxed, like a teacher cannot give two classes at the same time, a classroom cannot be occupied by two courses at the same time and soft constraints, like a teacher prefer to give classes in the afternoon. The main problem that we faced was the difficulty to make an hierarchy of soft constraints given more priority to a set than to another. We solved this problem given more weight to the sum of binary variables that control the relaxation of soft constraints with higher priority, but we recognize that this is not a satisfactory solution and in the near future we plan to apply evolutionary multiobjective algorithms to obtain the Pareto Front timetables and then apply a decision support algorithm to choose one timetable from the Pareto Front set of solutions. Next we show two hard constraints and two soft constraints. The GAMS primitive ord applied to an index gives the integer order of the value assumed by the index taking into account the set domain.

* HARD CONSTR- in one room, s, d and h it can be only 1 discipline hard_constr_ocup_s(s,d,h).. ∑ ( (c,t,sg), ocupation(s,d,h,c,t,sg) )≤ ≤1 ; * Hard Constraint- 1 student Cannot Have More than 1 Class at the Same Time constr1_student(sg,d,h).. ∑ ( (s,c,t), ocupation(s,d,h,c,t,sg) Class_sg(sg,c))≤ ≤1; *Soft Constraint- IF POSSIBLE Prevent Classes in the End of the Day constr2_student(d).. ∑ ( (s,t,h,c,sg), ocupation(s,d,h,c,t,sg) Class_sg(sg,c) (ord(h)=h_max) ) ≤ 100 Relax_constr2_st(d); *Soft Constraint- IF POSSIBLE 1 Teacher Must Not Give Consecutive Classes constr7_class(c,s,s1,d,d1,h,h1,t,sg,sg1)$(ord(d)>ord(d1)).. 50Relax_dist_c(t,c,sg,sg1) + ocupation(s,d,h,c,t,sg)ord(d)ocupation(s1,d1,h1,c,t,sg1)ord(d1)+ 50 (1-ocupation(s,d,h,c,t,sg)) + 50 (1-ocupation(s1,d1,h1,c,t,sg1))+ 50 (Teacher_hours(t,c)=0) ≥2 ; calc_obj.. obj=Flag_eq_h 100 aux_eq + ∑((c,sg),(n(c,sg)-Nhours_c(c)) ) + 5 ∑ ((t,c,p,sg),Category(t) Weights_pref(p) Relax_preference(t,c,p,sg)) + 5 aux3 + 5 aux4 + 50 aux5 + 1400 aux6 + 1350 aux61+ 1800 aux62 + 50 aux7 + 150 aux8 + 5000 aux9 + aux10 + aux11 + 1200 aux12 + 500 aux13 + 5 aux2_sg;

J. Barahona da Fonseca We claim that our automatic relaxation technique is the best solution to the very well know problem since 1990, the maximum constraint satisfaction problem, where we want to optimize one objective variable and simultaneously maximize the number of satisfied soft constraints. We also hope to show rigorously that our solution is the optimal solution to the constraint satisfaction problem.

4. Conclusions and Future Work The main conclusion of about the last four years of author’s mathematical programming experience is that mathematical programming tools have still a long way to go until we can solve real world linearized nonlinear problems with moderate computational resources using our linearization techniques. Although very powerful our linearization techniques turn the MILP model to solve a real world Nonlinear Problem too big to be solved by a PC. Nevertheless the author is very proud of having developed new techniques to linearize nonlinear problems that can be used by the mathematical programming community. The great difficulties of using nonlinear solvers, like the necessity of an initial feasible solution and the high probability of the solver being trapped in a local optimum points to the possible development of alternative nonlinear solvers based on neural networks and meta-heuristics. The great difficulty with the implementation of these solvers is to guarantee the same level of generality. For example, making solvers for meta-heuristics, in particular for Evolutionary Algorithms, implies the solution of two great problems: the automatic generation of good operators, which would only be possible using very sophisticated Artificial Intelligence techniques and the setting of the parameters, where there are already some results obtained in Fernando Lobo's PhD thesis [7].

Acknowledgements We are very grateful to Dr. Michael Bussieck of GAMS Corporation who gave us the solution of the linearization of the XOR function for the first version of our MILP model to generate error correcting codes.

5. References

[1] J. Barahona da Fonseca, “From the Magic Square to the Optimization of Networks of AGVs and from MILP to an Improved GRASP like Optimization Algorithm”, in Proceedings of CIMCA 06, IEEE Conference on Computational Intelligence for Modelling, Control and Automation, 181-186, 2006, available at IEEE Xplore database. [2] J. Barahona da Fonseca, “Code Design as an Optimization Problem: from MILP to an Improved High Performance Randomized GRASP like Algorithm”, in Proceedings of Escape-17, V. Plesu and P. S. Agachi (Eds), 279-284, 2007. [3] N. N. Horner, “Addition-Multiplication Magic Squares”, Scripta Math., 18, 300-303, 1952. [4] N. N. Horner, “Addition-Multiplication Magic Squares of Order 8”, Scripta Math., 21, 23-27, 1955. [5] J. Barahona da Fonseca, “The Magic Square as a Benchmark”, International Journal of Computing Anticipatory Systems, Vol. 18, 27-33, CHAOS, 2006.

Solving Nonlinear Problems with MILP Models

[6] T. Koch, Rapid Mathematical Programming, PhD thesis, Technische Universität Berlin, 2004. [7] F. Lobo, The Parameter Less Genetic Algorithm, PhD thesis, New University of Lisbon, 2000. [8] J. Barahona da Fonseca, “Training Hardlimit Multilayer Perceptrons with a MILP Model”, paper submitted to ESANN 2009 conference. [9] J. Barahona da Fonseca, “Generating Optimal Ternary Constant Weight Codes with a MILP Model”, paper submitted to the IEEE Transactions on Information Theory. [10] M. Svanström, P. R. J. Östergård and G. T. Bogdanova, “Bounds and Constructions for Ternary Constant-Composition Codes”, IEEE TRANSACTIONS ON INFORMATION THEORY, VOL. 48, NO. 1, JANUARY 2002. [11] J. Barahona da Fonseca, “Generating University Timetables with a MILP Model”, in Proceedings of Controlo 08, University of Tras-Os-Montes, Vila Real, Portugal, 2008.

Appendix 1. First MILP Model to Generate Error Correcting Codes where the Generalized XOR It is Linearized Set w1 /w1*w111/ * The code may have a maximum number of 111 words l /l1*l7/ * All words of the code have 7 characters a /0*2/; * Ternary code with alphanet={0,1,2} scalar d_h_min /3/; alias(w2, w1); alias(b, a); Variable n_p, h_d(w1,w2); Binary Variable opt_code(w1), c_bin(w1,l,a), c_bin2(w1,w2,l,a,b); Integer Variable word(w1,l); constr1_word(w1,l).. word(w1,l) ≥ 1; constr2_word(w1,l).. word(w1,l) ≤ card(a); constr_c_bin(w1,l).. ∑ (a, c_bin(w1,l,a)) = 1; calc_c_bin(w1,l).. ∑ (a, ord(a)*c_bin(w1,l,a)) = word(w1,l); * ALTERNATIVE WAY OF CALCULATING HAMMIG DISTANCE BETWEEN w1,w2 calc_c_bin2(w1,w2,l,a,b)$(ord(w1) > ord(w2)).. 2*c_bin2(w1,w2,l,a,b)≤c_bin(w1,l,a)+c_bin(w2,l,b); constr_c_bin2(w1,w2,l)$(ord(w1) > ord(w2)).. ∑ ((a,b), c_bin2(w1,w2,l,a,b))=1; calc_h_d(w1,w2)$(ord(w1) > ord(w2)).. h_d(w1,w2)= ∑ ((l,a,b), (ord(a) ne ord(b))* c_bin2(w1,w2,l,a,b));

J. Barahona da Fonseca * defd(w,l,a).. d(w,l,a) =e= c(l,a) xor 1$wl(w,l,a); * Can't do this with MILP, so we linearize: * d=1 => c xor wla = 1 *defd1(w1,w2,l,a)$(ord(w1) > ord(w2)).. c_bin(w1,l,a) + c_bin(w2,l,a) =l= 2 - df(w1,w2,l,a); *defd2(w1,w2,l,a)$(ord(w1) > ord(w2)).. c_bin(w1,l,a) + c_bin(w2,l,a) =g= df(w1,w2,l,a); * d=0 => c xor wl = 0 *defd3(w1,w2,l,a)$(ord(w1) > ord(w2)).. c_bin(w1,l,a) - c_bin(w2,l,a) =l= df(w1,w2,l,a); *defd4(w1,w2,l,a)$(ord(w1) > ord(w2)).. -c_bin(w1,l,a) + c_bin(w2,l,a) =l= df(w1,w2,l,a); * If w and c differ in l than the sum of the d's is 2, otherwise 0 *defdiff(w1,w2,l)$(ord(w1) > ord(w2)).. diff(w1,w2,l) =e= 0.5*sum(a, df(w1,w2,l,a)); ****************************************************************** *calc_h_d(w1,w2)$(ord(w1) > ord(w2)).. h_d(w1,w2)=e=∑ ∑ (l, diff(w1,w2,l)); constr_h_d_min(w1,w2)$(ord(w1) > ord(w2)).. h_d(w1,w2)+ (1-opt_code(w1))*card(l)+ (1-opt_code(w2))*card(l) ≥ d_h_min; calc_n_p.. n_p = ∑(w1, opt_code(w1)); *opt_code(w1)=>opt_code(w2) constr_contig(w1,w2)$( (ord(w1)=(ord(w2)-1))* (ord(w2)>1)).. opt_code(w2) ≤ opt_code(w1); constr_sat(w1,l).. word(w1,l) ≥ card(a)*(1-opt_code(w1)); Model OptTernCode /all/; Solve OptTernCode maximizing n_p using MIP;

Appendix 2. Second MILP Model to Generate Error Correcting Codes where the Number of Indexes is the Double of the Code Length Sets b0 /0*1/; alias (b1,b2,b3,b4, c0, c1, c2, c3, c4, Scalar d_h_min /3/ d_h_max /18/;

b0);

Parameter dist_h(b4,b3,b2,b1,b0, c4,c3,c2,c1,c0); *In this parameter is stored the Hamming distance between all possible pairs of words dist_h(b4,b3,b2,b1,b0, c4,c3,c2,c1,c0)=(ord(b4) ne ord(c4)) + (ord(b3) ne ord(c3)) + (ord(b2) ne ord(c2)) + (ord(b1) ne ord(c1)) + (ord(b0) ne ord(c0)) ; Variable n_p; Binary Variables word(b4,b3,b2,b1,b0); calc_constr_d_h(b4,b3,b2,b1,b0, c4,c3,c2,c1,c0)$( dist_h(b4,b3,b2,b1,b0,c4,c3,c2,c1,c0)>0).. * dist_h(b4,b3,b2,b1,b0, c4,c3,c2,c1,c0) > 0 means that these 2 words are different

Solving Nonlinear Problems with MILP Models d_h_min ≤ dist_h(b4,b3,b2,b1,b0, c4,c3,c2,c1,c0) + d_h_max*(1-word(b4,b3,b2,b1,b0))+ d_h_max*(1-word(c4,c3,c2,c1,c0) ); * The last two terms relax the constraint when one or both words do not be belong to the code calc_n_p.. n_p=∑ ( (b4,b3,b2,b1,b0), word(b4,b3,b2,b1,b0) ); Model OptCode /all/ ; Solve OptCode using MIP maximizing n_p;

Appendix 3. Third MILP Model to Generate Error Correcting Codes where the Number of Indexes is Equal to the Code Length Sets b0 /0*2/; alias (b1,b2,b3,b4,b5,b6, c0,c1,c2,c3,c4,c5,c6, b0); Scalar d_h_min /3/; Variable n_p; Binary Variable word(b0,b1,b2,b3,b4,b5,b6); calc_n_p.. n_p = ∑ ( (b0,b1,b2,b3,b4,b5,b6), word(b0,b1,b2,b3,b4,b5,b6) ); fund_constr(b0,b1,b2,b3,b4,b5,b6)..

∑ ( (c0,c1,c2,c3,c4,c5,c6)$

(((ord(b0) ne ord(c0)) + (ord(b1) ne ord(c1)) + (ord(b2) ne ord(c2)) + (ord(b3) ne ord(c3)) + (ord(b4) ne ord(c4)) + (ord(b5) ne ord(c5)) + (ord(b6) ne ord(c6)) )) ) > 0 ), (((ord(b0) ne ord(c0)) + (ord(b1) ne ord(c1)) + (ord(b2) ne ord(c2)) + (ord(b3) ne ord(c3)) + (ord(b4) ne ord(c4)) + (ord(b5) ne ord(c5)) + (ord(b6) ne ord(c6)) ) < d_h_min)*word(c0,c1,c2,c3,c4,c5,c6)) ≤ 100000*(1-word(b0,b1,b2,b3,b4,b5,b6)); Model OptCode /all/; Solve OptCode maximizing n_p using MIP;

Appendix 4. Solution of the Maximization of x2+y2+z2+w2 with a Linear MILP Model sets v1 /1*10/; alias(v2,v3,v4, v1); positive variable x,y,z,w; binary variable xyzw(v1,v2,v3,v4), x_bin(v1), y_bin(v1), z_bin(v1), w_bin(v1); variable obj; only_one.. ∑ ((v1,v2,v3,v4), xyzw(v1,v2,v3,v4))=1;

J. Barahona da Fonseca only_one_x_bin.. ∑ (v1, x_bin(v1))=1; only_one_y_bin.. ∑ (v1, y_bin(v1))=1; only_one_z_bin.. ∑ (v1, z_bin(v1))=1; only_one_w_bin.. ∑ (v1, w_bin(v1))=1; translate_x_x_bin.. ∑ (v1, ord(v1)*x_bin(v1))=x; translate_y_y_bin.. sum(v1, ord(v1)*y_bin(v1))=y; translate_w_w_bin.. ∑ (v1, ord(v1)*w_bin(v1))=w; translate_z_z_bin.. ∑ (v1, ord(v1)*z_bin(v1))=z; translate_x_y_z_w_v1_v2_v3_v4(v1,v2,v3,v4).. 4*xyzw(v1,v2,v3,v4) ≤ x_bin(v1)+y_bin(v2)+z_bin(v3)+w_bin(v4); calc_obj.. obj=∑ ((v1,v2,v3,v4), (ord(v1)2+ord(v2) 2+ord(v3)2+ord(v4) 2) * xyzw(v1,v2,v3,v4)); constr_x.. x ≤ 5; constr_y.. y ≤ 6; constr_z.. z ≤ 4; constr_w.. w ≤ 3; model NLF2a /all/; solve NLF2a using MIP maximizing obj;

Output of the run of this MILP model with the Cplex solver obtained after very few seconds:

----

80 VARIABLE xyzw.L

INDEX 1 = 5 3 6 .4 ----

1.000 80 VARIABLE x.L

=

5.000

Solving Nonlinear Problems with MILP Models ----

80 VARIABLE x_bin.L

5 1.000 ----

80 VARIABLE y.L

----

80 VARIABLE y_bin.L

=

6.000

=

4.000

=

3.000

=

86.000

6 1.000 ----

80 VARIABLE z.L

----

80 VARIABLE z_bin.L

4 1.000 ----

80 VARIABLE w.L

----

80 VARIABLE w_bin.L

3 1.000 ----

80 VARIABLE obj.L

Appendix 5. MILP Model to Solve a Multiplicative SemiMagic Square of Order 3 Using the Second Methodology of Linearization sets l /1*3/; alias(c,c1,l1,l); sets v1 /1*36/; alias(v2, v3, v1); positive variable a(l,c), prodc(c), prodl(l); variable obj; binary variable a_bin(l,c,v1), prodc_bin(c,v1,v2,v3), prodl_bin(l,v1,v2,v3); aux_constr1(l,c).. a(l,c) ≤ 36; aux_constr2(l,c).. a(l,c) ≥ 1;

J. Barahona da Fonseca constr_a_bin(l,c).. ∑ (v1, a_bin(l,c,v1)=1; constr_a_bin2(l,c).. ∑ (v1, ord(v1)*a_bin(l,c,v1))=a(l,c); * These two constraints translate a(l,c) into the index v1 all_different_constr(v1).. ∑ ( (l,c), a_bin(l,c,v1)) ≤ 1; * This is the fundamental constraint that guarantees that all a(l,c) are different from each other constr_prodc(c).. ∑ ( (v1,v2,v3), prodc_bin(c,v1,v2,v3))=1; constr_prodl(l).. ∑ ( (v1,v2,v3), prodl_bin(l,v1,v2,v3))=1; calc_prodc_bin(c,v1,v2,v3).. 3*prodc_bin(c,v1,v2,v3) ≤ a_bin('1',c,v1)+a_bin('2',c,v2)+a_bin('3',c,v3); * These set of constraints translate each column c to the indexes v1,v2,v3 calc_prodl_bin(l,v1,v2,v3).. 3*prodl_bin(l,v1,v2,v3) ≤ a_bin(l,'1',v1)+a_bin(l,'2',v2)+a_bin(l,'3',v3); * These set of constraints translate each line l to the indexes v1,v2,v3 calc_prodc(c).. prodc(c)= ∑ ((v1,v2,v3), ord(v1)*ord(v2)*ord(v3)*prodc_bin(c, v1,v2,v3)); calc_prodl(l).. prodl(l)= ∑ ((v1,v2,v3), ord(v1)*ord(v2)*ord(v3)*prodl_bin(l,v1,v2,v3)); eq_constrs(c,c1).. prodc(c)=prodc(c1); eq_constrs2(l,l1).. prodl(l)=prodl(l1); eq_constrs3(l,c).. prodl(l)=prodc(c); calc_obj.. obj=∑( (c,l), a(c,l)); model MultMgSq /all/; solve MultMgSq using MIP minimizing obj; Output of the model after about 6 hours of runtime in a 2GHz clock PC: ----

100 VARIABLE a.L 1

1 2 3 ----

6.000 35.000 1.000

3

5.000 2.000 21.000

7.000 3.000 10.000

100 VARIABLE a_bin.L 1

1.1 1.2 1.3

2

2

3

5

6

7

1.000 1.000 1.000

Solving Nonlinear Problems with MILP Models 2.2 2.3 3.1

1.000

+

10

2.1 3.2 3.3 ----

1.000 1.000 21

35 1.000

1.000 1.000 100 VARIABLE prodc.L

1 210.000, 2 210.000, 3 210.000 ----

100 VARIABLE prodc_bin.L

INDEX 1 = 1 1 6 .35

1.000

INDEX 1 = 2 21 5 .2

1.000

INDEX 1 = 3 10 7 .3 ----

1.000 100 VARIABLE prodl.L

1 210.000, 2 210.000, 3 210.000 ----

100 VARIABLE prodl_bin.L

INDEX 1 = 1 7 6 .5

1.000

INDEX 1 = 2

J. Barahona da Fonseca 3 35.2

1.000

INDEX 1 = 3 10 1 .21

1.000

Appendix 6. MILP Model to Solve the Sudoku Puzzle of Any Order using the AllDifferent Constraint sets l /1*16/; alias(c, v1, l); set c1 /1*4/; alias(l1, c1); binary variable a_bin(l,c, v1); scalar Order /4/; Parameter square(l1,c1,l,c); * Auxiliary binary parameter to define the alldifferent constraint over the squares square(l1,c1,l,c)=(ord(l) ge ((ord(l1)-1)*Order+1) ) * (ord(l) le ((ord(l1)-1)*Order+Order))* (ord(c) ge ((ord(c1)-1)*Order+1))*(ord(c) le ((ord(c1)-1)*Order+Order)); variable obj; only_one(l,c).. ∑ (v1, a_bin(l,c,v1))=1; all_different_line(l,v1).. ∑(c, a_bin(l,c,v1)) = 1; all_different_column(c,v1).. ∑ (l, a_bin(l,c,v1)) = 1; all_different_square(l1,c1,v1).. ∑ ((l,c)$( square(l1,c1,l,c)), a_bin(l,c,v1)) = 1; calc_obj.. obj=∑ ((l,c,v1), a_bin(l,c,v1)); model Sudoku /all/; solve Sudoku using MIP minimizing obj;

Solving Nonlinear Problems with MILP Models Solution obtained for a 25x25 Puzzle; the solution of a traditional puzzle may be easily obtained introducing constraints correspondent to the given fixed numbers of the matrix:

5 7 4 1 16 23 20 21 19 18 13 2 3 25 9 24 11 12 8 17 15 10 6 22 14

25 24 17 20 10 3 9 2 14 6 18 19 21 8 7 4 16 22 13 15 1 12 11 23 5

23 2 18 8 3 25 4 21 12 10 6 14 5 15 11 21 7 19 9 1 24 16 13 20 17

14 9 15 19 12 16 24 11 17 13 20 1 10 22 4 5 18 23 3 6 8 7 25 21 2

11 6 13 22 21 1 15 7 5 8 12 17 24 16 23 20 14 25 2 10 18 9 4 19 3

8 10 11 17 22 21 18 1 20 23 2 16 12 9 14 19 13 7 6 25 5 4 15 3 24

6 13 24 23 2 12 7 10 16 14 4 25 8 3 22 15 21 5 1 18 20 17 19 11 9

12 18 3 5 4 15 22 8 6 19 1 20 7 11 21 2 24 9 17 23 25 14 16 13 10

16 21 19 9 7 13 5 4 25 17 23 15 18 24 10 14 3 11 22 20 2 6 12 8 1

20 14 25 15 1 11 2 9 24 3 17 6 19 5 13 8 10 4 12 16 22 21 18 7 23

19 1 20 16 18 7 14 3 21 5 24 9 25 13 6 12 8 10 15 2 23 22 17 4 11

4 5 10 24 11 2 23 19 8 9 14 22 17 12 1 13 6 3 25 21 7 15 20 18 16

7 17 2 12 14 10 25 18 13 11 5 21 15 20 8 23 19 16 4 22 3 1 24 9 6

15 3 8 6 9 22 17 12 4 20 10 7 16 23 19 1 5 18 11 24 14 13 2 25 21

13 23 22 21 25 24 1 6 15 16 11 18 4 2 3 9 11 20 7 14 19 5 10 12 8

2 25 23 4 17 8 10 15 1 21 22 3 11 14 18 7 20 6 19 12 16 24 9 5 13

1 22 12 7 20 9 6 5 18 2 15 23 13 21 16 17 25 24 10 3 4 11 8 14 19

10 8 21 18 5 17 11 13 7 25 19 24 6 4 20 16 2 15 14 9 12 3 23 1 22

24 16 6 13 19 20 3 14 22 4 7 12 9 17 25 11 23 1 5 8 10 2 21 15 18

9 11 13 3 14 19 15 24 23 12 8 5 1 10 2 22 4 21 18 13 6 20 7 17 25

21 20 16 14 8 18 19 25 11 7 9 13 22 1 5 6 12 2 24 4 17 23 3 10 15

22 19 9 2 13 14 12 16 10 1 3 8 23 7 24 25 15 17 20 11 21 18 5 6 4

17 4 7 25 24 6 13 20 2 22 21 10 14 18 15 3 9 8 23 5 11 19 1 16 12

3 15 5 10 23 4 8 17 9 24 16 11 20 6 12 18 1 14 21 19 13 25 22 2 7

18 12 1 11 6 5 21 23 3 15 25 4 2 19 17 10 22 13 16 7 9 8 14 24 20

Solution obtained for a 36x36 Sudoku puzzle, a very combinatorial explosive problem, after some hours of runtime, without any fixed positions:

J. Barahona da Fonseca 36 2 9 22 5 13 1 8 17 24 31 30 12 28 23 3 4 33 15 10 25 14 16 20 35 21 11 27 18 6 34 19 7 32 26 29

20 23 6 18 7 28 16 19 22 34 3 21 8 10 35 5 9 27 29 36 17 11 24 4 14 32 26 15 25 30 33 12 2 1 13 31

21 1 8 15 17 14 2 9 4 25 33 5 13 36 32 19 26 24 35 12 7 18 28 30 22 20 31 29 16 34 6 11 27 23 3 10

33 35 27 29 25 24 32 10 11 14 6 18 22 31 17 34 7 30 26 5 2 13 21 3 9 8 12 23 19 1 16 28 20 4 36 15

26 32 16 34 12 4 13 20 27 36 23 15 25 2 18 29 14 11 19 8 9 1 6 31 7 3 10 28 33 17 22 21 5 24 35 30

10 19 31 30 11 3 28 26 29 35 7 12 6 15 20 21 1 16 23 27 33 34 32 22 5 24 36 2 13 4 25 14 8 9 18 17

8 9 25 21 31 5 26 30 23 17 34 4 15 29 33 11 22 6 28 3 27 20 12 18 16 13 19 10 2 14 1 7 32 35 24 36

30 29 28 36 6 23 35 14 3 13 20 32 17 4 19 31 27 5 11 2 1 15 33 24 21 7 34 9 22 12 26 8 18 10 25 16

32 19 33 26 2 16 25 15 28 10 9 24 30 23 9 35 21 8 36 31 34 29 19 17 27 18 1 5 6 3 12 22 14 13 4 11

13 22 1 24 18 10 27 2 33 11 8 29 14 3 36 12 34 25 7 32 16 6 9 21 26 4 23 20 35 15 30 5 19 17 31 28

7 4 14 17 3 11 21 12 16 18 1 19 20 13 2 32 10 26 25 30 35 5 22 23 24 28 8 36 29 31 27 34 9 33 15 6

34 15 35 12 27 19 22 5 6 7 36 31 9 16 28 1 24 18 8 26 4 10 14 13 33 30 25 17 11 32 2 3 21 29 23 20

2 12 10 9 28 6 24 35 19 15 21 27 26 25 34 8 36 1 22 7 3 23 5 33 11 29 13 4 30 20 14 18 31 16 17 32

18 14 13 35 22 30 33 7 2 1 28 36 27 21 24 10 5 9 34 4 12 32 25 29 17 19 15 31 8 16 11 26 6 3 20 23

11 3 32 5 34 25 18 22 10 30 8 31 12 6 23 20 19 16 17 26 28 35 36 1 27 2 14 7 30 27 4 13 15 9 33

19 31 29 27 33 21 11 25 5 20 14 13 16 7 15 4 17 22 18 6 25 9 2 1 3 10 32 35 20 23 22 36 34 8 12 24

16 7 36 1 26 20 3 4 31 23 17 6 29 14 11 30 18 32 21 15 24 8 13 27 12 9 33 34 28 22 19 25 10 2 5 35

4 8 15 23 24 17 12 16 9 32 26 34 2 35 3 13 33 28 20 14 10 31 11 19 25 6 5 18 21 36 29 27 1 7 30 22

9 30 3 16 8 29 6 21 36 2 32 25 35 34 13 18 12 17 31 33 14 27 7 26 4 22 20 30 5 28 29 1 15 11 10 25

28 21 4 33 14 15 31 24 34 9 10 35 5 22 7 16 29 3 13 25 36 19 23 11 30 2 6 1 12 26 20 32 17 18 8 27

22 17 7 2 36 12 29 33 1 28 18 11 32 20 27 9 6 15 10 24 8 30 34 5 31 14 35 19 23 25 3 13 26 21 16 4

25 27 20 11 19 35 5 23 12 4 13 3 33 1 26 24 30 36 2 18 22 17 29 9 10 16 21 32 15 8 31 6 28 34 14 7

6 18 24 32 23 34 8 17 7 26 19 16 28 11 14 2 31 13 4 20 21 12 1 15 36 33 3 13 9 27 35 29 30 25 22 5

31 5 26 10 13 1 30 27 20 22 15 14 23 8 21 25 19 4 6 35 32 16 3 28 18 17 29 7 34 11 36 33 18 12 2 9

15 16 22 7 9 8 23 32 24 21 4 17 36 6 25 20 13 31 33 34 18 2 10 14 29 26 28 11 1 19 5 35 12 30 27 3

12 26 21 4 20 32 19 28 13 16 22 1 18 5 30 14 35 2 9 23 15 7 17 8 6 34 27 33 3 10 24 31 29 36 11 25

14 6 5 3 35 18 20 31 15 29 25 33 1 17 12 7 28 23 27 16 11 26 30 32 2 36 4 8 24 13 9 10 22 19 34 21

27 24 34 28 10 31 14 36 18 6 12 2 11 9 22 33 3 29 1 19 13 35 4 25 15 5 30 16 32 21 8 17 23 20 7 26

17 11 30 13 1 2 7 34 35 5 27 10 24 19 4 26 8 21 3 22 29 36 20 6 23 12 18 25 31 9 32 15 16 28 33 14

23 36 19 25 29 33 9 11 8 3 30 26 10 32 16 27 15 34 5 21 28 24 31 12 20 35 14 22 17 7 18 2 4 6 1 13

29 10 11 14 30 26 34 13 25 31 2 7 4 33 9 15 16 8 32 28 23 21 36 35 19 1 24 12 27 5 17 20 3 22 6 18

35 34 2 19 4 22 36 18 14 27 5 9 7 26 29 28 11 20 30 1 6 3 8 16 13 23 17 21 10 33 15 24 25 31 32 12

3 33 23 31 15 27 17 6 21 8 16 28 34 24 10 36 2 14 12 29 20 22 18 7 32 25 9 26 4 35 13 30 11 5 19 1

24 25 18 8 16 9 4 1 26 19 11 20 3 30 31 22 32 12 17 13 5 33 27 34 28 15 7 6 36 29 10 23 35 14 21 2

1 13 12 6 32 7 10 29 30 33 24 22 21 27 5 17 23 35 14 11 19 25 15 2 8 31 16 3 20 18 4 9 36 26 28 34

5 28 17 20 21 36 15 3 32 12 35 23 19 18 1 6 25 13 24 9 31 4 26 10 34 11 22 30 14 2 7 16 33 27 29 8

Solving Nonlinear Problems with MILP Models But for the following hard puzzle we got the following in just few minutes of runtime: 32 24 16 9 36 26 2

28 10

25 18 7

3

3 14

9

27

13

6 1

9

32

12 2

22

6

13

30 10

8

2

25

25

18 36 17 16

24 14

5

20

29

24 19 18

16

22

28 8 9

23

13 18 1

29

20

4

13

36 25 28

34

2

22

4

8

32

31

8

7

6

7 34

6

32 30 35 17

5

2

1

9 31

8

28

34

25

3 33

21

10 13 25

9

8 15

1

2

17

5

21

4

19

18

2

3

23

10 15 14

7

28

23

7

6

1

13

18 34

5

1

16

3

18 15 17

33 32 34 26

15

21 20 26 2

19 1

29

8

20

16

11

12 11

5

30 25

6

22 33

2

35

3

5

31

1

23 18 35 36 27 22

10 4

28

28 10

20

3

19 29 12

14

15

6

15 21

7

36

13

9

1

35 21

27

15 27

4

29

20

6

8

14

28 17

1

32

3

10 20 9

18

11

22

12

6

14 26 24 32

14

25

34 23

12 11

11

3

35

31

5 16 22

12

22

6

16 30

2

9 16 29

29

23 33 35

28 32

11

29 8

1

9

31

35

36 34

23 32

11 10 33

8

3

1

3

31 17

28 19

21

26

6

22

20 12

35 20 30

24

9

3

8

19 10

22

4

16

24

18

36 15 31 23

2

19

3

9

22

23

25 26

23 36

11

19 25

10 30 11 16

30

13

7

29

2

5

35 36 20 16

22 20 31 16

33 6

2

27

16 33

24 27

17 22

8

21 26 24

4

30

9

21

21

27

27 31 16 13

20

32 15 13 17 16

1 18

26

11 12 20

25 33 34

36

7

15 33

29 12 13

7

36 20 19

5

31

25 15

7

4

33

3

2

26 9

24 34 13

23 30 34

32

26

36

3 4

7

3

1

13

11 25 18

7

31 15 27

1

35 18

18 28

22 11 24

5

8

4

29

4

12

27 30 14

31 10

31 21

28 32

25

11 30 12 25

10 35

23 13 14 21

17

1

8

3

16 34

36

13

4

3

27 7

13

5

23 18 15

10

6

25 22

29

2

6

23

7

6

28 35

16 30

33 36 27 15

5

9 36

34 31 24

15

6 8

10 26

14 13

33 20

2 33

19

16

24 32 1

21 17

27 25 11 32 24

32

32 19 14

21 11

35

17

35

7

18 30 15

5

32

33 29 12 24

13

27 12

3

1

2

34

35 30

20 34

36

3

33 26 35 10

9 14 6

28

9

3

36

27 33 36 35 17 28

36 20

26

1

19

23 26

29 21 31

24

20

23

29 22 20 28

34 27 17 16

22 30

17

15

6

11 29

22 12

19 21

30

33

21 17 18 34 24

10 5

9

4

19

7

15

15

7

4

25

9

4

31

5

23 20 31 16 32

14 25

19 11

13 33

22

27 32 12 34 36 25 23

28 8

2 8

29 35

8

3 21

23 33 34

13 17

7 11

15 13

17 11 18 13

8

26

9

25 34

4

10 25

16

33

4

30 12

2 14

7

J. Barahona da Fonseca

20

4

32 24 16

1

9

19 30 15 11 10 23 20 31 16 32

6

36 26

5

23 17

8

15 30

2

28 10

3

29 35

9

36 27 24 26

6

21 34 25 11 31

25 18 12 33 34 21

4

29

5

16 19

8

2

36 20 23

3

22 15 31 24 32 17 10 35 30 26

22 14 25 33 21 17 18 34 24 28

3

35 30 10 32

1

6

27 13 31

8

7

2

22 12

1

3 7

7

27 32 12 34 36 25 23 35

31

3

18 19 33 25 16

21 14 11 29 13 24 22 28

9

16 20

6

9

7

5

15 23

14

4

6

29 31

9

6

9

36

1

19 18 34 31 10 33 11 30 26 29

5

8

6

2

9

13 27 25 18 10 34 1

8 4

16 18 30

20 18 21 11 28 22 33

22 20 13 14 23 34

32 31 17 28

14 21

29 26 27 33 36 35 17 28

14 19 33 32 22 12

13 11

17

7

35 28 17

5

24 36 29 26

2

7 29

12 19 14 20 2

30

1

3

12

8

22 26

13 33

2

7

6

4

6

1

15

1

4

12

8

21 30 35 10 15 11

4

18

26

1

10 33 16 27 12 20

9

19 25 15

33

2

6

7

27 23 13 14 21 25 20 16 29 11 26 18 32 31 17

4

30

3

17 28

4 7

15

8

25 22

36 5

19 12 17 27 16

3

24

5

2

19 25

1

9

4

34 27

7

36 15 24 11 31

5

6

14 18 28 30 15 24 13 11 23 35

1

34 23 32

4

33 36 27

5

3

4

32 14 13 29 30 22 11 24

7

12 23 26 34 35

30 27 25 11 34 31 18 15

6

36

1

20 12 16

22 20 13 21 10

8

11 32 24

9

3

33 20 14 30 12 27 28 22 36 31 17 21 26 24

29 21 25

2

1

18 27 28 35

7

36

23 18 15 13

7

33 26 35 10 31 32 34 15

5

2

8

2

9

11

3

23 30 10 34 12

4

16 26 22 14 8

29 34 19 25 17 4

9

10 35 30 17

16 33 15 21 27

28 19

16 33

1

17

5

32 28

4

3

19 29 12

6

36 30 21

4

14 27 22

7

28

9

3

2

5

13 29 18 10 35 19

4

11 25

6

7

16

8

23

15

5

6

22 20 31 16 19 26 24 33 14 16

7

33

8

9

14 35 20 30 27 21

1

12

3

2

25

5

29

1

11 10 33 32 18

7

9

22 34

20 30 22

1

34 29 21 17 32 18 19

9

13

4

32 16

6

26 31

8

22 30

9

6

14 26

6

22 11 12 20 31

1

32 15 13 17 16 36 23 33 35

28 11 20 27 30 35

5

14 21 29 25 26

9

19 33 15

1

14 33 20 12

32

7

23

7

4

2

12 13 19 36 25 28 10

11 31 18 15 20 21 33 32 34 26 29

7

12 19

35

33 21 27 31

6

19 12 15 17

26 23 10 28

7

9

25 30 13 10 18

36 34

4

6

3

18

1

5

8

2

9

23 36 13

17

14

3

6

9

26

1

18 26 10 15 14

7

6

8

20 27 16 14 11 31

5

30 25

6

31 33

35

3

5

8

24

4

31 30

9

19 20 17 15 13 16 10 25

29

6

5

30

8

1

31 23 18 35 36 27 22

7

18 24 35 31 11 27 21 13

5

36 34

3

4

33 25 16 14 32 29

8

14 15 22

33 20 17 11 24 28 32 36

1

2

31 23

30

8

24 22 14 32 29 26 12

2

2

6

4

26 16 29 32

2

9

4

4

28 23 30

4

16

16 34 19 13 15 17 24 35

19 26 10 20 27 28 32 29 12 11

21 12 10 13 25 19 28 17

30 22 20

3

7

3

9

5

8

36 34

16 29 27 19 35

24 31 14

12 23 36 18

1

29 12 13 32 21 25 20 36 11 27 19 33 17 34 16 11

3

14 24

6

15 17 24 31 16

24 26 33 34 15

1

35

4

2

13

4

2

3

2

16 20 14 11 32

28 10

24 21 34 35 18 30 28 32

1

1

8

9

5

28 23

9

24

21

4

27 16 17

7

2

5

22 29 23 27 36

3

15

7

5

15 33 23 22

22 24 17 10

5

2

31 34 9

2

25 30 14

13 32

17 12 36 30 21 20 26

25 21

18 35 21 34

7

8

8

6

5

8

3

32 23 28 29 31 18 34

31 36 28 11 35 33 15 27 25 23 10 8

1

10 30 11 28 18

10 17 13 25 36 22

29

8

3

36

6

24

27 24 19 18 34 26

8

7

31

16 19 31 15 27 35

4

3

2

10 26 13 25

7

9

20 25

4

5

18

6

2

29

5

1

3

3

28

16 29 27 33 12 20

2

6

12

6

1

20 12 13

23

24 14 28 13

36

34 23

8

11 19 22 36 17 16 6

3

31 30 29 21 25 10 18 22 23 14 26 32 34

31 10 26

9

9

2

8

3

7

22

31

25

9

9

8

6

24

7

4

12

5

33 23 34 14 17 22 25

10 32 11 24 34 13 19

13 23 10 16 35 33 21 14 20 28 17 32 29 8

3

35 36 24 30

24 13 14 19

3

35

7

2

5

4

2

14 24 23 26 35

16

5

5

17

27 14

4

10 26

22 33 29 12

1

7

36 20 19

3

15 11 29 31 19 28 21 18 10 20 18 25 36

9

32 23 22

28

2

35 28 15 20 36

34 27 18 17 21 32 19

8

6

27 28 32

11 10 27 24 30 26 32 13 29 19 21 31 12 35 18

15

7

15 17 9

35 24

1

8

1

9

13

2

16 30

5

2

26 12 15 23 11

4

3

1

17

19 12

15 36 35 26 23 14 21 18

13 33

29 31 21

8

5

8

5

3

6

30 10 34

4

24 22 21 34 18 25 18

20 11 13 25 16 28 27

1

3

15 25 12 35

7

9

7

6

1

2

26 34 12

4

21

10 21 18 22 13 27 11 28 23 20 19 33

Solving Nonlinear Problems with MILP Models

Appendix 7. GAMS Implementation of our Novel MILP model to Solve Any Kakuro Puzzle sets i /1*9/; sets j /1*12/; set seg /1*3/; set v /1*9/; binary variable a_bin(i,j, v); Parameter n_segs_l(i), n_segs_c(j), ord_min_c(j,seg), ord_max_c(j,seg), ord_min_l(i,seg), ord_max_l(i,seg), sum_l(i,seg), sum_c(j,seg); ********************************************************************** * Next we will Define a 'Black Belt' Kakuro puzzle 12x9 that GAMS will solve in few seconds ********************************************************************** ********************************************************************** * First Step: we define the Number of 'Segments' of each Line ********************************************************************** n_segs_l('1')=3; n_segs_l('2')=2; n_segs_l('3')=2; n_segs_l('4')=3; n_segs_l('5')=3; n_segs_l('6')=3; n_segs_l('7')=2; n_segs_l('8')=2; n_segs_l('9')=3; ******************************************************* * Second Step: we define the Number of 'Segments' of each Column ******************************************************* n_segs_c('1')=2; n_segs_c('2')=1; n_segs_c('3')=2; n_segs_c('4')=2; n_segs_c('5')=2; n_segs_c('6')=3; n_segs_c('7')=3; n_segs_c('8')=2; n_segs_c('9')=2; n_segs_c('10')=2; n_segs_c('11')=1; n_segs_c('12')=2;

J. Barahona da Fonseca ******************************************************** * Third Step: we Define each Segment of Each Line and Its Sum: ******************************************************** ord_min_l('1','1')=1; ord_max_l('1','1')=3; sum_l('1','1')=7; ord_min_l('1','2')=5; ord_max_l('1','2')=7; sum_l('1','2')=16; ord_min_l('1','3')=9; ord_max_l('1','3')=12; sum_l('1','3')=28; ord_min_l('2','1')=1; ord_max_l('2','1')=3; sum_l('2','1')=24; ord_min_l('2','2')=5; ord_max_l('2','2')=12; sum_l('2','2')=40; ord_min_l('3','1')=2; ord_max_l('3','1')=4; sum_l('3','1')=23; ord_min_l('3','2')=8; ord_max_l('3','2')=12; sum_l('3','2')=26; ord_min_l('4','1')=1; ord_max_l('4','1')=5; sum_l('4','1')=34; ord_min_l('4','2')=7; ord_max_l('4','2')=9; sum_l('4','2')=16; ord_min_l('4','3')=11; ord_max_l('4','3')=12; sum_l('4','3')=17; ord_min_l('5','1')=1; ord_max_l('5','1')=3; sum_l('5','1')=23;

Solving Nonlinear Problems with MILP Models ord_min_l('5','2')=5; ord_max_l('5','2')=8; sum_l('5','2')=14; ord_min_l('5','3')=10; ord_max_l('5','3')=12; sum_l('5','3')=21; ord_min_l('6','1')=1; ord_max_l('6','1')=2; sum_l('6','1')=16; ord_min_l('6','2')=4; ord_max_l('6','2')=6; sum_l('6','2')=17; ord_min_l('6','3')=8; ord_max_l('6','3')=12; sum_l('6','3')=23; ord_min_l('7','1')=1; ord_max_l('7','1')=5; sum_l('7','1')=26; ord_min_l('7','2')=9; ord_max_l('7','2')=11; sum_l('7','2')=24; ord_min_l('8','1')=1; ord_max_l('8','1')=8; sum_l('8','1')=42; ord_min_l('8','2')=10; ord_max_l('8','2')=12; sum_l('8','2')=22; ord_min_l('9','1')=1; ord_max_l('9','1')=4; sum_l('9','1')=19; ord_min_l('9','2')=6; ord_max_l('9','2')=8; sum_l('9','2')=19; ord_min_l('9','3')=10;

J. Barahona da Fonseca ord_max_l('9','3')=12; sum_l('9','3')=23; ******************************************************** * Fourth Step: we Define each Segment of Each Column and Its Sum: ******************************************************** ord_min_c('1','1')=1; ord_max_c('1','1')=2; sum_c('1','1')=13; ord_min_c('1','2')=4; ord_max_c('1','2')=9; sum_c('1','2')=31; ord_min_c('2','1')=1; ord_max_c('2','1')=9; sum_c('2','1')=45; ord_min_c('3','1')=1; ord_max_c('3','1')=5; sum_c('3','1')=31; ord_min_c('3','2')=7; ord_max_c('3','2')=9; sum_c('3','2')=23; ord_min_c('4','1')=3; ord_max_c('4','1')=4; sum_c('4','1')=16; ord_min_c('4','2')=6; ord_max_c('4','2')=9; sum_c('4','2')=30; ord_min_c('5','1')=1; ord_max_c('5','1')=2; sum_c('5','1')=3; ord_min_c('5','2')=4; ord_max_c('5','2')=8; sum_c('5','2')=23; ord_min_c('6','1')=1; ord_max_c('6','1')=2; sum_c('6','1')=12; ord_min_c('6','2')=5;

Solving Nonlinear Problems with MILP Models ord_max_c('6','2')=6; sum_c('6','2')=4; ord_min_c('6','3')=8; ord_max_c('6','3')=9; sum_c('6','3')=17; ord_min_c('7','1')=1; ord_max_c('7','1')=2; sum_c('7','1')=16; ord_min_c('7','2')=4; ord_max_c('7','2')=5; sum_c('7','2')=16; ord_min_c('7','3')=8; ord_max_c('7','3')=9; sum_c('7','3')=16; ord_min_c('8','1')=2; ord_max_c('8','1')=6; sum_c('8','1')=18; ord_min_c('8','2')=8; ord_max_c('8','2')=9; sum_c('8','2')=4; ord_min_c('9','1')=1; ord_max_c('9','1')=4; sum_c('9','1')=29; ord_min_c('9','2')=6; ord_max_c('9','2')=7; sum_c('9','2')=16; ord_min_c('10','1')=1; ord_max_c('10','1')=3; sum_c('10','1')=23; ord_min_c('10','2')=5; ord_max_c('10','2')=9; sum_c('10','2')=34; ord_min_c('11','1')=1; ord_max_c('11','1')=9; sum_c('11','1')=45;

J. Barahona da Fonseca ord_min_c('12','1')=1; ord_max_c('12','1')=6; sum_c('12','1')=39; ord_min_c('12','2')=8; ord_max_c('12','2')=9; sum_c('12','2')=16; * Now the Matrix of the Kakuro puzzle is defined... * ... But it Remains to 'Define the 'empty positions’ with a Set of Constraints * undefined_value_li_cj(v1) ).. a_bin('li','cj',v1)=e=0; ****************************** variable obj; equations at_most_one_value(i,j) all_different_line(i,v,seg) all_different_column(j,v,seg) constraint_sum_l(i,seg) constraint_sum_c(j,seg) calc_obj ***************************************************************** * The Following Constraints Define the Empty Positions: ***************************************************************** undefined_value_l1_c1(v) undefined_value_l1_c3(v) undefined_value_l1_c2(v) undefined_value_l2_c1(v) undefined_value_l3_c1(v) undefined_value_l4_c1(v) undefined_value_l4_c2(v) undefined_value_l5_c1(v) undefined_value_l5_c2(v) undefined_value_l51_c2(v) undefined_value_l6_c2(v) undefined_value_l7_c1(v) undefined_value_l7_c2(v) undefined_value_l8_c1(v) undefined_value_l8_c2(v) undefined_value_l81_c2(v) undefined_value_l8_c3(v) undefined_value_l9_c1(v) undefined_value_l9_c2(v) undefined_value_l9_c3(v)

Solving Nonlinear Problems with MILP Models ***************************************************************** calc_obj ; * Each position of the Matrix have 1 or 0 values (in the case of 'Empty' Position): at_most_one_value(i,j).. sum(v, a_bin(i,j,v))=l=1; *Each Line Segment (i,seg) cannot have repetitions: all_different_line(i,v,seg)$(ord(seg) le n_segs_l(i)).. sum(j$( (ord(j) ge ord_min_l(i,seg)) and (ord(j) le ord_max_l(i,seg)) ), a_bin(i,j,v))=l=1; *Each Line l, Segment seg, Must has a Sum Equal to sum_l(i,seg): constraint_sum_l(i,seg)$(ord(seg) le n_segs_l(i)).. sum(( v,j)$( (ord(j) ge ord_min_l(i,seg)) and (ord(j) le ord_max_l(i,seg)) ) , ord(v)*a_bin(i,j,v))=e=sum_l(i,seg); undefined_value_l1_c1(v1).. a_bin('1','4',v)=e=0; undefined_value_l1_c3(v1).. a_bin('1','8',v)=e=0; undefined_value_l1_c2(v1).. a_bin('2','4',v)=e=0; undefined_value_l2_c1(v1).. a_bin('3','1',v)=e=0; undefined_value_l3_c1(v1).. a_bin('3','5',v)=e=0; undefined_value_l4_c1(v1).. a_bin('3','6',v)=e=0; undefined_value_l4_c2(v1).. a_bin('3','7',v)=e=0; undefined_value_l5_c1(v1).. a_bin('4','6',v)=e=0; undefined_value_l5_c2(v1).. a_bin('4','10',v)=e=0; undefined_value_l51_c2(v1).. a_bin('5','4',v)=e=0; undefined_value_l6_c2(v1).. a_bin('5','9',v)=e=0; undefined_value_l7_c1(v1).. a_bin('6','3',v)=e=0; undefined_value_l7_c2(v1).. a_bin('6','7',v)=e=0; undefined_value_l8_c1(v1).. a_bin('7','6',v)=e=0; undefined_value_l8_c2(v1).. a_bin('7','7',v)=e=0; undefined_value_l81_c2(v1).. a_bin('7','8',v)=e=0; undefined_value_l8_c3(v1).. a_bin('7','12',v)=e=0; undefined_value_l9_c1(v1).. a_bin('8','9',v)=e=0; undefined_value_l9_c2(v1).. a_bin('9','5',v)=e=0; undefined_value_l9_c3(v1).. a_bin('9','9',v)=e=0; *Each Column Segment (l,seg) cannot have repetitions: all_different_column(j,v,seg)$(ord(seg) le n_segs_c(j)).. sum(i$ ( (ord(i) ge ord_min_c(j,seg)) and (ord(i) le ord_max_c(j,seg)) ) , a_bin(i,j,v))=l=1; *Each Column c, Segment seg, Must has a Sum Equal to sum_c(c,seg): constraint_sum_c(j,seg)$( ord(seg) le n_segs_c(j) ).. sum(( v,i)$( (ord(i) ge ord_min_c(j,seg)) and (ord(i) le ord_max_c(j,seg)) ) , ord(v)*a_bin(i,j,v))=e=sum_c(j,seg); * To prevent a 'Trivial Solution' we must minimize the Sum of All Matrix Elements * where the 'Empty Positions' are forced to be 0: calc_obj.. obj=e=sum((i,j,v), a_bin(i,j,v));

J. Barahona da Fonseca model Kakuro /all/; option IterLim=1000000000; option ResLim=1000000000; option optcr=0; option optca=0; solve Kakuro using MIP maximizing obj; display a_bin.l, obj.l;

Appendix VIII. MILP Model to Obtain the Maximum, Minimum and to Sort By Ascendent?? and Decrescent?? Order of a Set of Integers sets i /1*25/ v /1*8/; alias(v1,v); alias(j,i); Variable a(i), b(i), c(i), maximo, minimo, obj, n_maxs, n_mins, n_values(v); Binary Variable a_c_bin(i,j,v), ordem_max(i), ordem_min(i), a_bin_max(i,v,v1), a_bin_min(i,v,v1), a_b_bin(i,j,v) ; Parameter ap(i), order; order=0; **ascendent order --> order=1, order=0--> descendent order ap(i)=round(uniform(1,card(v)));

Equations calc_n_values(v)

a_bin(i,v),

b_bin(i,v),

c_bin(i,v),

max_bin(v),

min_bin(v),

Solving Nonlinear Problems with MILP Models calc_a(i) calc_maximo(i) constr_a_bin(i) calc_a_bin(i) constr_max_bin calc_max_bin calc_a_bin_max(i,v,v1) constr_a_bin_max(i) calc_ordem calc_minimo(i) calc_obj ******CONSTRAINTS FOR OBTAINING ordem_min************** constr_min_bin calc_min_bin calc_a_bin_min(i,v,v1) constr_a_bin_min(i) calc_ordem_min(i) *********************************************************************************** set_order_b(i,j) constr_b_bin(i) calc_b_bin(i) constr1_a_b_bin(i) constr2_a_b_bin(i) calc_n_maxs calc_n_mins calc_b(j) constr_c_bin(i) calc_c_bin(i)

J. Barahona da Fonseca set_order_c(i,j) calc_c(j) constr1_a_c_bin(i) constr2_a_c_bin(i) ; *** Generate Statistic for each value calc_n_values(v).. n_values(v)=e=sum(i, a_bin(i,v)); calc_a(i).. a(i)=e=ap(i); constr_a_bin(i).. sum(v, a_bin(i,v))=e=1; calc_a_bin(i).. sum(v, ord(v)*a_bin(i,v))=e=a(i); constr_c_bin(i).. sum(v, c_bin(i,v))=e=1; calc_c_bin(i).. sum(v, ord(v)*c_bin(i,v))=e=c(i); ******CONSTRAINTS FOR OBTAINING ordem_max************** * The following 2 Constraints Translate maximo -> index v of binary variable max_bin(v) constr_max_bin.. sum(v, max_bin(v))=e=1; calc_max_bin.. sum(v, ord(v)*max_bin(v))=e=maximo; calc_a_bin_max(i,v,v1).. 2*a_bin_max(i,v,v1)=l=a_bin(i,v)+max_bin(v1); constr_a_bin_max(i).. sum((v,v1),a_bin_max(i,v,v1))=e=1; calc_n_maxs.. sum(i, ordem_max(i))=e=n_maxs; calc_ordem(i).. ordem_max(i)=e=sum((v,v1),(ord(v)=ord(v1))*a_bin_max(i,v,v1)); *(ord(v)=ord(v1)) ********************************************************************************** ******CONSTRAINTS FOR OBTAINING ordem_min************** constr_min_bin.. sum(v, min_bin(v))=e=1; calc_min_bin.. sum(v, ord(v)*min_bin(v))=e=minimo; calc_a_bin_min(i,v,v1).. 2*a_bin_min(i,v,v1)=l=a_bin(i,v)+min_bin(v1); constr_a_bin_min(i).. sum((v,v1),a_bin_min(i,v,v1))=e=1; calc_n_mins.. sum(i, ordem_min(i))=e=n_mins; calc_ordem_min(i).. ordem_min(i)=e=sum((v,v1),(ord(v)=ord(v1))*a_bin_min(i,v,v1));

Solving Nonlinear Problems with MILP Models **(ord(v)=ord(v1)) *********************************************************************************** ********CONSTRAINTS FOR Ordering b(i)************************************** ** Impose a Ascendent Order * if order j > i then b(j) must be greater than b(i) set_order_b(i,j)$(ord(j) > ord(i)).. b(j)*order+b(i)*(1-order)=g=b(i)*order+b(j)*(1-order); set_order_c(i,j)$(ord(j) > ord(i)).. c(j)*(1-order)+c(i)*order=g=c(i)*(1-order)+c(j)*order; **These two constraints Translate b(i)->v of b_bin(i,v) * Although b_bin will not be used by other constraint, this * ensures that b(j) as the same number of nonzero values as a(i) constr_b_bin(i).. sum(v, b_bin(i,v))=e=1; calc_b_bin(i).. sum(v, ord(v)*b_bin(i,v))=e=b(i); * These two constraints may be interpreted as the Predicate: * b(i) must has all its card(i) values different from 0 ******************************************************* ** These Constraints Set the Auxiliary 'Interface' Binary Var a_b_bin constr1_a_b_bin(i).. sum( (v,j), a_b_bin(i,j,v))=e=1; constr2_a_b_bin(i).. sum((v,j), ord(v)*a_b_bin(i,j,v))=e=a(i); constr1_a_c_bin(i).. sum( (v,j), a_c_bin(i,j,v))=e=1; constr2_a_c_bin(i).. sum((v,j), ord(v)*a_c_bin(i,j,v))=e=a(i); ** This Constraint Set the array b(j) that will be by Ascendent Order * For order i there is one and only one j and one v such that a_b_bin(i,j,v) !=0 calc_b(j).. b(j)=e=sum( (i,v), ord(v)*a_b_bin(i,j,v) ); calc_c(j).. c(j)=e=sum( (i,v), ord(v)*a_c_bin(i,j,v) ); ******************************************************************* calc_maximo(i).. maximo=g=a(i); calc_minimo(i).. minimo=l=a(i); calc_obj.. obj=e=maximo-minimo; option IterLim=1000000000; option ResLim=1000000000; Model MaxMin /all/;

J. Barahona da Fonseca Solve MaxMin using MIP minimizing obj; display a.l, maximo.l, ordem_max.l, minimo.l, ordem_min.l, n_maxs.l, n_mins.l, b.l, c.l, n_values.l;

Appendix IX. Output of the Run of the MILP Model to… ---- 169 VARIABLE a.L 1 2.000, 2 7.000, 3 5.000, 4 3.000, 5 3.000, 6 3.000 7 3.000, 8 7.000, 9 1.000, 10 5.000, 11 8.000, 12 5.000 13 8.000, 14 6.000, 15 2.000, 16 5.000, 17 2.000, 18 3.000 19 6.000, 20 4.000, 21 4.000, 22 3.000, 23 2.000, 24 2.000 25 5.000 ---- 169 VARIABLE maximo.L

=

8.000

=

1.000

=

2.000 1.000

---- 169 VARIABLE ordem_max.L 11 1.000,

13 1.000

---- 169 VARIABLE minimo.L ---- 169 VARIABLE ordem_min.L 9 1.000 ---- 169 VARIABLE n_maxs.L VARIABLE n_mins.L

=

---- 169 VARIABLE b.L 1 8.000, 2 8.000, 3 7.000, 4 7.000, 5 6.000, 6 6.000 7 5.000, 8 5.000, 9 5.000, 10 5.000, 11 5.000, 12 4.000 13 4.000, 14 3.000, 15 3.000, 16 3.000, 17 3.000, 18 3.000

Solving Nonlinear Problems with MILP Models

19 3.000, 25 1.000

20 2.000,

21 2.000,

22 2.000,

23 2.000,

24 2.000

---- 169 VARIABLE c.L 1 1.000, 2 2.000, 3 2.000, 4 2.000, 5 2.000, 6 2.000 7 3.000, 8 3.000, 9 3.000, 10 3.000, 11 3.000, 12 3.000 13 4.000, 14 4.000, 15 5.000, 16 5.000, 17 5.000, 18 5.000 19 5.000, 20 6.000, 21 6.000, 22 7.000, 23 7.000, 24 8.000 25 8.000 ---- 169 VARIABLE n_values.L 1 1.000, 8 2.000

2 5.000,

3 6.000,

4 2.000,

5 5.000,

6 2.000,

7 2.000

Suggest Documents