예제 #1
0
public class GreedyDSM1 implements ITask, IGreedyDS<Integer> {

  @SuppressWarnings("unused")
  private static Logger log = LogUtil.getLogger(GreedyDSM1.class);

  private Map<String, Long> runningTimeMap;

  public Map<String, Long> getRunningTimeMap() {
    return runningTimeMap;
  }

  private TaskLock lock;

  public TaskLock getLock() {
    return lock;
  }

  public void setLock(TaskLock lock) {
    this.lock = lock;
  }

  public Result run() throws InterruptedException {

    try {
      computing();
      Thread.sleep(1000);
      Result r = getResult();

      return r;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  public Result getResult() {
    return GreedyDSUtil.getResult(
        this.k, this.rUpperBoundary, this.dominatingSet, this.runningTimeMap);
  }

  private String indicator;

  /** the desired dominating set */
  List<Integer> dominatingSet;

  public List<Integer> getDominatingSet() {
    return dominatingSet;
  }

  /** the adjacency matrix of the graph */
  private List<String[]> adjacencyMatrix;

  /** parameters (k,r) for the fpt algroithm */
  private int k;

  private int rUpperBoundary;

  public GreedyDSM1(List<String[]> adjacencyMatrix, int k, int rUpperBoundary) {
    this.adjacencyMatrix = adjacencyMatrix;

    this.k = k;
    this.rUpperBoundary = rUpperBoundary;
  }

  /**
   * the major function do the computing to get the desired solution. In this case, the desired
   * result is a dominating set
   *
   * @throws InterruptedException
   */
  public void computing()
      throws MOutofNException, ExceedLongMaxException, ArraysNotSameLengthException,
          InterruptedException {
    long start = System.nanoTime();
    initialization();
    greedy();
    long end = System.nanoTime();
    long runningTime = end - start;
    this.runningTimeMap.put(AlgorithmUtil.RUNNING_TIME_TOTAL, runningTime);
  }

  private void initialization() {
    dominatingSet = new ArrayList<Integer>();

    this.runningTimeMap = new HashMap<String, Long>();
    GreedyDSUtil.initRunningTimeMap(this.runningTimeMap);
  }

  @SuppressWarnings({"rawtypes", "unchecked"})
  private void greedy()
      throws MOutofNException, ExceedLongMaxException, ArraysNotSameLengthException,
          InterruptedException {
    Graph<Integer, String> gOriginal = AlgorithmUtil.prepareGenericGraph(adjacencyMatrix);
    Graph<Integer, String> g = gOriginal;
    /* apply poly-rr */
    // Graph<Integer, String> g =		 GreedyDSUtil.applyPolyReductionRules(gOriginal,
    // this.runningTimeMap,GreedyDSUtil.POLY_RR_2_VALVE);
    /* apply degree-rr */
    DegreeRRReturn drrr = GreedyDSUtil.applyDegreeReductionRules(g, this.runningTimeMap);

    Map<Integer, Boolean> gDominatedMap = drrr.getDominatedMap();
    List<Integer> dI = drrr.getDsAfterDegreeRR();
    /*
     * it is possible that a graph has been dominated at this point, so a if
     * branch is needed here
     */

    // sort a list of vertex from lowest degree to highest
    List<Integer> vList = OrderPackageUtil.getVertexListDegreeAsc(g);

    Graph<Integer, String> gI = new SparseMultigraph<Integer, String>();
    // AlgorithmUtil.prepareGenericGraph(this.adjacencyMatrix, gI, dI);
    GreedyDSUtil.addCloseNeighborToSubgraph(g, gI, dI);
    List<Integer> undominatedVertices = null;
    Integer v;
    Integer u;
    do {

      gDominatedMap = GreedyDSUtil.getDominatedMap(g, dI);
      if (AlgorithmUtil.isAllDominated(gDominatedMap)) {
        break;
      }
      undominatedVertices = GreedyDSUtil.getUndominatedVertices(gDominatedMap);
      v = GreedyDSUtil.getTheFirstItemInOrderedListAndInAnotherList(vList, undominatedVertices);
      /*
       * if vi is not dominated, i) get its highest utility neighbor
       * vi;ii) put ui in DI; and iii) construct a GI with N[ui]; and iv)
       * if it is a moment of regret (in michael's original idea, it
       * always true),
       *
       * divide gI into V and DI, divide V into Vk (the k highest degree
       * vertices in V) and Vl (vertices not in Vk), divide DI into
       * Dk(Vk's neigbors which do not have neighbors in Vl), and Dl(the
       * neighbors of Vl)
       *
       * take Dk, GI, r as parameters to invoke dds fpt to get a solution
       * DDSI; v) set the smaller solution (of DI and DDSI ) to be
       * solution for GI
       */

      // i)
      u = AlgorithmUtil.getHighestUtilityNeighborOfAVertex(v, g, gDominatedMap);
      // ii)
      AlgorithmUtil.addElementToList(dI, u);
      // iii)
      GreedyDSUtil.addCloseNeighborToSubgraph(g, gI, u);

      MomentRegretReturn<Integer, String> mrr = null;
      if (isMomentOfRegret()) {
        mrr =
            GreedyDSUtil.applyAtMomentOfRegret(
                vList, dI, gI, this.indicator, k, this.rUpperBoundary, this.runningTimeMap, true);
      }

      if ((mrr.getDds() != null && mrr.getDds().size() > 0) && mrr.getDds().size() < dI.size()) {
        dI = mrr.getDds();
        Graph<Integer, String> gICopyNextRound = mrr.getGraph();
        GreedyDSUtil.addCloseNeighborToSubgraph(g, gICopyNextRound, dI);
        gI = gICopyNextRound;
      }

      // viii)
      gDominatedMap = GreedyDSUtil.getDominatedMap(g, dI);

    } while (!AlgorithmUtil.isAllDominated(gDominatedMap));

    // do guarantee, minimal, ls at the last step;
    List<Integer> gDI = GreedyDSUtil.useGreedyToCalcDS(g, this.runningTimeMap);
    if (dI.size() < gDI.size()) {
      this.dominatingSet = dI;
    } else {
      this.dominatingSet = gDI;
    }

    GreedyDSUtil.applyMinimal(g, this.dominatingSet, this.runningTimeMap);
    GreedyDSUtil.applyLS(g, this.dominatingSet, this.runningTimeMap);
  }

  private boolean isMomentOfRegret() {
    IMomentOfRegret mor = new MomentOfRegretDelta();
    return mor.isMomentOfRegret(null);
  }
}
예제 #2
0
@Deprecated
public class ExactVA implements ITask, IAlgorithm {

  @SuppressWarnings("unused")
  private static Logger log = LogUtil.getLogger(ExactVA.class);

  private long runningTime;

  private TaskLock lock;

  public TaskLock getLock() {
    return lock;
  }

  public void setLock(TaskLock lock) {
    this.lock = lock;
  }

  public Result run() throws InterruptedException {

    try {
      computing();
      Thread.sleep(1000);
      Result r = getResult();

      return r;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  public Result getResult() {
    Result r = new Result();
    r.setHasSolution(true);
    StringBuffer sb = new StringBuffer();

    sb.append(",").append(this.dominatingSet.size()).append(",").append(this.runningTime);
    r.setString(sb.toString());
    return r;
  }

  /** the graph */
  private Graph<Integer, Integer> g;
  /** a sorted vertices with their degree (from highest degree to the lowest) */
  @SuppressWarnings("unused")
  private String indicator;

  Map<Integer, Boolean> dominatedMap;
  /** the desired dominating set */
  List<Integer> dominatingSet;

  public List<Integer> getDominatingSet() {
    return dominatingSet;
  }

  /** number of vertices */
  private int numOfVertices;

  private int exactSolutionSize;

  /** the adjacency matrix of the graph */
  private List<String[]> adjacencyMatrix;

  public ExactVA(List<String[]> adjacencyMatrix, int exactSolutionSize) {
    this.adjacencyMatrix = adjacencyMatrix;
    this.exactSolutionSize = exactSolutionSize;
    this.numOfVertices = adjacencyMatrix.size();
    this.g = AlgorithmUtil.prepareGraph(this.adjacencyMatrix);
  }

  public ExactVA(String indicator, List<String[]> adjacencyMatrix, int exactSolutionSize) {
    this.indicator = indicator;
    this.adjacencyMatrix = adjacencyMatrix;
    this.exactSolutionSize = exactSolutionSize;
    this.numOfVertices = adjacencyMatrix.size();
    this.g = AlgorithmUtil.prepareGraph(this.adjacencyMatrix);
  }

  public ExactVA(Graph<Integer, Integer> g, int exactSolutionSize) {
    this.g = g;
    this.numOfVertices = g.getVertexCount();
    this.exactSolutionSize = exactSolutionSize;
  }

  /**
   * the major function do the computing to get the desired solution. In this case, the desired
   * result is a dominating set
   */
  public void computing()
      throws MOutofNException, ExceedLongMaxException, ArraysNotSameLengthException {
    long start = System.nanoTime();
    initialization();
    start();
    long end = System.nanoTime();
    runningTime = end - start;
  }

  private void initialization() {
    dominatingSet = new ArrayList<Integer>();

    dominatedMap = new HashMap<Integer, Boolean>();
    Collection<Integer> vertices = g.getVertices();
    for (Integer v : vertices) {
      dominatedMap.put(v, false);
    }
  }

  private void start() throws ArraysNotSameLengthException {
    Collection<Integer> vertices = g.getVertices();
    List<Integer> vList = new ArrayList<Integer>(vertices);

    boolean[] chosen =
        AlgorithmUtil.verifySubDS(vList, this.numOfVertices, this.exactSolutionSize, this.g);
    if (chosen == null) {
      // do nothing
    } else {
      List<Integer> tempDs = new ArrayList<Integer>(this.exactSolutionSize);

      for (int i = 0; i < this.numOfVertices; i++) {
        if (chosen[i]) {
          tempDs.add(vList.get(i));
        }
      }
      this.dominatingSet = tempDs;
    }
  }
}