mirror of
https://github.com/ANL-CEEESA/RELOG.git
synced 2025-12-05 23:38:52 -06:00
Improve error message when instance is infeasible
This commit is contained in:
@@ -72,9 +72,13 @@ function validate(json, schema)
|
||||
end
|
||||
|
||||
|
||||
function load(path::String)::Instance
|
||||
function parsefile(path::String)::Instance
|
||||
return RELOG.parse(JSON.parsefile(path))
|
||||
end
|
||||
|
||||
|
||||
function parse(json::Dict)::Instance
|
||||
basedir = dirname(@__FILE__)
|
||||
json = JSON.parsefile(path)
|
||||
json_schema = JSON.parsefile("$basedir/schemas/input.json")
|
||||
validate(json, Schema(json_schema))
|
||||
|
||||
@@ -167,7 +171,7 @@ function load(path::String)::Instance
|
||||
|
||||
# Capacities
|
||||
for (capacity_name, capacity_dict) in location_dict["capacities (tonne)"]
|
||||
push!(sizes, PlantSize(parse(Float64, capacity_name),
|
||||
push!(sizes, PlantSize(Base.parse(Float64, capacity_name),
|
||||
capacity_dict["variable operating cost (\$/tonne)"],
|
||||
capacity_dict["fixed operating cost (\$)"],
|
||||
capacity_dict["opening cost (\$)"]))
|
||||
|
||||
27
src/model.jl
27
src/model.jl
@@ -192,12 +192,12 @@ function create_process_node_constraints!(model::ManufacturingModel)
|
||||
end
|
||||
end
|
||||
|
||||
function solve(filename::String;
|
||||
milp_optimizer=optimizer_with_attributes(Cbc.Optimizer, "logLevel" => 0),
|
||||
lp_optimizer=optimizer_with_attributes(Clp.Optimizer, "LogLevel" => 0))
|
||||
default_milp_optimizer = optimizer_with_attributes(Cbc.Optimizer, "logLevel" => 0)
|
||||
default_lp_optimizer = optimizer_with_attributes(Clp.Optimizer, "LogLevel" => 0)
|
||||
|
||||
@info "Reading $filename..."
|
||||
instance = RELOG.load(filename)
|
||||
function solve(instance::Instance;
|
||||
milp_optimizer=default_milp_optimizer,
|
||||
lp_optimizer=default_lp_optimizer)
|
||||
|
||||
@info "Building graph..."
|
||||
graph = RELOG.build_graph(instance)
|
||||
@@ -213,6 +213,11 @@ function solve(filename::String;
|
||||
@info "Optimizing MILP..."
|
||||
JuMP.optimize!(model.mip)
|
||||
|
||||
if !has_values(model.mip)
|
||||
@warn "No solution available"
|
||||
return Dict()
|
||||
end
|
||||
|
||||
@info "Re-optimizing with integer variables fixed..."
|
||||
all_vars = JuMP.all_variables(model.mip)
|
||||
vals = Dict(var => JuMP.value(var) for var in all_vars)
|
||||
@@ -229,6 +234,18 @@ function solve(filename::String;
|
||||
return get_solution(model)
|
||||
end
|
||||
|
||||
function solve(filename::String;
|
||||
milp_optimizer=default_milp_optimizer,
|
||||
lp_optimizer=default_lp_optimizer)
|
||||
|
||||
@info "Reading $filename..."
|
||||
instance = RELOG.parsefile(filename)
|
||||
|
||||
return solve(instance,
|
||||
milp_optimizer=milp_optimizer,
|
||||
lp_optimizer=lp_optimizer)
|
||||
end
|
||||
|
||||
function get_solution(model::ManufacturingModel)
|
||||
mip, vars, eqs, graph, instance = model.mip, model.vars, model.eqs, model.graph, model.instance
|
||||
T = instance.time
|
||||
|
||||
@@ -6,7 +6,7 @@ using RELOG
|
||||
@testset "Graph" begin
|
||||
@testset "build_graph" begin
|
||||
basedir = dirname(@__FILE__)
|
||||
instance = RELOG.load("$basedir/../instances/s1.json")
|
||||
instance = RELOG.parsefile("$basedir/../instances/s1.json")
|
||||
graph = RELOG.build_graph(instance)
|
||||
process_node_by_location_name = Dict(n.location.location_name => n
|
||||
for n in graph.process_nodes)
|
||||
|
||||
@@ -6,7 +6,7 @@ using RELOG
|
||||
@testset "Instance" begin
|
||||
@testset "load" begin
|
||||
basedir = dirname(@__FILE__)
|
||||
instance = RELOG.load("$basedir/../instances/s1.json")
|
||||
instance = RELOG.parsefile("$basedir/../instances/s1.json")
|
||||
|
||||
centers = instance.collection_centers
|
||||
plants = instance.plants
|
||||
@@ -73,7 +73,7 @@ using RELOG
|
||||
end
|
||||
|
||||
@testset "validate timeseries" begin
|
||||
@test_throws String RELOG.load("fixtures/s1-wrong-length.json")
|
||||
@test_throws String RELOG.parsefile("fixtures/s1-wrong-length.json")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ using RELOG, Cbc, JuMP, Printf, JSON, MathOptInterface.FileFormats
|
||||
@testset "Model" begin
|
||||
@testset "build" begin
|
||||
basedir = dirname(@__FILE__)
|
||||
instance = RELOG.load("$basedir/../instances/s1.json")
|
||||
instance = RELOG.parsefile("$basedir/../instances/s1.json")
|
||||
graph = RELOG.build_graph(instance)
|
||||
model = RELOG.build_model(instance, graph, Cbc.Optimizer)
|
||||
set_optimizer_attribute(model.mip, "logLevel", 0)
|
||||
@@ -43,7 +43,6 @@ using RELOG, Cbc, JuMP, Printf, JSON, MathOptInterface.FileFormats
|
||||
|
||||
@testset "solve" begin
|
||||
solution = RELOG.solve("$(pwd())/../instances/s1.json")
|
||||
#JSON.print(stdout, solution, 4)
|
||||
|
||||
@test "Costs" in keys(solution)
|
||||
@test "Fixed operating (\$)" in keys(solution["Costs"])
|
||||
@@ -57,6 +56,15 @@ using RELOG, Cbc, JuMP, Printf, JSON, MathOptInterface.FileFormats
|
||||
@test "F3" in keys(solution["Plants"])
|
||||
@test "F4" in keys(solution["Plants"])
|
||||
end
|
||||
|
||||
@testset "infeasible solve" begin
|
||||
json = JSON.parsefile("$(pwd())/../instances/s1.json")
|
||||
for (location_name, location_dict) in json["products"]["P1"]["initial amounts"]
|
||||
location_dict["amount (tonne)"] *= 1000
|
||||
end
|
||||
RELOG.solve(RELOG.parse(json))
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user