更新for循环内的全局Array {Float64,2}变量

时间:2019-07-06 13:56:34

标签: julia

我试图编写Julia程序来解决次梯度问题,并遇到以下有关如何更新初始点的问题,即以下代码中带圆圈的 x 变量。 >

using Random
using Convex, SCS
using Plots

Random.seed!(1234)

n = 20; m = 200
A = randn(m, n); b = rand(m,1); c = -A' * randn(m,1)

x_min = Variable(n)
problem = minimize(c' * x_min, [A * x_min <= b])
solve!(problem, SCSSolver())
f_min = problem.optval

f = []; push!(f, Inf)
fbest = []; push!(fbest, Inf)
fconstr = []

MAX_ITERS = 2500; EPS = 1e-3; 

#############################################
x = zeros(n, 1) # initialized here          
#############################################

for k in 1:MAX_ITERS
  fval, ind = findmax(A * x - b)
  if fval > 0
    push!(fbest, fbest[end])
    g = A[ind, :]'
    alpha_tmp = (fval+EPS) / norm(g)^2
  else
    push!(f, c' * x)
    push!(fbest, min( (c'*x)[1], fbest[end] ))
    g = c
    alpha_tmp = 1 / k
  end
  push!(fconstr, fval)

#############################################
  global x = x - g * alpha_tmp #update here
#############################################

  if rem(k, 100) == 0
    break
  end
end

被告知的错误正在发生

ERROR: LoadError: DimensionMismatch("dimensions must match")
Stacktrace:
 [1] promote_shape at ./indices.jl:154 [inlined]
 [2] promote_shape at ./indices.jl:145 [inlined]
 [3] -(::Array{Float64,2}, ::LinearAlgebra.Adjoint{Float64,Array{Float64,1}}) at ./arraymath.jl:38
 [4] top-level scope at julia_code/test.jl:40 [inlined]
 [5] top-level scope at ./none:0
in expression starting at julia_code/test.jl:25

0 个答案:

没有答案