Exemplo n.º 1
0
def step1():

    example1 = circus.Circus(name="example1",
                             master_seed=12345,
                             start=pd.Timestamp("1 Jan 2017 00:00"),
                             step_duration=pd.Timedelta("1h"))

    person = example1.create_population(
        name="person",
        size=1000,
        ids_gen=SequencialGenerator(prefix="PERSON_"))

    hello_world = example1.create_story(
        name="hello_world",
        initiating_population=person,
        member_id_field="PERSON_ID",

        # after each story, reset the timer to 0, so that it will get
        # executed again at the next clock tick (next hour)
        timer_gen=ConstantDependentGenerator(value=0))

    hello_world.set_operations(
        ConstantGenerator(value="hello world").ops.generate(named_as="HELLO"),
        FieldLogger(log_id="hello"))

    example1.run(duration=pd.Timedelta("48h"),
                 log_output_folder="output/example1",
                 delete_existing_logs=True)

    with open("output/example1/hello.csv") as f:
        print("Logged {} lines".format(len(f.readlines()) - 1))
Exemplo n.º 2
0
def step4():
    """
    Woah, this got drastically slower
    """

    example1 = circus.Circus(name="example1",
                             master_seed=12345,
                             start=pd.Timestamp("1 Jan 2017 00:00"),
                             step_duration=pd.Timedelta("1h"))

    person = example1.create_population(
        name="person",
        size=1000,
        ids_gen=SequencialGenerator(prefix="PERSON_"))

    person.create_attribute("NAME",
                            init_gen=FakerGenerator(method="name",
                                                    seed=next(
                                                        example1.seeder)))

    sites = SequencialGenerator(prefix="SITE_").generate(1000)
    random_site_gen = NumpyRandomGenerator(method="choice",
                                           a=sites,
                                           seed=next(example1.seeder))

    allowed_sites = person.create_relationship(name="sites")
    for i in range(5):
        allowed_sites \
            .add_relations(from_ids=person.ids,
                           to_ids=random_site_gen.generate(person.size))

    hello_world = example1.create_story(
        name="hello_world",
        initiating_population=person,
        member_id_field="PERSON_ID",

        # after each story, reset the timer to 0, so that it will get
        # executed again at the next clock tick (next hour)
        timer_gen=ConstantDependentGenerator(value=0))

    duration_gen = NumpyRandomGenerator(method="exponential",
                                        scale=60,
                                        seed=next(example1.seeder))

    hello_world.set_operations(
        person.ops.lookup(id_field="PERSON_ID", select={"NAME": "NAME"}),
        ConstantGenerator(value="hello world").ops.generate(named_as="HELLO"),
        duration_gen.ops.generate(named_as="DURATION"),
        allowed_sites.ops.select_one(from_field="PERSON_ID", named_as="SITE"),
        example1.clock.ops.timestamp(named_as="TIME"),
        FieldLogger(log_id="hello"))

    example1.run(duration=pd.Timedelta("48h"),
                 log_output_folder="output/example1",
                 delete_existing_logs=True)

    with open("output/example1/hello.csv") as f:
        print("Logged {} lines".format(len(f.readlines()) - 1))
Exemplo n.º 3
0
def create_circus_with_population():
    example_circus = circus.Circus(
        name="example",
        master_seed=12345,
        start=pd.Timestamp("1 Jan 2017 00:00"),
        step_duration=pd.Timedelta("1h"))

    person = example_circus.create_population(
        name="person", size=1000,
        ids_gen=SequencialGenerator(prefix="PERSON_"))

    person.create_attribute(
        "NAME",
        init_gen=FakerGenerator(method="name",
                                seed=next(example_circus.seeder)))

    person.create_attribute(
        "age",
        init_gen=NumpyRandomGenerator(
            method="normal", loc=35, scale=5,
            seed=next(example_circus.seeder)))

    return example_circus
import logging
import pandas as pd
from tabulate import tabulate

from trumania.core import circus
import trumania.core.util_functions as util_functions
from trumania.core.random_generators import SequencialGenerator, FakerGenerator, NumpyRandomGenerator

util_functions.setup_logging()

logging.info("building circus")

example = circus.Circus(name="example",
                        master_seed=12345,
                        start=pd.Timestamp("1 Jan 2017 00:00"),
                        step_duration=pd.Timedelta("1h"))

person = example.create_population(
    name="person", size=1000, ids_gen=SequencialGenerator(prefix="PERSON_"))

person.create_attribute("NAME",
                        init_gen=FakerGenerator(method="name",
                                                seed=next(example.seeder)))

person.create_attribute("age",
                        init_gen=NumpyRandomGenerator(method="normal",
                                                      loc=35,
                                                      scale=5,
                                                      seed=next(
                                                          example.seeder)))
Exemplo n.º 5
0
def build_circus():
    return circus.Circus(name="example3",
                         master_seed=12345,
                         start=pd.Timestamp("1 Jan 2017 00:00"),
                         step_duration=pd.Timedelta("1h"))
