Пример #1
0
def pandemic():
    env = World(50, 10, 0.3)
    max_pop = len(env.list_available_tiles())
    pop = int(max_pop * 0.7)

    deathProbability = 0.5
    recoverProbability = 0.3
    infectedProbability = 0.8

    plague = Pandemic(1000, pop, infectedProbability, recoverProbability,
                      deathProbability, env, 1)
    plague.show(True)
Пример #2
0
def move():
    env = World(20, 20, 0.2)
    max_pop = len(env.list_available_tiles())
    pop = int(max_pop * 0.3)

    deathProbability = 0.04
    recoverProbability = 0.3
    infectedProbability = 0.7

    plague = Pandemic(1000, pop, infectedProbability, recoverProbability,
                      deathProbability, env, 1)
    plague.world.display()
Пример #3
0
def stack_chart():
    env = World(50, 10, 0.25)
    max_pop = len(env.list_available_tiles())
    pop = int(max_pop*0.7)

    deathProbability = 0.04
    recoverProbability = 0.2
    infectedProbability = 0.75

    epochs = 1000

    plague = Pandemic(epochs, pop, infectedProbability,
                      recoverProbability, deathProbability, env, 1, 1)
    plague.spread(True)

    x = range(1, plague.hardstop+2)
    y = [plague.healthy, plague.cured, plague.infected, plague.dead]
    
    pal = ["#2ecc71", "#3498db", "#e74c3c", "#9b59b6"]

    try:
        system('clear')
        for i in range(3):
            print("Simulation starting in " + str(3-i) + "...")
            time.sleep(1)
            system('clear')
    except KeyboardInterrupt:
        pass
    
    for i in range(plague.hardstop):
        try:
            y = [plague.healthy[:i], plague.cured[:i], plague.infected[:i], plague.dead[:i]]          
            plt.stackplot(x[:i], y, labels=['Unaffected', 'Cured', 'Infected', 'Dead'], colors=pal, alpha=0.4)
            plague.displayWorldHistory(i)
            if i == 0:
                plt.legend(loc='upper left')
            plt.pause(0.1)
        except KeyboardInterrupt:
            print("Exiting cleanly...")
            sys.exit()
    
    while(True):
        try:
            plt.show()
        except KeyboardInterrupt:
            print("Exiting cleanly...")
            sys.exit()
Пример #4
0
    def __init__(self,
                 epochs,
                 population,
                 infectProb,
                 healProb,
                 deathProb,
                 World,
                 n_infected=1,
                 spreadRange=1):
        self.epochs = epochs
        self.hardstop = 0
        self.spreadRange = spreadRange
        self.population = population
        self.infectProb = infectProb
        self.healProb = healProb
        self.deathProb = deathProb
        self.world = World

        self.worldHistory = []
        self.n_infected = n_infected
        self.has_spread = False

        self.nodes = dict()
        self.deaths_n = 0
        self.healthy = []
        self.cured = []
        self.infected = []
        self.dead = []

        free_tiles = World.list_available_tiles()
        free_tiles_n = len(free_tiles)

        if not (isinstance(population, int)):
            print(
                ColorsBook.WARNING +
                "Careful, population has to be an INT : forcing it at closest number"
                + ColorsBook.ENDC)
            population = int(population)

        if population > free_tiles_n:
            print(
                ColorsBook.WARNING +
                "Careful, population higher than allocated space : forcing it at maximum"
                + ColorsBook.ENDC)
            population = free_tiles_n

        if population < 0:
            print(
                ColorsBook.WARNING +
                "Careful, population can't be negative : forcing it at random number"
                + ColorsBook.ENDC)
            population = random.randint(1, free_tiles_n)

        self.population = population

        if epochs <= 0 or not (isinstance(epochs, int)):
            print(
                ColorsBook.FAIL +
                "Negative or Zero number of EPOCHS is not allowed, terminating..."
                + ColorsBook.ENDC)
            sys.exit()

        if (infectProb or healProb or deathProb) < 0 or (infectProb or healProb
                                                         or deathProb) > 1:
            print(
                ColorsBook.FAIL +
                "Probabilities not included between [0, 1] interval, terminating..."
                + ColorsBook.ENDC)
            sys.exit()

        if (spreadRange > 1):
            print(ColorsBook.WARNING +
                  "Careful, spreading range unsupported, forcing it at 1" +
                  ColorsBook.ENDC)
            self.spreadRange = 1

        elif (spreadRange < 0):
            print(ColorsBook.WARNING +
                  "Careful, spreading range unsupported, forcing it at 0" +
                  ColorsBook.ENDC)
            self.spreadRange = 0

        elif (not (isinstance(spreadRange, int))):
            print(
                ColorsBook.WARNING +
                "Careful, spreading range unsupported (should be int), forcing to random choice between 0 and 1"
                + ColorsBook.ENDC)
            self.spreadRange = round(random.random())

        if (n_infected < 0):
            print(
                ColorsBook.WARNING +
                "Careful, initial number of infected can't be negative, forcing it at 1"
                + ColorsBook.ENDC)
            self.n_infected = 1

        elif (not (isinstance(n_infected, int))):
            print(
                ColorsBook.WARNING +
                "Careful, initial number of infected must be int, forcing it at closest int"
                + ColorsBook.ENDC)
            self.n_infected = int(n_infected)

        # Generates Nodes at random available spot
        for i in range(self.population):
            random.shuffle(free_tiles)
            picked_spot = free_tiles.pop()
            self.world.pos_matrix[picked_spot] = State.UNAFFECTED
            self.nodes[picked_spot] = Node(State.UNAFFECTED, picked_spot)
Пример #5
0
def main():
    env = World(10, 10, 0.2)
    env.display()
Пример #6
0
def test_float_initial_cases():
    env = World(50, 10, 0.3)
    corona = Pandemic(1000, -3, 0, 0, 0, env, -2.9, 0.5)
    assert (isinstance(corona.n_infected, int) == True)
Пример #7
0
def test_float_population():
    env = World(50, 10, 0.3)
    corona = Pandemic(1000, 99.44, 0, 0, 0, env, 1, 1)
    assert (isinstance(corona.population, int) == True)
Пример #8
0
def test_outofbounds_spreadrange():
    env = World(50, 10, 0.3)
    corona = Pandemic(1000, -3, 0, 0, 0, env, -2, 500)
    assert (corona.spreadRange == 0 or corona.spreadRange == 1)
Пример #9
0
def test_float_spreadrange():
    env = World(50, 10, 0.3)
    corona = Pandemic(1000, -3, 0, 0, 0, env, -2, 0.5)
    assert (corona.spreadRange == 0 or corona.spreadRange == 1)
Пример #10
0
def test_negative_spreadrange():
    env = World(50, 10, 0.3)
    corona = Pandemic(1000, -3, 0, 0, 0, env, -2, -2)
    assert (corona.spreadRange == 0 or corona.spreadRange == 1)
Пример #11
0
def test_negative_initial_cases():
    env = World(50, 10, 0.3)
    corona = Pandemic(1000, -3, 0, 0, 0, env, -2)
    assert (corona.n_infected >= 0)
Пример #12
0
def test_population_overflow():
    env = World(50, 10, 0.3)
    corona = Pandemic(1000, 999999, 0.1, 0.1, 0.1, env)
    assert (corona.population == len(env.initial_available))
Пример #13
0
def test_negative_population():
    env = World(50, 10, 0.3)
    corona = Pandemic(1000, -3, 0.1, 0.1, 0.1, env)
    assert (corona.population > 0)
Пример #14
0
# -*- coding: utf-8 -*-

import sys
from os import path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))

from lib.env import World
from lib.pandemic import Pandemic
import random

a_env = World(50, 10, 0.3)
max_pop = len(a_env.list_available_tiles())
pop = int(max_pop * 0.7)
deathProbability = 0.5
recoverProbability = 0.3
infectedProbability = 0.8

plague = Pandemic(1000, pop, infectedProbability, recoverProbability,
                  deathProbability, a_env, 1)
plague.spread()


def test_population_consistancy():
    if plague.hardstop != 0:
        loop = plague.hardstop
    else:
        loop = plague.epochs

    for i in range(loop):
        assumed_pop = plague.dead[i] + plague.cured[i] + plague.healthy[
            i] + plague.infected[i]