<commit_before>package org.drools.planner.core.constructionheuristic.greedyFit.decider;

import org.drools.planner.core.bruteforce.BruteForcePlanningEntityIterator;
import org.drools.planner.core.constructionheuristic.greedyFit.GreedyFitSolverPhaseScope;
import org.drools.planner.core.constructionheuristic.greedyFit.GreedyFitStepScope;
import org.drools.planner.core.score.Score;

public class DefaultGreedyDecider implements GreedyDecider {

    private PickEarlyFitType pickEarlyFitType;

    protected boolean assertMoveScoreIsUncorrupted = false;

    public void setPickEarlyFitType(PickEarlyFitType pickEarlyFitType) {
        this.pickEarlyFitType = pickEarlyFitType;
    }

    public void setAssertMoveScoreIsUncorrupted(boolean assertMoveScoreIsUncorrupted) {
        this.assertMoveScoreIsUncorrupted = assertMoveScoreIsUncorrupted;
    }

    public void decideNextStep(GreedyFitStepScope greedyFitStepScope) {
        GreedyFitSolverPhaseScope greedyFitSolverPhaseScope = greedyFitStepScope.getGreedyFitSolverPhaseScope();
        // TODO use greedyFitSolverPhaseScope.getLastCompletedStepScope()
        Score lastStepScore = greedyFitSolverPhaseScope.calculateScoreFromWorkingMemory();
        BruteForcePlanningEntityIterator bruteForcePlanningEntityIterator = new BruteForcePlanningEntityIterator(
                greedyFitSolverPhaseScope, greedyFitStepScope.getPlanningEntity());
        Score maxScore = greedyFitSolverPhaseScope.getScoreDefinition().getPerfectMinimumScore();
        while (bruteForcePlanningEntityIterator.hasNext()) {
            bruteForcePlanningEntityIterator.next();
            Score score = greedyFitSolverPhaseScope.calculateScoreFromWorkingMemory();
            if (assertMoveScoreIsUncorrupted) {
                greedyFitSolverPhaseScope.assertWorkingScore(score);
            }
            if (score.compareTo(maxScore) > 0) {
                greedyFitStepScope.setVariableToValueMap(bruteForcePlanningEntityIterator.getVariableToValueMap());
                maxScore = score;
            }
            if (pickEarlyFitType == PickEarlyFitType.FIRST_LAST_STEP_SCORE_IMPROVING_OR_EQUAL
                    && score.compareTo(lastStepScore) >= 0) {
                break;
            }
        }
        greedyFitStepScope.setScore(maxScore);
    }

}<commit_msg>performance improvement: don't recalculate the score if not needed<commit_after>package org.drools.planner.core.constructionheuristic.greedyFit.decider;

import org.drools.planner.core.bruteforce.BruteForcePlanningEntityIterator;
import org.drools.planner.core.constructionheuristic.greedyFit.GreedyFitSolverPhaseScope;
import org.drools.planner.core.constructionheuristic.greedyFit.GreedyFitStepScope;
import org.drools.planner.core.score.Score;

public class DefaultGreedyDecider implements GreedyDecider {

    private PickEarlyFitType pickEarlyFitType;

    protected boolean assertMoveScoreIsUncorrupted = false;

    public void setPickEarlyFitType(PickEarlyFitType pickEarlyFitType) {
        this.pickEarlyFitType = pickEarlyFitType;
    }

    public void setAssertMoveScoreIsUncorrupted(boolean assertMoveScoreIsUncorrupted) {
        this.assertMoveScoreIsUncorrupted = assertMoveScoreIsUncorrupted;
    }

    public void decideNextStep(GreedyFitStepScope greedyFitStepScope) {
        GreedyFitSolverPhaseScope greedyFitSolverPhaseScope = greedyFitStepScope.getGreedyFitSolverPhaseScope();
        Score lastStepScore = greedyFitSolverPhaseScope.getLastCompletedStepScope().getScore();
        BruteForcePlanningEntityIterator bruteForcePlanningEntityIterator = new BruteForcePlanningEntityIterator(
                greedyFitSolverPhaseScope, greedyFitStepScope.getPlanningEntity());
        Score maxScore = greedyFitSolverPhaseScope.getScoreDefinition().getPerfectMinimumScore();
        while (bruteForcePlanningEntityIterator.hasNext()) {
            bruteForcePlanningEntityIterator.next();
            Score score = greedyFitSolverPhaseScope.calculateScoreFromWorkingMemory();
            if (assertMoveScoreIsUncorrupted) {
                greedyFitSolverPhaseScope.assertWorkingScore(score);
            }
            if (score.compareTo(maxScore) > 0) {
                greedyFitStepScope.setVariableToValueMap(bruteForcePlanningEntityIterator.getVariableToValueMap());
                maxScore = score;
            }
            if (pickEarlyFitType == PickEarlyFitType.FIRST_LAST_STEP_SCORE_IMPROVING_OR_EQUAL
                    && score.compareTo(lastStepScore) >= 0) {
                break;
            }
        }
        greedyFitStepScope.setScore(maxScore);
    }

}
{'fraction_non_alphanumeric': 0.06504770164787511, 'fraction_numerical': 0.0008673026886383347, 'mean_word_length': 3.92315901814301, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5716925', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1238, 'n_words': 161}