Example #1
0
 def run_simulation(self, clock_type):
     self.__register_strategy()
     try:
         data_portal = self._prepare_data_portal()
     except:
         return 0
     clock = FactoryClock.get_clock(clock_type, data_portal)
     self.api_strategy.set_internal_variables(clock, data_portal)
     data_api = DataAPI(data_portal=data_portal,
                        traded_pairs=self.traded_pairs)
     self.api_strategy.data_api = data_api
     scheduler = self.api_strategy.get_scheduler()
     simulation_manager = SimulationManager(clock, self.api_strategy,
                                            scheduler)
     simulation_manager.simulate()
def create_eventstream_from_simulator(simulator_file_name, number_events,
                                      limit, model_file):

    # simulator generates ~950 events per day
    simulated_days = (1.25 * number_events / 950)

    sm = SimulationManager(start=datetime.datetime.now(),
                           end=datetime.datetime.now() +
                           timedelta(days=simulated_days),
                           model_file=model_file)

    # different resource_limit values produce different amounts of overlap between process instances
    # limits = 5    => overlap ~40+
    # limits = 10   => overlap ~20
    # limits = 50   => overlap ~10
    #
    # writes results into <filename>.txt
    sm.simulate(name=simulator_file_name,
                resource_limit={
                    'support': limit,
                    'trust': limit
                })

    # Get file path for output of simulation
    # using sys.platform to distinguish between Mac OS / Windows
    if sys.platform == "darwin":
        simulator_output_path = "output/" + simulator_file_name + '.txt'
    else:
        simulator_output_path = "output\\" + simulator_file_name + '.txt'

    #Opens file to read simulator output
    f = open(simulator_output_path)

    average_overlap = calculate_concurrency(simulator_output_path)
    print("average_overlap in simulation:", str(average_overlap))

    cleaned_file_path = ""

    # Open file to write ordered output into
    if sys.platform == "darwin":
        cleaned_file_path = "output/" + simulator_file_name + "_cleaned.txt"
    else:
        cleaned_file_path = "output\\" + simulator_file_name + "_cleaned.txt"

    outfile = open(cleaned_file_path, "w")

    data_elements = ["Name", "City", "University", "Gender", "Income"]

    # list of data elements to pull from
    names = ['Alice', 'Bob', 'Charlie', 'David', 'Emily', 'Frank']
    accounts = ['acct' + str(x) for x in range(len(names))]

    data_element_lists = [names, accounts]

    sampled_data = {}

    activity_data = {}

    addToLine = ''
    splitLine = []

    number_events_in_output = 0

    for line in f.readlines():

        if number_events_in_output < number_events:
            number_events_in_output += 1
        else:
            break

        # remove '#' and everything beyond
        line = line[:line.index("#") - 1]

        splitLine = line.split()

        activity_instance_id = splitLine[1]

        if activity_instance_id not in activity_data.keys():
            activity_data[activity_instance_id] = ' Name=' + random.choice(
                data_element_lists[0])

        if ('START' in splitLine):
            outfile.write(line.rstrip() + '\n')

        elif ('END' in splitLine):
            outfile.write(line.rstrip() + '\n')
        else:
            outfile.write(line.rstrip() + activity_data[activity_instance_id] +
                          '\n')

    print("generated clean log: " + cleaned_file_path)

    # close files
    f.close()
    outfile.close()

    os.rename(
        cleaned_file_path, cleaned_file_path[:-4] + "_act=" +
        str(number_events_in_output) + '.txt')

    return number_events_in_output, cleaned_file_path