Exemple #1
0
    @Override
    public LocalSearchResult call() throws Exception {
      // get initial solution
      TTPSolution initialSolution = constructionHeuristic.getInitialSolution();
      LocalSearchStatistics localSearchStatistics = null;

      if (collectStatistics) {
        localSearchStatistics = new LocalSearchStatistics();
        localSearch.setLocalSearchStatistics(localSearchStatistics);
      }

      // apply local search
      TTPSolution lsSolution = localSearch.doLocalSearch(initialSolution);

      // logger.info("Iter: " + iteration + " Current Solution: " +
      // lsSolution.getCost());
      return new LocalSearchResult(iteration, lsSolution, localSearchStatistics);
    }
Exemple #2
0
  @Override
  public TTPSolution doSearch(TTPInstance instance) {
    if (searchStatistics != null) searchStatistics.start();

    ExecutorService executorService = Executors.newFixedThreadPool(noThreads);
    List<LocalSearchCallable> localSearches = new ArrayList<LocalSearchCallable>(noTries);
    constructionHeuristic.setProblemInstance(instance);

    try {
      for (int i = 0; i < noTries; i++) {
        IConstructionHeuristics<TTPInstance, TTPSolution> clonedConstructionHeuristic =
            constructionHeuristic.clone();
        ILocalSearch<TTPSolution> clonedLocalSearch = localSearch.clone();

        localSearches.add(
            new LocalSearchCallable(
                clonedConstructionHeuristic, clonedLocalSearch, i, searchStatistics != null));
      }
    } catch (CloneNotSupportedException e) {
      logger.error("Failed to create clone for multi-threaded local search", e);
    }

    try {
      List<Future<LocalSearchResult>> solutions = executorService.invokeAll(localSearches);
      TTPSolution bestSolution = null;

      for (Future<LocalSearchResult> futureSolution : solutions) {
        try {
          LocalSearchResult result = futureSolution.get();

          TTPSolution solution = result.getSolution();

          if (solution == null) {
            logger.warn("Solution was null");
            continue;
          }

          if (searchStatistics != null)
            searchStatistics.addLocalSearchStatistic(result.getIteration(), result.getStatistics());

          if (solution != null
              && (bestSolution == null || solution.getCost() < bestSolution.getCost()))
            bestSolution = solution;
        } catch (ExecutionException e) {
          logger.warn("One of the local search executors failed", e);
          e.printStackTrace();
        }
      }

      executorService.shutdown();

      if (searchStatistics != null) {
        searchStatistics.end();
        searchStatistics.setSolution(bestSolution);
      }

      return bestSolution;
    } catch (InterruptedException e) {
      logger.error("Failed to do multi-threaded local search", e);
      if (searchStatistics != null) {
        searchStatistics.setException(e);
        searchStatistics.end();
      }
      return null;
    }
  }