Esempio n. 1
0
def radRatio(inequalDict):
    chebRad = randomWalk.chebyshev(inequalDict)[1]
    cheb = randomWalk.chebyshev(inequalDict)[0]

    maxRad = -sys.maxint
    for l in inequalDict.values():
        for inequal in l:
            r = inequal[2] - cheb[inequal[1]] + cheb[inequal[0]]
            maxRad = max(r, maxRad)

        return chebRad / maxRad
Esempio n. 2
0
def avgMinRadRatio(inequalDict):
    chebRad = randomWalk.chebyshev(inequalDict)[1]
    cheb = randomWalk.chebyshev(inequalDict)[0]

    radSum = 0.0
    for l in inequalDict.values():
        currRad = sys.maxint
        for inequal in l:
            r = inequal[2] - cheb[inequal[1]] + cheb[inequal[0]]
            currRad = min(r, currRad)
        radSum += currRad

    avgRad = radSum / len(inequalDict.values())

    return chebRad / avgRad
Esempio n. 3
0
def avgRadRatio(inequalDict):
    chebRad = randomWalk.chebyshev(inequalDict)[1]
    cheb = randomWalk.chebyshev(inequalDict)[0]

    radSum = 0.0
    count = 0
    for l in inequalDict.values():
        for inequal in l:
            r = inequal[2] - cheb[inequal[1]] + cheb[inequal[0]]
            radSum += r
            count += 1

    avgRad = radSum / count

    return chebRad / avgRad
Esempio n. 4
0
def uniformRandSched(S, numPoints=100):
    pointList = []

    # start at arbitrary point, we take cheb
    curr = randomWalk.chebyshev(S)[0]
    # print curr
    curr = [0] + [coord - curr[0] for coord in curr[1:]]
    # print curr
    # retrieve a list of the constraints
    constraints = getList(S)
    # go through the number of points to cycle through
    i = 0
    while i < numPoints + 100:
        # set up a vector in a random direction
        vector = [0 for n in range(len(S.keys()))]

        for j in range(1, len(vector)):
            vector[j] = np.random.normal(0, 0.1, 1)[0]
        # print vector
        # calculate magnitude of this vector]
        magnitude = 0.0
        for j in range(1, len(vector)):
            magnitude += vector[j]**2
        magnitude = magnitude**0.5
        # print magnitude
        for j in range(1, len(vector)):
            vector[j] /= magnitude
        # print vector

        # initialize thetas for determining the lower and upper bounds for traveling along vector
        theta_pos = 10000
        theta_neg = -10000
        # loop through the bounds and calculate distance to each, updating thetas as we go, to get bounds
        for bound in constraints:
            const = bound[2]
            start = bound[0]
            end = bound[1]
            dist_to_bound = (const -
                             (curr[end] - curr[start])) / (vector[end] -
                                                           vector[start])
            if dist_to_bound > 0: theta_pos = min(theta_pos, dist_to_bound)
            if dist_to_bound < 0: theta_neg = max(theta_neg, dist_to_bound)
        # print theta_pos
        # print theta_neg
        # print vector
        # initialize next point
        # new_point = [0 for n in range(len(curr))]
        new_point = copy.deepcopy(curr)
        # uniformly choose displacement along vector and calculate the appropriate point
        displacement = np.random.uniform(theta_neg, theta_pos)
        for j in range(1, len(vector)):
            new_point[j] += vector[j] * displacement

        curr = new_point
        if i > 99:
            pointList.append(curr)
        i += 1
        # print 'i = ' + str(i)

    return pointList
