# TODO¶

• time-varying graphs
• table of results
• show combinatorics. why not NP hard. enumerate paths.
• remove $\mathcal{P}$. just describe a path with $x$ from the start?

# Allocating interdiction effort to catch a smuggler¶

We'll explore a game between a smuggler moving through a graph and a security team trying to catch him.

The smuggler moves along a directed graph (cycles are allowed) with $n$ nodes and $m$ edges from node $0$ (the source) to node $n-1$ (the destination). Edge $j$ has evasion probability $p_j$, which is the probability that the smuggler moves along that edge undetected. The detection events on the edges are independent, so the probability that the smuggler makes it to the destination undetected is $\prod_{j \in \mathcal{P}} p_j$, where $\mathcal{P} \subset \lbrace 0,\ldots, m-1\rbrace$ is the set of edges on the smuggler's path. We assume that the smuggler knows the evasion probabilities and will take a path that maximizes the probability of making it to the destination node undetected.

The security team is trying to catch the smuggler as he moves through the graph. They can control the evasion probabilities on the graph edges through, say, allocation of resources to the edges, assigning guards, etc. We'll keep this abstract for now, and just consider that security can modify the evasion probabilities subject to some constraints. Their goal is to choose evasion probabilities to minimize the chance that the smuggler gets through undetected.

# Transformations and mathematical formulation¶

We'll take the logarithm of all probabilities to make the problem amenable to convex optimization and simplify the remaining discussion. This transforms products of variables (generally not convex) into sums of variables (convex!).

Let $c_j = \log(p_j)$. As this is a one-to-one correspondence, we may refer to the 'edge detection probabilities' when we really mean to refer to their logarithm, $c$. This should be clear from context and hopefully won't cause any confusion.

Given $c$ and a path $\mathcal{P}$, let $P(c, \mathcal{P})$ be the logarithm of the evasion probability of that path. That is,

$$P(c, \mathcal{P}) = \log \left( \prod_{j \in \mathcal{P}} p_j \right) = \sum_{j \in \mathcal{P}} \log(p_j) = \sum_{j \in \mathcal{P}} c_j.$$

Define $P(c, \mathcal{P}) = -\infty$ if $\mathcal{P}$ is not a valid path from source to sink. We do this to implicity encode the constraint that the smuggler's path must be valid.

# Min/max game¶

We can now interpret this problem as a min/max game. The smuggler is trying to maximize his chance of getting through undetected by choosing $\mathcal{P}$, and security is trying to minimize the smuggler's chance by choosing $c$. That is, the smuggler is trying to maximize $P(c, \mathcal{P})$ while the security team is trying to minimize it.

We end up with a game with two 'moves'. Security 'moves' first by choosing the edge evasion probabilities via $c$. The smuggler 'moves' second (after $c$ is fixed) by choosing his path $\mathcal{P}$.

For security to make an optimal choice of edge evasion probabilities, they'll need to model what the smuggler will do when faced with any set of evasion probabilities. Thus, we'll let $P^\mathrm{opt}(c)$ be the optimal value of the problem

$\begin{array}{ll} \mbox{maximize} & P(c, \mathcal{P}) \ \mbox{subject to} & \mathcal{P} \mbox{ is a valid path}. \end{array}$

$P^\mathrm{opt}(c)$ is the (logarithm of the) evasion probability of the smuggler if he picks an optimal path, knowing the values of $c$.

The security team's goal is then to minimize this quantity, subject to some constraints on $c$, which we'll denote by the set $\mathcal{C}$. Let $P^{\star}$ be the optimal value of

$\begin{array}{ll} \mbox{minimize} & P^{\mathrm{opt}}(c) \ \mbox{subject to} & c \in \mathcal{C}. \end{array}$

In the remainder, we'll first consider the smuggler's objective so as to obtain a convex formulation of $P^\mathrm{opt}(c)$, which we can then use to analyze and optimize the security team's objective.

# Helper functions¶

The next cell contains some helper functions for generating the examples and plotting the results. This is the majority of the code. The actual CVXPY optimization code is only a few lines. We'll go over the optimization code later in the notebook.

In [2]:
#%config InlineBackend.figure_format = 'pdf'
#%config InlineBackend.figure_format = 'svg'
%config InlineBackend.figure_format = 'retina'

