Skip to content

Commit b0b7e1c

Browse files
deal with maximization problems (#95)
1 parent 8565845 commit b0b7e1c

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/presolve/presolve.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ The `PresolvedQuadraticModel{T, S} <: AbstractQuadraticModel{T, S}` is located i
132132
psqm = stats_ps.solver_specific[:presolvedQM]
133133
134134
and should be used to call [`postsolve`](@ref).
135+
Maximization problems are transformed to minimization problems.
136+
If you need the objective of a presolved maximization problem, make sure to take the opposite of the objective of the presolved problem.
135137
136138
If the presolved problem has 0 variables, `stats_ps.solution` contains a solution of the primal problem,
137139
`stats_ps.multipliers` is a zero `SparseVector`, and, if we define
@@ -148,8 +150,7 @@ function presolve(
148150
kwargs...,
149151
) where {T <: Real, S, M1 <: SparseMatrixCOO, M2 <: SparseMatrixCOO}
150152
start_time = time()
151-
@assert qm.meta.minimize
152-
psqm = deepcopy(qm)
153+
psqm = copy_qm(qm)
153154
psdata = psqm.data
154155
c = psdata.c
155156
lvar, uvar = psqm.meta.lvar, psqm.meta.uvar

src/presolve/presolve_utils.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1+
function switch_H_to_max!(
2+
data::QPData{T, S, M1, M2},
3+
) where {T, S, M1 <: SparseMatrixCOO, M2 <: SparseMatrixCOO}
4+
data.H.vals .= .-data.H.vals
5+
end
6+
7+
function switch_H_to_max!(data::QPData)
8+
data.H = -data.H
9+
end
10+
11+
function copy_qm(qm::QuadraticModel{T, S}) where {T, S}
12+
data = deepcopy(qm.data)
13+
if !qm.meta.minimize
14+
switch_H_to_max!(data)
15+
data.c .= .-data.c
16+
data.c0 = -data.c0
17+
meta = NLPModelMeta{T, S}(
18+
qm.meta.nvar,
19+
lvar = qm.meta.lvar,
20+
uvar = qm.meta.uvar,
21+
ncon = qm.meta.ncon,
22+
lcon = qm.meta.lcon,
23+
ucon = qm.meta.ucon,
24+
nnzj = qm.meta.nnzj,
25+
nnzh = qm.meta.nnzh,
26+
lin = copy(qm.meta.lin),
27+
islp = qm.meta.islp,
28+
x0 = qm.meta.x0,
29+
y0 = qm.meta.y0,
30+
minimize = true,
31+
)
32+
else
33+
meta = deepcopy(qm.meta)
34+
end
35+
return QuadraticModel(meta, qm.counters, data)
36+
end
37+
138
function vec_cnt!(v_cnt::Vector{Int}, v)
239
for k = 1:length(v)
340
i = v[k]

0 commit comments

Comments
 (0)