如下构造数据
using LinearAlgebra
using Distributions
导入随机数生成包
using Random
系数矩阵的行数和列数
m = 100
n = 1024
信号的稀疏度
s = 10
稀疏信号的构造
xo = zeros(n,1);
pn = randperm(n);
inds = pn[1:s];
xo[inds] = randn(s,1);
系数矩阵的构造
A = randn(m,n)
ε = 1e-6
A .-= ε*mean(A, dims = 1); # this apparently is good practice, better conditioning
A ./= sqrt.(sum(abs2, A, dims = 1));
生成观察数据
b = A*xo;
然后构造建模优化和求解:
导入求解器
using Ipopt
构造一个模型对象
BP = Model(Ipopt.Optimizer)
set_optimizer_attribute(BP, “print_level”, 3)
设置变量
@variable(BP,bpx[1:n]);
设置目标函数
@NLexpression(BP,l1linf,(norm(bpx,1)/norm(x,2)+0.00001))
@NLobjective(BP,Min,l1linf);
设置约束条件
@constraint(BP, b .== A*bpx);
打印模型
print(BP)
求解
resBP = JuMP.optimize!(BP)
solution_summary(BP)
输出解
BPx = JuMP.value.(bpx)
绘制求解的解的图示
BPx = JuMP.value.(bpx)
绘制求解的解的图示
psolBP = plot(1:n,BPx,linewidth = 2,label=“BP_Ipopt”,shape=:star5,color=:green)
输出错误信息
@NLexpression表达式的部分:
Warning: Function norm automatically registered with 2 arguments.
│
│ Calling the function with a different number of arguments will result in an
│ error.
│
│ While you can safely ignore this warning, we recommend that you manually
│ register the function as follows:
│ ```Julia
│ model = Model()
│ register(model, :norm, 2, norm; autodiff = true)
bpx[1] … bpx[1024] in nonlinear expression. Nonlinear expressions may contain only scalar expressions.
这个为什么会出现错误呢,我的目标函数就是L1范数和L2范数之比,可是就是不能通过的,请问这是为什么呢?
非常感谢大家。