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

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

Disabling and enabling constraints during runtime

问题

I've set the constaints like this:-

  1. ScoreDirectorFactoryConfig scoreDirectorFactoryConfig = new ScoreDirectorFactoryConfig();
  2. 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:-

  1. 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(规划解决方案类):

  1. @PlanningSolution
  2. public class ConferenceSolution {
  3. @ConstraintConfigurationProvider
  4. private ConferenceConstraintConfiguration constraintConfiguration;
  5. // ...
  6. }

ConstraintConfiguration(约束配置类):

  1. @ConstraintConfiguration(constraintPackage = "...conferencescheduling.score")
  2. public class ConferenceConstraintConfiguration {
  3. @ConstraintWeight("Speaker conflict")
  4. private HardMediumSoftScore speakerConflict = HardMediumSoftScore.ofHard(10);
  5. @ConstraintWeight("Theme track conflict")
  6. private HardMediumSoftScore themeTrackConflict = HardMediumSoftScore.ofSoft(10);
  7. @ConstraintWeight("Content conflict")
  8. private HardMediumSoftScore contentConflict = HardMediumSoftScore.ofSoft(100);
  9. // ...
  10. }

禁用约束:

  1. ConferenceConstraintConfiguration constraintConfig = new ConferenceConstraintConfiguration();
  2. 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:

  1. @PlanningSolution
  2. public class ConferenceSolution {
  3. @ConstraintConfigurationProvider
  4. private ConferenceConstraintConfiguration constraintConfiguration;
  5. // ...
  6. }

ConstraintConfiguration:

  1. @ConstraintConfiguration(constraintPackage = "...conferencescheduling.score")
  2. public class ConferenceConstraintConfiguration {
  3. @ConstraintWeight("Speaker conflict")
  4. private HardMediumSoftScore speakerConflict = HardMediumSoftScore.ofHard(10);
  5. @ConstraintWeight("Theme track conflict")
  6. private HardMediumSoftScore themeTrackConflict = HardMediumSoftScore.ofSoft(10);
  7. @ConstraintWeight("Content conflict")
  8. private HardMediumSoftScore contentConflict = HardMediumSoftScore.ofSoft(100);
  9. // ...
  10. }

Disabling constraints

  1. ConferenceConstraintConfiguration constraintConfig = new ConferenceConstraintConfiguration();
  2. 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:

确定