This is a algorithm solves system of inequalities to find out solutions of the system of linear inequalities.
The algorithm follows two basic rules as follows:
The first is the "additional side" property of two inequalities. When adding both sides of two inequalities in the same direction, the resulting inequality is always correct and does not change direction.
Property of multiplying a positive number by an inequality. When multiplying both sides of an inequality by a positive integer, the inequality is always correct and does not change direction.
With:
And
To download the tool, select Code > Zip Download
on Github page or type in Command Prompt on Windows or Terminal on other operating systems as follows:
git clone https://github.com/Truongphi20/inequaltion.git
Move the terminal's working directory to the downloaded "inequation" directory (cd inequation
).
python .\sinequal.py -h
Output:
usage: sinequal.py [-h] [-f FILE_INPUT] [-v]
optional arguments:
-h, --help show this help message and exit
-f FILE_INPUT, --file_input FILE_INPUT
-v, --version show version
Look at the file "example1.csv", the structure of the input file has the following structure:
2*a+b<=6
a+b>=0
a-b>=1
{'a':0,'b':0}
The first components are the inequalities, which are the first 3 lines in the example. The inequalities are separated by line breaks. The second element is the variable declaration and its extrema type, separated from the first element by a blank line. There are three types of extrema:
-
$0$ is none extrema -
$1$ is maxima -
$-1$ is minima
Note: If there are many extrema conditions, the algorithm will solve the extrema in order from right to left in the variable declaration.
Suppose we have to find the values of
To solve the system of inequalities in example 1 above, we run the following command line:
python .\sinequal.py -f .\example1.csv
Output:
(['a', 'b'], [[6, -6], [5, -5], [4, -4], [5, -4], [3, -3], [4, -3], [2, -2], [3, -2], [4, -2], [1, -1], [2, -1], [3, -1], [1, 0], [2, 0], [3, 0], [2, 1]])
As a result, the list has two components: the names of the variables and the the list of solutions of those corresponding variables.
So the solutions
Also with the above system of inequalities, if we want to find a solution such that
We consider P as a variable to be solved and to find its maximum value. So the variable declaration component in input file will be as follows:
{'a':0,'b':0,'P':1}
Similarly, to run example 2 we run the following command:
python .\sinequal.py -f .\example2.csv
Output:
(['a', 'b', 'P'], [[3, 0, 3], [2, 1, 3]])
So the maximum value of
As we can also see in Example 1, when adding the extrema condition P, only the above two solutions are satisfied.
Not stopping there, if we add one more extrema condition that
Similarly, we consider M to be an extrema variable to be solved, declare the variable, and set it to be the minima.
{'a':0,'b':0,'M':-1,'P':1}
Run the following command to solve:
.\sinequal.py -f .\example3.csv
Output:
(['a', 'b', 'M', 'P'], [[2, 1, 1, 3]])
From example 2 it is also easy to predict that the solution of the system of inequalities of example 3 is
So the meaning of the solution process in example 3 is that first find the solutions that satisfy the condition in example 2,
Therefore, it is necessary to pay attention to the order of extrema solutions when declaring variables. A different order will lead to different results