The Fundamental Theorem of Linear Programming

slope[pt1_,pt2_]:=If[First[pt1]≠First[pt2], (Last[pt2]-Last[pt1])/(First[pt2]-First[pt1]), Null];

intersect[pt1_,pt2_,a_,b_,c_]:={pt1,pt2,If[b==0,-((-c+a First[pt2])/(a (First[pt1]-First[pt2]))),If[slope[pt1,pt2]==-a/b,"Lines are parallel",((-c+a *First[pt2]+b *Last[pt2])/(-a *First[pt1]+a *First[pt2]-b*Last[pt1]+b*Last[pt2]))]]};

intpts[list_,a_,b_,c_]:=Select[MapThread[intersect[#1,#2,a,b,c]&,{list, RotateLeft[list]}], Last[#]≥0&&Last[#]<1&];

Manipulate[SeedRandom[sr];
corners=Table[{10*Cos[2*Pi*k], 10*Sin[2*Pi*k]}, {k,Sort[Table[Random[],{n}]]}];
max=Max[(a*First[#]+b*Last[#])&/@corners];
min=Min[(a*First[#]+b*Last[#])&/@corners];
Column[{Text@Grid[{{Row[{"Maximum value: ",max}]},{Row[{"Minimum value: ", min}]},{If[a≠0||b≠0,Style[Row[{a,"x + ",b,"y"," = ",c}],12,Red],"No objective function"]}}],Show[If[b≠0,Plot[(-a/b)*x+(c/b),{x,-10,10}, AxesOrigin->{0,0},AspectRatio->1],If[a≠0,Graphics[{Blue, Line[{{c/a,-14},{c/a,12}}]}, PlotRange->{{-11,11},{-10,14}},Axes->True],Graphics[Text["",{0,0}]]]],Graphics[{{{PointSize[.02], Point[#]}&/@corners},{LightGray, Opacity[.5],EdgeForm[Black], Polygon[corners]}, If[(a≠0||b≠0)&&Length[intpts[corners,a,b,c]]>0,
     {Red, Thick, Line[intpts[corners,a,b,c][[All,3]]*intpts[corners,a,b,c][[All,1]]+  
                                       (1-intpts[corners,a,b,c][[All,3]])*intpts[corners,a,b,c][[All,2]]
                                         ]
    }, Text["", {0,0}]
    ]}],PlotRange->{{-11,11},{-10,14}},ImageSize->{350,450}]},Center],{{n,8,"number of corners"},3,12,1, ImageSize->Small},{{a,9,"coefficient a"},-20,20, ImageSize->Small},{{b,3,"coefficient b"},-20,20, ImageSize->Small},{{c,5,"value of obj. func."},min-10,max+10, ImageSize->Small},{{sr,123,"random seed"},1,12345,1, ImageSize->Small}, ControlPlacement->Left, SaveDefinitions->True,TrackedSymbols:>{n,a,b,c,sr},AutorunSequencing->{1,2,3,5}]

A weak version of what is sometimes called the fundamental theorem of linear programming states that the extremal values of a linear function f(x,y)=a x+b y over a convex polygonal region are attained at corners of the region. (Moreover, if an extremum is attained at two corners then it is attained everywhere on the line segment connecting them.) The function f is called the objective function, and often represents profit or some other quantity to be optimized. The region is called the feasible set and represents all options for the variables x and y that satisfy the constraints in the problem. Points on the red line in the Demonstration are those points in the feasible set that produce the given value of the objective function.

Contributed by: Chris Boucher