Remove/Add 约束 - optaplanner
Remove/Add constraints - optaplanner
是否可以让一些规则完全被忽略?我有一套规则,但我希望用户能够添加某些输入,比如他们想要规则 1、规则 2、规则 3、规则 5,但可能不需要规则 4。所以我希望程序能够识别这一点,在检查约束违规时根本不输入规则 4
我该怎么做?
一个简单的解决方案是创建
class Guard {
String rulename; ... }
以及向您的规则添加模式
rule rule_1
when
Guard( rulename == "rule_1" )
...
然后您需要为应该被阻止的规则插入 Guard 事实。
我有一个类似的问题,除了我希望用户控制每个规则权重 [0 到 10] 而不是二进制控制 [活动 - 不活动]。我不确定这是一个性能优化的答案(在合理的时间内为我工作,但这当然取决于你的 corpus 大小)。
我们使用的方法是创建一个 Singleton,它将用户设置的每个规则权重保存在易失性内存中,使用 HashMap<Integer, Integer>
来实现更快的随机访问(key =规则编号,值 = 规则权重),因为推理引擎可能会多次调用它。
然后,在我们的规则文件中,我们检查给定规则是否在 when
子句中处于活动状态,并根据实际规则权重更新 then
子句中的分数:
import my.rules.singleton.package.MyRulesWeightSingleton;
rule "1"
when
eval(MyRulesWeightSingleton.isRuleActive(1))
//other conditions here.
then
scoreHolder.addHardConstraintMatch(kcontext, MyRulesWeightSingleton.getRuleWeight(1));
end
Singleton 看起来像这样:
import java.util.HashMap;
public class MyRulesWeightSingleton {
private static MyRulesWeightSingleton instance;
private HashMap<Integer, Integer> weights;
private MyRulesWeightSingleton() {
this.weights = new HashMap<Integer, Integer>();
}
public static MyRulesWeightSingleton getInstance() {
if (instance == null) {
instance = new MyRulesWeightSingleton();
}
return instance;
}
public boolean isRuleActive(int ruleId) {
Integer weight = weights.get(ruleId);
return (weight != null && weight > 0);
}
public int getRuleWeight(int ruleId) {
Integer weight = weights.get(ruleId);
if (weight != null) {
return weight;
}
return 0;
}
//Allows you to put and remove weights information
//before running planner.
public HashMap<Integer, Integer> getWeights() {
return this.weights;
}
}
PS:我们这里的实际问题比这复杂一点,所以出于教学原因我简化了我们的方法。 ;)
是否可以让一些规则完全被忽略?我有一套规则,但我希望用户能够添加某些输入,比如他们想要规则 1、规则 2、规则 3、规则 5,但可能不需要规则 4。所以我希望程序能够识别这一点,在检查约束违规时根本不输入规则 4
我该怎么做?
一个简单的解决方案是创建
class Guard {
String rulename; ... }
以及向您的规则添加模式
rule rule_1
when
Guard( rulename == "rule_1" )
...
然后您需要为应该被阻止的规则插入 Guard 事实。
我有一个类似的问题,除了我希望用户控制每个规则权重 [0 到 10] 而不是二进制控制 [活动 - 不活动]。我不确定这是一个性能优化的答案(在合理的时间内为我工作,但这当然取决于你的 corpus 大小)。
我们使用的方法是创建一个 Singleton,它将用户设置的每个规则权重保存在易失性内存中,使用 HashMap<Integer, Integer>
来实现更快的随机访问(key =规则编号,值 = 规则权重),因为推理引擎可能会多次调用它。
然后,在我们的规则文件中,我们检查给定规则是否在 when
子句中处于活动状态,并根据实际规则权重更新 then
子句中的分数:
import my.rules.singleton.package.MyRulesWeightSingleton;
rule "1"
when
eval(MyRulesWeightSingleton.isRuleActive(1))
//other conditions here.
then
scoreHolder.addHardConstraintMatch(kcontext, MyRulesWeightSingleton.getRuleWeight(1));
end
Singleton 看起来像这样:
import java.util.HashMap;
public class MyRulesWeightSingleton {
private static MyRulesWeightSingleton instance;
private HashMap<Integer, Integer> weights;
private MyRulesWeightSingleton() {
this.weights = new HashMap<Integer, Integer>();
}
public static MyRulesWeightSingleton getInstance() {
if (instance == null) {
instance = new MyRulesWeightSingleton();
}
return instance;
}
public boolean isRuleActive(int ruleId) {
Integer weight = weights.get(ruleId);
return (weight != null && weight > 0);
}
public int getRuleWeight(int ruleId) {
Integer weight = weights.get(ruleId);
if (weight != null) {
return weight;
}
return 0;
}
//Allows you to put and remove weights information
//before running planner.
public HashMap<Integer, Integer> getWeights() {
return this.weights;
}
}
PS:我们这里的实际问题比这复杂一点,所以出于教学原因我简化了我们的方法。 ;)