3
$\begingroup$

Consider

$1\rightarrow{}x+2y\leq{500}$,

$2\rightarrow{}2x +y \leq{520}$,

$3\rightarrow{}2x+5y \leq{}1200$,

$4\rightarrow{}x \geq{}0$,

$5\rightarrow{}y \geq{}0$,

The above is the set of inequalities that generates a linear programming problem whose objective function is

$f(x,y)=9x +12y$,

I need to maximize the objective function, but the graphics are unclear when it comes to the points,

a) The problem to be solved is to produce a plot with enough detail with the coordinates of the solution polygon, drawn it by hand, with geogebra, symbolab and it is not possible to appreciate it well

b) I have never solved a linear programming problem with Mathematica and I would like to know how it can be done using mathematical syntax.

If you can help me, I would appreciate it very much

Notes

I do not know the symplex method.
This is not homework or a work assignment, it is for personal knowledge.

Update

error corrected

$\endgroup$
3
  • 1
    $\begingroup$ Look at the condition: x,y <=0 and you want to maximize the objective function . The largest value the objective function can have under this assumption is zero. Therefore, I think there is something wrong with your question. $\endgroup$ Nov 7, 2020 at 21:03
  • $\begingroup$ @Daniel Hube see correction above $\endgroup$
    – rpujadas
    Nov 7, 2020 at 21:27
  • $\begingroup$ There is a function LinearProgamming" in MMA for minimization. In your case for maximization you would define: c= -{..},m=-{{},{},..}; b=- {..}. Note the minus signs. $\endgroup$ Nov 8, 2020 at 8:38

1 Answer 1

4
$\begingroup$
obj = 9 x + 12 y;
c1 = x + 2 y <= 500;
c2 = 2 x + y <= 520;
c3 = 2 x + 5 y <= 1200;
c4 = x >= 0;
c5 = y >= 0;

{max, argmax} = {#[[1]], {x, y} /. #[[2]]} & @
   NMaximize[{obj, And[c1, c2, c3, c4, c5]}, {x, y}]
 {3540., {180., 160.}}
Show[RegionPlot[And[c1, c2, c3, c4, c5], {x, 0, 300}, {y, 0, 300}, 
  ColorFunction -> (ColorData[{"TemperatureMap", {0, max}}][9 # + 12 #2] &), 
  ColorFunctionScaling -> False], 
 Normal[ContourPlot[obj, {x, 0, 300}, {y, 0, 300}, 
    ContourShading -> None, Contours -> Subdivide[0, max, 5]]] /. 
  Tooltip[l : {__, Line[x_, ___]}, t_] :> {l, Text[t, Mean[x]]}, 
 ListPlot[{Callout[argmax, argmax]}, PlotStyle -> Directive[Red, PointSize[Large]]]]

enter image description here

A variation: You can add a legend showing objective function value over the feasible region and use custom arrowheads to place and orient the contour labels along the contour lines:

Show[RegionPlot[And[c1, c2, c3, c4, c5], {x, 0, 300}, {y, 0, 300}, 
  ColorFunction -> (ColorData[{"TemperatureMap", {0, max}}][ 
      9 # + 12 #2] &), ColorFunctionScaling -> False,
   PlotLegends ->
    BarLegend[{ColorData[{"TemperatureMap", {0, max}}], {0, max}}, 
     LegendMarkerSize -> 400, LegendFunction -> "Frame", 
     LegendLabel -> "obj"]], 
 Normal[ContourPlot[obj, {x, 0, 300}, {y, 0, 300}, 
    ContourShading -> None, Contours -> Subdivide[0, max, 5]]] /. 
  Tooltip[{dir__, Line[x_, ___]}, t_] :> {dir, 
    Arrowheads[{{Automatic, .3, 
       Graphics @ Text[Framed[Style[t, 16, Bold, Opacity[1], Black], 
          Background -> White, FrameStyle -> None]]}}], 
    Arrow[Line @ SortBy[First] @ x]}, 
 ListPlot[{Callout[argmax, Style[argmax, 16, Bold]]}, 
  PlotStyle -> Directive[Red, PointSize[Large]]], ImageSize -> 500]

enter image description here

$\endgroup$
5
  • $\begingroup$ kglr__wow your first code works very well the second and third send me these errors, the graph appears but not with the color variation) "Part::partd: Part specification nmax[[1]]] is longer than depth of object. ColorData::notent: {TemperatureMap,{0,nmax[[1]]}} is not a known entity, class, or tag for ColorData. Use ColorData[] for a list of entities. (use MMA 12.1).What does the value 3540 mean in the first result? $\endgroup$
    – rpujadas
    Nov 7, 2020 at 21:53
  • 1
    $\begingroup$ @rpujadas, sorry, nmax[[1]] should be max . Fixed now. $\endgroup$
    – kglr
    Nov 7, 2020 at 21:57
  • $\begingroup$ @kglr_What does the value 3540 mean in the first result? $\endgroup$
    – rpujadas
    Nov 7, 2020 at 22:19
  • 2
    $\begingroup$ @rpujadas - The 3540 is the maximum value of the objective function subject to the given constraints. The system can be solved exactly {max, argmax} = {#[[1]], {x, y} /. #[[2]]} &@Maximize[{obj, c1, c2, c3, c4, c5}, {x, y}] $\endgroup$
    – Bob Hanlon
    Nov 7, 2020 at 22:34
  • $\begingroup$ @Bob Hanlon ,I had not understood the result format of the code. Thank you $\endgroup$
    – rpujadas
    Nov 7, 2020 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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