英文:
Disabling and enabling constraints during runtime
问题
I've set the constaints like this:-
ScoreDirectorFactoryConfig scoreDirectorFactoryConfig = new ScoreDirectorFactoryConfig();
scoreDirectorFactoryConfig.setConstraintProviderClass(VehicleRoutingConstraintProvider.class);
I couldn't find out anything related to enabling and/or disabling constraints in optaplanner.
So, I wanted to know if there's a way to enable and disable the constraints after or before setting the constraint provider class.
My question is:
Can we enable and/or disable constraint during runtime in optaplanner based on a condition?
英文:
I've set the constaints like this:-
ScoreDirectorFactoryConfig scoreDirectorFactoryConfig = new ScoreDirectorFactoryConfig(); scoreDirectorFactoryConfig.setConstraintProviderClass(VehicleRoutingConstraintProvider.class);
I couldn't find out anything related to enabling and/or disabling constraints in optaplanner.
So, I wanted to know if there's a way to enable and disable the constraints after or before setting the constraint provider class.
My question is:
Can we enable and/or disable constraint during runtime in optaplanner based on a condition?
答案1
得分: 1
你可以使用@ConstraintConfiguration
类来禁用约束,并将与禁用约束对应的@ConstraintWeight
字段设置为零分数(请参阅 https://www.optaplanner.org/docs/optaplanner/latest/score-calculation/score-calculation.html#constraintConfiguration 以获取完整的详细信息)。
Planning Solution Class(规划解决方案类):
@PlanningSolution
public class ConferenceSolution {
@ConstraintConfigurationProvider
private ConferenceConstraintConfiguration constraintConfiguration;
// ...
}
ConstraintConfiguration(约束配置类):
@ConstraintConfiguration(constraintPackage = "...conferencescheduling.score")
public class ConferenceConstraintConfiguration {
@ConstraintWeight("Speaker conflict")
private HardMediumSoftScore speakerConflict = HardMediumSoftScore.ofHard(10);
@ConstraintWeight("Theme track conflict")
private HardMediumSoftScore themeTrackConflict = HardMediumSoftScore.ofSoft(10);
@ConstraintWeight("Content conflict")
private HardMediumSoftScore contentConflict = HardMediumSoftScore.ofSoft(100);
// ...
}
禁用约束:
ConferenceConstraintConfiguration constraintConfig = new ConferenceConstraintConfiguration();
constraintConfig.contentConflict = HardMediumSoftScore.ZERO;
请注意,我已经将代码示例中的注释和代码部分翻译成中文。
英文:
You can disable constraints by using a @ConstraintConfiguration
class and set the @ConstraintWeight
field corresponding to the disabled constraint to the zero score (see https://www.optaplanner.org/docs/optaplanner/latest/score-calculation/score-calculation.html#constraintConfiguration for full details).
Planning Solution Class:
@PlanningSolution
public class ConferenceSolution {
@ConstraintConfigurationProvider
private ConferenceConstraintConfiguration constraintConfiguration;
// ...
}
ConstraintConfiguration:
@ConstraintConfiguration(constraintPackage = "...conferencescheduling.score")
public class ConferenceConstraintConfiguration {
@ConstraintWeight("Speaker conflict")
private HardMediumSoftScore speakerConflict = HardMediumSoftScore.ofHard(10);
@ConstraintWeight("Theme track conflict")
private HardMediumSoftScore themeTrackConflict = HardMediumSoftScore.ofSoft(10);
@ConstraintWeight("Content conflict")
private HardMediumSoftScore contentConflict = HardMediumSoftScore.ofSoft(100);
// ...
}
Disabling constraints
ConferenceConstraintConfiguration constraintConfig = new ConferenceConstraintConfiguration();
constraintConfig.contentConflict = HardMediumSoftScore.ZERO;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论