-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveCplexCuts.jl
166 lines (135 loc) · 5.99 KB
/
SaveCplexCuts.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
module SaveCplexCuts
export save_cpx_cuts, solve_root_node, remove_const_from_obj, parse_save_cpx_cuts
using CPLEXW, JuMP, JSON
function solve_callback(env, cbdata, wherefrom, cbhandle, useraction_p) :: Int32
@info "inside solve_callback"
nodelp_p = [CPXLPptr(0)]
CPXgetcallbacknodelp(env, cbdata, wherefrom, nodelp_p);
CPXwriteprob(env, nodelp_p[1], "./cuts/rootnodemodel.mps", C_NULL);
return 0
end
c_solve_callback = @cfunction(solve_callback, Cint, (
CPXENVptr, # env
Ptr{Cvoid}, # cbdata
Cint, # wherefrom
Ptr{Cvoid}, # cbhandle
Ptr{Cint}, # useraction_p
))
"""
solve_root_node(file_name::String)
read a MPS file, solve at root node, and save the root node LP that has CPLEX cuts added.
Note that, the file_name should be discoverable by the program.
# Examples
```julia-repl
julia> solve_root_node('./test.MPS')
1
```
"""
function solve_root_node(file_name::String)
status_p = [Cint(0)]
env = CPXopenCPLEX(status_p)
CPXsetintparam(env, CPX_PARAM_SCRIND, CPX_ON) # Print to the screen
CPXsetdblparam(env, CPX_PARAM_NODELIM, 0) # MIP node limit
#CPXsetdblparam(env, CPX_PARAM_CUTSFACTOR, 0) # Disable cutting planes
#CPXsetintparam(env, CPX_PARAM_PREIND, 0) # Disable preprocessing
#CPXsetintparam(env, CPX_PARAM_HEURFREQ, -1) # Disable primal heuristics
lp = CPXcreateprob(env, status_p, "problem")
CPXreadcopyprob(env, lp, file_name, "mps")
CPXsetsolvecallbackfunc(env, c_solve_callback, C_NULL)
CPXmipopt(env, lp);
@info "Root node solution is done! "
end
"""
parse_save_cpx_cuts(file_name::String)
read the MPS file saved by solve_root_node(), parse the CPLEX cuts, and save cuts into JSON files.
# Examples
```julia-repl
julia> solve_root_node('./test.MPS')
1
```
"""
function parse_save_cpx_cuts(file_name::String)
m = JuMP.read_from_file("./cuts/rootnodemodel.mps");
@info "JuMP read $(file_name) successfully."
# write_to_file(m, string(input_file_path, inputfiles[1], ".lp");format = MOI.FileFormats.FORMAT_LP)
cons = all_constraints(m, GenericAffExpr{Float64,VariableRef}, MOI.LessThan{Float64});
#cons = [all_constraints(m, GenericAffExpr{Float64,VariableRef}, MOI.LessThan{Float64});all_constraints(m, GenericAffExpr{Float64,VariableRef}, MOI.EqualTo{Float64});all_constraints(m, GenericAffExpr{Float64,VariableRef}, MOI.GreaterThan{Float64})];
ib_json_dict = Dict{String, Any}("file name" => file_name)
mir_json_dict = Dict{String, Any}("file name" => file_name)
f_json_dict = Dict{String, Any}("file name" => file_name)
all_cuts_json_dict = Dict{String, Any}("file name" => file_name)
dir_name = "./cuts/"
ib_cut_file = open(string(dir_name,file_name,"_ib.json"), "w");
mir_cut_file = open(string(dir_name,file_name,"_mir.json"), "w");
f_cut_file = open(string(dir_name,file_name,"_f.json"), "w");
all_cut_file = open(string(dir_name,file_name,"_cuts.json"), "w");
cut_name_file = open(string(dir_name,"cut_names.txt"), "a");
all_cuts_dict = Dict{String, Any}()
@info "Start parsing cuts"
cnt_ib = 0; cnt_mir = 0; cnt_f =0; cnt_all = 0;
for conRef in cons
if name(conRef)[begin] != 'c' && isdigit(name(conRef)[begin+1])
write(cut_name_file, string(name(conRef),"\n"))
end
if name(conRef)[begin] in ['i','f','m', 'r','L','z','v'] && isdigit(name(conRef)[begin+1])
@info name(conRef)
c = constraint_object(conRef)
cut_name = name(conRef)
dict = Dict(name(key)=> val for (key,val) in c.func.terms)
dict["rhs"] = c.func.constant
cnt_all = cnt_all + 1
all_cuts_dict[cut_name] = dict;
if name(conRef)[begin] == 'i' && isdigit(name(conRef)[2]) # implied bound cuts
ib_json_dict[cut_name] = dict;
cnt_ib = cnt_ib + 1;
elseif name(conRef)[begin] == 'm' && isdigit(name(conRef)[2])
mir_json_dict[cut_name] = dict;
cnt_mir = cnt_mir + 1;
else name(conRef)[begin] == 'f' && isdigit(name(conRef)[2])
f_json_dict[cut_name] = dict;
cnt_f = cnt_f + 1
end
end
end
all_cuts_json_dict["cuts"] = all_cuts_dict;
@info "Finished parsing. $(cnt_ib) implied bound cuts; $(cnt_mir) MIR cuts; $(cnt_f) flow cover cuts."
write(ib_cut_file, JSON.json(ib_json_dict));
write(mir_cut_file, JSON.json(mir_json_dict));
write(f_cut_file, JSON.json(f_json_dict));
write(all_cut_file,JSON.json(all_cuts_json_dict))
close(ib_cut_file)
close(mir_cut_file)
close(f_cut_file)
close(all_cut_file)
close(cut_name_file)
end
"""
remove_const_from_obj(file_name::String)
After CPLEX solves the model at root node, it might result in an objective function with a constant term, which will cause an error when JuMP tries to read the MPS file. This function remove the line in MPS file to remove the constant term in the objective function.
# Examples
```julia-repl
julia> remove_const_from_obj("rootnodemodel.mps");
```
"""
function remove_const_from_obj(file_name::String)
data = open(io -> String(read(io)), file_name);
range = findfirst(r"RHS\n[^\n]*obj[^\n]*\n", data)
if range === nothing
@info "Objective function does not have a constrant."
return nothing
end
open(file_name, "w") do writer
write(writer, SubString(data,1:first(range)+3))
write(writer, SubString(data,last(range)+1:lastindex(data)))
end
@info "Objective function has a constant and it has been removed."
end
function save_cpx_cuts(file_name::String)
if !isdir("./cuts")
mkdir("cuts")
end
solve_root_node(file_name)
remove_const_from_obj("./cuts/rootnodemodel.mps")
parse_save_cpx_cuts(file_name)
end
end