Beispiel #1
0
def clear():
    # einzelne umgebungen:
    global gesetzt
    global noch_da
    global pfad
    pfad = ""
    noch_da = 4
    gesetzt = False

    global rejection_sam_var
    rejection_sam_var = [graph, "", "", 20]

    global schwellwertffn
    global hintonnumberffn
    global alphaffn
    global ownexample
    ownexample = ""
    schwellwertffn.set(2.5)
    hintonnumberffn.set(20)
    alphaffn.set(0.2)

    # fuer alle:
    anzeigefenster.delete("all")
    printfenster.delete(1.0, END)
    if not error:
        zeichne.draw(graph, "", "")

    umgebung = graph.__class__.__name__

    if umgebung == "FFNetwork":
        matrix = np.empty([15, 15])  # fuer hinton diagramm
        #kantengewichte wieder zufaellig machen!
        for node in graph.get_nodes():
            for edge in node.get_edges():
                weight = random.randint(0, 100) / 100
                edge.set_weight(weight)
            # matrix fuellen:
        for zeile in range(15):
            for spalte in range(15):
                matrix[zeile][spalte] = 0
        for node in graph.get_nodes():
            name = node.name()
            for edge in node.get_edges():
                dest = edge.end().name()
                weight = edge.weight
                # ueberpruefung worein es geschireben wird:
                if name[0] == "I":
                    oben = int(name[1])
                elif name[0] == "H":
                    oben = int(name[1]) + 9
                if dest[0] == "H":
                    runter = int(dest[1]) + 9
                else:
                    runter = 14
                matrix[oben][14 - runter] = weight
                matrix[runter][14 - oben] = weight
        hinton.hinton(matrix)
Beispiel #2
0
def routefinding():
    global graph
    global startm
    global goalm
    global runbutton
    global startlabel
    global goallabel
    global error

    if neu:
        add_clear_and_window()
    else:
        clear_gui()
    # Leiste mit Auswahl für Start, Ziel, properties, Steps, Run
    startlabel = Label(ki, text="Start:")
    startlabel.grid(row=0, column=0)
    startm = OptionMenu(ki, startvar, "Start")
    startm.grid(row=0, column=1)
    goallabel = Label(ki, text="Goal:")
    goallabel.grid(row=0, column=2)
    goalm = OptionMenu(ki, goalvar, "Goal")
    goalm.grid(row=0, column=3)
    runbutton = Button(ki, text="Run", command=run)
    runbutton.grid(row=0, column=4)

    # ordner oeffnen, nur die Map-Files anzeigen:
    # datei ist die Ascii-Datei mit den staedten und strassen.
    folder = os.path.abspath(os.path.dirname("AIMA.py"))
    folder = folder.split("\\aima")[0]
    folder = folder + "\\routen"
    name = filedialog.askopenfilename(filetypes=[("Map files", "*.map")],
                                      initialdir=(folder))
    try:
        datei = open(name).read()

        szenario = []
        for string in datei.split("\n"):
            szenario.append(string)
        name = szenario[0]
        numberofcities = int(szenario[1])
        numberofstreets = int(szenario[2 + numberofcities])
        graph = MapGraph(name, anzeigefenster, printfenster, ki)

        # staedte in graphen aufnehmen, Liste fuer das Menue erweitern.
        new_cities_s = []
        new_cities_g = []
        for element in range(2, 2 + numberofcities, 1):
            szenario[element] = szenario[element].split()
            city = szenario[element][0]
            new_cities_s.append(city)
            new_cities_g.append(city)
            xcoord = int(szenario[element][1])
            ycoord = int(szenario[element][2])
            graph.add_node(city, xcoord, ycoord)
            # neue staedte in optionmenue aufnehmen:
        startm['menu'].delete(0, 'end')
        goalm['menu'].delete(0, 'end')
        for city in new_cities_s:
            startm["menu"].add_command(label=city,
                                       command=tk._setit(startvar, city))
        for city in new_cities_g:
            goalm["menu"].add_command(label=city,
                                      command=tk._setit(goalvar, city))
        startvar.set(new_cities_s[0])
        goalvar.set(new_cities_g[-1])
        # Straßen in graphen aufnehmen
        for element in range(3 + numberofcities,
                             3 + numberofcities + numberofstreets, 1):
            szenario[element] = szenario[element].split()
            start = szenario[element][0]
            end = szenario[element][1]
            start = graph.get_node(start)
            end = graph.get_node(end)
            lengthofroad = int(szenario[element][2])
            graph.add_edge(start.name(), end.name(), lengthofroad)
            # skalieren und zeichnen
        zeichne.draw(graph, "", "")
        error = False
    except:
        printfenster.insert(END, "Error\n")
        error = True
