mirror of
https://github.com/ANL-CEEESA/UnitCommitment.jl.git
synced 2025-12-06 08:18:51 -06:00
Merge pull request #1 from mtanneau/mt-nothing
Safer comparisons with nothing
This commit is contained in:
@@ -138,13 +138,13 @@ function from_json(json; fix=true)
|
|||||||
name_to_unit = Dict{String, Unit}()
|
name_to_unit = Dict{String, Unit}()
|
||||||
|
|
||||||
function timeseries(x; default=nothing)
|
function timeseries(x; default=nothing)
|
||||||
x != nothing || return default
|
x !== nothing || return default
|
||||||
x isa Array || return [x for t in 1:T]
|
x isa Array || return [x for t in 1:T]
|
||||||
return x
|
return x
|
||||||
end
|
end
|
||||||
|
|
||||||
function scalar(x; default=nothing)
|
function scalar(x; default=nothing)
|
||||||
x != nothing || return default
|
x !== nothing || return default
|
||||||
x
|
x
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -194,10 +194,10 @@ function from_json(json; fix=true)
|
|||||||
# Read and validate initial conditions
|
# Read and validate initial conditions
|
||||||
initial_power = scalar(dict["Initial power (MW)"], default=nothing)
|
initial_power = scalar(dict["Initial power (MW)"], default=nothing)
|
||||||
initial_status = scalar(dict["Initial status (h)"], default=nothing)
|
initial_status = scalar(dict["Initial status (h)"], default=nothing)
|
||||||
if initial_power == nothing
|
if initial_power === nothing
|
||||||
initial_status == nothing || error("unit $unit_name has initial status but no initial power")
|
initial_status === nothing || error("unit $unit_name has initial status but no initial power")
|
||||||
else
|
else
|
||||||
initial_status != nothing || error("unit $unit_name has initial power but no initial status")
|
initial_status !== nothing || error("unit $unit_name has initial power but no initial status")
|
||||||
initial_status != 0 || error("unit $unit_name has invalid initial status")
|
initial_status != 0 || error("unit $unit_name has invalid initial status")
|
||||||
if initial_status < 0 && initial_power > 1e-3
|
if initial_status < 0 && initial_power > 1e-3
|
||||||
error("unit $unit_name has invalid initial power")
|
error("unit $unit_name has invalid initial power")
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ function handle_message(logger::TimeLogger,
|
|||||||
print(time_string)
|
print(time_string)
|
||||||
println(message)
|
println(message)
|
||||||
end
|
end
|
||||||
if logger.file != nothing && level >= logger.io_log_level
|
if logger.file !== nothing && level >= logger.io_log_level
|
||||||
write(logger.file, time_string)
|
write(logger.file, time_string)
|
||||||
write(logger.file, message)
|
write(logger.file, message)
|
||||||
write(logger.file, "\n")
|
write(logger.file, "\n")
|
||||||
|
|||||||
16
src/model.jl
16
src/model.jl
@@ -45,11 +45,11 @@ function build_model(;
|
|||||||
variable_names::Bool=false,
|
variable_names::Bool=false,
|
||||||
) :: UnitCommitmentModel
|
) :: UnitCommitmentModel
|
||||||
|
|
||||||
if (filename == nothing) && (instance == nothing)
|
if (filename === nothing) && (instance === nothing)
|
||||||
error("Either filename or instance must be specified")
|
error("Either filename or instance must be specified")
|
||||||
end
|
end
|
||||||
|
|
||||||
if filename != nothing
|
if filename !== nothing
|
||||||
@info "Reading: $(filename)"
|
@info "Reading: $(filename)"
|
||||||
time_read = @elapsed begin
|
time_read = @elapsed begin
|
||||||
instance = UnitCommitment.read(filename)
|
instance = UnitCommitment.read(filename)
|
||||||
@@ -61,7 +61,7 @@ function build_model(;
|
|||||||
isf = zeros(0, 0)
|
isf = zeros(0, 0)
|
||||||
lodf = zeros(0, 0)
|
lodf = zeros(0, 0)
|
||||||
else
|
else
|
||||||
if isf == nothing
|
if isf === nothing
|
||||||
@info "Computing injection shift factors..."
|
@info "Computing injection shift factors..."
|
||||||
time_isf = @elapsed begin
|
time_isf = @elapsed begin
|
||||||
isf = UnitCommitment.injection_shift_factors(lines=instance.lines,
|
isf = UnitCommitment.injection_shift_factors(lines=instance.lines,
|
||||||
@@ -85,8 +85,8 @@ function build_model(;
|
|||||||
|
|
||||||
@info "Building model..."
|
@info "Building model..."
|
||||||
time_model = @elapsed begin
|
time_model = @elapsed begin
|
||||||
if model == nothing
|
if model === nothing
|
||||||
if optimizer == nothing
|
if optimizer === nothing
|
||||||
mip = Model()
|
mip = Model()
|
||||||
else
|
else
|
||||||
mip = Model(optimizer)
|
mip = Model(optimizer)
|
||||||
@@ -193,7 +193,7 @@ function add_unit!(model::UnitCommitmentModel, g::Unit)
|
|||||||
error("Partially must-run units are not currently supported")
|
error("Partially must-run units are not currently supported")
|
||||||
end
|
end
|
||||||
|
|
||||||
if g.initial_power == nothing || g.initial_status == nothing
|
if g.initial_power === nothing || g.initial_status === nothing
|
||||||
error("Initial conditions for $(g.name) must be provided")
|
error("Initial conditions for $(g.name) must be provided")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -416,7 +416,7 @@ function enforce_transmission(;
|
|||||||
instance, mip, vars = model.instance, model.mip, model.vars
|
instance, mip, vars = model.instance, model.mip, model.vars
|
||||||
limit::Float64 = 0.0
|
limit::Float64 = 0.0
|
||||||
|
|
||||||
if violation.outage_line == nothing
|
if violation.outage_line === nothing
|
||||||
limit = violation.monitored_line.normal_flow_limit[violation.time]
|
limit = violation.monitored_line.normal_flow_limit[violation.time]
|
||||||
@info @sprintf(" %8.3f MW overflow in %-5s time %3d (pre-contingency)",
|
@info @sprintf(" %8.3f MW overflow in %-5s time %3d (pre-contingency)",
|
||||||
violation.amount,
|
violation.amount,
|
||||||
@@ -439,7 +439,7 @@ function enforce_transmission(;
|
|||||||
@constraint(mip, flow <= limit + overflow)
|
@constraint(mip, flow <= limit + overflow)
|
||||||
@constraint(mip, -flow <= limit + overflow)
|
@constraint(mip, -flow <= limit + overflow)
|
||||||
|
|
||||||
if violation.outage_line == nothing
|
if violation.outage_line === nothing
|
||||||
@constraint(mip, flow == sum(vars.net_injection[b.name, violation.time] *
|
@constraint(mip, flow == sum(vars.net_injection[b.name, violation.time] *
|
||||||
isf[violation.monitored_line.offset, b.offset]
|
isf[violation.monitored_line.offset, b.offset]
|
||||||
for b in instance.buses
|
for b in instance.buses
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ using UnitCommitment, Cbc, JuMP
|
|||||||
|
|
||||||
# All units should have unknown initial conditions
|
# All units should have unknown initial conditions
|
||||||
for g in instance.units
|
for g in instance.units
|
||||||
@test g.initial_power == nothing
|
@test g.initial_power === nothing
|
||||||
@test g.initial_status == nothing
|
@test g.initial_status === nothing
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generate initial conditions
|
# Generate initial conditions
|
||||||
@@ -20,8 +20,8 @@ using UnitCommitment, Cbc, JuMP
|
|||||||
|
|
||||||
# All units should now have known initial conditions
|
# All units should now have known initial conditions
|
||||||
for g in instance.units
|
for g in instance.units
|
||||||
@test g.initial_power != nothing
|
@test g.initial_power !== nothing
|
||||||
@test g.initial_status != nothing
|
@test g.initial_status !== nothing
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: Check that initial conditions are feasible
|
# TODO: Check that initial conditions are feasible
|
||||||
|
|||||||
Reference in New Issue
Block a user