Make distance metric configurable; fix building period bug

This commit is contained in:
2022-12-15 10:21:21 -06:00
parent 48bd3c403f
commit 841fbf16fb
9 changed files with 55 additions and 27 deletions

View File

@@ -40,7 +40,6 @@ function build_graph(instance::Instance)::Graph
end
# Build arcs from collection centers to plants, and from one plant to another
metric = _KnnDrivingDistance()
for source in [collection_shipping_nodes; plant_shipping_nodes]
for dest in process_nodes_by_input_product[source.product]
distance = _calculate_distance(
@@ -48,7 +47,7 @@ function build_graph(instance::Instance)::Graph
source.location.longitude,
dest.location.latitude,
dest.location.longitude,
metric,
instance.distance_metric,
)
values = Dict("distance" => distance)
arc = Arc(source, dest, values)

View File

@@ -6,19 +6,12 @@ using Geodesy
using NearestNeighbors
using DataFrames
Base.@kwdef mutable struct _KnnDrivingDistance
tree = nothing
ratios = nothing
end
mutable struct _EuclideanDistance end
function _calculate_distance(
source_lat,
source_lon,
dest_lat,
dest_lon,
::_EuclideanDistance,
::EuclideanDistance,
)::Float64
x = LLA(source_lat, source_lon, 0.0)
y = LLA(dest_lat, dest_lon, 0.0)
@@ -30,7 +23,7 @@ function _calculate_distance(
source_lon,
dest_lat,
dest_lon,
metric::_KnnDrivingDistance,
metric::KnnDrivingDistance,
)::Float64
if metric.tree === nothing
basedir = joinpath(dirname(@__FILE__), "..", "..", "data")
@@ -54,13 +47,8 @@ function _calculate_distance(
end
# Compute Euclidean distance
dist_euclidean = _calculate_distance(
source_lat,
source_lon,
dest_lat,
dest_lon,
_EuclideanDistance(),
)
dist_euclidean =
_calculate_distance(source_lat, source_lon, dest_lat, dest_lon, EuclideanDistance())
# Predict ratio
idxs, _ = knn(metric.tree, [source_lat, source_lon, dest_lat, dest_lon], 5)

View File

@@ -23,10 +23,23 @@ function parse(json)::Instance
validate(json, Schema(json_schema))
building_period = [1]
if "building period (years)" in keys(json)
building_period = json["building period (years)"]
if "building period (years)" in keys(json["parameters"])
building_period = json["parameters"]["building period (years)"]
end
distance_metric = EuclideanDistance()
if "distance metric" in keys(json["parameters"])
metric_name = json["parameters"]["distance metric"]
if metric_name == "driving"
distance_metric = KnnDrivingDistance()
elseif metric_name == "Euclidean"
# nop
else
error("Unknown distance metric: $metric_name")
end
end
@show distance_metric
plants = Plant[]
products = Product[]
collection_centers = CollectionCenter[]
@@ -176,5 +189,12 @@ function parse(json)::Instance
@info @sprintf("%12d collection centers", length(collection_centers))
@info @sprintf("%12d candidate plant locations", length(plants))
return Instance(T, products, collection_centers, plants, building_period)
return Instance(
T,
products,
collection_centers,
plants,
building_period,
distance_metric,
)
end

View File

@@ -48,10 +48,21 @@ mutable struct Plant
storage_cost::Vector{Float64}
end
abstract type DistanceMetric end
Base.@kwdef mutable struct KnnDrivingDistance <: DistanceMetric
tree = nothing
ratios = nothing
end
mutable struct EuclideanDistance <: DistanceMetric end
mutable struct Instance
time::Int64
products::Vector{Product}
collection_centers::Vector{CollectionCenter}
plants::Vector{Plant}
building_period::Vector{Int64}
distance_metric::DistanceMetric
end

View File

@@ -14,6 +14,9 @@
"properties": {
"time horizon (years)": {
"type": "number"
},
"distance metric": {
"type": "string"
}
},
"required": [