Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中3个约束条件下6维问题的最小化_Python_Matrix_Vector_Modeling_Linear Programming - Fatal编程技术网

python中3个约束条件下6维问题的最小化

python中3个约束条件下6维问题的最小化,python,matrix,vector,modeling,linear-programming,Python,Matrix,Vector,Modeling,Linear Programming,我们遇到了一个问题,要求学生的食物成本最小化。她必须摄入卡路里(2000千卡)、蛋白质(55克)和钙(800毫克)。总共有六种食物。我们必须用Python CVXPY包来解决这个问题。 这是我的代码,但它返回了ValueError:cannotbroadcasteddimensions(3,)(3,1)error。 这是我的矩阵的代码 A=np.array([[110,205,160,160,420,260],[4,32,13,8,4,14],[2,12,54,285,22,80]) //No

我们遇到了一个问题,要求学生的食物成本最小化。她必须摄入卡路里(2000千卡)、蛋白质(55克)和钙(800毫克)。总共有六种食物。我们必须用Python CVXPY包来解决这个问题。 这是我的代码,但它返回了
ValueError:cannotbroadcasteddimensions(3,)(3,1)
error。 这是我的矩阵的代码

A=np.array([[110,205,160,160,420,260],[4,32,13,8,4,14],[2,12,54,285,22,80]) 
//Note:A is the matrix with the contents of all the foods//
b=np.array([[2000,55,800]])
//Note: b represents the desired value for calories, protein and calcium.//
c=np.array([[3,24,13,9,20,19]])
//Note c represents the price of the 6 types of food. //
在要求每种食物的最大食用量时,增加了一个额外的标准。我的代码是这样的:

d= np.array([[4,3,2,8,2,2]])
x_vec = cp.Variable(6)
//where d represented the max serving constraints on each of the six types of foods.//
最后,我必须编写最小化和解决问题的代码,因为某些原因,这似乎不起作用

matrix_prob = cp.Problem(cp.Minimize(cp.sum(c.T @ x_vec)),
                        constraints=[A@x_vec <= b, x_vec <= d])
//Note:here I minize the sum of matrix c while considering the constraints of b and d.//
matrix_solution = matrix_prob.solve()
print('Optimal value (matrix form):    {:.4f}'.format(matrix_solution))
matrix_prob=cp.Problem(cp.Minimize(cp.sum(c.T@x_vec)),
约束条件=[A@x_vec错误:

ValueError: Cannot broadcast dimensions  (3,) (3, 1)
自言自语:您正在尝试执行涉及一维和二维对象的操作。由于二维对象在其第二维中的长度为1,这表明删除该维将解决您的问题

因此,尝试制作
b
c
d
一维数组,例如:

d = np.array([4,3,2,8,2,2])

另外:Python使用
#
作为注释。我不知道为什么使用
/
,但是不要使用。

只需将
b
定义为
1d数组
,就像
b=np.array([2000,55800])
那样,这将导致形状
(3,)
。然后
A@x_vec