def test_constant0_variable_2(self):
     # to avoid static method warnings in tests,
     # that by construction of the unittest package have to be expressed in such way
     self.dummy_variable = "dummy_value"
     const = Constant0()
     values = [0.0000001, 0.0000001, 0.0000002]
     loglikeli = const.calculate_loglikelihood(values)
     if abs(loglikeli) < 10000000:
         raise Exception("problem in managing constant variables")
     rand = RandomVariable()
     rand.calculate_parameters(values)
     if not rand.get_distribution_type() == "IMMEDIATE":
         raise Exception("Expected a constant!")
     loglikeli = rand.calculate_loglikelihood(values)
     if abs(loglikeli) < 10000000:
         raise Exception("problem in managing constant variables (2)")
 def test_exponential_variable(self):
     # to avoid static method warnings in tests,
     # that by construction of the unittest package have to be expressed in such way
     self.dummy_variable = "dummy_value"
     loc = 0
     scale = 5
     tol = 0.2
     exp = Exponential(loc=loc, scale=scale)
     values = exp.get_values(no_values=400)
     rand = RandomVariable()
     rand.calculate_parameters(values)
     if not rand.get_distribution_type() == "EXPONENTIAL":
         raise Exception("Expected an exponential!")
     loc_r = rand.random_variable.loc
     scale_r = rand.random_variable.scale
     diff_value_loc = abs(loc - loc_r) / (max(abs(loc), abs(loc_r)) + 0.000001)
     diff_value_scale = abs(scale - scale_r) / (max(abs(scale), abs(scale_r)) + 0.000001)
     if diff_value_loc > tol or diff_value_scale > tol:
         raise Exception("parameters found outside tolerance")
 def test_normal_variable(self):
     # to avoid static method warnings in tests,
     # that by construction of the unittest package have to be expressed in such way
     self.dummy_variable = "dummy_value"
     mu = 53
     sigma = 4
     tol = 0.15
     norm = Normal(mu=mu, sigma=sigma)
     values = norm.get_values(no_values=400)
     rand = RandomVariable()
     rand.calculate_parameters(values)
     if not rand.get_distribution_type() == "NORMAL":
         raise Exception("Excepted a normal!")
     mu_r = rand.random_variable.mu
     sigma_r = rand.random_variable.sigma
     diff_value_mu = abs(mu - mu_r) / (max(abs(mu), abs(mu_r)))
     diff_value_sigma = abs(sigma - sigma_r) / (max(abs(sigma), abs(sigma_r)))
     if diff_value_mu > tol or diff_value_sigma > tol:
         raise Exception("parameters found outside tolerance")
 def test_uniform_variable(self):
     # to avoid static method warnings in tests,
     # that by construction of the unittest package have to be expressed in such way
     self.dummy_variable = "dummy_value"
     loc = 53
     scale = 32
     tol = 0.15
     unif = Uniform(loc=loc, scale=scale)
     values = unif.get_values(no_values=400)
     rand = RandomVariable()
     rand.calculate_parameters(values)
     if not rand.get_distribution_type() == "UNIFORM":
         raise Exception("Expected an uniform!")
     loc_r = rand.random_variable.loc
     scale_r = rand.random_variable.scale
     diff_value_loc = abs(loc - loc_r) / (max(abs(loc), abs(loc_r)))
     diff_value_scale = abs(scale - scale_r) / (max(abs(scale), abs(scale_r)))
     if diff_value_loc > tol or diff_value_scale > tol:
         raise Exception("parameters found outside tolerance")
