These codes were developed as a part of the our opinion game model.
In [49]:
import numpy as np
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
In [50]:
%matplotlib inline
rcParams['figure.figsize'] = 5, 4
sb.set_style('whitegrid')
In [51]:
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
In [77]:
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 = 11
config.learning_rate = 0.1
tau = 0.62 #tip of the tent potential function
config.printOut()
In [78]:
# seed PRNG: must do this before any random numbers are ever sampled during default generation
In [79]:
print("SEEDING PRNG: "+str(config.startingseed))
np.random.seed(config.startingseed)
In [80]:
# 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')
In [81]:
config.ntopics = 2
config.iterationmax = 500
config.no_of_experiments = 1
user_weights = og_coupling.weights_no_coupling(config.popSize, config.ntopics)
user_initialOpinions = og_opinions.initialize_opinions(config.popSize, config.ntopics)
user_adj = og_adj.make_adj(config.popSize, 'full')
In [118]:
state = og_state.WorldState(adj=user_adj,
couplingWeights=user_weights,
initialOpinions=user_initialOpinions,
initialHistorySize=100,
historyGrowthScale=2)
state.validate()
ufuncs = og_cfg.UserFunctions(og_select.FastPairSelection,
og_stop.iterationStop,
og_pot.createTent(tau))
In [13]:
#
# 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)
In [121]:
time, population_size, no_of_topics = rdict['history'].shape
topic1_evolution = rdict['history'][:,:,0].reshape(time, population_size)
topic2_evolution = rdict['history'][:,:,1].reshape(time, population_size)
In [125]:
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
fig.suptitle('Sharing Y axis')
ax1.plot(topic1_evolution)
ax1.set_title('Topic 1')
ax1.set(xlabel='Time', ylabel='Opinions')
ax2.plot(topic2_evolution)
ax2.set_title('Topic 2')
ax2.set(xlabel='Time', ylabel='Opinions')
fig.set_size_inches(20,5)
plt.show()