예제 #1
0
def randomG():
    rows = random.randint(4, 16)
    cols = random.randint(4, 16)
    rowSize = 480 // rows
    colSize = 480 // cols
    safeMaxRowPos = rowSize - 8
    safeMaxColPos = rowSize - 8

    vertexList = []
    for row in range(rows):
        vertexList.append([])
        for col in range(cols):
            x = row * rowSize + random.randint(8, safeMaxRowPos)
            y = col * colSize + random.randint(8, safeMaxColPos)
            vertexList[row].append((x, y))

    vS = vertexList[0][0]
    vG = vertexList[rows - 1][cols - 1]

    VSet = dict()
    for row in range(rows):
        for col in range(cols):
            pos = vertexList[row][col]
            v = frame.vertex(pos)
            v.setPotential(getDist(pos, vG), getDist(pos, vS))
            VSet[pos] = v

    ESet = dict()
    for row in range(rows):
        for col in range(cols):
            uPos = vertexList[row][col]
            u = VSet[uPos]
            if row != rows - 1:
                vPos = vertexList[row + 1][col]
                v = VSet[vPos]
                e = frame.edge(u, v)
                e.setWeight(getDist(uPos, vPos))
                u.addEdge(e)
                v.addEdgeR(e)
                ESet[(uPos, vPos)] = e
            if col != cols - 1:
                vPos = vertexList[row][col + 1]
                v = VSet[vPos]
                e = frame.edge(u, v)
                e.setWeight(getDist(uPos, vPos))
                u.addEdge(e)
                v.addEdgeR(e)
                ESet[(uPos, vPos)] = e

    vertexS = VSet[vS]
    vertexG = VSet[vG]

    return (VSet, ESet, vertexS, vertexG)
예제 #2
0
def decodeG(V, E, S, G):
    VSet = dict()
    for pos, pS, pG in V:
        v = frame.vertex(pos)
        v.setPotential(pS, pG)
        VSet[pos] = v

    ESet = dict()
    for uPos, vPos, w in E:
        u = VSet[uPos]
        v = VSet[vPos]
        e = frame.edge(u, v)
        e.setWeight(w)
        u.addEdge(e)
        v.addEdgeR(e)
        ESet[(uPos, vPos)] = e

    if S is None:
        vertexS = None
    else:
        vertexS = VSet[S]
    if G is None:
        vertexG = None
    else:
        vertexG = VSet[G]
    return (VSet, ESet, vertexS, vertexG)
