Пример #1
0
def tryP0(areaPoly=None, t=60):
    fig = plt.figure()
    lst = [0.5, 0.6, 0.7, 0.8]
    data = []
    best = None
    globalOrigin = (596250, 6727996)
    Rini = gr.SqGridGraph(areaPoly=areaPoly, globalOrigin=globalOrigin)
    R = copy.deepcopy(Rini)
    ref = simplified_bruteForce(R)
    print "cost:", cf.totalCost(ref)
    for index, p0 in enumerate(lst):
        R = copy.deepcopy(Rini)
        R, tries = stochastic_several(R, t=t / float(len(lst)), probListGen=ProbListGen(p0, 15))
        if not best or R.cost < best.cost:
            best = R
        data.append(tries)
    mi = min([min(tries) for tries in data] + [ref.cost])
    ma = max([max(tries) for tries in data] + [ref.cost])
    for index, p0 in enumerate(lst):
        ax = fig.add_subplot("%d1%d" % (len(lst), index + 1))
        ax.plot(data[index], "o")
        ax.plot([0, len(data[index])], [ref.cost, ref.cost], "r")  # reference
        ax.set_title("p0=%.1f" % p0)
        ax.set_ylabel("cost")
        ax.set_ylim(mi - 10, ma + 10)
        ax.set_xticklabels([])
    fig = plt.figure()
    ax = fig.add_subplot(111)
    print "best cost:", best.cost
    best.draw(ax=ax)
Пример #2
0
def findBugs(algList):
    """
	algorithms is a list of algorithms that should be tested
	"""
    while True:
        seed = int(random.uniform(0, 1000000))
        print "seed:", seed
        random.seed(seed)
        alg = random.choice(algList)
        print "chosen algorithm:", alg
        dx = random.uniform(-700, 700)
        globalOrigin = 596250 + dx, 6727996
        areaPoly = list(random.uniform(2, 3) * np.array([(0, 0), (48, 0), (73, 105), (0, 96)]))
        for i in range(len(areaPoly)):
            areaPoly[i] = tuple(areaPoly[i])
        R = gr.SqGridGraph(areaPoly=areaPoly, globalOrigin=globalOrigin)
        R = alg(R, aCap=0.2, anim=False)
    print "road area coverage:", go.roadAreaCoverage(R)
    print "internal evaluation of above:", R.areaCover
    print "total area:", fun.polygon_area(areaPoly)
    print "used total area:", R.A, R.Ainv
    print "total cost:", cf.totalCost(R)
Пример #3
0
def tmp(areaPoly=None):
    globalOrigin = 596250, 6727996
    R = gr.SqGridGraph(areaPoly=areaPoly, globalOrigin=globalOrigin)
    simplified_bruteForce(R, aCap=0.2, anim=False)
    c = None
    for i in range(5):
        Rt = copy.deepcopy(R)
        Rt = simplified_bruteForce(Rt, aCap=0.2, anim=False)
        if c:
            assert c == Rt.cost
        c = Rt.cost

    R = simplified_bruteForce(R, aCap=0.2, anim=False)
    # R=stochastic(R,aCap=0.2)
    fig = plt.figure()
    ax = fig.add_subplot(111, aspect="equal")
    R.draw(ax)

    print "road area coverage:", go.roadAreaCoverage(R)
    print "internal evaluation of above:", R.areaCover
    print "total area:", fun.polygon_area(areaPoly)
    print "used total area:", R.A, R.Ainv
    print "total cost:", cf.totalCost(R)
Пример #4
0
def compareAddAndDontAdd():
    dx = random.uniform(-700, 700)
    globalOrigin = 596250 + dx, 6727996

    ls = np.linspace(1.4, 3, 5)
    t1 = []
    t2 = []
    c1 = []
    c2 = []
    el = []

    it = 1
    aCap = 0.20
    for i in ls:
        areaPoly = list(i * np.array([(0, 0), (48, 0), (73, 105), (0, 96)]))
        for itmp in range(len(areaPoly)):
            areaPoly[itmp] = tuple(areaPoly[itmp])

        R = gr.SqGridGraph(areaPoly=areaPoly, globalOrigin=globalOrigin)
        c = 0
        t = 0
        for itmp in range(it):
            tic = time.clock()
            print R.areaCover
            assert R.areaCover > 0.19

            R1 = bruteForce(copy.deepcopy(R), aCap=aCap, add=False)
            R1.cost = cf.totalCost(R1)
            t += time.clock() - tic
            c += R1.cost
        t1.append(t / float(it))
        c1.append(c / float(it))
        el.append(R.elements)

        c = 0
        t = 0
        for itmp in range(it):
            tic = time.clock()
            print R.areaCover
            assert R.areaCover > 0.19
            R2 = bruteForce(copy.deepcopy(R), aCap=aCap, add=True)
            t += time.clock() - tic
            c += cf.totalCost(R2)
        t2.append(t / float(it))
        c2.append(c / float(it))

    fig = plt.figure()
    ax1 = fig.add_subplot(121)
    ax1.plot(el, t1, "b")
    ax1.plot(el, t2, "r")
    ax1.set_title("total time in seconds")
    plt.legend(["without add", "with add"])

    ax2 = fig.add_subplot(122)
    ax2.plot(el, c1, "b")
    ax2.plot(el, c2, "r")
    ax2.set_title("road cost")

    fig2 = plt.figure()
    ax = fig2.add_subplot(121, aspect="equal")
    ax.set_title("without add, cost: %f" % R1.areaCover)
    R1.draw(ax)
    ax2 = fig2.add_subplot(122, aspect="equal")
    ax2.set_title("with add, cost: %f" % R2.areaCover)
    R2.draw(ax2)