How to set the floating point precision in Scipy? - python

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.

Related

Checking array for a value

I have an array as below.
testgrid = [
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6]]
On passing the testgrid into the following function it should return all indexes that do not contain a value of zero currently it returns (0, 0). I would expect all indexes to be returned on this grid. It seems to be checking the indexes rather than the value stored at that index. I am a noob so probably am missing something obvious here.
def not_empty_location(grid):
# checks if current location is empty and assign location not empty
for i in range(9):
for j in range(9):
if grid[i][j] != 0:
return (i, j)
return None
Use yield instead of return:
def not_empty_location(grid):
# checks if current location is empty and assign location not empty
for i in range(9):
for j in range(9):
if grid[i][j] != 0:
yield (i, j)
return None
This would return all the values as a generator which can then simply be converted into a list.
Output:
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8)]
try by storing the index tuples in a list and return
def not_empty_location(grid):
result = [] # list to store the index
# checks if current location is empty and assign location not empty
for i in range(9):
for j in range(9):
if grid[i][j] != 0:
result.append( (i, j) ) # In your code you are returning at your very first success of getting != 0
return result

A given number from 2 to N should have each number divisible outputted as [(2,4), (2,6),( 2,8),( 3,9),( 3,12), (3,15) .....] in Python 2/3

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.

Python: Create route from Arcs

I am working on a capacitated vehicle routing problem and have found an optimal solution with the following set of arcs on my graph active:
[(0, 1),
(0, 4),
(0, 5),
(0, 6),
(0, 7),
(0, 10),
(1, 0),
(2, 13),
(3, 9),
(4, 12),
(5, 0),
(6, 14),
(7, 8),
(8, 0),
(9, 0),
(10, 11),
(11, 0),
(12, 3),
(13, 0),
(14, 2)]
the list is called arcsDelivery.
I would like to restructure this list to come to my found routes stored in the list routesdelivery:
[[0,1,0],[0,4,12,3,9,0],[0,5,0],[0,6,14,2,13,0],[0,7,8,0],[0,10,11,0]]
However, I have been struggeling to do so, anyone with some helpful tips?
Here is a way to do it (considering that the arcsdelivery list is sorted in ascending order based on the first element of each tuple):
def findTuple(elem):
for t in arcsDelivery:
if t[0]==elem:
return t
return None
arcsDelivery = [(0, 1),
(0, 4),
(0, 5),
(0, 6),
(0, 7),
(0, 10),
(1, 0),
(2, 13),
(3, 9),
(4, 12),
(5, 0),
(6, 14),
(7, 8),
(8, 0),
(9, 0),
(10, 11),
(11, 0),
(12, 3),
(13, 0),
(14, 2)]
routesDelivery = []
startRoutes = len(list(filter(lambda elem: elem[0]==0, arcsDelivery)))
for i in range(startRoutes):
tempList = []
currentTuple = arcsDelivery[i]
tempList.append(currentTuple[0])
tempList.append(currentTuple[1])
while True:
if currentTuple[1]==0:
break
else:
nextTuple = findTuple(currentTuple[1])
currentTuple = nextTuple
tempList.append(currentTuple[1])
routesDelivery.append(tempList)
print(routesDelivery)
Output:
[[0, 1, 0], [0, 4, 12, 3, 9, 0], [0, 5, 0], [0, 6, 14, 2, 13, 0], [0, 7, 8, 0], [0, 10, 11, 0]]

Python: mathematical operation of Combination on list of numbers

