0
$\begingroup$

I got a little performance problem with the function NMinimize (it takes mathematica approximately 10 minutes to compute the results).

Here i s my sample code:

     (*Define variables*)
c = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
Subscript[f, k] = {1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1};
L = {{1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 1, 1, 1, 
    0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 
    0, 0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 
    0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
    1, 0}, {0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
    1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 
    0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 
    0, 1, 0, 0, 1, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 
    0, 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0}, {0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 1, 1, 1}};



y = Table[Unique["y"], {10}];
x = Table[Unique["x"], {38}];
variables = Join[y, x];

(*Define constraints*)
constraints1 = N[L].x == y;
constraints2 = N[H].x == N[Subscript[f, k]];
constraints3 = x >= 0;
constraints4 = y >= 0;

Print[constraints1]; Print[constraints2]; Print[constraints3]; \
Print[constraints4];

fun[y_, c_] := 
  Piecewise[{{y, 0 <= y/c <= 1/3}, {3 y - (2/3) c, 
     1/3 <= y/c <= 2/3}, {10 y - (16/3) c, 
     2/3 <= y/c <= 9/10}, {70 y - (178/3) c, 
     9/10 <= y/c <= 1}, {70 y - (178/3) c, 
     9/10 <= y/c <= 1}, {500 y - (1468/3) c, 
     1 <= y/c <= 11/10}, {5000 y - (16318/3) c, 
     11/10 <= y/c <= \[Infinity]}}];
NMinimize[{Sum[N[fun[y[[i]], N[c[[i]]]]/N[c[[i]]], 8], {i, 10}], 
   constraints1, constraints2, constraints3, constraints4}, variables,
   Method -> {"RandomSearch", 
    "InitialPoints" -> 
     Flatten[Table[{l, m}, {l, -2, 5, 0.00001}, {m, -2, 5, 0.00001}], 
      1]}] // AbsoluteTiming

Are there maybe some options to increase the execution speed using "PrecisionGoal", "Accuracy Goal" or "WorkingPrecision". I was also thinking about using floating point numbers. In general, the solution to the problem is obtained with very high accuracy, which is not necessary in my case. So it would be nice if I could scarify some accuracy to get a better computation time.

Further, I know that the result will show the following error message, which is because of my strict constraints. What I don't understand is, why do I still get some results at the end if it violates these constraints? Is Mathematica then automatically switching to another approximation scheme?

NMinimize::incst: NMinimize was unable to generate any initial points satisfying the inequality constraints 

Best,

Julian

$\endgroup$
8
  • $\begingroup$ NMinimize takes a Method option. in which you can specify a set of initial points. Take a look here for the different options you can use. reference.wolfram.com/language/tutorial/…. For example, Method -> {"RandomSearch", "InitialPoints" -> ... } $\endgroup$ Oct 30, 2014 at 8:42
  • $\begingroup$ I also suggest trying it with reals rather than integers. By converting all your variables using N[] I get a result, or rather the NMinimize::incst error you specify, in under 90 seconds, not 10 minutes. $\endgroup$ Oct 30, 2014 at 8:46
  • $\begingroup$ how would you then set the InitialPoints? Could you maybe also post the changes that you have made so that I can test them on my machine? $\endgroup$
    – Julian
    Oct 30, 2014 at 9:04
  • $\begingroup$ Scroll down the tutorial to the Random Search method, and you'll find an example NMinimize[f, {{x, -50, 50}, {y, -50, 50}}, Method -> {"RandomSearch", "InitialPoints" -> Flatten[Table[{i, j}, {i, -45, 45, 5}, {j, -45, 45, 5}], 1]}] $\endgroup$ Oct 30, 2014 at 9:44
  • $\begingroup$ In terms of making it all numerics, just enclose all your variables in N[], e.g. use N[H] instead of just H. $\endgroup$ Oct 30, 2014 at 9:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.