Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to combine 2 independent linear optimization models while individually they are working combined they are not. My guess on the mistake is that the last part where I solve for the variables is not right (also suggested by Mathematica, but I don't understand what I'm doing wrong). What do I have to change in my combined model?

The latter part is:

{Flatten[Table[Subscript[X, t, z], {t, 1, 2}, {z, 1, 2}]], 
     Flatten[Table[Subscript[W, c, i], {c, 1, 2}, {i, 1, 2}]]}]

Here is the individual first model:

{Subscript[e, 1], Subscript[e, 2]} = {1, 2}; 
{Subscript[H, 1], Subscript[H, 2]} = {2, 1}; 
NMinimize[{Sum[
   Subscript[e, t]*Subscript[H, z]*Subscript[X, t, z], {t, 1, 2}, {z, 
    1, 2}], 
     And @@ 
    Flatten[Table[
      Element[Subscript[X, t, z], Integers], {t, 1, 2}, {z, 1, 2}]] && 
       And @@ 
    Flatten[Table[
      0 <= Subscript[X, t, z] <= 1, {z, 1, 2}, {t, 1, 2}]] && 
   And @@ Table[Sum[Subscript[X, t, z], {z, 1, 2}] == 1, {t, 1, 2}] && 
       And @@ 
    Table[Sum[Subscript[X, t, z], {t, 1, 2}] == 1, {z, 1, 2}]}, 
 Flatten[Table[Subscript[X, t, z], {t, 1, 2}, {z, 1, 2}]]]

Here is the individual second model:

{Subscript[d, 1], Subscript[d, 2]} = {1, 2}; 
{Subscript[A, 1], Subscript[A, 2]} = {2, 1}; 
NMinimize[{Sum[Subscript[d, c]*Subscript[A, i]*Subscript[W, c, i], 
       {c, 1, 2}, {i, 1, 2}], 
     And @@ 
    Flatten[Table[Element[Subscript[W, c, i], Integers], {c, 1, 2}, 
             {i, 1, 2}]] && 
   And @@ Flatten[Table[0 <= Subscript[W, c, i] <= 1, 
             {c, 1, 2}, {i, 1, 2}]] && 
       And @@ 
    Table[Sum[Subscript[W, c, i], {c, 1, 2}] == 1, {i, 1, 2}] && 
       And @@ 
    Table[Sum[Subscript[W, c, i], {i, 1, 2}] == 1, {c, 1, 2}]}, 
   Flatten[Table[Subscript[W, c, i], {c, 1, 2}, {i, 1, 2}]]]

Here is the combined model:

{Subscript[e, 1], Subscript[e, 2]} = {1, 2}; 
{Subscript[H, 1], Subscript[H, 2]} = {2, 1}; 
{Subscript[d, 1], Subscript[d, 2]} = {1, 2}; 
{Subscript[A, 1], Subscript[A, 2]} = {2, 1}; 
NMinimize[{Sum[Subscript[e, t]*Subscript[H, z]*Subscript[X, t, z], 
         {t, 1, 2}, {z, 1, 2}] + Sum[Subscript[d, c]*Subscript[A, i]*
           Subscript[W, c, i], {c, 1, 2}, {i, 1, 2}], 
     And @@ 
    Flatten[Table[Element[Subscript[X, t, z], Integers], {t, 1, 2}, 
             {z, 1, 2}]] && 
   And @@ Flatten[Table[0 <= Subscript[X, t, z] <= 1, 
             {z, 1, 2}, {t, 1, 2}]] && 
       And @@ Flatten[Table[Element[Subscript[W, c, i], Integers], 
             {c, 1, 2}, {i, 1, 2}]] && 
       And @@ Flatten[Table[0 <= Subscript[W, c, i] <= 1, {c, 1, 2}, 
             {i, 1, 2}]] && 
   And @@ Table[Sum[Subscript[X, t, z], {z, 1, 2}] == 
             1, {t, 1, 2}] && 
       And @@ 
    Table[Sum[Subscript[X, t, z], {t, 1, 2}] == 1, {z, 1, 2}] && 
       And @@ 
    Table[Sum[Subscript[W, c, i], {c, 1, 2}] == 1, {i, 1, 2}] && 
       And @@ 
    Table[Sum[Subscript[W, c, i], {i, 1, 2}] == 1, {c, 1, 2}]}, 
   {Flatten[Table[Subscript[X, t, z], {t, 1, 2}, {z, 1, 2}]], 
     Flatten[Table[Subscript[W, c, i], {c, 1, 2}, {i, 1, 2}]]}]
share|improve this question
1  
@gert Welcome to the Mathematica Stackexchange! Please format your code using four indents or back-ticks (``), so that people will find it easier to read your code. For more info check out the FAQ: mathematica.stackexchange.com/faq –  Ajasja Dec 11 '12 at 15:54
add comment

closed as too localized by Ajasja, tkott, Mr.Wizard Dec 11 '12 at 21:07

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

The error message is very clear:

NMinimize::lvar: "Variables {{Subscript[X, 1,1],Subscript[X, 1,2],Subscript[X, 2,1],Subscript[X, 2,2]},{Subscript[W, 1,1],Subscript[W, 1,2],Subscript[W, 2,1],Subscript[W, 2,2]}} should be a list of variables, with each element being a variable, or a list containing a variable and lower and upper bounds for the starting region for that variable. "

Mathematica graphics

Obviously all your variables are not in one list but two. This is easily solved by using an extra flatten.

Here is the full code:

{Subscript[e, 1], Subscript[e, 2]} = {1, 2}; {Subscript[H, 1], 
  Subscript[H, 2]} = {2, 1}; {Subscript[d, 1], Subscript[d, 2]} = {1, 
  2}; {Subscript[A, 1], Subscript[A, 2]} = {2, 
  1}; NMinimize[{Sum[
    Subscript[e, t]*Subscript[H, z]*Subscript[X, t, z], {t, 1, 2}, {z,
      1, 2}] + 
   Sum[Subscript[d, c] Subscript[A, i] Subscript[W, c, i], {c, 1, 
     2}, {i, 1, 2}], 
  And @@ Flatten[
     Table[Element[Subscript[X, t, z], Integers], {t, 1, 2}, {z, 1, 
       2}]] && And @@ 
    Flatten[Table[
      0 <= Subscript[X, t, z] <= 1, {z, 1, 2}, {t, 1, 2}]] && 
   And @@ Flatten[
     Table[Element[Subscript[W, c, i], Integers], {c, 1, 2}, {i, 1, 
       2}]] && And @@ 
    Flatten[Table[
      0 <= Subscript[W, c, i] <= 1, {c, 1, 2}, {i, 1, 2}]] && 
   And @@ Table[Sum[Subscript[X, t, z], {z, 1, 2}] == 1, {t, 1, 2}] &&
    And @@ Table[Sum[Subscript[X, t, z], {t, 1, 2}] == 1, {z, 1, 2}] &&
    And @@ Table[Sum[Subscript[W, c, i], {c, 1, 2}] == 1, {i, 1, 2}] &&
    And @@ Table[Sum[Subscript[W, c, i], {i, 1, 2}] == 1, {c, 1, 2}]},
  Flatten@{Flatten[Table[Subscript[X, t, z], {t, 1, 2}, {z, 1, 2}]], 
   Flatten[Table[Subscript[W, c, i], {c, 1, 2}, {i, 1, 2}]]}]
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.