Package bgu.bio.algorithms.graphs.hsa.matchers

Examples of bgu.bio.algorithms.graphs.hsa.matchers.BipartiteCavityMatcher


    // A,u,v,B,_,C,w,D,p
    // A,_,_,B,x,C,y,D,_

    int sizeX = 8;
    int sizeY = 6;
    BipartiteCavityMatcher matcher = new OrderedBipartiteCavityMatcher(
        sizeX, sizeY);

    for (int y = 0; y < sizeY; ++y) {
      matcher.setDelCostY(y, 2);
    }

    for (int x = 0; x < sizeX; ++x) {
      matcher.setDelCostX(x, 2);
      for (int y = 0; y < sizeY; ++y) {
        matcher.setMatchCost(x, y, 5);
      }
    }

    matcher.setMatchCost(0, 4, -1);
    matcher.setMatchCost(3, 5, -1);
    matcher.setMatchCost(4, 1, -1);
    matcher.setMatchCost(6, 3, -1);

    matcher.setMatchCost(5, 2, 3);

    boolean res = matcher.minCostCavityMatching(7, 5) == 13;
    System.out.println("Cavity matching test: " + res);

    res = matcher.minCostMatching() == 7;
    System.out.println("Full matching test: " + res);
    TIntArrayList[] matching = matcher.getCurrMatching();

    System.out.println("Current matching:");
    for (int i = 0; i < matching[0].size(); ++i) {
      System.out.print("(" + matching[0].get(i) + ","
          + matching[1].get(i) + "), ");
View Full Code Here


    // 2, 0: -23.0 *** Cost: -16.0, Matching: (0, 2), (1, 3),
    // 2, 1: -49.0 *** Cost: -46.0, Matching: (0, 2), (1, 0),
    // 2, 2: -32.0 *** Cost: -25.0, Matching: (0, 1), (1, 0),
    // 2, 3: -49.0 *** Cost: -46.0, Matching: (0, 2), (1, 0),

    BipartiteCavityMatcher matcher = new UnorderedBipartiteCavityMatcherMinCostFlow(
        3, 4);

    matcher.setAllMatchCost(0, -4, -3, -20, -2);
    matcher.setAllMatchCost(1, -30, -4, -4, -4);
    matcher.setAllMatchCost(2, -4, 4, 4, 4);

    matcher.setAllDelCostX(5, 5, 2);
    matcher.setAllDelCostY(5, 1, 5, 1);

    checkCavityMatchings(matcher);

    // check forced matchings:
    double inf = Double.POSITIVE_INFINITY;
    matcher.setDelCostX(0, inf);
    matcher.setDelCostY(2, inf);

    System.out.println("\n");
    System.out.println("Checking forced match of x0 and y2: ");

    checkCavityMatchings(matcher);

    matcher.setDelCostY(2, 5);
    matcher.setDelCostY(0, inf);

    System.out.println("\n");
    System.out.println("Checking forced match of x0 and y0: ");
    checkCavityMatchings(matcher);

    matcher.setDelCostX(0, 5);
    matcher.setDelCostY(0, 5);

    System.out.println("\n");
    System.out.println("Checking forced match of x2: ");
    matcher.setDelCostX(2, inf);
    checkCavityMatchings(matcher);

    System.out.println("\n");
    System.out.println("Checking group replacement: ");

    matcher = new UnorderedBipartiteCavityMatcherMinCostFlow(4, 3);
    matcher.setAllMatchCost(0, -4, -30, -4);
    matcher.setAllMatchCost(1, -3, -4, 4);
    matcher.setAllMatchCost(2, -20, -4, 4);
    matcher.setAllMatchCost(3, -2, -4, 4);

    matcher.setAllDelCostX(5, 1, 5, 1);
    matcher.setAllDelCostY(5, 5, 2);

    checkCavityMatchings(matcher);
  }
