Skip to content

Commit b58b57f

Browse files
authored
fix all the warns. (#16)
* fix all the warns. * Fix according to comments in PR. * fix all the warns.
1 parent 0fb5f15 commit b58b57f

8 files changed

+39
-34
lines changed

src/OptimTestProblems.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ module OptimTestProblems
22

33
export MultivariateProblems, UnivariateProblems
44

5-
include("optim_tests/multivariate/multivariate.jl")
5+
6+
include("optim_tests/multivariate/MultivariateProblems.jl")
67
include("optim_tests/univariate/bounded.jl")
78

89
# Deprecation stuff

src/optim_tests/multivariate/multivariate.jl src/optim_tests/multivariate/MultivariateProblems.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module MultivariateProblems
22

3-
import Base.gradient
3+
import LinearAlgebra: dot
44

55
export UnconstrainedProblems
66

src/optim_tests/multivariate/constrained.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module ConstrainedProblems
22

3-
using ..OptimizationProblem,..ConstraintData
3+
import ..OptimizationProblem
4+
import ..ConstraintData
45

56
examples = Dict{AbstractString, OptimizationProblem}()
67

src/optim_tests/multivariate/more_testing.jl

+7-7
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function extrosenbrock_hessian!(storage,x,param)
5555
end
5656

5757
function _extrosenbrockproblem(N::Int;
58-
initial_x::AbstractArray{T} = repmat([-1.2,1],Int(N/2)),
58+
initial_x::AbstractArray{T} = repeat([-1.2,1]; inner = round(Int, N/2)),
5959
name::AbstractString = "Extended Rosenbrock ($N)") where T
6060
@assert mod(N,2) == 0
6161
OptimizationProblem(name,
@@ -65,7 +65,7 @@ function _extrosenbrockproblem(N::Int;
6565
extrosenbrock_hessian!,
6666
nothing, # Constraints
6767
initial_x,
68-
ones(initial_x),
68+
fill(T(1), length(initial_x)),
6969
zero(T),
7070
true,
7171
false,
@@ -148,7 +148,7 @@ function extpowell_hessian!(storage,x,param)
148148
end
149149

150150
function _extpowellproblem(N::Int;
151-
initial_x::AbstractArray{T} = repmat(float([3,-1,0,1]),Int(N/4)),
151+
initial_x::AbstractArray{T} = repeat(float([3,-1,0,1]); inner = round(Int, N/4)),
152152
name::AbstractString = "Extended Powell ($N)") where T
153153
@assert mod(N,4) == 0
154154
OptimizationProblem(name,
@@ -158,7 +158,7 @@ function _extpowellproblem(N::Int;
158158
extpowell_hessian!,
159159
nothing, # Constraints
160160
initial_x,
161-
zeros(initial_x),
161+
fill(zero(T), length(initial_x)),
162162
zero(T),
163163
true,
164164
false,
@@ -303,7 +303,7 @@ function _trigonometricproblem(N::Int;
303303
trigonometric_hessian!,
304304
nothing, # Constraints
305305
initial_x,
306-
zeros(initial_x),
306+
fill(T(0), length(initial_x)),
307307
zero(T),
308308
true,
309309
false,
@@ -333,7 +333,7 @@ examples["Trigonometric"] = _trigonometricproblem(100)
333333
sumsq_obj(f, x) = sum(f(x).^2)
334334

335335
function sumsq_gradient!(g::AbstractVector, f, J, x::AbstractVector)
336-
copy!(g, sum((2.0 .* f(x)) .* J(x), 1))
336+
copyto!(g, sum((2.0 .* f(x)) .* J(x), dims = 1))
337337
end
338338

339339
function sumsq_hessian!(h::AbstractMatrix, f, J, H, x::AbstractVector)
@@ -343,7 +343,7 @@ function sumsq_hessian!(h::AbstractMatrix, f, J, H, x::AbstractVector)
343343
for i = 1:length(fx)
344344
htmp += (2.0 * fx[i]) * H(x, i)
345345
end
346-
copy!(h, htmp)
346+
copyto!(h, htmp)
347347
end
348348

349349
const beale_y = [1.5, 2.25, 2.625]

src/optim_tests/multivariate/quad_transforms.jl

+11-15
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
function quad(x::Vector, param)
77
mat = param.mat
88
xt = x-param.vec
9-
return 0.5*vecdot(xt, mat*xt)
9+
return 0.5*dot(xt, mat*xt)
1010
end
1111

1212
function quad_gradient!(storage::Vector, x::Vector, param)
@@ -19,7 +19,7 @@ function quad_fun_gradient!(storage::Vector, x::Vector, param)
1919
mat = param.mat
2020
xt = x-param.vec
2121
storage .= mat*xt
22-
return 0.5*vecdot(xt, mat*xt)
22+
return 0.5*dot(xt, mat*xt)
2323
end
2424

2525
function quad_hessian!(storage::Matrix, x::Vector, param)
@@ -32,7 +32,7 @@ struct MatVecHolder{Tv <: AbstractVector,
3232
vec::Tv
3333
end
3434

35-
function _quadraticproblem(N::Int; mat::AbstractArray{T,2} = spdiagm(float(1:N)),
35+
function _quadraticproblem(N::Int; mat::AbstractArray{T,2} = sparse(Diagonal(float(1:N))),
3636
x0::AbstractVector{T} = ones(N),
3737
initial_x::AbstractVector{T} = zeros(N),
3838
name::AbstractString = "Quadratic Diagonal ($N)") where T <: Number
@@ -72,7 +72,7 @@ function paraboloid(x::AbstractArray, param::ParaboloidStruct)
7272
@. xt = x - param.vec
7373
xt[2:end] .-= param.alpha*xt[1]^2
7474

75-
return 0.5*vecdot(xt, mat*xt)
75+
return 0.5*dot(xt, mat*xt)
7676
end
7777

7878
function paraboloid_gradient!(storage::AbstractArray, x::AbstractArray, param::ParaboloidStruct)
@@ -97,14 +97,14 @@ function paraboloid_fun_gradient!(storage::AbstractArray, x::AbstractArray, para
9797
storage .= mat*xt
9898
storage[1] -= 2.0*param.alpha*xt[1]*sum(storage[2:end])
9999

100-
return 0.5*vecdot(xt, mat*xt)
100+
return 0.5*dot(xt, mat*xt)
101101
end
102102

103103
function paraboloid_hessian!(storage,x,param)
104104
error("Hessian not implemented for Paraboloid")
105105
end
106106

107-
function _paraboloidproblem(N::Int; mat::AbstractArray{T,2} = spdiagm(float(1:N)),
107+
function _paraboloidproblem(N::Int; mat::AbstractArray{T,2} = sparse(Diagonal(float(1:N))),
108108
x0::AbstractVector{T} = ones(N),
109109
initial_x::AbstractVector{T} = zeros(N),
110110
alpha::T = 10.0,
@@ -126,21 +126,17 @@ end
126126
examples["Paraboloid Diagonal"] = _paraboloidproblem(100)
127127

128128
function _randommatrix(N::Int, scaling::Bool=true)
129-
F = qrfact(randn(N,N))
129+
F = qr(randn(N,N))
130130
if scaling
131-
retval = F[:Q]'*spdiagm(float(1:N))*F[:Q]
131+
retval = F.Q'*sparse(Diagonal(float(1:N)))*F.Q
132132
else
133-
retval = F[:Q]'*F[:Q]
133+
retval = F.Q'*F.Q
134134
end
135135
retval
136136
end
137137

138-
# TODO: From Julia 0.7 onwards, we can use Base.Test.guardsrand() to restore the existing seed
139-
oldseed = copy(Base.GLOBAL_RNG) # Store current seed
140-
141-
srand(0)
138+
guardsrand(0) do
142139
examples["Paraboloid Random Matrix"] = _paraboloidproblem(100;
143140
name = "Paraboloid Random Matrix (100)",
144141
mat = _randommatrix(100))
145-
146-
copy!(Base.GLOBAL_RNG, oldseed) # Restore current seed
142+
end

src/optim_tests/multivariate/unconstrained.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
module UnconstrainedProblems
22

3-
using ..OptimizationProblem
3+
import ..OptimizationProblem
44

55
export OptimizationProblem
66

7+
import LinearAlgebra: Diagonal, qr, dot
8+
import SparseArrays: sparse
9+
import Test: guardsrand
10+
711
import ..objective, ..gradient, ..hessian
812

913
examples = Dict{AbstractString, OptimizationProblem}()

src/optim_tests/univariate/bounded.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ module UnivariateProblems
3737
examples["Problem13"] = UnivariateProblem("Problem13",
3838
p13,
3939
[0.001, 0.99],
40-
[1./sqrt(2.0),],
41-
[p13(1./sqrt(2.0)),])
40+
[1 ./ sqrt(2.0),],
41+
[p13(1 ./ sqrt(2.0)),])
4242

4343
# Problem 18 from [1]
4444
p18(x) = x <= 3.0 ? (x-2.0)^2 : 2.0*log(x-2.0)+1.0

test/runtests.jl

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using OptimTestProblems
22
using OptimTestProblems.MultivariateProblems
33

4+
import LinearAlgebra: norm
5+
46
using Test
57

68
verbose = false
@@ -35,14 +37,14 @@ end
3537
gs = similar(p.initial_x)
3638
g! = gradient(p)
3739
g!(gs, p.solutions)
38-
soltest && @test vecnorm(gs, Inf) < tol
40+
soltest && @test norm(gs, Inf) < tol
3941

4042
fg! = objective_gradient(p)
4143
fgs = similar(gs)
4244
g!(gs, p.initial_x)
4345

4446
@test fg!(fgs, p.initial_x) f(p.initial_x)
45-
@test vecnorm(fgs.-gs, Inf) < eps(eltype(gs))
47+
@test norm(fgs.-gs, Inf) < eps(eltype(gs))
4648
end
4749
end
4850

@@ -62,8 +64,9 @@ end
6264
@test all(p.solutions .< p.constraintdata.ux)
6365
end
6466

65-
if !isempty(p.constraintdata.lc)
66-
c = zeros(p.constraintdata.lc)
67+
lc = p.constraintdata.lc
68+
if !isempty(lc)
69+
c = fill(eltype(lc)(0.0), size(lc))
6770
p.constraintdata.c!(c, p.solutions)
6871
@test all(c .>= p.constraintdata.lc)
6972
@test all(c .<= p.constraintdata.uc)
@@ -73,13 +76,13 @@ end
7376
gs = similar(p.initial_x)
7477
g! = gradient(p)
7578
g!(gs, p.solutions)
76-
#soltest && @test vecnorm(gs, Inf) < tol
79+
#soltest && @test norm(gs, Inf) < tol
7780

7881
fg! = objective_gradient(p)
7982
fgs = similar(gs)
8083
g!(gs, p.initial_x)
8184

8285
@test fg!(fgs, p.initial_x) f(p.initial_x)
83-
@test vecnorm(fgs.-gs, Inf) < eps(eltype(gs))
86+
@test norm(fgs.-gs, Inf) < eps(eltype(gs))
8487
end
8588
end

0 commit comments

Comments
 (0)