コード例 #1
0
ファイル: Utils.java プロジェクト: azinazadi/TripNet
 public static GraphModel getACopy(GraphModel g) {
   GraphModel ret = new GraphModel(g.isDirected());
   VertexModel[] map = new VertexModel[g.getVerticesCount()];
   for (VertexModel v : g) {
     VertexModel t = new VertexModel(v);
     map[v.getId()] = t;
     ret.insertVertex(t);
   }
   for (EdgeModel e : g.edges()) {
     EdgeModel t = new EdgeModel(e, map[e.source.getId()], map[e.target.getId()]);
     ret.insertEdge(t);
     setDup(e, t);
   }
   return ret;
 }
コード例 #2
0
ファイル: Utils.java プロジェクト: azinazadi/TripNet
  public static GraphModel createIPGraph(Vector<Triplet> tow) {
    // create the DAG
    dag = new GraphModel(true);
    HashMap<Object, VertexModel> V = new HashMap<Object, VertexModel>();
    for (Triplet t : tow) {
      VertexModel ik = getVertex(t.i, t.k, dag, V);
      VertexModel jk = getVertex(t.j, t.k, dag, V);
      VertexModel ij = getVertex(t.i, t.j, dag, V);
      EdgeModel e = new EdgeModel(ij, ik);
      e.getProp().obj = t;
      dag.insertEdge(e);
      e = new EdgeModel(ij, jk);
      e.getProp().obj = t;
      dag.insertEdge(e);
    }

    // convert it to a dag (from each cycle remove an edge)
    // convertToDag(dag);
    boolean first = true;
    while ((cycle = DAG.findACycle(dag)) != null) {
      if (first) {
        TripNet.println("Warning 0: Input triplets are inconsistent!");
        first = false;
      }
      ArrayList<Triplet> cycleTriplets = TripNet.getCycleTriplets(cycle);
      Triplet minT = cycleTriplets.get(0);
      for (Triplet t : cycleTriplets) {
        if (t.w < minT.w) minT = t;
      }
      TripNet.println(
          "removing: "
              + minT
              + " with weight: "
              + minT.w
              + " ::: Resolving the cycle: "
              + cycleTriplets);
      if (TripNet.REAMOVE_CYCLIC_TRIPLETS) {
        TripNet.println("removing Triplet of tow: " + cycleTriplets);
        tow.remove(minT);
      }
      VertexModel ik = getVertex(minT.i, minT.k, dag, V);
      VertexModel jk = getVertex(minT.j, minT.k, dag, V);
      VertexModel ij = getVertex(minT.i, minT.j, dag, V);
      dag.removeEdge(dag.getEdge(ij, jk));
      dag.removeEdge(dag.getEdge(ij, ik));
    }
    // topological sort
    AbstractList<VertexModel> topsort = DAG.doSort(dag);

    if (topsort == null) {
      // the inputs are inconsistent, they are not a dag
      // GraphData gd = new GraphData(Application.blackboard);
      // gd.core.showGraph(dag);
      cycle = DAG.findACycle(dag);
      VertexModel p = cycle.get(0);
      VertexModel c = cycle.get(1);
      EdgeModel e = dag.getEdge(p, c);
      dag.removeEdge(e);
      TripNet.println("Err 0: please contact us for this error! :(");
      return null;
    }
    // set Xij

    // find the length of the longest chain
    int max = -1;
    for (Pair<VertexModel, Integer> p : DAG.findLongestPath(dag)) {
      max = Math.max(max, p.second);
    }

    // set the Xij as large as u can
    for (int i = topsort.size() - 1; i >= 0; i--) {
      VertexModel v = topsort.get(i);
      int min = max + 1;
      for (VertexModel trg : dag.getNeighbors(v)) {
        min = Math.min(min, trg.getColor());
      }
      v.setColor(min - 1); // the color will be related edge's weight (or
      // formally Xij)
    }

    // create the network graph
    HashMap<Object, VertexModel> VR = new HashMap<Object, VertexModel>();
    GraphModel ret = new GraphModel(false);
    if (TripNet.verbose) TripNet.println("Integer Programming Results:");
    TripNet.ths.maxW = max;
    for (VertexModel v : topsort) {
      Pair<Integer, Integer> edge = (Pair<Integer, Integer>) getVId(v);
      VertexModel i = getVertex(edge.first, ret, VR);
      VertexModel j = getVertex(edge.second, ret, VR);
      EdgeModel e = new EdgeModel(i, j);
      int w = v.getColor();
      e.setWeight(w);
      ret.insertEdge(e);
      if (TripNet.verbose) System.out.println(edge.first + "," + edge.second + ": " + w);
      TripNet.ths.F0.put(edge.first + "," + edge.second, w);
      TripNet.ths.weights.put(new Pair<Integer, Integer>(edge.first, edge.second), w);
    }
    // check consistency of results
    for (Triplet t : tow) {
      if ((TripNet.getW(t.i, t.j).intValue() <= TripNet.getW(t.i, t.k).intValue())
          && (TripNet.getW(t.i, t.j).intValue() <= TripNet.getW(t.j, t.k).intValue())) continue;
      System.err.println("inconsistant weight");
    }
    return ret;
  }