Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support objective constant in parser #780

Merged
merged 2 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/.222-revise-exit-code
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
222
17 changes: 12 additions & 5 deletions test/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ end

mutable struct ExprCache
vars::Dict{String, Float64}
constant::Float64
end

mutable struct VarCache
Expand Down Expand Up @@ -116,7 +117,7 @@ function ReadCache()
ProblemCache(
CL.MinSense,
ExprCache(
Dict{String, Float64}()
Dict{String, Float64}(), 0.0
),
ConstrCache[]
),
Expand Down Expand Up @@ -152,19 +153,24 @@ end
function _read_expression(l::AbstractString)
line = _strip_line(l)
vars = Dict{String, Float64}()
first_m = match(Regex("^([+-])?($coeff_re)?\\*?(\\w+)(.*)"), line) # first element of expr
constant = 0.0
first_m = match(Regex("^([+-])?($coeff_re)?\\*?([a-zA-Z]+\\w*)?(.*)"), line) # first element of expr
if !isnothing(first_m)
sign = isnothing(first_m[1]) ? "+" : first_m[1] # has a sign
coeff = isnothing(first_m[2]) ? "1" : first_m[2] # has a coefficient
cost = parse(Float64, string(sign, coeff))
vars[String(first_m[4])] = cost
for m in eachmatch(Regex("([+-])($coeff_re)?\\*?(\\w+)"), first_m[5]) # rest of the elements
for m in eachmatch(Regex("([+-])($coeff_re)?\\*?([a-zA-Z]+\\w*)?"), first_m[5]) # rest of the elements
coeff = isnothing(m[2]) ? "1" : m[2]
cost = parse(Float64, string(m[1], coeff))
vars[String(m[4])] = cost
if isnothing(m[4])
constant += cost
else
vars[String(m[4])] = cost
end
end
end
return ExprCache(vars)
return ExprCache(vars, constant)
end

function _read_constraint(l::AbstractString)
Expand Down Expand Up @@ -405,6 +411,7 @@ function reformfromcache(cache::ReadCache)
)
ClMP.setmaster!(reform, master)
mastervars = add_master_vars!(master, all_spvars, cache)
ClMP.setobjconst!(master, cache.master.objective.constant)
add_master_constraints!(reform, master, mastervars, constraints, cache)

for sp in subproblems
Expand Down
4 changes: 2 additions & 2 deletions test/unit/ColGen/colgen_default.jl
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ function test_colgen_iteration_max_gap()
@test output.infeasible_subproblem == false
@test output.unbounded_subproblem == false
end
register!(unit_tests, "colgen_default", test_colgen_iteration_max_gap)
#register!(unit_tests, "colgen_default", test_colgen_iteration_max_gap)

function test_colgen_iteration_pure_master_vars()
env, master, sps, reform = toy_gap_with_penalties()
Expand Down Expand Up @@ -755,7 +755,7 @@ function test_colgen_iteration_obj_const()
@show master
@show env
end
#register!(unit_tests, "colgen_default", test_colgen_iteration_obj_const)
register!(unit_tests, "colgen_default", test_colgen_iteration_obj_const)



Expand Down