import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
import cvxpy as cvx

from matplotlib import rcParams
rcParams.update({'font.size': 16})

def formGraph(N,mu,eta,buildings=None,seed=None,dummyNodes=False):
''' Form N by N grid of nodes, perturb by mu and connect nodes within eta.
mu and eta are relative to 1/(N-1)

buildings is a list of tuples (x,y,w,h)
'''
if seed is not None:
np.random.seed(seed)

mu = mu/(N-1)
eta = eta/(N-1)

# generate perterbed grid positions for the nodes
pos = [(i + mu*np.random.randn(), j + mu*np.random.randn())\
for i in np.linspace(0,1,N) for j in np.linspace(1,0,N)]

#select out nodes that end up inside buildings
if buildings is not None and buildings != []:
pos2 = []
for p in pos:
inside = False
for x,y,w,h in buildings:
if x <= p[0] and p[0] <= x + w and y <= p[1] and p[1] <= y + h:
inside = True
break
if not inside:
pos2.append(p)
pos = pos2

# add dummy nodes for multiple entry/exit example
if dummyNodes:
pos = [(-.5,.5)] + pos + [(1.5,.5)]

pos = dict(enumerate(pos))
n = len(pos)

# connect nodes with edges
G = nx.random_geometric_graph(n,eta,pos=pos)

# remove edges that cross buildings
if buildings is not None and buildings != []:
for e in G.edges():
blocked = False
for x,y,w,h in buildings:
if intersect(pos[e[0]],pos[e[1]],x,x+w,y,y+h):
blocked = True
if blocked:
G.remove_edge(*e)

G = nx.DiGraph(G)

# add edges connecting dummy nodes to nodes on left and right edges of grid
if dummyNodes:
for i in range(N):

return G, pos

def showPaths(G,pos,edgeProbs=1.0,path=None,visibleNodes=None,Gnodes=None,Rnodes=None,guards=None):
''' Takes directed graph G, node positions pos, and edge probabilities.
Optionally uses path (a list of edge indices) to plot the smuggler's path.

edgeProbd gives the probabilities for all the edges, including hidden ones.

path includes all the edges, including the hidden ones

Gnodes and Rnodes denote the source and destination nodes, to be plotted green
and red respectively.

guards is a list of node indices for denoting guards with a black dot on the plot
'''
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111, aspect='equal')

n = G.number_of_nodes()
if visibleNodes is None:
visibleNodes = G.nodes()

# draw the regular interior nodes in the graph
nx.draw_networkx_nodes(G,pos,nodelist=visibleNodes,node_color='w',node_size=100,ax=ax)

# draw the origin and destination nodes
for nodes, color in zip([Gnodes,Rnodes],['g','r']):
for color2, alpha in zip(['w',color],[1,.2]):
nx.draw_networkx_nodes(G,pos,
nodelist=nodes,
node_color=color2,
node_size=200,
ax=ax,alpha=alpha)

# draw guard nodes
if guards is not None:
nx.draw_networkx_nodes(G,pos,nodelist=guards,node_color='.0',node_size=100,ax=ax)

if path is None:
alpha = 1
else:
alpha = .15

# start messing with edges
edge2ind = {e:i for i,e in enumerate(G.edges())}
ind2edge = {i:e for i,e in enumerate(G.edges())}

# only display edges between non-dummy nodes
visibleEdges = [i for i in range(len(edge2ind)) if ind2edge[i][0] in visibleNodes and ind2edge[i][1] in visibleNodes]

edgelist = [ind2edge[i] for i in visibleEdges]

if isinstance(edgeProbs,float):
edgeProbs = [edgeProbs]*G.number_of_edges()

p = [edgeProbs[i] for i in visibleEdges]

# draw edges of graph, make transparent if we're drawing a path over them
edges = nx.draw_networkx_edges(G,pos,edge_color=p,width=4,
edge_cmap=plt.cm.RdYlGn,arrows=False,edgelist=edgelist,edge_vmin=0.0,
edge_vmax=1.0,ax=ax,alpha=alpha)