View Full Code Here

    // A,u,v,B,_,C,w,D,p
    // A,_,_,B,x,C,y,D,_

    int sizeX = 8;
    int sizeY = 6;
    BipartiteCavityMatcher matcher = new OrderedBipartiteCavityMatcher(
        sizeX, sizeY);

    for (int y = 0; y < sizeY; ++y) {
      matcher.setDelCostY(y, 2);
    }

    for (int x = 0; x < sizeX; ++x) {
      matcher.setDelCostX(x, 2);
      for (int y = 0; y < sizeY; ++y) {
        matcher.setMatchCost(x, y, 5);
      }
    }

    matcher.setMatchCost(0, 4, -1);
    matcher.setMatchCost(3, 5, -1);
    matcher.setMatchCost(4, 1, -1);
    matcher.setMatchCost(6, 3, -1);

    matcher.setMatchCost(5, 2, 3);

    Assert.assertEquals("Cavity matching answer is wrong", 13,
        matcher.minCostCavityMatching(7, 5), 0.001);

    TIntArrayList[] matching = matcher.getCavityMatching(7, 5);
    IntPairComparator comp = new IntPairComparator();

    ArrayList<IntPair> ans = new ArrayList<IntPair>();
    ArrayList<IntPair> exp = new ArrayList<IntPair>();

    ans.add(new IntPair(6, 3));
    ans.add(new IntPair(5, 2));
    ans.add(new IntPair(4, 1));
    for (int i = 0; i < matching[0].size(); ++i) {
      exp.add(new IntPair(matching[0].get(i), matching[1].get(i)));
    }

    Collections.sort(ans, comp);
    Collections.sort(exp, comp);

    isSame(ans, exp);

    Assert.assertEquals("Minimum matching answer is wrong", 7,
        matcher.minCostMatching(), 0.001);

    ans = new ArrayList<IntPair>();
    exp = new ArrayList<IntPair>();

    matching = matcher.getCurrMatching();

    ans.add(new IntPair(3, 5));
    ans.add(new IntPair(0, 4));
    ans.add(new IntPair(6, 3));
    ans.add(new IntPair(5, 2));
View Full Code Here

    // 2, 0: -23.0 *** Cost: -16.0, Matching: (0, 2), (1, 3),
    // 2, 1: -49.0 *** Cost: -46.0, Matching: (0, 2), (1, 0),
    // 2, 2: -32.0 *** Cost: -25.0, Matching: (0, 1), (1, 0),
    // 2, 3: -49.0 *** Cost: -46.0, Matching: (0, 2), (1, 0),

    BipartiteCavityMatcher matcher = new UnorderedBipartiteCavityMatcherMinCostFlow(
        3, 4);

    matcher.setAllMatchCost(0, -4, -3, -20, -2);
    matcher.setAllMatchCost(1, -30, -4, -4, -4);
    matcher.setAllMatchCost(2, -4, 4, 4, 4);

    matcher.setAllDelCostX(5, 5, 2);
    matcher.setAllDelCostY(5, 1, 5, 1);

    checkCavityMatchings(matcher);

    // check forced matchings:
    double inf = Double.POSITIVE_INFINITY;
    matcher.setDelCostX(0, inf);
    matcher.setDelCostY(2, inf);

    System.out.println("\n");
    System.out.println("Checking forced match of x0 and y2: ");

    checkCavityMatchings(matcher);

    matcher.setDelCostY(2, 5);
    matcher.setDelCostY(0, inf);

    System.out.println("\n");
    System.out.println("Checking forced match of x0 and y0: ");
    checkCavityMatchings(matcher);

    matcher.setDelCostX(0, 5);
    matcher.setDelCostY(0, 5);

    System.out.println("\n");
    System.out.println("Checking forced match of x2: ");
    matcher.setDelCostX(2, inf);
    checkCavityMatchings(matcher);

    System.out.println("\n");
    System.out.println("Checking group replacement: ");

    matcher = new UnorderedBipartiteCavityMatcherMinCostFlow(4, 3);

    matcher.setAllMatchCost(0, -4, -30, -4);
    matcher.setAllMatchCost(1, -3, -4, 4);
    matcher.setAllMatchCost(2, -20, -4, 4);
    matcher.setAllMatchCost(3, -2, -4, 4);

    matcher.setAllDelCostX(5, 1, 5, 1);
    matcher.setAllDelCostY(5, 5, 2);

    checkCavityMatchings(matcher);
  }
