Beispiel #1
0
def earth_mover_distance(real_logs, petri_nets):
    language = []
    dic = {}
    for i in range(8):
        language.append(variants_module.get_language(real_logs[i]))
    for i, variant in enumerate(variants):
        net, im, fm = petri_nets[i]
        emd = []
        for j in range(8):
            playout_log = simulator.apply(
                net[j],
                im[j],
                fm[j],
            )
            playout_log = simulator.apply(
                net[j],
                im[j],
                fm[j],
                parameters={
                    simulator.Variants.STOCHASTIC_PLAYOUT.value.Parameters.LOG:
                    playout_log  # noqa: E501
                },
                variant=simulator.Variants.STOCHASTIC_PLAYOUT,
            )
            model_language = variants_module.get_language(playout_log)
            emd.append(evaluator.apply(model_language, language[j]))
        dic[variant] = emd
    plot(
        pd.DataFrame(dic, index=list(range(1, 9))),
        "Earth Mover Distance",
        "earth_mover_distance.png",
        ylabel="emd",
    )
Beispiel #2
0
 def test_playout(self):
     log = xes_importer.apply(
         os.path.join("input_data", "running-example.xes"))
     from pm4py.algo.discovery.alpha import algorithm as alpha_miner
     net, im, fm = alpha_miner.apply(log)
     from pm4py.simulation.playout import simulator
     log2 = simulator.apply(net, im, fm)
Beispiel #3
0
 def test_emd_2(self):
     log = xes_importer.apply(os.path.join("input_data", "running-example.xes"))
     lang_log = variants_get.get_language(log)
     net1, im1, fm1 = inductive_miner.apply(log)
     lang_model1 = variants_get.get_language(
         simulator.apply(net1, im1, fm1, variant=simulator.Variants.STOCHASTIC_PLAYOUT,
                         parameters={simulator.Variants.STOCHASTIC_PLAYOUT.value.Parameters.LOG: log}))
     emd = earth_mover_distance.apply(lang_model1, lang_log)
def generate_one_trace_log(n):

    # here we create a petri net with n transitions and n+1 places, which can replay a single trace of length n
    def create_PetriNet(n):
        # label_set[0] = 'a',...,label_set[26]=z
        label_set = dict(enumerate(string.ascii_lowercase))
        if n >= 26:
            print("N is >= 26 but there are only 26 unique letters.")

        # create new petrinet
        net = PetriNet("PN")

        place_list = []
        # create start and end place
        start = PetriNet.Place("start")
        end = PetriNet.Place("end")
        net.places.add(start)
        net.places.add(end)
        place_list.insert(0, start)
        place_list.insert(n + 1, end)

        # create the rest of n-2 places (n+1 places are needed for a length n trace)
        for i in range(n - 1):
            p = PetriNet.Place(f"p{i}")
            net.places.add(p)
            place_list.insert(i + 1, p)

        transitions_list = []
        # create n transitions
        for i in range(n):
            t = PetriNet.Transition(f"t{i}", label_set[i])
            net.transitions.add(t)
            transitions_list.insert(i, t)

        for i, t in enumerate(transitions_list):
            utils.add_arc_from_to(place_list[i], t, net)
            utils.add_arc_from_to(t, place_list[i + 1], net)

        # defining initial and final marking
        im = Marking()
        im[start] = 1
        fm = Marking()
        fm[end] = 1

        return net, im

    petrinet, im = create_PetriNet(n)
    # by using the extensive variant, we obtain the set of all possible runs
    # there is only one trace with length <27 (can only generate trace with up to 26 distinct letters as transition labels)
    simulated_log = simulator.apply(petrinet, initial_marking=im, variant=simulator.Variants.EXTENSIVE, parameters={simulator.Variants.EXTENSIVE.value.Parameters.MAX_TRACE_LENGTH: 30})

    return simulated_log
