gpt4 book ai didi

optaplanner - 自定义 VariableListener 更新多个影子变量

转载 作者:行者123 更新时间:2023-12-02 01:27:04 29 4
gpt4 key购买 nike

我正在阅读有关影子变量和自定义变量监听器的文档,我想知道我是否在理解它们的工作方式方面走在正确的轨道上。

复制自 OptaPlanner 文档的第 4.3.6.4 节。 “自定义变量监听器”

If one VariableListener changes two shadow variables (because having two separate VariableListeners would be inefficient), then annotate only the first shadow variable with the variableListenerClass and let the other shadow variable(s) reference the first shadow variable:

@PlanningVariable(...)
public Standstill getPreviousStandstill() {
return previousStandstill;
}

@CustomShadowVariable(variableListenerClass = TransportTimeAndCapacityUpdatingVariableListener.class,
sources = {@CustomShadowVariable.Source(variableName = "previousStandstill")})
public Integer getTransportTime() {
return transportTime;
}

@CustomShadowVariable(variableListenerRef = @PlanningVariableReference(variableName = "transportTime"))
public Integer getCapacity() {
return capacity;
}


因此,据我了解,当真正的计划变量发生更改并且我们有更多的影子变量需要在另一个计划实体中相应地更新时,我们可以在真正的计划实体的同一变量监听器中执行此操作。

如果是这样,那么这样的事情是否有效?

以下是影子规划实体中影子变量的注释方法。
//shadow variables
protected Integer variable;
protected Integer shadowVariable2;

@CustomShadowVariable(variableListenerClass = CustomVariableListener.class,
sources = {@CustomShadowVariable.Source(variableName = "variable")})
public Integer getVariable() {
return variable;
}

@CustomShadowVariable(variableListenerRef = @PlanningVariableReference(variableName = "variable"))
public Integer getShadowVariable2() {
return shadowVariable2;
}

以及真正的规划实体的VariableListener的代码
public class CustomVariableListener implements VariableListener<GenuinePlanningEntity> {

@Override
public void afterEntityAdded(ScoreDirector scoreDirector, GenuinePlanningEntity genuinePlanningEntity) {

}

@Override
public void afterEntityRemoved(ScoreDirector scoreDirector, GenuinePlanningEntity genuinePlanningEntity) {

}

@Override
public void afterVariableChanged(ScoreDirector scoreDirector, GenuinePlanningEntity genuinePlanningEntity) {
List<ShadowPlanningEntity> shadowPlanningEntities = genuinePlanningEntity.getShadowPlanningEntities();
Integer variable = genuinePlanningEntity.getVariable();
for(ShadowPlanningEntity shadowPlanningEntity : shadowPlanningEntities){
scoreDirector.beforeVariableChanged(shadowPlanningEntity,"variable");
shadowPlanningEntity.setVariable(variable);
scoreDirector.afterVariableChanged(shadowPlanningEntity,"variable");

scoreDirector.beforeVariableChanged(shadowPlanningEntity,"shadowVariable2");
shadowPlanningEntity.setshadowVariable2(shadowPlanningEntity.getshadowVariable2() + 1);
scoreDirector.afterVariableChanged(shadowPlanningEntity,"shadowVariable2");

}

}

@Override
public void beforeEntityAdded(ScoreDirector scoreDirector, GenuinePlanningEntity genuinePlanningEntity) {

}

@Override
public void beforeEntityRemoved(ScoreDirector scoreDirector, GenuinePlanningEntity genuinePlanningEntity) {

}

@Override
public void beforeVariableChanged(ScoreDirector scoreDirector, GenuinePlanningEntity genuinePlanningEntity) {

}



}

如果那不行,那么如何从影子规划实体正确更新所有影子变量?
影子变量的 beforeVariableChanged 和 afterVariableChanged 方法应该这样调用吗?

最佳答案

是的,乍一看似乎没问题。看例如ArrivalTimeUpdatingVariableListener一个很好的例子,说明在 VRP 链中更改 1 个客户如何影响该客户之后链中所有其他客户的到达时间。

关于optaplanner - 自定义 VariableListener 更新多个影子变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36538367/

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