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

WIP: A total rewrite #40

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
*.jl.cov
*.jl.*.cov
*.jl.mem
.DS_Store
7 changes: 4 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
Cbc = "≥ 0.6.0"
JuMP = "~0.19, ~0.20"
MathOptInterface = "~0.8, ~0.9"
JuMP = "~0.21"
MathOptInterface = "~0.9"
julia = "1"

[extras]
Cbc = "9961bab8-2fa3-5c5a-9d89-47fab24efd76"
Gurobi = "2e9cd046-0924-5485-92f1-d5272153d98b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Cbc"]
test = ["Test", "Cbc", "Gurobi"]
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# PiecewiseLinearOpt

A package for modeling optimization problems containing piecewise linear functions. Current support is for (the graphs of) continuous univariate functions.

This package is an accompaniment to a paper entitled [_Nonconvex piecewise linear functions: Advanced formulations and simple modeling tools_](https://arxiv.org/abs/1708.00050), by Joey Huchette and Juan Pablo Vielma.
Expand Down
86 changes: 83 additions & 3 deletions src/PiecewiseLinearOpt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,95 @@ __precompile__()

module PiecewiseLinearOpt

using JuMP
import JuMP
import MathOptInterface
const MOI = MathOptInterface
using LinearAlgebra
using Random

export PWLFunction, UnivariatePWLFunction, BivariatePWLFunction, piecewiselinear
export piecewiselinear

include("types.jl")
include("jump.jl")

mutable struct PWLData
counter::Int
PWLData() = new(0)
end

function initPWL!(m::JuMP.Model)
if !haskey(m.ext, :PWL)
m.ext[:PWL] = PWLData()
end
return nothing
end

const VarOrAff = Union{JuMP.VariableRef,JuMP.AffExpr}

include(joinpath("methods", "util.jl"))

export Incremental, LogarithmicEmbedding, LogarithmicIndependentBranching, NativeSOS2, ZigZagBinary, ZigZagInteger
include(joinpath("methods", "univariate", "incremental.jl"))
include(joinpath("methods", "univariate", "logarithmic_embedding.jl"))
include(joinpath("methods", "univariate", "logarithmic_independent_branching.jl"))
include(joinpath("methods", "univariate", "native_sos2.jl"))
include(joinpath("methods", "univariate", "zig_zag_binary.jl"))
include(joinpath("methods", "univariate", "zig_zag_integer.jl"))
# ConvexCombination has an SOS2 formulation, so defer this until after the
# multivariate formulations are defined
include(joinpath("methods", "univariate", "sos2_formulation_base.jl"))

# Consider the colloqial "log" to refer to the embedding formulation
const Logarithmic = LogarithmicEmbedding
export Logarithmic

export K1, NineStencil, OptimalIndendentBranching, OptimalTriangleSelection, SixStencil, UnionJack
include(joinpath("methods", "bivariate", "k1.jl"))
include(joinpath("methods", "bivariate", "nine_stencil.jl"))
include(joinpath("methods", "bivariate", "optimal_independent_branching.jl"))
include(joinpath("methods", "bivariate", "optimal_triangle_selection.jl"))
include(joinpath("methods", "bivariate", "six_stencil.jl"))
include(joinpath("methods", "bivariate", "union_jack.jl"))
include(joinpath("methods", "bivariate", "common.jl"))

export ConvexCombination, DisaggregatedLogarithmic, MultipleChoice, OptimalIndependentBranching, OptimalTriangleSelection
include(joinpath("methods", "multivariate", "convex_combination.jl"))
include(joinpath("methods", "multivariate", "disaggregated_logarithmic.jl"))
include(joinpath("methods", "multivariate", "multiple_choice.jl"))

function formulate_pwl!(model::JuMP.Model, input_vals::Vector{NTuple{D,VarOrAff}}, output_vals::Vector{NTuple{F,VarOrAff}}, pwl::PWLFunction, method::Method, direction::DIRECTION) where {D,F}
error("No support for a R^$D -> R^$F piecewise linear function using the $method method.")
end

_default_method(::Val{1}) = Logarithmic()
_default_method(::Val{2}) = SixStencil()
# _default_method(::Val) = MultipleChoice()

function piecewiselinear(model::JuMP.Model,
input_vars::NTuple{D,VarOrAff},
pwl::PWLFunction{D,F,SegmentPointRep{D,F}};
method::Method = _default_method(Val(D)),
direction::DIRECTION = Graph,
output_vars::Union{Nothing,NTuple{F,VarOrAff}} = nothing) where {D,F}
initPWL!(model)
counter = model.ext[:PWL].counter
counter += 1
model.ext[:PWL].counter = counter

if isempty(pwl.segments)
error(
"I don't know how to handle a piecewise linear function with no breakpoints."
)
end

output_lb = minimum(minimum(segment.output_vals) for segment in pwl.segments)
output_ub = maximum(maximum(segment.output_vals) for segment in pwl.segments)

if output_vars === nothing
output_vars = tuple(JuMP.@variable(model, [i in 1:F], lower_bound=output_lb[i], upper_bound=output_ub[i], base_name="y_$counter")...)
end

formulate_pwl!(model, input_vars, output_vars, pwl, method, direction)
return output_vars
end

end # module
Loading