def ellipsoid_points_random(a=1, b=1, c=1, n=1000): pts = np.zeros((n, 3)) i = 0 CC = a * b * c * 1000 while i < n: theta = arccos(urand(-1, 1)) phi = urand(0, 2 * np.pi) y = urand(0, 1) r = R(a, b, c, theta, phi) pp = p(a, b, c, theta, phi) if CC * y <= pp: pts[i, :] = [ r * sin(theta) * np.cos(phi), r * sin(theta) * sin(phi), r * cos(theta) ] i += 1 # print('%.f/%.f'%(i,n)) x = np.column_stack(pts) tris = ch(x.T).simplices tris, normals, centroids = surface_normals(x.T, tris) return x.T, tris, normals, centroids
def tournament_selection(self, k=0.75): pOne, pTwo = rchoice(self.population), rchoice(self.population) # modified to avoid using __cmp__ method of chromosome if urand() < k: return pOne if pOne > pTwo else pTwo else: return pOne if pOne < pTwo else pTwo
def pareto_tournament_selection(self, k=0.75): '''Psuedo-Pareto tournament selection for multi-objective offspring. Offspring that are better in a majority (2/3) of the fitness terms win the tournaments.''' pOne, pTwo = rchoice(self.population), rchoice(self.population) f1 = MATH.array([pOne.fitness,pOne.parsimony,pOne.finitewts]) f2 = MATH.array([pTwo.fitness,pTwo.parsimony,pTwo.finitewts]) if urand() < k: return pOne if MATH.sign((f1-f2)).sum() > 0 else pTwo else: return pOne if MATH.sign((f1-f2)).sum() < 0 else pTwo
def mate(self, parentOne, parentTwo): """Accepts two parents and returns ONE offspring; if the offspring is unchanged from one of the parents, the fitness values are not re-evaluated, just copied. Only one category of mutation is performed on a single offspring.""" # one parent will form the basis for the new offspring; the other is just there for # potential crossover (by default the mother will become the offspring) mother, father = parentOne.copy(), parentTwo.copy() # fitEval will be set to True if any mutations occur that make the offspring different from # its copied parent; this way we avoid unnecessary fitness evaluations fitEval = False # only one category of mutation is allowed per mating event r = urand() if r < self.pC: # parental crossover fitEval = True mNode = rchoice(mother.tree.getNodes()) fNode = rchoice(father.tree.getNodes()) CGAGenerator.single_crossover(mNode,fNode) elif r < self.pC + self.pHC: # headless chicken crossover fitEval = True mNode = rchoice(mother.tree.getNodes()) rNode = rchoice(self.initialize_tree().getNodes()) CGAGenerator.single_crossover(mNode,rNode) elif r < self.pC + self.pHC + self.pM: # point mutation (uses pM/node for mutation prob.) fitEval = True for n in mother.tree.getNodes(): if urand() < self.pM: CGAGenerator.point_mutate(mother.tree, n) fitEval = True elif r < self.pC + self.pHC + self.pM + self.pP: # pruning - guaranteed to do one pruning operation fitEval = True mNode = rchoice(mother.tree.getNodes()) CGAGenerator.prune(mother.tree,mNode) elif r < self.pC + self.pHC + self.pM + self.pP + self.pG: # growth - guaranteed to do one growth op. fitEval = True mTerm = rchoice(mother.tree.getTermini()) CGAGenerator.grow(mother.tree,mTerm) else: # offspring will just be a copy pass if fitEval: mother.fitness,mother.parsimony,mother.finitewts = self.evaluate_fitness(mother.tree) return mother
def _get_ngmix_obs(self, gal_img, psf_img, psf_pars=[0.0, 0.0, -0.01, 0.01, 0.15, 1.0], fit_psf=True): """ """ eps=0.01 lm_pars={'maxfev': 2000, 'xtol': 5.0e-5, 'ftol': 5.0e-5} img_shape = gal_img.shape # Jacobian gal_jacob=ngmix.DiagonalJacobian(scale=self._pixel_scale, x=int((img_shape[0]-1)/2.), y=int((img_shape[1]-1)/2.)) psf_jacob=ngmix.DiagonalJacobian(scale=self._pixel_scale, x=int((img_shape[0]-1)/2.), y=int((img_shape[1]-1)/2.)) # PSF fittitng psf_noise = np.sqrt(np.sum(psf_img**2)) / 500 psf_weight = np.ones_like(psf_img) / psf_noise**2 psf_obs=ngmix.Observation(psf_img, weight=psf_weight, jacobian=psf_jacob) if fit_psf: pfitter=ngmix.fitting.LMSimple(psf_obs,'gauss',lm_pars=lm_pars) guess=np.array(psf_pars) guess[0] += urand(low=-eps,high=eps) guess[1] += urand(low=-eps,high=eps) guess[2] += urand(low=-eps, high=eps) guess[3] += urand(low=-eps, high=eps) guess[4] *= (1.0 + urand(low=-eps, high=eps)) guess[5] *= (1.0 + urand(low=-eps, high=eps)) pfitter.go(guess) # print(np.abs((pfitter.get_result()['g']-np.array([-0.01, 0.01]))/np.array([-0.01, 0.01]))) psf_gmix_fit = pfitter.get_gmix() psf_obs.set_gmix(psf_gmix_fit) # Gal fitting # Get noise level direclty from the image sigma_noise = self.mad(gal_img) noise = np.random.normal(size=img_shape) * sigma_noise weight = np.ones_like(gal_img) * 1/sigma_noise**2. obs = ngmix.Observation(gal_img, weight=weight, noise=noise, jacobian=gal_jacob, psf=psf_obs) return obs
def Tau_scatter(): r = urand(0., 1.) tau_sca = -np.log(r) return tau_sca
def Tau_ffs(tau_path): r = urand(0., 1.) tau_sca = -np.log(1. - (r) * (1. - np.exp(-tau_path))) return tau_sca
def HG_scatter(g): r = (1. - urand(low=(0.), high=(1.))) return 1.0 / (2.0 * g) * ((1.0 + g**2.) - ((1 - g**2.) / (1 - g + 2 * g * r))**2.)
observer[1] = 0.0 observer[2] = scipy.cos(obs_incl) Errorcount = 0 No_interact = 0 #run loop until wanted amount of photon packets simulated while (packet_count < n_packet): #set origin of path back at star O[:] = Star[:] #set packet weight W_packet = 1.0 #generate packet direction phot_phi = 2. * np.pi * urand(low=0., high=1.) phot_theta = urand(low=cos_theta_min, high=cos_theta_max) direction[0] = np.sqrt(1. - phot_theta**2.) * scipy.cos(phot_phi) direction[1] = np.sqrt(1. - phot_theta**2.) * scipy.sin(phot_phi) direction[2] = phot_theta #if emitter is exactly on same z-coord as vertex, see if beam goes #to cells above or below, in case of purely radial beam flip coin, #raytrace detects edge intersects ind_cell[:] = 0 for i in range(0, n_z + 1): if (O[2] == grid_vertex[0, i, 0, 2]): if (phot_theta > 0.0): ind_cell[1] = i