<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Opinion Game - One Topic in the network
These codes were developed as a part of the our opinion game model.
Import Python built-in functions we need to run and plot the game
Step1:  Set up inline matplotlib
This tells Python to plot the figure right in here, in the notebook.
Step2:  Import Game Modules From a Given Path
User have to edit the path and put the correct one on his/her machine.
Step3:  Setting Up Game Parameters
Step4:  seed PRNG
Step5:  Set up the state of the system
State of the system includes
Step6:  User Defined States Can go in the following cell
Step7:  Run The Game
Step8:  Reshape the history
Since here we have just one topic, the matrix can be converted to a 2D array, rather than 3.
This makes the plotting job easier
Step9:  Convert to Panda DataFrame and then plot.
Step10:  Plot the numpy array
<ASSISTANT_TASK:>
Python Code:
import numpy as np
from numpy.random import randn
import pandas as pd
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.image as mpimg
from matplotlib import rcParams
import seaborn as sb
Explanation: Opinion Game - One Topic in the network
These codes were developed as a part of the our opinion game model.
Import Python built-in functions we need to run and plot the game
End of explanation
%matplotlib inline
#rcParams['figure.figsize'] = 5, 4
sb.set_style('whitegrid')
Explanation: Set up inline matplotlib
This tells Python to plot the figure right in here, in the notebook.
End of explanation
import sys
# search path for modules
sys.path.append('/Users/hn/Documents/GitHub/PyOpinionGame/')
import opiniongame.config as og_cfg
import opiniongame.IO as og_io
import opiniongame.coupling as og_coupling
import opiniongame.state as og_state
import opiniongame.adjacency as og_adj
import opiniongame.selection as og_select
import opiniongame.potentials as og_pot
import opiniongame.core as og_core
import opiniongame.stopping as og_stop
import opiniongame.opinions as og_opinions
Explanation: Import Game Modules From a Given Path
User have to edit the path and put the correct one on his/her machine.
End of explanation
config = og_cfg.staticParameters()
path = '/Users/hn/Documents/GitHub/PyOpinionGame/'  # path to the 'staticParameters.cfg'
staticParameters = path + 'staticParameters.cfg'
config.readFromFile(staticParameters)
config.threshold = 0.0001
config.Kthreshold = 0.00001
config.startingseed = 20
config.learning_rate = 0.1
tau = 0.62 #tip of the tent potential function
config.printOut()
Explanation: Setting Up Game Parameters
End of explanation
print("SEEDING PRNG: "+str(config.startingseed))
np.random.seed(config.startingseed)
Explanation: seed PRNG: must do this before any random numbers are ever sampled during default generation
End of explanation
# These are the default matrices for the state of the system:
# If you want to change them, you can generate a new one in the following cell
default_weights = og_coupling.weights_no_coupling(config.popSize, config.ntopics)
default_initialOpinions = og_opinions.initialize_opinions(config.popSize, config.ntopics)
default_adj = og_adj.make_adj(config.popSize, 'full')
Explanation: Set up the state of the system
State of the system includes:
Weight Matrix (Matrix of the coupling wieghts between topic)
Initial Opinions of agents
Adjacency matrix of the network
End of explanation
# user_weights = 
# user_initialOpinions = 
# user_adj = 
state = og_state.WorldState(adj=default_adj, 
                            couplingWeights=default_weights, 
                            initialOpinions=default_initialOpinions, 
                            initialHistorySize=100, 
                            historyGrowthScale=2)
state.validate()
ufuncs = og_cfg.UserFunctions(og_select.FastPairSelection,
                              og_stop.iterationStop,
                              og_pot.createTent(tau))
Explanation: User Defined States Can go in the following cell:
End of explanation
#
# run the game
#
state = og_core.run_until_convergence(config, state, ufuncs)
# Extract and save the history of the game
rdict = {}
rdict['history'] = state.history
og_io.saveMatrix('output.mat', rdict)
Explanation: Run The Game
End of explanation
time, population_size, no_of_topics = rdict['history'].shape
evolution = rdict['history'].reshape(time, population_size)
Explanation: Reshape the history
Since here we have just one topic, the matrix can be converted to a 2D array, rather than 3.
This makes the plotting job easier:
End of explanation
df = pd.DataFrame(data=evolution, index=None, columns=None)
fig = plt.figure()
ax = df.plot(legend=False)
ax.set_xlabel("Time")
ax.set_ylabel("Opinions")
ax.set_title('Evolution of Opinions')
plt.show()
Explanation: Convert to Panda DataFrame and then plot.
End of explanation
fig1 = plt.figure()
plt.plot(evolution)
plt.xlabel('Time')
plt.ylabel('Opinionds')
plt.title('Evolution of Opinions')
plt.show()
Explanation: Plot the numpy array
End of explanation
<END_TASK>