# draw the path, only between visible nodes
if path is not None:
visiblePath = [i for i in path if ind2edge[i][0] in visibleNodes and ind2edge[i][1] in visibleNodes]
path_pairs = [ind2edge[i] for i in visiblePath]
path_colors = [edgeProbs[i] for i in visiblePath]
edges = nx.draw_networkx_edges(G,pos,edge_color=path_colors,width=8,
edge_cmap=plt.cm.RdYlGn,edgelist=path_pairs,arrows=False,edge_vmin=0.0,
edge_vmax=1.0)

fig.colorbar(edges,label='Evasion probability')

ax.axis([-0.05,1.05,-0.05,1.05])
#ax.axis('tight')
#ax.axis('equal')
ax.axis('off')

return fig, ax

def optPath(G,p):
''' Find the optimal smuggler's path in directed graph G
with edge evasion probabilities p and dictionary
edge2ind bringing node pairs to edge indices
'''
edge2ind = {e:i for i,e in enumerate(G.edges())}
H = G.copy()
p = np.minimum(p,1)
w = -np.log(p)
n = H.number_of_nodes()

for i in H:
for j in H[i]:
H[i][j]['w'] = w[edge2ind[(i,j)]]

path = nx.shortest_path(H,0,n-1,'w')
#path = nx.astar_path(H,0,n-1,weight='w')

foo = [edge2ind[(i,j)] for i,j in zip(path[:-1],path[1:])]
x = np.zeros_like(p)
x[foo] = 1
return x

def intersect(p1,p2,xmin,xmax,ymin,ymax):
'''determine if a rectangle given by xy limits blocks the line of sight between p1 and p2'''

block = False

# if either point inside block
for p in [p1,p1]:
if xmin <= p[0] and p[0] <= xmax and ymin <= p[1] and p[1] <= ymax:
return True

# if the two points are equal at this stage, then they are outside the block
if p1[0] == p2[0] and p1[1] == p2[1]:
return False

if p2[0] != p1[0]:
for x in [xmin,xmax]:
alpha = (x-p1[0])/(p2[0] - p1[0])
y = p1[1] + alpha*(p2[1] - p1[1])

if 0 <= alpha and alpha <= 1 and ymin <= y and y <= ymax:
return True

if p2[1] != p1[1]:
for y in [ymin,ymax]:
alpha = (y-p1[1])/(p2[1] - p1[1])
x = p1[0] + alpha*(p2[0] - p1[0])

if 0 <= alpha and alpha <= 1 and xmin <= x and x <= xmax:
return True

return False

def getGuardEffects(G,pos,guardIdxs,buildings=None,dummyNodes=None):
''' for n guards and m edges, return an m by n matrix giving the
effect of each guard on the evasion probabilities of each edge
in the graph.

Ignore dummy nodes, if any. Guards cannot see through buildings.

guardIdxs is a list of the node indices where we would consider placing
guards

Return the log of the detection probabilities for each guard.

'''
return min(r+.1,1)

m = G.number_of_edges()
if buildings is None:
buildings = []
if dummyNodes is None:
dummyNodes = []

ind2edge = {i:e for i,e in enumerate(G.edges())}
edgeCenters = []
for e in G.edges():
edgeCenters.append((np.array(pos[e[0]]) + np.array(pos[e[1]]))/2)

edgeCenters = np.array(edgeCenters)
numGuards = len(guardIdxs)
edgeVals = np.zeros((m,numGuards))

for i,gid in enumerate(guardIdxs):
for j in range(m):
blocked = False
for x,y,w,h in buildings:
if intersect(edgeCenters[j,:],pos[gid],x,x+w,y,y+h):
blocked = True
break
e = ind2edge[j]
if e[0] in dummyNodes or e[1] in dummyNodes:
blocked = True
if not blocked:
return edgeVals

def get_a(G,seed=None):
'''
Generate a random value in [0,1] for each edge in the graph.
For directed graphs, directed edges between the same nodes should have the same value
'''
if seed is not None:
np.random.seed(seed)
m = G.number_of_edges()
a = np.zeros((m))
edge2ind = {e:i for i,e in enumerate(G.edges())}
for e in G.edges():
a[edge2ind[e]] = np.random.rand()
e2 = (e[1],e[0])
if e2 in edge2ind:
a[edge2ind[e2]] = a[edge2ind[e]]
return a


