Demo entry 6734756

The source code of my project

   

Submitted by Lijie Liu on Apr 22, 2018 at 13:47
Language: ANTLR With Python Target. Code size: 5.7 kB.

import random
import matplotlib.pyplot as plt

MAX_TIME = 100
t = 0

Length = 20
Player1 = 250
L = random.sample(range(Length * Length), Player1)
Matrix = [[0 for x in range(Length)] for x in range(Length)]
for i in range(0, Length):
    for j in range(0, Length):
        Matrix[i][j] = {'X': i, 'Y': j, 'Index': i * Length + j, 'Game': 'Dota2', 'Interest': -0.5}
for i in range(0, Length):
    for j in range(0, Length):
        for index in range(0, Player1):
            if Matrix[i][j]['Index'] == L[index]:
                Matrix[i][j]['Game'] = 'LeagueOfLegend'
                Matrix[i][j]['Interest'] = 0.5

def GetPlayerNum(matrix, Length):
    LOL = 0
    for i in range(0, Length):
        for j in range(0, Length):
            if matrix[i][j]['Game'] == 'LeagueOfLegend':
                LOL = LOL + 1
    DOTA2 = Length * Length - LOL
    return LOL, DOTA2

def GetGlobalInfluence(Player1, Player2):
    Influence1 = Player1 / (Player1 + Player2)
    Influence2 = 1 - Influence1
    GlobalInfluence = Influence1 - Influence2
    return GlobalInfluence

def GetLocalDirectInfluence(Dict):
    if Dict['X'] == 0:
        UpInfluence = 0
    else:
        UpInfluence = Matrix[Dict['X'] - 1][Dict['Y']]['Interest']
    if Dict['X'] == 19:
        DownInfluence = 0
    else:
        DownInfluence = Matrix[Dict['X'] + 1][Dict['Y']]['Interest']
    if Dict['Y'] == 0:
        LeftInfluence = 0
    else:
        LeftInfluence = Matrix[Dict['X']][Dict['Y'] - 1]['Interest']
    if Dict['Y'] == 19:
        RightInfluence = 0
    else:
        RightInfluence = Matrix[Dict['X']][Dict['Y'] + 1]['Interest']
    LocalDirectInfluence = 0
    LocalDirectInfluence = LocalDirectInfluence + UpInfluence
    LocalDirectInfluence = LocalDirectInfluence + DownInfluence
    LocalDirectInfluence = LocalDirectInfluence + LeftInfluence
    LocalDirectInfluence = LocalDirectInfluence + RightInfluence
    return LocalDirectInfluence

def GetLocalIndirectInfluence(Dict):
    if Dict['X'] == 0 or Dict['Y'] == 0:
        LeftUpInfluence = 0
    else:
        LeftUpInfluence = Matrix[Dict['X'] - 1][Dict['Y'] - 1]['Interest']
    if Dict['X'] == 0 or Dict['Y'] == 19:
        RightUptInfluence = 0
    else:
        RightUptInfluence = Matrix[Dict['X'] - 1][Dict['Y'] + 1]['Interest']
    if Dict['X'] == 19 or Dict['Y'] == 0:
        LeftDownInfluence = 0
    else:
        LeftDownInfluence = Matrix[Dict['X'] + 1][Dict['Y'] - 1]['Interest']
    if Dict['X'] == 19 or Dict['Y'] == 19:
        RightDownInfluence = 0
    else:
        RightDownInfluence = Matrix[Dict['X'] + 1][Dict['Y'] + 1]['Interest']
    LocalIndirectInfluence = 0
    LocalIndirectInfluence = LocalIndirectInfluence + LeftUpInfluence
    LocalIndirectInfluence = LocalIndirectInfluence + LeftDownInfluence
    LocalIndirectInfluence = LocalIndirectInfluence + RightUptInfluence
    LocalIndirectInfluence = LocalIndirectInfluence + RightDownInfluence
    return LocalIndirectInfluence

def ChangeInterest(Dict, Global, Direct, Indirect):
    Influence = Global + Direct + Indirect
    Dict['Interest'] = Dict['Interest'] + Influence
    return Dict

def DrawGraph(matrix, t):
    x1 = []
    y1 = []
    x2 = []
    y2 = []
    for i in range(20):
        for j in range(20):
            if matrix[i][j]['Game'] == 'Dota2':
                x1.append(matrix[i][j]['X'])
                y1.append(matrix[i][j]['Y'])
            else:
                x2.append(matrix[i][j]['X'])
                y2.append(matrix[i][j]['Y'])
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    titlename = 'GameForTime' + str(t)
    ax1.set_title('GamerForTime' + str(t))
    plt.xlabel('X')
    plt.ylabel('Y')
    svalue1 = len(x1)
    svalue2 = len(x2)
    ax1.scatter(x1, y1, s=svalue1, c='r', marker='x')
    ax1.scatter(x2, y2, s=svalue2, c='y', marker='o')
    plt.show()
    fig.savefig('/Users/Uranus-Lj/Desktop/UWA/CITS4403/CaseStudyGraphs/' + titlename + '.png', dpi = 75)

def CheckInterest(dict):
    if dict['Interest'] > 1:
        return 1
    if dict['Interest'] < -1:
        return -1
    else:
        return dict['Interest']

Current = [[0 for x in range(Length)] for x in range(Length)]
a = []
b = []
while t < MAX_TIME:
    for i in range(0, 20):
        for j in range(0, 20):
            Current[i][j] = Matrix[i][j]

    influence = 0
    DrawGraph(Matrix, t)
    lol, dota2 = GetPlayerNum(Current, Length)
    a.append(lol)
    b.append(dota2)
    globalin = GetGlobalInfluence(lol, dota2)
    influence = influence + globalin * 0.01

    for i in range(0, 20):
        for j in range(0, 20):
            influence = globalin * 0.01
            locald = 0
            locali = 0
            locald = GetLocalDirectInfluence(Current[i][j])
            influence = influence + locald * 0.05
            locali = GetLocalIndirectInfluence(Current[i][j])
            influence = influence + locali * 0.025
            Matrix[i][j]['Interest'] = Matrix[i][j]['Interest'] + influence
            Matrix[i][j]['Interest'] = CheckInterest(Matrix[i][j])
            if Matrix[i][j]['Interest'] < 0:
                Matrix[i][j]['Game'] = 'Dota2'
            elif Matrix[i][j]['Interest'] > 0:
                Matrix[i][j]['Game'] = 'LeagueOfLegend'

    t = t + 1
c = []

for i in range(0, 100):
    c.append(i)
linechart = plt.figure()
plt.plot(c, a, "b--", label = "$League of Legends$", linewidth = 1)
plt.plot(c, b, label = "$Dota2$", color = "red", linewidth = 2)
plt.xlabel("Time steps")
plt.ylabel("Player number")
plt.title("Player number line chart")
plt.legend()
plt.show()
linechart.savefig("/Users/Uranus-Lj/Desktop/UWA/CITS4403/CaseStudyGraphs/PlayerNumber.png")

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).