Initiate and Save 1D Chain

home > Research
  1. Initiate and Save 1D Chain
  2. Load 1D Chain and Plot

This tutorial sets up a 1D chain automatically using a function in the kero.multib library. This is a simple system of network (well, a one dimensional chain) consisting of 5 members: “1”, “2”, “3”, “4” and “5” linked to form a chain. The system will run over 100 time steps. At each time step, each member performs action P with 75% probability and Q with 25% probability.

1. Set up system

Use build_1D_chain() to create a 1D chain of 5 individual members. Then attach a probability vector field specifying the available actions P and Q for each member, with their corresponding probabilities of occurring at each time step, respectively 0.75 and 0.25. It is important to set action_index_set. This variable is nothing but an alternative, numerical representation of the available actions. Here we assign P to 0 and Q to 1. It will be very convenient, and thus we have build up functions based on this convention.

We will often denote the member as v, and these members “1”, …, “5” are stored in the network as the dictionary keys to Network object’s property member_set. For each key, the values in the dictionary are the members of of the network linked to it. For example, in this chain of 5 members, “1” is linked to “2”, so member_set[“1”] is [“2”].   Another example, since “3” is linked to “4” and “5”, we have member_set[“3”] = [“4″,”5”]. To see this, uncomment # explanation 1 and run the code.

Check point: If we set the available actions to [a1, a2, a3, …, aN], make sure that action_index_set is set to [0, 1, 2, … , N-1] in this order.  Since it is convenient, many functions have been built upon this convention.

Let us save the whole code here in as


The code imports from here.

import kero.multib.nDnet as nd
import matplotlib.pyplot as plt
from peripheral import *
import os

if not os.path.exists("save_folder"):

# --------------------------------------------------------
# 1. Set up system
# --------------------------------------------------------
print("Explanation 1:\n", chain.member_set) # explanation 1

def attach_a_probability_vector_field():
	chain.set_of_fields["prob"] = {}
	action_set = ["P","Q"] 
	action_index_set = [0,1] 
	action_probability_set = [0.75,0.25] 
	for v in chain.member_set: 
		chain.set_of_fields["prob"][v] = [action_set,action_probability_set]
	return action_set, action_index_set # 6.

action_set, action_index_set=attach_a_probability_vector_field()
# print(chain.set_of_fields["prob"])

2. Run Simulation

Here we set through variable Tset the length of simulation, 100 steps.

The variable action_history stores the actions of network members as a dictionary data whose keys identify the members. For example,  action_history[“1”] might be [[P, 0], [P, 0], [ Q, 1], … ]. This means that member “1” performed actions P in the first two steps and Q at the third. To see this, uncomment # explanation 2.

The variable field_history stores the evolved field(s) in the order of time steps [field_now1, field_now2,…]. For each field_now in field_history, it is a dictionary and we only set field_now[“prob”] because in this example we only attach a single field called “prob” to the network. This field specifies the probability that one of the specified available actions P or Q will occur at a time step. But it has to be specified for every member of network, hence we set field_now[“prob”][v] where v is a member. To see this, uncomment # explanation 3. Note that the field remains the same over time, since we do not introduce any interaction. (continued…)

# --------------------------------------------------------
# 2. Run Simulation
# --------------------------------------------------------
start1 = time.time()
Tset=range(100) # Simulation length
action_history = {}
field_history = []

for v in chain.member_set:

for v in chain.member_set:
for t in Tset: # [0]: # 
	for v in chain.member_set:
		this_action_set = chain.set_of_fields["prob"][v][0][:] # eg [a1,a2]
		this_probability_set = chain.set_of_fields["prob"][v][1][:] # eg [0.5,0.5]
		action,action_index = nd.choose_action_continuous_probability(this_action_set, this_probability_set, N=1)
	# ---------------------------------------
	# Add interaction in this region
	# ---------------------------------------
	# ** No interaction in this example

	# ---------------------------------------
	# Store evolved field state
	# ---------------------------------------
	for v in chain.member_set:
	# print(field_now)

# print("Explanation 2:\n") # explanation 2
# for v in action_history:
#     print(v, " : [",end="")
#     for i in range(5):
#     	print(action_history[v][i], ", ", end="")
#     print("... ]")

# print("Explanation 3:\n") # explanation 3
# for i in range(5):
# 	temp=field_history[i]['prob']
# 	for k in temp:
# 		print(k, " : ", temp[k])
# 	print("--------------------------")


start2 = time.time()

3. Save State

Save data that we have generated. To keep things organized, we save the network as an object containing only members and links, processor as the object that stores data derived directly from the simulation of the network interaction over time, while the SAVE_POINT object saves any other data. (continued…)

# --------------------------------------------------------
# 3. Save State
# --------------------------------------------------------
import pickle
spt = nd.SAVE_POINT(){
	# Save any more data here
	"N": N, # chain length
	"mean_over_N": mean_over_N,
	"interval_mean_history": interval_mean_history,

	"network" : chain,
	"processor" : processor,
	"SAVEPOINT" : spt

f = open("save_folder/test_beta1.pckl","wb")

We can proceed to the next part, where we will load these data in another script and plot them.
Simulation is done using kero version: 0.5.1.