# Smuggler's objective¶

The smuggler chooses a path $\mathcal{P}$ only after $c$ is fixed. His goal is to find a valid path in the graph which maximizes $\sum_{j \in \mathcal{P}} c_j$. Note that this problem can be cast and solved exactly as a shortest path problem in graph theory. We formulate it here as a convex problem so that the security team can use the model to thwart the smuggler.

We'll denote possible paths with a Boolean decision variable $x \in \lbrace 0,1 \rbrace^m$, where $x_j = 1$ denotes $j \in \mathcal{P}$. Note that this allows us to write the smuggler's objective as

$$\sum_{j \in \mathcal{P}} c_j = \sum_{j=0}^{n-1} c_j x_j = c^T x.$$

The $x$ variable needs to satisfy certain constraints to represent a valid path. Firstly, the number of times the path exits the source node must be exactly one more than the number of times it enters the source node. Similarly, the number of times the path enters the destination node must be exactly one more than the number of times it exits destination node. For all other nodes in the graph, the number of times the path enters and exits the node must be equal. These constraints can be represented as $Ax = b$, where

$b_i = \begin{cases} -1 & i = 0 \ +1 & i = n-1\ 0 & \mbox{otherwise,} \end{cases}$

$A_{ij} = \begin{cases} +1 & \mbox{if edge j enters node i} \ -1 & \mbox{if edge j exits node i} \ 0 & \mbox{otherwise.} \end{cases}$

The smuggler's problem can then be written so that $P^\mathrm{opt}(c)$ is the optimum value of the problem

$\begin{array}{ll} \mbox{maximize} & c^T x \ \mbox{subject to} & A x = b\ & x \in \lbrace 0,1 \rbrace^m. \end{array}$

This is a linear problem with Boolean variables, which is not convex, but we can transform it into a convex problem by relaxing the Boolean constraints to get the linear program

$\begin{array}{ll} \mbox{maximize} & c^T x \ \mbox{subject to} & A x = b\ & x \geq 0, \end{array}$ with optimum value $P^{\mathrm{opt}}_\mathrm{R}(c)$.

It turns out that this relaxed convex problem solves the original Boolean problem exactly. That is, $P^{\mathrm{opt}}(c) = P^{\mathrm{opt}}_\mathrm{R}(c)$, so we will only write $P^{\mathrm{opt}}$ in the remainder. In addition, the solution to the LP, $x^\star$, will be Boolean when there is a unique optimal path. In the presence of multiple optimal paths, the $x^\star$ may have fractional entries, but an optimal path can still be recovered.

For the purposes of the security team, we'll only need that the Boolean and LP optimal values are equal. Before continuing on to security's perspective, we'll look at an example of the smuggler choosing an optimal path in a given graph.

# Smuggler's path example¶

We'll solve the smuggler's problem on an example network to see an example of an optimal path.

We'll create an $N \times N$ grid of points with small perturbations to make the graph irregular, connecting two nodes with two directed edges (one going each way) if the nodes are within distance $\eta$ of each other. Pairs of edges $(i,j)$ and $(j,i)$ will have identical evasion probabilities, i.e., the evasion probability is the same in either direction between two connected nodes. The evasion probability will be distributed like $p_j = e^{-a_j}$, where $a_j$ is a uniform random variable over the interval $\left[0,1\right]$.

The smuggler will find an optimal path from node $0$ in the upper-left corner of the graph plot to node $n-1$ in the lower-right corner of the graph plot.

We show the graph with the edge evasion probabilities below.

In [53]:
N = 10
G, pos = formGraph(N,.12,1.2,seed=5)
n = G.number_of_nodes()

a = get_a(G,seed=2)
p = np.exp(-a)

fig, ax = showPaths(G,pos,p,Gnodes=[0],Rnodes=[n-1])


We form the smuggler's relaxed convex problem and solve it to find his optimal path. We plot the path below.

In [54]:
A = nx.incidence_matrix(G,oriented=True).toarray()
n,m = A.shape

b = np.zeros(n)
b[0] = -1
b[n-1] = 1

c = np.log(p)

edge2ind = {e: i for i,e in enumerate(G.edges_iter())}

