Microsoft Solver Foundation Services 声明性语法

发布于 2024-08-04 11:20:32 字数 1230 浏览 6 评论 0原文

我有以下简单问题,我想用它来试验 [MS Solver Foundation][1]:

我有 10 个槽,需要用 1 到 5 范围内的整数填充。我只想强制执行两个约束:

  • slot[n] != slot[n + 1]
  • 所有槽的总和应大于 20

我可以简单地创建以下决策:

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");

然后手动设置约束,如

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 );

但是,我必须想象有更好的方法这样做——希望以更类似于声明性语法的方式进行。

I have the following simple problem that I'd like to use to experiment with [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:

  • slot[n] != slot[n + 1]
  • the sum of all slots should be more than 20

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漆黑的白昼 2024-08-11 11:20:32

代码。

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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文