Examples of mxCostFunction


Examples of com.mxgraph.costfunction.mxCostFunction

    int[] currCoords = new int[2];
    Object oldMove = vertices[startVertexValue];
    currCoords = getVertexGridCoords(xDim, yDim, startVertexValue);
    resultPath.add(oldMove);
    Object nextMove = getNextKnightMove(aGraph, xDim, yDim, currCoords[0], currCoords[1], resultPath);
    mxCostFunction costFunction = aGraph.getGenerator().getCostFunction();
    mxGraphView view = graph.getView();
   
    //the main loop
    while (nextMove != null)
    {
      // connect current with the possible move that has minimum number of its (possible moves)
      graph.insertEdge(parent, null, null, oldMove, nextMove);
      resultPath.add(nextMove);
      // that vertex becomes the current vertex and we repeat until no possible moves
     
      currValue = (int) costFunction.getCost(new mxCellState(view, nextMove, null));
      currCoords = getVertexGridCoords(xDim, yDim, currValue);
      oldMove = nextMove;
      nextMove = getNextKnightMove(aGraph, xDim, yDim, currCoords[0], currCoords[1], resultPath);
    }
View Full Code Here

Examples of com.mxgraph.costfunction.mxCostFunction

    Object[] possibleMoves = getKnightMoveVertexes(aGraph, xDim, yDim, xCoord, yCoord);
    //get the position with minimum possible moves
    int minMoveNum = 9;
    float biggestDistance = 0;
    Object currVertex = null;
    mxCostFunction costFunction = aGraph.getGenerator().getCostFunction();
    mxGraphView view = aGraph.getGraph().getView();
   
    for (int i = 0; i < possibleMoves.length; i++)
    {
      int currValue = (int) costFunction.getCost(new mxCellState(view, possibleMoves[i], null));
      int[] currCoords = getVertexGridCoords(xDim, yDim, currValue);
      int currMoveNum = getPossibleKnightMoveCount(aGraph, xDim, yDim, currCoords[0], currCoords[1]);
      float currDistance = getDistanceFromGridCenter(xDim, yDim, currValue);

      if ((currMoveNum < minMoveNum || (currMoveNum == minMoveNum && currDistance > biggestDistance))
View Full Code Here

Examples of com.mxgraph.costfunction.mxCostFunction

            {
              List<Map<Object, Object>> bellmanFord = mxTraversal.bellmanFord(aGraph, startVertex);

              Map<Object, Object> distanceMap = bellmanFord.get(0);
              Map<Object, Object> parentMap = bellmanFord.get(1);
              mxCostFunction costFunction = aGraph.getGenerator().getCostFunction();
              mxGraphView view = aGraph.getGraph().getView();

              System.out.println("Bellman-Ford traversal test");
              Object[] vertices = aGraph.getChildVertices(aGraph.getGraph().getDefaultParent());
              int vertexNum = vertices.length;

              System.out.print("Distances from " + costFunction.getCost(view.getState(startVertex)) + " to [ ");

              for (int i = 0; i < vertexNum; i++)
              {
                System.out.print(i + ":" + Math.round((Double) distanceMap.get(vertices[i]) * 100.0) / 100.0 + " ");
              }

              System.out.println("]");

              System.out.print("Parents are [ ");

              for (int i = 0; i < vertexNum; i++)
              {
                System.out.print(i + ":" + costFunction.getCost(view.getState(parentMap.get(vertices[i]))) + " ");
              }

              System.out.println("]");

              if ((Double) distanceMap.get(endVertex) != Double.MAX_VALUE)
              {
                System.out.println("The shortest distance from vertex " + costFunction.getCost(view.getState(startVertex))
                    + " to vertex " + (Double) costFunction.getCost(view.getState(endVertex)) + " is: "
                    + distanceMap.get(endVertex));
              }
              else
              {
                System.out.println("The selected vertices aren't connected.");
View Full Code Here

Examples of com.mxgraph.costfunction.mxCostFunction

    Object[] vertices = aGraph.getChildVertices(aGraph.getGraph().getDefaultParent());

    int childNum = vertices.length;
    int vertexValue = 0;
    mxCostFunction costFunction = aGraph.getGenerator().getCostFunction();
    mxGraphView view = graph.getView();

    for (int i = 0; i < childNum; i++)
    {
      Object currVertex = vertices[i];

      vertexValue = (int) costFunction.getCost(new mxCellState(view, currVertex, null));

      if (vertexValue == value)
      {
        return currVertex;
      }
View Full Code Here

Examples of com.mxgraph.costfunction.mxCostFunction

   */
  public static boolean isCutEdge(mxAnalysisGraph aGraph, Object edge)
  {
    mxGraph graph = aGraph.getGraph();
    mxIGraphModel model = graph.getModel();
    mxCostFunction costFunction = aGraph.getGenerator().getCostFunction();
    mxGraphView view = graph.getView();

    int srcValue = (int) costFunction.getCost(new mxCellState(view, aGraph.getTerminal(edge, true), null));
    int destValue = (int) costFunction.getCost(new mxCellState(view, aGraph.getTerminal(edge, false), null));

    if (aGraph.getTerminal(edge, false) != null || aGraph.getTerminal(edge, true) != null)
    {
      Object[] cells = model.cloneCells(aGraph.getChildCells(graph.getDefaultParent(), true, true), true);
      mxGraphModel modelCopy = new mxGraphModel();
      mxGraph graphCopy = new mxGraph(modelCopy);
      graphCopy.addCells(cells);
      mxAnalysisGraph aGraphCopy = new mxAnalysisGraph();
      aGraphCopy.setGraph(graphCopy);
      aGraphCopy.setGenerator(aGraph.getGenerator());
      aGraphCopy.setProperties(aGraph.getProperties());

      Object[] edges = aGraphCopy.getChildEdges(aGraphCopy.getGraph().getDefaultParent());
      Object currEdge = edges[0];
      mxCostFunction costFunctionCopy = aGraphCopy.getGenerator().getCostFunction();
      mxGraphView viewCopy = graphCopy.getView();

      int currSrcValue = (int) costFunctionCopy.getCost(new mxCellState(viewCopy, aGraphCopy.getTerminal(currEdge, true), null));
      int currDestValue = (int) costFunctionCopy.getCost(new mxCellState(viewCopy, aGraphCopy.getTerminal(currEdge, false), null));
      int i = 0;

      while (currSrcValue != srcValue || currDestValue != destValue)
      {
        i++;
View Full Code Here

Examples of com.mxgraph.costfunction.mxCostFunction

      vertexList.add((Object) vertexes[i]);
      vertexListStatic.add((Object) vertexes[i]);
    }

    distances[vertexListStatic.indexOf(startVertex)] = 0;
    mxCostFunction costFunction = aGraph.getGenerator().getCostFunction();
    mxGraphView view = aGraph.getGraph().getView();

    while (vertexList.size() > 0)
    {
      //find closest vertex
      double minDistance;
      Object currVertex;
      Object closestVertex;
      currVertex = vertexList.get(0);
      int currIndex = vertexListStatic.indexOf(currVertex);
      double currDistance = distances[currIndex];
      minDistance = currDistance;
      closestVertex = currVertex;

      if (vertexList.size() > 1)
      {
        for (int i = 1; i < vertexList.size(); i++)
        {
          currVertex = vertexList.get(i);
          currIndex = vertexListStatic.indexOf(currVertex);
          currDistance = distances[currIndex];

          if (currDistance < minDistance)
          {
            minDistance = currDistance;
            closestVertex = currVertex;
          }
        }
      }

      // we found the closest vertex
      vertexList.remove(closestVertex);

      Object currEdge = new Object();
      Object[] neighborVertices = aGraph.getOpposites(aGraph.getEdges(closestVertex, null, true, true, false, true), closestVertex,
          true, true);

      for (int j = 0; j < neighborVertices.length; j++)
      {
        Object currNeighbor = neighborVertices[j];

        if (vertexList.contains(currNeighbor))
        {
          //find edge that connects to the current vertex
          Object[] neighborEdges = aGraph.getEdges(currNeighbor, null, true, true, false, true);
          Object connectingEdge = null;

          for (int k = 0; k < neighborEdges.length; k++)
          {
            currEdge = neighborEdges[k];

            if (aGraph.getTerminal(currEdge, true).equals(closestVertex)
                || aGraph.getTerminal(currEdge, false).equals(closestVertex))
            {
              connectingEdge = currEdge;
            }
          }

          // check for new distance
          int neighborIndex = vertexListStatic.indexOf(currNeighbor);
          double oldDistance = distances[neighborIndex];
          double currEdgeWeight;

          currEdgeWeight = costFunction.getCost(new mxCellState(view, connectingEdge, null));

          double newDistance = minDistance + currEdgeWeight;

          //final part - updating the structure
          if (newDistance < oldDistance)
View Full Code Here

Examples of com.mxgraph.costfunction.mxCostFunction

    Object[] edges = aGraph.getChildEdges(graph.getDefaultParent());
    int vertexNum = vertices.length;
    int edgeNum = edges.length;
    Map<Object, Object> distanceMap = new HashMap<Object, Object>();
    Map<Object, Object> parentMap = new HashMap<Object, Object>();
    mxCostFunction costFunction = aGraph.getGenerator().getCostFunction();
    mxGraphView view = graph.getView();

    for (int i = 0; i < vertexNum; i++)
    {
      Object currVertex = vertices[i];
      distanceMap.put(currVertex, Double.MAX_VALUE);
    }

    distanceMap.put(startVertex, 0.0);
    parentMap.put(startVertex, startVertex);

    for (int i = 0; i < vertexNum; i++)
    {
      for (int j = 0; j < edgeNum; j++)
      {
        Object currEdge = edges[j];
        Object source = aGraph.getTerminal(currEdge, true);
        Object target = aGraph.getTerminal(currEdge, false);

        double dist = (Double) distanceMap.get(source) + costFunction.getCost(new mxCellState(view, currEdge, null));

        if (dist < (Double) distanceMap.get(target))
        {
          distanceMap.put(target, dist);
          parentMap.put(target, source);
        }

        //for undirected graphs, check the reverse direction too
        if (!mxGraphProperties.isDirected(aGraph.getProperties(), mxGraphProperties.DEFAULT_DIRECTED))
        {
          dist = (Double) distanceMap.get(target) + costFunction.getCost(new mxCellState(view, currEdge, null));

          if (dist < (Double) distanceMap.get(source))
          {
            distanceMap.put(source, dist);
            parentMap.put(source, target);
          }
        }

      }
    }

    for (int i = 0; i < edgeNum; i++)
    {
      Object currEdge = edges[i];
      Object source = aGraph.getTerminal(currEdge, true);
      Object target = aGraph.getTerminal(currEdge, false);

      double dist = (Double) distanceMap.get(source) + costFunction.getCost(new mxCellState(view, currEdge, null));

      if (dist < (Double) distanceMap.get(target))
      {
        throw new StructuralException("The graph contains a negative cycle, so Bellman-Ford can't be completed.");
      }
View Full Code Here

Examples of com.mxgraph.costfunction.mxCostFunction

    {
      Arrays.fill(weight[i], Double.MAX_VALUE);
    }

    boolean isDirected = mxGraphProperties.isDirected(aGraph.getProperties(), mxGraphProperties.DEFAULT_DIRECTED);
    mxCostFunction costFunction = aGraph.getGenerator().getCostFunction();
    mxGraphView view = aGraph.getGraph().getView();

    for (Object currEdge : edges)
    {
      Object source = aGraph.getTerminal(currEdge, true);
      Object target = aGraph.getTerminal(currEdge, false);

      weight[indexMap.get(source)][indexMap.get(target)] = costFunction.getCost(view.getState(currEdge));

      if (!isDirected)
      {
        weight[indexMap.get(target)][indexMap.get(source)] = costFunction.getCost(view.getState(currEdge));
      }
    }

    for (int i = 0; i < nodes.length; i++)
    {
View Full Code Here

Examples of com.mxgraph.costfunction.mxCostFunction

      }
    }

    if (startVertex != targetVertex)
    {
      mxCostFunction cf = aGraph.getGenerator().getCostFunction();
      mxGraphView view = aGraph.getGraph().getView();
      ArrayList<Object> currPath = new ArrayList<Object>();
      currPath.add(startVertex);

      while (startVertex != targetVertex)
View Full Code Here

Examples of com.mxgraph.costfunction.mxCostFunction

              System.out.println("] ");
            }

            System.out.println("Path info:");

            mxCostFunction costFunction = aGraph.getGenerator().getCostFunction();
            mxGraphView view = aGraph.getGraph().getView();

            for (int i = 0; i < vertexNum; i++)
            {
              System.out.print("[");

              for (int j = 0; j < vertexNum; j++)
              {
                if (paths[i][j] != null)
                {
                  System.out.print(" " + costFunction.getCost(view.getState(paths[i][j])));
                }
                else
                {
                  System.out.print(" -");
                }
              }

              System.out.println(" ]");
            }

            try
            {
              Object[] path = mxTraversal.getWFIPath(aGraph, FWIresult, vertices[0], vertices[vertexNum - 1]);
              System.out.print("The path from " + costFunction.getCost(view.getState(vertices[0])) + " to "
                  + costFunction.getCost((view.getState(vertices[vertexNum - 1]))) + " is:");

              for (int i = 0; i < path.length; i++)
              {
                System.out.print(" " + costFunction.getCost(view.getState(path[i])));
              }

              System.out.println();
            }
            catch (StructuralException e1)
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.