let's say I have a list of numbers:
my_list = range(0,1001,50)
I want to make every single possible combination of threes (threes = [a,b,c]) based on my_list, for example: [0,50,100], [0,50,150], ..., [0,50,1000], [0,100,150], ...
Threes don't have to be stored in a list, it's just an example of data structure.
And after that I want to put every single threes' values (a,b,c) to some sort of formula.
How could I make that? I'm kinda beginner in Python and haven't experienced with more complicated mathematical structures yet. Or is it possible to do on some kind of loop...?
Any help would be appreciated.
You can use combinations :
my_list = range(0,1000,50)
from itertools import combinations
combinations(my_list,3)
From the doc :
combinations() p, r r-length tuples, in sorted order, no repeated
elements
It creates an iterable. Converted to a list, it looks like :
[(0, 50, 100), (0, 50, 150), (0, 50, 200), (0, 50, 250), (0, 50, 300), ...
What you want is the wonderful itertools module, that has combinations() in it:
>>> import itertools
>>> list(itertools.combinations(range(10), 3))
[(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 1, 5), (0, 1, 6), (0, 1, 7), (0, 1, 8), (0
, 1, 9), (0, 2, 3), (0, 2, 4), (0, 2, 5), (0, 2, 6), (0, 2, 7), (0, 2, 8), (0, 2
, 9), (0, 3, 4), (0, 3, 5), (0, 3, 6), (0, 3, 7), (0, 3, 8), (0, 3, 9), (0, 4, 5
), (0, 4, 6), (0, 4, 7), (0, 4, 8), (0, 4, 9), (0, 5, 6), (0, 5, 7), (0, 5, 8),
(0, 5, 9), (0, 6, 7), (0, 6, 8), (0, 6, 9), (0, 7, 8), (0, 7, 9), (0, 8, 9), (1,
2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 2, 7), (1, 2, 8), (1, 2, 9), (1, 3,
4), (1, 3, 5), (1, 3, 6), (1, 3, 7), (1, 3, 8), (1, 3, 9), (1, 4, 5), (1, 4, 6)
, (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 6), (1, 5, 7), (1, 5, 8), (1, 5, 9), (
1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7, 8), (1, 7, 9), (1, 8, 9), (2, 3, 4), (2,
3, 5), (2, 3, 6), (2, 3, 7), (2, 3, 8), (2, 3, 9), (2, 4, 5), (2, 4, 6), (2, 4,
7), (2, 4, 8), (2, 4, 9), (2, 5, 6), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7),
(2, 6, 8), (2, 6, 9), (2, 7, 8), (2, 7, 9), (2, 8, 9), (3, 4, 5), (3, 4, 6), (3
, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 6), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6
, 7), (3, 6, 8), (3, 6, 9), (3, 7, 8), (3, 7, 9), (3, 8, 9), (4, 5, 6), (4, 5, 7
), (4, 5, 8), (4, 5, 9), (4, 6, 7), (4, 6, 8), (4, 6, 9), (4, 7, 8), (4, 7, 9),
(4, 8, 9), (5, 6, 7), (5, 6, 8), (5, 6, 9), (5, 7, 8), (5, 7, 9), (5, 8, 9), (6,
7, 8), (6, 7, 9), (6, 8, 9), (7, 8, 9)]
Then you can feed that to your function:
def f(a,b,c):
return a * b * c
print [f(*x) for x in itertools.combinations(range(10), 3)]

Finding value in matrix from tuple

