def __init__(self, E_nu, nu_dir, e_angle_generator): self.nuebar = particle(NUEBAR, 0, E_nu, nu_dir) self.ebar = particle(EBAR, e0_e, 0, np.array([0, 0, 0])) self.n = particle(NEUTRON, e0_n, 0, np.array([0, 0, 0])) ROOT.gRandom.SetSeed(0) self.rng = ROOT.TRandom3() cos_e_theta = e_angle_generator(E_nu) self.initialize(cos_e_theta)
def merge(arr, l, m, r): n1 = m - l + 1 n2 = r - m # create temp arrays L = [particle(0, 0, 0, 0, 0, 0)] * (n1) R = [particle(0, 0, 0, 0, 0, 0)] * (n2) # Copy data to temp arrays L[] and R[] for i in xrange(0, n1): L[i] = arr[l + i] for j in xrange(0, n2): R[j] = arr[m + 1 + j] # Merge the temp arrays back into arr[l..r] i = 0 # Initial index of first subarray j = 0 # Initial index of second subarray k = l # Initial index of merged subarray while i < n1 and j < n2: if L[i].wt >= R[j].wt: arr[k] = L[i] i += 1 else: arr[k] = R[j] j += 1 k += 1 # Copy the remaining elements of L[], if there # are any while i < n1: arr[k] = L[i] i += 1 k += 1 # Copy the remaining elements of R[], if there # are any while j < n2: arr[k] = R[j] j += 1 k += 1
def __init__(self, target, pt_number, landmarks, gps_error): self.target = target self.landmarks = landmarks self.pt_number = pt_number self.gps_error = gps_error # self.x = 0.0 # self.y = 0.0 self.pts = [] self.residual = [] for i in range(pt_number): pt = particle(self.target) pt.set_noise(0.05, 0.05, 2.0) self.pts.append(pt)
def __init__(self,num,Vmax,iterations,C1,C2,filename): print "begin Initial parameters" self.__numParticle=num self.__Vmax=Vmax self.__Max_ITERATIONS=iterations self.__C1=C1 self.__C2=C2 self.__filename=filename self.__particleNum=[] self.__gbest=1.0 self.__gbestb=-2.0 for i in xrange(self.__numParticle): p=particle(0.05*i,0.01*i) self.__particleNum.append(p) print "have initial done the num:%s,iterations:%s" %(self.__numParticle,self.__Max_ITERATIONS)
thrd.daemon = True # python will shut down the thread when the code end thrd.start() # start running the thread # deciding for manual or A* tryagain = True while (tryagain == True): try: option = int(input("1 - Manual mode / 2 - A*: ")) except: continue # manual mode if option == 1: tryagain = False print("\n\n"+"-"*50+"\n--- Use the numpad to move particle around ---\n"+"-"*50+"\n\n") p = particle(mp,(mp.start[0][0],mp.start[0][1])) while(True): p.update() nxt = get_key() val = p.move_particle(nxt) if nxt == "close" or val == -1: break else: pass print("End poit reached!\nSelect the maze and press esc to close...\n") # A* elif option == 2: tryagain = False
import particleModelEnsemble as pmEns def objectBlacklist(obj): """ find out which of an ojects attributes cannot be pickled """ d = obj.__dict__ black = [] for k in d.keys(): try: pkl.dumps(d[k]) except: black+=[k] return black PF = plasmaField(fastOn = True) p = particle() ep = emittingParticle() p.inject(PF) PS = ps.particleSolver(p) PS.solve() PE = pmEns.basicParticleEnsemble(pmEns.basicGriddedParticleParameterGenerator, 'pickleTestEns') PE.generate(n = (2,2)) PE.ensembleSolve() print 'done!' L = [ p,ep, PE, PS, gm.gasMixture()] n = [ 'particle','emitting particle', 'particle ensemble', 'particle solver', 'gas mixture'] print "begin pickling..." for i in range(len(L)): try:
def particle_filter(motions, measurements, N=500): # I know it's tempting, but don't change N! # -------- # # Make particles # #f = open("data.txt", 'w') p = [] for i in range(N): r = particle() r.set_noise(bearing_noise, steering_noise, distance_noise) p.append(r) #f.write("%s\t%s\n" %(r.x, r.y)) # -------- # # Update particles # real = particle() real.set(45,75,pi) for t in range(len(motions)): if motions[t] == 0: real.set(real.x +cos(real.orientation), real.y +sin(real.orientation), real.orientation) # motion update (prediction) # moving every particle for every robot motion p2 = [] #f.write("Next set\n\n") plot_x = [] plot_y = [] for i in range(N): p2.append(p[i].move(motions[t])) plot_x.append(p[i].x) plot_y.append(p[i].y) #f.write("%s\t%s\n" %(r.x, r.y)) p = p2 real_x = real.x real_y = real.y plt.plot(plot_x, plot_y, '.') plt.plot(real_x, real_y, 'ro') plt.plot([10,10],[10,20],'k') plt.plot([20,20],[0,10],'k') plt.plot([10,20],[20,20],'k') plt.plot([0,20],[30,30],'k') plt.plot([10,10],[40,60],'k') plt.plot([30,30],[60,70],'k') plt.plot([0,10],[60,60],'k') plt.plot([0,10],[70,70],'k') plt.plot([20,20],[60,70],'k') plt.plot([30,30],[10,40],'k') plt.plot([20,30],[40,40],'k') plt.plot([20,40],[50,50],'k') plt.plot([20,20],[60,70],'k') plt.plot([20,50],[70,70],'k') plt.plot([40,40],[0,10],'k') plt.plot([40,40],[30,40],'k') plt.plot([40,40],[50,60],'k') plt.plot([40,50],[20,20],'k') plt.plot([50,50],[0,30],'k') plt.plot([50,50],[50,70],'k') plt.plot([50,60],[30,30],'k') plt.plot([50,60],[40,40],'k') plt.plot([60,60],[0,10],'k') plt.plot([60,60],[40,50],'k') plt.plot([60,60],[60,80],'k') plt.plot([60,70],[10,10],'k') plt.plot([60,80],[20,20],'k') plt.plot([60,70],[30,30],'k') plt.plot([70,80],[40,40],'k') plt.plot([70,70],[40,50],'k') plt.plot([70,70],[60,70],'k') plt.grid(b=1) plt.axis([0, 80, 0, 80],) plt.show() # measurement update # getting the probability measurement for each particle w = [] for i in range(N): w.append(p[i].measurement_prob(measurements[t])) # resampling (reproducing, sex) p3 = [] index = int(random.random() * N) beta = 0.0 mw = max(w) for i in range(N): beta += random.random() * 2.0 * mw while beta > w[index]: beta -= w[index] index = (index + 1) % N p3.append(p[index]) p = p3 return [get_position(p), plot_x, plot_y]
def getParticles(self): self.particles = [] for p in range(self.tree.Particle_size): self.particles.append(particle(self.tree, p))
def smearJets(jets): smearedJets = [] for jet in jets: smearedJets.append(particle(None, None, jet)) smearedJets.sort(key=lambda jet: jet.pt, reverse=True) return smearedJets
def particle_filter(motions, measurements, N=500, start_pos = [45, 45, 0]): # I know it's tempting, but don't change N! # -------- # # Make particles # #f = open("data.txt", 'w') p = [] for i in range(N): r = particle() r.set_noise(bearing_noise, steering_noise, distance_noise) p.append(r) #f.write("%s\t%s\n" %(r.x, r.y)) # -------- # # Update particles # #real = particle() #real.set(start_pos[0], start_pos[1], start_pos[2]) for t in range(len(motions)): print t """ turn_val = 3.25 if motions[t] == 0: real.set(real.x +cos(real.orientation), real.y +sin(real.orientation), real.orientation) elif motions[t] == 1: if real.orientation == 0: new_x = real.x - turn_val new_y = real.y - turn_val elif real.orientation == pi/2: new_x = real.x + turn_val new_y = real.y - turn_val elif real.orientation == pi: new_x = real.x + turn_val new_y = real.y + turn_val elif real.orientation == 3*pi/2: new_x = real.x - turn_val new_y = real.y + turn_val new_o = (real.orientation + (3 * pi / 2)) % (2 * pi) real.set(new_x, new_y, new_o) elif motions[t] == 2: if real.orientation == 0: new_x = real.x - turn_val new_y = real.y + turn_val elif real.orientation == pi/2: new_x = real.x - turn_val new_y = real.y - turn_val elif real.orientation == pi: new_x = real.x + turn_val new_y = real.y - turn_val elif real.orientation == 3*pi/2: new_x = real.x + turn_val new_y = real.y + turn_val new_o = (real.orientation + (pi / 2)) % (2 * pi) real.set(new_x, new_y, new_o) elif motions[t] == 3: if real.orientation == 0: new_x = real.x - 2 * turn_val new_y = real.y elif real.orientation == pi/2: new_x = real.x new_y = real.y - 2 * turn_val elif real.orientation == pi: new_x = real.x + 2 * turn_val new_y = real.y elif real.orientation == 3*pi/2: new_x = real.x new_y = real.y + 2 * turn_val new_o = (real.orientation + (pi)) % (2 * pi) real.set(new_x, new_y, new_o) """ # motion update (prediction) # moving every particle for every robot motion p2 = [] #f.write("Next set\n\n") plot_x = [] plot_y = [] for i in range(N): p2.append(p[i].move(motions[t])) plot_x.append(p2[i].x) plot_y.append(p2[i].y) #f.write("%s\t%s\n" %(r.x, r.y)) p = p2 #real_x = real.x #real_y = real.y ## save fig ## Video writer - animations and videos plt.clf() plt.plot(plot_x, plot_y, '.') #plt.plot(real_x, real_y, 'ro') plt.plot([10,10],[10,20],'k') plt.plot([20,20],[0,10],'k') plt.plot([10,20],[20,20],'k') plt.plot([0,20],[30,30],'k') plt.plot([10,10],[40,60],'k') plt.plot([30,30],[60,70],'k') plt.plot([0,10],[60,60],'k') plt.plot([0,10],[70,70],'k') plt.plot([20,20],[60,70],'k') plt.plot([30,30],[10,40],'k') plt.plot([20,30],[40,40],'k') plt.plot([20,40],[50,50],'k') plt.plot([20,20],[60,70],'k') plt.plot([20,50],[70,70],'k') plt.plot([40,40],[0,10],'k') plt.plot([40,40],[30,40],'k') plt.plot([40,40],[50,60],'k') plt.plot([40,50],[20,20],'k') plt.plot([50,50],[0,30],'k') plt.plot([50,50],[50,70],'k') plt.plot([50,60],[30,30],'k') plt.plot([50,60],[40,40],'k') plt.plot([60,60],[0,10],'k') plt.plot([60,60],[40,50],'k') plt.plot([60,60],[60,80],'k') plt.plot([60,70],[10,10],'k') plt.plot([60,80],[20,20],'k') plt.plot([60,70],[30,30],'k') plt.plot([70,80],[40,40],'k') plt.plot([70,70],[40,50],'k') plt.plot([70,70],[60,70],'k') plt.grid(b=1) plt.axis([0, 80, 0, 80],) filename = str('Testing/Robot_11/Image_%02d' % t) + '.png' plt.savefig(filename, dpi=100) print "saving figure", filename #plt.show() # measurement update # getting the probability measurement for each particle w = [] for i in range(N): w.append(p[i].measurement_prob(measurements[t])) # resampling (reproducing, sex) p3 = [] index = int(random.random() * N) beta = 0.0 mw = max(w) for i in range(N): beta += random.random() * 2.0 * mw while beta > w[index]: beta -= w[index] index = (index + 1) % N p3.append(p[index]) p = p3 return get_position(p)