예제 #3
0
    def mouseReleaseEvent(self, event):
        #new elements
        if self.tool == 0 or self.tool == 1 or self.tool == 2 or self.tool == 3:
            if self.vertex:
                self.vertexPosX, self.vertexPosY = self.getPos(event)
                tPos = (self.vertexPosX, self.vertexPosY)
                pos = self.matchVertex(tPos)  #DO NOT use tPos instead of pos
                if pos[0] is not None:
                    self.valid = False
                else:
                    tempVertex = frame.vertex(tPos)
                    res = self.editVertex(tempVertex)
                    if not res:
                        self.valid = False
                    else:
                        self.valid = True
                        self.main.BDASBtn.setEnabled(False)
                        self.vertexSet[tPos] = tempVertex
                        if self.tool == 2:
                            self.vertexS = tempVertex
                        elif self.tool == 3:
                            self.vertexG = tempVertex

            elif self.edge:
                self.edgeGPosX, self.edgeGPosY = self.getPos(event)
                tUPos = (self.edgeSPosX, self.edgeSPosY)
                tVPos = (self.edgeGPosX, self.edgeGPosY)
                if self.valid:
                    tVPos = self.matchVertex(tVPos)
                    if tVPos[0] is not None:
                        if tUPos != tVPos:
                            tPos = (tUPos, tVPos)
                            self.edgeGPosX, self.edgeGPosY = tVPos
                            tempEdge = frame.edge(self.vertexSet[tUPos],
                                                  self.vertexSet[tVPos])
                            res = self.editEdge(tempEdge)
                            if not res:
                                self.valid = False
                            else:
                                self.valid = True
                                self.main.BDASBtn.setEnabled(False)
                                self.edgeSet[tPos] = tempEdge
                                self.vertexSet[tUPos].addEdge(tempEdge)
                                self.vertexSet[tVPos].addEdgeR(tempEdge)
                        else:
                            self.valid = False
                    else:
                        self.valid = False

        #edit elementes
        elif self.tool == 4 or self.tool == 5:
            if self.vertex:
                self.vertexPosX, self.vertexPosY = self.getPos(event)
                tPos = (self.vertexPosX, self.vertexPosY)
                tPos = self.matchVertex(tPos)
                if tPos[0] is not None:
                    if tPos != (self.vertexPPosX, self.vertexPPosY):
                        self.valid = False
                    else:
                        self.vertexPosX, self.vertexPosY = tPos
                        res = self.editVertex(self.vertexSet[tPos])
                        if not res:
                            self.valid = False
                        else:
                            self.valid = True
                            self.main.BDASBtn.setEnabled(False)
                else:
                    self.valid = False

            elif self.edge:
                self.edgeGPosX, self.edgeGPosY = self.getPos(event)
                tUPos = (self.edgeSPosX, self.edgeSPosY)
                tVPos = (self.edgeGPosX, self.edgeGPosY)
                if self.valid:
                    tVPos = self.matchVertex(tVPos)
                    if tVPos[0] is not None:
                        tPos = (tUPos, tVPos)
                        if tPos in self.edgeSet:
                            self.edgeGPosX, self.edgeGPosY = tVPos
                            res = self.editEdge(self.edgeSet[(tUPos, tVPos)])
                            if not res:
                                self.valid = False
                            else:
                                self.valid = True
                                self.main.BDASBtn.setEnabled(False)
                        else:
                            self.valid = False
                    else:
                        self.valid = False

        #delete elements
        elif self.tool == 6 or self.tool == 7:
            if self.vertex:
                self.vertexPosX, self.vertexPosY = self.getPos(event)
                tPos = (self.vertexPosX, self.vertexPosY)
                tPos = self.matchVertex(tPos)
                if tPos[0] is not None:
                    if tPos != (self.vertexPPosX, self.vertexPPosY):
                        self.valid = False
                    else:
                        self.valid = True
                        self.main.BDASBtn.setEnabled(False)
                        self.vertexPosX, self.vertexPosY = tPos
                        tempVertex = self.vertexSet.pop(tPos)
                        self.deleteVertex(tempVertex)

                        if tempVertex is self.vertexS:
                            self.vertexS = None
                        elif tempVertex is self.vertexG:
                            self.vertexG = None
                else:
                    self.valid = False

            elif self.edge:
                self.edgeGPosX, self.edgeGPosY = self.getPos(event)
                tUPos = (self.edgeSPosX, self.edgeSPosY)
                tVPos = (self.edgeGPosX, self.edgeGPosY)
                if self.valid:
                    tVPos = self.matchVertex(tVPos)
                    if tVPos[0] is not None:
                        tPos = (tUPos, tVPos)
                        if tPos in self.edgeSet:
                            self.valid = True
                            self.main.BDASBtn.setEnabled(False)
                            self.edgeGPosX, self.edgeGPosY = tVPos
                            tempEdge = self.edgeSet.pop(tPos)
                            self.deleteEdge(tempEdge)
                        else:
                            self.valid = False
                    else:
                        self.valid = False

        self.update()
        return
예제 #4
0
            if col != cols - 1:
                vPos = vertexList[row][col + 1]
                v = VSet[vPos]
                e = frame.edge(u, v)
                e.setWeight(getDist(uPos, vPos))
                u.addEdge(e)
                v.addEdgeR(e)
                ESet[(uPos, vPos)] = e

    vertexS = VSet[vS]
    vertexG = VSet[vG]

    return (VSet, ESet, vertexS, vertexG)


def getDist(u, v):
    a, b = u
    c, d = v
    return math.floor(pow(pow(a - c, 2) + pow(b - d, 2), 0.5))


if __name__ == '__main__':
    vertexSet = dict()
    edgeSet = dict()
    a = frame.vertex((1, 1))
    b = frame.vertex((2.2))
    vertexSet[(1, 1)] = a
    vertexSet[(2, 2)] = b
    e = frame.edge(a, b)
    edgeSet[((1, 1), (2, 2))] = e
    saveG((vertexSet, edgeSet), './', 'graph.pkl')