I have this matrix:
matrix = np.array([[3,3,3,3,3,3,3,3,3,3,3,3],
[3,2,2,2,2,2,0,0,0,0,0,3],
[3,2,2,2,2,0,0,0,0,0,0,3],
[3,2,2,2,0,0,0,0,0,0,0,3],
[3,2,2,0,0,0,0,0,0,0,0,3],
[3,2,0,0,0,0,0,0,0,0,0,3],
[3,0,0,0,0,0,0,0,0,0,1,3],
[3,0,0,0,0,0,0,0,0,1,1,3],
[3,0,0,0,0,0,0,0,1,1,1,3],
[3,0,0,0,0,0,0,1,1,1,1,3],
[3,0,0,0,0,0,1,1,1,1,1,3],
[3,3,3,3,3,3,3,3,3,3,3,3]])
and a list of lists filled with tuples like these:
[[(10, 6), (10, 5), (10, 7), (9, 6), (9, 5), (9, 7)], [(9, 7), (9, 6), (9, 8), (8, 7), (8, 6), (8, 8), (10, 7), (10, 6), (10, 8)], [(10, 7), (10, 6), (10, 8), (9, 7), (9, 6), (9, 8)], [(8, 8), (8, 7), (8, 9), (7, 8), (7, 7), (7, 9), (9, 8), (9, 7), (9, 9)], [(9, 8), (9, 7), (9, 9), (8, 8), (8, 7), (8, 9), (10, 8), (10, 7), (10, 9)], [(10, 8), (10, 7), (10, 9), (9, 8), (9, 7), (9, 9)], [(7, 9), (7, 8), (7, 10), (6, 9), (6, 8), (6, 10), (8, 9), (8, 8), (8, 10)], [(8, 9), (8, 8), (8, 10), (7, 9), (7, 8), (7, 10), (9, 9), (9, 8), (9, 10)], [(9, 9), (9, 8), (9, 10), (8, 9), (8, 8), (8, 10), (10, 9), (10, 8), (10, 10)], [(10, 9), (10, 8), (10, 10), (9, 9), (9, 8), (9, 10)], [(6, 10), (6, 9), (5, 10), (5, 9), (7, 10), (7, 9)], [(7, 10), (7, 9), (6, 10), (6, 9), (8, 10), (8, 9)], [(8, 10), (8, 9), (7, 10), (7, 9), (9, 10), (9, 9)], [(9, 10), (9, 9), (8, 10), (8, 9), (10, 10), (10, 9)], [(10, 10), (10, 9), (9, 10), (9, 9)]]
which are the number i'm looking for and its neighboring spaces that aren't <10 in y or x, and I'm using a function to classify them between spaces filled with 0s or anything else and append them to a list. How do I correct this function which does the classification?
def classify(neighbors,matrix):
for x in neighbors:
y = x[0]
z = x[1]
if matrix[y][z] == 0:
step.append(x)
else:
hop.append(x)
print(hop,step)
import numpy as np
from array import *
matrix = np.array([[3,3,3,3,3,3,3,3,3,3,3,3],
[3,2,2,2,2,2,0,0,0,0,0,3],
[3,2,2,2,2,0,0,0,0,0,0,3],
[3,2,2,2,0,0,0,0,0,0,0,3],
[3,2,2,0,0,0,0,0,0,0,0,3],
[3,2,0,0,0,0,0,0,0,0,0,3],
[3,0,0,0,0,0,0,0,0,0,1,3],
[3,0,0,0,0,0,0,0,0,1,1,3],
[3,0,0,0,0,0,0,0,1,1,1,3],
[3,11,11,0,0,0,0,1,1,1,1,3],
[3,12,12,0,0,0,1,1,1,1,1,3],
[3,3,13,3,3,3,3,3,3,3,3,3]])
neighbors = [[(10, 6), (10, 5), (10, 7), (9, 6), (9, 5), (9, 7)],(10, 5),[(9, 7), (9, 6), (9, 8), (8, 7), (8, 6), (8, 8), (10, 7), (10, 6), (10, 8)]]
def classify(neighbors,matrix):
hop = []
step = []
for x in neighbors:
print x
if isinstance(x,list):
for x1 in x:
y = x1[0]
z = x1[1]
if matrix[y][z] == 0:
step.append(x1)
else:
hop.append(x1)
else:
y = x[0]
z = x[1]
if matrix[y][z] == 0:
step.append(x)
else:
hop.append(x)
print "neighbors which are not having value as zero =" ,hop
print "neighbors which are having zero value =" ,step
classify(neighbors,matrix)
OUTPUT :-
neighbors which are not having value as zero = [(10, 6), (10, 7), (9, 7), (9, 7), (9, 8), (8, 8), (10, 7), (10, 6), (10, 8)]
neighbors which are having zero value = [(10, 5), (9, 6), (9, 5), (10, 5), (9, 6), (8, 7), (8, 6)]

Categories