Message Boards Message Boards

0
|
1425 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Solving optimization problem with matrix variable

Hello all,

I have been working on an optimization problem with a matrix variable in Mathematica, but I haven't managed to get a numerical solution, as I am receiving some error messages. Below is a simple example representing my code:

Xrs1 = RandomReal[{0, 1}, {10, 3}];
Xrs2 = RandomReal[{0, 1}, {40, 3}];
Xdr2 = RandomReal[{-5, 5}, {40, 2}];

d = DistanceMatrix[Xrs1, Xrs2];

dr[Xdr1_] := DistanceMatrix[Xdr1, Xdr2];

f[Xdr1_] := Total[(dr[Xdr1] - d)^2, 2];

FindMinimum[f[Xdr1], {Xdr1, ConstantArray[0., {10, 2}]}]

I receive several errors, despite the fact that the function f works perfectly with the initial solution or any other numerical matrix with that dimensions.

I would appreciate any help.

Thank you in advance for your time and support.

3 Replies

I guess that DistanceMatrix was designed and optimized for numerical speed. Here we are feeding it symbolic parameters, which was not intended usage.

POSTED BY: Gianluca Gorni

I think that FindMinimum does not accept symbolic matrix variables. Also, DistanceMatrix must be given an explicit DistanceFunction when the data are mixed numerical-symbolic, otherwise we get unexpected results:

DistanceMatrix[{{x}}, {{0}}]

{{1}}

This seems to give an answer:

Xrs1 = RandomReal[{0, 1}, {10, 3}];
Xrs2 = RandomReal[{0, 1}, {40, 3}];
Xdr2 = RandomReal[{-5, 5}, {40, 2}];

d = DistanceMatrix[Xrs1, Xrs2];

dr[Xdr1_] := 
  DistanceMatrix[Xdr1, Xdr2, 
    DistanceFunction -> EuclideanDistance] /. Abs -> Identity;

f[Xdr1_] := Total[(dr[Xdr1] - d)^2, 2];

FindMinimum[f[Array[Xdr1, {10, 2}]], 
 Flatten[Table[{Xdr1[i, j], 0}, {i, 10}, {j, 2}], 1] // N]

I was forced to append an N because of this further strange behaviour of DistanceMatrix:

In[137]:= DistanceMatrix[{{x[1]}}, {0.}, 
 DistanceFunction -> EuclideanDistance]

Out[137]= {{Abs[0. + x[1.]]}}

whereby x[1] in input becomes x[1.] on output.

POSTED BY: Gianluca Gorni

Thank you very much for your time and your answer.

That behavior of the DistanceMatrix function is wild, and also the way in which the variable needs to be handled for FindMinimum.

But yes! It works.

So thank you once again for the support.

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