Beispiel #5
0
def generate_process_tree(**kwargs) -> ProcessTree:
    """
    Generates a process tree

    Parameters
    -------------
    kwargs
        Parameters of the process tree generator algorithm

    Returns
    -------------
    model
        process tree
    """
    from pm4py.simulation.tree_generator import simulator
    return simulator.apply(**kwargs)
Beispiel #6
0
def play_out(*args: Union[Tuple[PetriNet, Marking, Marking], dict, Counter,
                          ProcessTree], **kwargs) -> EventLog:
    """
    Performs the playout of the provided model,
    i.e., gets a set of traces from the model.
    The function either takes a petri net, initial and final marking, or, a process tree as an input.

    Parameters
    ---------------
    args
        Model (Petri net, initial, final marking) or ProcessTree
    kwargs
        Parameters of the playout

    Returns
    --------------
    log
        Simulated event log
    """
    if len(args) == 3:
        from pm4py.objects.petri.petrinet import PetriNet
        if type(args[0]) is PetriNet:
            from pm4py.simulation.playout import simulator
            return simulator.apply(args[0],
                                   args[1],
                                   final_marking=args[2],
                                   **kwargs)
        elif type(args[0]) is dict or type(args[0]) is Counter:
            from pm4py.objects.dfg.utils import dfg_playout
            return dfg_playout.apply(args[0], args[1], args[2], **kwargs)
    elif len(args) == 1:
        from pm4py.objects.process_tree.process_tree import ProcessTree
        if type(args[0]) is ProcessTree:
            from pm4py.simulation.tree_playout import algorithm
            return algorithm.apply(args[0], **kwargs)
    raise Exception("unsupported model for playout")
fm[end] = 1

    # visualizing the petrinet
    #from pm4py.objects.petri.exporter import exporter as pnml_exporter

    #pnml_exporter.apply(net, im, "createdPetriNet1.pnml", final_marking=fm)

    #from pm4py.visualization.petrinet import visualizer as pn_visualizer

    #gviz = pn_visualizer.apply(net, im, fm)
    #pn_visualizer.view(gviz)


#by using the extensive variant, we obtain the set of all possible runs
#there are 6 possible traces
simulated_log = simulator.apply(net, im, variant=simulator.Variants.EXTENSIVE, parameters={simulator.Variants.EXTENSIVE.value.Parameters.MAX_TRACE_LENGTH: 5})

#saving possible traces in a dictionary
possible_traces = {}
possible_traces[0] = ['a', 'b', 'e']
possible_traces[1] = ['a', 'b', 'd', 'e']
possible_traces[2] = ['a', 'd', 'b', 'e']
possible_traces[3] = ['a', 'c', 'e']
possible_traces[4] = ['a', 'c', 'd', 'e']
possible_traces[5] = ['a', 'd', 'c', 'e']

#creating stochastic map assigning probabilities to transitions
smap = {}
for t in transitions:
    rand = RandomVariable()
    #we do not use the uniform distribution, but we need some kind of distribution to initialize
