main.jl 2.87 KB
Newer Older
1
include("utils.jl")
2

3
### ABS material properties
4
α_ABS = 63e-6
5
6
E_ABS = 2.5e9    # 1.19e9 - 2.9e9
σ̄ = 6e7          # mega
7
Tcutoff = 100 # temperature above which strain isn't happening
8
σ̄_ABS = [
9
10
11
    [σ̄       σ̄*0.5   σ̄*0.375]
    [σ̄*0.5   σ̄       σ̄*0.375]
    [σ̄*0.375 σ̄*0.375 σ̄*0.75 ]
12
    ]
13
14
15
16
17
ABS = material(α_ABS, E_ABS, σ̄_ABS, Tcutoff)
td = tempdecay(215, 25, 0.04) # extrusion temp, room temp, decay rate
tdslow = tempdecay(215, 25, 0.01)
tdfast = tempdecay(215, 25, 0.08)
# visualise_tempdecay(tdfast)
18

19

20
### LOAD IN DATA
21
obj = "/Users/jayant/phd/tempaware/models/" * "M5"
Jayant Khatkar's avatar
Jayant Khatkar committed
22
contours = clean_contour.(contour.(JSON.parse(open(obj * "contours.json"))))
23
cdata = contourdata(contours, 20, 5) # contour data
24
@time vd = voxdata(obj * "_voxels.csv", cdata)
25
#stress_multiplier!(vd.voxels, 10)
26
rl = random_rollout(cdata)
27

28
29
### CONSTRUCT COST FUNCTION GIVEN MODEL ADN MATERIAL
construct_cost(cdata, vd, ABS, td, :cost_f)
30
c = cost_f(rl)
31
32

# local search loop
33
k = 150 # TODO k is hardcoded in function below (uses global variable)
34
function best_neighbor!(rl::Vector{Int}, current_cost::Number)
35
    costs = Dict()
36
    for i in 1:1:length(rl)-1
37
        for j in i+1:min(i+k,length(rl)) # TODO length rl is constant
38
39
            if valid_swap(rl, i, j, cdata)
                swap!(rl, i, j)
40
                costs[i,j] = cost_f(rl) # TODO cost function global vairable being used here
41
42
43
44
45
                swap!(rl, i, j)
            end
        end
    end
    v, (i,j) = findmin(costs)
46
47
48
    if abs(v - current_cost) < current_cost/1e6
        return 0
    elseif v < current_cost
49
        swap!(rl, i, j)
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        return v
    end
    return 0
end


function local_search!(rl::Vector{Int}, max_iter::Int)
    cost_val = Inf
    for l in 1:max_iter
        c = best_neighbor!(rl, cost_val)
        if c  0
            cost_val = c
        else
            break
        end
65
    end
66
67
68
69
    return cost_val
end


70
# single local search
71
rl = random_rollout(cdata)
72
cost_f(rl)
73
max_iterations = 50
74
@time local_search!(rl, max_iterations)
75

76
# run many times
77
n_starts=10
78
79
opt_costs = zeros(n_starts)
start_costs = zeros(n_starts)
80
81
threads = zeros(n_starts)
@time Threads.@threads for i in 1:n_starts
82
83
84
    rl = random_rollout(cdata)
    start_costs[i] = cost_f(rl)
    opt_costs[i] = local_search!(rl, max_iterations)
85
    threads[i] = Threads.threadid()
Jayant Khatkar's avatar
Jayant Khatkar committed
86
end
87
88
89
90

@time for i in 1:n_starts
    my_costs[i] = local_search!(random_rollout(cdata), 5)
    threads[i] = Threads.threadid()
Jayant Khatkar's avatar
Jayant Khatkar committed
91
end
92
93

### Visualise distribution of stresses
94
95
96
97
data_stress = .( vd.voxels.Sx.^2 + vd.voxels.Sy.^2 + vd.voxels.Sz.^2)
data_strain = .(vd.voxels.Txy.^2 + vd.voxels.Tyz.^2 + vd.voxels.Txz.^2)
data = data_stress
histogram(data) # vast majority of voxels near 0 stress - can ignore
98
99
100
histogram!(sort(data, rev=true)[1:4000]) # vast majority of voxels near 0 stress - can ignore

### Visualise voxmap
101
102
103
plot(vd, 46, cdata)
plot(Vector(1:length(rl)), cdata)
plot(rl, cdata)