[英]Microsoft Solver Foundation Services declarative syntax
I have the following simple problem that I'd like to use to experiment with [MS Solver Foundation][1]: 我要使用以下简单问题来尝试[MS Solver Foundation] [1]:
I have 10 slots that I need to fill with integers in the range 1 to 5. I want to enforce only two constraints: 我有10个插槽,需要填充1到5范围内的整数。我只想强制执行两个约束:
I could simply create the following decisions: 我可以简单地做出以下决定:
Decision s1 = new Decision(Domain.IntegerRange(1, 5), "slot1");
Decision s2 = new Decision(Domain.IntegerRange(1, 5), "slot2");
Decision s3 = new Decision(Domain.IntegerRange(1, 5), "slot3");
Decision s4 = new Decision(Domain.IntegerRange(1, 5), "slot4");
Decision s5 = new Decision(Domain.IntegerRange(1, 5), "slot5");
Decision s6 = new Decision(Domain.IntegerRange(1, 5), "slot6");
Decision s7 = new Decision(Domain.IntegerRange(1, 5), "slot7");
Decision s8 = new Decision(Domain.IntegerRange(1, 5), "slot8");
Decision s9 = new Decision(Domain.IntegerRange(1, 5), "slot9");
Decision s10 = new Decision(Domain.IntegerRange(1, 5), "slot10");
And then setup constraints manually as in 然后手动设置约束,如
model.AddConstraints("neighbors not equal",
s1 != s2, s2 != s3, s3 != s4, s4 != s5,
s5 != s6, s6 != s7, s7!= s8, s8 != s9, s9 != s10
);
model.AddConstraint("sum",
s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 > 20 );
However, I have to imagine that there's a better way to do this--hopefully in something more akin to declarative syntax. 但是,我必须想象有一种更好的方法来完成此操作-希望采用类似于声明性语法的方式。
The code. 编码。
SolverContext context = SolverContext.GetContext();
Model model = context.CreateModel();
Decision[] slot = new Decision[10];
for (int i = 0; i < slot.Length; i++)
{
slot[i] = new Decision(Domain.IntegerRange(1, 5), "slot" + i.ToString());
model.AddDecision(slot[i]);
if (i > 0) model.AddConstraint("neighbors not equal", slot[i-1] != slot[i]);
}
model.AddConstraint("sum", Model.Sum(slot) > 20);
Solution solution = context.Solve();
Discussions are moved to our official forum already: 讨论已移至我们的官方论坛:
http://code.msdn.microsoft.com/solverfoundation/Thread/View.aspx?ThreadId=2256 http://code.msdn.microsoft.com/solverfoundation/Thread/View.aspx?ThreadId=2256
Lengning 冷凝管
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.