Message Boards Message Boards

0
|
3545 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Summation indices inside of NMaximize

Posted 10 years ago

Hello!

Why don't the following commands produce equivalent results?

NMaximize[{10 *(-d[1] + d[2])^4* q[1, 2]^4 + (-d[1] + d[2])^2* 
    q[1, 2]^2* (4* q[1, 2]^2 + (q[1, 1] - q[2, 2])^2) - 
   6 *(d[1] - d[2])^3 *
    q[1, 2]^2 (q[1, 1] - q[2, 2]) (d[1] *q[1, 1] + d[2]* q[2, 2]) + 
   2 *(d[1] - d[2])^4* q[1, 2]^2 *(d[1]* q[1, 1] + d[2]* q[2, 2])^2, 
  d[1]^2 + d[2]^2 == 1, 
  q[1, 1]^2 + 2*q[1, 2]^2 + q[2, 2]^2 == 1}, {q[1, 1], q[1, 2], 
  q[2, 2], d[1], d[2]}]

and

NMaximize[{10 *(-x + y)^4 *b^4 + (-x + y)^2* 
    b^2 *(4* b^2 + (a - c)^2) - 
   6 (x - y)^3* b^2 (a - c)* (x* a + y *c) + 
   2 *(x - y)^4 *b^2*(x *a + y *c)^2, x^2 + y^2 == 1, 
  a^2 + 2*b^2 + c^2 == 1}, {a, b, c, x, y}]

For the first I get output :

NMaximize::nosat: Obtained solution does not satisfy the following constraints within Tolerance -> 0.001`: {1-d[1]^2-d[2]^2==0,1-q[1,1]^2-2 q[1,2]^2-q[2,2]^2==0}. >>

For the second I get (correct) output:

{12.0002, {a -> 1.01851*10^-6, b -> -0.707108, c -> -3.9843*10^-7, 
  x -> -0.707108, y -> 0.70711}}

The first comes from using a summation index inside a NMaximize routine and I get the wrong result. When I replace the indexed variables with a,b,c,x,y then I get the correct results. I want to be able to vary the limit of summation for such functions and optimize them but it is not computing correctly.

Thanks!

POSTED BY: PW Laslo
4 Replies

The issue seems to appear when Method->"NelderMead" is used. Using "DifferentialEvolution" or "SimulatedAnnealing" works fine for the first example.

It is a bit strange since Method->"NelderMead" works fine on the second example. The variable names do influence the algebraic form given to NMaximize, but I didn't see any obvious evidence that was the issue. It might still be the case.

Anyway, long story short, I'd try using Method->"DifferentialEvolution".

POSTED BY: Sean Clarke
Posted 10 years ago

Sean changing the method works, thanks!

POSTED BY: PW Laslo

I feels like there is something buggy going on here.

Here is the original expression that yields the error message reported.

NMaximize[{10 (-d[1] + d[2])^4 q[1, 2]^4 + (-d[1] + d[2])^2 q[1, 
     2]^2 (4 q[1, 2]^2 + (q[1, 1] - q[2, 2])^2) - 
   6 (d[1] - d[2])^3 q[1, 
     2]^2 (q[1, 1] - q[2, 2]) (d[1] q[1, 1] + d[2] q[2, 2]) + 
   2 (d[1] - d[2])^4 q[1, 2]^2 (d[1] q[1, 1] + d[2] q[2, 2])^2, 
  d[1]^2 + d[2]^2 == 1, 
  q[1, 1]^2 + 2 q[1, 2]^2 + q[2, 2]^2 == 1}, {q[1, 1], q[1, 2], 
  q[2, 2], d[1], d[2]}]

Let's try to get rid of the problem by defining the functions d and q to replace themselves by symbols as in this:

d[i_] := ToExpression["d" <> ToString[i]];
q[i_, j_] := ToExpression["q" <> ToString[i] <> ToString[j]];

When we do this the original NMaximize problem above still yields the error message.

Ok, let's try the expression with the substitutions evaluated in the arguments of NMaximize as in this:

NMaximize[{10 (-d1 + d2)^4 q12^4 + (-d1 + 
      d2)^2 q12^2 (4 q12^2 + (q11 - q22)^2) - 
   6 (d1 - d2)^3 q12^2 (q11 - q22) (d1 q11 + d2 q22) + 
   2 (d1 - d2)^4 q12^2 (d1 q11 + d2 q22)^2, d1^2 + d2^2 == 1, 
  q11^2 + 2 q12^2 + q22^2 == 1}, {q11, q12, q22, d1, d2}]

This still gives the error message. Hmmmm.... ok, let's add some rules to the definitions for the functions d and q so that they just evaluate to the parameters that are given in Laslo's 2nd example that works out correctly:

d[1] := x;
d[2] := y;
q[1, 1] := a;
q[1, 2] := b;
q[2, 2] := c;

Now when we execute

NMaximize[{10 (-d[1] + d[2])^4 q[1, 2]^4 + (-d[1] + d[2])^2 q[1, 
     2]^2 (4 q[1, 2]^2 + (q[1, 1] - q[2, 2])^2) - 
   6 (d[1] - d[2])^3 q[1, 
     2]^2 (q[1, 1] - q[2, 2]) (d[1] q[1, 1] + d[2] q[2, 2]) + 
   2 (d[1] - d[2])^4 q[1, 2]^2 (d[1] q[1, 1] + d[2] q[2, 2])^2, 
  d[1]^2 + d[2]^2 == 1, 
  q[1, 1]^2 + 2 q[1, 2]^2 + q[2, 2]^2 == 1}, {q[1, 1], q[1, 2], 
  q[2, 2], d[1], d[2]}]

no error message is generated and the correct answer is returned.

It is odd that using the variables {d1, d2, q11, q12, q22} give the error message while using {x,y,a, b, c} do not.

It smells like a bug but the possibilities that Sean is suggesting would still be in play here -- especially the issue of sorting the expression form. [Note that I edited my post to correct my earlier statement that Sean's comments may not cover my example. They well may.]

POSTED BY: David Reiss

There are essentially 2 related reasons why I think people see these behavior.

  1. You have not specified a value for the Method option for NMaximize. The "Method" option tells NMaximize which numerical method to use. Without it, NMaximize will try to select the numerical method that is most appropriate based on what it sees. In this case, it's pretty likely it's choosing different numerical methods and in one case the choice is better than the other. My advice is to try a number of them and see which work best for your kind of problem.

  2. As you may know, floating point numbers are sensitive. For example, the order in which a set of floating number is added together can matter greatly.

When adding some number of symbols together in the Wolfram Language, they will be sorted alphabetically. So c+b+a, becomes a+b+c. This means that if you're not careful, the names of your symbols can change the equations used by NMaximize. This could be a problem if the new equation is less numerically stable.

POSTED BY: Sean Clarke
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract