Skip to content

Commit f3b189d

Browse files
committed
up matrix2vec
1 parent 41816fe commit f3b189d

8 files changed

Lines changed: 146 additions & 56 deletions

File tree

profiling/interpolate.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
begin
2+
using Revise
3+
using CTModels
4+
5+
using JET
6+
using BenchmarkTools
7+
using Profile
8+
9+
function make_interpolation()
10+
11+
T = [0, 1]
12+
13+
A = [
14+
0 1
15+
2 3
16+
]
17+
18+
V = CTModels.matrix2vec(A, 1)
19+
20+
f = CTModels.ctinterpolate(T, V)
21+
22+
for x in LinRange(0, 1, 100)
23+
f(x)
24+
end
25+
26+
return f(0.5)
27+
end
28+
29+
let
30+
println("--------------------------------")
31+
println("Make interpolation")
32+
@code_warntype make_interpolation()
33+
end
34+
35+
# let
36+
# println("--------------------------------")
37+
# println("Make interpolation")
38+
# println(@report_opt make_interpolation())
39+
# end
40+
41+
let
42+
println("--------------------------------")
43+
println("Make interpolation")
44+
display(@benchmark make_interpolation())
45+
end
46+
47+
# let
48+
# println("--------------------------------")
49+
# println("Make interpolation")
50+
# @code_native debuginfo = :none dump_module = false make_interpolation()
51+
# end
52+
53+
end

src/default.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,11 @@ function __variable_components(q::Dimension, name::String)::Vector{String}
9494
return q > 1 ? [name * CTBase.ctindices(i) for i in range(1, q)] : [name]
9595
end
9696
end
97+
98+
"""
99+
$(TYPEDSIGNATURES)
100+
101+
Used to set the default value of the storage of elements in a matrix.
102+
The default value is `1`.
103+
"""
104+
__matrix_dimension_storage() = 1

