Resource Allocation Algorithms - Minimize cost given Time - python-3.x

I want assign tasks to workers in a way that cost is minimized given a time constraint.
Suppose:
Labor Cost: $20/hr and handles 10Kg
Machine Cost: $30/hr and handles 20Kg and requires a Machine Operator
Machine Operator Cost: 15$/hr
Constraints:
Total Available Labor = 10
Total Available Machines = 5
Total Available Machine Operator = 3
Total Required Load to Handle = 1000Kg
Total Available Hours to complete Task = 8
How can I solve this problem using Python? Are there any libraries to solve such problems?
Edit:
Objective:
Minimize:
Cost = 20 * ith_labor_time + 30 * jth_Machine_time + 15 * kth_Machine_Operator_time
Now the constraints:
1) 0 <= i(total_labor) <= 10
2) 0 <= j(total_machine) <= 5 (Not 5 because only 3 operators available and machine is dependent on operator)
3) 0 <= k(total_machine_operator_time) <= 3
4) sum(ith_labor_time <= 80)
5) sum(ith_machine_time <= 40)
5) sum(ith_Operator_time) - sum(ith_Machine_time) = 0
6) constraint that total weight handled is 1000kg (unable to make this constraint)
7) machine_time and operator_time are dependent on each other as 1 machine requires 1 operator (I believe this is handled in constraint 5)
Practical Example:
Suppose we have total_labor = 3, total_machines = 3 and total_operators = 3
Cost per labor/hr = $20
Cost per machinery/hr = $30
Cost per operator/hr = $15
Labor process rate/hr = 10 Kg
Machine process rate/hr = 20 Kg
Next each labor can work for 8 hours similarly each machinery can work for 8 hours and same for the machine operator they can work for 8 hours.
Task needs to be completed in 8 hours and 1000kg load needs to be processed.
My formulation:
Min:
Cost = 20*L1 + 20*L2 + 20*L3 + 30*M1 + 30*M2 + 30*M3 + 15*O1 + 15*O2 + 15*O3
Constraints:
L1 + L2 + L3 <= 24 (if each labor work simultaneously they can total up to 24hrs)
L1 <= 8
L2 <= 8
L3 <= 8
M1 + M2 + M3 <= 24 (if each machinery works simultaneously they can total up to 24hrs)
M1 <= 8
M2 <= 8
M3 <= 8
M1 + M2 + M3 - O1 - O2 - O3 = 0 (Each machinery requires an operator)
10*L1 + 10*L2 + 10*L3 + 20*M1 + 20*M2 + 20*M3 = 1000 (Total load to be processed = 1000 Kg)
Code
from scipy.optimize import linprog
c = [20, 20, 20, 30, 30, 30, 15, 15, 15]
A = [[1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, -1, -1, -1]]
A1 = [[10, 10, 10, 20, 20, 20, 0, 0, 0]]
b = [24, 24, 8, 8, 8, 8, 8, 8, 0]
b2 = [1000]
res = linprog(c, A_ub=A, A_eq=A1, b_ub=b, b_eq=b2, method='revised simplex')

Related

How to make a checkerboard in Pytorch?

I see that a simple checkerboard pattern can be created fairly concisely with numpy Does anyone know if a checkerboard where each square may contain multiple values could be created? E.g.:
1 1 0 0 1 1
1 1 0 0 1 1
0 0 1 1 0 0
0 0 1 1 0 0
Although there is no equivalent of np.indices in PyTorch, you can still find a workaround using a combination of torch.arange, torch.meshgrid, and torch.stack:
def indices(h,w):
return torch.stack(torch.meshgrid(torch.arange(h), torch.arange(w)))
This allows you to define a base tensor with a checkboard pattern following your linked post:
>>> base = indices(2,3).sum(axis=0) % 2
tensor([[0, 1, 0],
[1, 0, 1]])
Then you can repeat the row end columns with torch.repeat_interleave:
>>> base.repeat_interleave(2, dim=0).repeat_interleave(2, dim=1)
tensor([[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0],
[1, 1, 0, 0, 1, 1],
[1, 1, 0, 0, 1, 1]])
And you can take the opposite of a given checkboard x by computing 1-x.
So you could define a function like this:
def checkerboard(shape, k):
"""
shape: dimensions of output tensor
k: edge size of square
"""
h, w = shape
base = indices(h//k, w//k).sum(dim=0) % 2
x = base.repeat_interleave(k, 0).repeat_interleave(k, 1)
return 1-x
And try with:
>>> checkerboard((4,6), 2)
tensor([[1, 1, 0, 0, 1, 1],
[1, 1, 0, 0, 1, 1],
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0]])

