'Why does the uninfected dot infect the dot that is 2 columns above eventhough I set the condition to 1 space around?

import random


class Being():
    def __init__(self, limits, name):
        self.row = random.randint(0, limits[0])
        self.col = random.randint(0, limits[1])
        self.name = name
        self.infected = False
        self.dead = False

    def getRow(self):
        return self.row

    def getCol(self):
        return self.col

    def getName(self):
        return self.name

    def lure(self, limits, beings):
        self.row += random.randint(-1, 1)
        self.col += random.randint(-1, 1)

        if self.row > limits[0]:
            self.row -= 1
        if self.row < 0:
            self.row += 1
        if self.col > limits[1]:
            self.col -= 1
        if self.col < 0:
            self.col += 1

        for i in range(len(beings)):
            for j in range(len(beings)):
                if i != j:
                    if beings[i].row == beings[j].row and beings[i].col == beings[j].col:
                        k = [-1, 1]
                        beings[i].row += random.choice(k)
                        beings[i].col += random.choice(k)

        for i in range(len(beings)):
            if not beings[i].infected:
                for j in range(len(beings)):
                    if beings[j].infected:
                        if i != j:
                            if beings[j].row + 1 == beings[i].row and beings[j].col + 1 == beings[i].col:
                                if random.randint(1,2)==1:
                                    beings[i].infected = True
                                    print(beings[j].name, "infected", beings[i].name)
                            if beings[j].row - 1 == beings[i].row and beings[j].col - 1 == beings[i].col:
                                if random.randint(1,2)==1:
                                    beings[i].infected = True
                                    print(beings[j].name, "infected", beings[i].name)
                            if beings[j].row + 1 == beings[i].row and beings[j].col - 1 == beings[i].col:
                                if random.randint(1,2)==1:
                                    beings[i].infected = True
                                    print(beings[j].name, "infected", beings[i].name)
                            if beings[j].row - 1 == beings[i].row and beings[j].col + 1 == beings[i].col:
                                if random.randint(1,2)==1:
                                    beings[i].infected = True
                                    print(beings[j].name, "infected", beings[i].name)                                



import matplotlib.pyplot as plt
import numpy as np
import random

MAXROWS = 10
MAXCOLS = 10


def flipCoords(row, col, limits):
    xpos = col
    ypos = row
    return (xpos, ypos)


def plot_being_scatter(beings):
    xlist = []
    ylist = []
    slist = []
    clist = []
    for k in range(len(beings)):
        if not beings[k].infected:
            ylist.append(beings[k].row)
            xlist.append(beings[k].col)
            slist.append(50)
            plt.scatter(xlist, ylist, s=slist, c="blue")


def plot_infected_scatter(beings):
    xlist = []
    ylist = []
    slist = []
    clist = []
    for k in range(len(beings)):
        if beings[k].infected:
            ylist.append(beings[k].row)
            xlist.append(beings[k].col)
            slist.append(50)
            plt.scatter(xlist, ylist, s=slist, c="red")


def main():
    beingNames = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18",
                  "19", "20"]
    limits = [MAXROWS, MAXCOLS]
    numBeings = 20
    beingList = []

    for i in range(numBeings):
        beingList.append(Being(limits, beingNames[i]))

    beingList[0].infected = True

    print("### Timestep ", 0, "###")
    plot_being_scatter(beingList)
    plot_infected_scatter(beingList)
    print(beingList[0].col, beingList[0].row)
    print(beingList[0].name, "is infected at row", beingList[0].row, "column", beingList[0].col)
    plt.title("Beacon Simulation")
    plt.xlabel("Columns")
    plt.ylabel("Rows")
    for i in range(numBeings):
        plt.annotate(beingList[i].name, flipCoords(beingList[i].getRow(), beingList[i].getCol(), limits))
    plt.xlim(-1, MAXCOLS)
    plt.ylim(-1, MAXROWS)
    plt.pause(1)
    plt.clf()

    for t in range(20):
        print("### Timestep ", t, "###")

        for i in range(numBeings):
            beingList[i].lure(limits, beingList)
            print(beingList[i].col, beingList[i].row)

        a = 20
        b = 0
        for i in range(20):
            if beingList[i].infected:
                b += 1
                print(beingList[i].name, "is infected at row", beingList[i].row, "column", beingList[i].col)
        print("Infected = ", b)
        print("Uninfected = ", a - b)
        plot_being_scatter(beingList)
        plot_infected_scatter(beingList)
        plt.title("Beacon Simulation")
        plt.xlabel("Columns")
        plt.ylabel("Rows")
        for i in range(numBeings):
            plt.annotate(beingList[i].name, flipCoords(beingList[i].getRow(), beingList[i].getCol(), limits))
        plt.xlim(-1, MAXCOLS)
        plt.ylim(-1, MAXROWS)
        plt.pause(1)
        plt.clf()


if __name__ == "__main__":
    main()

I dont get why the infected dot is infecting the uninfected dot that is 2 rows above. I have a condition to stop that though in the def(lure) section.

This is a simulation where beings move randomly around a space. There is an infected being and it has a 0.5 chance of spreading the disease to beings around it by 1 unit. The problem is my program is sometimes not doing that. I set a 0.5 chance of spreading but it happenes avery time now. Also it happens when they are 2 units away.

My ultimate goal is to make a graph over time which has 2 lines of infected and uninfected number of beings. How can I do that?

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source