Beispiel #3
0
def load_sampling():
    # ausgabe wird in algo zurueckgegeben
    if not error:
        rejection_sam_var[0] = graph
        ausgabe = importiert[algoindex - 1].main(rejection_sam_var[0],
                                                 rejection_sam_var[1],
                                                 rejection_sam_var[2].split(),
                                                 rejection_sam_var[3])
        # rejecion gibt Zahlen zurueck
        anzeigefenster.delete("all")
        zeichne.draw(graph, "", "")
        if isinstance(ausgabe[0],
                      float):  # hier ist rejection sampling gelaufen!
            pos = round(ausgabe[0], 3)
            neg = round(ausgabe[1], 3)
            printfenster.insert(
                END, "P({0}|{1}) = ({2},{3})".format(rejection_sam_var[1],
                                                     rejection_sam_var[2], pos,
                                                     neg))
            printfenster.insert(END, "\n")
            # ausgabe der wkeit
            # zeichnen(erfragter wird blau, bedinungen fuer die anderen gruen/rot)

            #1. blauer Knoten:
            for node in graph.get_nodes():
                if rejection_sam_var[1] == node.name():
                    x = node.get_x_draw()
                    y = node.get_y_draw()
                    zeichne.zeichne_oval(x, y, x + 60, y + 40, "Lightblue")
                    zeichne.schreibe_text_anzeige(x + 7, y + 20, node.name())
                # positive Knoten:
                for item in rejection_sam_var[2].split():
                    if item == node.name():
                        #wird gruen!
                        x = node.get_x_draw()
                        y = node.get_y_draw()
                        zeichne.zeichne_oval(x, y, x + 60, y + 40, "Green")
                        zeichne.schreibe_text_anzeige(x + 7, y + 20,
                                                      node.name())
                    if item == "-" + node.name():
                        #wird rot!
                        x = node.get_x_draw()
                        y = node.get_y_draw()
                        zeichne.zeichne_oval(x, y, x + 60, y + 40, "Red")
                        zeichne.schreibe_text_anzeige(x + 7, y + 20,
                                                      node.name())
        else:
            printfenster.insert(END, "Neuer Durchgang: ")
            for el in ausgabe:
                printfenster.insert(END, el)
                printfenster.insert(END, " ")
                for node in graph.get_nodes():
                    if el == node.name():
                        #wird gruen
                        x = node.get_x_draw()
                        y = node.get_y_draw()
                        zeichne.zeichne_oval(x, y, x + 60, y + 40, "Green")
                        zeichne.schreibe_text_anzeige(x + 7, y + 20,
                                                      node.name())
                    elif el == "-" + node.name():
                        x = node.get_x_draw()
                        y = node.get_y_draw()
                        zeichne.zeichne_oval(x, y, x + 60, y + 40, "Red")
                        zeichne.schreibe_text_anzeige(x + 7, y + 20,
                                                      node.name())

            printfenster.insert(END, "\n")
    else:
        printfenster.insert(END, "Cannot execute due to previous error!\n")
