| Server IP : 172.64.80.1 / Your IP : 172.70.100.163 Web Server : Apache System : Linux mail.federalpolyede.edu.ng 5.10.0-32-amd64 #1 SMP Debian 5.10.223-1 (2024-08-10) x86_64 User : federalpolyede.edu.ng_idh35skikv ( 10000) PHP Version : 7.4.33 Disable Function : opcache_get_status MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/vhosts/federalpolyede.edu.ng/phd_code/ |
Upload File : |
import numpy as np
import pandas as pd
import os
# Specify the path and filename of the Excel file
filename = 'output.xlsx'
# Check if the file exists
if os.path.exists(filename):
# Delete the file
os.remove(filename)
# Assuming there are two players and three strategies for each player
num_players = 2
num_strategies = 3
#initialise Data
r=500
#setting value for Cost of Attack
ca0=0;ca1=2;ca2=4
#Setting value for cost of Defence
cd0=0;cd1=3;cd2=6
# setting Equation for Payoff Matrix
a11=0;a12=0;a13=0
a21=r-ca1;a22=-ca1;a23=-ca1
a31=r-ca2;a32=r-ca2;a33=-ca2
d11=r;d12=r-cd1;d13=r-cd2
d21=-r;d22=r-cd1;d23=r-cd2
d31=-r;d32=-r-cd1;d33=r-cd2
#pass in the Model Formulated - probability Distribution of Defender
q_0=ca1/r
q_1=(ca2-ca1)/r
q_2=1-(ca2/r)
#Pass Model for Probability Distibution of Attacker
p_0=1+(cd1-cd2)/r
p_1=cd1/2*r
p_2=(2*cd2-3*cd1)/(2*r)
data=[]
# Calcualte probabilities associated with a Nash equilibrium
nash_equilibrium_probs = np.array([[p_0, p_1, p_2], [q_0, q_1, q_2]])
# Number of simulations or iterations
num_simulations = 10000
# Simulate strategies
chosen_strategiesk=0
data = {
'Attacker':[0],
'Defender':[0]
}
df = pd.DataFrame(data)
# Specify the path and filename for the Excel file
filename = 'output_3.xlsx'
# Write the DataFrame to an Excel file
# df.to_excel(filename, index=False)
for _ in range(num_simulations):
chosen_strategies = []
for player in range(num_players):
random_num = np.random.random() # Generate a random number between [0, 1]
cumulative_probs = np.cumsum(nash_equilibrium_probs[player])
chosen_strategy = np.argmax(random_num <= cumulative_probs)
chosen_strategies.append(chosen_strategy)
print("Chosen Strategies:", chosen_strategies)
# New record to add
new_record = {'Attacker': chosen_strategies[0], 'Defender': chosen_strategies[-1]}
# Append the new record to the DataFrame
df = df.append(new_record, ignore_index=True)
df.to_excel(filename, index=False)
# Print the updated DataFrame
print(df)
# df.to_excel(filename, index=False)
print("Data successfully exported to Excel file:", filename)