B = np.zeros((m/2,m))
count = 0
for i in G:
for j in G[i]:
if i < j:
B[count,edge2ind[(i,j)]] = 1
B[count,edge2ind[(j,i)]] = -1
count += 1

x = cvx.Variable(m)
constr = [A*x == b,x>=0, x <= 1]
cvx.Problem(cvx.Maximize(x.T*c),constr).solve(verbose=True)
x = np.array(x.value).flatten()

ECOS 1.0.4 - (c) A. Domahidi, Automatic Control Laboratory, ETH Zurich, 2012-2014.

It     pcost         dcost      gap     pres    dres     k/t     mu      step     IR
0   +8.219e+01   -4.479e+02   +5e+02   1e-02   3e-16   1e+00   8e-01    N/A     1 1 -
1   +5.388e+01   -3.725e+01   +9e+01   2e-03   4e-16   2e-01   1e-01   0.8500   1 1 1
2   +1.745e+01   -8.925e+00   +3e+01   7e-04   5e-16   5e-02   4e-02   0.7788   1 1 1
3   +9.313e+00   +4.727e-01   +9e+00   2e-04   7e-16   2e-02   1e-02   0.6993   1 1 1
4   +5.212e+00   +4.172e+00   +1e+00   3e-05   3e-16   2e-03   2e-03   0.9673   1 1 1
5   +4.788e+00   +4.650e+00   +1e-01   4e-06   5e-16   2e-04   2e-04   0.8829   2 1 1
6   +4.723e+00   +4.717e+00   +7e-03   2e-07   1e-15   9e-06   9e-06   0.9750   2 1 1
7   +4.720e+00   +4.720e+00   +7e-05   2e-09   3e-16   1e-07   1e-07   0.9899   2 1 1
8   +4.720e+00   +4.720e+00   +7e-07   2e-11   3e-16   1e-09   1e-09   0.9899   3 1 1
9   +4.720e+00   +4.720e+00   +7e-09   3e-12   2e-16   1e-11   1e-11   0.9899   2 1 1

OPTIMAL (within feastol=3.3e-12, reltol=1.5e-09, abstol=7.1e-09).
Runtime: 0.004822 seconds.


In [55]:
path = list(np.flatnonzero(x > .1))
showPaths(G,pos,p,path,Gnodes=[0],Rnodes=[n-1])
print "The evasion probability of the smuggler's optimal path is %e, or %.3f%%."%(np.exp(x.dot(c)), np.exp(x.dot(c))*100)

The evasion probability of the smuggler's optimal path is 8.913907e-03, or 0.891%.


We run a discrete graph-theoretic shortest path algorithm on the same graph to check that we get the same optimal path. The function optPath is using the NetworkX Python package and Dijkstra's_algorithm to compute the optimal path.

In [56]:
y = optPath(G,p)
path = list(np.flatnonzero(y > .1))
showPaths(G,pos,p,path,Gnodes=[0],Rnodes=[n-1])
print "The evasion probability of the smuggler's optimal path is %e, or %.3f%%."%(np.exp(y.dot(c)), np.exp(y.dot(c))*100)

The evasion probability of the smuggler's optimal path is 8.913907e-03, or 0.891%.


# Security's objective¶

The security team's goal is to minimize $P^\mathrm{opt}(c)$, subject to some constraints (say, limits on budget or personnel), which we'll denote by $c \in \mathcal{C}$:

$\begin{array}{ll} \mbox{minimize} & P^{\mathrm{opt}}(c) \ \mbox{subject to} & c \in \mathcal{C}. \end{array}$

But note that $P^{\mathrm{opt}}(c)$ is the optimal value of the problem

$\begin{array}{ll} \mbox{maximize} & c^T x \ \mbox{subject to} & A x = b\ & x \geq 0. \end{array}$

We'd like to combine these two optimization problems into a single problem for the security team to solve, but this is problematic as the variables of one problem, $x$, are multiplied with the variables of the other, $c$, which is not a convex objective in general. To get around this, we'll take the dual (Chapter 5 of the Convex Optimization book) of the smuggler's problem.

Let $D^\mathrm{opt}(c)$ denote the optimal value of the dual of the smuggler's problem, which is

$\begin{array}{ll} \mbox{minimize} & b^T \lambda \ \mbox{subject to} & A^T \lambda \geq c, \end{array}$ with dual variable $\lambda$.