'matrix' object has no attribute 'eigenvals'

I have the following code
import numpy as np
import sympy as sp
def bra(i,d):
arr = np.zeros((1,d))
if i <= (d-1):
arr[:,i] = 1
else:
print("Index Out of bounds")
return arr
def density(i,j,d):
return bra(i,d).T*bra(j,d)
SIGMA = (1/3)*(np.kron(np.kron(bra(0,3),bra(1,3)).T,np.kron(bra(0,3),bra(1,3)))+np.kron(np.kron(bra(1,3),bra(2,3)).T,np.kron(bra(1,3),bra(2,3)))+np.kron(np.kron(bra(2,3),bra(0,3)).T,np.kron(bra(2,3),bra(0,3))))
DELTA = (1/3)*(np.kron(np.kron(bra(1,3),bra(0,3)).T,np.kron(bra(1,3),bra(0,3)))+np.kron(np.kron(bra(2,3),bra(1,3)).T,np.kron(bra(2,3),bra(1,3)))+np.kron(np.kron(bra(0,3),bra(2,3)).T,np.kron(bra(0,3),bra(2,3))))
Essentially bra(i,d) gives a (1,d) array with (1,i)th element as one and rest as zero. For example,
bra(0,3)=[[1,0,0]]
bra(1,3)=[[0,1,0]]
bra(0,3)=[[0,0,1]]
density(i,j,d) returns (d,d) matrix with (i,j)th element as one rest as zero, for example,
density(0,1,3) = array([[0., 1., 0.], [0., 0., 0.],[0., 0., 0.]]).
Using the above codes, I have produced a (9,9) Matrix object by using np.kron
a = sp.symbols('a', positive = True)
HORO = (2/21)*(np.kron(density(0,0,3),density(0,0,3))+np.kron(density(1,0,3),density(0,1,3))+np.kron(density(0,0,3),density(2,2,3))
+np.kron(density(0,1,3),density(1,0,3))+np.kron(density(1,1,3),density(1,1,3))+np.kron(density(2,1,3),density(1,2,3))
+np.kron(density(0,2,3),density(2,0,3))+np.kron(density(1,2,3),density(2,1,3))+np.kron(density(2,2,3),density(2,2,3)))+(a/7)*SIGMA+((5-a)/7)*DELTA
M = np.asmatrix(HORO)
It can be shown that M matrix is invertible and it
is the following (9,9) matrix
matrix([[0.0952380952380952, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0.0476190476190476*a, 0, 0.0952380952380952, 0, 0, 0, 0, 0],
[0, 0, 0.333333333333333 - 0.0476190476190476*a, 0, 0, 0,
0.0952380952380952, 0, 0],
[0, 0.0952380952380952, 0,
0.238095238095238 - 0.0476190476190476*a, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0.0952380952380952, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0.0476190476190476*a, 0, 0.0952380952380952, 0],
[0, 0, 0, 0, 0, 0, 0.0476190476190476*a, 0, 0],
[0, 0, 0, 0, 0, 0.0952380952380952, 0,
0.238095238095238 - 0.0476190476190476*a, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0.0952380952380952]], dtype=object)
For this symbolic matrix I want to calculate the eigenvalues as
M.eigenvals()
However, it is returning the following error
AttributeError: 'matrix' object has no attribute 'eigenvals'
I can't figure out what went wrong.
Usually, numpy and sympy don't mix well. Numpy can't work with sympy functions and symbols. Also, sympy tries to calculate exact symbolic solutions, which doesn't work well together with floats (sympy strongly prefers integers, rationals and symbolic expressions such as sp.sqrt()).
In this case, provided you don't change too much, numpy managed to calculate the matrix elements well (sums and productions are executed via the underlying standard Python).
Now, if you just use that matrix in sympy, you get:
M = sp.Matrix(HORO)
print(M.eigenvals())
You get:
{0.0952380952380952: 3,
0.119047619047619 - 0.152455338986496 * sqrt(0.0975609756097561 * a ** 2 - 0.48780487804878 * a + 1.0): 2,
0.152455338986496 * sqrt(0.0975609756097561 * a ** 2 - 0.48780487804878 * a + 1.0) + 0.119047619047619: 2,
0.0476190476190476 * a: 1,
0.333333333333333 - 0.0476190476190476 * a: 1}
A better approach uses nsimplify to convert floats to rationals:
M = sp.Matrix(sp.nsimplify(HORO))
print(M.eigenvals())
This gives a nicer symbolic solution:
{2 / 21: 3,
5 / 42 - sqrt(4 * a ** 2 - 20 * a + 41) / 42: 2,
sqrt(4 * a ** 2 - 20 * a + 41) / 42 + 5 / 42: 2,
a / 21: 1,
1 / 3 - a / 21: 1}

