Message Boards Message Boards


Constraint Optimization Spurious Results

Posted 6 years ago
1 Reply
0 Total Likes

I am working on a constraint problem, trying to implement using Mathematica. My answer appears to work until I try to add additional constraints to select only a small subset of the available variables.

Here is the model that works:

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] 

Adding constraints that all selections from each of x and y are net zero (#negative = #positive), and that denominator always positive:

constraintFull = Join[{Apply[Plus,DomainX] == 0},{Apply[Plus,DomainY] == 0},
{DomainFull \[Element]Integers},{NetT > 0}];

result = NMaximize[{Objective,constraintFull},DomainFull]

I have modified the output to make it easier to read here, but the result is:

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,

So far so good. Now suppose I want to add a constraint to pick 2 items each from X and Y, 1 each positive and 1 each negative. I add the following constraint:

extraConstraint = {Apply[Plus,Map[Abs,DomainX]] == 2, Apply[Plus,Map[Abs,DomainY]] == 2};

spurious = NMaximize[{Objective,Join[constraintFull,extraConstraint]},DomainFull]

This then returns......:

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,

....which is definitely different, but also definitely not the behavior I expected. I think this must have to do with me confusing the underlying solver with my constraints; can anyone help me shed some light on this behavior, and perhaps recommend a better way to constrain the behavior I am looking for?

Posted 6 years ago

Notebook attached for reference

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract