Example #1
0
def extend(pts, speedup='try'):
    if holes.count_convex_rholes(pts, 6) != 0:
        print "Initial set has empty hexagons"
    p = datastructures.randPoint(10000000000)
    bestp = p[:]
    pts.append(p)
    emptyRegions = []
    minH = holes.count_convex_rholes(pts, 6, speedup=speedup)
    print "starting with", minH
    pts.pop()
    Ap, Bp = holes.count_convex_rholes_p(p, pts, 6, speedup=speedup)
    regionsChecked = 0
    for pol in pointExplorer.getRandomWalkDFS(p, pts, float('inf')):
        regionsChecked += 1
        print "checking region", regionsChecked
        q = pointExplorer.getCenter(pol)
        if q is None:
            emptyRegions.append(pol)
        else:
            print "trying with", q
            Aq, Bq = holes.count_convex_rholes_p(q, pts, 6, speedup=speedup)
            newH = minH + Aq - Ap + Bp - Bq
            
            if newH <= minH:
                if newH < minH:
                    print "%d points, %d 6 holes"%(len(pts)+1, newH)
                minH = newH
                Ap, Bp = Aq, Bq
                bestp = q[:]
                
                if minH == 0:
                    print "yay!"
                    name = "%d_pts%d_holes%d.pts"%(len(pts)+1, minH, int(time.time()) )
                    pts.append(bestp)
                    f = open(name, "wb")
                    pickle.dump(pts, f)
                    f.close()
                    return pts
    print "Checked", regionsChecked, "best result:", minH, "with", bestp
    return emptyRegions
Example #2
0
def hill_climbing(pts = None, tries = 1000, start=10, t=1000000000, run_time=300, days=0, save_interval = 300, speedup='try'):
    
    if days>0:
        run_time=24*3600*days
        
    start_time = time.time()
    last_save = time.time()
    
    if pts is None:
        pts = [datastructures.randPoint(t) for i in xrange(start)]
    else:
        for i in xrange(start - len(pts)):
            pts.append(datastructures.randPoint(t))
        
#    p = random.choice(pts)
            
#    for i in range(n-len(pts)):
#            if colored: 
#               pts.append([random.randint(-k,k),random.randint(-k,k),random.randint(0,1)])
#            else:
#               pts.append([random.randint(-k,k),random.randint(-k,k)]) 
    minH = holes.count_convex_rholes(pts, 6, speedup=speedup)
    
    while minH == 0:
        pts.append(datastructures.randPoint(t))
        minH = holes.count_convex_rholes(pts, 6, speedup=speedup)    
    
    while time.time()-start_time<run_time:
        minH = holes.count_convex_rholes(pts, 6)
        print "Starting with %d points, %d holes"%(len(pts), minH)
        
        if time.time()-last_save > save_interval:
            Id = str(int(time.time()))+"_%d_pts_%d_h"%(len(pts), minH)
            f = open(Id, "w")
            pickle.dump(pts, f)
            f.close()        
            last_save = time.time()
       
        idx = random.randint(0,len(pts)-1)
        p = pts.pop(idx)
        Ap, Bp = holes.count_convex_rholes_p(p, pts, 6, speedup=speedup)
        
        for pol in pointExplorer.getRandomWalkDFS(p, pts, tries):
            q = pointExplorer.getCenter(pol)
            if q is None:
                continue
            q = [int(q[0]), int(q[1])]
#            pts[idxp] = q
            Aq, Bq = holes.count_convex_rholes_p(q, pts, 6, speedup=speedup)
            newH = minH + Aq - Ap + Bp - Bq
            
            if newH <= minH:
                if newH < minH:
                    print "%d points, %d 6 holes"%(len(pts)+1, newH)
                minH = newH
                Ap, Bp = Aq, Bq
                p = q[:]
                
                if minH == 0:
                    print "yay!"
                    break
        pts.append(p)
        if minH == 0:
           # return pts
            Id = str(int(time.time()))+"_%d_pts_%d_h"%(len(pts), minH)
            f = open(Id, "w")
            pickle.dump(pts, f)
            f.close()
            print pts
            
            pts.append(datastructures.randPoint(t))
                
    print "Done!"
    Id = str(int(time.time()))+"_%d_pts_%d_h"%(len(pts), minH)
    f = open(Id, "w")
    pickle.dump(pts, f)
    f.close()
    return pts