Python 借助fmin最小化范数

Python 借助fmin最小化范数,python,optimization,minimization,Python,Optimization,Minimization,我想解决这个问题。我想研究一下这个问题,看看如果只允许0,1,2,。。。向量x中的非零值。我构造了一个特殊的矩阵a和向量b作为测试用例。但不知何故,输出没有任何意义。有人能帮我吗 import numpy as np from scipy.optimize import minimize from scipy.optimize import leastsq from scipy import optimize import itertools import math #this method

我想解决这个问题。我想研究一下这个问题,看看如果只允许0,1,2,。。。向量x中的非零值。我构造了一个特殊的矩阵a和向量b作为测试用例。但不知何故,输出没有任何意义。有人能帮我吗

import numpy as np
from scipy.optimize import minimize
from scipy.optimize import leastsq
from scipy import optimize
import itertools
import math

#this method is used for calculating all possible non-zero positions in the vector x
def power_set(input):
    # returns a list of all subsets of the list a
    if (len(input) == 0):
        return [[]]
    else:
        main_subset = [ ]
        for small_subset in power_set(input[1:]):
            main_subset += [small_subset]
            main_subset += [[input[0]] + small_subset]
        return main_subset

if __name__ == '__main__':
    #construct matrix A and vector b
    #I tried using a matrix A of a special form and a random vector b
    #For the test case just insert n=3, k=1
    n=int(input('Please enter the dimension n:'))
    k=int(input('Please enter the index of the 2-entry in the vector a:'))
    if(k>=n):
        print('k has to be smaller than n')
    
    def constructMatrixRandomly():  
        A_help=np.identity(n)
        a_transposed=np.ones((1,n))
        A=np.r_[a_transposed,A_help]
        A[0,k]=2
        w=np.random.rand(1,n)*100
        b=A.dot(w.transpose())
        return A,b
   #this method generates the matrix A and the vector b in the test case
    def constructMatrixForNEquals3():
        A_help=np.identity(3)
        a_transposed=np.ones((1,3))
        A=np.r_[a_transposed,A_help]
        A[0,1]=2
        b=[8,3,2,1]
        return A,b
    #the objective function 
    def func(x):
        x = x.reshape(n_tilde, 1)
        return ((np.dot(A_tilde, x)- b)**2).sum()

    A,b=constructMatrixForNEquals3() 
    l = range(0,n)
    possibilities = power_set(l)
    possibilities.remove([])
    possibilities.remove(range(0,n))
    possibilities.sort(key=len)
    best_elements_with_length = np.full((2,(n-1)), -1)
    best_elements_with_length[0, :] = range(1,n)
    print(A)
    print(b)
    print(possibilities)
#for every possible 0-combination in the vector x i want to compute the minimum of the given Problem and if it is worse than the one with one 0 component more I want to replace it with the previous one. 
    for i in possibilities: 
        A_tilde = A[:, i]
        n_tilde = len(i)
        length_of_elements = len(i)
        print(A_tilde)
        w = optimize.fmin(func, np.random.rand(n_tilde,1), full_output=True)
        if w[1] < best_elements_with_length[1, length_of_elements-1] or best_elements_with_length[1, length_of_elements-1]==-1: 
            best_elements_with_length[1, length_of_elements-1]=w[1] 
    print(best_elements_with_length)
    for i in range(1, n-1):
        if best_elements_with_length[1, i-1] <= best_elements_with_length[1, i]: 
            best_elements_with_length[1,i]=best_elements_with_length[1,i-1]
    print(best_elements_with_length)

    
    
将numpy导入为np
从scipy.optimize导入最小化
从scipy.optimize import leastsq
从scipy导入优化
进口itertools
输入数学
#此方法用于计算向量x中所有可能的非零位置
def电源设置(输入):
#返回列表a的所有子集的列表
如果(len(输入)==0):
返回[]]
其他:
主_子集=[]
对于幂_集合中的小_子集(输入[1:]):
主_子集+=[小_子集]
主_子集+=[[输入[0]]+小_子集]
返回主集合
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
#构造矩阵A和向量b
#我试着使用一个特殊形式的矩阵a和一个随机向量b
#对于测试用例,只需插入n=3,k=1
n=int(输入('请输入维度n:'))
k=int(输入('请在向量a中输入2项的索引:'))
如果(k>=n):
打印('k必须小于n')
def constructMatrixRandomy():
A_help=np.identity(n)
a_转置=np.one((1,n))
A=np.r\u[A\u转置,A\u帮助]
A[0,k]=2
w=np.随机随机随机数(1,n)*100
b=A.dot(w.transpose())
返回A,b
#该方法在测试用例中生成矩阵A和向量b
def constructMatrixForNEquals3():
A_help=np.identity(3)
a_转置=np.one((1,3))
A=np.r\u[A\u转置,A\u帮助]
A[0,1]=2
b=[8,3,2,1]
返回A,b
#目标函数
def func(x):
x=x.重塑(n_tilde,1)
返回((np.dot(A_tilde,x)-b)**2).sum()
A、 b=构造矩阵xfornequals3()
l=范围(0,n)
可能性=功率设置(l)
删除([])
可能性。移除(范围(0,n))
可能性排序(key=len)
最佳元素长度为np.full((2,(n-1)),-1)
长度为[0,:]=范围(1,n)的最佳元素
印刷品(A)
印刷品(b)
打印(可能性)
#对于向量x中的每一个可能的0-组合,我想计算给定问题的最小值,如果它比一个0分量的组合更差,我想用前一个替换它。
对于我来说,在可能性方面:
A_tilde=A[:,i]
n_tilde=len(i)
元素的长度=len(i)
打印(平铺)
w=optimize.fmin(func,np.random.rand(n_tilde,1),full_output=True)
如果w[1]<长度为[1,元素长度为-1]的最佳元素或长度为[1,元素长度为-1]的最佳元素==-1:
最佳元素,元素长度为[1,元素的长度为-1]=w[1]
打印(最佳元素长度)
对于范围(1,n-1)内的i:

如果最佳元素长度为[1,i-1],则使用MIQP(混合整数二次规划)解算器会更容易。然后您可以添加一个限制x中非零元素数量的约束。注意:scipy没有这样的解算器。