Beispiel #4
0
def rainbayes():
    global graph
    global bayesrunbutton
    global probtable
    global probslabel
    global probtable
    global probsvar
    global parameterbutton
    global error
    if neu:
        add_clear_and_window()
    else:
        clear_gui()
    probsvar = StringVar(ki)
    probsvar.set("Choose Node")
    probsvar.trace("w", show_prob)  # reagiert, wenn variable sich aendert!
    # buttons etc:
    bayesrunbutton = Button(ki, text="Run", command=load_sampling)
    bayesrunbutton.grid(row=0, column=4)
    probslabel = Label(ki, text="Show Probabilities for: ")
    probslabel.grid(row=0, column=1)
    probtable = OptionMenu(ki, probsvar, "Probabilities")
    probtable.grid(row=0, column=2)
    parameterbutton = Button(ki, text="Properties", command=properties_bayes)
    parameterbutton.grid(row=0, column=3)

    folder = os.path.abspath(os.path.dirname("AIMA.py"))
    folder = folder.split("\\aima")[0]
    folder = folder + "\\bayes"
    name = filedialog.askopenfilename(filetypes=[("Bayes files", "*.bayes")],
                                      initialdir=(folder))
    try:
        newnodes = []
        datei = open(name).read()
        szenario = []
        for string in datei.split("\n"):
            szenario.append(string)
        namegraph = szenario[0]
        graph = BayesGraph(namegraph, anzeigefenster, printfenster, ki)
        numberofnodes = int(szenario[1])
        for item in range(2, 2 + numberofnodes, 1):
            nodewkeit = []
            szenario[item] = szenario[item].split(";")
            nodename = szenario[item][0]
            nodeebene = int(szenario[item][1])
            nodenumberparents = int(szenario[item][2])  # soviele kanten dazu!
            if nodenumberparents > 0:
                nodeparents = szenario[item][3].split()
                table = szenario[item][4]
            else:
                table = szenario[item][3]
            table = table.split(",")
            for line in range(len(table)):
                nodewkeit.append([])
                for el in table[line].split():
                    try:
                        nodewkeit[line].append(float(el))
                    except:
                        el = el.replace('"', '').strip()
                        nodewkeit[line].append(el.replace("+", ", "))
            # das komplementaer dazu:
            for element in range(len(nodewkeit)):
                komp = round(1 - float(nodewkeit[element][-1]), 2)
                nodewkeit[element].append(komp)
            newnodes.append(nodename)
            graph.add_node(nodename, nodewkeit, nodeebene)
            # Jetzt die Kanten:
            node = graph.get_node(nodename)
            for kante in range(nodenumberparents):
                graph.add_edge(node.name(), nodeparents[kante])
                graph.add_edge(nodeparents[kante], node.name())
        zeichne.draw(graph, "", "")
        error = False
        probtable['menu'].delete(0, 'end')
        for node in newnodes:
            probtable["menu"].add_command(label=node,
                                          command=tk._setit(probsvar, node))
    except:
        error = True
        printfenster.insert(END, "Error\n")
