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.
The scenario includes 15 products, each with a specific weight and value. The product details are as follows:
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.
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.
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:
This model was built in FlexSim 24.0
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
Hello,
How can I set the parameter for these type of problem as you have done in the model?
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.
Whenever I need to use this parameter, I concatenate the name of the table with the parameter:
Let me know if you have further questions!
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.
Hello, this error is also resolved i just changed the pyomo version for the python 3.11.0
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
Hey @Sarthak_22 ,
Check this model Knapsack_Problem.fsmthat loads the remaining productos after the optimal combination was selected.
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.
Hi
I had difficulties with Python installation and path but after solving those I'm getting the following error: [WinError 6] The handle is invalid
It comes to FlexSim System Concole. Any idea how to fix this?
FlexSim version: 24.0.8
Python 3.11
Hey @tomi_kosunenACQLV ,
This seems like a Windows-related issue. I have no idea why it pops up, however, I google the error and found a lot of info about it. You should give it a try and read what's on the web.
Hey @sebastian_canas9DYX7 and thanks for your answer. After discussing with AI I got it running. I don't know the original source of the problem but now it works 🙂
Here's the AI answer to the problem source. It seems that I cannot attach py code here but running the function below before line "