示例#1
0
文件: main.py 项目: fmgc/abss
def runSimulation(numsteps, case, base, logSpec):
    m = __import__("cases.%s" % case)
    caseEdges = eval("m.%s.EDGES" % case)
    caseNames = eval("m.%s.NAMES" % case)

    s = SPSim()

    max_node = 0
    for e in caseEdges:
        max_node = max(max_node, e[0], e[1])

    for i in range(max_node + 1):
        # ag = RationalAgent( random.random(), profileRandom(), profileRandom() )
        ag = TestAgent(profileRandom(), profileRandom())
        ag.setName(caseNames[i])
        s.addAgent(ag)

    for e in caseEdges:
        s.addNeighborEdge(e[0], e[1])

    logFiles = dict()
    for spec in logSpec:
        logFiles[spec] = file("logs/%s-%s.log" % (base, spec), "w")

    for i in range(numsteps):
        actualStress = s.getStressValues()
        f = file("media/%s%.5d.delta" % (base, i), "w")
        f.write("%s,%s" % (actualStress, s.getActionsDiedges()))
        f.close()

        n = len(actualStress)
        for spec in logSpec:
            if spec == "a":
                logFiles[spec].write("%f\n" % (reduce(lambda x, y: x + y, actualStress.values()) / n))
            elif spec == "b":
                logFiles[spec].write("%f\n" % (min(actualStress.values())))
            elif spec == "c":
                logFiles[spec].write("%f\n" % (max(actualStress.values())))
            elif spec == "d":
                logFiles[spec].write("%f\n" % (actualStress.values()[0]))

        s.step()

    for spec in logSpec:
        logFiles[spec].close()