Example #1
0
def check_fitness(self):
    size = len(world.fitEval)
    fitEval = world.fitEval
    #    i = 0
    #    for fit in fitEval:
    # 	i += 1
    # 	print "%d: %s" %(i,fit)

    for eval in xrange(size - 1):
        before = float(fitEval[eval])
        after = float(fitEval[eval + 1])
        assert after <= before, "In iteration %d fitness %f is lower than %f" % (eval, after, before)
    aspects.without_wrap(measureFitness, world.ssga.updateWorst)
Example #2
0
def plot_function(minimum, maximum, dif):
    def fitness_two(xs,ys):
	[maxi,maxj] = xs.shape
	result = zeros(xs.shape,dtype=float)
	for i in range(maxi):
	    for j in range(maxj):
		x = xs[i,j]
		y = ys[i,j]
		sol = np.array([x,y])
		fit=function_fitness(sol)
		result[i,j] = fit

	return result
    dx = dy = dif
    x = np.arange(minimum, maximum, dx)
    y = np.arange(minimum, maximum, dy)
    X,Y = meshgrid(x, y)
    Z = fitness_two(X, Y)
    global fig
    fig = plt.figure()
    ax = subplot(111)
    x, y = (np.random.rand(2, 50))*100
    ea.initPopulation(50)
    global eval
    eval=0
    population = ea.population()
    x = scale_var(population[:,0])
    y = scale_var(population[:,1])
#    print x
#    print y
#    print "\n"
    global line
    line, = ax.plot(x, y, 'wo')
    im = imshow(Z, cmap=cm.jet)
    im.set_interpolation('bilinear')
    save()
    global numberOfTimeSteps,stepframe
    aspects.with_wrap(measureFitness, SSGA.updateWorst)
    ea.run(numberOfTimeSteps*stepframe)
    aspects.without_wrap(measureFitness, SSGA.updateWorst)
    # Clear the figure to make way for the next image.
    plt.clf()
finally: print verdict,test

test,verdict = "without wrap: removing arbitrary wraps",FAIL
try:
    def wrapme(x): return "wrapme" + x
    def w1(x): yield aspects.proceed("w1" + x)
    def w2(x): yield aspects.proceed("w2" + x)
    def w3(x): yield aspects.proceed("w3" + x)
    def w4(x): yield aspects.proceed("w4" + x)

    for w in (w1, w2, w4, w3, w4): aspects.with_wrap(w, wrapme)
    # check that everything is fine
    assert wrapme("") == "wrapmew1w2w4w3w4", "preparation failed"
    
    # remove the topmost wrap
    aspects.without_wrap(w4, wrapme)
    assert wrapme("") == "wrapmew1w2w4w3", "topmost"

    # remove the wrap in the middle
    aspects.without_wrap(w4, wrapme)
    assert wrapme("") == "wrapmew1w2w3", "middle"
    
    # remove the wrap on the bottom
    aspects.without_wrap(w1, wrapme)
    assert wrapme("") == "wrapmew2w3", "bottom"

    # remove the rest wraps
    aspects.without_wrap(w2, wrapme)
    aspects.without_wrap(w3, wrapme)
    assert wrapme("") == "wrapme", "rest"
    def w2(x):
        yield aspects.proceed("w2" + x)

    def w3(x):
        yield aspects.proceed("w3" + x)

    def w4(x):
        yield aspects.proceed("w4" + x)

    for w in (w1, w2, w4, w3, w4):
        aspects.with_wrap(w, wrapme)
    # check that everything is fine
    assert wrapme("") == "wrapmew1w2w4w3w4", "preparation failed"

    # remove the topmost wrap
    aspects.without_wrap(w4, wrapme)
    assert wrapme("") == "wrapmew1w2w4w3", "topmost"

    # remove the wrap in the middle
    aspects.without_wrap(w4, wrapme)
    assert wrapme("") == "wrapmew1w2w3", "middle"

    # remove the wrap on the bottom
    aspects.without_wrap(w1, wrapme)
    assert wrapme("") == "wrapmew2w3", "bottom"

    # remove the rest wraps
    aspects.without_wrap(w2, wrapme)
    aspects.without_wrap(w3, wrapme)
    assert wrapme("") == "wrapme", "rest"