Example #1
0
def findBestNeighbor(point):
  from math import sin,cos,pi
  radius=HILL_CLIMBING_RADIUS
  for t in frange(0, 2*pi, 2*pi/16):
    x=point[0]+radius*cos(t)
    y=point[1]+radius*sin(t)
  bestNeighbor=Vector(x,y)
  if(bestNeighbor.cost()<point.cost()):
    point.equals(bestNeighbor)
  return point
Example #2
0
def runRandomSearch(): #working?
    #evaluate 'a million random solutions and choose the best one
    from random import random
    print("2. Random Probing used", end=" ")
    start= time.clock()
    count=0
    minV=Vector(9999999,999999)
    while time.clock()-start<.1:

        xmin=random()*DOMAIN_LIMIT
        ymin=random()*DOMAIN_LIMIT
        vector= Vector(xmin,ymin)
        minV= minV.minVec(vector)
        count+=1
    print(count, end="")
    print(" probes")
    print("x=",minV[0]," y=",(minV[1]),"cost=",minV.cost())
    print("Time= ",.1)
Example #3
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")