Пример #1
0
  @Override
  public Solution execute() {
    Solution s = null;
    Solution s_;
    do {
      s_ = factory.newSolution();
      localSearch.getProblem().setInitialSolution(s_);
      s_ = localSearch.execute();
      if (s == null || s_.getCost() < s.getCost()) {
        s = s_;
      }

      // System.out.println(String.format("it:%d, current: %f, best: %s.", current_it,
      //		s.getCost(), optimizationResult.getBestSolution().getCost()));

    } while (endIteration(s_));

    return s;
  }
  @Override
  public Solution execute() {
    Solution s = initialSolution == null ? problem.getInitialSolution() : initialSolution;
    System.out.println("Initial s: " + problem.getCostEvaluator().eval(s));

    int i = 0;
    int totalIt = 0;
    Solution s_;

    while (i < maxItWithoutImprovement && totalIt < max_it) {
      System.out.println("#### i = " + i + "####");
      double lastSCost = s.getCost();
      int k = 0;
      do {
        if (totalIt > max_it) {
          break;
        }
        s_ = neighborhoods[k].getRandomNeighbor(s); // pick at random
        localSearchMethod.setProblem(problem);
        localSearchMethod.getProblem().setInitialSolution(s_);
        s_ = localSearchMethod.execute();

        System.out.println("s_, n" + k + ": " + s_.getCost());

        if (s_.getCost() < s.getCost()) {
          s = s_;
          k = 0;
        } else {
          k++;
        }
        totalIt++;

      } while (endIteration(s_) && k < neighborhoods.length);

      if (lastSCost > s.getCost()) {
        i = 0;
      } else {
        i++;
      }
    }

    System.out.println("Final s: " + s.getCost());

    return s;
  }