src/solution.jl

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,26 @@ function build_solution(
7979
end
8080

8181
# variables: remove additional state for lagrange objective
82-
x = TX <: Function ? X : ctinterpolate(T, matrix2vec(X[:, 1:dim_x], 1))
83-
p = if TU <: Function
82+
x = if TX <: Function
83+
X
84+
else
85+
V = matrix2vec(X[:, 1:dim_x], 1)
86+
ctinterpolate(T, V)
87+
end
88+
p = if TP <: Function
8489
P
8590
elseif length(T) == 2
8691
t -> P[1, 1:dim_x]
8792
else
88-
ctinterpolate(T[1:(end - 1)], matrix2vec(P[:, 1:dim_x], 1))
93+
V = matrix2vec(P[:, 1:dim_x], 1)
94+
ctinterpolate(T[1:(end - 1)], V)
95+
end
96+
u = if TU <: Function
97+
U
98+
else
99+
V = matrix2vec(U[:, 1:dim_u], 1)
100+
ctinterpolate(T, V)
89101
end
90-
u = TP <: Function ? U : ctinterpolate(T, matrix2vec(U[:, 1:dim_u], 1))
91102

92103
# force scalar output when dimension is 1
93104
fx = (dim_x == 1) ? deepcopy(t -> x(t)[1]) : deepcopy(t -> x(t))
@@ -102,34 +113,44 @@ function build_solution(
102113
path_constraints_fun = if isnothing(path_constraints)
103114
nothing
104115
else
105-
t -> ctinterpolate(T, matrix2vec(path_constraints, 1))(t)
116+
V = matrix2vec(path_constraints, 1)
117+
t -> ctinterpolate(T, V)(t)
106118
end
119+
107120
path_constraints_dual_fun = if isnothing(path_constraints_dual)
108121
nothing
109122
else
110-
t -> ctinterpolate(T, matrix2vec(path_constraints_dual, 1))(t)
123+
V = matrix2vec(path_constraints_dual, 1)
124+
t -> ctinterpolate(T, V)(t)
111125
end
112126

113127
# box constraints multipliers
114128
state_constraints_lb_dual_fun = if isnothing(state_constraints_lb_dual)
115129
nothing
116130
else
117-
t -> ctinterpolate(T, matrix2vec(state_constraints_lb_dual[:, 1:dim_x], 1))(t)
131+
V = matrix2vec(state_constraints_lb_dual[:, 1:dim_x], 1)
132+
t -> ctinterpolate(T, V)(t)
118133
end
134+
119135
state_constraints_ub_dual_fun = if isnothing(state_constraints_ub_dual)
120136
nothing
121137
else
122-
t -> ctinterpolate(T, matrix2vec(state_constraints_ub_dual[:, 1:dim_x], 1))(t)
138+
V = matrix2vec(state_constraints_ub_dual[:, 1:dim_x], 1)
139+
t -> ctinterpolate(T, V)(t)
123140
end
141+
124142
control_constraints_lb_dual_fun = if isnothing(control_constraints_lb_dual)
125143
nothing
126144
else
127-
t -> ctinterpolate(T, matrix2vec(control_constraints_lb_dual[:, 1:dim_u], 1))(t)
145+
V = matrix2vec(control_constraints_lb_dual[:, 1:dim_u], 1)
146+
t -> ctinterpolate(T, V)(t)
128147
end
148+
129149
control_constraints_ub_dual_fun = if isnothing(control_constraints_ub_dual)
130150
nothing
131151
else
132-
t -> ctinterpolate(T, matrix2vec(control_constraints_ub_dual[:, 1:dim_u], 1))(t)
152+
V = matrix2vec(control_constraints_ub_dual[:, 1:dim_u], 1)
153+
t -> ctinterpolate(T, V)(t)
133154
end
134155

135156
# build Models

src/utils.jl

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,12 @@ end
1010
"""
1111
$(TYPEDSIGNATURES)
1212
13-
Transforms `x` to a Vector{<:Vector{<:ctNumber}}.
13+
Transforms `A` to a Vector{<:Vector{<:ctNumber}}.
1414
1515
**Note.** `dim` ∈ {1, 2} is the dimension along which the matrix is transformed.
1616
"""
1717
function matrix2vec(
18-
x::Matrix{<:ctNumber}, dim::Int=__matrix_dimension_stock()
18+
A::Matrix{<:ctNumber}, dim::Int=__matrix_dimension_storage()
1919
)::Vector{<:Vector{<:ctNumber}}
20-
m, n = size(x)
21-
y = nothing
22-
if dim == 1
23-
y = [x[1, :]]
24-
for i in 2:m
25-
y = vcat(y, [x[i, :]])
26-
end
27-
else
28-
y = [x[:, 1]]
29-
for j in 2:n
30-
y = vcat(y, [x[:, j]])
31-
end
32-
end
33-
return y
20+
return dim==1 ? [A[i,:] for i in 1:size(A,1)] : [A[:,i] for i in 1:size(A,2)]
3421
end

test/extras/plot_manual.jl

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -105,35 +105,35 @@ stopping = :Solve_Succeeded
105105
# Success: Bool
106106
success = true
107107

108-
# Path constraints: Matrix{Float64}
109-
path_constraints = zeros(0, 0)
108+
# # Path constraints: Matrix{Float64}
109+
# path_constraints = zeros(0, 0)
110110

111-
# Path constraints dual: Matrix{Float64}
112-
path_constraints_dual = zeros(0, 0)
111+
# # Path constraints dual: Matrix{Float64}
112+
# path_constraints_dual = zeros(0, 0)
113113

114-
# Boundary constraints: Vector{Float64}
115-
boundary_constraints = zeros(0)
114+
# # Boundary constraints: Vector{Float64}
115+
# boundary_constraints = zeros(0)
116116

117-
# Boundary constraints dual: Vector{Float64}
118-
boundary_constraints_dual = zeros(0)
117+
# # Boundary constraints dual: Vector{Float64}
118+
# boundary_constraints_dual = zeros(0)
119119

120-
# State constraints lower bound dual: Matrix{Float64}
121-
state_constraints_lb_dual = zeros(0, 0)
120+
# # State constraints lower bound dual: Matrix{Float64}
121+
# state_constraints_lb_dual = zeros(0, 0)
122122

123-
# State constraints upper bound dual: Matrix{Float64}
124-
state_constraints_ub_dual = zeros(0, 0)
123+
# # State constraints upper bound dual: Matrix{Float64}
124+
# state_constraints_ub_dual = zeros(0, 0)
125125

126-
# Control constraints lower bound dual: Matrix{Float64}
127-
control_constraints_lb_dual = zeros(0, 0)
126+
# # Control constraints lower bound dual: Matrix{Float64}
127+
# control_constraints_lb_dual = zeros(0, 0)
128128

129-
# Control constraints upper bound dual: Matrix{Float64}
130-
control_constraints_ub_dual = zeros(0, 0)
129+
# # Control constraints upper bound dual: Matrix{Float64}
130+
# control_constraints_ub_dual = zeros(0, 0)
131131

132-
# Variable constraints lower bound dual: Vector{Float64}
133-
variable_constraints_lb_dual = zeros(0)
132+
# # Variable constraints lower bound dual: Vector{Float64}
133+
# variable_constraints_lb_dual = zeros(0)
134134

135-
# Variable constraints upper bound dual: Vector{Float64}
136-
variable_constraints_ub_dual = zeros(0)
135+
# # Variable constraints upper bound dual: Vector{Float64}
136+
# variable_constraints_ub_dual = zeros(0)
137137

138138
# solution
139139
sol = CTModels.build_solution(
@@ -149,16 +149,16 @@ sol = CTModels.build_solution(
149149
message=message,
150150
stopping=stopping,
151151
success=success,
152-
path_constraints=path_constraints,
153-
path_constraints_dual=path_constraints_dual,
154-
boundary_constraints=boundary_constraints,
155-
boundary_constraints_dual=boundary_constraints_dual,
156-
state_constraints_lb_dual=state_constraints_lb_dual,
157-
state_constraints_ub_dual=state_constraints_ub_dual,
158-
control_constraints_lb_dual=control_constraints_lb_dual,
159-
control_constraints_ub_dual=control_constraints_ub_dual,
160-
variable_constraints_lb_dual=variable_constraints_lb_dual,
161-
variable_constraints_ub_dual=variable_constraints_ub_dual,
152+
# path_constraints=path_constraints,
153+
# path_constraints_dual=path_constraints_dual,
154+
# boundary_constraints=boundary_constraints,
155+
# boundary_constraints_dual=boundary_constraints_dual,
156+
# state_constraints_lb_dual=state_constraints_lb_dual,
157+
# state_constraints_ub_dual=state_constraints_ub_dual,
158+
# control_constraints_lb_dual=control_constraints_lb_dual,
159+
# control_constraints_ub_dual=control_constraints_ub_dual,
160+
# variable_constraints_lb_dual=variable_constraints_lb_dual,
161+
# variable_constraints_ub_dual=variable_constraints_ub_dual,
162162
)
163163

164164
#

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ include("solution_example.jl")
2525
:plot,
2626
:init,
2727
:export_import,
28+
:utils,
2829
)
2930
@testset "$(name)" begin
3031
test_name = Symbol(:test_, name)

test/solution_test.jld2

0 Bytes
Binary file not shown.

test/test_utils.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function test_utils()
2+
3+
A = [
4+
0 1
5+
2 3
6+
]
7+
8+
V = CTModels.matrix2vec(A)
9+
@test V[1] == [0, 1]
10+
@test V[2] == [2, 3]
11+
12+
V = CTModels.matrix2vec(A, 1)
13+
@test V[1] == [0, 1]
14+
@test V[2] == [2, 3]
15+
16+
W = CTModels.matrix2vec(A, 2)
17+
@test W[1] == [0, 2]
18+
@test W[2] == [1, 3]
19+
20+
end

0 commit comments

Comments
 (0)