Skip to content
Closed
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
3 changes: 1 addition & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1.5'
- '^1.6.0-0'
- '1.6'
- 'nightly'
os:
- ubuntu-latest
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.jl.cov
*.jl.*.cov
*.jl.mem
docs/build/
docs/build/
Manifest.toml
26 changes: 13 additions & 13 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
name = "IntervalConstraintProgramming"
uuid = "138f1668-1576-5ad7-91b9-7425abbf3153"
version = "0.12.2"

[compat]
IntervalArithmetic = "0.16, 0.17"
IntervalContractors = "0.4"
IntervalRootFinding = "0.5"
MacroTools = "0.4, 0.5"
ModelingToolkit = "3"
julia = "1.3, 1.4"
version = "0.13.0"

[deps]
IntervalArithmetic = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253"
IntervalContractors = "15111844-de3b-5229-b4ba-526f2f385dc9"
IntervalRootFinding = "d2bf35a9-74e0-55ec-b149-d360ff49b807"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
ReversePropagation = "527681c1-8309-4d3f-8790-caf822a419ba"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"

[compat]
IntervalArithmetic = "0.16, 0.17, 0.18, 0.19, 0.20"
IntervalContractors = "0.4"
MacroTools = "0.4, 0.5"
ReversePropagation = "0.1"
Symbolics = "1, 2, 3, 4"
julia = "1.6, 1.7"

[extras]
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "ModelingToolkit"]
test = ["Test", "Symbolics"]
135 changes: 135 additions & 0 deletions examples/basic_examples.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using Revise
using IntervalArithmetic
using IntervalConstraintProgramming
using Symbolics

IntervalArithmetic.configure!(directed_rounding=:fast, powers=:fast)

vars = @variables x, y


f(x, y) = x^2 + y^2

f(x, y)

ex = x^2 ≤ 1 - y^2

ex
typeof(ex)

@time S1 = Separator(ex, vars);

X = IntervalBox(-10..10, 2)
S1(X)

using BenchmarkTools

@btime $S1($X)



const plot_options = Dict(:ratio=>1, :leg=>false, :alpha=>0.5, :size=>(500, 300), :lw=>0.3)

function plot_paving!(p; kw...)
inner, boundary = p

plot!(inner; plot_options..., kw...)
plot!(boundary; plot_options..., kw...)
end


using Plots

ex2 = (x - 0.5)^2 + (y - 0.5)^2 <= 1

S2 = Separator(ex2, vars)

@constraint 1 <= x^2 + y^2 <= 3

p1 = pave(X, S1, 0.1)
p2 = pave(X, S2, 0.1)


S = S1 ∩ S2

p3 = pave(X, S, 0.1)

p = plot(; plot_options...)

plot_paving!(p1, lw=0)
plot_paving!(p2, lw=0)
plot_paving!(p3)

p4 = pave(IntervalBox(-3..3, 2), !(S), 0.1)

plot_paving!(p4; plot_options...)

typeof(S)

# SS = S1 ∩ (!S2);

SS = setdiff(S1, S2);


SS

p5 = pave(IntervalBox(-3..3, 2), SS, 0.1)

p = plot(; plot_options...)
plot_paving!(p5, lw=0)


SS = setdiff(S1, S2);


SS2 = symdiff(S1, S2)

p6 = pave(IntervalBox(-3..3, 2), SS2, 0.01)

p = plot(; plot_options...)
plot_paving!(p6, lw=0)

SS2




using ReversePropagation

vars = @variables x, y

ex = x^2 + y^2

ReversePropagation.forward_backward_contractor(ex, vars)

p7 = pave(X, Separator((y - 5) * cos(4 * sqrt( (x-4)^2 + y^2 )) > x * sin(2 * sqrt( x^2 +y^2 )), vars), 0.1)

plot(; plot_options...)
plot_paving!(p7)


separator(1 ≤ x^2 + y^2, vars)


constraint(x^2 + y^2 ≤ 1)


m = Model()

@constraint(m, x^2 + y^2 <= 1)


# models

m = Model()

vars = @variables x, y, z
add_variables!(m, vars)

add_constraint!(m, x^2 + y^2 ≤ 1)


c = constraint(m.constraints[1], variables(m))

