article

Sebastián Cañas avatar image
5 Likes"
Sebastián Cañas posted Sarthak_22 commented

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)
· 9
5 |100000

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

Sarthak_22 avatar image Sarthak_22 commented ·

Hello,

How can I set the parameter for these type of problem as you have done in the model?

0 Likes 0 ·
Sebastián Cañas avatar image Sebastián Cañas Sarthak_22 commented ·

Hey @Sarthak_22 ,

Perhaps you are asking about setting different tables as parameter. I usually called them sequencially, so GlobalTable1, GlobalTable2, GlobalTable3, etc. Then set a Option Parameter with the value 1, 2, 3 and so on.

1742997837387.png

Whenever I need to use this parameter, I concatenate the name of the table with the parameter:

1742997892823.png

Let me know if you have further questions!

0 Likes 0 ·
1742997837387.png (26.5 KiB)
1742997892823.png (20.5 KiB)
Sarthak_22 avatar image Sarthak_22 Sebastián Cañas commented ·

Hello actually I am also facing the same error (Binding the Python With flexsim) which is mentioned above in the problem where i have followed all the steps and done all the requirements and also the path variable i have set correctly.


0 Likes 0 ·
Show more comments
Sarthak_22 avatar image Sarthak_22 commented ·

Hello @Sebastián Cañas In the model we get the optimal solution according to the parameter constraint in which all the products are not selected as optimal solution so can we collect the remaining products or the products which are not selected after the optimal solution is collected? If we can collect it, how can we collect it? What changes should i make in the model? Please help me through these as i have tried to understand the task sequences though i am not able to do it. It will also help me in the real-life implementation.
Can you try to do it? Please!
I am attaching the model same as you where i have made some changes according to me

Kp1.fsm

0 Likes 0 ·
kp1.fsm (54.0 KiB)
Sebastián Cañas avatar image Sebastián Cañas Sarthak_22 commented ·

Hey @Sarthak_22 ,

Check this model Knapsack_Problem.fsmthat loads the remaining productos after the optimal combination was selected.

0 Likes 0 ·
Sarthak_22 avatar image Sarthak_22 Sebastián Cañas commented ·

Thank You @Sebastián Cañas By just changing some basic things for the Elements in my model it is working according to my expectations.


1 Like 1 ·

Article

Contributors

sebas.cava contributed to this article