用scipy施加单调性。

发布于 2025-02-10 23:14:09 字数 1047 浏览 2 评论 0原文

我正在尝试最大程度地减少长度20的向量的函数,但是我想将解决方案限制为单调,即

x [1]< = x [2] [2] ...< = x [20]

我尝试使用以下方式使用此例程的“约束”来实现:

cons = tuple([{'type':'ineq', 'fun': lambda x: x[i]- x[i-1]} for i in range(1, len(node_vals))])

res = sp.optimize.minimize(localisation, b, args=(d), constraints = cons) #optimize

但是,即使初始猜测b是,我得到的结果也不是单调的优化器完全忽略了约束。可能出了什么问题?我还尝试将约束更改为x [i] ** 3 -x [i+1] ** 3使其“更平滑”,但它根本没有帮助。我的目标函数<代码>本地化是特征值问题的解决方案的组成部分,其参数已事先定义:

def localisation(node_vals, domain): #calculate localisation for solutions with piecewise linear grading

        f = piecewise(node_vals, domain) #create piecewise linear function using given values at nodes
        #plt.plot(domain, f(domain))
        
        M = diff_matrix(f(domain)) #differentiation matrix created from piecewise linear function
        m = np.concatenate(([0], get_solutions(M)[1][:, 0], [0]))
        
        integral = num_int(domain, m)
        
        return integral

I am trying to minimize a function of a vector of length 20, but I want to constrain the solution to be monotonic, i.e.

x[1] <= x[2]... <= x[20]

I have tried to implement this in the following way using "constraints" for this routine:

cons = tuple([{'type':'ineq', 'fun': lambda x: x[i]- x[i-1]} for i in range(1, len(node_vals))])

res = sp.optimize.minimize(localisation, b, args=(d), constraints = cons) #optimize

However, the results I get are not monotonic, even when the initial guess b is, it seems that the optimizer is completely ignoring the constraints. What could be going wrong? I have also tried changing the constraint to x[i]**3 - x[i+1]**3 to make it "smoother", but it didn't help at all. My objective function, localisation is the integral of solution to an eigenvalue problem whose parameters are defined beforehand:

def localisation(node_vals, domain): #calculate localisation for solutions with piecewise linear grading

        f = piecewise(node_vals, domain) #create piecewise linear function using given values at nodes
        #plt.plot(domain, f(domain))
        
        M = diff_matrix(f(domain)) #differentiation matrix created from piecewise linear function
        m = np.concatenate(([0], get_solutions(M)[1][:, 0], [0]))
        
        integral = num_int(domain, m)
        
        return integral

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风疏影 2025-02-17 23:14:09

您没有发布我们可以运行的最低可再现示例。但是,您是否尝试指定在Scipy中使用哪种优化算法?这样的事情:

res = sp.optimize.minimize(localisation, b, args=(d), constraints = cons, method=‘SLSQP’)

You didn’t post a minimum reproducible example that we can run. However, did you try to specify which optimization algorithm to use in SciPy? Something like this:

res = sp.optimize.minimize(localisation, b, args=(d), constraints = cons, method=‘SLSQP’)
月下伊人醉 2025-02-17 23:14:09

我遇到了一个非常相似的问题,但是单调性属性上有额外的上限和下限。我正在解决这样的问题(也许对您有帮助):

  1. 使用Scipy给出的信任区域约束算法。这为我们提供了一种处理矩阵曼纳中线性约束的方法:
  lb <= A.dot(x) <= ub

其中lb&amp;和ub是此约束问题的下部(上)边界,a是矩阵,代表线性约束问题。

  1. 矩阵的每一行a是一个线性术语,它定义了约束

  2. ,例如x [0]&lt; = x [1],那么这可以被转换为x [0] - x [1]&lt; = 0,它在线性约束矩阵a方面看起来像[1,-1 ,...],前提是上限向量在此级别上具有0个值(反之亦然,但无论哪种方式,至少具有下限或上限的一个,使此简单)

  3. 设置足够的这些不平等现象,同时将其中的几个合并到一个不平等可能会产生足够的矩阵来解决此问题。

希望这有点有帮助,它为我的问题做了工作。

I'm having a very similar problem but with additional upper and lower bounds on the monotonicity property. I'm tackling the problem like this (maybe it helps you):

  1. Using the Trust-Region Constrained Algorithm given by scipy. This provides us a way of dealing with linear constraints in a matrix-manner:
  lb <= A.dot(x) <= ub

where lb & and ub are the lower (upper) bounds of this constraint problem and A is the matrix, representing the linear constraint problem.

  1. every row of matrix A is a linear term which defines a constraint

  2. If, for example, x[0] <= x[1], then this can be transformed into x[0] - x[1] <= 0 which in terms of the linear constraint matrix A looks like this [1, -1,...], provided that the upper bound vector has a 0 value on this level of course (vice versa is also possible but either way, having at least one of both, lower or upper bound, makes this easy)

  3. Setting up enough of these inequalities and at the same time merging a couple of those into a single inequality may create a sufficient matrix to solve this.

Hope this helps a bit, It did the job for my problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文