コード例 #1
0
	def test_percentages(self):
		neutral_range = 0.001
		self.G.node[0]['opinion'] = 0.001 # neutral
		self.G.node[1]['opinion'] = 0.01 # positive
		self.G.node[2]['opinion'] = -0.5 # negative
		percentages_exp = [1/3,1/3,1/3]
		np.testing.assert_array_equal(cp.percentages(self.G,neutral_range),percentages_exp)


		self.G.node[2]['opinion'] = 0.5
		percentages_exp = [2/3,0/3,1/3]
		np.testing.assert_array_equal(cp.percentages(self.G,neutral_range),percentages_exp)

		self.G.node[1]['opinion'] = -0.001
		self.G.node[2]['opinion'] = -0.01
		percentages_exp = [0/3,1/3,2/3]
		np.testing.assert_array_equal(cp.percentages(self.G,neutral_range),percentages_exp)
コード例 #2
0
    def test_percentages(self):
        neutral_range = 0.001
        self.G.node[0]['opinion'] = 0.001  # neutral
        self.G.node[1]['opinion'] = 0.01  # positive
        self.G.node[2]['opinion'] = -0.5  # negative
        percentages_exp = [1 / 3, 1 / 3, 1 / 3]
        np.testing.assert_array_equal(cp.percentages(self.G, neutral_range),
                                      percentages_exp)

        self.G.node[2]['opinion'] = 0.5
        percentages_exp = [2 / 3, 0 / 3, 1 / 3]
        np.testing.assert_array_equal(cp.percentages(self.G, neutral_range),
                                      percentages_exp)

        self.G.node[1]['opinion'] = -0.001
        self.G.node[2]['opinion'] = -0.01
        percentages_exp = [0 / 3, 1 / 3, 2 / 3]
        np.testing.assert_array_equal(cp.percentages(self.G, neutral_range),
                                      percentages_exp)
コード例 #3
0
    # If strategy 1 is not a smart peer, the 2 forceful peers can be added simultaneously
    if STRATEGY1 != 'smart':
        gm.add_forceful(G, STRATEGY1, BUDGET1, STRATEGY2, BUDGET2)
    # If strategy 1 is smart then ofrceful peer with strategy 2 is added first then smart peer
    else:
        #Add one forceul peer to the graph
        gm.add_one_forceful(G, STRATEGY2, BUDGET2)
        ## Linear programming method to beat an existing peer with minimum budget
        BUDGET1 = gm.add_smart(G, ALPHA, BUDGET2, NEUTRAL_RANGE)
    # Calculate final opinion vector by equation, considering the presence of
    # 2 forceful peers in the last 2 indices of the graph
    #R_inf = cp.R_inf(G,ALPHA)
    # Calculate final opinion vector by iterations
    R_itr = gm.R_itr(G, ALPHA)
    # Calculate the percentage of normal peers that followed either of the forceful peers
    tmp = cp.percentages(G, NEUTRAL_RANGE)
    # Update S1_wins_list for analytical reason
    if (tmp[0] > 0.5): S1_wins_list.append(1)
    else: S1_wins_list.append(0)
    # Update percentages arrays
    cp.update_percentages(tmp, S1_followers, S2_followers, neutral, i, wins)
    i += 1
    # Asserting that R_inf calculated by equation and iteration are equal to decimal places
    #try:
    #	np.testing.assert_array_almost_equal(R_inf,R_itr,4)
    #except AssertionError:
    #	sys.exit('ConvergenceError: convergence of R_inf is not correct to 4 decimal places\nProgram will terminate')

wins[0] = (wins[0] / SIMULATIONS) * 100
wins[1] = (wins[1] / SIMULATIONS) * 100
wins[2] = (wins[2] / SIMULATIONS) * 100
コード例 #4
0
	# If strategy 1 is not a smart peer, the 2 forceful peers can be added simultaneously
	if STRATEGY1 != 'smart':
		gm.add_forceful(G, STRATEGY1, BUDGET1, STRATEGY2, BUDGET2)
	# If strategy 1 is smart then ofrceful peer with strategy 2 is added first then smart peer
	else:
		#Add one forceul peer to the graph
		gm.add_one_forceful(G, STRATEGY2, BUDGET2)
		## Linear programming method to beat an existing peer with minimum budget
		BUDGET1 = gm.add_smart(G,ALPHA,BUDGET2, NEUTRAL_RANGE)
	# Calculate final opinion vector by equation, considering the presence of 
	# 2 forceful peers in the last 2 indices of the graph
	#R_inf = cp.R_inf(G,ALPHA)
	# Calculate final opinion vector by iterations
	R_itr = gm.R_itr(G,ALPHA)
	# Calculate the percentage of normal peers that followed either of the forceful peers
	tmp = cp.percentages(G,NEUTRAL_RANGE)
	# Update S1_wins_list for analytical reason
	if (tmp[0]>0.5): S1_wins_list.append(1)
	else: S1_wins_list.append(0)
	# Update percentages arrays
	cp.update_percentages(tmp, S1_followers, S2_followers, neutral, i, wins)
	i += 1
	# Asserting that R_inf calculated by equation and iteration are equal to decimal places
	#try:
	#	np.testing.assert_array_almost_equal(R_inf,R_itr,4)
	#except AssertionError:
	#	sys.exit('ConvergenceError: convergence of R_inf is not correct to 4 decimal places\nProgram will terminate')

wins[0] = (wins[0]/SIMULATIONS)*100 
wins[1] = (wins[1]/SIMULATIONS)*100
wins[2] = (wins[2]/SIMULATIONS)*100