Optaplanner自定义MoveFactory未被使用

时间:2016-02-28 15:51:22

标签: java optaplanner

我正在恢复我在6个月前写的optaplanner代码的过程中,在试图弄清楚为什么我的一些硬约束被破坏时,我发现我编写的过滤器应该是过滤掉非法移动没有被提及。我在移动工厂的所有方法,移动方法和过滤器中都设置了断点,并且没有调用任何方法。我很确定在我更新到最新版本之前的情况并非如此,但我可能错了。

更新:当我在我的测试用例中运行optaplanner但未投入生产时正在使用工厂,所以我想这不是因为我的配置而是因为情景,但我不知道会影响什么是否正在使用

我的求解器配置:

<?xml version="1.0" encoding="UTF-8"?>
<solver>
<environmentMode>FULL_ASSERT</environmentMode>

<!-- Domain model configuration -->
<solutionClass>com.rdthree.plenty.services.activities.planner.ActivitySolution</solutionClass>
<entityClass>com.rdthree.plenty.services.activities.helpers.dtos.TaskPlannerDto</entityClass>
<entityClass>com.rdthree.plenty.services.activities.helpers.dtos.TaskResourceAllocationPlannerDto</entityClass>

<!-- Score configuration -->
<scoreDirectorFactory>
    <scoreDefinitionType>HARD_SOFT</scoreDefinitionType>
    <scoreDrl>com/rdthree/plenty/services/activities/planner/activity-scoring.drl</scoreDrl>
    <initializingScoreTrend>ONLY_DOWN</initializingScoreTrend>
</scoreDirectorFactory>

<!-- Optimization algorithms configuration -->
<termination>
    <terminationCompositionStyle>OR</terminationCompositionStyle>
    <bestScoreLimit>0hard/0soft</bestScoreLimit>
    <secondsSpentLimit>60</secondsSpentLimit>
</termination>

<constructionHeuristic>
    <queuedEntityPlacer>
        <entitySelector id="resourceAllocationSelector">
            <entityClass>com.rdthree.plenty.services.activities.helpers.dtos.TaskResourceAllocationPlannerDto</entityClass>
            <cacheType>PHASE</cacheType>
            <selectionOrder>SORTED</selectionOrder>
            <sorterManner>DECREASING_DIFFICULTY_IF_AVAILABLE</sorterManner>
        </entitySelector>
        <changeMoveSelector>
            <entitySelector mimicSelectorRef="resourceAllocationSelector" />
            <valueSelector>
                <variableName>resource</variableName>
                <cacheType>PHASE</cacheType>
            </valueSelector>
        </changeMoveSelector>
    </queuedEntityPlacer>
</constructionHeuristic>

<constructionHeuristic>
    <queuedEntityPlacer>
        <entitySelector id="taskSelector">
            <entityClass>com.rdthree.plenty.services.activities.helpers.dtos.TaskPlannerDto</entityClass>
            <cacheType>PHASE</cacheType>
            <selectionOrder>SORTED</selectionOrder>
            <sorterManner>DECREASING_DIFFICULTY_IF_AVAILABLE</sorterManner>
        </entitySelector>
        <changeMoveSelector>
            <entitySelector mimicSelectorRef="taskSelector" />
            <filterClass>com.rdthree.plenty.services.activities.planner.filters.TaskLengthChnageFilter</filterClass>
            <valueSelector>
                <variableName>interval</variableName>
                <cacheType>PHASE</cacheType>
            </valueSelector>
        </changeMoveSelector>
    </queuedEntityPlacer>
</constructionHeuristic>

<localSearch>
    <unionMoveSelector>
        <moveListFactory>
            <moveListFactoryClass>com.rdthree.plenty.services.activities.planner.MoveResourceAllocationMoveFactory</moveListFactoryClass>
        </moveListFactory>
        <changeMoveSelector>
            <fixedProbabilityWeight>1.0</fixedProbabilityWeight>
            <filterClass>com.rdthree.plenty.services.activities.planner.filters.TaskLengthChnageFilter</filterClass>
            <entitySelector id="taskMoveSelector">
                <entityClass>com.rdthree.plenty.services.activities.helpers.dtos.TaskPlannerDto</entityClass>
            </entitySelector>
            <valueSelector>
                <variableName>interval</variableName>
            </valueSelector>
        </changeMoveSelector>
    </unionMoveSelector>

    <acceptor>
        <valueTabuSize>7</valueTabuSize>
    </acceptor>
    <forager>
        <acceptedCountLimit>2000</acceptedCountLimit>
    </forager>
</localSearch>

我的自定义搬家工厂:

public class MoveResourceAllocationMoveFactory implements MoveListFactory<ActivitySolution> {

@Override
public List<? extends Move> createMoveList(ActivitySolution solution) {
    List<Move> moveList = new ArrayList<Move>();
    for (TaskResourceAllocationPlannerDto allocation : solution.getResourceAllocations()) {
        for (TaskResourcePlannerDto resource : solution.getResources()) {
            moveList.add(new MoveResourceAllocations(allocation, resource));
        }
    }
    return moveList;
}

}