Esempio n. 5
0
def mpRandomShaver(tup):
    genstn = tup[0]
    numShaves = tup[1]
    numRandos = tup[2]

    cent = randomSchedule.centroid(genstn)
    cheb = randomWalk.chebyshev(genstn)[0]
    mid = randomSchedule.midPoint(genstn)

    centAvg = 0.0
    chebAvg = 0.0
    randoAvg = 0.0
    midAvg = 0.0

    for i in range(numRandos):
        rando = randomSchedule.simpleRandSched(genstn)

        for j in range(numShaves):
            randoAvg += randomShave(genstn, rando)

    randoAvg /= (numRandos * numShaves)

    for j in range(numShaves):
        centAvg += randomShave(genstn, cent)
        chebAvg += randomShave(genstn, cheb)
        midAvg += randomShave(genstn, mid)

    centAvg /= float(numShaves)
    chebAvg /= float(numShaves)
    midAvg /= float(numShaves)
    print "SHAAAVVVVEEEE " + str(tup[3])
    return (chebAvg, centAvg, midAvg, randoAvg)
Esempio n. 6
0
def mpPointBrawl(tup):
    genstn = tup[0]
    dim = len(genstn.keys()) - 1

    sphereMet = metricFunctions.sphericalMetric(genstn, dim, "spherical")
    burgerMet = metricFunctions.sphericalMetric(
        genstn, dim, "burgers") / (dim * (dim - 1) / 2)
    wilsonMet = metricFunctions.sphericalMetric(genstn, dim, "wilson") / dim
    radRatio = metricFunctions.sphericalMetric(genstn, dim, "radRatio")
    naiveMet = metricFunctions.sphericalMetric(genstn, dim, "hell")

    cheb = randomWalk.chebyshev(genstn)[0]
    cent = centroid(genstn)
    birdie = earlyBird(genstn)
    mid = midPoint(genstn)

    randAvg = 0.0
    randAvgRay = 0.0
    randAvgRayPos = 0.0
    #randAvgShaves = 0.0

    for i in range(100):
        curr = simpleRandSched(genstn)

        for j in range(numWalks):
            randAvg += randomWalk.perturb(genstn, curr, 1, 0, 0)
            randAvgRay += randomWalk.perturb(genstn, curr, 0, 0, 1)
            randAvgRayPos += randomWalk.perturb(genstn, curr, 0, 1, 1)
            #andAvgShaves += randomMelt.randomShave(genstn, curr)

    randAvg /= i * j
    randAvgRay /= i * j
    randAvgRayPos /= i * j
    #randAvgShaves /= i*j

    chebAvg = 0.0
    chebAvgRay = 0.0
    chebAvgRayPos = 0.0
    #chebAvgShaves = 0.0

    centAvg = 0.0
    centAvgRay = 0.0
    centAvgRayPos = 0.0
    #centAvgShaves = 0.0

    birdAvg = 0.0
    birdAvgRay = 0.0
    birdAvgRayPos = 0.0
    #birdAvgShaves = 0.0

    midAvg = 0.0
    midAvgRay = 0.0
    midAvgRayPos = 0.0
    #midAvgShaves = 0.0

    for i in range(numWalks):
        chebAvg += randomWalk.perturb(genstn, cheb, 1, 0, 0)
        chebAvgRay += randomWalk.perturb(genstn, cheb, 0, 0, 1)
        chebAvgRayPos += randomWalk.perturb(genstn, cheb, 0, 1, 1)
        #chebAvgShaves += randomMelt.randomShave(genstn, cheb)

        centAvg += randomWalk.perturb(genstn, cent, 1, 0, 0)
        centAvgRay += randomWalk.perturb(genstn, cent, 0, 0, 1)
        centAvgRayPos += randomWalk.perturb(genstn, cent, 0, 1, 1)
        #centAvgShaves += randomMelt.randomShave(genstn, cent)

        birdAvg += randomWalk.perturb(genstn, birdie, 1, 0, 0)
        birdAvgRay += randomWalk.perturb(genstn, birdie, 0, 0, 1)
        birdAvgRayPos += randomWalk.perturb(genstn, birdie, 0, 1, 1)
        #birdAvgShaves += randomMelt.randomShave(genstn, bird)

        midAvg += randomWalk.perturb(genstn, mid, 1, 0, 0)
        midAvgRay += randomWalk.perturb(genstn, mid, 0, 0, 1)
        midAvgRayPos += randomWalk.perturb(genstn, mid, 0, 1, 1)
        #midAvgShaves += randomMelt.randomShave(genstn, mid)

    chebAvg /= numWalks
    chebAvgRay /= numWalks
    chebAvgRayPos /= numWalks
    #chebAvgShaves /= numWalks

    centAvg /= numWalks
    centAvgRay /= numWalks
    centAvgRayPos /= numWalks
    #centAvgShaves /= numWalks

    birdAvg /= numWalks
    birdAvgRay /= numWalks
    birdAvgRayPos /= numWalks
    #birdAvgShaves /= numWalks

    midAvg /= numWalks
    midAvgRay /= numWalks
    midAvgRayPos /= numWalks
    #midAvgShaves /= numWalks

    print "TRIAL!! " + str(tup[1])

    return [
        genstn, dim, sphereMet, burgerMet, wilsonMet, radRatio, naiveMet,
        chebAvg, centAvg, birdAvg, midAvg, randAvg, chebAvgRay, centAvgRay,
        birdAvgRay, midAvgRay, randAvgRay, chebAvgRayPos, centAvgRayPos,
        birdAvgRayPos, midAvgRayPos, randAvgRayPos
    ]
