I need to find the maximum minimum point of a function.
我需要找出一个函数的最大最小点。
There are 2 matrices M_A
, and M_B
, where M_A < M_B
and I > M_B
(element-wise comparison), where I
is the matrix of all ones.
有2个矩阵M_A和M_B,其中M_A
M_B(逐元素比较),其中I是所有1的矩阵。
I need to find the maximum x
value (between 0 and 1) such that:
我需要找到最大x值(介于0和1之间),以便:
x*M_A+(1-x)*I >= M_B
(element-wise comparison)
X*M_A+(1-x)*i>=M_B(逐个元素比较)
To do this I am trying to find the root of the following of the following expression x*M_A+(1-x)*I - M_B
. To do this, I define a scalar function by taking the sum of all negative entries of the above expression (x*M_A+(1-x)*I - M_B
). The objective is to find the x
value such that my_f(x)=0
. However, there are infinite x
s that satisfy this condition, for example my_f(0)=0
. I would like to get the maximum x
.
为此,我尝试寻找以下表达式x*M_A+(1-x)*i-M_B的根。为此,我通过获取上述表达式(x*M_A+(1-x)*i-M_B)的所有负项的和来定义标量函数。目标是找到使my_f(X)=0的x值。然而,有无限个X满足这个条件,例如my_f(0)=0。我想要得到最大的x。
def my_f(x):
matrix_comb = x*M_A + (1-x)*np.ones((n, n))
matrix_diff = matrix_comb - M_B
summ = 0
for i in range(0, len(matrix_diff)):
for j in range(0, len(matrix_diff)):
if matrix_diff[i,j] <= 0:
summ = summ + matrix_diff[i,j]
return -summ
my_f(scipy.optimize.fminbound(my_f, 0, 1))
The code returns 0.85. However, there are other greater x
values satisfying the conditions.
代码返回0.85。然而,还有其他更大的x值满足条件。
更多回答
When you say that M_A < M_B
, are you referring to the element-wise comparison? Also, what is M_V
? Why are you summing the negative values?
当你说M_A
What does x*M_A+(1-x)*I >= M_V
mean that you are performing the sum of the entries? I'm assuming M_V
is a matrix, in which case I don't know what the comparison is here and what it means for this to be satisfied.
X*M_A+(1-x)*i>=M_V是什么意思?我假设M_V是一个矩阵,在这种情况下,我不知道这里的比较是什么,以及它对满足这一点意味着什么。
And if you're trying to satisfy my_f(x)=0
, that is a root-finding problem, not a minimization problem. You seem to want to be doing both a maximization (to find the largest x
) and a root-finding problem (where x
is the root).
如果你试图满足my_f(X)=0,这是一个求根问题,而不是最小化问题。您似乎想要同时进行最大化(寻找最大的x)和求根问题(其中x是根)。
By M_A < M_B
I do mean element-wise comparison. I want the maximum x
satisfying the inequality x*M_A+(1-x)*I >= M_V
(element-wise comparison). Then, I look for the maximum x
satisfying the equality x*M_A+(1-x)*I = M_V
. Therefore, yes it's both maximization and root-finding problem
我所说的M_A=M_V(逐个元素比较)。然后,我寻找满足等式x*M_A+(1-x)*i=M_V的最大值x。因此,这既是一个极大化问题,也是一个求根问题
I can't help but notice that you never use M_B? Do you mean M_B every time you write M_V?
我不禁注意到你从来不用M_B?你是说你每次写M_V时都写M_B吗?
I interpret your question literally; that is, you really are looking for maximum x
subject to your inequality; I ignore everything after To do this I am.... as root-finding does not help with this goal.
我从字面上理解你的问题;那就是,你真的在寻找最大的x,受制于你的不平等;我忽略了之后的一切。因为寻根无助于这一目标。
import numpy as np
import scipy
rand = np.random.default_rng(seed=0)
m = 13
n = 7
M_A = rand.uniform(low=-1, high=1, size=(m, n))
M_B = rand.uniform(low=M_A, high=1, size=(m, n))
'''
Maximize x s.t.
0 <= x <= 1
x*M_A + (1-x)*ones >= M_B
x(MA - 1) >= MB - 1
'''
result = scipy.optimize.milp(
c=-1, # maximize
integrality=0, # continuous
bounds=scipy.optimize.Bounds(lb=0, ub=1),
constraints=scipy.optimize.LinearConstraint(
A=M_A.reshape((m*n, 1)) - 1,
lb=M_B.ravel() - 1,
)
)
assert result.success, result.message
print('x =', result.x)
更多回答
我是一名优秀的程序员,十分优秀!