Skip to content

Instantly share code, notes, and snippets.

@Krewn
Created October 29, 2014 22:42
Show Gist options
  • Save Krewn/6f34425575566a8c7c88 to your computer and use it in GitHub Desktop.
Save Krewn/6f34425575566a8c7c88 to your computer and use it in GitHub Desktop.
A basic paradigm for genetic algorithms.
# ___ ____ _ _ ____ ____ ____ ___ __ __ ___
# / __)( ___)( \( )( ___)(_ _)(_ _)/ __) /__\ ( ) / __)
#( (_-. )__) ) ( )__) )( _)(_( (__ /(__)\ )(__( (_-.
# \___/(____)(_)\_)(____) (__) (____)\___) (__)(__)(____)\___/
#
from pylab import *
def QuickerPlot(x):
close()
y=[]
for k in range(1,len(x)+1):
y.append(k)
plot(y,x)
xlabel('x')
ylabel('y')
grid(True)
formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
ax = subplot(111)
ax.yaxis.set_major_formatter(formatter)
ax.xaxis.set_major_formatter(formatter)
show()
def ran():
import random
r = float(random.random())
r = (r-0.5)*2.0
return(r)
ans=[]
a = 0
for k in range(0,10,1):
a += ran()
print 'Pure random 10 itterations: ' + str(a)
ans.append(a)
a = 0
for k in range(0,10,1):
t1 = ran()
t2 = ran()
a += t1 if t1>=t2 else t2
print 'genetic algorithm degree 2, 10 itterations: ' + str(a)
ans.append(a)
for k in range(3,10):
a=0
for k2 in range(0,10):
t=[]
for k3 in range(0,k):
t.append(ran())
a+=max(t)
print 'genetic algorithm degree '+str(k)+', 10 itterations: ' + str(a)
ans.append(a)
QuickerPlot(ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment