2
$\begingroup$

I want to solve a large LP problem using NMaximize. This is a small instance of my bigger problem:

$$max \sum^{4}_{t=1}(\sum^{4}_{c=1} V_{c}*f_{c,t})\\ s.t\quad \sum^{5}_{t=1} f_{c,t} \leq 100,\forall t$$

where $ f_{c,t} \geq 0$ and $c=1...5$ and $t=1...5$

I wrote the following:

NMaximize[{Sum[f[c, t]*V, {c, 1, 4}, {t, 1, 4}], 
  Sum[f[c, t], {c, 1, 5}] <= 100}, {f[c, t]}]

How can I declare f[c,t] as a variable?

$\endgroup$
1
$\begingroup$
SeedRandom[42];
n = 3;
Array[(v@# = RandomInteger[100]) &, n]
NMaximize[{
           Sum[f[c, t]*v[c], {c, n}, {t, n}],
           And[And @@ Thread[Flatten@Array[f, {n, n}] >= 0], 
               And @@ Table[Sum[f[c, t], {c, n}] <= 100, {t, n}]]}, 
           Flatten@Array[f, {n, n}]]
(*
{54, 66, 4}

{19800., {f[1, 1] -> 0.,   f[1, 2] -> 0.,   f[1, 3] -> 0., 
          f[2, 1] -> 100., f[2, 2] -> 100., f[2, 3] -> 100., 
          f[3, 1] -> 0.,   f[3, 2] -> 0.,   f[3, 3] -> 0.}}
*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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