Exemple #1
0
  /**
   * Back-end to pick using BFS.
   *
   * @param limit maximum value of combinations
   * @param items object value table
   * @return solution
   */
  public static <T> Pack<T> breadthFirstSearch(Long limit, AbstractMap<T, Long> items) {
    if (Pack.needSearch(limit, items)) {
      return new Pack<T>(limit, new ArrayList<T>(items.keySet()));
    }

    ArrayList<Pack<T>> table = new ArrayList<Pack<T>>();
    table.add(new Pack<T>());

    for (Entry<T, Long> e : items.entrySet()) {
      ArrayList<Pack<T>> tmp = new ArrayList<Pack<T>>();
      for (Pack<T> p : table) {
        Long newSize = p.getScore() + e.getValue();
        if (newSize <= limit) {
          ArrayList<T> newDirs = new ArrayList<T>(p.getItems());
          newDirs.add(e.getKey());
          tmp.add(new Pack<T>(newSize, newDirs));
        }
      }
      table.addAll(table.size(), tmp);
    }

    Pack<T> max = new Pack<T>();
    for (Pack<T> p : table) {
      if (p.getScore() >= max.getScore()) {
        max = p;
      }
    }
    return max;
  }
Exemple #2
0
    public Pack<T> call() {
      while (!this.canStop()) {
        this.crossOver();
        this.mutation();
        Collections.sort(this.population_);
        this.population_.subList(this.table_.size(), this.population_.size()).clear();
      }

      Cell<T> survivor = this.population_.get(0);
      Pack<T> result = new Pack<T>(survivor.getValue(), new ArrayList<T>());
      for (Entry<T, Boolean> e : survivor.getTable().entrySet()) {
        if (e.getValue()) {
          result.getItems().add(e.getKey());
        }
      }
      return result;
    }