Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Jayant Khatkar
tempaware
Commits
a67612f2
Commit
a67612f2
authored
Sep 23, 2021
by
Jayant Khatkar
Browse files
2x speed up of slowest part of cost function
parent
039bada7
Changes
1
Hide whitespace changes
Inline
Side-by-side
main.jl
View file @
a67612f2
...
...
@@ -6,6 +6,7 @@ using NearestNeighbors
using
Statistics
using
BenchmarkTools
using
Plots
using
LinearAlgebra
struct
material
...
...
@@ -34,6 +35,7 @@ struct voxmap
seglen
::
Vector
{
Float64
}
segoffset
::
Vector
{
Float64
}
segcontours
::
Vector
{
Int
}
c
::
Number
end
...
...
@@ -170,7 +172,7 @@ function voxmap(vox::Vector{Float64}, vox_d::Number, cdata::contourdata)
t_start
=
0
if
l
>
length
(
cdata
.
layers
)
return
voxmap
(
seglen
,
segoffset
,
segcontours
)
return
voxmap
(
seglen
,
segoffset
,
segcontours
,
0
)
end
for
cid
in
cdata
.
layers
[
l
]
...
...
@@ -328,7 +330,7 @@ function voxmap(vox::Vector{Float64}, vox_d::Number, cdata::contourdata)
# for those contours find exact segments
end
return
voxmap
(
seglen
,
segoffset
,
segcontours
)
return
voxmap
(
seglen
./
sum
(
seglen
)
,
segoffset
,
segcontours
,
Float64
(
segoffset
⋅
seglen
)
/
sum
(
seglen
)
)
end
...
...
@@ -522,21 +524,85 @@ function calc_cost(rollout::Vector{Int}, cdata::contourdata, vd::voxdata, mat::m
end
function
construct_cost
(
cdata
::
contourdata
,
vd
::
voxdata
,
mat
::
material
)
a
=
1
b
=
:
(
$
a
+
5
)
# using $ 'interpolates' literal expression into the 'quoted' expression
c
=
:
(
5
*
5
+
a
)
#function construct_cost(cdata::contourdata, vd::voxdata, mat::material)
# store contour time lengths inside function
contour_times
=
[
cdata
.
contours
[
c
]
.
time
[
end
]
for
c
in
1
:
length
(
cdata
.
contours
)]
d
=
:
(
$
b
+
$
c
)
#interpolate other expressions to form larger expressions
# considered voxels
not_empty_voxels
=
length
.
([
m
.
seglen
for
m
in
vd
.
maps
])
.>
0
considered_voxels
=
(
1
:
length
(
vd
.
below
))[(
vd
.
below
.!=
0
)
.&
not_empty_voxels
]
considered_voxels
=
considered_voxels
[
not_empty_voxels
[
vd
.
below
[
considered_voxels
]]]
ex1
=
:
(
i
*
2
)
# construct this according to voxels and contours
ex2
=
quote
function
cost_f
(
i
::
Int
)
return
$
ex1
# calculate stresses at each voxel
F
=
(
2
/
mat
.
σ̄
[
1
,
1
]
^
2
-
1
/
mat
.
σ̄
[
3
,
3
]
^
2
)
/
2
G
=
1
/
(
2
*
mat
.
σ̄
[
3
,
3
]
^
2
)
L
=
1
/
(
2
*
mat
.
σ̄
[
1
,
2
]
^
2
)
M
=
1
/
(
2
*
mat
.
σ̄
[
1
,
3
]
^
2
)
# voxtimes = [:(sum($(v.seglen).*($(v.segoffset) + timestart[$(v.segcontours)]))/$(sum(v.seglen))) for v in vd.maps]
# voxtimes = voxtimes[considered_voxels]
relmaps
=
vd
.
maps
[
considered_voxels
]
relbelows
=
vd
.
below
[
considered_voxels
]
rel_voxels
=
vd
.
voxels
[
considered_voxels
,
:
]
# trying to flatten to one equation
# n = length(relmaps)
# voxtimes = [:() for i in 1:length(vd.maps)]
# for i in 1:length(vd.maps)
# v = vd.maps[i]
# if length(v.segcontours) == 0
# continue
# end
# c = 0
# for j in 1:length(v.segcontours)
# if j == 1
# v_time = :(timestart[$(v.segcontours[j])] * $(v.seglen[j]/sum(v.seglen)))
# else
# v_time = :($v_time + timestart[$(v.segcontours[j])] * $(v.seglen[j]/sum(v.seglen)))
# end
# c += v.segoffset[j] *v.seglen[j]/sum(v.seglen)
# end
# v_time = :($v_time + $c)
# voxtimes[i] = v_time
# end
# voxtimes_expression = :[]
# voxtimes_expression.args = voxtimes
# replace!(x-> x==:() ? 0 : x, voxtimes_expression.args)
a
=
quote
function
cost_f
(
rl
::
Vector
{
Int
})
# println("Cumulative Sum") # 100k
timestart
=
cumsum
([
$
contour_times
[
c
]
for
c
in
rl
])
#println("Vox times") # 600
voxtimes
=
[
v
.
seglen
⋅
timestart
[
v
.
segcontours
]
+
v
.
c
for
v
in
vd
.
maps
]
# println("time diff") # 28k
Δt
=
voxtimes
[
considered_voxels
]
-
voxtimes
[
relbelows
]
#println("Temp diff") # 6k
ΔT
=
Tcutoff
.-
(
Tc
.+
$
(
T0
-
Tc
)
.*
ℯ
.^
(
-
k
.*
Δt
))
# println("Temp diff clean") # 82k
replace!
(
x
->
x
<
0
?
0
:
x
,
ΔT
)
# println("Stresses") # 6k
σ11
=
rel_voxels
.
Sx
+
$
(
mat
.
E
*
mat
.
α
)
*
ΔT
σ22
=
rel_voxels
.
Sy
+
$
(
mat
.
E
*
mat
.
α
)
*
ΔT
σ33
=
rel_voxels
.
Sz
σ12
=
rel_voxels
.
Txy
σ23
=
rel_voxels
.
Tyz
+
$
((
cdata
.
layer_height
/
vd
.
width
)
*
mat
.
E
*
mat
.
α
)
*
ΔT
σ31
=
rel_voxels
.
Txy
+
$
((
cdata
.
layer_height
/
vd
.
width
)
*
mat
.
E
*
mat
.
α
)
*
ΔT
cost
=
sum
(
$
F
*
(
σ11
-
σ22
)
.^
2
+
$
G
*
((
σ33
-
σ11
)
.^
2
+
(
σ33
-
σ22
)
.^
2
)
+
$
(
2
*
L
)
*
(
σ12
)
.^
2
+
$
(
2
*
M
)
*
(
σ23
+
σ31
)
.^
2
)
return
cost
end
end
retur
n
eval
(
ex2
)
# return the contructed functio
n
end
#
return
#
end
function
clean_contour
(
c
::
contour
)
# remove first element of array if second element is the same
...
...
@@ -559,13 +625,13 @@ end
# ABS material properties
α_ABS
=
10
0
# 78 - 108
E_ABS
=
2.5e9
# 1.19e9 - 2.9e9
σ̄
=
3
e7
α_ABS
=
10
e-6
#100 # 78 - 108 #10-6
E_ABS
=
2.5e9
# 1.19e9 - 2.9e9
σ̄
=
6
e7
# mega
σ̄_ABS
=
[
[
σ̄
σ̄
*
0.5
σ̄
*
0.5
]
[
σ̄
*
0.5
σ̄
σ̄
*
0.5
]
[
σ̄
*
0.5
σ̄
*
0.5
σ̄
*
0.75
]
[
σ̄
σ̄
*
0.5
σ̄
*
0.
37
5
]
[
σ̄
*
0.5
σ̄
σ̄
*
0.
37
5
]
[
σ̄
*
0.
37
5
σ̄
*
0.
37
5
σ̄
*
0.75
]
]
ABS
=
material
(
α_ABS
,
E_ABS
,
σ̄_ABS
)
...
...
@@ -573,11 +639,11 @@ ABS = material(α_ABS, E_ABS, σ̄_ABS)
obj
=
"/Users/jayant/phd/tempaware/"
*
"M1"
contours
=
clean_contour
.
(
contour
.
(
JSON
.
parse
(
open
(
obj
*
"contours.json"
))))
cdata
=
contourdata
(
contours
,
5
,
5
)
# contour data
vd
=
voxdata
(
obj
*
"_voxels.csv"
,
cdata
)
# TODO Need to debug
stress_multiplier!
(
vd
.
voxels
,
0.5
)
@time
vd
=
voxdata
(
obj
*
"_voxels.csv"
,
cdata
)
#
stress_multiplier!(vd.voxels, 0.5)
rl
=
random_rollout
(
cdata
)
@benchmark
rl
=
random_rollout
(
cdata
)
# 20/second
@benchmark
valid_swap
(
rl
,
rand
(
1
:
length
(
contours
)),
rand
(
1
:
length
(
contours
)),
cdata
)
# 1mil/second
#
@benchmark rl = random_rollout(cdata) # 20/second
#
@benchmark valid_swap(rl, rand(1:length(contours)), rand(1:length(contours)), cdata) # 1mil/second
calc_cost
(
rl
,
cdata
,
vd
,
ABS
)
# 500/second
...
...
@@ -586,7 +652,8 @@ o_rl = copy(rl)
n
=
length
(
rl
)
k
=
5
cost_val
=
Inf
for
l
in
1
:
10
o
=
0
@time
for
l
in
1
:
10
println
(
l
)
println
(
cost_val
)
costs
=
Dict
()
...
...
@@ -596,6 +663,7 @@ for l in 1:10
swap!
(
rl
,
i
,
j
)
costs
[
i
,
j
]
=
calc_cost
(
rl
,
cdata
,
vd
,
ABS
)
swap!
(
rl
,
i
,
j
)
o
+=
1
end
end
end
...
...
@@ -604,4 +672,5 @@ for l in 1:10
cost_val
=
v
swap!
(
rl
,
i
,
j
)
end
end
\ No newline at end of file
end
println
(
o
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment