Instantly share code, notes, and snippets.

# Conor LawlessCnrLwlss

• Sort options
Last active Dec 10, 2015
R function for carrying out discrete stochastic simulations of the logistic population model. http://cnr.lwlss.net/DiscreteStochasticLogistic/ #R #Rstats #discrete #stochastic #logistic
View simDSLogistic.R
 simDSLogistic=function(K,r,N0){ # Unusually, for this model, we know the number of events a priori eventNo=K-N0 # So we can just generate all required random numbers (quickly) in one go unifs=runif(eventNo) # Every event produces one cell and consumes one unit of nutrients clist=(N0+1):K # Simulate time between events by generating # exponential random numbers using the inversion method dts=-log(1-unifs)/(r*clist*(1-clist/K))
Last active Dec 10, 2015
Python function for carrying out discrete stochastic simulations of the logistic population model. http://cnr.lwlss.net/DiscreteStochasticLogistic/
View simDSLogistic.py
 import numpy as np def simDSLogistic(K,r,N0): '''Discrete stochastic logistic model simulation. Carrying capacity K, intrinsic growth rate r, initial population size (inoculum) N0. Returns an array with a (continuous) time column and a (discrete) population size column.''' # Unusually, for this model, we know the number of events a priori eventNo=K-N0 # So we can just generate all required random numbers (quickly) in one go unifs=np.random.uniform(size=eventNo)
Last active Dec 4, 2016
Functions for packing N circles into a rectangle of width W and height H, together with a function for plotting solution and some example code fitting 13 circles into a square. http://cnr.lwlss.net/CircleObjectivesR/
View circlePacking.R
 ########### # Optimum circle packing in R ########### # Function closure, sets up list of possible combinations # and returns objective function createObj<-function(N,W,H){ print("Generating combinations...") # Generate all possible circle pairings
Created Jan 19, 2013
Testing different algorithms for solving the constrained optimisation problem: packing circles into a rectangle. http://cnr.lwlss.net/GlobOptR/ #R #optimisation #circle #packing #deoptim #snow #rgenoud
View circlePackingGlobal.R
 library(DEoptim) library(rgenoud) library(snow) # CirclePacking.R from https://gist.github.com/4571810 source("CirclePacking.R") # Number of circles and dimensions of bounding rectangle N=100; W=10; H=10 root="N64_100"
Created Jan 19, 2013
Functions for packing N circles into a rectangle of width W and height H, together with a function for plotting solution and some example code fitting 13 circles into a square. http://cnr.lwlss.net/CircleObjectivesPython/ #python #opimisation #circle #packing #svg #function #closure
View circleObjective.py
 import random, numpy def genGuess(N,W,H): '''Generate sensible initial guess vector (random circle coordinates and radii)''' z=numpy.zeros(3*N) for i in xrange(0,N): z[i*3]=random.random()*W z[i*3+1]=random.random()*H z[i*3+2]=0.001*min(W,H)+random.random()*(0.009*min(W,H)) return(z)
Last active Dec 11, 2015
Demonstrating a function for simulating constrained random walks. Like a discrete Brownian bridge. http://cnr.lwlss.net/ConstrainedRandomWalk
View ConstrainedRandomWalks.R
 # Simulating n steps of a biased random walk # starting from x0 and with decreasing steps # occurring with probability theta randomWalk=function(n=100,x0=0,theta=0.5){ x=x0 res=array(x0,dim=n+1) unifs=runif(n) for(i in 0:(n-1)){ if (unifs[i+1]<=theta){x=x-1}else{x=x+1} res[i+2]=x
Created Feb 19, 2013
R function for carrying out hybrid continuous deterministic / discrete stochastic simulations of the logistic population model. http://cnr.lwlss.net/DiscreteStochasticLogistic/ #R #Rstats #discrete #stochastic #logistic
View simDSLogisticHybrid.R
 simCellsHybrid=function(K,r,N0,NSwitch,detpts=100){ # Every event produces one cell and consumes one unit of nutrients if(NSwitch>N0){ # Unusually, for this model, we know the number of events a priori eventNo=NSwitch-N0 # So we can just generate all required random numbers (quickly) in one go unifs=runif(eventNo) clist=(N0+1):NSwitch # Time between events dts=-log(1-unifs)/(r*clist*(1-clist/K))
Last active Dec 14, 2015