gpt4 book ai didi

java - Optaplanner 自定义 MoveFactory 未使用

转载 作者:行者123 更新时间:2023-12-01 10:19:39 25 4
gpt4 key购买 nike

我正在恢复我大约 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;
}

}

我的自定义 Action :

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) 也许第 2 个构建启发阶段永远不会完全完成。打开 INFO 日志记录(或者更好的是 DEBUG)。当 2 个构造启发法中的每一个结束时,它将记录。

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

关于java - Optaplanner 自定义 MoveFactory 未使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35684664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com