Esempio n. 7
0
def pointBrawl(numTrials,
               dim,
               manWalk=False,
               posTime=False,
               randDim=False,
               multi=False,
               ray=False):

    chebMargin = 0
    centMargin = 0
    birdMargin = 0
    chebVCentMargin = 0
    chebVBirdMargin = 0
    centVBirdMargin = 0

    # Matric to store all the different quantities we may want to compute in a
    # simulation. The numbers on the right refer to indices of the matrix
    lol = [
        [],  # sphereMetric          0
        [],  # burgerMetric          1
        [],  # wilsonMetric          2
        [],  # radRatio              3
        [],  # naiveMetric           4
        [],  # chebMargin            5
        [],  # centMargin            6
        [],  # birdMargin            7
        [],  # chebVCentMargin       8
        [],  # chebVBirdMargin       9
        [],  # centVBirdMargin       10
        [],  # randomWalkCheb        11
        [],  # randomWalkCent        12
        [],  # randomWalkBird        13
        [],  # randomWalkManyPoints  14
        [],  # dimension             15
        [],  # STNs.                 16
    ]

    # Run the point arena on numTrials number of STNs
    procPool = mp.Pool(2)
    for i in range(numTrials):
        print "\nTrial " + str(i)
        chebAvg = 0
        centAvg = 0
        birdAvg = 0
        randAvg = 0

        numWalks = 100

        # Randomly choose the dimension between 2 and 10 events
        if randDim:
            dim = random.randint(2, 10)

        # generate STN with dim events, each constrained by 0 to 50
        genstn = stnConverter.matrixToDict(test.generateRandomSTN(dim, 0, 50))

        # minimize the STN because why not
        genstn = stnConverter.matrixToDict(
            test.minimal(stnConverter.dictToMatrix(genstn)))

        lol[15].append(dim)
        lol[16].append(genstn)

        # Metrics for analyzing an STN depending on what we are looking for.
        # This could be how "spherelike" the solution space is,
        sphereMet = metricFunctions.sphericalMetric(genstn, dim, "spherical")
        burgerMet = metricFunctions.sphericalMetric(genstn, dim, "burgers")
        wilsonMet = metricFunctions.sphericalMetric(genstn, dim, "wilson")
        radRatio = metricFunctions.sphericalMetric(genstn, dim, "radRatio")
        naiveMet = metricFunctions.sphericalMetric(genstn, dim, "hell")

        # Compute the possible "centers", or points we think may be optimal
        cheb = randomWalk.chebyshev(genstn)[0]
        cent = centroid(genstn)
        birdie = earlyBird(genstn)

        # Append the computed metric values to their respective rows
        lol[0].append(sphereMet)
        lol[1].append(burgerMet)
        lol[2].append(wilsonMet)
        lol[3].append(radRatio)
        lol[4].append(naiveMet)

        # Take the average of 100 randomWalks using 100 different points
        if not multi:
            for i in range(100):
                curr = simpleRandSched(genstn)
                for i in range(numWalks):
                    randAvg += randomWalk.perturb(genstn, curr, not ray,
                                                  posTime, ray)
        else:
            tuplist = [(genstn, numWalks, posTime, ray)] * 100
            results = procPool.map(mpRandoRandomWalker, tuplist)
            randAvg = sum(results)

        randAvg /= 100 * float(numWalks)

        # Take the average of 100 randomWalks starting at each "center"
        if not multi:
            for i in range(numWalks):
                chebAvg += randomWalk.perturb(genstn, cheb, not ray, posTime,
                                              ray)
                centAvg += randomWalk.perturb(genstn, cent, not ray, posTime,
                                              ray)
                birdAvg += randomWalk.perturb(genstn, birdie, not ray, posTime,
                                              ray)
        else:
            tup = [(genstn, cheb, cent, birdie, posTime, ray)] * numWalks
            results = procPool.map(mpCenterRandomWalker, tup)
            chebAvg = sum([res[0] for res in results])
            centAvg = sum([res[1] for res in results])
            birdAvg = sum([res[2] for res in results])

        chebAvg /= float(numWalks)
        centAvg /= float(numWalks)
        birdAvg /= float(numWalks)

        print "cheb " + "Average: " + str(chebAvg)
        print "cent " + "Average: " + str(centAvg)
        print "bird " + "Average: " + str(birdAvg)
        print "rand " + "Average: " + str(randAvg)

        # Compute the different margins of the centers vs each other
        if chebAvg == 0 and centAvg == 0:
            chebVCentMargin = 0
        else:
            chebVCentMargin = 2 * (chebAvg - centAvg) / (chebAvg + centAvg)
        if chebAvg == 0 and birdAvg == 0:
            chebVBirdMargin = 0
        else:
            chebVBirdMargin = 2 * (chebAvg - birdAvg) / (chebAvg + birdAvg)
        if centAvg == 0 and centAvg == 0:
            centVBirdMargin = 0
        else:
            centVBirdMargin = 2 * (centAvg - birdAvg) / (centAvg + birdAvg)

        # Compute the different margins of the centers vs random points
        if chebAvg == 0 and randAvg == 0:
            chebMargin = 0
        else:
            chebMargin = 2 * (chebAvg - randAvg) / (chebAvg + randAvg)
        if centAvg == 0 and randAvg == 0:
            centMargin = 0
        else:
            centMargin = 2 * (centAvg - randAvg) / (centAvg + randAvg)
        if birdAvg == 0 and randAvg == 0:
            birdMargin = 0
        else:
            birdMargin = 2 * (birdAvg - randAvg) / (birdAvg + randAvg)

        # Append the computed margins to their respective rows
        lol[5].append(chebMargin)
        lol[6].append(centMargin)
        lol[7].append(birdMargin)
        lol[8].append(chebVCentMargin)
        lol[9].append(chebVBirdMargin)
        lol[10].append(centVBirdMargin)

        # Append the computed randomWalk lengths starting at each center to
        # their respective rows
        lol[11].append(chebAvg)
        lol[12].append(centAvg)
        lol[13].append(birdAvg)
        lol[14].append(randAvg)

        print "SPHEERE " + str(sphereMet)
        print "BURGERS " + str(burgerMet)
        print "WILSOON " + str(wilsonMet)
        print "RAAAADD " + str(radRatio)
        print "NAIIIVE " + str(naiveMet)

    title = "THE_GRAND_BATTLE_ON_"
    if randDim:
        title += "MANY"
    else:
        title += str(dim)
    title += "_DIMENSIONS_"
    title += "AND_" + str(numTrials) + "_MILLION_TRIALS"
    if posTime:
        title += "_AS_TIME_MOVES_FORWARD!"
    if ray:
        title += "...IN A RAY!"

    # Code to store data in a json file
    with open(os.path.abspath('./Data/' + title + '.json'), 'w') as fp:
        json.dump(lol, fp)

    return lol