View Full Code Here

      if (tRoot != -1) {
        alignment[0].add(tRoot);
        alignment[1].add(sRoot);
        alignment[2].add(0);

        BipartiteCavityMatcher matcher = matchers[tRoot][sRoot];
        matcher.setMinCostFullMatching(Double.NaN);
        matcher.minCostMatching();
        TIntArrayList[] matching = matcher.getCurrMatching();

        int nextGroupId = 1;
        for (int pair = 0; pair < matching[0].size(); ++pair) {
          nextGroupId = traceback(t, s, alignment, tRoot,
              matching[0].get(pair), sRoot,
View Full Code Here

    int sTo = s.outAdjLists[sFrom][sToNeighborIx];

    int tFromNeighborIx = t.getNeighborIx(tTo, tFrom);
    int sFromNeighborIx = s.getNeighborIx(sTo, sFrom);

    BipartiteCavityMatcher matcher = matchers[tTo][sTo];

    if (MathOperations.equals(cost,
        matcher.minCostCavityMatching(tFromNeighborIx, sFromNeighborIx)
            + costFunction.cost(t.labels[tTo], s.labels[sTo]))) {
      alignment[0].add(tTo);
      alignment[1].add(sTo);
      alignment[2].add(nextGroupIx);

      TIntArrayList[] matching = matcher.getCavityMatching(
          tFromNeighborIx, sFromNeighborIx);
      if (matching[0].size() != 1 || t.outDeg(tTo) != 2
          || s.outDeg(sTo) != 2) {
        ++nextGroupIx;
      }
      for (int pair = 0; pair < matching[0].size(); ++pair) {
        nextGroupIx = traceback(t, s, alignment, tTo,
            matching[0].get(pair), sTo, matching[1].get(pair),
            nextGroupIx);
      }
      return nextGroupIx;
    }

    double pruneAll = -matcher.safeDelCostX(tFromNeighborIx);
    // -t.weights[tTo][tFromNeighborIx];
    for (int u = 0; u < t.outAdjLists[tTo].length; ++u) {
      pruneAll += matcher.safeDelCostX(u);
      // t.weights[tTo][u];
    }

    double initWeight = pruneAll + t.smoothCost[tTo];

    for (int u = 0; u < t.outAdjLists[tTo].length; ++u) {
      if (u != tFromNeighborIx) {
        if (MathOperations.equals(cost, initWeight
            + matchers[tTo][sFrom].getMatchCostXY(u, sToNeighborIx)
            - matcher.safeDelCostX(u))) { // t.weights[tTo][u])) {
          if (t.outDeg(tTo) != 2) {
            ++nextGroupIx;
          }
          nextGroupIx = traceback(t, s, alignment, tTo, u, sFrom,
              sToNeighborIx, nextGroupIx);
          return nextGroupIx;
        }
      }
    }

    pruneAll = -matcher.safeDelCostY(sFromNeighborIx);// -s.weights[sTo][sFromNeighborIx];
    for (int u = 0; u < s.outAdjLists[sTo].length; ++u) {
      pruneAll += matcher.safeDelCostY(u);// s.weights[sTo][u];
    }

    initWeight = pruneAll + s.smoothCost[sTo];

    for (int u = 0; u < s.outAdjLists[sTo].length; ++u) {
      if (u != sFromNeighborIx) {
        if (MathOperations.equals(cost, initWeight
            + matchers[tFrom][sTo].getMatchCostXY(tToNeighborIx, u)
            - matcher.safeDelCostY(u))) { // s.weights[sTo][u])) {
          if (s.outDeg(sTo) != 2) {
            ++nextGroupIx;
          }
          nextGroupIx = traceback(t, s, alignment, tFrom,
              tToNeighborIx, sTo, u, nextGroupIx);
View Full Code Here

        sSize = Math.max(matchers[0].length, s.getNodeNum());
      }
      this.matchers = new BipartiteCavityMatcher[tSize][sSize];
    }

    BipartiteCavityMatcher matcher;

    // initiation of the matchers
    for (int i = 0; i < t.getNodeNum(); i++) {
      for (int j = 0; j < s.getNodeNum(); j++) {
        final int degI = t.outDeg(i);
        final int degJ = s.outDeg(j);
        matcher = factory.make(
            degI,
            degJ,
            !MathOperations.isInfinity(costFunction.cost(
                t.getLabel(i), s.getLabel(j))));
        matchers[i][j] = matcher;
        for (int u = 0; u < degI; u++) {
          matcher.setDelCostX(u, t.weights[i][u]);
        }

        for (int u = 0; u < degJ; u++) {
          matcher.setDelCostY(u, s.weights[j][u]);
        }
      }
    }

    if (tEdgeEncounters == null || tEdgeEncounters.length < t.getNodeNum()) {
      tEdgeEncounters = new int[t.getNodeNum()];
    }
    Arrays.fill(tEdgeEncounters, 0, t.getNodeNum(), FIRST_TIME);

    if (sEdgeEncounters == null || sEdgeEncounters.length < s.getNodeNum()) {
      sEdgeEncounters = new int[s.getNodeNum()];
    }

    computationTime = System.currentTimeMillis();

    double currScore, tmpScore; // , pruneAll
    for (int et = 0; et < t.getEdgeNum(); et++) {
      int fromT = t.edgeToNeighbors[et][0];
      int toTNeighborIx = t.edgeToNeighbors[et][1];
      int toT = t.outAdjLists[fromT][toTNeighborIx];
      int fromTNeighborIx = t.getNeighborIx(toT, fromT);

      Arrays.fill(sEdgeEncounters, 0, s.getNodeNum(), FIRST_TIME);
      for (int es = 0; es < s.getEdgeNum(); es++) {
        int fromS = s.edgeToNeighbors[es][0];
        int toSNeighborIx = s.edgeToNeighbors[es][1];
        int toS = s.outAdjLists[fromS][toSNeighborIx];
        int fromSNeighborIx = s.getNeighborIx(toS, fromS);

        matcher = matchers[toT][toS];

        if (useCavity) {
          if (sEdgeEncounters[toS] == SECOND_TIME) {
            if (tEdgeEncounters[toT] == FIRST_TIME) {
              matcher.processAllCavityMatchingY(fromTNeighborIx);
            } else if (tEdgeEncounters[toT] == SECOND_TIME) {
              matcher.processAllPairsCavityMatching();
            }
          }
        }
       
        final BipartiteCavityMatcher matcherToFrom = matchers[toT][fromS];
        double cavityPrune = 0;
        boolean foundForced = false;

        // IMPROTANT: we assume that at most one forced match exists!!!
        tmpScore = MathOperations.INFINITY;
        final int tNeighborsSize = t.outAdjLists[toT].length;
        for (int u = 0; u < tNeighborsSize; ++u) {
          if (u != fromTNeighborIx) {
            if (!MathOperations.isInfinity(t.weights[toT][u])) {
              cavityPrune += t.weights[toT][u];
              final double value = matcherToFrom.getMatchCostXY(u,
                  toSNeighborIx) - t.weights[toT][u];
              if (MathOperations.greater(
                  tmpScore,
                  value)
                  && !foundForced) {
                tmpScore = value;
              }
            }       
            else {
              if (foundForced) {
                throw new RuntimeException(
                    "more than two forced matches!");
              }
              tmpScore = matcherToFrom.getMatchCostXY(u,
                  toSNeighborIx);
              foundForced = true;
            }

          }
        }

        currScore = tmpScore + cavityPrune + t.smoothCost[toT];


        final BipartiteCavityMatcher matcherFromTo = matchers[fromT][toS];
        tmpScore = MathOperations.INFINITY;
        foundForced = false;
        cavityPrune = 0;

        // pruneAll = 0;
        final int sNeighborsSize = s.outAdjLists[toS].length;
        for (int u = 0; u < sNeighborsSize; ++u) {
          if (u != fromSNeighborIx) {
            if (!MathOperations.isInfinity(s.weights[toS][u])) {
              cavityPrune += s.weights[toS][u];
              final double value = matcherFromTo.getMatchCostXY(toTNeighborIx,
                  u) - s.weights[toS][u];
              if (MathOperations.greater(
                  tmpScore,
                  value)
                  && !foundForced) {
                tmpScore = value;
              }
              // pruneAll += s.weights[toS][u];
            } else {
              if (foundForced) {
                throw new RuntimeException(
                    "more than two forced matches!");
              }
              tmpScore = matcherFromTo.getMatchCostXY(
                  toTNeighborIx, u);
              foundForced = true;
            }
          }
        }
View Full Code Here

TOP

Related Classes of bgu.bio.algorithms.graphs.hsa.matchers.BipartiteCavityMatcher

Copyright © 2018 www.massapicom. 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.