我的自定义举动:

public class MoveResourceAllocations extends AbstractMove {

private TaskResourceAllocationPlannerDto allocation;

private TaskResourcePlannerDto newResource;

@Getter
@Setter
boolean doMove;

public MoveResourceAllocations(TaskResourceAllocationPlannerDto allocation, TaskResourcePlannerDto newResource) {
    super();
    this.allocation = allocation;
    this.newResource = newResource;
}

@Override
public boolean isMoveDoable(ScoreDirector scoreDirector) {
    if (allocation.getResource().equals(newResource)) {
        return false;
    }
    return new ResourceTypeMismatchFilter().acceptCustomMove(scoreDirector, this);
}

@Override
public Move createUndoMove(ScoreDirector scoreDirector) {
    return new MoveResourceAllocations(allocation, allocation.getResource());
}

@Override
public void doMoveOnGenuineVariables(ScoreDirector scoreDirector) {
    scoreDirector.beforeVariableChanged(allocation, "resource");

    updateOnHandAmounts(scoreDirector);

    allocation.setResource(newResource);

    scoreDirector.afterVariableChanged(allocation, "resource");
}


private void updateOnHandAmounts(ScoreDirector scoreDirector) {
    ActivitySolution solution = (ActivitySolution) scoreDirector.getWorkingSolution();
    List<OnHandForProduct> onHandForProducts = solution.getOnHandForProducts();
    List<ProductInventoryTransactionPlannerDto> transactions = solution.getTransactions();
    boolean transactionFoundForTask = false;
    if ((newResource.getClass().getSimpleName().contains(Product.class.getSimpleName()))
            && allocation.getResourceClass().equals(Product.class)) {
        // find the transaction caused by the task and product in question and replace the product in the
        // transaction with the newly assigned product and revert this for an undo move
        for (ProductInventoryTransactionPlannerDto transaction : transactions) {
            if (transaction.getCauseId().equals(allocation.getTaskId())
                    && transaction.getProductId() == (allocation.getResource().getId())
                    && transaction.getTransactionTypeName().equals(InventoryTransactionType.SUBTRACT)) {
                transaction.setProductId(newResource.getId());
                transactionFoundForTask = true;
                break;
            }
        }
        if (!transactionFoundForTask) {
            throw new EmptyResultDataAccessException(
                    "Internal scheduler fail: no product transaction found for the product-requiring task with id: "
                            + allocation.getTaskId() + " for product : " + allocation.getResource(), 1);
        }
        TaskPlannerDto thisTask = null;
        for (TaskPlannerDto task : solution.getTasks()) {
            if (task.getId().equals(allocation.getTaskId())) {
                thisTask = task;
            }
        }
        Long oldProductId = allocation.getResource().getId();
        Long newProductId = newResource.getId();
        for (OnHandForProduct onHandForProduct : onHandForProducts) {
            if (onHandForProduct.getProductId().equals(oldProductId)
                    && onHandForProduct.getDate().isAfter(
                            thisTask.getInterval().getStart().withTimeAtStartOfDay()
                                    .plusDays(0/* - GeneralPrefs.PRODUCT_PRESENCE_SAFETY_BUFFER*/))) {
                onHandForProduct.setAmount(onHandForProduct.getAmount() + allocation.getAmount());
            }
            if (onHandForProduct.getProductId().equals(newProductId)
                    && onHandForProduct.getDate().isAfter(
                            thisTask.getInterval().getStart().withTimeAtStartOfDay()
                            .plusDays(0/* - GeneralPrefs.PRODUCT_PRESENCE_SAFETY_BUFFER*/))) {
                onHandForProduct.setAmount(onHandForProduct.getAmount() - allocation.getAmount());
            }
        }
    }
}

@Override
public Collection<? extends Object> getPlanningEntities() {
    return Collections.singletonList(allocation);
}

@Override
public Collection<? extends Object> getPlanningValues() {
    return Collections.singletonList(newResource);
}

@Override
public String toString() {
    return "replacing resource " + allocation.getResource() + " for task with id " + allocation.getId() + " with "
            + newResource;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((allocation == null) ? 0 : allocation.hashCode());
    result = prime * result + ((newResource == null) ? 0 : newResource.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    MoveResourceAllocations other = (MoveResourceAllocations) obj;
    if (allocation == null) {
        if (other.allocation != null)
            return false;
    } else if (!allocation.equals(other.allocation))
        return false;
    if (newResource == null) {
        if (other.newResource != null)
            return false;
    } else if (!newResource.equals(other.newResource))
        return false;
    return true;
}

}

1 个答案:

答案 0 :(得分:1)

配置看起来不错。

1)也许2构造启发式阶段永远不会完全结束。 打开INFO日志记录(或者更好的是DEBUG)。当两个构造启发式结束时,它将记录。

2)也许本地搜索以ChangeMoveSelector开头(它是一个联合,因此2个选择器中的任何一个都可以先行),并且它会以某种方式挂起在过滤器中。打开TRACE日志记录以查看选定的移动。