X = IntervalBox(-3..3, 3)
c(X)
70 changes: 50 additions & 20 deletions src/IntervalConstraintProgramming.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,67 @@ __precompile__()
module IntervalConstraintProgramming

using IntervalArithmetic,
IntervalRootFinding,
IntervalContractors

using ModelingToolkit
using MacroTools
using Symbolics
using Symbolics: operation, value, arguments, toexpr, Sym

using Symbolics: @register

using ReversePropagation

# using MacroTools

import Base:
show, ∩, ∪, !, ⊆, setdiff
show, ∩, ∪, !, ⊆, setdiff, symdiff, &, |, ∈

import IntervalArithmetic: sqr, setindex


@register ¬(x)
@register x & y
@register x | y
@register x ∈ y::Interval
@register x ∨ y
@register x ∧ y

export
BasicContractor,
@contractor,
# BasicContractor,
# @contractor,
Contractor,
Separator, separator, @separator, @constraint,
@function,
SubPaving, Paving,
pave, refine!,
Vol,
show_code
Separator, #, separator, @separator, @constraint,
#@function,
#SubPaving, Paving,
pave, #, refine!,
# Vol,
# show_code
separator, constraint,
Model,
@constraint,
add_constraint!,
variables,
add_variables!,
ConstraintProblem

export ¬

const reverse_operations = IntervalContractors.reverse_operations

include("ast.jl")
include("code_generation.jl")
include("contractor.jl")
include("separator.jl")
include("paving.jl")
include("setinversion.jl")
include("volume.jl")
include("functions.jl")
# include("ast.jl")
# include("code_generation.jl")
# include("contractor.jl")
# include("separator.jl")
# include("paving.jl")
# include("setinversion.jl")
# include("volume.jl")
# include("functions.jl")


include("utils.jl")
include("new_contractor.jl")
include("set_operations.jl")
include("model.jl")
include("new_pave.jl")


end # module
107 changes: 107 additions & 0 deletions src/model.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@


struct Model
vars::Set{Sym{Real, Nothing}}
params
constraints
end

Model() = Model(Set([]), Set([]), [])

add_variable!(m::Model, v::Num) = add_variable!(m, value(v))
add_variable!(m::Model, v::Sym) = push!(m.vars, v)

function add_variables!(m::Model, vars)
for var in vars
add_variable!(m, var)
end
end

function add_constraint!(m::Model, s::Num)
# push!(m.vars, s.vars...)
push!(m.constraints, s)
end





variables(m::Model) = sort(collect(m.vars), lt = (x, y) -> x.name < y.name)


extract_vars(ex) = Symbol[]
extract_vars(ex::Symbol) = [ex]

function extract_vars(ex::Expr)
if ex.head == :call
reduce(vcat, extract_vars.(ex.args[2:end]))

else
reduce(vcat, extract_vars.(ex.args))
end
end

function add_constraint!(m, ex::Expr)
# @show m, ex

m2 = esc(m)

vars = extract_vars(ex)

@show vars

# create variables in global scope:
code = [:($(esc(v)) = Sym{Real}($(Meta.quot(v)))) for v in vars]

code2 = [:(push!($(m2).vars, $(esc(v)))) for v in vars]

@show code

quote
$(code...)
$(code2...)

push!($(m2).constraints, $(esc(ex)))
end

end

macro constraint(m, ex)
# @show m, ex

add_constraint!(m, ex)
end


# m = Model()
# @constraint(m, x^2 + y^2 <= 1)

# separator(m.constraints[1], sort(collect(m.vars), lt = (x, y) -> x.name < y.name))

# @constraint(m, z < 3)

# separator(m.constraints[1], sort(collect(m.vars), lt = (x, y) -> x.name < y.name))


# rename to ConstraintSatisfactionProblem?
struct ConstraintProblem
vars
constraint_expressions
constraints
end

function ConstraintProblem(vars, constraint_exprs)
constraints = constraint.(constraint_exprs, Ref(vars))

return ConstraintProblem(vars, constraint_exprs, constraints)
end

function ConstraintProblem(constraint_exprs)

vars = collect(reduce(∪, Symbolics.get_variables.(constraint_exprs)))

return ConstraintProblem(vars, constraint_exprs)
end



Loading