Duality theory guarantees that $D^\mathrm{opt}(c) = P^{\mathrm{opt}}(c)$, which allows us to write the security team's problem as

$\begin{array}{ll} \mbox{minimize} & D^{\mathrm{opt}}(c) \ \mbox{subject to} & c \in \mathcal{C}, \end{array}$ which we can rewrite as the single optimization problem

$\begin{array}{ll} \mbox{minimize} & b^T \lambda \ \mbox{subject to} & A^T \lambda \geq c\ &c \in \mathcal{C}, \end{array}$ where $c$ and $\lambda$ are the optimization variables.

We will denote the optimal value of this problem as $P^\star$. By solving to find $c^\star$, the security team will have optimally allocated resources to make detection of the smuggler as likely as possible.

# Security example¶

We'll consider security's problem on the same network as the last example with the edge evasion probabilities modeled as $p_j = e^{-a_j r_j}$, where $r_j \in \mathbf{R}_+$ denotes the effort (say, yearly budget) allocated to edge $j$. We'll assume $a_j \in \mathbf{R}_{++}$ are given and represent the cost involved in securing an edge. As in the last example, $a_j$ is a uniform random variable over the interval $\left[0,1\right]$.

We'll use the same random seed as the last example, so the last example corresponds to the specific allocation $r_j = 1$ for all $j$ in the current model. We'll use this to compare the detection probability of a naive, uniform effort allocation against the optimal allocation.

For this example, we'll impose a maximum budget constraint $\sum_{j=0}^{m-1} r_j = m$, and a uniform spending limit on each edge, $r_j \leq R$.

We'll also constrain that the evasion probability is equal in both directions. That is, edge $(i,j)$ and edge $(j,i)$ have equal evasion probabilities. We'll enforce that constraint with $Br = 0$, for some matrix $B$.

The final model is $\begin{array}{ll} \mbox{minimize} & b^T \lambda \ \mbox{subject to} & A^T \lambda \geq c \ & c_j = -a_j r_j \ & Br = 0 \ & 0 \leq r \leq R\ &\mathbf{1}^T r = m \end{array}$

We solve the model below with $R=5$ and report the evasion probability of the smuggler's optimal path.

In [57]:
nu = cvx.Variable(n)
r = cvx.Variable(m)
constr = [A.T*nu >= -cvx.mul_elemwise(a,r), cvx.sum_entries(r) == m, r >= 0, B*r == 0, r <= 5]
cvx.Problem(cvx.Minimize(nu.T*b),constr).solve(verbose=True)
nu = np.array(nu.value).flatten()
r = np.array(r.value).flatten()

ECOS 1.0.4 - (c) A. Domahidi, Automatic Control Laboratory, ETH Zurich, 2012-2014.

It     pcost         dcost      gap     pres    dres     k/t     mu      step     IR
0   +8.656e-17   -2.680e+03   +3e+03   3e-03   5e+00   1e+00   3e+00    N/A     1 1 -
1   -1.558e+00   -4.454e+02   +5e+02   6e-04   9e-01   2e-01   4e-01   0.8475   1 1 1
2   -5.901e+00   -1.399e+02   +1e+02   2e-04   3e-01   1e-01   1e-01   0.8810   1 1 1
3   -9.799e+00   -8.402e+01   +8e+01   1e-04   1e-01   6e-02   7e-02   0.5281   2 2 2
4   -1.353e+01   -4.576e+01   +3e+01   4e-05   6e-02   3e-02   3e-02   0.6240   1 2 2
5   -1.668e+01   -3.615e+01   +2e+01   3e-05   4e-02   2e-02   2e-02   0.4594   1 1 2
6   -1.805e+01   -3.053e+01   +1e+01   2e-05   2e-02   1e-02   1e-02   0.4915   1 1 1
7   -1.894e+01   -2.786e+01   +9e+00   1e-05   2e-02   8e-03   9e-03   0.3545   1 1 2
8   -1.954e+01   -2.486e+01   +6e+00   7e-06   1e-02   5e-03   5e-03   0.7330   1 1 2
9   -2.008e+01   -2.274e+01   +3e+00   3e-06   5e-03   3e-03   3e-03   0.8195   1 1 1
10   -2.074e+01   -2.178e+01   +1e+00   1e-06   2e-03   1e-03   1e-03   0.7700   2 1 2
11   -2.098e+01   -2.153e+01   +6e-01   7e-07   1e-03   6e-04   5e-04   0.7530   2 2 2
12   -2.121e+01   -2.126e+01   +5e-02   6e-08   1e-04   5e-05   5e-05   0.9767   2 2 2
13   -2.124e+01   -2.124e+01   +1e-03   2e-09   2e-06   1e-06   1e-06   0.9768   2 1 1
14   -2.124e+01   -2.124e+01   +1e-05   2e-11   2e-08   1e-08   1e-08   0.9899   3 1 1
15   -2.124e+01   -2.124e+01   +1e-07   1e-12   2e-10   1e-10   1e-10   0.9899   3 1 1

