我正在研究一个约束问题,尝试使用Mathematica实现。我的答案似乎有效,直到我尝试添加其他约束以仅选择一小部分可用变量。
更新:在此处下载笔记本文件以供参考:https://www.dropbox.com/s/14nt4pf42lxteq3/ConstraintIssues.nb?dl=0
以下是适用的模型:
NetV = -Abs[Sum[Ydata[[i]] * Subscript[y,i],{i,Length[Ydata]}]-
Sum[Xdata[[i]] * Subscript[x,i],{i,Length[Xdata]}]];
NetT =Sum[Ydata[[i]] * Subscript[y,i],{i,Length[Ydata]}]
+Sum[Xdata[[i]] * Subscript[x,i],{i,Length[Xdata]}];
Objective = NetV / NetT;
DomainX = Table[Subscript[x,i],{i, Length[Xdata]}];
DomainY =Table[Subscript[y,i],{i,Length[Ydata]}];
DomainFull = Join[DomainX,DomainY]
添加约束,x和y中的每一个中的所有选择都是净零(#negative = #positive),并且该分母始终为正:
constraintFull = Join[{Apply[Plus,DomainX] == 0},{Apply[Plus,DomainY] == 0},
{DomainFull \[Element]Integers},{NetT > 0}];
result = NMaximize[{Objective,constraintFull},DomainFull]
我修改了输出以便于在此处阅读,但结果是:
Object = -.000496983
X = {-2,-3,-1,-1,1,-1,-1,1,0,0,0,-1,0,2,-2,1,3,0,0,1,0,-3,2,2,2,0,0,-1,1,-7}
Y= {1,-1,0,1,-1,2,-1,0,-1,1,-2,1,0,-1,0,2,1,1,1,-1,1,0,1,0,1,4,0,2,-1,1,-1,0,
0,-1,-1,0,-2,1,0,1,0,2,-2,0,-3,2,-1,0,0,-1,0,0,0,1}
到目前为止一切顺利。现在假设我想添加一个约束来从X和Y中挑选2个项目,每个正面1个,每个负1个。我添加以下约束:
extraConstraint = {Apply[Plus,Map[Abs,DomainX]] == 2, Apply[Plus,Map[Abs,DomainY]] == 2};
spurious = NMaximize[{Objective,Join[constraintFull,extraConstraint]},DomainFull]
然后返回......:
Objective = -.700899
X = {0,-1,0,0,0,0,0,0,1,-1,-1,0,0,0,1,0,0,0,0,0,0,-1,1,1,0,0,1,-1,0,0}
Y = {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
0,0,0,-1,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,-1,0,0,0}
....这绝对是不同的,但也绝对不我期望的行为。我认为这必须与我混淆底层求解器与我的约束;任何人都可以帮助我阐明这种行为,也许可以推荐一种更好的方法来限制我正在寻找的行为?