import numpy as np
import matplotlib.pylab as plt
import seaborn as sns
from fcmpy.simulator.transfer import Sigmoid, Bivalent, Trivalent, HyperbolicTangent
import os
import pandas as pd
from fcmpy import ExpertFcm, FcmSimulator, FcmIntervention
# define a simulator
sim = FcmSimulator()
# use the data below for the simulation
C1 = [0.0, 0.0, 0.6, 0.9, 0.0, 0.0, 0.0, 0.8]
C2 = [0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2, 0.5]
C3 = [0.0, 0.7, 0.0, 0.0, 0.9, 0.0, 0.4, 0.1]
C4 = [0.4, 0.0, 0.0, 0.0, 0.0, 0.9, 0.0, 0.0]
C5 = [0.0, 0.0, 0.0, 0.0, 0.0, -0.9, 0.0, 0.3]
C6 = [-0.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
C7 = [0.0, 0.0, 0.0, 0.0, 0.0, 0.8, 0.4, 0.9]
C8 =[0.1, 0.0, 0.0, 0.0, 0.0, 0.1, 0.6, 0.0]
# It is important to notice that it is not the only way to create a weight matrix. Our algorithm also accepts the weight matrix in the form of NumPy array!!!!!
W = np.array([[0.0, 0.0, 0.6, 0.9, 0.0, 0.0, 0.0, 0.8]
[0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.2, 0.5]
[0.0, 0.7, 0.0, 0.0, 0.9, 0.0, 0.4, 0.1]
[0.4, 0.0, 0.0, 0.0, 0.0, 0.9, 0.0, 0.0]
[0.0, 0.0, 0.0, 0.0, 0.0, -0.9, 0.0, 0.3]
[-0.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
[0.0, 0.0, 0.0, 0.0, 0.0, 0.8, 0.4, 0.9]
[0.1, 0.0, 0.0, 0.0, 0.0, 0.1, 0.6, 0.0]])
weight_matrix = pd.DataFrame([C1,C2, C3, C4, C5, C6, C7, C8],
columns=['C1','C2','C3','C4','C5','C6','C7','C8'])
# define initial state of the vector as [1,1,0,0,0,0,0,0] for C1 to C8 as dictionary
init_state = {'C1': 1, 'C2': 1, 'C3': 0, 'C4': 0, 'C5': 0,
'C6': 0, 'C7': 0, 'C8': 0}
Here we run a simulation on top of the defined FCM structure using the sigmoid transfer function and the modified Kosko's inference method. The simulation will run $50$ iterations and will stop if the absolute difference between the concept values between the simulation steps is $\leq 0.001$. The steepness parameter for the sigmoid function is set to $1$. $Simulate$ accepts weight matrix as a data frame or NumPy array.
res_mK = sim.simulate(initial_state=init_state, weight_matrix=weight_matrix, transfer='sigmoid', inference='mKosko', thresh=0.001, iterations=50, l=1)
res_mK
Here we will use the same initial state and the weight matrix defined in the previous example. Let's first create an instance of the FcmIntervention class. To do so we need to pass an fcmpy Simulator object.
Here we just run a simulation on top of a defined FCM (where no intervention exists) with a given vector of initial conditions. The baseline of comparison is the derived equilibrium states of the concepts in the FCM.
Now we can specify the interventions that we want to test. Let's consider three such hypothetical interventions we wish to test in our FCM. The first intervention targets concepts (nodes) C1 and C2. It negatively impacts concept C1 (-.3) while positively impacting concept C2 (.5). We consider a case where the intervention has maximum effectiveness (1). The other two interventions follow the same logic but impact other nodes (see below).
# initialize the intervention object
inter = FcmIntervention(FcmSimulator)
# intialize the intervention
inter.initialize(initial_state=init_state, weight_matrix=weight_matrix,
transfer='sigmoid', inference='mKosko', thresh=0.001, iterations=50, l=1)
# run the simulation using the intervention
inter.add_intervention('intervention_1', impact={'C1':-.3, 'C2' : .5}, effectiveness=1)
inter.test_intervention('intervention_1')
inter.test_results['intervention_1']