在列X
中是那些将在每个列j中具有值的变量,在这种情况下,只有U1
、X4
和U2
具有值,属于列表['B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2']
的其余变量都将具有其值0
#example matrix
new_matrix = [[ 'C', 'X', 'B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2'],
[ 0.0, 'U1', 8, 2.0, 1.0, -1.0, 0, 0, 1.0, 0],
['+M', 'X4', 2, 1.0, 1.0, 0, 1.0, 0, 0, 0],
['+M', 'U2', 8, 1.0, 2.0, 0, 0, -1.0, 0, 1.0]]
variables = ['X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2', 'B'] #select the first row (only variables)
variables_j_col_values = [[variables.pop(variables.index('B'))] + variables, []] # --> ['B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2']
这样做的问题是,我需要创建以下变量值的矩阵(不使用库),其中我将有以下内容:
variables_j_col_values = [['B', 'X1', 'X2', 'X3', 'X4', 'X5', 'U1', 'U2'],
[ 0 , 0, 0, 0, 2, 0, 8, 8], #column new_matrix[][2]
[ 0 , 0, 0, 0, 1.0, 0, 2.0, 1.0], #column new_matrix[][3]
[ 0 , 0, 0, 0, 1.0, 0, 1.0, 2.0], #column new_matrix[][4]
[ 0 , 0, 0, 0, 0, 0, -1.0, 0], #column new_matrix[][5]
[ 0 , 0, 0, 0, 1.0, 0, 0, 0], #column new_matrix[][6]
[ 0 , 0, 0, 0, 0, 0, 0, -1.0], #column new_matrix[][7]
[ 0 , 0, 0, 0, 0, 0, 1.0, 0], #column new_matrix[][8]
[ 0 , 0, 0, 0, 0, 0, 0, 1.0], ] #column new_matrix[][9]
在创建variables_j_col_values
之后,替换funcion_obj_z
中字符串中的行的值(除了variables_j_col_values
数组的第0行,因为它是一个头)逻辑是使用遍历行的循环,并执行.replace(new_matrix[][n], this_element)
funcion_obj_z = 'Z = 3 * X1 + 2 * X2 + 0 * X3 + 0 * X4 + 0 * X5 + M * U1 + M * U2'
通过这种方式,使用所述字符串作为表达式,如果它在每次j
迭代中打印j_func
的值,则它将在控制台中获得这些打印。这些将是所需的正确输出:
#for loop, print the j string replacement the values in the string
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 2 + 0 * 0 + M * 8 + M * 8' #iteration 1
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 1.0 + 0 * 0 + M * 2.0 + M * 1.0' #iteration 2
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 1.0 + 0 * 0 + M * 1.0 + M * 2.0' #iteration 3
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + M * -1.0 + M * 0' #iteration 4
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 1.0 + 0 * 0 + M * 0 + M * 0' #iteration 5
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + M * 0 + M * -1.0' #iteration 6
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + M * 1.0 + M * 0' #iteration 7
j_func = 'Z = 3 * 0 + 2 * 0 + 0 * 0 + 0 * 0 + 0 * 0 + M * 0 + M * 1.0' #iteration 8
1条答案
按热度按时间mfuanj7w1#
尽管这是一个丑陋的解决方案,但它应该会给予你所需要的转换: