article

Sebastián Cañas avatar image
3 Likes"
Sebastián Cañas posted Sebastián Cañas edited

Integrating FlexSim with Python to solve the Knapsack Problem

This model is a proof-of-concept example demonstrating the integration of FlexSim with Python's Pyomo package to solve the Knapsack Problem. The model simulates a loading process of a logistic company. The truck has a weight capacity of 200 kg.

knapsack-problem.gif

The scenario includes 15 products, each with a specific weight and value. The product details are as follows:

productsdetail.png

The objective is to determine which products to load onto the truck to maximize the total value of goods while ensuring the total weight does not exceed 200 kg.

The ProductCreation Process Flow creates the products in a Queue. The General Process Flow has a Custom Code that creates a couple of Maps to store the products weights and values. It sets the capacity variable from the Parameters Table. These three parameters can be passed to python. Then it evaluates KnapsackProblem label on the Process Flow, passing those parameters in. The label is configured to connect to the KnapsackProblem function defined in the KnapsackProblem.py module. This function formulates the Knapsack Problem with Pyomo, solves the program, and then returns the optimal collection of products to be load onto the truck.

Since the Decision Variables are binary, once the products are resolved, the values are stored in a Global Table, where 1 means that the product was selected.

optimalcombination.png

A Combiner uses this table to set the Component List. A forklift load the products and once completed, the truck leaves. When it enters the Sink a message is displayed showing the total weight and value loaded.

Model Parameters

There are two parameters that can be changed in this model. One is the Truck Capacity, which is the constraint of this problem. The value ranges from 100 to 300.

There are three Global Tables in this model that store a different set of Weights and Values for each products. The table selected for the problem can also be changed using the GUI.

gui.png

Potential additions to this model could use priority for the products or include multiple trucks or constraints such as volume.

Requirements to run the model

In order to run this model, you need python properly configured, including:

  • Install one of these python versions: 3.9, 3.10, 3.11
  • Install pyomo and highspy packages: python -m pip install pyomo highspy
  • Make sure the python directory is part of your PATH environment variable.
  • Configure your Global Preferences (the Code tab) to use the associated python version.

This model was built in FlexSim 24.0

Knapsack_Problem.zip

Troubleshooting

If you are getting this error:

exception: Code Binding Error: could not bind to function
Node: /Tools/ProcessFlow/ProcessFlow>labels/KnapsackProblem
Binding string: /**external python: */ /**/"KnapsackProblem"/**/
/** \nfunction name:*/ /**/"KnapsackProblem"/**/
Windows Error Code : 126

Check this post

pythonlogisticsknapsackproblem
productsdetail.png (29.5 KiB)
gui.png (21.1 KiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Article

Contributors

sebas.cava contributed to this article