Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ using JuMP, PiecewiseAffineApprox, HiGHS
m = Model(HiGHS.Optimizer)
@variable(m, x)
# Create a piecewise linear approximation to x^2 on the interval [-1, 1]
pwa = approx(x -> x[1]^2, [(-1, 1)], Convex(), MILP(optimizer = HiGHS.Optimizer, planes=5))
pwa = approx(x -> x^2, [(-1, 1)], Convex(), MILP(optimizer = HiGHS.Optimizer, planes=5))
# Add the pwa function to the model
z = pwaffine(m, x, pwa)
# Minimize
Expand All @@ -45,9 +45,9 @@ The following demonstrates how this can be achieved:
using PiecewiseAffineApprox, CairoMakie, HiGHS

x = LinRange(0, 1, 20)
f(x) = first(x)^2
f(x) = x^2
pwa = approx(f, [(0, 1)], Convex(), MILP(optimizer = HiGHS.Optimizer, planes = 3))
p = plot(x, f.(x), pwa)
p = Makie.plot(x, f.(x), pwa)

save("approx.svg", p)
```
Expand Down
2 changes: 1 addition & 1 deletion src/convexapprox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function _sample_uniform(f::Function, bbox::Vector{<:Tuple}, nsamples)
)
x = vec(collect(it))
end
y = [f(xx) for xx ∈ x]
y = [f(xx...) for xx ∈ x]
return FunctionEvaluations(x, y)
end

Expand Down
8 changes: 4 additions & 4 deletions test/test_kazda_li.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
end

# 2D
g(x) = x[1]^2 + x[2]^2
g(x, y) = x^2 + y^2
vals = PiecewiseAffineApprox._sample_uniform(g, [(-1, 1), (-1, 1)], 10)
pwa_red = approx(
vals,
Expand All @@ -29,7 +29,7 @@
@test evaluate(pwa_red, (0, 0)) ≈ 0.024 atol = 0.001

@testset "Nonconvex" begin
h(x) = sin(5 * x[1]) * (x[1]^2 + x[2]^2)
h(x, y) = sin(5 * x) * (x^2 + y^2)
vals = PiecewiseAffineApprox._sample_uniform(h, [(-1, 1), (-1, 1)], 10)

@test_throws ErrorException approx(
Expand Down Expand Up @@ -88,15 +88,15 @@ end
end

# 2D
g(x) = x[1]^2 + x[2]^2
g(x, y) = x^2 + y^2
vals = PiecewiseAffineApprox._sample_uniform(g, [(-1, 1), (-1, 1)], 10)
pwa_red =
approx(vals, Convex(), FullOrder(optimizer = optimizer, metric = :max))
@test length(pwa_red.planes) == 100
@test evaluate(pwa_red, (0, 0)) ≈ 0.024 atol = 0.001

@testset "Nonconvex" begin
h(x) = sin(5 * x[1]) * (x[1]^2 + x[2]^2)
h(x, y) = sin(5 * x) * (x^2 + y^2)
vals = PiecewiseAffineApprox._sample_uniform(h, [(-1, 1), (-1, 1)], 10)

@test_throws ErrorException approx(
Expand Down