How to initialize an 3D array variable in Gekko?

I'm trying to solve a step from a three-dimensional master timetabling model, which involvs periods(5), courses(19) and locations(8).
So I have a problem to initialize these variables with an 3D array in Gekko. Without this initialization the algorithm doesn't converge, after more than 15 minutes run and 1000 iterations.
When I try initialize, this error appears:
"
raise Exception(response)
Exception: #error: Equation Definition
Equation without an equality (=) or inequality (>,<)
true
STOPPING...
"
How can I fix this problem? Follows a version of my code:
import numpy as np
from gekko import GEKKO
# Input data
# Schedule of periods and courses
sched = np.array([ [0, 1, 0, 0, 1], [0, 0, 1, 1, 0], [0, 0, 1, 1, 0], \
[0, 0, 0, 0, 1], [1, 0, 0, 0, 1], [0, 0, 0, 1, 1], [0, 1, 1, 0, 0], \
[1, 0, 0, 1, 0], [0, 1, 0, 0, 1], [1, 1, 0, 0, 0], [0, 1, 1, 0, 0], \
[0, 1, 1, 0, 0], [1, 0, 0, 1, 0], [1, 0, 0, 1, 0], [0, 0, 1, 0, 1], \
[1, 0, 1, 0, 0], [0, 1, 0, 1, 0], [0, 0, 1, 1, 0], [0, 1, 0, 0, 1] ], dtype=np.int64)
# Initial allocation of all periods, courses and locations
alloc=np.array([0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,\
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,\
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,0,0,\
0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,\
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], dtype=np.int64)
# Number of students enrolled in each course
enrol = np.array([ 60, 60, 60, 40, 40, 110, 120, 50, 60, 55, 50, \
55, 40, 64, 72, 50, 50, 55, 55], dtype=np.float64)
# Capacity of each location (classroom)
capac = np.array([ 60, 60, 120, 60, 80, 60, 60, 65], dtype=np.float64)
# Total costs of using each location
costs = np.array([ 9017.12, 9017.12, 12050.24, 9017.12, 9413.68, 9017.12, \
9017.12, 9188.96 ])
# Estimated cost of each location by period and student
ecost = np.repeat(np.array([[costs[i]*pow(enrol[j]*5,-1) for j in range(19)] for i in range(8)]), 5)
# The model construction
m = GEKKO()
# Constant arrays
x = m.Array(m.Const,(19,5))
y = m.Array(m.Const,(8,19,5))
N = m.Array(m.Const,(19))
C = m.Array(m.Const,(8))
Ec = m.Array(m.Const,(8,19,5))
Ecy = m.Array(m.Const,(8,19,5))
Alt = m.Array(m.Const,(8,19,5))
for k in range(5):
for j in range(19):
N[j] = enrol[j]
x[j,k] = sched[j,k]
for i in range(8):
C[i] = capac[i]
Ec[i,j,k] = ecost[k+j*5+i*19*5]
y[i,j,k] = alloc[k+j*5+i*19*5]
Ecy[i,j,k] = Ec[i,j,k]*y[i,j,k]
if sched[j,k]==1:
Alt[i,j,np.where(sched[j,:]==1)[0][0]]=-sched[j,k]*(1-sum(sched[j,:]))
if sum(sched[j,:])==2:
Alt[i,j,np.where(sched[j,:]==1)[0][1]]=sched[j,k]*(1-sum(sched[j,:]))
else:
Alt[i,j,k]=0
# Initialize the variable z with the initial value y:
# These commented approaches produce the error.
z = m.Array(m.Var,(8,19,5),lb=0,ub=1,integer=True)
#for i in range(8):
# for j in range(19):
# for k in range(5):
# z[i,j,k] = y[i,j,k]
# nor
#z = m.Array(m.Var,(8,19,5),value=y,lb=0,ub=1,integer=True)
# Intermediate equations
Ecz = m.Array(m.Var,(8,19,5),lb=0)
Altz = m.Array(m.Var,(8,19))
for i in range(8):
for j in range(19):
Altz[i,j]=m.Intermediate(m.sum(Alt[i,j,:]*z[i,j,:]))
for k in range(5):
Ecz[i,j,k]=m.Intermediate(Ec[i,j,k]*z[i,j,k])
# Constraints
m.Equation(m.sum(m.sum(m.sum(Ecz)))<=m.sum(m.sum(m.sum(Ecy))))
for j in range(19):
for k in range(5):
m.Equation(m.sum(z[:,j,k])==x[j,k])
for i in range(8):
for k in range(5):
m.Equation(m.sum(z[i,:,k])==m.sum(y[i,:,k]))
for i in range(8):
for j in range(19):
m.Equation(m.sum((C[i]/N[j]-x[j,:])*z[i,j,:])>=0)
# Objective: to minimize the quantity of courses allocated in different locations
# Example: with the solution y, I have 12 courses in different locations in the periods
# print(sum([sum(Alt[i,j,:]*y[i,j,:])**2 for j in range(19) for i in range(8)])/2)
for i in range(8):
for j in range(19):
m.Obj(Altz[i,j]**2/2)
# Options and final results
m.options.SOLVER=1
m.options.IMODE=2
m.solve()
print(z)
print(m.options.OBJFCNVAL)
Note: My original problem has 20 periods, 171 courses, and 18 locations.
Use z[i,j,k].value = y[i,j,k] to give an initial guess for z. Using z[i,j,k] = y[i,j,k] redefines z entries as floating point numbers instead of gekko variable types.
One other issue is that the variables Ecz and Altz are defined as Variables as m.Var and then overridden as Intermediates. Instead, try allocating them and assigning them as intermediates:
Ecz = np.empty((8,19,5),dtype=object)
Altz = np.empty((8,19),dtype=object)
Use flatten() to simplify the summation of all elements of the 3 dimensional array.
m.Equation(m.sum(Ecz.flatten())<=sum(Ecy.flatten()))
The constant arrays can be defined as numpy arrays to avoid additional symbolic processing by Gekko. This speeds up the model compile time but has no effect on the final solution.
x = np.empty((19,5))
y = np.empty((8,19,5))
N = np.empty((19))
C = np.empty((8))
Ec = np.empty((8,19,5))
Ecy = np.empty((8,19,5))
Alt = np.empty((8,19,5))
The IMODE should be 3 for optimization. IMODE=2 is for parameter regression. IMODE=2 should also work for this problem but 3 is the correct option because you aren't trying to fit to data.
m.options.IMODE=3
Try using IPOPT to obtain an initial non-integer solution and then use APOPT to find an integer solution.
m.solver_options = ['minlp_gap_tol 1.0e-2',\
'minlp_maximum_iterations 10000',\
'minlp_max_iter_with_int_sol 500',\
'minlp_branch_method 1']
Mixed Integer Nonlinear Programming (MINLP) problems can be challenging to solve so you may need to use some of the solver options to speed up the solution. Try minlp_branch_method 1 to help the solver find an initial integer solution to do better pruning. The gap tolerance can also help to speed up the solution if a sub-optimal solution is okay. Below is the complete script. Consider using remote=False to run locally instead of using the public servers, especially for large optimization problems.
import numpy as np
from gekko import GEKKO
# Input data
# Schedule of periods and courses
sched = np.array([ [0, 1, 0, 0, 1], [0, 0, 1, 1, 0], [0, 0, 1, 1, 0], \
[0, 0, 0, 0, 1], [1, 0, 0, 0, 1], [0, 0, 0, 1, 1], [0, 1, 1, 0, 0], \
[1, 0, 0, 1, 0], [0, 1, 0, 0, 1], [1, 1, 0, 0, 0], [0, 1, 1, 0, 0], \
[0, 1, 1, 0, 0], [1, 0, 0, 1, 0], [1, 0, 0, 1, 0], [0, 0, 1, 0, 1], \
[1, 0, 1, 0, 0], [0, 1, 0, 1, 0], [0, 0, 1, 1, 0], [0, 1, 0, 0, 1] ], dtype=np.int64)
# Initial allocation of all periods, courses and locations
alloc=np.array([0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,\
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,\
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,\
0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,\
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], dtype=np.int64)
# Number of students enrolled in each course
enrol = np.array([ 60, 60, 60, 40, 40, 110, 120, 50, 60, 55, 50, \
55, 40, 64, 72, 50, 50, 55, 55], dtype=np.float64)
# Capacity of each location (classroom)
capac = np.array([ 60, 60, 120, 60, 80, 60, 60, 65], dtype=np.float64)
# Total costs of using each location
costs = np.array([ 9017.12, 9017.12, 12050.24, 9017.12, 9413.68, 9017.12, \
9017.12, 9188.96 ])
# Estimated cost of each location by period and student
ecost = np.repeat(np.array([[costs[i]*pow(enrol[j]*5,-1) for j in range(19)] for i in range(8)]), 5)
# The model construction
m = GEKKO(remote=True)
# Constant arrays
x = np.empty((19,5))
y = np.empty((8,19,5))
N = np.empty((19))
C = np.empty((8))
Ec = np.empty((8,19,5))
Ecy = np.empty((8,19,5))
Alt = np.empty((8,19,5))
for k in range(5):
for j in range(19):
N[j] = enrol[j]
x[j,k] = sched[j,k]
for i in range(8):
C[i] = capac[i]
Ec[i,j,k] = ecost[k+j*5+i*19*5]
y[i,j,k] = alloc[k+j*5+i*19*5]
Ecy[i,j,k] = Ec[i,j,k]*y[i,j,k]
if sched[j,k]==1:
Alt[i,j,np.where(sched[j,:]==1)[0][0]]=-sched[j,k]*(1-sum(sched[j,:]))
if sum(sched[j,:])==2:
Alt[i,j,np.where(sched[j,:]==1)[0][1]]=sched[j,k]*(1-sum(sched[j,:]))
else:
Alt[i,j,k]=0
# Initialize the variable z with the initial value y:
# These commented approaches produce the error.
z = m.Array(m.Var,(8,19,5),lb=0,ub=1,integer=True)
for i in range(8):
for j in range(19):
for k in range(5):
z[i,j,k].value = y[i,j,k]
# nor
#z = m.Array(m.Var,(8,19,5),value=y,lb=0,ub=1,integer=True)
# Intermediate equations
Ecz = np.empty((8,19,5),dtype=object)
Altz = np.empty((8,19),dtype=object)
for i in range(8):
for j in range(19):
Altz[i,j]=m.Intermediate(m.sum(Alt[i,j,:]*z[i,j,:]))
for k in range(5):
Ecz[i,j,k]=m.Intermediate(Ec[i,j,k]*z[i,j,k])
# Constraints
m.Equation(m.sum(Ecz.flatten())<=sum(Ecy.flatten()))
for j in range(19):
for k in range(5):
m.Equation(m.sum(z[:,j,k])==x[j,k])
for i in range(8):
for k in range(5):
m.Equation(m.sum(z[i,:,k])==m.sum(y[i,:,k]))
for i in range(8):
for j in range(19):
m.Equation(m.sum((C[i]/N[j]-x[j,:])*z[i,j,:])>=0)
# Objective: to minimize the quantity of courses allocated in different locations
# Example: with the solution y, I have 12 courses in different locations in the periods
# print(sum([sum(Alt[i,j,:]*y[i,j,:])**2 for j in range(19) for i in range(8)])/2)
for i in range(8):
for j in range(19):
m.Obj(Altz[i,j]**2/2)
# Options and final results
m.options.IMODE=3
# Initialize with IPOPT
m.options.SOLVER=3
m.solve()
# Integer solution with APOPT
m.options.SOLVER=1
m.solver_options = ['minlp_gap_tol 1.0e-2',\
'minlp_maximum_iterations 10000',\
'minlp_max_iter_with_int_sol 500',\
'minlp_branch_method 1']
m.solve()
print(z)
print(m.options.OBJFCNVAL)

