-1

I am trying to implement fuzzy logic for classifying the weather into different climatic zone. I am getting this Null exception when I call fuzzy_inference() function of sets package in RStudio. The following is my code defining sets and fuzzy rules.

sets_options("universe", seq(1, 200, 0.5))
variables <- set(
rainfall=fuzzy_partition(varnames = c(range1 = 30, range2 = 70, range3 = 
90),sd = 5.0),
maxTemp = fuzzy_partition(varnames = c(hot = 30, hotter = 70),sd = 5.0),
humidity = fuzzy_partition(varnames = c(less_humid = 30, humid = 60, 
more_humid = 80),sd = 3.0),
soil = fuzzy_partition(varnames=c(Black_soil=10,Mixed_Red_and_Black_soil=20,
Mixed_Red_and_clay_soil=30,
Mixed_red_and_sandy_soil=40,Mixed_Red_Loamy_and_sandy_soil=50,
Mixed_sandy_and_clayey_soil=60,
Red_forest_soil=70,Red_Lateritic_soil=80,Red_Loamy_soil=90,                
                                  Red_sandy_soil=100),sd = 3.0),
 climate=fuzzy_partition(varnames = 
 c(Northern_Dry_Zone=10,
 Eastern_Dry_Zone=20,
 North_Eastern_Transition_Zone=30,
 Southern_Dry_Zone=40,Eastern_Dry_Zone=50,Hilly_Zone=60,   

Central_Dry_Zone=70,Coastal_Zone=80,Southern_Transition_Zone=90,                
                                   Northern_Transition_Zone=100),sd = 3.0),                       
FUN = fuzzy_cone, radius = 10)
rules <- set(
fuzzy_rule(humidity %is% less_humid && maxTemp %is% hot && rainfall %is% 
range2 && soil %is% Black_soil 
         ,climate %is% Northern_Dry_Zone),
fuzzy_rule(humidity %is% humid && maxTemp %is% hot && rainfall %is% range2 
&& soil %is% Red_Loamy_soil 
         ,climate %is% Eastern_Dry_Zone),
fuzzy_rule(humidity %is% humid && maxTemp %is% hot && rainfall %is% range2 
&& soil %is% Black_soil 
         ,climate %is% Northern_Dry_Zone),
fuzzy_rule(humidity %is% less_humid && maxTemp %is% hotter && rainfall %is% 
range1 && soil %is% Mixed_Red_and_Black_soil 
         ,climate %is% Northern_Dry_Zone),
fuzzy_rule(humidity %is% less_humid && maxTemp %is% hot && rainfall %is% 
range1 && soil %is% Black_soil 
         ,climate %is% Northern_Transition_Zone),
fuzzy_rule(humidity %is% humid && maxTemp %is% hot && rainfall %is% range3 
&& soil %is% Red_forest_soil 
         ,climate %is% Southern_Dry_Zone),
fuzzy_rule(humidity %is% humid && maxTemp %is% hot && rainfall %is% range2 
&& soil %is% Mixed_red_and_sandy_soil 
         ,climate %is% Eastern_Dry_Zone),
fuzzy_rule(humidity %is% less_humid && maxTemp %is% hotter && rainfall %is% 
range2 && soil %is% Mixed_Red_and_Black_soil 
         ,climate %is% Central_Dry_Zone),
fuzzy_rule(humidity %is% more_humid && maxTemp %is% hot  && rainfall %is% 
range3 && soil %is% Red_Loamy_soil 
         ,climate %is% Hilly_Zone),
fuzzy_rule(humidity %is% more_humid && maxTemp %is% hotter && rainfall %is% 
range3 && soil %is% Red_Lateritic_sandy_soil 
         ,climate %is% Coastal_Zone),
fuzzy_rule(humidity %is% less_humid && maxTemp %is% hotter && rainfall %is% 
range3 && soil %is% Black_soil 
         ,climate %is% North_Eastern_Dry_Zone),
fuzzy_rule(humidity %is% more_humid && maxTemp %is% hot && rainfall %is% 
range3 && soil %is% Red_Loamy_soil 
         ,climate %is% Southern_Transition_Zone),
fuzzy_rule(humidity %is% humid && maxTemp %is% hot && rainfall %is% range2 
&& soil %is% Mixed_Red_Loamy_and_sandy_soil 
         ,climate %is% Eastern_Dry_Zone),
fuzzy_rule(humidity %is% less_humid && maxTemp %is% hotter && rainfall %is% 
range2 && soil %is% Black_soil 
         ,climate %is% Northern_Dry_Zone),
fuzzy_rule(humidity %is% humid && maxTemp %is% hot && rainfall %is% range3 
&& soil %is% Red_Loamy_soil 
         ,climate %is% Southern_Dry_Zone),
fuzzy_rule(humidity %is% less_humid && maxTemp %is% hot && rainfall %is% 
range1 && soil %is% Black_soil 
         ,climate %is% North_Eastern_Dry_Zone),
fuzzy_rule(humidity %is% humid && maxTemp %is% hotter && rainfall %is% 
range2 && soil %is% Mixed_Red_Loamy_and_sandy_soil 
         ,climate %is% Eastern_Dry_Zone)
)
model<- fuzzy_system(variables, rules)
### error code ####
example.1 <- fuzzy_inference(model,
list(humidity=63,maxTemp=31,rainfall=72.5,soil=41.5))

The following line of the code is throwing null error. ### error code #### example.1 <- fuzzy_inference(model, list(humidity=63,maxTemp=31,rainfall=72.5,soil=41.5))

  • 1
    I don't think it is appropriate to drop a code-bomb, and say you have an error. At least try to pinpoint the error yourself and be specific about what you have tried so far. For future reference, it would be good if you had a look at [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – KenHBS Mar 21 '18 at 09:08

2 Answers2

1

I haven't evaluated your code, but if the problem is that you try to set an attribute on NULL, then the solution is simple. Don't do that.

The NULL object is not really an object. It is the absence of an object. You cannot modify it or set attributes to it; you cannot have NULL objects of different classes; NULL is nothing.

You can set attributes to NA because that is an object -- just a missing value, but of a specific type. It is an explicit representation of the lack of a value. But NULL is different. It is R's way of representing that you do not have an object.

There is no way to assign attributes to NULL. If you want a value that represents nothing but has classes or attributes, you need NA or some other representation. NULL isn't going to work.

halfer
  • 19,824
  • 17
  • 99
  • 186
Thomas Mailund
  • 1,674
  • 10
  • 16
0

I had the same issue and found this old thread without a solution. In my case, I had rules defined with not matching characteristics as the defined variables.

So check your variables and your rules.

Matthis
  • 21
  • 3
  • 1
    Welcome to Stack Oveflow. I'm not really sure this is an answer. Please check [answer] for guidance on providing a good answer. – Peter May 19 '20 at 18:39