自定义VariableListener更新多个阴影变量 [英] Custom VariableListener updating more than one shadow variables

查看:271
本文介绍了自定义VariableListener更新多个阴影变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关影子变量和自定义变量侦听器的文档,我想知道我是否在正确理解它们如何工作的正确轨道上.

I am reading in the documentation about shadow variables and custom variable listeners and i was wondering if i am on the right track in understanding how they work.

从OptaPlanner文档的第4.3.6.4节中复制. 自定义变量监听器"

如果一个VariableListener更改了两个阴影变量(因为 两个单独的VariableListeners效率不高),然后进行注释 只有第一个阴影变量具有variableListenerClass并让 其他阴影变量引用第一个阴影变量:

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;
}

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

So as i understand from this when the genuine planning variable gets changed and we have more shadow variables that need to be updated in another planning entity accordingly, we can do this in the same variable listener for the genuine planning entity.

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

If this is so then would something like this be valid?

以下是阴影计划实体中阴影变量的带注释方法.

Here are the annotated methods for the shadow variables in the shadow planning entity.

//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的代码

and the code of the VariableListener of the genuine planning entity

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方法应该这样调用吗?

If that is not ok then how to properly update all the shadow variables from the shadow planning entity? Should the beforeVariableChanged and afterVariableChanged methods of the shadow variables be called like that?

推荐答案

是的,乍一看似乎还可以.例如,以ArrivalTimeUpdatingVariableListener为例,可以很好地说明在VRP链中更改1个客户如何影响该链中所有其他客户在该客户之后的到达时间.

Yes, that seems ok on first sight. Look at for example ArrivalTimeUpdatingVariableListener for a good example on how changing 1 Customer in a VRP chain can affect the arrival time of all other customers in the chain after that customer.

这篇关于自定义VariableListener更新多个阴影变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