def test_main(): text = """ A = True B = Random C = Random D = Random B* = A or C C* = A and not D D* = B and C """ repeat = 5000 coll = util.Collector() for i in range(repeat): update_progress(i, repeat) model = boolean2.Model(text, mode='async') model.initialize() model.iterate(steps=15) # in this case we take all nodes # one could just list a few nodes such as [ 'A', 'B', 'C' ] nodes = model.nodes # this collects states for each run coll.collect(states=model.states, nodes=nodes) # this step averages the values for each node # returns a dictionary keyed by nodes and a list of values # with the average state for in each timestep avgs = coll.get_averages(normalize=True) # make some shortcut to data to for easier plotting valueB = avgs["B"] valueC = avgs["C"] valueD = avgs["D"] # # plot the state of the nodes # p1, = plt.plot(valueB, 'ob-') p2, = plt.plot(valueC, 'sr-') p3, = plt.plot(valueD, '^g-') plt.legend([p1, p2, p3], ["B", "C", "D"]) plt.show() plt.savefig('t-05_figure.png')
def run(text, nodes, repeat, steps): """ Runs the simulation and collects the nodes into a collector, a convenience class that can average the values that it collects. """ coll = util.Collector() for i in xrange(repeat): engine = Model(mode='async', text=text) engine.RULE_GETVALUE = new_getvalue # minimalist initial conditions, missing nodes set to false engine.initialize(missing=util.false) engine.iterate(steps=steps) coll.collect(states=engine.states, nodes=nodes) print '- completed' avgs = coll.get_averages(normalize=True) return avgs