Exemplo n.º 1
0
 static void completeEdges(SubGraph reminder) {
   for (VertexModel v : reminder.vertices) {
     for (VertexModel u : reminder.vertices) {
       if (u.getId() > v.getId())
         if (SubGraph.getEdge(reminder, u, v) == null) {
           Integer w = TripNet.getW(TripNet.Xi(u), TripNet.Xi(v));
           if (w != null) {
             EdgeModel e = new EdgeModel(u, v);
             e.setWeight(w);
             reminder.graph.insertEdge(e);
             reminder.edges.add(e);
           }
         }
     }
   }
 }
Exemplo n.º 2
0
  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;
  }