In [2]:
from numpy import *

In [3]:
# let's make a test function
def E(z):
global ncalls
ncalls += 1
return 7 + (z[0]-2.)**2 + (z[1]-3.)**2   # minimizer is (2,3)

In [7]:
# choose a search radius
# pick a starting guess
z = array([0,1.])

# to begin with, let's do a fixed number of iterations
nits = 30000
r = 1  # choose based on domain knowledge
Ez = E(z)
for i in range(nits):
ztry = z + 2*r*random.rand(len(z)) - r
Eztry = E(ztry)
if Eztry < Ez:
# replace z by ztry
z = ztry
Ez = Eztry
print(z)

[ 2.00302735  2.99703217]

In [12]:
# choose a search radius
# pick a starting guess
z = array([0,1.])

# to begin with, let's do a fixed number of iterations
nits = 30
r = 1  # choose based on domain knowledge
Ez = E(z)
for i in range(nits):
ztry = z + 2*r*random.rand(len(z)) - r
Eztry = E(ztry)
if Eztry < Ez:
# replace z by ztry
z = ztry
Ez = Eztry
print(i,'success')
else:
print(i,'FAIL!')
print(z)

0 success
1 success
2 success
3 FAIL!
4 success
5 FAIL!
6 FAIL!
7 success
8 FAIL!
9 FAIL!
10 FAIL!
11 FAIL!
12 FAIL!
13 success
14 FAIL!
15 FAIL!
16 FAIL!
17 FAIL!
18 FAIL!
19 FAIL!
20 FAIL!
21 FAIL!
22 FAIL!
23 FAIL!
24 FAIL!
25 FAIL!
26 FAIL!
27 FAIL!
28 FAIL!
29 FAIL!
[ 1.76588029  2.85339969]


# pick a starting guess¶

z = array([0,100.])

# nits = 30¶

ncalls = 0 tol = .001 nfailsmax = 5 r = 10. # choose based on domain knowledge Ez = E(z)

# for i in range(nits):¶

nfails = 0 while(True): ztry = z + 2rrandom.rand(len(z)) - r Eztry = E(ztry) if Eztry < Ez:

    # replace z by ztry
z = ztry
Ez = Eztry
#print(i,'success')
nfails = 0
else:
#print(i,'FAIL!')
nfails += 1
if nfails > nfailsmax:
r /= 2
if r < tol: break


print(z,ncalls)

In [7]:
%matplotlib notebook
import matplotlib.pyplot as plt
# pick a starting guess
z = array([0,100.])

# to begin with, let's do a fixed number of iterations
#nits = 30
ncalls = 0
tol = .001
nfailsmax = 10
r = 10.  # choose based on domain knowledge
Ez = E(z)
#for i in range(nits):
nfails = 0
plt.subplot(111,aspect=1)
plt.plot(z[0],z[1],'mo',alpha=0.9)
while(True):
ztry = z + 2*r*random.rand(len(z)) - r
Eztry = E(ztry)
if Eztry < Ez:
# replace z by ztry
plt.plot([z[0],ztry[0]],[z[1],ztry[1]],'b',alpha=0.9)
plt.plot(ztry[0],ztry[1],'bo',alpha=0.9)
z = ztry
Ez = Eztry
#print(i,'success')
nfails = 0
else:
#print(i,'FAIL!')
plt.plot(ztry[0],ztry[1],'ro',alpha=0.2)
plt.plot([z[0],ztry[0]],[z[1],ztry[1]],'r',alpha=0.2)
nfails += 1
if nfails > nfailsmax:
r /= 2
print(r)
if r < tol:
plt.plot(z[0],z[1],'co',alpha=0.9)
break
print(z,ncalls)

5.0
2.5
1.25
0.625
0.3125
0.15625
0.078125
0.0390625
0.01953125
0.009765625
0.0048828125
0.00244140625
0.001220703125
0.0006103515625
[ 2.00016896  3.00019059] 198

In [28]:
gpsdata = loadtxt('gpsdata.txt',skiprows=1)
gpsdata

Out[28]:
array([[  91.  ,  213.  ,  222.  ],
[-213.  ,  701.  ,  457.  ],
[ -91.  ,  183.  ,  205.  ],
[-304.8 ,  335.28,  463.  ],
[  91.5 ,  120.1 ,  135.5 ],
[  93.  ,  248.  ,  194.  ],
[  91.4 ,  703.1 ,  286.  ],
[   0.  ,  214.  ,  262.5 ],
[  10.  ,  202.  ,  545.  ],
[ 330.  ,   30.  ,  337.  ]])
In [ ]:
def E(guess):
global gpsdata
# return the error