本文介绍了向量对矩阵乘积的热求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编辑我的代码,包括所有涉及的参数和变量:

I edit my code including all the parameters and variables involved:

(D是从Python导入的numpy矩阵)

(D is a numpy matrix imported from Python)

import pyomo
from pyomo.environ import *
from array import *


import numpy as np
import scipy as sp
from diff_matrix import D  ##N=10????
print(D)


m =ConcreteModel()
...
m.n = Param(initialize = 10, within = Integers)               
m.Ns = Set(initialize = range(0,value(m.n)))   
m.x1 = Var(m.N, domain = Reals)
m.D = Param(m.N, m.N, initialize=D)

m.f_x1 = Var(m.N)
def f_x1_definition(model,i):
        return m.f_x1[i] == sum(m.x1[j]*m.D[i,j] for j in range(value(m.n)))
m.f_x1_const = Constraint(m.Ns, rule = f_x1_definition)

但是我收到下一个错误:

But I get the next error:

   ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

有帮助吗?

推荐答案

最简单的方法是仅使用Python sum()函数而不是Pyomo summation()函数:

The simplest thing is to just use the Python sum() function instead of the Pyomo summation() function:

def f_x1_definition(model,i):
    return model.f_x1[i] == sum(model.x1[j]*model.D[i,j] for j in range(value(model.n)))

此外,请注意,我颠倒了Pyomo Var(m.x1)和矩阵(m.D)的顺序.根据您的其他问题(将矩阵从Python导入Pyomo ) ,我假设矩阵是NumPy矩阵.当将NumPy值和Pyomo分量(VarParam)相乘时,请始终将Pyomo对象放在第一位.这是由于在当前版本的Pyomo(至少达到5.1)中NumPy运算符重载与Pyomo运算符重载之间发生冲突.

Also, note that I reversed the order of the Pyomo Var (m.x1) and the matrix (m.D). Based on your other questions (Importing a matrix from Python to Pyomo), I am assuming that the matrix is a NumPy matrix. When multiplying a NumPy value and a Pyomo component (Var or Param), always put the Pyomo object first. This is due to a conflict between the NumPy operator overloading and the Pyomo operator overloading in current versions of Pyomo (up through at least 5.1).

编辑1 :有关反转操作数顺序的注意事项:在您最初的问题中,尚不清楚将m.D定义为Pyomo Param.表达式中Pyomo对象的顺序无关紧要.当将NumPy对象与Pyomo组件相乘时,上述操作符重载问题仅是 .此外,此时(直到Pyomo 5.1为止),Pyomo不支持矩阵代数-即像矩阵矩阵或矩阵向量乘积之类的运算.由于每个表达式都是标量表达式,所以交换运算中的项的顺序(+*)不会改变表达式的含义.

EDIT 1: Note on reversing the order of operands: in your original question, it was not clear that m.D was being defined as a Pyomo Param. There is no concern with the order of Pyomo objects in expressions. The operator overloading problem mentioned above is only when multiplying NumPy objects with Pyomo components. Further, at this time (up through Pyomo 5.1), Pyomo does not support matrix algebra - that is, operations like matrix-matrix or matrix-vector products. Since every expression is a scalar expression, the ordering of the terms in a commutative operation (+, *) does not change the meaning of the expression.

编辑2 :您的错误与最初发布的sum/summation无关.问题在于您如何初始化参数.目前(直到Pyomo 5.1),您不能直接从numpy.ndarray初始化Param.您需要先将NumPy对象转换为Python字典,例如:

EDIT 2: Your error has nothing to do with the sum/summation you originally posted. The problem is with how you are initializing your Param. At this time (up through Pyomo 5.1), you cannot directly initialize a Param from a numpy.ndarray. You need to first convert the NumPy object into a Python dictionary with something like:

m.D = Param(m.N, m.N, initialize=dict(((i,j),D[i,j]) for i in m.N for j in m.N))

这篇关于向量对矩阵乘积的热求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 07:41