Exemplo n.º 1
0
def performSimulation(i, first_vehicle, verbose=True):
    """
	Performs a single simulation with id 'i' starting the message spreading
	from the vehicle with index 'first_vehicle' in cars list
	"""
    # Load vehicle data
    cars = init_cars()

    # Init simulator with the vehicle connectivity graph
    s = Simulator(cars)

    # Get one random car and start the dissemination from it
    if s.no_graphics:
        cars[first_vehicle].on_receive(Msg.dummy())
    else:
        bubbles = displayCars(s.car_dict)
        firstinfected = s.getCar(firstInfection())
        firstinfected.infect(Msg.dummy())

    # Run simulation
    s.runSimulation()

    # If verbose print the result of this simulation
    if verbose:
        tmp = str([c.state for c in cars])
        print("Simulation", i, "ended")
        print("Vulnerable: ", tmp.count("State.VULNERABLE"))
        print("Infected: ", tmp.count("State.INFECTED"))
        print("Recovered: ", tmp.count("State.RECOVERED"))
        print()

    return s
Exemplo n.º 2
0
def performSimulation(i, verbose=True):
    cars = init_cars()

    def print_graph_stats():
        grade = 0
        for car in cars:
            for a in car.adj:
                grade += a
        print('number of cars', len(cars))
        print('number of edges', grade / 2)
        print('density', grade / (2 * len(cars)))

    #print_graph_stats()

    s = Simulator(cars)

    if s.no_graphics:
        random.sample(cars, 1)[0].infect(Msg.dummy())
    else:
        bubbles = displayCars(s.car_dict)
        firstinfected = s.getCar(firstInfection())
        firstinfected.infect(
            Msg(firstinfected.plate, 'ciao',
                (firstinfected.pos[0], firstinfected.pos[1]),
                (firstinfected.pos[0], firstinfected.pos[1]), 0, 100))

    s.runSimulation()

    if verbose:
        tmp = str([c.state for c in cars])
        print("Simulation", i, "ended")
        print("Vulnerable: ", tmp.count("State.VULNERABLE"))
        print("Infected: ", tmp.count("State.INFECTED"))
        print("Recovered: ", tmp.count("State.RECOVERED"))
        print()

    return s