---
title: Monte Carlo Optimization for Beginners
description: VP Product Management Eric Kelso introduces users running Monto Carlos simulations to users of the River Logic prescriptive analytics platform.
image: https://download.riverlogic.com/hubfs/Blog_Images/LAB_1.png
---

### Get in Touch

[![logo](https://download.riverlogic.com/hs-fs/hubfs/logo.png?width=3153&height=398&name=logo.png "logo")](http://riverlogic.com)

[![logo-hover](https://download.riverlogic.com/hs-fs/hubfs/logo-hover.png?width=3153&height=398&name=logo-hover.png "logo-hover")](http://riverlogic.com)

- [Solutions](http://www.riverlogic.com/solutions) 
    - [Capacity Planning](https://download.riverlogic.com/solutions/capacity-planning)
    - [Commodity Trading & Logistics](https://download.riverlogic.com/solutions/commodity-trading-and-logistics-optimization)
    - [Product Mix & Customer Profitability](https://download.riverlogic.com/solutions/product-mix-customer-profitability)
    - [Production Planning](https://download.riverlogic.com/solutions/production-planning-and-scheduling)
    - [Sales & Operations Planning](https://download.riverlogic.com/solutions/sales-and-operations-planning)
    - [Sourcing & Procurement](https://download.riverlogic.com/solutions/sourcing-and-procurement-optimization)
    - [Supply Chain & Logistics Optimization](https://download.riverlogic.com/solutions/supply-chain-optimization)
    - [Supply Chain Network Optimization](https://download.riverlogic.com/solutions/supply-chain-network-optimization)
    - [Trade Promotion Optimization](https://download.riverlogic.com/solutions/trade-promotion-optimization)
- [Industries](https://download.riverlogic.com/industries) 
    - [Building Products](https://download.riverlogic.com/industries/building-products)
    - [Chemicals](https://download.riverlogic.com/industries/chemicals)
    - [Consumer Packaged Goods](https://download.riverlogic.com/industries/consumer-packaged-goods)
    - [Food and Beverage](https://download.riverlogic.com/industries/food-beverage)
    - [Manufacturing](https://download.riverlogic.com/industries/manufacturing)
    - [Mining & Metals](https://download.riverlogic.com/industries/mining-and-metals)
    - [Oil & Gas](https://download.riverlogic.com/industries/oil-and-gas)
    - [Pharmaceuticals](https://download.riverlogic.com/industries/pharmaceuticals)
    - [Pulp & Paper](https://download.riverlogic.com/industries/pulp-and-paper)
    - [Utilities](https://download.riverlogic.com/industries/utilities)
- [Technology](https://download.riverlogic.com/technology/prescriptive-analytics-platform) 
    - [Our Platform](https://download.riverlogic.com/technology/prescriptive-analytics-platform)
    - [Prescriptive Analytics](https://download.riverlogic.com/technology/prescriptive-analytics)
- [Resources](https://download.riverlogic.com/resources) 
    - [Blog](https://download.riverlogic.com/blog)
    - [News](https://download.riverlogic.com/news-2)
    - [Testimonials](https://download.riverlogic.com/testimonials)
- [Partners](https://download.riverlogic.com/partner-program) 
    - [Our Partners](https://download.riverlogic.com/our-partners)
- [About Us](https://download.riverlogic.com/about-us) 
    - [Contact Us](https://download.riverlogic.com/contact-us)
    - [Our Board](https://download.riverlogic.com/board-of-directors)
    - [Our Leadership](https://download.riverlogic.com/leadership)
    - [Careers](https://download.riverlogic.com/careers-current)
- Get in Touch

Get in Touch

[![Get in Touch](https://hubspot-no-cache-na2-prod.s3.amazonaws.com/cta/default/484375/8a2efec4-88ad-40a3-bc49-91f4a5e57e38.png)](https://hubspot-cta-redirect-na2-prod.s3.amazonaws.com/cta/redirect/484375/8a2efec4-88ad-40a3-bc49-91f4a5e57e38)

Share This <https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fdownload.riverlogic.com%2Fblog%2Fmonte-carlo-optimization-for-beginners> <https://twitter.com/intent/tweet?url=https%3A%2F%2Fdownload.riverlogic.com%2Fblog%2Fmonte-carlo-optimization-for-beginners&text=Monte+Carlo+Optimization+for+Beginners> <https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fdownload.riverlogic.com%2Fblog%2Fmonte-carlo-optimization-for-beginners&title=Monte+Carlo+Optimization+for+Beginners&summary=VP+Product+Management+Eric+Kelso+introduces+users+running+Monto+Carlos+simulations+to+users+of+the+River+Logic+prescriptive+analytics+platform.+> [mailto:?subject=Monte%20Carlo%20Optimization%20for%20Beginners&body=https%3A%2F%2Fdownload.riverlogic.com%2Fblog%2Fmonte-carlo-optimization-for-beginners](mailto:?subject=Monte%20Carlo%20Optimization%20for%20Beginners&body=https%3A%2F%2Fdownload.riverlogic.com%2Fblog%2Fmonte-carlo-optimization-for-beginners)

# Monte Carlo Optimization for Beginners

The Monte Carlo method is a well-known simulation technique that uses statistical random sampling to solve mathematical problems. In use for about 85 years, many variants exist across a wide range of disciplines. If you're not familiar with these, I suggest reading this [Wiki page.](https://en.wikipedia.org/wiki/Monte_Carlo_method)

This discussion is limited to the particular Monte Carlo method of randomly generating stochastic input data based on a probabilistic distribution function with parameters; solving a mathematical model using optimization algorithms; and then repeating a sequence of process steps ‘n’ times, as desired. This might sound complicated, but trust me it’s not too difficult.

For context, the following example uses River Logic’s [prescriptive analytics platform](https://www.riverlogic.com/technology/prescriptive-planning-platform/), which includes two interoperable components: Enterprise Optimizer and EO+.

Enterprise Optimizer is the underlying visual modeling and optimization component. EO+ is the cloud-based component with which business users interact to manage data, generate and run scenarios, and collaboratively analyze those scenarios with Power BI embedded.

Read more about EO+ in this blog post: *[Next Generation Scenario Analysis in River Logic's EO+](https://download.riverlogic.com/next-generation-scenario-analysis-river-logics-eo)*

The discussion below takes a deep dive into Enterprise Optimizer and it’s sister component, EO Server. Used by analysts and consultants, the Visual Modeler component allows user to create constraint-based optimization models with a drag-and-drop interface. At the conclusion, an example on how this approach can be embedded within a commercial application is provided.

### Example: Nutmeg Candy Company

This Monte Carlo example is based on a simple EO model created many years ago. The fictional business, **Nutmeg Candy Company, buys ingredients and makes and sells two products, nutcrunch and megacrunch.** EO users who have taken the online training know this model well. Here is the process diagram as defined in the EO model.

Process Diagram of Nutmeg Candy Company

![LAB_1.png](https://download.riverlogic.com/hs-fs/hubfs/Blog_Images/LAB_1.png?width=715&height=384&name=LAB_1.png "LAB_1.png")

Inside the Sales object, product prices are set to stochastically generate new random prices prior to each time the model is solved.

![LAB_2.png](https://download.riverlogic.com/hs-fs/hubfs/Blog_Images/LAB_2.png?width=715&height=217&name=LAB_2.png "LAB_2.png")Both prices use a Normal distribution; a mean equal to the base input price (40 and 30, respectively); and a standard deviation of 7.5 for nutcrunch and 5.0 for megacrunch.

Additional data structures were added to the model schema to support the Monte Carlo process. A user-defined variable, \_scenarioID, was created to store the current scenario identifier; and two user-defined tables, SolutionResults and SalesActivityMTP, were created to store each scenario solution’s results. And, a set of queries were created to manage deleting old data and to populate these tables with new data after each solve. More on this below.

![LAB_3.png](https://download.riverlogic.com/hs-fs/hubfs/Blog_Images/LAB_3.png?width=715&height=715&name=LAB_3.png "LAB_3.png")

The model was then saved with file name Sweet\_stochastic.cor.

[![Optimization or Simulation - Inline](https://hubspot-no-cache-na2-prod.s3.amazonaws.com/cta/default/484375/43605e26-328e-4d61-8471-766bbb911844.png)](https://hubspot-cta-redirect-na2-prod.s3.amazonaws.com/cta/redirect/484375/43605e26-328e-4d61-8471-766bbb911844)

### Enterprise Optimizer Server – Overview

In a previous blog, [Why Optimization Modeling Should Move Beyond Using Scripts,](https://download.riverlogic.com/why-optimization-modeling-should-move-beyond-using-scripts) I briefly introduced why Enterprise Optimizer Server, River Logic’s code-free deployment application for [Enterprise Optimizer®](http://www.riverlogic.com/technology/enterprise-optimizer), is a significant advancement over the ‘traditional’ algebraic modeling tools reliance on scripting languages. The following example is intended to prove why this is the case. **Although it’s possible that the same example could be recreated in an algebraic modeling software program, it would take vastly more work to code, debug and deploy.**

### For background, EO Server:

### Enterprise Optimizer Server – Controlled Loops

**Automating the Monte Carlo process requires two separate but related jobs, one to act as a control loop with conditional logic needed for branching, and one to act as a master job that contains the steps that each solve process will follow.** We begin with the first EO Server job, Sweet\_stochastic\_control, intended to control the overall process. It is important to remember that after each task is executed, the job flow can branch to any other task anywhere in the job. Generally, there are two branching choices — if the task was successful upon execution, or not successful (a small number have more than 2 choices or no choices).

Therefore, jobs that appear linear when viewing the task list in the EOSMS can instead have nested loops defined. As you will see, loops are essential **when the same set of tasks must be executed repeatedly, as is the case when doing Monte Carlo simulation.** They can be as simple as 2 or 3 tasks, or include as many as 50 or more tasks.

Reasons why a loop might be needed in a job include (highlighted text is included in this example):

### Enterprise Optimizer Server – Conditional Branching

At any point in the loop, but typically at the end, the condition of one or more variables can be considered. Depending on the result, the job flow is then branched upward to an earlier “beginning” task (i.e., repeat the loop) or continues to a later task. It might be helpful to think of this logic as similar to “IF…THEN…GOTO” logic in a traditional programming language, except no coding is required. And, there is no maximum number of conditions that can be evaluated, including (highlighted text in this example):

### Monte Carlo Example – Defining the Control Job

Below is a screenshot of the control job’s task list, as viewed within the EO Server Management Studio user interface. Defining this job was a process of creating new tasks by dragging and dropping the 15 task types from the Toolbox in the upper-right-corner to the middle window, and then adding task parameters and other information in the window on the bottom-right-corner. No code writing was needed to successfully create this job!

![LAB_4.png](https://download.riverlogic.com/hs-fs/hubfs/Blog_Images/LAB_4.png?width=715&height=408&name=LAB_4.png "LAB_4.png")

Some brief task descriptions (by task number):

#1 – SetJobVariable task defines variable **scenarioID** and initializes it to 1.

#2 – SetJobVariable task defines variable **maxSolves** and initializes it to 1000.

#3 – OpenModel task opens EO model ****Sweet\_stochastic.cor****.

#4 and #5 – ExecuteQuery tasks delete old data from user-defined tables in the EO model.

#6 – After deleting old data, model is saved using the SaveAsFile task.

#7 – WriteMessage task is used to write custom message at the beginning of each loop.

#8 – Delay task is set to 5 seconds to make certain that the prior scenarioID’s solve is complete prior to the next solve starting. Note, the decision to execute cloned jobs in sequence was strictly user choice, as EO Server allows up to 999 simultaneous jobs using the same EOS agent.

#9 – DeleteJob task deletes old job with the same **scenarioID**, if it exists.

#10 – CloneJob task clones the master job and renames the cloned job to include the scenarioID. More on this below.

#11 – All job variables are passed from the control job to the cloned job. In this example, only **scenarioID** is needed in the cloned job.

#12 – The cloned job is submitted for execution by the EOS agent.

#13 – The Counter task increments the scenarioID value by +1.

#14 – The ConditionalJump task evaluates the **scenarioID** value and determines if it is less than or equal to the **maxSolves** If true, the process goes to or “jumps” to task #7, WriteMessage, and the next iteration begins. If false, it means that the maximum number of solves has been reached and the process goes to the next task #15, Exit, and the job will finish.

### Monte Carlo Example – Defining the Master Job (To be Cloned During Each Loop)

The master job, Sweet\_stochastic\_master, to be cloned during each loop is displayed below. It was created in the same manner as job #1.![LAB_5.png](https://download.riverlogic.com/hs-fs/hubfs/Blog_Images/LAB_5.png?width=715&height=297&name=LAB_5.png "LAB_5.png")

Some brief task descriptions (by task number):

#1 – OpenModel task opens EO model **Sweet\_stochastic.cor**. This is the same model used in the control job, except it will now be open during each scenario. Note, the name could have been passed as a job variable from the control job.

#2 – WriteMessage task writes the **scenarioID** in a custom message. This can be helpful from separating one solve’s messages from another.

#3 – SetUserDefinedVariable stores the **scenarioID** value to the user-defined object variable, \_scenarioID, inside the EO model. This value is referenced by the queries to properly store the solution results after each solve.

#4 – The EO model is solved using the Solve step. Note, EO Server has many task types which allow for setting the solver name (“CPLEX”, “MIO”) and a list of solver parameters.

#5 and #6 – ExecuteQuery tasks copy **the \_scenarioID** plus model solution results from pre-defined EO tables to the user-defined tables.

#7 – SaveAsFile task saves the EO model after each solve. Notice that the same model file is being saved during each **scenarioID**. This parameter could be easily modified to change the model file name to something like **Sweet\_stochastic\_{$scenario$}.cor**, which would have created and saved 1,000 different model files – one for each scenarioID.

#8 – Exit task automatically closes the job.

### Running the Monte Carlo Simulation

For this example, only the control job **Sweet\_stochastic\_control** was manually submitted in the EOSMS – and only just 1 time!

The job was immediately picked up by an available EOS agent and processed, which took about 95 minutes to execute 8,528 tasks or approximately 5.7 seconds per job execution. (Most time for each job was to generate and solve the model.)

### Control Job messages

As displayed below, the control job resulted in over 16,000 messages generated and stored—most related to task execution—for the 1,000 times the EO model was solved.

![LAB_6.png](https://download.riverlogic.com/hs-fs/hubfs/Blog_Images/LAB_6.png?width=715&height=416&name=LAB_6.png "LAB_6.png")

### Master Job messages

The master job stored all the messages generated during each cloned job’s execution. Over 49,000 total messages were generated, including all the solve-related messages that would normally be displayed in the EO model’s Run window during a manual solve process.

### ![LAB_7.png](https://download.riverlogic.com/hs-fs/hubfs/Blog_Images/LAB_7.png?width=715&height=371&name=LAB_7.png "LAB_7.png")

### Analyzing the Results

Once the EO Server control job is complete, the Monte Carlo simulation is done. Results can be analyzed either within the EO model, such as the EO dashboard displayed below, or data can be easily exported from EO to any relational database, MS Access, MS Excel, flat file or other format.

### ![LAB_8.png](https://download.riverlogic.com/hs-fs/hubfs/Blog_Images/LAB_8.png?width=715&height=395&name=LAB_8.png "LAB_8.png")

### Embedding into Applications

Importantly, both Enterprise Optimizer® and EO Server were designed to be embedded as components into other applications. The EO Server jobs described above can run strictly in memory. All job variables can be passed to the job from external applications and actions, such as submitting a job, retrieving messages, displaying results, etc. can be easily accessed the extension API, which can be called from Microsoft development tools (C++, .NET), web services, or other methods.

River Logic has a long history of working with its consulting partners, VARs, system integrators, software companies, and other companies to successfully incorporate these types of capabilities into their applications. A very good example is the [Trade Promotion Optimization (TPO)](https://download.riverlogic.com/news-blog/creating-industry-leading-trade-promotion-optimization-solution) solution developed with partner AFS Technologies.

## Concluding Remarks

This example highlights these important concepts:

Finally, one of the key benefits with the River Logic platform, including the Visual Modeler as detailed above, is that it’s built on Microsoft Azure, adding scale, elasticity of services, security, and computer power. It also offers cutting-edge, easy-to-deploy capabilities like machine learning, predictive modeling, IoT, and more.

 

You May Also Enjoy Reading

<https://download.riverlogic.com/blog/designing-a-supply-chain-network-5-steps-for-optimizing-profit>

[Designing a Supply Chain Network: 5 Steps for Optimizing Profit](https://download.riverlogic.com/blog/designing-a-supply-chain-network-5-steps-for-optimizing-profit)…[(Read More)](https://download.riverlogic.com/blog/designing-a-supply-chain-network-5-steps-for-optimizing-profit)

<https://download.riverlogic.com/blog/what-is-the-difference-between-optimization-modeling-and-simulation>

[What Is the Difference Between Optimization Modeling and Simulation?](https://download.riverlogic.com/blog/what-is-the-difference-between-optimization-modeling-and-simulation)…[(Read More)](https://download.riverlogic.com/blog/what-is-the-difference-between-optimization-modeling-and-simulation)

#### About River Logic

We’re on a mission to ensure companies across the globe drive maximum value with every decision they make, no matter how complex.

[![Learn More](https://hubspot-no-cache-na2-prod.s3.amazonaws.com/cta/default/484375/a2bde0d6-69fe-4923-8616-00e630d07900.png)](https://hubspot-cta-redirect-na2-prod.s3.amazonaws.com/cta/redirect/484375/a2bde0d6-69fe-4923-8616-00e630d07900)

#### **Search**

#### Explore Our Solutions

We offer a suite of supply chain planning, network optimization, order allocation, and general planning solutions that are purpose-built for business users rather than data scientists.

[![Explore Now](https://hubspot-no-cache-na2-prod.s3.amazonaws.com/cta/default/484375/244b60cb-8886-4e40-a50b-a0ab86c40833.png)](https://hubspot-cta-redirect-na2-prod.s3.amazonaws.com/cta/redirect/484375/244b60cb-8886-4e40-a50b-a0ab86c40833)

#### Ready to Chat with Someone?

We’re ready to help! Just click the button below, and grab a time slot that works for your schedule.

[![Schedule a call now](https://hubspot-no-cache-na2-prod.s3.amazonaws.com/cta/default/484375/798117a2-c8b3-49c5-aca3-cfe9c69c3b12.png)](https://hubspot-cta-redirect-na2-prod.s3.amazonaws.com/cta/redirect/484375/798117a2-c8b3-49c5-aca3-cfe9c69c3b12)

#### Solutions

- #### [Solutions](https://www.riverlogic.com/solutions)
- [Capacity Planning](https://www.riverlogic.com/solutions/capacity-planning)
- [Commodity Trading & Logistics](https://www.riverlogic.com/solutions/commodity-trading-and-logistics-optimization)
- [Product Mix & Customer Profitability](https://www.riverlogic.com/solutions/product-mix-customer-profitability)
- [Production Planning](https://www.riverlogic.com/solutions/production-planning-and-scheduling)
- [Sales & Operations Planning](https://www.riverlogic.com/solutions/sales-and-operations-planning)
- [Supply Chain & Logistics Optimization](https://www.riverlogic.com/solutions/supply-chain-logistics-optimization)
- [Supply Chain Network Optimization](https://www.riverlogic.com/solutions/supply-chain-network-optimization)
- [Trade Promotion Optimization](https://www.riverlogic.com/solutions/trade-promotion-optimization)

- #### [Technology](https://www.riverlogic.com/technology/prescriptive-analytics-platform)
- [Our Platform](https://www.riverlogic.com/technology/prescriptive-analytics-platform)
- [Prescriptive Analytics](https://www.riverlogic.com/technology/prescriptive-analytics)

- #### [Industries](https://www.riverlogic.com/industries)
- [Building Products](https://www.riverlogic.com/industries/building-products)
- [Chemicals](https://www.riverlogic.com/industries/chemicals)
- [Consumer Packaged Goods](https://www.riverlogic.com/industries/consumer-packaged-goods)
- [Manufacturing](https://www.riverlogic.com/industries/manufacturing)
- [Mining & Metals](https://www.riverlogic.com/industries/mining-and-metals)
- [Oil & Gas](https://www.riverlogic.com/industries/oil-and-gas)
- [Pulp & Paper](https://www.riverlogic.com/industries/pulp-and-paper)
- [Utilities](https://www.riverlogic.com/industries/utilities)

- #### [About Us](https://www.riverlogic.com/about-us)
- [Our Board](https://www.riverlogic.com/board-of-directors)
- [Our Leadership](https://www.riverlogic.com/leadership)
- [Contact Us](https://www.riverlogic.com/contact-us)

- #### [Partners](https://www.riverlogic.com/partner-program)
- [Our Partners](https://www.riverlogic.com/our-partners)

- #### [Resources](https://www.riverlogic.com/resources)
- [Blog](https://blog.riverlogic.com/)
- [Content Library](https://www.riverlogic.com/resources)
- [News](https://www.riverlogic.com/news)
- [Testimonials](https://www.riverlogic.com/testimonials)
- [Careers](https://download.riverlogic.com/careers-current)

[Facebook](https://www.facebook.com/River-Logic-480102222138357/)

[LinkedIn](https://www.linkedin.com/company/river-logic/)

[Youtube](https://www.youtube.com/channel/UCMJGbIQn9AX2kGojeRHhu-w/)

- [Disclaimer](https://www.riverlogic.com/disclaimer)
- [Support](mailto:support@riverlogic.com)
- [Privacy Policy](https://www.riverlogic.com/privacy-policy)
- [Sitemap](https://www.riverlogic.com/site-map)
- [Terms & Conditions](https://www.riverlogic.com/terms-and-conditions)

© 2022 [River Logic, Inc.](https://www.riverlogic.com) All rights reserved.

![](https://googleads.g.doubleclick.net/pagead/viewthroughconversion/985826217/?value=0&guid=ON&script=0) 

![](https://dc.ads.linkedin.com/collect/?pid=173242&fmt=gif) [![Supply Chain Brief](https://widget.aggregage.com/images/supplychainbrief/badge-clear.gif "Supply Chain Brief")](https://www.supplychainbrief.com)

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Eric Kelso",
    "url" : "https://download.riverlogic.com/blog/author/eric-kelso"
  },
  "dateModified" : "2020-08-18T14:29:50.436Z",
  "datePublished" : "2015-12-16T12:00:00.000Z",
  "headline" : "Monte Carlo Optimization for Beginners",
  "image" : [ "https://download.riverlogic.com/hubfs/Blog_Images/LAB_1.png" ],
  "mainEntityOfPage" : {
    "@id" : "https://download.riverlogic.com/blog/monte-carlo-optimization-for-beginners",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://download.riverlogic.com/hubfs/4.28_Logos/logo_mobile-1.png"
    },
    "name" : "River Logic"
  }
}
```