Johannes Radinger
2013-May-08 12:51 UTC
[R] NaN-result from fuzzy_inference (package "sets") with certain input
Hi,
I am trying to use the fuzzy_inference system on multiple input values.
Basically I combine two variables (depth, velocity) into the variable
suitability.
Both depth and velocity have 3 trapezoid classes (verylow,medium,high) each.
The consequent (suitability) has 4 fuzzy levels (none,low,medium,high) and
should
finally range between 0 and 1.
I already set up the level definitions (trapez-corners) and the rules (a
combination
of all depth-levels with all velocity-levels and the consequent
suitability).
Now I wanted to test the system on a dataframe with dummy variables (in the
range
of values I will supply), but some how I get for some combinations of depth
and velocity no resulting suitablilty.
I provide here now some code (simpler than my actual) that produces also
this
NaN values:
# Load "sets"
library(sets)
## set universe
sets_options("universe", seq(from = 0, to = 1, by = 0.001))
## set up fuzzy variables
variables <-
set(depth = fuzzy_variable(verylow = fuzzy_trapezoid(corners
c(-3,0,0.2,0.5)),
medium = fuzzy_trapezoid(corners
=c(0.45,0.55,0.7,0.9)),
veryhigh = fuzzy_trapezoid(corners
c(0.85,1,15,20))),
velocity = fuzzy_variable(verylow = fuzzy_trapezoid(corners
c(-3,0,0.2,0.5)),
medium = fuzzy_trapezoid(corners
c(0.35,0.55,0.65,0.8)),
veryhigh = fuzzy_trapezoid(corners
c(0.7,0.9,15,20))),
suitability = fuzzy_partition(varnames = c(none = 0.2, low = 0.4,
medium = 0.6, high = 0.8),FUN = fuzzy_cone, radius = 0.15))
## set up rules
rules <-
set(
fuzzy_rule(depth %is% verylow && velocity %is% verylow, suitability
%is% low),
fuzzy_rule(depth %is% medium && velocity %is% verylow, suitability
%is%
medium),
fuzzy_rule(depth %is% veryhigh && velocity %is% verylow, suitability
%is% low),
fuzzy_rule(depth %is% verylow && velocity %is% medium, suitability
%is%
medium),
fuzzy_rule(depth %is% medium && velocity %is% medium, suitability
%is%
high),
fuzzy_rule(depth %is% veryhigh && velocity %is% medium, suitability
%is% medium),
fuzzy_rule(depth %is% verylow && velocity %is% veryhigh, suitability
%is% none),
fuzzy_rule(depth %is% medium && velocity %is% veryhigh, suitability
%is% low),
fuzzy_rule(depth %is% veryhigh && velocity %is% veryhigh,
suitability
%is% none)
)
## combine to a system
system <- fuzzy_system(variables, rules)
print(system)
plot(system) ## plots variables
# test df
test_df <-
data.frame(depth=round(runif(20,0,2),2),velocity=round(runif(20,0,1.5),2),suitability=NA)
# do inference on test_df
fuzzy_result <- function(df){
gset_defuzzify(fuzzy_inference(system, list(depth = df["depth"],
velocity
= df["velocity"])),"centroid")
}
apply(test_df,1,fuzzy_result)
Does anyone know what is happening there?
/Johannes
[[alternative HTML version deleted]]
Johannes Radinger
2013-May-10 07:49 UTC
[R] Fwd: NaN-result from fuzzy_inference (package "sets") with certain input
Hi,
@David: thank you for your fast response. I changed the universe to cover
the at least the total range of the values in the dataframe. However, this
does not save the problem for several combination of depth and velocity. I
attached the same example as before but with a dataframe of such
combinations that don't work. Is the problem caused by the definition of
the membership classes? How can the problem bis solved?
## set universe
sets_options("universe", seq(from = 0, to = 5, by = 0.001))
## set up fuzzy variables
variables <-
set(depth = fuzzy_variable(verylow = fuzzy_trapezoid(corners
c(-3,0,0.2,0.5)),
medium = fuzzy_trapezoid(corners
=c(0.45,0.55,0.7,0.9)),
veryhigh = fuzzy_trapezoid(corners
c(0.85,1,15,20))),
velocity = fuzzy_variable(verylow = fuzzy_trapezoid(corners
c(-3,0,0.2,0.5)),
medium = fuzzy_trapezoid(corners
c(0.35,0.55,0.65,0.8)),
veryhigh = fuzzy_trapezoid(corners
c(0.7,0.9,15,20))),
suitability = fuzzy_partition(varnames = c(none = 0.2, low = 0.4,
medium = 0.6, high = 0.8),FUN = fuzzy_cone, radius = 0.2))
## set up rules
rules <-
set(
fuzzy_rule(depth %is% verylow && velocity %is% verylow, suitability
%is% low),
fuzzy_rule(depth %is% medium && velocity %is% verylow, suitability
%is%
medium),
fuzzy_rule(depth %is% veryhigh && velocity %is% verylow, suitability
%is% low),
fuzzy_rule(depth %is% verylow && velocity %is% medium, suitability
%is%
medium),
fuzzy_rule(depth %is% medium && velocity %is% medium, suitability
%is%
high),
fuzzy_rule(depth %is% veryhigh && velocity %is% medium, suitability
%is% medium),
fuzzy_rule(depth %is% verylow && velocity %is% veryhigh, suitability
%is% none),
fuzzy_rule(depth %is% medium && velocity %is% veryhigh, suitability
%is% low),
fuzzy_rule(depth %is% veryhigh && velocity %is% veryhigh,
suitability
%is% none)
)
## combine to a system
system <- fuzzy_system(variables, rules)
print(system)
plot(system) ## plots variables
# test df
test_df <-
data.frame(depth=c(1.71,0.61,1.56,0.47,0.70,0.42,1.90),velocity=c(0.70,1.40,0.95,0.65,0.58,0.47,1.24),suitability=NA)
# do inference on test_df
fuzzy_result <- function(df){
gset_defuzzify(fuzzy_inference(system, list(depth = df["depth"],
velocity
= df["velocity"])),"centroid")
}
apply(test_df,1,fuzzy_result)
/Johannes
On Thu, May 9, 2013 at 12:35 AM, David Meyer <david.meyer@wu.ac.at> wrote:
> It's because you restricted the universe to [0,1], so the fuzzy
inference
> returns empty sets for those entries in your df with values larger than 1
> ...
>
> David
>
>
> On 2013-05-08 14:53, Johannes Radinger wrote:
>
>> This email has already been sent to the R-mailing list,
>> but maybe you as the author of the package "sets" can help
here best....
>>
>> Hi,
>>
>> I am trying to use the fuzzy_inference system on multiple input values.
>> Basically I combine two variables (depth, velocity) into the variable
>> suitability.
>> Both depth and velocity have 3 trapezoid classes (verylow,medium,high)
>> each.
>> The consequent (suitability) has 4 fuzzy levels (none,low,medium,high)
>> and should
>> finally range between 0 and 1.
>> I already set up the level definitions (trapez-corners) and the rules
(a
>> combination
>> of all depth-levels with all velocity-levels and the consequent
>> suitability).
>>
>> Now I wanted to test the system on a dataframe with dummy variables (in
>> the range
>> of values I will supply), but some how I get for some combinations of
>> depth
>> and velocity no resulting suitablilty.
>>
>> I provide here now some code (simpler than my actual) that produces
also
>> this
>> NaN values:
>>
>> # Load "sets"
>> library(sets)
>>
>> ## set universe
>> sets_options("universe", seq(from = 0, to = 1, by = 0.001))
>>
>> ## set up fuzzy variables
>> variables <-
>> set(depth = fuzzy_variable(verylow = fuzzy_trapezoid(corners
>> c(-3,0,0.2,0.5)),
>> medium = fuzzy_trapezoid(corners
>> =c(0.45,0.55,0.7,0.9)),
>> veryhigh = fuzzy_trapezoid(corners
>> c(0.85,1,15,20))),
>> velocity = fuzzy_variable(verylow = fuzzy_trapezoid(corners
>> c(-3,0,0.2,0.5)),
>> medium = fuzzy_trapezoid(corners
>> c(0.35,0.55,0.65,0.8)),
>> veryhigh = fuzzy_trapezoid(corners
>> c(0.7,0.9,15,20))),
>> suitability = fuzzy_partition(varnames = c(none = 0.2, low
>> 0.4, medium = 0.6, high = 0.8),FUN = fuzzy_cone, radius = 0.15))
>>
>>
>> ## set up rules
>> rules <-
>> set(
>> fuzzy_rule(depth %is% verylow && velocity %is% verylow,
suitability
>> %is% low),
>> fuzzy_rule(depth %is% medium && velocity %is% verylow,
suitability
>> %is% medium),
>> fuzzy_rule(depth %is% veryhigh && velocity %is% verylow,
>> suitability %is% low),
>> fuzzy_rule(depth %is% verylow && velocity %is% medium,
suitability
>> %is% medium),
>> fuzzy_rule(depth %is% medium && velocity %is% medium,
suitability
>> %is% high),
>> fuzzy_rule(depth %is% veryhigh && velocity %is% medium,
suitability
>> %is% medium),
>> fuzzy_rule(depth %is% verylow && velocity %is% veryhigh,
>> suitability %is% none),
>> fuzzy_rule(depth %is% medium && velocity %is% veryhigh,
suitability
>> %is% low),
>> fuzzy_rule(depth %is% veryhigh && velocity %is% veryhigh,
>> suitability %is% none)
>> )
>>
>> ## combine to a system
>> system <- fuzzy_system(variables, rules)
>> print(system)
>> plot(system) ## plots variables
>>
>> # test df
>> test_df <-
>> data.frame(depth=round(runif(**20,0,2),2),velocity=round(**
>> runif(20,0,1.5),2),**suitability=NA)
>>
>> # do inference on test_df
>> fuzzy_result <- function(df){
>> gset_defuzzify(fuzzy_**inference(system, list(depth =
df["depth"],
>> velocity = df["velocity"])),"centroid")
>> }
>>
>> apply(test_df,1,fuzzy_result)
>>
>>
>> Does anyone know what is happening there?
>>
>> /Johannes
>>
>>
>>
> --
> Priv.-Doz. Dr. David Meyer
> Department of Information Systems and Operations
>
> WU
> Wirtschaftsuniversität Wien
> Vienna University of Economics and Business
> Augasse 2-6, 1090 Vienna, Austria
> Tel: +43-1-313-36-4393
> Fax: +43-1-313-36-90-4393
> HP: http://ec.wu.ac.at/~meyer
>
[[alternative HTML version deleted]]