private void applyVariables(
      VehicleRoutingSolution inputSolution, VehicleRoutingSolution varSolution) {
    List<Vehicle> inputVehicleList = inputSolution.getVehicleList();
    Map<Long, Vehicle> inputVehicleMap = new LinkedHashMap<Long, Vehicle>(inputVehicleList.size());
    for (Vehicle vehicle : inputVehicleList) {
      inputVehicleMap.put(vehicle.getId(), vehicle);
    }
    List<Customer> inputCustomerList = inputSolution.getCustomerList();
    Map<Long, Customer> inputCustomerMap =
        new LinkedHashMap<Long, Customer>(inputCustomerList.size());
    for (Customer customer : inputCustomerList) {
      inputCustomerMap.put(customer.getId(), customer);
    }

    for (Vehicle varVehicle : varSolution.getVehicleList()) {
      Vehicle inputVehicle = inputVehicleMap.get(varVehicle.getId());
      Customer varNext = varVehicle.getNextCustomer();
      inputVehicle.setNextCustomer(varNext == null ? null : inputCustomerMap.get(varNext.getId()));
    }
    for (Customer varCustomer : varSolution.getCustomerList()) {
      Customer inputCustomer = inputCustomerMap.get(varCustomer.getId());
      Standstill varPrevious = varCustomer.getPreviousStandstill();
      inputCustomer.setPreviousStandstill(
          varPrevious == null
              ? null
              : varPrevious instanceof Vehicle
                  ? inputVehicleMap.get(((Vehicle) varPrevious).getId())
                  : inputCustomerMap.get(((Customer) varPrevious).getId()));
      Customer varNext = varCustomer.getNextCustomer();
      inputCustomer.setNextCustomer(varNext == null ? null : inputCustomerMap.get(varNext.getId()));
    }
    ScoreDirector scoreDirector = scoreDirectorFactory.buildScoreDirector();
    scoreDirector.setWorkingSolution(inputSolution);
    scoreDirector.calculateScore();
  }
예제 #2
0
 public Solver buildSolver() {
   DefaultSolver solver = new DefaultSolver();
   BasicPlumbingTermination basicPlumbingTermination = new BasicPlumbingTermination();
   solver.setBasicPlumbingTermination(basicPlumbingTermination);
   EnvironmentMode environmentMode =
       this.environmentMode == null ? EnvironmentMode.REPRODUCIBLE : this.environmentMode;
   if (randomSeed != null) {
     solver.setRandomSeed(randomSeed);
   } else {
     if (environmentMode != EnvironmentMode.PRODUCTION) {
       solver.setRandomSeed(DEFAULT_RANDOM_SEED);
     }
   }
   SolutionDescriptor solutionDescriptor = buildSolutionDescriptor();
   ScoreDirectorFactoryConfig scoreDirectorFactoryConfig_ =
       scoreDirectorFactoryConfig == null
           ? new ScoreDirectorFactoryConfig()
           : scoreDirectorFactoryConfig;
   ScoreDirectorFactory scoreDirectorFactory =
       scoreDirectorFactoryConfig_.buildScoreDirectorFactory(environmentMode, solutionDescriptor);
   solver.setScoreDirectorFactory(scoreDirectorFactory);
   ScoreDefinition scoreDefinition = scoreDirectorFactory.getScoreDefinition();
   TerminationConfig terminationConfig_ =
       terminationConfig == null ? new TerminationConfig() : terminationConfig;
   Termination termination =
       terminationConfig_.buildTermination(scoreDefinition, basicPlumbingTermination);
   solver.setTermination(termination);
   BestSolutionRecaller bestSolutionRecaller = buildBestSolutionRecaller(environmentMode);
   solver.setBestSolutionRecaller(bestSolutionRecaller);
   if (CollectionUtils.isEmpty(solverPhaseConfigList)) {
     throw new IllegalArgumentException(
         "Configure at least 1 phase (for example <localSearch>) in the solver configuration.");
   }
   List<SolverPhase> solverPhaseList = new ArrayList<SolverPhase>(solverPhaseConfigList.size());
   int phaseIndex = 0;
   for (SolverPhaseConfig solverPhaseConfig : solverPhaseConfigList) {
     SolverPhase solverPhase =
         solverPhaseConfig.buildSolverPhase(
             phaseIndex, environmentMode, solutionDescriptor, scoreDefinition, termination);
     ((AbstractSolverPhase) solverPhase).setBestSolutionRecaller(bestSolutionRecaller);
     solverPhaseList.add(solverPhase);
     phaseIndex++;
   }
   solver.setSolverPhaseList(solverPhaseList);
   return solver;
 }
예제 #3
0
 public void solvingStarted(DefaultSolverScope solverScope) {
   if (solverScope.getBestSolution() == null) {
     throw new IllegalStateException(
         "The planningProblem must not be null." + " Use Solver.setPlanningProblem(Solution).");
   }
   solverScope.setStartingSystemTimeMillis(System.currentTimeMillis());
   solverScope.setScoreDirector(scoreDirectorFactory.buildScoreDirector());
   solverScope.setWorkingRandom(randomFactory.createRandom());
   solverScope.setWorkingSolutionFromBestSolution();
   bestSolutionRecaller.solvingStarted(solverScope);
   for (SolverPhase solverPhase : solverPhaseList) {
     solverPhase.solvingStarted(solverScope);
   }
   logger.info(
       "Solving started: time spend ({}), score ({}), new best score ({}), random ({}).",
       solverScope.calculateTimeMillisSpend(),
       solverScope.getStartingInitializedScore(),
       solverScope.getBestScore(),
       (randomFactory != null ? randomFactory : "not fixed"));
 }