def __init__(self, N, L, rho, eta): """Creates and manages the field, which is not a lattice, but a continuous square plane. Only two parameters are needed; set the other to zero. Assumes a radius around the particle of 1. N Number of particles to create. L Side length of the square field. Must be an integer. rho Density, approximately equivalent to N/L^2. """ # Handle the choose-any-two nature of the parameters self.N = (N if (N != 0 and N is not None) else int(rho * L * L)) self.width = (L if (L != 0 and L is not None) else int(math.sqrt(N / rho))) self.height = self.width self.eta = eta # generate the N agents, stored in a triple-nested list. Use this to # iterate over all particles in a given cell: # do_stuff(p) for p in self.particles[y][x] self.particles = [[[] for __ in range(self.width)] for _ in range(self.height)] x = y = 0 for i in range(self.N): x = self.width * U() y = self.height * U() self.particles[int(y)][int(x)].append(Particle(x, y))
def naiv_parametrix(T, Lambda, counter): #naiv implementation of the forward method for a simple exemple X_pred = x0 TETA = 1 # t = 0 delta_tau = -(1. / Lambda) * log(U(a=0, b=1)) t += delta_tau Var = 0 if (delta_tau > T): counter = counter + 1 X_new = x0 + b(x0) * T + sig(x0) * sqrt(T) * W(0, 1) Var = (exp(Lambda * T) * f(X_new))**2 return exp(Lambda * T) * f(X_new), counter, Var else: while (t < T): X_new = X_pred + delta_tau * b(X_pred) + sqrt(delta_tau) * W( 0, 1) * sig(X_pred) TETA = TETA * teta(delta_tau, X_pred, X_new) / Lambda X_pred = X_new delta_tau = -(1. / Lambda) * log(U(a=0, b=1)) t = t + delta_tau delta = T - t + delta_tau last_X = X_pred + delta * b(X_pred) + sqrt(delta) * W(0, 1) * sig(X_pred) TETA = TETA * teta(delta, X_pred, last_X) / Lambda Var = (exp(Lambda * T) * f(last_X) * TETA)**2 return exp(Lambda * T) * f(last_X) * TETA, counter, Var
def parametrix_plot(T, Lambda, counter): #modification de naive_parmetrix pour l'affichage X_pred = x0 TETA = 1 t = 0 delta_tau = -(1. / Lambda) * log(U(a=0, b=1)) t += delta_tau Var = 0 bool = True if (delta_tau > T): counter = counter + 1 X_new = x0 + b(x0) * T + sig(x0) * sqrt(T) * W(0, 1) Var = (exp(Lambda * T) * f(X_new))**2 return exp(Lambda * T) * f(X_new), Var, counter, bool, X_new else: while (t < T): X_new = X_pred + delta_tau * b(X_pred) + sqrt(delta_tau) * W( 0, 1) * sig(X_pred) TETA = TETA * teta(delta_tau, X_pred, X_new) / Lambda X_pred = X_new delta_tau = -(1. / Lambda) * log(U(a=0, b=1)) t = t + delta_tau delta = T - t + delta_tau last_X = X_pred + delta * b(X_pred) + sqrt(delta) * W(0, 1) * sig(X_pred) TETA = TETA * teta(delta, X_pred, last_X) / Lambda Var = (exp(Lambda * T) * f(last_X) * TETA)**2 bool = False return exp(Lambda * T) * f(last_X) * TETA, Var, counter, bool, last_X
def _optimization_step(self, param): """ This method is run on a separate process at each iteration. Applies the formula (*) - see it below - for one particle :param param: contains particle, velocity, pbest and gbest, all needed to implement formula :return: returns updated values for particle, velocity, pbest and gbest """ particle, velocity, pbest, gbest = param # save particle because we will compute velocity at the end old_particle = deepcopy(particle) old_particle = op_perm_fix(x=self.full_particle(old_particle), P=self.precedences) # apply formula (*): v(k+1) = [inertia * v(k)] + [personal * rand() * (p(i) - x(i))] + [social * rand() * (g - x(i))] # compute each term inside square brackets velocity_inertia = op_scalar_mul_velocity(c=self.coef_inertia, v=velocity) diff_velocity_personal = op_perm_sub_perm(pbest, particle) velocity_personal = op_scalar_mul_velocity( self.coef_personal * U(0, 1), diff_velocity_personal) diff_velocity_social = op_perm_sub_perm(gbest, particle) velocity_social = op_scalar_mul_velocity(self.coef_social * U(0, 1), diff_velocity_social) # apply each velocity individually to particle because we don't have a velocity + velocity operator particle = op_perm_sum_velocity(particle, velocity_inertia) particle = op_perm_sum_velocity(particle, velocity_personal) particle = op_perm_sum_velocity(particle, velocity_social) # fix current particle so that it repects precedence constraints particle = op_perm_fix(x=self.full_particle(particle), P=self.precedences) # compute velocity that transforms old_particle into self.patricles[i] velocity = op_perm_sub_perm(particle, old_particle) cost = self.cost(particle) # update personal best in case we got a better particle than previous personal best if cost < self.cost(pbest): pbest = deepcopy(particle) if cost < self.cost(self.gbest): self.gbest = deepcopy(particle) return particle, velocity, pbest, cost
def update(self): """Generates a new list of particles, computes their velocities, and then does an integration step (delta t = 1) to determine the new particle's x and y position. """ new_particles = [[[] for __ in range(self.width)] for _ in range(self.height)] x = 0 y = 0 while y < self.height: x = 0 while x < self.width: for p in self.particles[y][x]: # p is each current particle # updating velocity of new particle new_angle = self.getLocalAverageAngle( p, x, y) + self.eta * (U() - 0.5) P = Particle(0, 0) P.setVelocityAngleMagnitude(new_angle, 0.03) # numerical integration with dt=1, and we are on a torus P.xpos = (p.xpos + P.xvel) % self.width P.ypos = (p.ypos + P.yvel) % self.height # store in correct tile new_particles[int(P.ypos)][int(P.xpos)].append(P) x += 1 y += 1 # when finished updating everything, save the new agents and let the GC # handle the old self.particles = new_particles
def reject_gamma(a): u = U(0,1) if u < (exp(1)/(a+exp(1))): Y = (((a+exp(1))/exp(1))*u)**(1./a) else: Y = -log((1-u)*((a+exp(1))/a*exp(1))) if Y < 1: q = exp(-Y) if Y> 1 : q = Y**(a-1) else : q = 0 v = U(0,1) if v <= q: return Y else: return reject_gamma(a)
def __init__(self, x, y, v=0.03): """This represents a single particle in the system. It is created with a velocity of ``v`` in a random direction, at point (x,y). """ self.xpos = x self.ypos = y theta = 2 * math.pi * U() # pick a random angle self.xvel = v * math.cos(theta) self.yvel = v * math.sin(theta)
def __init__(self, N, L, rho, eta): """Creates and manages the field, which is not a lattice, but a continuous square plane. Only two parameters are needed; set the other to zero. N Number of particles to create. L Side length of the square field. rho Density, equivalent to N/L^2. """ # Handle the choose-any-two nature of the parameters self.N = (N if N != 0 and N is not None else rho*L*L) self.width = (L if L != 0 and L is not None else int(math.sqrt(N/rho))) self.height = self.width self.eta = eta # generate the N agents self.particles = [Particle(self.width * U(), self.height * U()) for _ in range(self.N)]
def parametrix_beta(T, gamma, counter, tau_bar): #naiv implementation of the forward method for a simple exemple X_pred = x0 TETA = 1 t = 0 E = -1 * log(U(a=0, b=1)) delta_tau = tau_bar * exp(-(1 - gamma) * E) t += delta_tau Var = 0 Sn = np.array([t]) if (delta_tau > T): counter = counter + 1 X_new = x0 + b(x0) * T + sig(x0) * sqrt(T) * W(0, 1) Sn = np.array([0]) P = pn(tau_bar, Sn, T, gamma) Var = (f(X_new) / P)**2 return f(X_new) / P, Var, counter else: while (t < T): X_new = X_pred + delta_tau * b(X_pred) + sqrt(delta_tau) * W( 0, 1) * sig(X_pred) TETA = TETA * teta(delta_tau, X_pred, X_new) X_pred = X_new E = -1 * log(U(a=0, b=1)) delta_tau = tau_bar * exp(-(1 - gamma) * E) t = t + delta_tau if (t < T): Sn = np.append(Sn, t) delta = T - t + delta_tau last_X = X_pred + delta * b(X_pred) + sqrt(delta) * W(0, 1) * sig(X_pred) TETA = TETA * teta(delta, X_pred, last_X) P = pn(tau_bar, Sn, T, gamma) Var = (f(last_X) * TETA / P)**2 return f(last_X) * TETA / P, Var, counter
def update(self, delta_t): """Generates a new list of particles, computes their velocities, and then does an integration step to determine the new particle's x and y position. """ new_particles = [Particle(0, 0) for _ in range(self.N)] # loop over both lists at once: for (p, P) in zip(self.particles, new_particles): # p is each current particle # P is each new particle neighbors = self.getNeighbors(p, 1) # updating velocity of new particle new_angle = math.atan2(sum(n.yvel for n in neighbors), sum(n.xvel for n in neighbors)) + self.eta*(U()-0.5) P.setVelocityAngleMagnitude(new_angle, 0.03) # numerical integration P.xpos = (p.xpos + P.xvel*delta_t) % self.width P.ypos = (p.ypos + P.yvel*delta_t) % self.height # when finished updating everything, save the new agents and let the GC # handle the old self.particles = new_particles
H = P(f) if H.lower() == g: if D: A(h) for S in D: A(O(S, i)) break I = j for J in G: if H in J.lower(): I = T E.append(J) if not I: A(k) else: K = [A for A in E for B in C if B in A.lower()] if K: B = U(K) else: B = U(E) A(l.format(O(B, m))) V.copy(B) A(n) L = P(X) if o in L: A(p) D.append(B) G.remove(B) if not L: for M in B: if M.lower() in C: C.remove(M.lower()) elif W.lower() == 'french': with Y('touslesmots.txt', b) as R: G = R.read().splitlines()
def gauss(m,s): u = U(a=0,b=1) return inv_gauss(u)*sqrt(s)+m
def cauchy(L): u = U(a=0,b=1) return L*tan(pi*(u-0.5))
def expn(L): u = U(a=0,b=1) return -(1./L)*log(u)
def bern(p=0.5): u = U(a=0,b=1) if u < p: return 1 else: return 0
def gauss_Box_Muller(m,s): R = sqrt(expn(0.5)) u = U(a=0,b=2*pi) return np.array([(R* cos(u))*sqrt(s)+m,(R* sin(u))*sqrt(s)+m])