In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you the relevant knowledge of how to achieve linear programming in Python+PuLP. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
Installation of 1.PuLP library
If you are using Anaconda (which I actually recommend), you need to activate the virtual environment you want to install first, and then type in Prompt
Pip install pulp
If nothing happens, the installation will be finished in a minute.
two。 Brief introduction of Linear programming
You must know what linear programming means if you can click on this article. So let me say it briefly with two examples.
2.1 Linear programming
2.1.1 topic description
If the variable xPowery satisfies the constraint condition:
Find the maximum value of z=3x+y.
2.1.2 basic concepts
First of all, we have to recognize that x and y are changeable in this problem, so we call them decision variables. Three inequalities are called constraints, that is, x and y must satisfy these three inequalities at the same time. If we draw a picture:
The areas that do not meet the constraints are colored by me, so the value of xmemy can only be obtained in the pure white area, which is called the feasible region.
Let's take a look at the final goal: to find the maximum value of z=x+3y.
So z=x+3y is called the objective function, and our job is to find the maximum value of the objective function.
The whole problem is described as follows:
And then what? Don't worry. Let's look at another example.
2.2 Integer programming
2.2.1 topic description
The automobile factory produces three types of cars: small, medium and large. It is known that the demand for steel, working time and profits of each type of car are shown in the table below. It is required that the monthly steel consumption should not exceed 600 tons and the total working time should not exceed 60 000 hours. Try to specify a production plan to maximize the monthly profit of the factory.
Small car medium car large car steel / t1.535 working hours / h280250400 profit / 10,000 yuan 234
2.2.2 ideas for solving problems
First of all, set up three decision variables, and use x1memex2fux3 to denote the number of small cars, medium cars, and large cars produced respectively, but pay attention to meeting the following requirements:
The number of cars can only be an integer.
The number of cars is greater than or equal to 0.
Other constraints are listed directly in the title:
Finally, write the objective function:
Z=2x1+3x2+4x3
To sum up, the whole problem is described as follows:
In addition, we can see that because this problem involves three decision variables, the feasible domain is quite abstract, so we will not draw hhh~ here.
3. Solving process
First, introduce the required pulp tool libraries at the beginning:
Import pulp as pl
This sentence is the introduction of the pulp library and abbreviated to pl, a python library can only be used later after the start of import. In this way, all the functions used in pulp will be written as pl.xxx.
Here are the following steps:
Define the model
Define decision variables
Add constraint condition
Add objective function
Model solving
Print the result
3.1Definition model # Define the modelmodel = pl.LpProblem (name= "My-Model", sense=pl.LpMaximize)
This operation creates a model using pl.LpProblem and assigns a value to the variable model, receiving two parameters:
Name: the name of the model, just pick one
Sense: type of model. Pl.LpMinimize is the minimum value of the objective function, and pl.LpMaximize is the maximum value.
3.2Defining decision variable # Define the decision variablesx = pl.LpVariable (name='x') y = pl.LpVariable (name='y')
You can simply write this if you have fewer variables. This means that two floating-point variables are defined, and the range of values is the entire real field. Note that the variable to the left of the equal sign is the symbol you will use in later calculations, and the parameter name will only be printed when the result is finally printed. In addition, if you have other requirements for variables, you can add the following parameters:
LowBound: the minimum value of the variable (if not written, the default is negative infinity)
UpBound: the maximum value of the variable (default positive infinity)
Cat: the type of variable, including pl.Binary logical variables, pl.Integer integers, pl.Continuous real numbers (default)
If you have so many variables that you have to use 1, 2, 3. To number, you can write something like this:
# Define the decision variablesx = {I: pl.LpVariable (name=f "x {I}", lowBound=0, cat=pl.LpInteger) for i in range (1,9)}
This is to define eight variables at a time and store them in an array-like structure, all of which are positive integers, represented by x [1], x [2],., x [8], respectively, named x1, x2., x8.
Note that the interval indicated by range (left, right) is left closed and right open.
Add constraints # Add constraintsmodel + = (2 * x + 3 * y-6 > = 0, "constrain_1") model + = (x + 3 * y-3 = = 0, "constrain_2")
That's right! As you can see, the first variable in parentheses is your constraint inequality or equality, and the second variable is your custom constraint name (which can be given a meaningful name or omitted, of course).
Due to some comparative mathematical reasons, the greater than sign ">" or the less than sign cannot be used in constraints.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.