Python, Filling a matrix using while loop and modulo operator for filling patterns -
"I'm trying to fill a zero matrix (matrix[j,i]) that represents the weekly production schedule, made from 16 shifts (i variable) for each product (j variable). There are some considerations to fill the matrix:
Each product has weekly demand to be fulfilled as much as possible (DDA1 array). This means the sum of one row of j product has to be equal or less to its corresponding demand in DDA1 array. To produce each product j on the i shift, it has a production capacity found in the CAP1 array. There are two types of scheduled halts over the production line in which there is no production on i shift. One halt happens every 3 shifts and the other halt happens every 4 shifts. There is no parallel production of products, this means until j product's demand is fulfilled, there won't be production of the next j product. In this case, we only have demand for products DDA1[2] and DDA1[3], and the demand for j = 2 is more than weekly production (sum of the whole 3rd row of the matrix), so there will be only production of j = 2 product.
Problem: According to the following code, everything seems to be working properly but the element matrix[2,8] of the matrix is equal to 2795 and it should be equal to 0, because of the ""every 3 shifts halt"" rule. ¿What is the reason behind this? I'm getting started with these kinds of while loops. the code is as follows: import numpy as np import pandas as pd
turns = 16 DDA1 = [0, 0, 28600, 11500, 0] #Demand for j product CAP1 = [1944.0, 2640.0, 2795.0, 2733.0, 1639.0] #Production capacity for j product
matrix = np.zeros((len(DDA1), turns)) #Matrix to be filled
j=0 #Product i=0 #shift number
stop = True while j < len(DDA1) and stop == True: acum = 0 #accumulated production while acum < DDA1[j]: if i < turns : #Normal production prod = min(DDA1[j]-acum,CAP1[j]) acum += prod matrix[j,i] = prod if i == turns - 1: stop = False break else: i += 1
if i % 3 == 2: #not producing every 3 turns prod = min(DDA1[j]-acum, 0) acum += prod matrix[j,i] = prod if i == turns - 1: stop = False break else: i += 1
if i % 4 == 3: #not producing every 4 turns prod = min(DDA1[j]-acum, 0) acum += prod matrix[j,i] = prod if i == turns - 1: stop = False break else: i += 1
j += 1 print(matrix)
The result I get is : [[ 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.] [2795. 2795. 0. 0. 2795. 0. 2795. 0. 2795. 2795. 2795. 0. 2795. 2795. 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.]]
The desired result is: [[ 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.] [2795. 2795. 0. 0. 2795. 0. 2795. 0. 0. 2795. 2795. 0. 2795. 2795. 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.]] ```
python while-loop modulo
ShareShare a link to this question Copy linkCC BY-SA 4.0
Follow Follow this question to receive notifications
asked 4 mins ago barunsonnbarunsonn 344 bronze badges"
|