예제 #1
0
 public void run() {
   // TODO Auto-generated method stub
   run = true;
   System.out.println("Running a map");
   while (run) {
     // Figure out how to read input file
     // Evaluate it
     Map<String, List<String>> input = reader.getKeyValuePairs();
     if (input == null) {
       map.setStatus(-1);
       Message msg = new Message();
       msg.setTask(map);
       msg.setType('f');
       coord.conn.sendMessage(msg);
       return;
     }
     OutputCollector<String, String> collect = new OutputCollector<String, String>();
     Set<String> keySet = input.keySet();
     for (String key : keySet) {
       List<String> values = input.get(key);
       for (String value : values) {
         map.getJob().map(key, value, collect);
       }
     }
     List<Pair> results = collect.getResults();
     int length = results.size();
     FileOutputStream out;
     try {
       System.out.println("Trying to Write to File");
       out = new FileOutputStream(new File(map.getOutput().get(0)));
       BufferedWriter dw = new BufferedWriter(new OutputStreamWriter(out));
       for (Pair p : results) {
         dw.append(p.toString());
         dw.newLine();
       }
       dw.newLine();
       dw.flush();
       dw.close();
       out.close();
       coord.dataNode.addFileToDFS(map.getOutput().get(0), coord.conn.port, false);
     } catch (Exception e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       map.setStatus(-1);
       Message msg = new Message();
       msg.setTask(map);
       msg.setType('f');
       coord.conn.sendMessage(msg);
       return;
     }
     map.setStatus(1);
     Message msg = new Message();
     msg.setTask(map);
     msg.setType('f');
     coord.conn.sendMessage(msg);
     break;
   }
 }
예제 #2
0
  public Pair<Integer, Integer> bottomGuy(Pair<Integer, Integer> CurrentLoc) {
    Pair<Integer, Integer> ret = new Pair<Integer, Integer>(-1, -1);

    if (CurrentLoc._o2 == 0) {
      ret._o2 = 0;
    } else {
      ret._o2 = CurrentLoc._o2 - 1;
    }

    ret._o1 = CurrentLoc._o1;

    return ret;
  }
예제 #3
0
  public Pair<Integer, Integer> topGuy(Pair<Integer, Integer> CurrentLoc) {
    Pair<Integer, Integer> ret = new Pair<Integer, Integer>(-1, -1);

    if (CurrentLoc._o2 == _nSize - 1) {
      ret._o2 = _nSize - 1;
    } else {
      ret._o2 = CurrentLoc._o2 + 1;
    }

    ret._o1 = CurrentLoc._o1;

    return ret;
  }
예제 #4
0
  /**
   * @param pCurrentLoc
   * @return
   */
  public Pair<Integer, Integer> leftGuy(Pair<Integer, Integer> CurrentLoc) {
    Pair<Integer, Integer> ret = new Pair<Integer, Integer>(-1, -1);

    if (CurrentLoc._o1 == 0) {
      ret._o1 = 0;
    } else {
      ret._o1 = CurrentLoc._o1 - 1;
    }

    ret._o2 = CurrentLoc._o2;

    return ret;
  }
예제 #5
0
 /**
  * Test whether the pair returns the correct string relating to the values of its first and second
  * values.
  */
 @Test
 public void testString() {
   Pair<Double> testPair = new Pair<Double>(testFirst, testSecond);
   assertEquals(testString, testPair.toString());
 }
예제 #6
0
 /** Test whether the second item in the pair returns the correct value */
 @Test
 public void testSecond() {
   Pair<Double> testPair = new Pair<Double>(testFirst, testSecond);
   assertEquals(testSecond, testPair.second());
 }
예제 #7
0
  @Override
  public Solution preprocess(Solution sol, boolean verbose) {
    List<Integer> good = new ArrayList<Integer>(sol.size());
    List<Integer> wrong = new ArrayList<Integer>();

    if (verbose == true) {
      System.out.print("Preprocessing ... ");
    }

    // Etape 1 : les reines dispatchee dans good et wrong
    good.add(sol.get(0));

    // On tente ensuite d'ajouter la reine suivante de la solution initiale
    // dans la liste Good. Si elle genere un conflit, elle est retiree
    // et placee dans la liste Wrong
    for (int i = 1; i < sol.size(); ++i) {
      good.add(sol.get(i));

      Solution s = new Solution(good);

      if (s.cost() != 0) {
        good.remove(good.size() - 1);
        wrong.add(sol.get(i));
      }
    }

    // Liste contenant pour chaque reine de wrong la liste
    // positions possibles avec leur cout
    List<List<Pair<Integer, Integer>>> pouet = new ArrayList<List<Pair<Integer, Integer>>>();

    // Liste des positions d'une reine de wrong avec leur cout
    List<Pair<Integer, Integer>> meuh;

    boolean addition;
    int idxWrong;

    while (wrong.isEmpty() == false) {
      idxWrong = 0;
      addition = false;
      pouet.clear();

      while (idxWrong < wrong.size() && (addition == false)) {
        meuh = new ArrayList<Pair<Integer, Integer>>();

        for (int idxGood = 0; (idxGood <= good.size()) && (addition == false); ++idxGood) {

          good.add(idxGood, wrong.get(idxWrong));
          Solution s = new Solution(good);

          if (s.cost() == 0) {
            addition = true;
          } else {
            meuh.add(new Pair<Integer, Integer>(idxGood, s.cost()));
            good.remove(idxGood);
          }
        }

        if (addition == false) {
          pouet.add(meuh);
          ++idxWrong;
        } else {
          wrong.remove(idxWrong);
        }
      }

      if (addition == false) {
        int minCost = Integer.MAX_VALUE;
        Pair<Integer, Integer> posMin = null;
        int cost = 0;

        for (int idxPouet = 0; idxPouet < pouet.size(); ++idxPouet) {
          List<Pair<Integer, Integer>> l = pouet.get(idxPouet);

          for (int idxMeuh = 0; idxMeuh < l.size(); ++idxMeuh) {
            cost = l.get(idxMeuh).getSecond();

            if (cost < minCost) {
              minCost = cost;
              posMin = new Pair<Integer, Integer>(idxPouet, idxMeuh);
            }
          }
        }

        if (posMin != null) {
          good.add(
              pouet.get(posMin.getFirst()).get(posMin.getSecond()).getFirst(),
              wrong.get(posMin.getFirst()));

          int index = posMin.getFirst();

          wrong.remove(index);
        }
      }
    }

    if (verbose == true) {
      System.out.println("done");
    }

    return new Solution(good);
  }