Beispiel #8
0
def execute_script():
    M = {
        ("a", "b", "d", "e"): 0.49,
        ("a", "d", "b", "e"): 0.49,
        ("a", "c", "d", "e"): 0.01,
        ("a", "d", "c", "e"): 0.01
    }

    L1 = {
        ("a", "b", "d", "e"): 0.49,
        ("a", "d", "b", "e"): 0.49,
        ("a", "c", "d", "e"): 0.01,
        ("a", "d", "c", "e"): 0.01
    }
    # the distance between M and L1, that we expect to be zero, is zero according to the EMD
    emd = earth_mover_distance.apply(M, L1)
    print("M L1 emd distance:", emd)

    L2 = {
        ("a", "b", "e"): 0.5,
        ("a", "b", "d", "e"): 0.245,
        ("a", "d", "b", "e"): 0.245,
        ("a", "c", "d", "e"): 0.005,
        ("a", "d", "c", "e"): 0.005
    }
    # the distance between M and L2 according to the EMD is 0.1275 (paper value 0.125)
    emd = earth_mover_distance.apply(M, L2)
    print("M L2 emd distance:", emd)

    L3 = {
        ("a", "b", "d", "e"): 0.489,
        ("a", "d", "b", "e"): 0.489,
        ("a", "c", "d", "e"): 0.01,
        ("a", "d", "c", "e"): 0.01,
        ("a", "b", "e"): 0.002
    }
    # the distance between M and L3 according to the EMD is 0.0005 (paper value 0.0005), perfect!
    emd = earth_mover_distance.apply(M, L3)
    print("M L3 emd distance:", emd)

    L4 = {("a", "b", "d", "e"): 0.5, ("a", "d", "b", "e"): 0.5}
    # the distance between M and L4 according to the EMD is 0.005 (paper value 0.005), perfect!
    emd = earth_mover_distance.apply(M, L4)
    print("M L4 emd distance:", emd)

    L5 = {("a", "c", "d", "e"): 0.5, ("a", "d", "c", "e"): 0.5}
    # the distance between M and L5 according to the EMD is 0.245 (paper value 0.245), perfect!
    emd = earth_mover_distance.apply(M, L5)
    print("M L5 emd distance:", emd)

    log = xes_importer.apply(
        os.path.join("..", "tests", "input_data", "running-example.xes"))
    lang_log = variants_get.get_language(log)
    net0, im0, fm0 = alpha_miner.apply(log)
    net1, im1, fm1 = inductive_miner.apply(log)
    lang_model0 = variants_get.get_language(
        simulator.apply(
            net0,
            im0,
            fm0,
            variant=simulator.Variants.STOCHASTIC_PLAYOUT,
            parameters={
                simulator.Variants.STOCHASTIC_PLAYOUT.value.Parameters.LOG: log
            }))
    lang_model1 = variants_get.get_language(
        simulator.apply(
            net1,
            im1,
            fm1,
            variant=simulator.Variants.STOCHASTIC_PLAYOUT,
            parameters={
                simulator.Variants.STOCHASTIC_PLAYOUT.value.Parameters.LOG: log
            }))
    emd = earth_mover_distance.apply(lang_model0, lang_log)
    print("running-example alpha emd distance: ", emd)
    emd = earth_mover_distance.apply(lang_model1, lang_log)
    print("running-example inductive emd distance: ", emd)
Beispiel #9
0
            a32f0n00_log,
            a32f0n00_net,
            a32f0n00_im,
            a32f0n00_fm,
            variant=alignments.Variants.VERSION_DIJKSTRA_NO_HEURISTICS)
        t1 = time.time()
        T4[0] = (t1 - t0)
        T4[2] = math.ceil(T4[1] / (T4[0] + 0.00000001) * 1000.0)
        print(
            "TEST 4 - Applying alignments between A32F0N00 log and model - %.5f s (test score: %d)"
            % (T4[0], T4[2]))

    if ENABLE_TESTS:
        # TEST 5: perform playout of the a32f0n00 Petri net
        t0 = time.time()
        playout.apply(a32f0n00_net, a32f0n00_im, a32f0n00_fm)
        t1 = time.time()
        T5[0] = (t1 - t0)
        T5[2] = math.ceil(T5[1] / (T5[0] + 0.00000001) * 1000.0)
        print("TEST 5 - Doing playout - %.5f s (test score: %d)" %
              (T5[0], T5[2]))

    if ENABLE_TESTS:
        # TEST 6: discover DFG from Pandas
        t0 = time.time()
        pd_dfg_discovery.get_dfg_graph(roadtraffic_df)
        t1 = time.time()
        T6[0] = (t1 - t0)
        T6[2] = math.ceil(T6[1] / (T6[0] + 0.00000001) * 1000.0)
        print(
            "TEST 6 - Discovering DFG from Pandas - %.5f s (test score: %d)" %