Exemplo n.º 6
0
def step7():

    example1 = circus.Circus(name="example1",
                             master_seed=12345,
                             start=pd.Timestamp("1 Jan 2017 00:00"),
                             step_duration=pd.Timedelta("1h"))

    person = example1.create_population(
        name="person",
        size=1000,
        ids_gen=SequencialGenerator(prefix="PERSON_"))

    person.create_attribute("NAME",
                            init_gen=FakerGenerator(method="name",
                                                    seed=next(
                                                        example1.seeder)))
    person.create_attribute("POPULARITY",
                            init_gen=NumpyRandomGenerator(
                                method="uniform",
                                low=0,
                                high=1,
                                seed=next(example1.seeder)))

    sites = SequencialGenerator(prefix="SITE_").generate(1000)
    random_site_gen = NumpyRandomGenerator(method="choice",
                                           a=sites,
                                           seed=next(example1.seeder))

    allowed_sites = person.create_relationship(name="sites")

    # SITES ------------------

    # Add HOME sites
    allowed_sites.add_relations(from_ids=person.ids,
                                to_ids=random_site_gen.generate(person.size),
                                weights=0.4)

    # Add WORK sites
    allowed_sites.add_relations(from_ids=person.ids,
                                to_ids=random_site_gen.generate(person.size),
                                weights=0.3)

    # Add OTHER sites
    for i in range(3):
        allowed_sites \
            .add_relations(from_ids=person.ids,
                           to_ids=random_site_gen.generate(person.size),
                           weights=0.1)

    # FRIENDS ------------------

    friends = person.create_relationship(name="friends")

    friends_df = pd.DataFrame.from_records(
        make_random_bipartite_data(
            person.ids,
            person.ids,
            p=0.005,  # probability for a node to be connected to
            # another one : 5 friends on average = 5/1000
            seed=next(example1.seeder)),
        columns=["A", "B"])

    friends.add_relations(from_ids=friends_df["A"], to_ids=friends_df["B"])

    # PRICE ------------------

    def price(story_data):

        result = pd.DataFrame(index=story_data.index)

        result["PRICE"] = story_data["DURATION"] * 0.05
        result["CURRENCY"] = "EUR"

        return result

    # STORIES ------------------

    hello_world = example1.create_story(
        name="hello_world",
        initiating_population=person,
        member_id_field="PERSON_ID",

        # after each story, reset the timer to 0, so that it will get
        # executed again at the next clock tick (next hour)
        timer_gen=ConstantDependentGenerator(value=0))

    duration_gen = NumpyRandomGenerator(method="exponential",
                                        scale=60,
                                        seed=next(example1.seeder))

    hello_world.set_operations(
        person.ops.lookup(id_field="PERSON_ID", select={"NAME": "NAME"}),
        ConstantGenerator(value="hello world").ops.generate(named_as="HELLO"),
        duration_gen.ops.generate(named_as="DURATION"),
        friends.ops.select_one(
            from_field="PERSON_ID",
            named_as="COUNTERPART_ID",
            weight=person.get_attribute_values("POPULARITY"),
            # For people that do not have friends, it will try to find
            # the POPULARITY attribute of a None and crash miserably
            # Adding this flag will discard people that do not have friends
            discard_empty=True),
        person.ops.lookup(id_field="COUNTERPART_ID",
                          select={"NAME": "COUNTER_PART_NAME"}),
        allowed_sites.ops.select_one(from_field="PERSON_ID", named_as="SITE"),
        allowed_sites.ops.select_one(from_field="COUNTERPART_ID",
                                     named_as="COUNTERPART_SITE"),
        Apply(source_fields=["DURATION", "SITE", "COUNTERPART_SITE"],
              named_as=["PRICE", "CURRENCY"],
              f=price,
              f_args="dataframe"),
        example1.clock.ops.timestamp(named_as="TIME"),
        FieldLogger(log_id="hello"))

    example1.run(duration=pd.Timedelta("48h"),
                 log_output_folder="output/example1",
                 delete_existing_logs=True)

    with open("output/example1/hello.csv") as f:
        print("Logged {} lines".format(len(f.readlines()) - 1))
Exemplo n.º 7
0
def build_circus():
    return circus.Circus(name="salestock",
                         master_seed=12345,
                         start=pd.Timestamp("1 Jun 2018 00:00"),
                         step_duration=pd.Timedelta("1h"))
Exemplo n.º 8
0
    # These are already declared in the geography
    # "n_dealers_l2": 25,
    # "n_dealers_l1": 4,
    "n_telcos": 1,
    "n_field_agents": 100,
    "n_customers": 20000,
}

if __name__ == "__main__":

    util_functions.setup_logging()

    snd = circus.Circus(name=static_params["circus_name"],
                        master_seed=12345,
                        start=pd.Timestamp(static_params["clock_start_date"]),
                        step_duration=pd.Timedelta(
                            static_params["clock_time_step"]))

    distributor_id_gen = random_generators.SequencialGenerator(prefix="DIST_")

    snd_products.create_products(snd, static_params)

    snd_geo.load_geo_actors(snd, static_params)
    snd_customers.add_customers(snd, static_params)

    snd_pos.add_pos(snd, static_params)

    snd_dealer.add_telcos(snd, static_params, distributor_id_gen)
    snd_dealer.prepare_dealers(snd, params=static_params)
Exemplo n.º 9
0
hrs = "48h"
#4 days

#for changing num logs per user per hour
#2 logs per hr = 0.5h, 5 logs per hour = 1/5=0.2h
num_log_per_hr = 1
val = 1 / num_log_per_hr
step_dur = "{0}h".format(val)
#7am-7pm = 13 hrs
num_log = 13 * (num_log_per_hr)

setup_logging()
start_date = pd.Timestamp("09 Aug 2020 00:00:00")

example1 = circus.Circus(name="example1",
                         master_seed=123456,
                         start=start_date,
                         step_duration=pd.Timedelta(step_dur))

person = example1.create_population(name="person",
                                    size=num_user,
                                    ids_gen=SequencialGenerator(prefix=""))

person.create_attribute("NAME",
                        init_gen=FakerGenerator(method="name",
                                                seed=next(example1.seeder)))

activity = (100, 100, 100, 100, 54, 54, 54, 26, 20, 22)
normed_activity = [float(i) / sum(activity) for i in activity]
"""
sites = SequencialGenerator(prefix="").generate(num_sites)										
random_site_gen = NumpyRandomGenerator(method="choice", a=sites,