Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import math
  4. import scipy.special
  5. import time
  6. import os
  7. import random
  8. import copy
  9.  
  10.  
  11.  
  12. def placeCities(N,xmax,ymax):
  13. c = np.zeros([N,2])
  14. c[:,0],c[:,1] = map(lambda x: random.randint(0,xmax),c[:,0]),map(lambda x: random.randint(0,xmax),c[:,1])
  15. return c
  16.  
  17. #plt.figure(1)
  18. #c = placeCities(5,100,100)
  19. #plt.scatter(c[:,0],c[:,1])
  20. #plt.show()
  21.  
  22. def distances(c):
  23. A = np.zeros([len(c),len(c)])
  24. for i in range(0,len(c)):
  25. for j in range(i+1,len(c)):
  26. A[i,j] = np.linalg.norm(c[i,:]-c[j,:])
  27. A[j,i] = A[i,j]
  28. return A
  29.  
  30. def genRoute(N):
  31. r = np.array(range(0,N),dtype=int)
  32. random.shuffle(r)
  33. return r
  34.  
  35. def rlength(r,R):
  36. return sum(R[r[1:],r[:-1]])
  37.  
  38. def genNRoutes(NR,NS):
  39. R = np.zeros([NR,NS],dtype=int)
  40. R[:,:] = map (lambda x: genRoute(NS),R)
  41. return R
  42.  
  43. def shortestRoute(rn,c):
  44. m,mi = 0,0
  45. R = distances(c)
  46. for i in range(0,rn.shape[0]):
  47. t = rlength(rn[i],R)
  48. if t < m:
  49. m,mi = t,i
  50. return rn[i]
  51.  
  52. def duell(rn,c):
  53. R = distances(c)
  54. d = range(0,rn.shape[0])
  55. random.shuffle(d)
  56. s = np.zeros([rn.shape[0]/2,rn.shape[1]],dtype=int)
  57. j=0
  58. for i in range(0,len(d),2):
  59. s[j] = rn[d[i],:] if rlength(rn[d[i],:],R)<rlength(rn[d[i+1],:],R) else rn[d[i+1],:]
  60. j+=1
  61. return s
  62.  
  63. def plotroute(r,c):
  64. plt.figure(1)
  65. plt.scatter(c[:,0],c[:,1])
  66. plt.plot(c[r][:,0],c[r][:,1])
  67. plt.show()
  68.  
  69. def nextGen(rn,M):
  70. parents = copy.deepcopy(rn)
  71. children = copy.deepcopy(rn)
  72. for i in range(0,M):
  73. c,p1,p2 = random.randint(0,len(rn)-1),random.randint(0,rn.shape[1]-1),random.randint(0,rn.shape[1]-1)
  74. if p1!=p2:
  75. rn[c,p2],rn[c,p1] = rn[c,p1],rn[c,p2]
  76.  
  77. return parents,children
  78.  
  79. def tsp():
  80. cpop = 200
  81. ccities = 15
  82. c = placeCities(ccities,100,100)
  83. R = distances(c) #TODO!!! duell
  84. rn = duell(duell(duell(duell(genNRoutes(16*cpop,ccities),c),c),c),c)
  85. #plotroute(shortestRoute(rn,c),c)
  86. ltest = np.zeros(200);
  87. for k in range(0,200):
  88. parents,children = nextGen(rn,1)
  89. g = np.concatenate((parents,children),axis=0)
  90. rn = duell(g,c)
  91. ltest[k] = rlength(shortestRoute(rn,c),R)
  92. plt.figure(3)
  93. plt.plot(range(0,200),ltest)
  94. plt.show()
  95. plotroute(shortestRoute(rn,c),c)
  96.  
  97. tsp()
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. # def V(d):
  128. # return (np.pi**(d/2))/scipy.special.gamma((d/2)+1)
  129.  
  130.  
  131.  
  132. # plt.figure(1)
  133. # plt.plot(range(0,11),[V(i) for i in range(0,11)])
  134. # plt.show()
  135.  
  136.  
  137. # def U(u,x):
  138. # return (1/2.0)*u*x**2
  139.  
  140. # def p(u,u0,kT,x):
  141. # return np.exp(-u(u0, x)/kT)/np.sqrt(2*np.pi*kT/u0)
  142.  
  143. # def mA(U,u0,kT,N,r):
  144. # ra = random(7**5,0,(2**31)-1)
  145. # x = np.zeros(N+1)
  146. # counter = 0.0
  147. # x[0] = float(ra.next())/(2**31-1)
  148. # for i in range(1,N):
  149. # a = -r+(2*r)*float(ra.next())/(2**31-1)
  150. # U1 = U(u0, x[i-1]+a) - U(u0, x[i-1])
  151. # if U(u0, x[i-1]+a) <= U(u0, x[i-1]):
  152. # x[i] = x[i-1] + a
  153. # else:
  154. # if float(ra.next())/(2**31-1) <= p(U,U1,kT,x[N-1]):
  155. # x[i] = x[i-1]+a
  156. # else:
  157. # x[i] = x[i-1]
  158. # counter += 1
  159. # return x, 1-(counter/N)
  160.  
  161. # print 'Akzeptanzrate: '+str(mA(U,1.0,2.0,10000,1.8)[1])
  162.  
  163. # x = np.linspace(-100,100,10001)
  164.  
  165. # plt.figure()
  166. # plt.hist(mA(U,1.0,2.0,10000,1.9)[0],100,normed=1)
  167. # plt.plot(x,p(U,1,2.,x))
  168. # plt.show()
  169.  
  170.  
  171. def random(a,c,m):
  172. n = int(((np.abs(64979*(round(time.time()*1000))*(os.getpid()-83))%104729)/(104729))*m)
  173. while 1:
  174. yield n
  175. n = (a*n+c)%m
  176.  
  177. r = random(7**5,0,(2**31)-1)
  178. z = [float(r.next())/((2**31)-1) for i in range(0,10000)]
  179. # print np.average(z)
  180. # print np.std(z)
  181. # plt.hist(z)
  182. #plt.show()
  183.  
  184.  
  185. def crudemc(f,a,b,N):
  186. return ((b-a)/N)*sum(np.vectorize(f)(a+(b-a)*[random(7**5,0,(2**31)-1)for i in range(0,N)]))
  187.  
  188.  
  189. #Aufgabe 11.2.1:
  190. def crudemc(f,a,b,N):
  191. r = random(7**5,0,(2**31)-1)
  192. print (b-a)/N
  193. return ((b-a)/N)*sum(f([a+float((b-a)*float(r.next()/(2**31))) for i in range(0,N)]))
  194.  
  195.  
  196. def hommc(f,a,b,N,fmin,fmax):
  197. r = random(7**5,0,(2**31)-1)
  198. hits = 0
  199. for i in range(0,N):
  200. hits+=1 if(f(a+((b-a)*float(r.next())/(2**31-1)))-fmin>(fmax-fmin)*float(r.next())/(2**31-1)) else 0
  201. print (b-a)
  202. print ((hits/N)*(fmax-fmin)+fmin)
  203. return (b-a)*((hits/N)*(fmax-fmin)+fmin)
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210. # def f(x):
  211. # return 1/(1+np.power(np.cos(x),2))
  212. # def g(x):
  213. # return np.power(x,-12.0)-np.power(x,-6.0)
  214.  
  215. # def dft_forward(a):
  216. # tmp = np.zeros(len(a),dtype="complex")
  217. # for k in range(0,len(a)):
  218. # for i in range(0,len(a)):
  219. # tmp[k] += np.exp(-2j*np.pi*i*k/len(a))*a[i]
  220. # return tmp
  221.  
  222. # def dft_backward(a):
  223. # tmp = np.zeros(len(a),dtype="complex")
  224. # for k in range(0,len(a)):
  225. # for i in range(0,len(a)):
  226. # tmp[k] += np.exp(2j*np.pi*i*k/len(a))*a[i]
  227. # tmp[k] = tmp[k]/len(a)
  228. # return tmp
  229.  
  230.  
  231.  
  232. # signal = np.genfromtxt('/Users/blank/signal.txt')
  233. # signal_fft = np.fft.fft((signal[:,1]))
  234. # plt.figure(1)
  235. # # plt.plot(signal)
  236. # plt.plot(signal_fft)
  237. # # print signal[:,1]
  238. # plt.show()
  239.  
  240.  
  241. # guitar = scipy.io.wavfile.read('/Users/blank/uni/comphy/guitar.wav')
  242. # plt.figure(1)
  243. # plt.plot(np.fft.rfft(guitar[1]))
  244. # d2 = np.fft.rfft(guitar[1])
  245. # for x in range(0,len(d2)):
  246. # if np.abs(d2[x])<70000:
  247. # d2[x]=0
  248. # plt.figure(2)
  249. # plt.plot(d2)
  250. # plt.show()
  251. # rev = np.array(np.fft.irfft(d2),dtype="int16")
  252. # scipy.io.wavfile.write("testfile.wav",guitar[0],rev)
  253.  
  254. # def V(x):
  255. # y = np.zeros_like(x)
  256. # for i in range(0,length(x)):
  257. # y[i] = x[i]*np.e**((-x[i]**2)/4) if x < 5 and x > -5 else np.inf
  258. # return y
  259.  
  260. # def y_deriv(y,x,E,V):
  261. # return y[1],2*(V(x)-E)*y[0]
  262.  
  263. # x=np.linspace(-5,5.0001,0.01)
  264. # E=-0.99
  265. # y=np.array([0,1])
  266. # y0=np.array([0,1])
  267.  
  268. # y = scipy.integrate.odeint(y_deriv,y0,x,args=(E,V))
  269.  
  270.  
  271.  
  272. # def skonf(dx,x0):
  273. # temp = np.zeros(1.0/dx)
  274. # temp[x0] = 1
  275. # return temp
  276.  
  277. # def solve(dx,dt,x0,D,T_max):
  278. # steps = int(T_max/dt)
  279. # c = np.zeros([1.0/dx,steps])
  280. # c[:,0] = skonf(dx,x0)
  281. # for i in range(0,steps-1):
  282. # for j in range(0,int(1/dx)-1):
  283. # c[j,i+1] = c[j,i]+D*(dt/(dx**2))*(c[j+1,i]-2.0*c[j,i]+c[j-1.0,i])
  284. # return c
  285.  
  286.  
  287.  
  288. # D = ((1.38068*10**-23)*298)/(6*np.pi*(10**-3)*(7.3*10**-10))
  289. # plt.figure(1)
  290. # plt.plot(solve(1.0/100,16,20,D,3600))
  291. # plt.show()
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311. def y_(y):
  312. return alpha*(y**2)*(1-y)-0.01*y
  313.  
  314. def phi(y1, y2):
  315. return y2,-np.sqrt((y1+9.81)*(80/0.2))
  316. # y_k+1 = y_k + h*f(t_k,y_k) k = 0,1,2,...
  317. def euler(f,steps,h,y01,y02):
  318. y1 = np.zeros(steps)
  319. y2 = np.zeros(steps)
  320. y1[0] = y01
  321. y2[0] = y02
  322. for j in range(1,steps):
  323. y11,y21 = f(y1[j-1],y2[j-1])
  324. y1[j] = y1[j-1]+h*y11
  325. y2[j] = y2[j-1]+h*y21
  326. return y1,y2
  327.  
  328. #Aufgabe 8.2.3:
  329. def rk4(f,steps,h,y01,y02):
  330. y1 = np.zeros(steps)
  331. y1[0] = y01
  332. y2 = np.zeros(steps)
  333. y2[0] = y02
  334. for i in range(1,steps):
  335. k11,k12 = f(y1[i-1],y2[i-1])
  336. k21,k22 = f(y1[i-1]+(h/2)*k11,y2[i-1]+(h/2)*k12)
  337. k31,k32 = f(y1[i-1]+(h/2)*k21,y2[i-1]+(h/2)*k22)
  338. k41,k42 = f(y1[i-1]+h*k31,y1[i-1]+h*k32)
  339. y1[i]=y1[i-1]+(h/6)*(k11+2*k21+2*k31+k41)
  340. y2[i]=y2[i-1]+(h/6)*(k12+2*k22+2*k32+k42)
  341. return y1,y2
  342.  
  343. def test():
  344. steps = 20
  345. i,k = rk4(phi,steps,0.05,3000,3000)
  346. plt.plot(range(0,steps),k)
  347. plt.show()
  348. #test()
  349.  
  350. def rk4(f,steps,h,y0):
  351. y = np.zeros(steps)
  352. y[0] = y0
  353. for i in range(1,steps):
  354. k1 = f(y[i-1])
  355. k2 = f(y[i-1]+(h/2)*k1)
  356. k3 = f(y[i-1]+(h/2)*k2)
  357. k4 = f(y[i-1]+h*k3)
  358. y[i]=y[i-1]+(h/6)*(k1+2*k2+2*k3+k4)
  359. return y
  360.  
  361. def aufg812():
  362. global alpha
  363. steps = 100000
  364. alpha = 1
  365. i = euler(y_,steps,0.05,1)
  366. plt.plot(range(0,steps),i,label="alpha = 1")
  367. alpha = 0.05
  368. i = euler(y_,steps,0.05,1)
  369. plt.plot(range(0,steps),i,label="alpha = 0.05")
  370. alpha = 0.04
  371. i = euler(y_,steps,0.05,1)
  372. plt.plot(range(0,steps),i,label="alpha = 0.04")
  373. alpha = 0.03
  374. i = euler(y_,steps,0.05,1)
  375. plt.plot(range(0,steps),i,label="alpha = 0.03")
  376. plt.legend()
  377. plt.show()
  378.  
  379.  
  380. def aufg813():
  381. global alpha
  382. steps = 10000
  383. alpha = 1
  384. i = euler(y_,steps,0.05,1)
  385. plt.plot(range(0,steps),i,label="alpha = 1")
  386. alpha = 0.05
  387. i = euler(y_,steps,0.05,1)
  388. plt.plot(range(0,steps),i,label="alpha = 0.05")
  389. alpha = 0.04
  390. i = euler(y_,steps,0.05,1)
  391. plt.plot(range(0,steps),i,label="alpha = 0.04")
  392. alpha = 0.03
  393. i = euler(y_,steps,0.05,1)
  394. plt.plot(range(0,steps),i,label="alpha = 0.03")
  395. alpha = 0.001
  396. i = euler(y_,steps,0.05,1)
  397. plt.plot(range(0,steps),i,label="alpha = 0.001")
  398. alpha = -0.001
  399. i = euler(y_,steps,0.05,1)
  400. plt.plot(range(0,steps),i,label="alpha = -0.001")
  401. plt.legend()
  402. plt.show()
  403. #aufg813()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement