在运行时禁用和启用约束条件

huangapple go评论81阅读模式
英文:

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;

huangapple
  • 本文由 发表于 2023年5月17日 13:29:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定