Skip to content

Commit f45eb78

Browse files
authored
generate julia code (#21)
* inital code generation implementation * more docs and tests * commit missing file * fix tests on 1.6 * add codegen for sugeno * update documentation * codegen of bisector defuzzifier
1 parent 1f84faa commit f45eb78

14 files changed

+646
-126
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ using FuzzyLogic
2828
- **Compatible!** Read your models from [IEC 61131-7 Fuzzy Control Language](https://ffll.sourceforge.net/fcl.htm), [IEEE 1855-2016 Fuzzy Markup Language](https://en.wikipedia.org/wiki/Fuzzy_markup_language) and Matlab Fuzzy toolbox `.fis` files.
2929
- **Expressive!** Clear Domain Specific Language to write your model as human readable Julia code
3030
- **Productive!** Several visualization tools to help debug and tune your model.
31+
- **Portable!** Compile your final model to Julia or C/C++ code.
32+
3133
## Quickstart example
3234

3335
```julia

docs/src/api/readwrite.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
@sugfis
88
```
99

10+
## Generate Julia code
11+
12+
```@docs
13+
compilefis
14+
```
15+
1016
## Read from file
1117

1218
```@docs

docs/src/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
88
- ![](https://img.shields.io/badge/new%20feature-green.svg) allow to specify input and output variables as vectors (e.g. `x[1:10]`) and support for loops to avoid repetitive code.
99
- ![](https://img.shields.io/badge/new%20feature-green.svg) added support for type-2 membership functions and type-2 systems.
1010
- ![](https://img.shields.io/badge/new%20feature-green.svg) added parser for Fuzzy Markup Language.
11+
- ![](https://img.shields.io/badge/new%20feature-green.svg) added generation of native Julia code.
1112

1213
## v0.1.1 -- 2023-02-25
1314

docs/src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ using FuzzyLogic
2828
- **Compatible!** Read your models from [IEC 61131-7 Fuzzy Control Language](https://ffll.sourceforge.net/fcl.htm), [IEEE 1855-2016 Fuzzy Markup Language](https://en.wikipedia.org/wiki/Fuzzy_markup_language) and Matlab Fuzzy toolbox `.fis` files.
2929
- **Expressive!** Clear Domain Specific Language to write your model as human readable Julia code
3030
- **Productive!** Several visualization tools to help debug and tune your model.
31+
- **Portable!** Compile your final model to Julia or C/C++ code.
3132

3233
## Quickstart example
3334

docs/src/literate/tutorials/mamdani.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fis = @mamfis function tipper(service, food)::tip
5757
service == excellent || food == delicious --> tip == generous
5858

5959
aggregator = ProbSumAggregator
60-
defuzzifier = BisectorDefuzzifier
60+
defuzzifier = CentroidDefuzzifier
6161
end
6262

6363
#=
@@ -186,3 +186,19 @@ res = fis(service = 2, food = 3)
186186
# The value of a specific output variable can be extracted using the variable name as key.
187187

188188
res[:tip]
189+
190+
#=
191+
## Code generation
192+
193+
The model can be compiled to native Julia code using the [`compilefis`](@ref) function.
194+
This produces optimized Julia code independent of the library, that can be executed as
195+
stand-alone function.
196+
=#
197+
198+
fis_ex = compilefis(fis)
199+
200+
# The new expression can now be evaluated as normal Julia code. Notice that the generated
201+
# function uses positional arguments.
202+
203+
eval(fis_ex)
204+
tipper(2, 3)

docs/src/literate/tutorials/sugeno.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,12 @@ Like the Mamdani case, we can plot the whole system.
7575
=#
7676

7777
plot(fis)
78+
79+
# Similarly to Mamdani, we can also generate stand-alone Julia code
80+
81+
fis_ex = compilefis(fis)
82+
83+
#
84+
85+
eval(fis_ex)
86+
tipper(2, 3)

src/FuzzyLogic.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ include("parser.jl")
1313
include("evaluation.jl")
1414
include("plotting.jl")
1515
include("genfis.jl")
16+
include("tojulia.jl")
1617
include("readwrite.jl")
1718

1819
export DifferenceSigmoidMF, LinearMF, GeneralizedBellMF, GaussianMF, ProductSigmoidMF,
@@ -26,6 +27,7 @@ export DifferenceSigmoidMF, LinearMF, GeneralizedBellMF, GaussianMF, ProductSigm
2627
@mamfis, MamdaniFuzzySystem, @sugfis, SugenoFuzzySystem, set,
2728
LinearSugenoOutput, ConstantSugenoOutput,
2829
fuzzy_cmeans,
30+
compilefis,
2931
readfis
3032

3133
## parsers

src/membership_functions.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ struct PiecewiseLinearMF{T <: Real, S <: Real} <: AbstractMembershipFunction
301301
points::Vector{Tuple{T, S}}
302302
end
303303
function (plmf::PiecewiseLinearMF)(x::Real)
304-
x <= plmf.points[1][1] && return plmf.points[1][2]
305-
x >= plmf.points[end][1] && return plmf.points[end][2]
304+
x <= plmf.points[1][1] && return float(plmf.points[1][2])
305+
x >= plmf.points[end][1] && return float(plmf.points[end][2])
306306
idx = findlast(p -> x >= p[1], plmf.points)
307307
x1, y1 = plmf.points[idx]
308308
x2, y2 = plmf.points[idx + 1]

0 commit comments

Comments
 (0)