예제 #1
0
def runNelderMead():
    start= time.clock()
    print("1. Nelder-Mead used ",end="")
    from random import random
    triangleCount=0
    a= Vector(DOMAIN_LIMIT*random(), DOMAIN_LIMIT*random()) #someow find a random point
    b=Vector(DOMAIN_LIMIT*random(), DOMAIN_LIMIT*random())
    c=Vector(DOMAIN_LIMIT*random(), DOMAIN_LIMIT*random())
    while triangleCount<MAX_TRIANGLE_COUNT and time.clock()-start<.1:
        if b.dist(a)<.02:#dist btwn a and b
            break
        if a.cost()<b.cost():
            a.swap(b)

        if c.cost()<b.cost():
            c.swap(b)

        if a.cost()<b.cost():
            a.swap(b)

        d=b+c-a
        e=(3*(b+c)-4*a)/2
        f=(3*(b+c)-2*a)/4
        g=(2*a+b+c)/4
        x=f.minVec(g)
        triangleCount+=1
        if d.cost()<a.cost() and e.cost()<a.cost():
            a.equals(e)
        elif d.cost()<a.cost():
            a.equals(d)
        elif x.cost()<a.cost(): a.equals(x)
        else:
            a.equals((a+b)/2)
            c.equals((b+c)/2)

        #if d.cost() <A and E.cost()<A.cost, E isnow A. go to while
        #elif D<A.cost()  A=D. go to while loop
        #elif X= vector (G or F) with min cost. if X.cost()<A.cost(), then A=X
        #else: A=H, C=I. Best is still B. go to #3
    print(triangleCount," random triangles.")
    print("x= ",b[0],end="")
    print(" y=",b[1],end="")
    print(" cost=" ,b.cost())
    timee=time.clock()-start
    print("Search Time= ",timee)
    print("\n")