本文介绍了CVXPY中的TypeError:float()参数必须是字符串或数字,而不是'Inequality'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍在玩CVXPY.这次我收到一个有趣的错误.让我们看看这个最小的代码

Still playing with CVXPY. This time I get an interesting error. Let us look at this minimal code

import cvxpy as cp
import numpy as np

A = np.random.normal(0, 1, (64,6))
b = np.random.normal(0, 1, (64,1))
theta = cp.Variable(shape = (6,1))

prob = cp.Problem(
    cp.Minimize(cp.max(A*theta -b) <= 5),
    [-10 <= theta, theta <= 10])

编译后,出现以下错误:

Once compiled, I get the following error:

〜\ Anaconda3 \ lib \ site-packages \ cvxpy \ interface \ numpy_interface \ ndarray_interface.py in const_to_matrix(self,value,convert_scalars) 48返回结果 其他49个: ---> 50返回result.astype(numpy.float64) 51 52#返回一个单位矩阵.

~\Anaconda3\lib\site-packages\cvxpy\interface\numpy_interface\ndarray_interface.py in const_to_matrix(self, value, convert_scalars) 48 return result 49 else: ---> 50 return result.astype(numpy.float64) 51 52 # Return an identity matrix.

TypeError:float()参数必须是字符串或数字,而不是'Inequality'

TypeError: float() argument must be a string or a number, not 'Inequality'

推荐答案

我不知道您想精确建模什么,但是这里有些有效:

I don't know what you want to model exactly, but here something which works:

import cvxpy as cp
import numpy as np

A = np.random.normal(0, 1, (64,6))
b = np.random.normal(0, 1, (64,1))
theta = cp.Variable(shape = (6,1))

prob = cp.Problem(
            cp.Minimize(cp.sum(theta)),  # what do you want to minimize?
            [
                cp.max(A*theta -b) <= 5,
                -10 <= theta,
                theta <= 10
            ]
        )

有效并且应该显示问题.

works and should show the problem.

我希望使用更干净的隐含形式,例如:

I would prefer a more clean impl like:

import cvxpy as cp
import numpy as np

A = np.random.normal(0, 1, (64,6))
b = np.random.normal(0, 1, (64,1))
theta = cp.Variable(shape = (6,1))

obj = cp.Minimize(cp.sum(theta))          # what do you want to minimize?
                                          # feasibility-problem? -> use hardcoded constant: cp.Minimize(0)
constraints = [
    cp.max(A*theta -b) <= 5,
    -10 <= theta,
    theta <= 10
]

prob = cp.Problem(obj, constraints)

原因:更容易准确地了解正在发生的事情.

The reason: it's easier to read out what's happening exactly.

您的问题:您的目标受到约束,这是不可能的.

Your problem: your objective has a constraint, which is impossible.

import cvxpy as cp
import numpy as np

A = np.random.normal(0, 1, (64,6))
b = np.random.normal(0, 1, (64,1))
theta = cp.Variable(shape = (6,1))

prob = cp.Problem(
cp.Minimize(cp.max(A*theta -b) <= 5),  # first argument = objective
                                       # -> minimize (constraint) : impossible!
    [-10 <= theta, theta <= 10])       # second argument = constraints
                                       # -> box-constraints

简而言之:

  • 想要以最小化功能
  • 要做使不平等最小化
  • you want to minimize a function
  • you do minimize an inequality

编辑

obj = cp.Minimize(cp.max(cp.abs(A*theta-b)))

小支票:

print((A*theta-b).shape)
(64, 1)
print((cp.abs(A*theta-b)).shape)
(64, 1)

元素级Abs:好

最后的外部max产生单个值,否则cp.Minimize将不接受它.好

The final outer max results in a single value, or else cp.Minimize won't accept it. good

编辑,或者让我们让cvxpy变得更快乐:

EDIT Or let's make cvxpy and us more happy:

obj = cp.Minimize(cp.norm(A*theta-b, "inf"))

这篇关于CVXPY中的TypeError:float()参数必须是字符串或数字,而不是'Inequality'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 18:21