Skip to content

Commit

Permalink
Finish implementing parser
Browse files Browse the repository at this point in the history
  • Loading branch information
iSoron committed Nov 30, 2023
1 parent 76b085e commit 4947ad1
Show file tree
Hide file tree
Showing 7 changed files with 456 additions and 179 deletions.
56 changes: 53 additions & 3 deletions src/instance/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function parse(json)::Instance

timeseries(x::Union{Nothing,Number}) = repeat([x], time_horizon)
timeseries(x::Array) = x
timeseries(d::OrderedDict) = OrderedDict(k => timeseries(v) for (k,v) in d)
timeseries(d::OrderedDict) = OrderedDict(k => timeseries(v) for (k, v) in d)

# Read products
products = Product[]
Expand Down Expand Up @@ -42,8 +42,8 @@ function parse(json)::Instance
outputs = [products_by_name[p] for p in cdict["outputs"]]
operating_cost = timeseries(cdict["operating cost (\$)"])
prod_dict(key, null_val) = OrderedDict(
p => [v === nothing ? null_val : v for v in timeseries(cdict[key][p.name])] for
p in outputs
p => [v === nothing ? null_val : v for v in timeseries(cdict[key][p.name])]
for p in outputs
)
fixed_output = prod_dict("fixed output (tonne)", 0.0)
var_output = prod_dict("variable output (tonne/tonne)", 0.0)
Expand All @@ -68,6 +68,54 @@ function parse(json)::Instance
centers_by_name[cname] = center
end

plants = Plant[]
plants_by_name = OrderedDict{String,Plant}()
for (pname, pdict) in json["plants"]
prod_dict(key; scale = 1.0, null_val = Inf) = OrderedDict{Product,Vector{Float64}}(
products_by_name[p] => [
v === nothing ? null_val : v * scale for v in timeseries(pdict[key][p])
] for p in keys(pdict[key])
)

latitude = pdict["latitude (deg)"]
longitude = pdict["longitude (deg)"]
input_mix = prod_dict("input mix (%)", scale = 0.01)
output = prod_dict("output (tonne)")
emissions = timeseries(pdict["processing emissions (tonne)"])
storage_cost = prod_dict("storage cost (\$/tonne)")
storage_limit = prod_dict("storage limit (tonne)")
disposal_cost = prod_dict("disposal cost (\$/tonne)")
disposal_limit = prod_dict("disposal limit (tonne)")
initial_capacity = pdict["initial capacity (tonne)"]
capacities = PlantCapacity[]
for cdict in pdict["capacities"]
size = cdict["size (tonne)"]
opening_cost = timeseries(cdict["opening cost (\$)"])
fix_operating_cost = timeseries(cdict["fixed operating cost (\$)"])
var_operating_cost = timeseries(cdict["variable operating cost (\$/tonne)"])
push!(
capacities,
PlantCapacity(; size, opening_cost, fix_operating_cost, var_operating_cost),
)
end

plant = Plant(;
latitude,
longitude,
input_mix,
output,
emissions,
storage_cost,
storage_limit,
disposal_cost,
disposal_limit,
capacities,
initial_capacity,
)
push!(plants, plant)
plants_by_name[pname] = plant
end

return Instance(;
time_horizon,
building_period,
Expand All @@ -76,5 +124,7 @@ function parse(json)::Instance
products_by_name,
centers,
centers_by_name,
plants,
plants_by_name,
)
end
23 changes: 23 additions & 0 deletions src/instance/structs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ Base.@kwdef struct Center
disposal_cost::OrderedDict{Product,Vector{Float64}}
end

Base.@kwdef struct PlantCapacity
size::Float64
opening_cost::Vector{Float64}
fix_operating_cost::Vector{Float64}
var_operating_cost::Vector{Float64}
end

Base.@kwdef struct Plant
latitude::Float64
longitude::Float64
input_mix::OrderedDict{Product,Vector{Float64}}
output::OrderedDict{Product,Vector{Float64}}
emissions::OrderedDict{String,Vector{Float64}}
storage_cost::OrderedDict{Product,Vector{Float64}}
storage_limit::OrderedDict{Product,Vector{Float64}}
disposal_cost::OrderedDict{Product,Vector{Float64}}
disposal_limit::OrderedDict{Product,Vector{Float64}}
capacities::Vector{PlantCapacity}
initial_capacity::Float64
end

