English 中文(简体)
问题涉及对沙尔清真寺最小功能的不正确反应
原标题:Problem with incorrect response of the minimize function of the scipy library in Python
  • 时间:2023-10-17 14:42:46
  •  标签:
  • python
  • scipy

为了计算电线,我在计算同时连接的电路时遇到了从最小功能中作出不正确反应的问题。 例如:

“parallel

Creating a matrix of connections and fill in the resistance matrix: Equation of the form: A*x = V ,where function sum(F * x**3) -> minimum

import numpy as np
from scipy.optimize import minimize, LinearConstraint

def CreateArrays(A,V):
    A = np.array(A) #matrix of connections
    V = np.array(V)
    buf = [0.1 for i in range(len(A[0]))]
    x0 = np.array(buf)
    return A,V,x0

def start(A,V,F):
    def obj_func(x):
        return np.sum(F * x**3)
    A,V,x0 = CreateArrays(A,V)
    lincon = LinearConstraint(A, lb=V, ub=V)
    
    res = minimize(obj_func, x0, method= trust-constr , constraints=lincon)
    return res.x
#Matrix of connections, vector V, vector F
I = start([[1, 0, 1], [0, -1, -1], [-1, 1, 0]], 
      [100, -100, 0],[1e-4, 1e-4, 8e-4])
print(I)

The result is [33.36666667 33.36666667 66.63333333] but it s a mistake; the true result is [66.63333333, 66.63333333, 33.36666667], where value of function is minimum. The higher the resistance, the lower the electric currents.

我发现这一法典最低:

f = [1e-4,1e-4,8e-4]
s=0

save = 0.5
x = [0.5,0.5,99.5]
for i in range(len(x)):
    s = s + f[i] * x[i]**3
mini = s                     #start minimum
print(mini)
j = 0.5
while j < 100:
    x = [j, j, 100-j]  
    s=0
    for i in range(len(x)):
        s = s + f[i] * x[i]**3 
    if s < mini:
        mini = s
        save = x  
    j =j +  0.5
print(mini,save)
问题回答

问题似乎在这里:

lincon = LinearConstraint(A, lb=V, ub=V)

这一制约因素似乎出现在SLSQP、COBYLA和信托公司。 SLSQP和COBYLA要么抱怨单体制约矩阵,要么(在信任的情况下)在找到解决办法后不取得进展。

我发现有两点帮助。

首先是使用更好的先头guesX0,首先采用服从这些限制的东西。

def CreateArrays(A,V):
    A = np.array(A) #matrix of connections
    V = np.array(V)
    x0 = np.linalg.lstsq(A, V, rcond=None)[0]
    return A,V,x0

第二件事是给一条线性限制室,让它要么被一丝不.带走。 最初,线性限制允许在0.001上有所变化,并且从那里减少。

def start(A,V,F):
    def obj_func(x):
        ret = np.sum(F * x**3)
        return ret
    A,V,x0 = CreateArrays(A,V)
    for eps in [1e-3, 1e-6, 1e-9, 0]:
        lincon = LinearConstraint(A, lb=V - eps, ub=V + eps)
        res = minimize(obj_func, x0, method= trust-constr , constraints=lincon)
        x0 = res.x
    return res.x




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签