how to store solutions in Monte Carlo simulation

  • 23 March 2022
  • 6 replies

Userlevel 1
Badge +4


I was trying to play around with Monte Carlo simulation in AIMMS. I can save the results from MP.Objective for every run. But, how can I save the solution for every run? I tried to use something like below for every run:

Variable_Result(run, param1, param2) := Variable(param1,param2);

It’s in the procedure “RunMonteCarlo” in the attached file. 

But, it doesn’t work. Any idea?




Best answer by Marcel Hunting 24 March 2022, 14:00

View original

6 replies

Userlevel 5
Badge +4

Hi @zulfan.adiputra . It seems that the solution is always the same, expect for the objective variable and the (superfluous) variable TotalCostMC. You can check this by printing the constraint listing plus the variables values by setting the following Solvers General options:

  • 'Constraint Listing' to 'At every solve' (or 'At first solve')
  • 'Constraint Listing Variable Values' to 'Print variable values'

If you change the definition of UnitTransportCostMC to


then the solution will change some times.

Userlevel 5
Badge +4

Hi @zulfan.adiputra , if Variable_Result is a variable then it will be emptied before the next solve (because you defined AllVariables as the set of variables for the mathematical program LeastCostTransportPlan, which includes Variable_Result). There are ways to work around that but better would be to make Variable_Result a parameter.

Userlevel 1
Badge +4

Hi Marcel @Marcel Hunting

Thanks for this. Indeed, the “Variable_Result” is a parameter. I wrote it wrongly here. So, for every run, I write: Parameter_Result(run, param1, param2) := Variable(param1, param2).

What’s recorded in the Parameter_Result is only the latest solution from the Variable (repeated for all runs). I was hoping to capture the solutions for each solution. 


Userlevel 1
Badge +4

Hi @Marcel Hunting , so basically what you’re saying is that there is nothing wrong in the code. It’s just the solution is the same for all scenarios. Why didn’t I think of that, hahahaha… thanks a lot

Userlevel 4
Badge +5

Hello @zulfan.adiputra 

Not sure if you are working on this locally or on cloud, but if the later, I suggest you read:


About breaking up into smaller jobs, which I imagine you do in a MC simulation.


Userlevel 1
Badge +4

Thanks @luispinto . This particular exercise is local. But, we’ll do real projects in cloud. 



Didn't find what you were looking for? Try searching on our documentation pages:

AIMMS Developer & PRO | AIMMS How-To | AIMMS SC Navigator