How to check the distance between a specific element an index? Python3

A = [1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0]
B = 9
I want to find out the distances between the index[9] (B) and each of it's closest's 1's. For example, If we look at list A, we see that index 9 is this:
A = [1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0]
^
I would like to figure out the distances between B(index 9) and it's nearest 1's. For example, the nearest 1's in this case would be this:
A = [1, 0, 0, 0, 1, 0, 0, 1, 0, B, 0, 0, 1, 0]
^ ^
So in this case the output would be:
>> [2, 3] ##Since the distance between 1, 0, B is 2 and the distance between B, 0, 0, 1 is 3.
So far I've come up with the following code:
def checkDistance(A, B):
for i in A:
if i == 1:
#Check if it is the closest to B
#Check distance
Sadly I'm a beginner in python and I am struggling to finish. Any help would be much appreciated :)
def distance(lst,index):
c=[i for i,j in enumerate(lst) if j==1]
for k,l in zip(c[:-1],c[1:]):
if k < index < l:
return [index-k, l-index]
a = [1, 0, 0, 0, 1, 0, 0, 1, 0, B, 0, 0, 1, 0]
b = 9
distance(a, b)
Out: [2, 3]
You could use the following function. In this case, to make the function more abstract, you needn't force the value for the comparison to be 1 in the function.
In the function below, you do a for loop starting at the position you specified (in Python indexes start at 0, not at 1) and finishing when the list finishes.
The if statement compares element with the value of the list at a given position i
def checkDistance(lst,index,element):
counter = 0
results = []
for i in range(index,len(lst)):
if lst[i] == element:
print("Detected element at distance: " + str(counter))
results.append(counter)
counter += 1
return results

Finding an index in a list using 2 lists

Im trying to find the index of each 1 that is at the end of each group(sequence) of 1s. The output for the example below should be [17, 12, 9, 5, 1].
I can find the 1 at index 17 using the formula below but I also need to find the 1 at index 12. (i.e. it's the last instance of 1 in each sequence (or group) of 1s)
list = [1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0]
index_list1 = [i for i, v in enumerate(list) if v == 1]
index_list0 = [i for i, v in enumerate(list) if v == 0]
val1 = None
for i in index_list1[::-1]:
if i < index_list0[-1]:
val1 = i
break
else:
val1 = index_list1[-1]
if val1 is not None:
index1 = val1
You can simply use a conditional list comprehension:
my_list = [1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0]
results = [i for i, value in enumerate(my_list) if value == 1 and (my_list+[0])[i+1] != 1]
print(results)
this will print
[1, 5, 9, 12, 17]
The reason to append a [0] is to take care of cases where the original list ends with a 1.

Resources