OPTIMAL (within feastol=2.5e-10, reltol=6.3e-09, abstol=1.3e-07).
Runtime: 0.012406 seconds.


In [58]:
print "The evasion probability of the smuggler's optimal path is %e."%(np.exp(nu.dot(b)),)
print "The smuggler's chance of evasion is %.2f times smaller than with the uniform resource allocation."%(np.exp(x.dot(c))/np.exp(nu.dot(b)))

The evasion probability of the smuggler's optimal path is 5.963000e-10.
The smuggler's chance of evasion is 14948695.12 times smaller than with the uniform resource allocation.


Here we plot the resulting edge evasion probabilities from the optimal allocation.

In [59]:
p = np.exp(-a*r)
showPaths(G,pos,p,Gnodes=[0],Rnodes=[n-1])

Out[59]:
(<matplotlib.figure.Figure at 0x10e9b5810>,
<matplotlib.axes.AxesSubplot at 0x10e9b5c10>)

We can now solve the smuggler's problem with these optimal resource allocations, but we won't recover a Boolean solution for $x^{\star}$ because the optimal path is not unique. Note, however, that the optimal evasion probability is the same.

In [60]:
c = np.log(p)
x = cvx.Variable(m)
constr = [A*x == b,x>=0, x <= 1]
cvx.Problem(cvx.Maximize(x.T*c),constr).solve(verbose=True)
x = np.array(x.value).flatten()

plt.plot(x)
print "The evasion probability of the smuggler's optimal path is %e."%(np.exp(x.dot(c)),)

ECOS 1.0.4 - (c) A. Domahidi, Automatic Control Laboratory, ETH Zurich, 2012-2014.

It     pcost         dcost      gap     pres    dres     k/t     mu      step     IR
0   +9.649e+01   -1.080e+03   +1e+03   1e-02   4e-16   1e+00   2e+00    N/A     1 1 -
1   +5.962e+01   -4.143e+02   +5e+02   6e-03   6e-16   5e-01   7e-01   0.6885   1 1 1
2   +2.876e+01   -6.403e+01   +1e+02   1e-03   5e-16   1e-01   1e-01   0.8229   1 1 1
3   +2.198e+01   +3.683e+00   +2e+01   2e-04   8e-16   2e-02   3e-02   0.8212   1 1 1
4   +2.128e+01   +2.055e+01   +8e-01   9e-06   5e-16   9e-04   1e-03   0.9712   1 1 1
5   +2.124e+01   +2.123e+01   +8e-03   9e-08   3e-16   9e-06   1e-05   0.9899   1 1 1
6   +2.124e+01   +2.124e+01   +1e-04   1e-09   7e-16   1e-07   1e-07   0.9872   2 1 1
7   +2.124e+01   +2.124e+01   +1e-05   2e-10   5e-16   2e-08   2e-08   0.8558   3 3 3
8   +2.124e+01   +2.124e+01   +3e-06   5e-11   2e-13   4e-09   5e-09   0.8147   3 3 3
9   +2.124e+01   +2.124e+01   +1e-06   9e-11   1e-11   1e-09   1e-09   0.8312   3 3 3

OPTIMAL (within feastol=8.6e-11, reltol=4.6e-08, abstol=9.9e-07).
Runtime: 0.004146 seconds.

The evasion probability of the smuggler's optimal path is 5.962999e-10.