Beispiel #5
0
def restaurantffn_net_topology():
    global graph
    global error
    global ffnname

    number_nodes = [0, 0]
    graph = FFNetwork("", anzeigefenster, printfenster, ki)  # fuer clear_gui

    folder = os.path.abspath(os.path.dirname("AIMA.py"))
    folder = folder.split("\\aima")[0]
    path = folder + "\\feedforward"

    #path = os.path.abspath(os.path.dirname(__file__))
    # datei mit selben namen.ffn wie bsp werden geoeffnet
    ffnname = ffnname.split("/")
    ffnname = ffnname[-1]
    ffnname = ffnname.split(".")
    ffnname = ffnname[0]
    name = str(path) + "/" + ffnname + ".ffn"
    print(name)
    try:
        datei = open(name).read()
        matrix = np.empty([15, 15])  # fuer hinton diagramm
        szenario = []
        for string in datei.split("\n"):
            szenario.append(string)
        name = szenario[0]
        numberofnodes = int(szenario[1])
        numberofedges = int(szenario[2 + numberofnodes])
        graph = FFNetwork(name, anzeigefenster, printfenster, ki)
        # knoten und kanten in graphen aufnehmen:
        for element in range(2, 2 + numberofnodes, 1):
            szenario[element] = szenario[element].split()
            nameofnode = szenario[element][0]
            nodetype = szenario[element][1]
            graph.add_node(nameofnode, nodetype)
            if nameofnode[0] == "I":
                number_nodes[0] += 1
            if nameofnode[0] == "H":
                number_nodes[1] += 1
        for element in range(3 + numberofnodes,
                             3 + numberofnodes + numberofedges, 1):
            szenario[element] = szenario[element].split()
            start = szenario[element][0]
            end = szenario[element][1]
            start = graph.get_node(start)
            end = graph.get_node(end)
            weight = random.randint(0, 100) / 100
            graph.add_edge(start.name(), end.name(), weight)
            # matrix fuellen:
        for zeile in range(15):
            for spalte in range(15):
                matrix[zeile][spalte] = 0
        for node in graph.get_nodes():
            name = node.name()
            for edge in node.get_edges():
                dest = edge.end().name()
                weight = edge.weight
                # ueberpruefung worein es geschireben wird:
                if name[0] == "I":
                    oben = int(name[1])
                elif name[0] == "H":
                    oben = int(name[1]) + 9
                if dest[0] == "H":
                    runter = int(dest[1]) + 9
                else:
                    runter = 14
                matrix[oben][14 - runter] = weight
                matrix[runter][14 - oben] = weight
        hinton.hinton(matrix)
        # unspruengliche Matrix mit noch zufaelligen Werten anzeigen:
        plt.show()
        # testen ob genug input/output
        output = 0
        input = 0
        for node in graph.get_nodes():
            if node.get_type() == "output":
                output += 1
            if node.get_type() == "input":
                input += 1
        if (output == 0 or input == 0):
            printfenster.insert(END, "Not enough Input/Outputnodes")
        zeichne.draw(graph, "", "")
        return number_nodes
    except:
        error = True
        printfenster.insert(END, "Error\n")
Beispiel #6
0
def restaurantffn():
    global graph
    global backprob
    global img
    global examplebutton
    global backproperties
    global error
    global ffnname

    if neu:
        add_clear_and_window()
    else:
        clear_gui()
    if graph.__class__.__name__ == "FFNetwork":
        zeichne.draw(graph, "", "")
    number_inputnodes = 0
    number_hiddennodes = 0
    # Buttons etc:
    backprob = Button(ki,
                      text="Backpropagation",
                      command=lambda: backpropagation(ex, number_inputnodes,
                                                      number_hiddennodes))
    backprob.grid(row=0, column=5)
    backproperties = Button(ki, text="Poperties", command=properties_ffn)
    backproperties.grid(row=0, column=2)
    examplebutton = Button(ki,
                           text="Insert own Example!",
                           command=enterbackprop)
    examplebutton.grid(row=0, column=4)

    # bsp aufnhemen:
    folder = os.path.abspath(os.path.dirname("AIMA.py"))
    folder = folder.split("\\aima")[0]
    folder = folder + "\\feedforward"
    name = filedialog.askopenfilename(filetypes=[("Feature files", "*.feat")],
                                      initialdir=(folder))
    ffnname = name
    try:
        datei = open(name).read()
        ex = []
        szenario = []
        for string in datei.split("\n"):
            szenario.append(string)
        name = szenario[0]
        numberofexamples = int(szenario[3])
        for element in range(4, 4 + numberofexamples, 1):
            ex.append([])
            szenario[element] = szenario[element].split()
            for item in szenario[element]:
                if item == "Yes" or item == "Full" or item == "Expensive":
                    bewertung = 1
                elif item == "No" or item == "None" or item == "Cheap":
                    bewertung = -1
                elif item == "Some" or item == "Okay":
                    bewertung = 0
                elif item == "0-10" or item == "French":
                    bewertung = 1
                elif item == "10-30" or item == "Thai":
                    bewertung = 0.5
                elif item == "30-60" or item == "Burger":
                    bewertung = -0.5
                elif item == ">60" or item == "Italian":
                    bewertung = -1
                ex[element - 4].append(bewertung)
        error = False
        number_nodes = restaurantffn_net_topology()
        number_inputnodes = number_nodes[0]
        number_hiddennodes = number_nodes[1]
    except:
        printfenster.insert(END, "Error\n")
        error = True