Example #5
0
def get_map_from_log_and_net(log,
                             net,
                             initial_marking,
                             final_marking,
                             force_distribution=None,
                             parameters=None):
    """
    Get transition stochastic distribution map given the log and the Petri net

    Parameters
    -----------
    log
        Event log
    net
        Petri net
    initial_marking
        Initial marking of the Petri net
    final_marking
        Final marking of the Petri net
    force_distribution
        If provided, distribution to force usage (e.g. EXPONENTIAL)
    parameters
        Parameters of the algorithm, including:
            PARAM_ACTIVITY_KEY -> activity name
            PARAM_TIMESTAMP_KEY -> timestamp key

    Returns
    -----------
    stochastic_map
        Map that to each transition associates a random variable
    """
    stochastic_map = {}

    if parameters is None:
        parameters = {}

    activity_key = parameters[
        PARAM_ACTIVITY_KEY] if PARAM_ACTIVITY_KEY in parameters else log_lib.util.xes.DEFAULT_NAME_KEY
    timestamp_key = parameters[
        PARAM_TIMESTAMP_KEY] if PARAM_TIMESTAMP_KEY in parameters else "time:timestamp"

    parameters_variants = {PARAM_ACTIVITY_KEY: activity_key}
    variants_idx = variants_module.get_variants_from_log_trace_idx(
        log, parameters=parameters_variants)
    variants = variants_module.convert_variants_trace_idx_to_trace_obj(
        log, variants_idx)

    parameters_tr = {PARAM_ACTIVITY_KEY: activity_key, "variants": variants}

    # do the replay
    aligned_traces = token_replay.apply(log,
                                        net,
                                        initial_marking,
                                        final_marking,
                                        parameters=parameters_tr)

    element_statistics = performance_map.single_element_statistics(
        log,
        net,
        initial_marking,
        aligned_traces,
        variants_idx,
        activity_key=activity_key,
        timestamp_key=timestamp_key)

    for el in element_statistics:
        if type(
                el
        ) is PetriNet.Transition and "performance" in element_statistics[el]:
            values = element_statistics[el]["performance"]

            rand = RandomVariable()
            rand.calculate_parameters(values,
                                      force_distribution=force_distribution)

            no_of_times_enabled = element_statistics[el]['no_of_times_enabled']
            no_of_times_activated = element_statistics[el][
                'no_of_times_activated']

            if no_of_times_enabled > 0:
                rand.set_weight(
                    float(no_of_times_activated) / float(no_of_times_enabled))
            else:
                rand.set_weight(0.0)

            stochastic_map[el] = rand

    return stochastic_map
Example #6
0
def get_map_from_log_and_net(log,
                             net,
                             initial_marking,
                             final_marking,
                             force_distribution=None,
                             parameters=None):
    """
    Get transition stochastic distribution map given the log and the Petri net

    Parameters
    -----------
    log
        Event log
    net
        Petri net
    initial_marking
        Initial marking of the Petri net
    final_marking
        Final marking of the Petri net
    force_distribution
        If provided, distribution to force usage (e.g. EXPONENTIAL)
    parameters
        Parameters of the algorithm, including:
            Parameters.ACTIVITY_KEY -> activity name
            Parameters.TIMESTAMP_KEY -> timestamp key

    Returns
    -----------
    stochastic_map
        Map that to each transition associates a random variable
    """
    stochastic_map = {}

    if parameters is None:
        parameters = {}

    token_replay_variant = exec_utils.get_param_value(
        Parameters.TOKEN_REPLAY_VARIANT, parameters,
        executor.Variants.TOKEN_REPLAY)
    activity_key = exec_utils.get_param_value(Parameters.ACTIVITY_KEY,
                                              parameters,
                                              xes_constants.DEFAULT_NAME_KEY)
    timestamp_key = exec_utils.get_param_value(
        Parameters.TIMESTAMP_KEY, parameters,
        xes_constants.DEFAULT_TIMESTAMP_KEY)

    parameters_variants = {
        constants.PARAMETER_CONSTANT_ACTIVITY_KEY: activity_key
    }
    variants_idx = variants_module.get_variants_from_log_trace_idx(
        log, parameters=parameters_variants)
    variants = variants_module.convert_variants_trace_idx_to_trace_obj(
        log, variants_idx)

    parameters_tr = {
        token_replay.Parameters.ACTIVITY_KEY: activity_key,
        token_replay.Parameters.VARIANTS: variants
    }

    # do the replay
    aligned_traces = executor.apply(log,
                                    net,
                                    initial_marking,
                                    final_marking,
                                    variant=token_replay_variant,
                                    parameters=parameters_tr)

    element_statistics = performance_map.single_element_statistics(
        log,
        net,
        initial_marking,
        aligned_traces,
        variants_idx,
        activity_key=activity_key,
        timestamp_key=timestamp_key,
        parameters={"business_hours": True})

    for el in element_statistics:
        if type(
                el
        ) is PetriNet.Transition and "performance" in element_statistics[el]:
            values = element_statistics[el]["performance"]

            rand = RandomVariable()
            rand.calculate_parameters(values,
                                      force_distribution=force_distribution)

            no_of_times_enabled = element_statistics[el]['no_of_times_enabled']
            no_of_times_activated = element_statistics[el][
                'no_of_times_activated']

            if no_of_times_enabled > 0:
                rand.set_weight(
                    float(no_of_times_activated) / float(no_of_times_enabled))
            else:
                rand.set_weight(0.0)

            stochastic_map[el] = rand

    return stochastic_map