Esempio n. 8
0
def distMetric(S, dim):
    one = randomWalk.chebyshev(S)[0]
    cent = centroid(S)
    return randomWalk.distance(one, cent)
Esempio n. 9
0
def sphereVol(S, dim):
    chebsph = randomWalk.chebyshev(S)
    return test.sphereVolume(float(chebsph[1]), dim) / (
        test.naiveVolFlex(stnConverter.dictToMatrix(S)) + 0.00000001)
Esempio n. 10
0
def spherical(STN, dim):
    rad = randomWalk.chebyshev(STN)[1]
    return sphereVolume(rad, dim)**(1.0 / dim)
Esempio n. 11
0
def mpHelper(tup):
    genstn = tup[0]
    numWalks = tup[1]
    dim = len(genstn.keys()) - 1

    # Compute the STN flexibility metric values for the current STN
    # Hunsberger, Wilson, and Naive are all adjusted to account for the
    # different dimensions
    sphereMet = metricFunctions.sphericalMetric(genstn, dim, "spherical")
    burgerMet = metricFunctions.sphericalMetric(
        genstn, dim, "burgers") / (dim * (dim - 1) / 2)
    wilsonMet = metricFunctions.sphericalMetric(genstn, dim, "wilson") / dim
    naiveMet = metricFunctions.sphericalMetric(genstn, dim, "hell") / dim

    # Compute the centers of interest for the current STN
    cheb = randomWalk.chebyshev(genstn)[0]
    cent = randomSchedule.centroid(genstn, 100)
    avgMid = randomSchedule.jimPoint(genstn)

    # Lists to hold the disturbance lengths
    randWalks = []
    randShaves = []

    # Lists to hold the durability metric values
    randclosevals = []
    randfurthvals = []
    # randcubevals = []
    randgeovals = []

    # Compute the disturbance lengths and durability metric values for 100
    # random schedules in the STN
    pointList = randomMelt.uniformRandSched(genstn)

    for i in range(100):
        walk = 0.0
        shave = 0.0
        # curr = randomSchedule.simpleRandSched(genstn)
        # print len(pointList)
        curr = pointList[i]

        randclosevals.append(schedMetrics.sphereMet(genstn, curr))
        randfurthvals.append(randomSchedule.furthestBoundary(genstn, curr))
        # randcubevals.append(schedMetrics.wilsonCubeMet(genstn, curr))
        randgeovals.append(schedMetrics.geoMet(genstn, curr))

        for j in range(numWalks):
            walk += randomWalk.perturb(genstn, curr, 1, 0, 0)
            shave += randomMelt.randomShave(genstn, curr)

        walk /= numWalks
        shave /= numWalks

        randWalks.append(walk)
        randShaves.append(shave)

    # Compute averages for the disturbance lengths and durability metric
    # values for the 100 STNs
    randAvg = sum(randWalks) / len(randWalks)
    randAvgShaves = sum(randShaves) / len(randShaves)
    randClose = sum(randclosevals) / len(randclosevals)
    randFurth = sum(randfurthvals) / len(randfurthvals)
    # randCube = sum(randcubevals)/len(randcubevals)
    randGeo = sum(randgeovals) / len(randgeovals)

    # Compute correlation coefficients for the disturbance lengths vs each of
    # our durability metrics
    try:
        walk_close_corr = np.corrcoef(randclosevals, randWalks)[1][0]
    except:
        walk_close_corr = 0.0
    try:
        walk_furth_corr = np.corrcoef(randfurthvals, randWalks)[1][0]
    except:
        walk_furth_corr = 0.0
    # try:
    #     walk_cube_corr = np.corrcoef(randcubevals, randWalks)[1][0]
    # except:
    #     walk_cube_corr = 0.0
    try:
        walk_geo_corr = np.corrcoef(randgeovals, randWalks)[1][0]
    except:
        walk_geo_corr = 0.0

    try:
        shave_close_corr = np.corrcoef(randclosevals, randShaves)[1][0]
    except:
        shave_close_corr = 0.0
    try:
        shave_furth_corr = np.corrcoef(randfurthvals, randShaves)[1][0]
    except:
        shave_furth_corr = 0.0
    # try:
    #     shave_cube_corr = np.corrcoef(randcubevals, randShaves)[1][0]
    # except:
    #     shave_cube_corr = 0.0
    try:
        shave_geo_corr = np.corrcoef(randgeovals, randShaves)[1][0]
    except:
        shave_geo_corr = 0.0

    # walk_close_corr = 0.0
    # walk_cube_corr = 0.0
    # walk_geo_corr = 0.0
    # shave_close_corr = 0.0
    # shave_cube_corr = 0.0
    # shave_geo_corr = 0.0
    if math.isnan(walk_close_corr):
        walk_close_corr = 0.0
    if math.isnan(walk_furth_corr):
        walk_furth_corr = 0.0
    if math.isnan(walk_geo_corr):
        walk_geo_corr = 0.0

    if math.isnan(shave_close_corr):
        shave_close_corr = 0.0
    if math.isnan(shave_furth_corr):
        shave_furth_corr = 0.0
    if math.isnan(shave_geo_corr):
        shave_geo_corr = 0.0

    # Placeholders for disturbance lengths for centers of interest
    chebAvg = 0.0
    chebAvgShaves = 0.0

    centAvg = 0.0
    centAvgShaves = 0.0

    midAvg = 0.0
    midAvgShaves = 0.0

    # Compute disturbance lengths for centers of interest multiple times
    for i in range(numWalks):
        chebAvg += randomWalk.perturb(genstn, cheb, 1, 0, 0)
        chebAvgShaves += randomMelt.randomShave(genstn, cheb)

        centAvg += randomWalk.perturb(genstn, cent, 1, 0, 0)
        centAvgShaves += randomMelt.randomShave(genstn, cent)

        midAvg += randomWalk.perturb(genstn, avgMid, 1, 0, 0)
        midAvgShaves += randomMelt.randomShave(genstn, avgMid)

    # Compute average disturbance lengths for centers of interest
    chebAvg /= numWalks
    chebAvgShaves /= numWalks

    centAvg /= numWalks
    centAvgShaves /= numWalks

    midAvg /= numWalks
    midAvgShaves /= numWalks

    # Compute durability metric values for centers of interest
    cheb_close = schedMetrics.sphereMet(genstn, cheb)
    cent_close = schedMetrics.sphereMet(genstn, cent)
    mid_close = schedMetrics.sphereMet(genstn, avgMid)
    cheb_furth = randomSchedule.furthestBoundary(genstn, cheb)
    cent_furth = randomSchedule.furthestBoundary(genstn, cent)
    mid_furth = randomSchedule.furthestBoundary(genstn, avgMid)
    # cheb_cube = schedMetrics.wilsonCubeMet(genstn, cheb)
    # cent_cube = schedMetrics.wilsonCubeMet(genstn, cent)
    # mid_cube = schedMetrics.wilsonCubeMet(genstn, avgMid)
    cheb_geo = schedMetrics.geoMet(genstn, cheb)
    cent_geo = schedMetrics.geoMet(genstn, cent)
    mid_geo = schedMetrics.geoMet(genstn, avgMid)

    print "TRIAL!! " + str(tup[2])

    return [
        genstn, dim, cheb, cent, avgMid, sphereMet, burgerMet, wilsonMet,
        naiveMet, randAvg, randAvgShaves, randClose, randFurth, randGeo,
        walk_close_corr, walk_furth_corr, walk_geo_corr, shave_close_corr,
        shave_furth_corr, shave_geo_corr, chebAvg, chebAvgShaves, centAvg,
        centAvgShaves, midAvg, midAvgShaves, cheb_close, cent_close, mid_close,
        cheb_furth, cent_furth, mid_furth, cheb_geo, cent_geo, mid_geo
    ]