简体   繁体   English

Microsoft Solver Foundation模运算符

[英]Microsoft Solver Foundation Modulo Operator

I'm trying to add a basic constraint to my solver foundation by doing the following: 我正在尝试通过执行以下操作为我的求解器基础添加基本约束:

model.AddConstraint("c1", x % y == 0);

I get a compile error saying "Operator '%' cannot be applied to operands of type 'Microsoft.SolverFoundation.Services.Decision' and 'Microsoft.SolverFoundation.Services.Decision'". 我收到一个编译错误,指出“运算符'%'无法应用于类型'Microsoft.SolverFoundation.Services.Decision'和'Microsoft.SolverFoundation.Services.Decision'的操作数”。

This makes sense since a lot of operators aren't supported. 这是有道理的,因为不支持许多运算符。 However, a lot of the operators that aren't supported (sin, cos, tan, etc.) are available as specific methods on the Model class such as below: 但是,许多不支持的运算符(sin,cos,tan等)可以作为Model类的特定方法使用,如下所示:

model.AddConstraint("c1", Model.Sum(x,y) == 0);

If I replace "Sum" with "Mod", there is no method available. 如果将“ Sum”替换为“ Mod”,则没有可用的方法。

Any ideas on how to perform a modulo operation in Solver Foundation? 关于如何在Solver Foundation中执行模运算的任何想法? According to the documentation here , it is supported. 根据此处的文档,该文档受支持。

I am going to start using reflector to dig through the code, but I figured I would post it on here also. 我将开始使用反射器来挖掘代码,但我想我也将其发布在这里。 If I find a solution, I will update my question to include the answer. 如果找到解决方案,我将更新问题以包括答案。

I don't see Mod at http://msdn.microsoft.com/en-us/library/ff525341(v=vs.93).aspx , but here's a workaround: 我在http://msdn.microsoft.com/zh-cn/library/ff525341(v=vs.93).aspx上看不到Mod,但这是一种解决方法:

Model.Floor(Model.Quotient(x,y))==Model.Ceiling(Model.Quotient(x,y))

This is only true if x/y is an integer, and so x%y==0. 仅当x / y是整数,并且x%y == 0时,这才是正确的。

There are also other ways of combining the allowed operators to check if it is divisible. 还有其他方式组合允许的运算符以检查其是否可分割。 My favorite (though I wouldn't recommend it because of floating point precision) is 我最喜欢的(尽管由于浮点精度我不推荐使用)是

Model.Sin(Math.PI*Model.Quotient(x,y))==0 

Really interesting, I wanted to write the exact same question at SO :-) 真的很有趣,我想在SO上写下完全相同的问题:-)

I found out that a Mod operator is defined for the OML language at http://msdn.microsoft.com/en-us/library/ff818505(v=vs.93).aspx . 我在http://msdn.microsoft.com/zh-cn/library/ff818505(v=vs.93).aspx上为OML语言定义了Mod运算符。

There is an overload of the AddConstraint method, that accepts an expression string(I guess an OML expression?) instead of the Term. AddConstraint方法有一个重载,它接受表达式字符串(我猜是OML表达式吗?)而不是Term。 Unfortunately, I don't know the right formatting, but once we get the knack out of it, we would be able to use all of the other operators as well. 不幸的是,我不知道正确的格式,但是一旦我们知道了诀窍,我们也将能够使用所有其他运算符。

EDIT 编辑

It looks like not every OML expression described in API is working. 似乎并非API中描述的每个OML表达式都在起作用。 For example, 例如,

SolverContext sc = SolverContext.GetContext();
Model m = sc.CreateModel();

m.AddDecision(new Decision(Domain.IntegerRange(0,10), "a"));
m.AddDecision(new Decision(Domain.IntegerRange(0, 10), "b"));

m.AddConstraint(null, "a > 0");
m.AddConstraint(null, "b == Plus[a,2]");

The Plus[x,y] in this case is working and the solver computes the following decision 在这种情况下,Plus [x,y]正在运行,求解器将计算以下决策

a: 1, b: 3 a:1,b:3

But if I replace the last constraint with this one 但是如果我用这个替换最后一个约束

m.AddConstraint(null, "b == Mod[a,2]");

I get an OmlParseException ("Failed to parse OML model. Expression: Mod[a,2]."). 我得到了OmlParseException(“无法解析OML模型。表达式:Mod [a,2]。”)。 I guess we are on our own here and purhaps the best thing we can do is sticking with the answer of user141603. 我想我们现在就一个人了,也许我们能做的最好的事情就是坚持user141603的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM