Skip to content

Commit

Permalink
Added python script to generate diagram in the post.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Stucchio committed Jul 16, 2012
1 parent b490027 commit 8e7301b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
10 changes: 6 additions & 4 deletions cloud-planning.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ a scalar, and `y` is also a vector. `f(x)` is typically called the *objective fu
The constraint takes the form `M*x >= v`, where `M` is a matrix, and `v` is a vector.
Inequality is interpreted pointwise. A simple example:
find argmax 2*x+y
find argmax 1.5*x+y
subject to:
x >= 0
y >= 0
-1*x + -2*y >= 4
The constraints demand that the solution `(x, y)` lives in a bounded set, specifically
the triangle with vertices `(0,0)`, `(4,0)` and `(0,2)`. And the goal is to make `2*x+y`
the triangle with vertices `(0,0)`, `(4,0)` and `(0,2)`. And the goal is to make `1.5*x+y`
as big as possible.
So where can the solution lie?
Expand All @@ -66,8 +66,8 @@ The first fact to observe is that it can't lie on the *interior* of the triangle
be on one of the edges. To see why this is so, imagine the maximum were at some point`(x0, y0)`
inside the triangle but not on the edge. In that case, I can find a very small value `d`
for which `(x0+d, y0)` is still inside the triangle - specifically, I just pick `d` equal
to half the distance to the edge. If I compute `f(x0+d,y) = 2*x0+2*d+y0`, then this value
is bigger than `f(x0,y)=2*x0+y0`. This means that `(x0, y0)` was not really the maximum.
to half the distance to the edge. If I compute `f(x0+d,y) = 1.5*x0+1.5*d+y0`, then this value
is bigger than `f(x0,y)=1.5*x0+y0`. This means that `(x0, y0)` was not really the maximum.
I can make similar arguments about the edges. Suppose `p` is a point
on the edge of triangle (but not the corner), and suppose `d` is a
Expand All @@ -77,6 +77,8 @@ objective function by moving in the direction of `d`, otherwise I can
increase it by moving in the direction of `-d`. This means the maximum
can't be on the edge either, except in the special case that `f(d)=0`.
![diagram illustrating reasoning](polygon_diagram.png)
So since the maximum of the objective function doesn't live inside the
triangle or on the side, we find it must like at one of the corners of
the triangle.
Expand Down
34 changes: 34 additions & 0 deletions diagram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from pylab import *

xlim(-0.5, 5)
ylim(-0.5, 5)

fill([0,2,0], [0,0,4])

xlabel("x")
ylabel("y")


def obj(x, y):
return str(1.5*x+y)

plot([1], [1], 'ko')
annotate("(x0, y0), objective=" + obj(1,1), (0.95, 0.85))

plot([1.1], [1], 'ro')
annotate("(x0+d, y0), objective=" + obj(1.1, 1), (0.95, 1.1), color='r')



plot([0.75], [4-2*0.75], 'ko')
annotate("(x0, y0), objective=" + obj(0.75, 4-2*0.75), (0.80, 4-2*0.75-0.05))

arrow( 0.75, 4-2*0.75, -0.05, 0.1, ec='g', fc='g', lw=4, head_width=0.04)

plot([0.65], [4-2*0.65], 'ro')
annotate("(x0, y0) + d, objective=" + obj(0.65, 4-2*0.65), (0.7, 4-2*0.65-0.05), color='r')

plot([0.85], [4-2*0.85], 'yo')
annotate("(x0, y0) - d, objective=" + obj(0.85, 4-2*0.85), (0.9, 4-2*0.85-0.05), color='y')

savefig("polygon_diagram.png")

0 comments on commit 8e7301b

Please sign in to comment.