Skip to content

Commit

Permalink
Merge pull request #119 from opencobra/develop
Browse files Browse the repository at this point in the history
CPLEX solver
  • Loading branch information
mtefagh authored Oct 21, 2022
2 parents adf125e + 49547d1 commit 301bd14
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 125 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "COBRA"
uuid = "58298e0b-d05c-52ec-a210-0694647ebfc7"
version = "0.4.1"
version = "0.4.2"

[deps]
CPLEX = "a076750e-1247-5638-91d2-ce28b192dca0"
Expand Down
126 changes: 2 additions & 124 deletions src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,128 +145,6 @@ function linprog(c, A, sense, b, l, u, solver)
)
end

#-------------------------------------------------------------------------------------------
"""
buildlp(c, A, sense, b, l, u, solver)
Function used to build a model using JuMP.
# INPUTS
- `c`: The objective vector, always in the sense of minimization
- `A`: Constraint matrix
- `sense`: Vector of constraint sense characters '<', '=', and '>'
- `b`: Right-hand side vector
- `l`: Vector of lower bounds on the variables
- `u`: Vector of upper bounds on the variables
- `solver`: A `::SolverConfig` object that contains a valid `handle`to the solver
# OUTPUTS
- `model`: An `::LPproblem` object that has been built using the JuMP.
- `x`: Primal solution vector
# EXAMPLES
```julia
julia> model, x = buildlp(c, A, sense, b, l, u, solver)
```
"""

function buildlp(c, A, sense, b, l, u, solver)
N = length(c)
model = Model(solver)
x = @variable(model, l[i] <= x[i=1:N] <= u[i])
@objective(model, Min, c' * x)
eq_rows, ge_rows, le_rows = sense .== '=', sense .== '>', sense .== '<'
@constraint(model, A[eq_rows, :] * x .== b[eq_rows])
@constraint(model, A[ge_rows, :] * x .>= b[ge_rows])
@constraint(model, A[le_rows, :] * x .<= b[le_rows])
return model, x, c
end

#-------------------------------------------------------------------------------------------
"""
solvelp(model, x)
Function used to solve a LPproblem using JuMP.
# INPUTS
- `model`: An `::LPproblem` object that has been built using the JuMP.
- `x`: Primal solution vector
# OUTPUTS
- `status`: Termination status
- `objval`: Optimal objective value
- `sol`: Primal solution vector
# EXAMPLES
```julia
julia> status, objval, sol = solvelp(model, x)
```
"""

function solvelp(model, x)
optimize!(model)
return (
status = termination_status(model),
objval = objective_value(model),
sol = value.(x)
)
end

#-------------------------------------------------------------------------------------------
"""
linprog(c, A, sense, b, l, u, solver)
Function used to build and solve a LPproblem using JuMP.
# INPUTS
- `c`: The objective vector, always in the sense of minimization
- `A`: Constraint matrix
- `sense`: Vector of constraint sense characters '<', '=', and '>'
- `b`: Right-hand side vector
- `l`: Vector of lower bounds on the variables
- `u`: Vector of upper bounds on the variables
- `solver`: A `::SolverConfig` object that contains a valid `handle`to the solver
# OUTPUTS
- `status`: Termination status
- `objval`: Optimal objective value
- `sol`: Primal solution vector
# EXAMPLES
```julia
julia> status, objval, sol = linprog(c, A, sense, b, l, u, solver)
```
"""

function linprog(c, A, sense, b, l, u, solver)
N = length(c)
model = Model(solver)
@variable(model, l[i] <= x[i=1:N] <= u[i])
@objective(model, Min, c' * x)
eq_rows, ge_rows, le_rows = sense .== '=', sense .== '>', sense .== '<'
@constraint(model, A[eq_rows, :] * x .== b[eq_rows])
@constraint(model, A[ge_rows, :] * x .>= b[ge_rows])
@constraint(model, A[le_rows, :] * x .<= b[le_rows])
optimize!(model)
return (
status = termination_status(model),
objval = objective_value(model),
sol = value.(x)
)
end

#-------------------------------------------------------------------------------------------
"""
buildCobraLP(model, solver)
Expand Down Expand Up @@ -360,7 +238,7 @@ function changeCobraSolver(name, params=[]; printLevel::Int=1)
if abs(printLevel) > 1
printLevel = 1
end
solver.handle = CplexSolver(CPX_PARAM_SCRIND=printLevel)
solver.handle = CPLEX.Optimizer
catch
error("The solver `CPLEX` cannot be set using `changeCobraSolver()`.")
end
Expand All @@ -373,7 +251,7 @@ function changeCobraSolver(name, params=[]; printLevel::Int=1)
solver.handle = GLPK.Optimizer
end
catch
error("The solver `GLPK` or `GLPKMathProgInterface` cannot be set using `changeCobraSolver()`.")
error("The solver `GLPK` cannot be set using `changeCobraSolver()`.")
end

elseif name == "Gurobi"
Expand Down

0 comments on commit 301bd14

Please sign in to comment.