ich habe ein kleines Problem, da ich eine Variable, welche ich als Funktionsparameter übergebe, wärend der Laufzeit dynamisch je nach zwischenstand verändern möchte. Dies geschieht in folgenden Zeilen :
Code: Alles auswählen
if ((anzahlfitter/len(population))<0.2):
sigma=(0.82*sigma)
else:
sigma=(1.22*sigma)
Traceback (most recent call last):
File "/home/general/Uni/Eaza/rutsche.py", line 136, in <module>
rutsche(19,True,True,1,7,50)
File "/home/general/Uni/Eaza/rutsche.py", line 125, in rutsche
yArray = rekombination(xArray,yArray,pop,generationen)
File "/home/general/Uni/Eaza/rutsche.py", line 92, in rekombination
yArrayTemp[i+1] = mutation(sigma,yArrayTemp[i+1])
UnboundLocalError: local variable 'sigma' referenced before assignment
Mein gesamter Code sieht folgendermassen aus :
Code: Alles auswählen
import random
import math
global sigma
def rutsche(stuetzparam,Kommaoperator,mutieren,sigma,pop,generationen):
if ( stuetzparam > 0):
stuetzpunkte = stuetzparam
else:
stuetzpunkte = 19
yArray = []
xArray = []
hoehe = 20
breite = 40
yArray.append(hoehe)
xArray.append(0)
for i in xrange(stuetzpunkte):
aktuellehoehe = hoehe - hoehe/stuetzpunkte*(i+1)
yArray.append(aktuellehoehe)
aktuellebreite = 0 + (breite/stuetzpunkte)*(i+1)
xArray.append(aktuellebreite)
xArray.append(breite)
yArray.append(0)
print yArray
print xArray
def abstand(x1,x2,y1,y2):
s = math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
return s
def beschleunigung(y1,y2,s):
a = 9.81*(y1-y2)/s
return a
def zeit(v,a,s,y1,y2):
if y2 == y1:
if (v == 0):
t = 1000
else:
t=s/v
else:
if ((v*v+2*a*s)>0):
t=-(v/a)+((math.sqrt(v*v+2*a*s))/a)
else:
t = 1000
return t
def geschwindigkeit(v,a,t):
vneu = v +a*t
return vneu
def fitness(xArray,yArray):
zeitinsec = 0
v = 0.0
for i in xrange(len(xArray)-1):
s = abstand(xArray[i],xArray[i+1],yArray[i],yArray[i+1])
#print s
a = beschleunigung(yArray[i],yArray[i+1],s)
#print a
t = zeit(v,a,s,(yArray[i]),(yArray[i+1]))
#print t
v = geschwindigkeit(v,a,t)
zeitinsec = zeitinsec + t
return zeitinsec
print fitness(xArray,yArray)
def mutation(sigma,y):
#y = y + random.randint(-sigma,sigma)
y = y + random.normalvariate(0,sigma)
if (y > 20):
y =20
return y
def modsigma(faktor,sigma):
newsigma = faktor*sigma
sigma = eval('sigma*faktor')
def rekombination(xArray,yArray,pop,generationen):
population = []
xArrayTemp = xArray
yArrayTemp = list(yArray)
for k in xrange(generationen):
aktuellefitness = fitness(xArray,yArray)
for j in xrange(pop):
for i in xrange(len (yArrayTemp)-2):
# yArrayTemp[i+1] = (yArray[i+1]+yArray[i+1+(random.randint(-1,1))])/2
if (mutieren == True):
yArrayTemp[i+1] = mutation(sigma,yArrayTemp[i+1])
yArrayNeu = list(yArrayTemp)
population.append(yArrayNeu)
yArrayTemp = list(yArray)
population[j] = yArrayTemp
if (Kommaoperator == True):
population.append(yArray)
if (k==0):
for item in population:
print item
fit = 100
fittest = 0
anzahlfitter = 0
for j in xrange(len(population)):
if (fitness(xArray,population[j])<fit):
fit = fitness(xArray,population[j])
if (fit>aktuellefitness):
anzahlfitter = anzahlfitter +1
fittest = j
if (k==0):
print fitness(xArray,population[j])
if ((anzahlfitter/len(population))<0.2):
sigma=(0.82*sigma)
else:
sigma=(1.22*sigma)
yArray = population[fittest]
return yArray
yArray = rekombination(xArray,yArray,pop,generationen)
yArrayRound = []
for item in yArray:
y = int(round(item))
yArrayRound.append(y)
print yArrayRound
print fitness(xArray,yArrayRound)
rutsche(19,True,True,1,7,50)
Gruß Jan