Question: cardinal constraint in a {-1,0,1} Linear Program

Test.mw

If I use a binary {0,1} linear program the cardinal constraint ie control the
number of 1's (long positions in portfolio) works beautiful. See attached worksheet.

The problem starts when I convert the problem into a constrained integer {-1,0,1}
linear problem where -1= short position, 0=no position and 1=long position.

In the first example when we add the constraint  add(w[i], i = 1 .. NC) = 4
Then we get 4 long positions (+1) and the rest NULL positions (0).
Which is exactly what we want ie we are controlling the number of long positions.

In the second example when we add the constraint  add(w[i], i = 1 .. NC) = 4
Then we get only {-1,1} positions with a surplus of 4 {1} positions and
zero NULL positions. This is not the way I want it. We should get 4 long positions (+1)
and 4 short positions (-1) and the rest NULL positions (0)

The reason for this is because -1 cancel out +1 hence these positions does not count.
I have tried to solve this by using absolute values ie add(abs(w[i]), i = 1 .. NC) = 4
but then Maple complain that the constraint is not linear.

Extremely complicated stuff! What should I do to solve the problem?


# problem child = constrained integer {-1,0,1}
PS := 4:
W := Vector[column]([seq(w[i], i = 1 .. NC)]):

PP := Vector[column]([seq(evalf(100*(DD[NR, j]-DD[1, j])/DD[1, j], 5), j = 1 .. NC)]);
Draw := Vector[column]([seq(evalf(100*(max(Column(DD, j))-min(Column(DD, j)))/min(Column(DD, j)), 5), j = 1 .. NC)]);

data1 := LPSolve(add(PP[i]*w[i], i = 1 .. NC)-add(Draw[i]*w[i], i = 1 .. NC), {seq(w[i] >= -1, i = 1 .. NC), seq(w[i] <= 1, i = 1 .. NC), add(w[i], i = 1 .. NC) = PS}, maximize = true, assume = integer);

add(rhs(data1[2][i]), i = 1 .. 50);


data1 := [1247.44700, [w[1] = 1, w[2] = -1, w[3] = -1, w[4] = -1, w[5] = -1,

  w[6] = -1, w[7] = -1, w[8] = 1, w[9] = 1, w[10] = -1, w[11] = -1, w[12] = 1,

  w[13] = 1, w[14] = 1, w[15] = 1, w[16] = 1, w[17] = 1, w[18] = -1,

  w[19] = 1, w[20] = -1, w[21] = -1, w[22] = 1, w[23] = -1, w[24] = 1,

  w[25] = 1, w[26] = -1, w[27] = -1, w[28] = -1, w[29] = 1, w[30] = -1,

  w[31] = 1, w[32] = -1, w[33] = -1, w[34] = 1, w[35] = 1, w[36] = 1,

  w[37] = 1, w[38] = 1, w[39] = 1, w[40] = 1, w[41] = 1, w[42] = 1,

  w[43] = -1, w[44] = 1, w[45] = -1, w[46] = 1, w[47] = -1, w[48] = -1,

  w[49] = -1, w[50] = 1]]

                                      4


Please Wait...