From 76b085e10500f6e762a7f33329d3e5e5f8a9325f Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Thu, 30 Nov 2023 11:06:31 -0600 Subject: [PATCH] Parser: Allow scalars in timeseries fields --- src/instance/parse.jl | 16 ++++++++++------ test/fixtures/simple.json | 18 +++++++++--------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/instance/parse.jl b/src/instance/parse.jl index 5dc9a07..8e250c7 100644 --- a/src/instance/parse.jl +++ b/src/instance/parse.jl @@ -11,13 +11,17 @@ function parse(json)::Instance building_period = json["parameters"]["building period (years)"] distance_metric = json["parameters"]["distance metric"] + 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) + # Read products products = Product[] products_by_name = OrderedDict{String,Product}() for (pname, pdict) in json["products"] - tr_cost = pdict["transportation cost (\$/km/tonne)"] - tr_energy = pdict["transportation energy (J/km/tonne)"] - tr_emissions = pdict["transportation emissions (tonne/km/tonne)"] + tr_cost = timeseries(pdict["transportation cost (\$/km/tonne)"]) + tr_energy = timeseries(pdict["transportation energy (J/km/tonne)"]) + tr_emissions = timeseries(pdict["transportation emissions (tonne/km/tonne)"]) prod = Product(; name = pname, tr_cost, tr_energy, tr_emissions) push!(products, prod) products_by_name[pname] = prod @@ -33,12 +37,12 @@ function parse(json)::Instance revenue = [0.0 for t = 1:time_horizon] if cdict["input"] !== nothing input = products_by_name[cdict["input"]] - revenue = cdict["revenue (\$/tonne)"] + revenue = timeseries(cdict["revenue (\$/tonne)"]) end outputs = [products_by_name[p] for p in cdict["outputs"]] - operating_cost = cdict["operating cost (\$)"] + operating_cost = timeseries(cdict["operating cost (\$)"]) prod_dict(key, null_val) = OrderedDict( - p => [v === nothing ? null_val : v for v in cdict[key][p.name]] for + 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) diff --git a/test/fixtures/simple.json b/test/fixtures/simple.json index b299e5e..5781e9e 100644 --- a/test/fixtures/simple.json +++ b/test/fixtures/simple.json @@ -6,10 +6,10 @@ }, "products": { "P1": { - "transportation cost ($/km/tonne)": [0.015, 0.015, 0.015, 0.015], - "transportation energy (J/km/tonne)": [0.12, 0.12, 0.12, 0.12], + "transportation cost ($/km/tonne)": 0.015, + "transportation energy (J/km/tonne)": 0.12, "transportation emissions (tonne/km/tonne)": { - "CO2": [0.052, 0.052, 0.052, 0.052], + "CO2": 0.052, "CH4": [0.003, 0.003, 0.003, 0.003] } }, @@ -52,7 +52,7 @@ "P2": [0.12, 0.25, 0.12, 0.0], "P3": [0.25, 0.25, 0.25, 0.0] }, - "revenue ($/tonne)": [12.0, 12.0, 12.0, 12.0], + "revenue ($/tonne)": 12.0, "collection cost ($/tonne)": { "P2": [0.25, 0.25, 0.25, 0.25], "P3": [0.37, 0.37, 0.37, 0.37] @@ -73,21 +73,21 @@ "input": null, "outputs": ["P4"], "variable output (tonne/tonne)": { - "P4": [0, 0, 0, 0] + "P4": 0 }, "fixed output (tonne)": { "P4": [50, 60, 70, 80] }, "revenue ($/tonne)": null, "collection cost ($/tonne)": { - "P4": [0.25, 0.25, 0.25, 0.25] + "P4": 0.25 }, "operating cost ($)": [150.0, 150.0, 150.0, 150.0], "disposal limit (tonne)": { - "P4": [null, null, null, null] + "P4": null }, "disposal cost ($/tonne)": { - "P4": [0, 0, 0, 0] + "P4": 0 } }, "C3": { @@ -99,7 +99,7 @@ "constant output (tonne)": {}, "revenue ($/tonne)": [12.0, 12.0, 12.0, 12.0], "collection cost ($/tonne)": {}, - "operating cost ($)": [150.0, 150.0, 150.0, 150.0], + "operating cost ($)": 150.0, "disposal limit (tonne)": {}, "disposal cost ($/tonne)": {} }