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, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1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, 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
NMinimize
takes aMethod
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$N[]
I get a result, or rather theNMinimize::incst
error you specify, in under 90 seconds, not 10 minutes. $\endgroup$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$N[]
, e.g. useN[H]
instead of justH
. $\endgroup$