I am using Nminimize for simulation based optimization. I define the objective function (simulation with a variable "a") as a module to be used in the minimization. What I have found is if I do not print the function value (f[a]) using the EvaluationMonitor, NMinimize outputs 1000s of iterations of possible values for "a" extremely quickly without running the corresponding simulation run (because if it runs simulations, it will need few seconds for each simulation run and can not do 1000s of simulations in no time). However, when I include "f[a]" in the evaluation monitor, NMinimize runs the simulation for each possible value of "a" it outputs, but is disconnected to the optimization process. This is evident because NMinimize does not converge even after 1000s of iterations, when there are only 10 possible values "a" can take and I know the minimum occurs at "a"=10. Will someone help me understand what I am missing?

demand[n_,k_]:=Min[k Vf,n capacity];
supply[n_,k_]:=Min[(n Kj-k) w,n capacity];
n=Round[Flen/dx];m=Round[SimTime/dt];p=Round[Rlen/dx];RMLocation=Round[(2/3) p];
\[Alpha][a1_]:=1800.;\[Beta][a2_]:=0.1;L=1.;Flen=4.;Rlen=3.;delta = 1.;SimTime=15./60.;dt=6./3600.;
Clear[j];j=0;RM[x_,t_]:=100 a;k=k0;
For[i=2,i<n,i++,kr[[i,1]]=\[Alpha][i dx] delta/Vf];
  \[Phi]=\[Gamma][[i]] demand[1,kr[[i,p]]]/delta;
  Qr=(\[Phi]-\[Beta][i dx] FQin) dx;
   MR=If[ir==RMLocation+1,RM[i dx,j dt],capacity];
   MR=If[ir==RMLocation,RM[i dx,j dt],capacity];
   RQout=Min[MR,If[ir<p,flo[1,kr0[[ir]],kr0[[ir+1]]],\[Phi] delta]];
  kr[[i,1]]=If[j<=m,\[Alpha][i dx] delta/Vf,0]];
NtwrkTT dt]
NMinimize[{f[a],3<=a<=12&&Element[a,Integers]},a,Method->"SimulatedAnnealing",EvaluationMonitor:>Print["a = ",a]]

edit: I edited this post extensively to make it clear. Apologize for any confusion.

  • $\begingroup$ Define "not working". What is happening, and how does this differ from what you expect? $\endgroup$
    – rcollyer
    Commented Feb 26, 2014 at 18:17
  • $\begingroup$ If you define as f[a1_?NumericQ, a2_?NumericQ, a3_?NumericQ] :=... that will effectively disable any symbolic preprocessing that might otherwise be happening. $\endgroup$ Commented Feb 26, 2014 at 18:33
  • $\begingroup$ rcollyer, I edited the question to define "not working". Daniel Lichtblau, I am not completely sure but I do not think ?NumericQ will fix anything and moreover ma not be applicable in this problem. $\endgroup$
    – brama
    Commented Feb 26, 2014 at 18:41
  • $\begingroup$ There are only 27 sets of integers satisfying your constraints. Instead of using NMinimize just evaluate for all the cases and select the minimum $\endgroup$
    – george2079
    Commented Feb 27, 2014 at 13:28
  • $\begingroup$ @george2079, this is a simplified version of the code, the original solution space has over 65 million combinations. So I need to use the minimizer $\endgroup$
    – brama
    Commented Feb 27, 2014 at 13:41

1 Answer 1


I found the solution on another website "http://eternaldisturbanceincosmos.wordpress.com/2011/04/27/nminimize-in-mathematica-could-drive-you-insane/" which says "It turns out that NMinimize does not hold its arguments. This means that as the list of arguments is read from left to right, each argument is evaluated and replaced by the result of the evaluation". So I used Hold[] in the NMinimize function that fixed the problem.

Edit: Please see the responses/comments on Simulated Annealing Convergence

  • $\begingroup$ This isn't quite right. Please check @Szabolc's comment under your other (identical) answer mathematica.stackexchange.com/a/43310/193 $\endgroup$ Commented Mar 3, 2014 at 20:53
  • $\begingroup$ @ belisarius, I agree. I made an edit on the answer. Thanks. $\endgroup$
    – brama
    Commented Mar 3, 2014 at 21:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.