Base.@kwdef struct Instance
building_period::Vector{Int}
centers_by_name::OrderedDict{String,Center}
Expand All @@ -29,4 +50,6 @@ Base.@kwdef struct Instance
products_by_name::OrderedDict{String,Product}
products::Vector{Product}
time_horizon::Int
plants::Vector{Plant}
plants_by_name::OrderedDict{String,Plant}
end
26 changes: 13 additions & 13 deletions test/fixtures/boat_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -54,31 +54,31 @@
"nail_factory = {\n",
" \"input\": None,\n",
" \"outputs\": [\"Nail\"],\n",
" \"fixed output (tonne)\": 1,\n",
" \"variable output (tonne/tonne)\": {},\n",
" \"fixed output (tonne)\": {\"Nail\": 1},\n",
" \"variable output (tonne/tonne)\": {\"Nail\": 0},\n",
" \"revenue ($/tonne)\": None,\n",
" \"collection cost ($/tonne)\": {\"Nail\": 1000},\n",
" \"operating cost ($)\": 0,\n",
" \"disposal limit (tonne)\": 1e5,\n",
" \"disposal limit (tonne)\": {\"Nail\": None},\n",
" \"disposal cost ($/tonne)\": {\"Nail\": 0},\n",
"}\n",
"\n",
"forest = {\n",
" \"input\": None,\n",
" \"outputs\": [\"Wood\"],\n",
" \"fixed output (tonne)\": 100,\n",
" \"variable output (tonne/tonne)\": {},\n",
" \"fixed output (tonne)\": {\"Wood\": 100},\n",
" \"variable output (tonne/tonne)\": {\"Wood\": 0},\n",
" \"revenue ($/tonne)\": None,\n",
" \"collection cost ($/tonne)\": {\"Wood\": 250},\n",
" \"operating cost ($)\": 0,\n",
" \"disposal limit (tonne)\": 1e5,\n",
" \"disposal limit (tonne)\": {\"Wood\": None},\n",
" \"disposal cost ($/tonne)\": {\"Wood\": 0},\n",
"}\n",
"\n",
"retail = {\n",
" \"input\": \"NewBoat\",\n",
" \"outputs\": [\"UsedBoat\"],\n",
" \"fixed output (tonne)\": 0,\n",
" \"fixed output (tonne)\": {\"UsedBoat\": 0},\n",
" \"variable output (tonne/tonne)\": {\"UsedBoat\": [0.10, 0.25, 0.10]},\n",
" \"revenue ($/tonne)\": 3_000,\n",
" \"collection cost ($/tonne)\": {\"UsedBoat\": 100},\n",
Expand Down Expand Up @@ -115,13 +115,13 @@
" },\n",
" \"capacities\": [\n",
" {\n",
" \"size\": 50,\n",
" \"size (tonne)\": 50,\n",
" \"opening cost ($)\": 10_000,\n",
" \"fixed operating cost ($)\": 1_000,\n",
" \"variable operating cost ($/tonne)\": 5,\n",
" },\n",
" {\n",
" \"size\": 500,\n",
" \"size (tonne)\": 500,\n",
" \"opening cost ($)\": 20_000,\n",
" \"fixed operating cost ($)\": 2_000,\n",
" \"variable operating cost ($/tonne)\": 5,\n",
Expand All @@ -147,13 +147,13 @@
" \"disposal limit (tonne)\": {\"Nail\": None, \"Wood\": None},\n",
" \"capacities\": [\n",
" {\n",
" \"size\": 50,\n",
" \"size (tonne)\": 50,\n",
" \"opening cost ($)\": 5_000,\n",
" \"fixed operating cost ($)\": 500,\n",
" \"variable operating cost ($/tonne)\": 2.5,\n",
" },\n",
" {\n",
" \"size\": 500,\n",
" \"size (tonne)\": 500,\n",
" \"opening cost ($)\": 10_000,\n",
" \n",
" \"fixed operating cost ($)\": 1_000,\n",
Expand All @@ -166,7 +166,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
Expand Down
Loading

0 comments on commit 4947ad1

Please sign in to comment.