# Linear Programming using Maximize

I am trying to solve a more complicated version of the following LP problem: I have some constraint amounts a,b,c and two variables made of those amounts.

I would like to find the best way to allocate my constraint amounts so that the function 2 x + 3 y is at a maximum. Mathematica keeps telling me that x and y are not variables. Any idea what I'm doing wrong?

x = 2 a + 4 b + c;
y = a + 0 b + 3 c;
Maximize[
{2 x + 3 y, 0 < a < 40 && 0 < b < 20 && 0 < c < 10},
{x, y}
]


You used Set to define values for x and y, so they can't be used as variables. You probably want to do something like:

x = 2 a + 4 b + c;
y = a + 0 b + 3 c;
{max, rules} = Maximize[{2 x + 3 y, 0<a<40 && 0<b<20 && 0<c<10}, {a, b, c}]


Maximize::wksol: Warning: there is no maximum in the region in which the objective function is defined and the constraints are satisfied; a result on the boundary will be returned.

{550, {a -> 40, b -> 20, c -> 10}}

Then:

{x, y} /. rules


{170, 70}

will give you the values of x and y at the maximum.

• I don't know if you wanted this, but if your solution is restricted to integer values for {a, b, c}, you get a value (without the warning) if you do this: Maximize[{2*(2 a + 4 b + c) + 3*(a + 3 c), 0 < a < 40 && 0 < b < 20 && 0 < c < 10}, {a, b, c}, Integers] This (obviously) restricts the {a, b, c} to the domain Integers and gives this for the answer: {524, {a -> 39, b -> 19, c -> 9}}. Then follow what Carl wrote to get the values for x and y. – Mark R Jun 17 '19 at 6:48
• One other suggestion: if you have lots of constraints, you may wish to construct them as a list (lpConstraints, for example) and use Maximize[{objectiveFunction, And[Flatten[lpConstraints]]}, lpChoices, Integers]. So, I'm obviously suggesting that you name another variable as the "objectiveFunction". – Mark R Jun 17 '19 at 6:54