I am using Scipy to solve the following linear programming problem. However, I have to set the precision for the decision variable r to a maximum of double-precision floating-point numbers. Can someone tell me how I can set the precision, please? I checked the linprog documentation but I don't see such an option.
The implemented code:
# Objective function
def objective():
return [time[e] for e in edge]
# setting the bounds for the decision variables
def bounds():
return [(0, V[e[0]]) for e in edge]
# generating the constraints
def constraints():
b = []
A = []
const = []
const_2 = []
for i in region:
for e in edge:
if e[0] != e[1] and (e[0]==i or e[1]==i):
if (e[0]==i):
const.append(1)
const_2.append(1)
else:
const.append(-1)
const_2.append(0)
else:
const.append(0)
const_2.append(0)
# const 1 \sum (r_{ij} - r_{ji}) \leq V_i - D_i
A.append(const)
b.append(V[i] - D[i])
# const 2 \sum r_{ij} <= V_i
A.append(const_2)
b.append(V[i])
const = []
const_2 = []
return A, b
obj_fn = objective()
a_up, b_up = constraints()
res = linprog(obj_fn, A_ub=a_up, b_ub=b_up, bounds=bounds())
The minimum required data to run the code:
V = {0: 1, 1: 71, 2: 6, 3: 0, 4: 34, 5: 51, 6: 88, 7: 61, 8: 0, 9: 0, 10: 43, 11: 62, 12: 144, 13: 36, 14: 0, 15: 12}
D = {0: 94, 1: 16, 2: 38, 3: 6, 4: 66, 5: 22, 6: 134, 7: 8, 8: 46, 9: 6, 10: 3, 11: 36, 12: 39, 13: 26, 14: 40, 15: 22}
edge = [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10), (0, 11), (0, 12), (0, 13), (0, 14), (0, 15), (1, 0), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (1, 12), (1, 13), (1, 14), (1, 15), (2, 0), (2, 1), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (2, 11), (2, 12), (2, 13), (2, 14), (2, 15), (3, 0), (3, 1), (3, 2), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (3, 11), (3, 12), (3, 13), (3, 14), (3, 15), (4, 0), (4, 1), (4, 2), (4, 3), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (4, 10), (4, 11), (4, 12), (4, 13), (4, 14), (4, 15), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (5, 11), (5, 12), (5, 13), (5, 14), (5, 15), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 7), (6, 8), (6, 9), (6, 10), (6, 11), (6, 12), (6, 13), (6, 14), (6, 15), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 8), (7, 9), (7, 10), (7, 11), (7, 12), (7, 13), (7, 14), (7, 15), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 9), (8, 10), (8, 11), (8, 12), (8, 13), (8, 14), (8, 15), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 10), (9, 11), (9, 12), (9, 13), (9, 14), (9, 15), (10, 0), (10, 1), (10, 2), (10, 3), (10, 4), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9), (10, 11), (10, 12), (10, 13), (10, 14), (10, 15), (11, 0), (11, 1), (11, 2), (11, 3), (11, 4), (11, 5), (11, 6), (11, 7), (11, 8), (11, 9), (11, 10), (11, 12), (11, 13), (11, 14), (11, 15), (12, 0), (12, 1), (12, 2), (12, 3), (12, 4), (12, 5), (12, 6), (12, 7), (12, 8), (12, 9), (12, 10), (12, 11), (12, 13), (12, 14), (12, 15), (13, 0), (13, 1), (13, 2), (13, 3), (13, 4), (13, 5), (13, 6), (13, 7), (13, 8), (13, 9), (13, 10), (13, 11), (13, 12), (13, 14), (13, 15), (14, 0), (14, 1), (14, 2), (14, 3), (14, 4), (14, 5), (14, 6), (14, 7), (14, 8), (14, 9), (14, 10), (14, 11), (14, 12), (14, 13), (14, 15), (15, 0), (15, 1), (15, 2), (15, 3), (15, 4), (15, 5), (15, 6), (15, 7), (15, 8), (15, 9), (15, 10), (15, 11), (15, 12), (15, 13), (15, 14)]
region = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
time = {(0, 1): 1, (0, 2): 1, (0, 3): 2, (0, 4): 2, (0, 5): 2, (0, 6): 2, (0, 7): 3, (0, 8): 3, (0, 9): 3, (0, 10): 3, (0, 11): 4, (0, 12): 4, (0, 13): 3, (0, 14): 4, (0, 15): 5, (1, 0): 1, (1, 2): 1, (1, 3): 1, (1, 4): 2, (1, 5): 2, (1, 6): 2, (1, 7): 3, (1, 8): 3, (1, 9): 3, (1, 10): 3, (1, 11): 4, (1, 12): 4, (1, 13): 3, (1, 14): 4, (1, 15): 4, (2, 0): 2, (2, 1): 1, (2, 3): 1, (2, 4): 3, (2, 5): 2, (2, 6): 2, (2, 7): 2, (2, 8): 4, (2, 9): 3, (2, 10): 3, (2, 11): 3, (2, 12): 5, (2, 13): 4, (2, 14): 4, (2, 15): 4, (3, 0): 2, (3, 1): 2, (3, 2): 1, (3, 4): 3, (3, 5): 3, (3, 6): 2, (3, 7): 2, (3, 8): 4, (3, 9): 4, (3, 10): 3, (3, 11): 3, (3, 12): 5, (3, 13): 5, (3, 14): 5, (3, 15): 4, (4, 0): 2, (4, 1): 2, (4, 2): 2, (4, 3): 3, (4, 5): 1, (4, 6): 2, (4, 7): 2, (4, 8): 2, (4, 9): 2, (4, 10): 2, (4, 11): 3, (4, 12): 2, (4, 13): 3, (4, 14): 3, (4, 15): 4, (5, 0): 2, (5, 1): 2, (5, 2): 2, (5, 3): 2, (5, 4): 1, (5, 6): 2, (5, 7): 2, (5, 8): 2, (5, 9): 2, (5, 10): 2, (5, 11): 3, (5, 12): 3, (5, 13): 2, (5, 14): 3, (5, 15): 3, (6, 0): 2, (6, 1): 2, (6, 2): 2, (6, 3): 2, (6, 4): 2, (6, 5): 1, (6, 7): 1, (6, 8): 3, (6, 9): 2, (6, 10): 2, (6, 11): 2, (6, 12): 3, (6, 13): 3, (6, 14): 3, (6, 15): 3, (7, 0): 3, (7, 1): 3, (7, 2): 2, (7, 3): 2, (7, 4): 2, (7, 5): 2, (7, 6): 2, (7, 8): 3, (7, 9): 3, (7, 10): 2, (7, 11): 2, (7, 12): 4, (7, 13): 4, (7, 14): 3, (7, 15): 3, (8, 0): 3, (8, 1): 3, (8, 2): 3, (8, 3): 3, (8, 4): 1, (8, 5): 2, (8, 6): 3, (8, 7): 3, (8, 9): 2, (8, 10): 2, (8, 11): 2, (8, 12): 1, (8, 13): 2, (8, 14): 2, (8, 15): 3, (9, 0): 3, (9, 1): 3, (9, 2): 3, (9, 3): 4, (9, 4): 2, (9, 5): 2, (9, 6): 2, (9, 7): 3, (9, 8): 2, (9, 10): 2, (9, 11): 2, (9, 12): 2, (9, 13): 1, (9, 14): 2, (9, 15): 2, (10, 0): 4, (10, 1): 3, (10, 2): 3, (10, 3): 3, (10, 4): 3, (10, 5): 2, (10, 6): 2, (10, 7): 2, (10, 8): 2, (10, 9): 2, (10, 11): 2, (10, 12): 2, (10, 13): 2, (10, 14): 2, (10, 15): 2, (11, 0): 4, (11, 1): 4, (11, 2): 3, (11, 3): 2, (11, 4): 3, (11, 5): 3, (11, 6): 2, (11, 7): 2, (11, 8): 2, (11, 9): 2, (11, 10): 2, (11, 12): 2, (11, 13): 3, (11, 14): 2, (11, 15): 1, (12, 0): 3, (12, 1): 4, (12, 2): 4, (12, 3): 4, (12, 4): 2, (12, 5): 3, (12, 6): 3, (12, 7): 4, (12, 8): 1, (12, 9): 2, (12, 10): 2, (12, 11): 3, (12, 13): 1, (12, 14): 2, (12, 15): 2, (13, 0): 5, (13, 1): 3, (13, 2): 4, (13, 3): 5, (13, 4): 3, (13, 5): 3, (13, 6): 3, (13, 7): 3, (13, 8): 2, (13, 9): 2, (13, 10): 2, (13, 11): 2, (13, 12): 2, (13, 14): 1, (13, 15): 2, (14, 0): 5, (14, 1): 4, (14, 2): 4, (14, 3): 4, (14, 4): 3, (14, 5): 3, (14, 6): 3, (14, 7): 3, (14, 8): 2, (14, 9): 2, (14, 10): 2, (14, 11): 2, (14, 12): 2, (14, 13): 1, (14, 15): 1, (15, 0): 5, (15, 1): 5, (15, 2): 4, (15, 3): 4, (15, 4): 4, (15, 5): 3, (15, 6): 3, (15, 7): 3, (15, 8): 3, (15, 9): 2, (15, 10): 2, (15, 11): 2, (15, 12): 2, (15, 13): 2, (15, 14): 2}
Update 1
In the code that I am working on, there are two linear programs whose results are fed to each other in a loop-like fashion Linprog 1 -> process result -> Linprog 2 -> process result -> Linprog 1 -> ..... I am analyzing the process result block but because I am not the original developer of the code, I haven't figured out if the problem is here.
The program was written by using CPLEX (IPL) as the solver. When Scipy is used, the results produced by these Linprograms cause each other to be infeasible: The problem is infeasible. (HiGHS Status 8: model_status is Infeasible; primal_status is Basic).
Using the same dataset, the CPLEX code works fine.
When Scipy is used, it shows the warning solve Ill-conditioned matrix detected. Result is not guaranteed to be accurate. or sometimes terminates with infeasible results.
The individual Scipy codes work correctly (with CPLEX results as input)
When I round the results using round() the frequency of warning and infeasible results messages reduces.
We have a given number from 2 to N. Each number divisible should appear as below:
[(2,4), (2,6), (2,8),(3,9),(3,12),(3,15),(4,8),(4,12),(4,16) ...] upto N
numbers.
I tried it myself (see below), but I am not getting the expected output as above (which is the one I want).
>>> [(a, b) for a in range(5) for b in range(5) if a%2 == 0 and b %2==0]
>>> [(0, 0), (0, 2), (0, 4), (2, 0), (2, 2), (2, 4), (4, 0), (4, 2), (4, 4)]
NOTE: Any number divisible by n (where n is a whole number - 1, 2, 3, 4 ...) is a multiple of n.
You can try:
n = int(input("Enter a number: "))
multiples = [(a, b) for a in range(2, n + 1) for b in range(2, n + 1) if b%a == 0 and a != b]
print (multiples)
where n is the number "below" which the number of multiples is printed for a specific number, but using n + 1 prints the number of multiples "up to" n (only if it is possible).
For example, when n = 10, it will give this output, [(2, 4), (2, 6), (2, 8), (2, 10), (3, 6), (3, 9), (4, 8), (5, 10)].
This has two conditions: b%a == 0, which makes sure that a is not zero, as b / 0 == math error and b%a checks whether the second number is a factor of the first number, or not.
Without b%a == 0, you would have:
[(2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (3, 2), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 2), (4, 3), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 2), (5, 3), (5, 4), (5, 6), (5, 7), (5, 8), (5, 9), (6, 2), (6, 3), (6, 4), (6, 5), (6, 7), (6, 8), (6, 9), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 8), (7, 9), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 9), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8)]
Since you do not want a equal to b (3 == 3), you can use a != b, so don't have to worry about getting (3, 3) or (4, 4) etc.
To get the maximum number of multiples for a good range of numbers ( 2 to n) , I would use a larger sample such as n = 50, which will give this output:
[(2, 4), (2, 6), (2, 8), (2, 10), (2, 12), (2, 14), (2, 16), (2, 18), (2, 20), (2, 22), (2, 24), (2, 26), (2, 28), (2, 30), (2, 32), (2, 34), (2, 36), (2, 38), (2, 40), (2, 42), (2, 44), (2, 46), (2, 48), (2, 50), (3, 6), (3, 9), (3, 12), (3, 15), (3, 18), (3, 21), (3, 24), (3, 27), (3, 30), (3, 33), (3, 36), (3, 39), (3, 42), (3, 45), (3, 48), (4, 8), (4, 12), (4, 16), (4, 20), (4, 24), (4, 28), (4, 32), (4, 36), (4, 40), (4, 44), (4, 48), (5, 10), (5, 15), (5, 20), (5, 25), (5, 30), (5, 35), (5, 40), (5, 45), (5, 50), (6, 12), (6, 18), (6, 24), (6, 30), (6, 36), (6, 42), (6, 48), (7, 14), (7, 21), (7, 28), (7, 35), (7, 42), (7, 49), (8, 16), (8, 24), (8, 32), (8, 40), (8, 48), (9, 18), (9, 27), (9, 36), (9, 45), (10, 20), (10, 30), (10, 40), (10, 50), (11, 22), (11, 33), (11, 44), (12, 24), (12, 36), (12, 48), (13, 26), (13, 39), (14, 28), (14, 42), (15, 30), (15, 45), (16, 32), (16, 48), (17, 34), (18, 36), (19, 38), (20, 40), (21, 42), (22, 44), (23, 46), (24, 48), (25, 50)]
Hope this helps!
[(a, b) for a in range(5) for b in range(5)]
gives tuples containing all the combinations of pairs of numbers starting from, and including 0, up to (but not including) 5.
a%2 == 0 is checking for a being even. Likewise for b%2 == 0, so you are finding tuples of even numbers from the unfiltered list above, hence your output.
If you don't want zero, start at 1, e.g. range(1,5) or range(2, 5).
In fact, you need to avoid zeros.
To check the second number is a factor of the first, check b%a, having made sure a won't be zero.
So more like this:
>>> [(a, b) for a in range(2, 5) for b in range(2, 5) if b%a == 0]
[(2, 2), (2, 4), (3, 3), (4, 4)]
To avoid (3,3) or other cases add a != b to the condition in the list comprehension.
[(a, b) for a in range(2, 5) for b in range(2, 5) if b%a == 0 and a!=b]
[(2, 4)]
If you then want each second number to just appear once, you can subsequently filter that list.
I am trying to represent the following as a list comprehension:
L = []
for x in range(n):
for y in range(x):
L.append( (x, y) )
I have done nested list comprehension in the more typical matrix scenario where the inner loop range is not dependent on the outer loop.
I have considered there may be solutions in itertools, using product() or chain() but have been unsuccessful there as well.
Remember to wrap the x, y in parentheses this is the only slight caveat that if omitted leads to a SyntaxError.
Other than that, the translation is pretty straightforward; the order of the fors inside the comprehension is similar to that with the nested statements:
n = 5
[(x, y) for x in range(n) for y in range(x)]
Yields similar results to its nested loop counterpart:
[(1, 0),
(2, 0),
(2, 1),
(3, 0),
(3, 1),
(3, 2),
(4, 0),
(4, 1),
(4, 2),
(4, 3)]
List comprehensions are designed to make a straightforward translation of that loop possible:
[ (x,y) for x in range(3) for y in range(x) ]
Is that not what you wanted?
Below is the example to convert your code to list comprehension.
>>> n = 10
>>> [ (x,y) for x in range(n) for y in range(x)]
[(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2), (4, 0), (4, 1), (4, 2), (4, 3), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8)]
Alternatively, you achieve the same result using itertools library (sharing it just for your knowledge info, but is not recommended for this problem statement):
>>> import itertools
>>> list(itertools.chain.from_iterable(([(list(itertools.product([x], range(x)))) for x in range(n)])))
[(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2), (4, 0), (4, 1), (4, 2), (4, 3), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8)]