Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.drools.planner.core.localsearch.decider.forager;
 
 import java.util.List;
 
An AcceptedForager is a Forager which forages accepted moves and ignores unaccepted moves.

 
 public class AcceptedForager extends AbstractForager {
 
     // final to allow better hotspot optimization. TODO prove that it indeed makes a difference
     protected final PickEarlyType pickEarlyType;
     protected final int minimalAcceptedSelection;
 
 
     protected int selectedCount;
     protected List<MoveScopeacceptedList;
     protected boolean listSorted;
     protected Score maxScore;
     protected double acceptChanceMaxScoreTotal;
 
     protected MoveScope earlyPickedMoveScope = null;
 
     public AcceptedForager(PickEarlyType pickEarlyTypeint minimalAcceptedSelection) {
         this. = pickEarlyType;
         this. = minimalAcceptedSelection;
     }
 
     // ************************************************************************
     // Worker methods
     // ************************************************************************
 
     @Override
     public void beforeDeciding(LocalSearchStepScope localSearchStepScope) {
          = new AcceptedMoveScopeComparator(localSearchStepScope.getDeciderScoreComparator());
          = 0;
          = new ArrayList<MoveScope>(1024); // TODO use size of moveList in decider
          = false;
          = 0.0;
          = null;
     }
 
     public void addMove(MoveScope moveScope) {
         ++;
         if (moveScope.getAcceptChance() > 0.0) {
             checkPickEarly(moveScope);
             addMoveScopeToAcceptedList(moveScope);
         }
     }
 
     protected void checkPickEarly(MoveScope moveScope) {
         switch () {
             case :
                 break;
             case :
                 if (moveScope.getLocalSearchStepScope().getDeciderScoreComparator().compare(moveScope.getScore(),
                         moveScope.getLocalSearchStepScope().getLocalSearchSolverPhaseScope().getBestScore()) > 0) {
                      = moveScope;
                 }
                 break;
             case :
                 if (moveScope.getLocalSearchStepScope().getDeciderScoreComparator().compare(moveScope.getScore(),
                         moveScope.getLocalSearchStepScope().getLocalSearchSolverPhaseScope().getLastCompletedLocalSearchStepScope().getScore())
                         > 0) {
                      = moveScope;
                 }
                 break;
             default:
                 throw new IllegalStateException("The pickEarlyType (" +  + ") is not implemented");
         }
     }
    protected void addMoveScopeToAcceptedList(MoveScope moveScope) {
        .add(moveScope);
         = false;
        if (moveScope.getLocalSearchStepScope().getDeciderScoreComparator().compare(moveScope.getScore(), ) > 0) {
             = moveScope.getAcceptChance();
             = moveScope.getScore();
        } else if (moveScope.getScore().equals()) {
             += moveScope.getAcceptChance();
        }
    }
    public boolean isQuitEarly() {
        return  != null || .size() >= ;
    }
    public MoveScope pickMove(LocalSearchStepScope localSearchStepScope) {
        if ( != null) {
            return ;
        } else {
            return pickMaxScoreMoveScopeFromAcceptedList(localSearchStepScope);
        }
    }
    protected MoveScope pickMaxScoreMoveScopeFromAcceptedList(LocalSearchStepScope localSearchStepScope) {
        if (.isEmpty()) {
            return null;
        }
        sortAcceptedList();
        MoveScope pickedMoveScope = null;
        double randomChance = localSearchStepScope.getWorkingRandom().nextDouble();
        double acceptMark =  * randomChance;
        for (ListIterator<MoveScopeit = .listIterator(.size()); it.hasPrevious();) {
            MoveScope moveScope = it.previous();
            acceptMark -= moveScope.getAcceptChance();
            // TODO That underflow warn is nonsense. randomChance can be 0.0 and the last acceptMark can end up 0.0
            // TODO so < is nonsense (do a testcase though)
            if (acceptMark < 0.0) {
                pickedMoveScope = moveScope;
                break;
            }
        }
        if (pickedMoveScope == null) {
            // TODO This isn't really underflow when an forager accepts only moves with acceptChance 0.0
            .warn("Underflow occurred with acceptChanceMaxScoreTotal ({}) " +
                    "and randomChance ({})."randomChance);
            // Deal with it anyway (no fail-fast here)
            pickedMoveScope = .get(.size() - 1);
        }
        return pickedMoveScope;
    }
    public int getAcceptedMovesSize() {
        return .size();
    }
    public List<MovegetTopList(int topSize) {
        sortAcceptedList();
        int size = .size();
        List<MovetopList = new ArrayList<Move>(Math.min(topSizesize));
        List<MoveScopesubAcceptedList = .subList(Math.max(0, size - topSize), size);
        for (MoveScope moveScope : subAcceptedList) {
            topList.add(moveScope.getMove());
        }
        return topList;
    }
    protected void sortAcceptedList() {
        if (!) {
            Collections.sort();
             = true;
        }
    }
New to GrepCode? Check out our FAQ X