Skip to content

Commit 9aba7c6

Browse files
committed
Also use temporary copy so that last change is applied.
1 parent 732312a commit 9aba7c6

1 file changed

Lines changed: 27 additions & 29 deletions

File tree

src/main/apply_geography.jl

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ and similarly for graph traversal costs `delta_tau`.
3535
param, graph = create_graph(init_parameters())
3636
geography = (z = 10*(rand(graph[:J]) .> 0.95), obstacles = [1 15; 70 72])
3737
updated_graph = apply_geography(graph, geography)
38+
39+
plot_graph(updated_graph, geography = geography, obstacles = true)
3840
```
3941
"""
4042
function apply_geography(graph, geography; kwargs...)
@@ -61,23 +63,17 @@ function apply_geography(graph, geography; kwargs...)
6163
end
6264

6365
op = dict_to_namedtuple(options)
64-
if haskey(geography, :z)
65-
z = geography[:z]
66-
else
67-
z = nothing
68-
end
69-
if haskey(geography, :obstacles)
70-
obstacles = geography[:obstacles]
71-
else
72-
obstacles = nothing
73-
end
74-
z_is_friction = haskey(geography, :z_is_friction) && geography.z_is_friction === true
66+
67+
z = haskey(geography, :z) ? geography[:z] : nothing
68+
obstacles = haskey(geography, :obstacles) ? geography[:obstacles] : nothing
69+
z_is_friction = haskey(geography, :z_is_friction) && geography[:z_is_friction] === true
7570

7671
# New graph object components
77-
delta_i_new = copy(graph.delta_i)
78-
delta_tau_new = copy(graph.delta_tau)
72+
delta_i_new = deepcopy(graph.delta_i)
73+
delta_tau_new = deepcopy(graph.delta_tau)
74+
7975
if obstacles !== nothing
80-
adjacency_new = copy(graph.adjacency)
76+
adjacency_new = deepcopy(graph.adjacency)
8177
nodes_new = deepcopy(graph.nodes)
8278
end
8379

@@ -95,6 +91,12 @@ function apply_geography(graph, geography; kwargs...)
9591

9692
# Remove edges where geographical barriers are (rivers)
9793
if obstacles !== nothing
94+
95+
# Store initial delta matrics (avoid double counting)
96+
delta_i_new_tmp = deepcopy(delta_i_new)
97+
delta_tau_new_tmp = deepcopy(delta_tau_new)
98+
99+
# Initialize flags for across and along obstacles
98100
sz = size(obstacles)[1]
99101
across_obstacle = falses(graph.J, graph.J)
100102
along_obstacle = falses(graph.J, graph.J)
@@ -134,21 +136,17 @@ function apply_geography(graph, geography; kwargs...)
134136
if rmj !== nothing
135137
deleteat!(nodes_new[i], rmj)
136138
end
137-
adjacency_new[i, j] = 0
138-
adjacency_new[j, i] = 0
139+
adjacency_new[i, j] = false
140+
adjacency_new[j, i] = false
139141
has_been_destroyed = true
140142
else
141-
# Or make it costly to cross: link could cross multiple obstacles, but we only allow one obstacle crossing to take effect
142-
if !across_obstacle[i, j]
143-
across_obstacle[i, j] = true
144-
delta_i_new[i, j] *= op.across_obstacle_delta_i
145-
delta_tau_new[i, j] *= op.across_obstacle_delta_tau
146-
end
147-
if !across_obstacle[j, i]
148-
across_obstacle[j, i] = true
149-
delta_i_new[j, i] *= op.across_obstacle_delta_i
150-
delta_tau_new[j, i] *= op.across_obstacle_delta_tau
151-
end
143+
# Or make it costly to cross
144+
across_obstacle[i, j] = true
145+
delta_i_new[i, j] = delta_i_new_tmp[i, j] * op.across_obstacle_delta_i
146+
delta_tau_new[i, j] = delta_tau_new_tmp[i, j] * op.across_obstacle_delta_tau
147+
across_obstacle[j, i] = true
148+
delta_i_new[j, i] = delta_i_new_tmp[j, i] * op.across_obstacle_delta_i
149+
delta_tau_new[j, i] = delta_tau_new_tmp[j, i] * op.across_obstacle_delta_tau
152150
end
153151
end
154152
end
@@ -169,8 +167,8 @@ function apply_geography(graph, geography; kwargs...)
169167
if rmjo !== nothing
170168
deleteat!(nodes_new[io], rmjo)
171169
end
172-
adjacency_new[io, jo] = 0
173-
adjacency_new[jo, io] = 0
170+
adjacency_new[io, jo] = false
171+
adjacency_new[jo, io] = false
174172
end
175173
else
176174
for (io, jo) in zip(obstacles[:, 1], obstacles[:, 2])

0 commit comments

Comments
 (0)