Skip to content

Commit aab7506

Browse files
authored
Make ModelingToolkit optional - alternative (#210)
* .gitignore * fix header spaces in NEWS * remove redundant version in Project.toml * remove REQUIRE files * remove .DS_Store * use isnothing * minor formatting * remove IntervalRootFinding * remove undefined exports * prepare for making ModelingToolkit an optional dependency * make ModelingToolkit an optional dependency * use test/Project.toml * remove unbound arg * add coverage badge * outsource ModelingToolkit tests * add CI runs in older Julia versions
1 parent 4ddec09 commit aab7506

19 files changed

+279
-240
lines changed

.DS_Store

-6 KB
Binary file not shown.

.github/workflows/CI.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414
fail-fast: false
1515
matrix:
1616
version:
17+
- '1.3' # oldest Julia version that works
18+
- '1.8' # newest Julia version that works with ModelingToolkit
1719
- '1'
1820
- 'nightly'
1921
os:

.gitignore

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
*.jl.cov
22
*.jl.*.cov
33
*.jl.mem
4-
docs/build/
4+
docs/build/
5+
6+
*.DS_Store
7+
8+
coverage/
9+
10+
.vscode
11+
12+
Manifest.toml

NEWS.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# IntervalConstraintProgramming.jl
22

3-
#v0.11
3+
# v0.11
44
## Minimum Julia version
55
- The minimum Julia version supported is now Julia 1.1
66

7-
##Functionality's are added
7+
## Functionality's are added
88
- Contractor can be make by just function name only
99
- New type of Contractor named as `BasicContractor` can be construct which only contain fields of useful data.
1010

11-
11+
1212
# v0.10
1313
## Minimum Julia version
1414
- The minimum Julia version supported is now Julia 1.0.
@@ -17,6 +17,7 @@
1717

1818
- By the help of `ModelingToolkit.jl` we can construct contractors and separators without the use of macros.
1919

20+
2021
# v0.9
2122
## Minimum Julia version
2223
- The minimum Julia version supported is now Julia 0.7. The package is fully compatible with Julia 1.0.
@@ -29,12 +30,14 @@
2930
## Minimum Julia version
3031
- The minimum Julia version required has been bumped to 0.6; this will be the last release to support 0.6.
3132

33+
3234
# v0.7
3335

3436
## New dependency: `IntervalContractors.jl`
3537

3638
The reverse functions used for constraint propagation have been factored out into the `IntervalContractors.jl` package.
3739

40+
3841
# v0.6
3942
## Minimum Julia version
4043
- The minimum Julia version required has been bumped to 0.5
@@ -45,6 +48,7 @@ The reverse functions used for constraint propagation have been factored out int
4548
## Dependency change
4649
- The dependency on `ValidatedNumerics.jl` has been replaced by `IntervalArithmetic.jl` and `IntervalRootFinding.jl`
4750

51+
4852
# v0.5
4953
- API change: Contractors now have their dimension as a type parameter
5054
- Refactoring for type stability
@@ -53,6 +57,7 @@ The reverse functions used for constraint propagation have been factored out int
5357
- Generated code uses simpler symbols
5458
- Example notebooks have been split out into a separate repository: https://github.com/dpsanders/IntervalConstraintProgrammingNotebooks
5559

60+
5661
# v0.4
5762
- `@function f(x) = 4x` defines a function
5863
- Functions may be used inside constraints
@@ -84,6 +89,7 @@ C = @constraint (x-$a)^2 + (y-$b)^2
8489
The constraint will *not* change if the constants are changed, but may be
8590
updated (changed) by calling the same `@constraint` command again.
8691

92+
8793
# v0.2
8894
- `setinverse` now returns an object of type `Paving` [#17](https://github.com/dpsanders/IntervalConstraintProgramming.jl/pull/17)
8995

@@ -95,7 +101,7 @@ updated (changed) by calling the same `@constraint` command again.
95101
to eliminate collisions with user-defined variables [#20](https://github.com/dpsanders/IntervalConstraintProgramming.jl/pull/20)
96102

97103

98-
## v0.1.1
104+
# v0.1.1
99105
- Add `sqrtRev` reverse-mode function
100106

101107
- Add solid torus example, including 3D visualization with GLVisualize

Project.toml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,12 @@ version = "0.12.4"
55
[deps]
66
IntervalArithmetic = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253"
77
IntervalContractors = "15111844-de3b-5229-b4ba-526f2f385dc9"
8-
IntervalRootFinding = "d2bf35a9-74e0-55ec-b149-d360ff49b807"
98
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
10-
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
9+
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1110

1211
[compat]
1312
IntervalArithmetic = "0.16, 0.17, 0.18, 0.19, 0.20"
1413
IntervalContractors = "0.4"
15-
IntervalRootFinding = "0.5"
1614
MacroTools = "0.4, 0.5"
17-
ModelingToolkit = "3"
18-
julia = "1.3, 1.4"
19-
20-
[extras]
21-
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
22-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
23-
24-
[targets]
25-
test = ["Test", "ModelingToolkit"]
15+
Requires = "0.5, 1"
16+
julia = "1.3"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[![Build Status](https://github.com/JuliaIntervals/IntervalConstraintProgramming.jl/workflows/CI/badge.svg)](https://github.com/JuliaIntervals/IntervalConstraintProgramming.jl/actions/workflows/CI.yml)
44
[![Docs](https://img.shields.io/badge/docs-stable-blue.svg)](https://juliaintervals.github.io/pages/packages/intervalconstraintprogramming/)
5+
[![coverage](https://codecov.io/gh/JuliaIntervals/IntervalConstraintProgramming.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaIntervals/IntervalConstraintProgramming.jl)
56

67
This Julia package allows us to specify a set of constraints on real-valued variables,
78
given by inequalities, and

REQUIRE

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/IntervalConstraintProgramming.jl

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ __precompile__()
22

33
module IntervalConstraintProgramming
44

5-
using IntervalArithmetic,
6-
IntervalRootFinding,
7-
IntervalContractors
8-
9-
using ModelingToolkit
5+
using IntervalArithmetic,
6+
IntervalContractors
7+
using Requires
108
using MacroTools
119

1210
import Base:
@@ -18,12 +16,11 @@ export
1816
BasicContractor,
1917
@contractor,
2018
Contractor,
21-
Separator, separator, @separator, @constraint,
19+
Separator, @constraint,
2220
@function,
2321
SubPaving, Paving,
24-
pave, refine!,
25-
Vol,
26-
show_code
22+
pave,
23+
Vol
2724

2825
const reverse_operations = IntervalContractors.reverse_operations
2926

@@ -35,5 +32,6 @@ include("paving.jl")
3532
include("setinversion.jl")
3633
include("volume.jl")
3734
include("functions.jl")
35+
include("init.jl")
3836

3937
end # module

src/ast.jl

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,39 @@ Returns the variable at the top of the current piece of the tree."""
116116

117117
# TODO: Parameters
118118

119-
# numbers:
120-
function flatten!(flatAST::FlatAST, ex::ModelingToolkit.Constant, var)
121-
return ex.value # nothing to do the AST; return the number
119+
function _load_MT_flatten()
120+
return quote
121+
# numbers:
122+
function flatten!(flatAST::FlatAST, ex::Constant, var)
123+
return ex.value # nothing to do the AST; return the number
124+
end
125+
126+
function flatten!(flatAST::FlatAST, ex::Variable, var) # symbols are leaves
127+
if isempty(var)
128+
add_variable!(flatAST, Symbol(ex)) # add the discovered symbol as an input variable
129+
end
130+
return Symbol(ex)
131+
end
132+
133+
function flatten!(flatAST::FlatAST, ex::Operation, var)
134+
# top = process_operation!(flatAST, ex, var)
135+
# set_top!(flatAST, top)
136+
137+
if ex.op isa Variable
138+
return flatten!(flatAST, ex.op, var)
139+
else
140+
top = process_operation!(flatAST, ex, var)
141+
set_top!(flatAST, top)
142+
end
143+
end
144+
end
122145
end
123146

124147
function flatten!(flatAST::FlatAST, ex, var)
125148
return ex # nothing to do to the AST; return the number
126149
end
127150

128151
# symbols:
129-
function flatten!(flatAST::FlatAST, ex::Variable, var) # symbols are leaves
130-
if isempty(var)
131-
add_variable!(flatAST, Symbol(ex)) # add the discovered symbol as an input variable
132-
end
133-
return Symbol(ex)
134-
end
135-
136152
function flatten!(flatAST::FlatAST, ex::Symbol, var)
137153
if isempty(var)
138154
add_variable!(flatAST, ex) # add the discovered symbol as an input variable
@@ -169,18 +185,6 @@ function flatten!(flatAST::FlatAST, ex::Expr, var = [])
169185
set_top!(flatAST, top)
170186
end
171187

172-
function flatten!(flatAST::FlatAST, ex::Operation, var)
173-
# top = process_operation!(flatAST, ex, var)
174-
# set_top!(flatAST, top)
175-
176-
if ex.op isa Variable
177-
return flatten!(flatAST, ex.op, var)
178-
else
179-
top = process_operation!(flatAST, ex, var)
180-
set_top!(flatAST, top)
181-
end
182-
end
183-
184188

185189
function process_constant!(flatAST::FlatAST, ex)
186190
return esc(ex.args[1]) # interpolate the value of the external constant
@@ -335,7 +339,7 @@ function process_call!(flatAST::FlatAST, ex, var = [], new_var=nothing)
335339
#@show op
336340

337341
if op keys(reverse_operations) # standard operator
338-
if new_var == nothing
342+
if isnothing(new_var)
339343
new_var = make_symbol()
340344
end
341345

@@ -408,7 +412,7 @@ function process_operation!(flatAST::FlatAST, ex, var, new_var=nothing)
408412
#@show op
409413

410414
if Symbol(op) keys(reverse_operations) # standard operator
411-
if new_var == nothing
415+
if isnothing(new_var)
412416
new_var = make_symbol()
413417
end
414418

src/contractor.jl

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
21
"""
32
`Contractor` represents a `Contractor` from ``\\mathbb{R}^N`` to ``\\mathbb{R}^N``.
43
Nout is the output dimension of the forward part.
54
"""
65
abstract type AbstractContractor end
76

8-
struct Contractor{N, Nout, F1<:Function, F2<:Function, ex<:Union{Operation,Expr}} <:AbstractContractor
7+
struct Contractor{N, Nout, F1<:Function, F2<:Function, ex} <:AbstractContractor
98
variables::Vector{Symbol} # input variables
109
forward::GeneratedFunction{F1}
1110
backward::GeneratedFunction{F2}
@@ -86,70 +85,73 @@ function (C::BasicContractor)(A::IntervalBox{Nout,T}, X::IntervalBox{N,T})where
8685
end
8786

8887
# allow 1D contractors to take Interval instead of IntervalBox for simplicty:
89-
(C::BasicContractor)(A::Interval{T}, X::IntervalBox{N,T}) where {N,Nout,T} = C(IntervalBox(A), X)
90-
91-
""" Contractor can also be construct without the use of macros
92-
vars = @variables x y z
93-
C = Contractor(x + y , vars)
94-
C(-Inf..1, IntervalBox(0.5..1.5,3))
95-
"""
88+
(C::BasicContractor)(A::Interval{T}, X::IntervalBox{N,T}) where {N,T} = C(IntervalBox(A), X)
9689

97-
function Contractor(variables, expr::Operation)
9890

99-
var = [i.op.name for i in variables]
100-
top, linear_AST = flatten(expr, var)
91+
function Base.show(io::IO, C::BasicContractor{F1,F2}) where {F1,F2}
92+
println(io, " Basic version of Contractor")
93+
end
10194

95+
function _load_MT_contractor()
96+
return quote
97+
""" Contractor can also be construct without the use of macros
98+
vars = @variables x y z
99+
C = Contractor(x + y , vars)
100+
C(-Inf..1, IntervalBox(0.5..1.5,3))
101+
"""
102+
function Contractor(variables, expr::Operation)
102103

103-
forward_code, backward_code = forward_backward(linear_AST)
104+
var = [i.op.name for i in variables]
105+
top, linear_AST = flatten(expr, var)
104106

105107

106-
# @show top
108+
forward_code, backward_code = forward_backward(linear_AST)
107109

108-
if isa(top, Symbol)
109-
top = [top]
110-
end
111110

112-
forward = eval(forward_code)
113-
backward = eval(backward_code)
111+
# @show top
114112

115-
Contractor(linear_AST.variables,
116-
top,
117-
GeneratedFunction(forward, forward_code),
118-
GeneratedFunction(backward, backward_code),
119-
expr)
113+
if isa(top, Symbol)
114+
top = [top]
115+
end
120116

121-
end
117+
forward = eval(forward_code)
118+
backward = eval(backward_code)
122119

120+
Contractor(linear_AST.variables,
121+
top,
122+
GeneratedFunction(forward, forward_code),
123+
GeneratedFunction(backward, backward_code),
124+
expr)
123125

124-
function BasicContractor(variables, expr::Operation)
126+
end
125127

126-
var = [i.op.name for i in variables]
127-
top, linear_AST = flatten(expr, var)
128128

129-
forward_code, backward_code = forward_backward(linear_AST)
129+
function BasicContractor(variables, expr::Operation)
130130

131-
forward = eval(forward_code)
132-
backward = eval(backward_code)
131+
var = [i.op.name for i in variables]
132+
top, linear_AST = flatten(expr, var)
133133

134-
BasicContractor{typeof(forward), typeof(backward)}(forward, backward)
135-
end
134+
forward_code, backward_code = forward_backward(linear_AST)
136135

137-
function Base.show(io::IO, C::BasicContractor{F1,F2}) where {F1,F2}
138-
println(io, " Basic version of Contractor")
139-
end
136+
forward = eval(forward_code)
137+
backward = eval(backward_code)
140138

141-
BasicContractor(expr::Operation) = BasicContractor([], expr::Operation)
139+
BasicContractor{typeof(forward), typeof(backward)}(forward, backward)
140+
end
142141

143-
BasicContractor(vars::Union{Vector{Operation}, Tuple{Vararg{Operation,N}}}, g::Function) where N = BasicContractor(vars, g(vars...)) #Contractor can be constructed by function name only
142+
BasicContractor(expr::Operation) = BasicContractor([], expr::Operation)
144143

145-
BasicContractor(vars, f::Function) = BasicContractor([Variable(Symbol(i))() for i in vars], f([Variable(Symbol(i))() for i in vars]...))#if vars is not vector of Operation
144+
BasicContractor(vars::Union{Vector{Operation}, Tuple{Vararg{Operation,N}}}, g::Function) where N = BasicContractor(vars, g(vars...)) #Contractor can be constructed by function name only
146145

146+
BasicContractor(vars, f::Function) = BasicContractor([Variable(Symbol(i))() for i in vars], f([Variable(Symbol(i))() for i in vars]...))#if vars is not vector of Operation
147147

148-
Contractor(expr::Operation) = Contractor([], expr::Operation)
148+
Contractor(expr::Operation) = Contractor([], expr::Operation)
149149

150-
Contractor(vars::Union{Vector{Operation}, Tuple{Vararg{Operation,N}}}, g::Function) where N = Contractor(vars, g(vars...)) #Contractor can be constructed by function name only
150+
Contractor(vars::Union{Vector{Operation}, Tuple{Vararg{Operation,N}}}, g::Function) where N = Contractor(vars, g(vars...)) #Contractor can be constructed by function name only
151151

152-
Contractor(vars, f::Function) = Contractor([Variable(Symbol(i))() for i in vars], f([Variable(Symbol(i))() for i in vars]...))#if vars is not vector of Operation
152+
Contractor(vars, f::Function) = Contractor([Variable(Symbol(i))() for i in vars], f([Variable(Symbol(i))() for i in vars]...))#if vars is not vector of Operation
153+
end
154+
end
153155

154156
function make_contractor(expr::Expr, var = [])
155157
# println("Entering Contractor(ex) with ex=$ex")

0 commit comments

Comments
 (0)