Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
Define "not working". What is happening, and how does this differ from what you expect? – rcollyer Feb 26 '14 at 18:17
If you define as f[a1_?NumericQ, a2_?NumericQ, a3_?NumericQ] :=... that will effectively disable any symbolic preprocessing that might otherwise be happening. – Daniel Lichtblau Feb 26 '14 at 18:33
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. – brama Feb 26 '14 at 18:41
There are only 27 sets of integers satisfying your constraints. Instead of using NMinimize just evaluate for all the cases and select the minimum – george2079 Feb 27 '14 at 13:28
@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 – brama Feb 27 '14 at 13:41
up vote 2 down vote accepted

I found the solution on another website "" 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

share|improve this answer
This isn't quite right. Please check @Szabolc's comment under your other (identical) answer – Dr. belisarius Mar 3 '14 at 20:53
@ belisarius, I agree. I made an edit on the answer. Thanks. – brama Mar 3 '14 at 21:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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