home Forums # Technical Support Time and Absolute Values?

This topic contains 5 replies, has 3 voices, and was last updated by Juan Rada-Vilela Juan Rada-Vilela 2 years, 11 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
  • #942



    I’m after some suggestions here….

    1st – I need to be able to represent time/duration in some of my rules. Could be something like time of day (eg: “now > 12.00am …”) but can’t quite figure out a elegant way to do it.. but I was thinking about utilizing functions.

    eg, a input variable (now) that is “number of seconds since midnight” – easy enough
    and a function say called time() – where it takes a human readable time, and converts it to seconds – Does that sound doable? (so the rule becomes “now > time(12.00am) …”
    But looking through the code in Function.cpp it appears they will only take scalars as variables?

    2nd – some of my output variables are essentially boolean (either 1 or 0). In playing with qtfuzzylite, I can’t seem to figure out how to do it as absolute values… I always end up with something between 0 and 1. (taking your dimmer as a example, I’d just want something like (“brightness is dark, then light is on”)

    Any ideas?

    Juan Rada-Vilela
    Juan Rada-Vilela

    Hi Fishwaldo,

    Could you elaborate further on your time-based rules? Could you write a few rules to understand what you want to do?

    As for your boolean output variables, I suggest that you utilize for those the WeightedAverage defuzzifier and their respective terms be Constants. For example,

    OutputVariable: light
      defuzzifier: WeightedAverage
      accumulation: AlgebraicSum
      term: on Constant 1.0
      term: off Constant 0.0
      activation: AlgebraicProduct
      rule: if brightness is dark, then light is on

    Furthermore, I would create a rule block specifically for those boolean functions in order to set the activation operator to the AlgebraicProduct, which might not necessarily be the activation operator for the other rules. The model I propose will still give you values between 0 and 1, for which you will just have to round them manually (std::round).



    Hi Juan,
    Our use case for fuzzylite is in a HVAC controller for hotel rooms. We currently have hardcoded some rules to take advantage of off-peak electricity tarrif’s as well as room occupancy.

    So, rules would be like:
    if room = occupied & time > 6pm & tempsetting = cold then aircon = fullspeed
    if room = occupied & time < 6pm & tempsetting = cold then aircon = halfspeed

    The reason we ask for time based rules, is that the off-peak tariffs are changing on a frequent basis. The reason I would prefer to have this handled directly in the fuzzylite engine (rather than externally) is that the rules will be edited by normal users, (not necessarily IT staff)

    I’m thinking the changes would have to be in Function.cpp. We could add a new function say Time(“string”) and in Function::parse, test for literal strings rather than variables, and maybe some conversion functions to convert from strings to input variables from the rule blocks.

    Do you follow me?

    Juan Rada-Vilela
    Juan Rada-Vilela

    Hi Fishwaldo,

    Thank you for your post and for sharing information about an interesting application of fuzzylite.

    I have been thinking about your case since I read about it, and I have one solution.

    As for now, I am reluctant to allow the kind of rules you present, but I will surely consider it for another version of fuzzylite in the future.

    My solution is to create an InputVariable called Time, besides InputVariable Room, as follows:

    InputVariable: Time
      enabled: true
      range: 0.0 23.59
      term: After6pm Function ge(Time, 18)
      term: Before6pm Function lt(Time, 18)
      term: After8pm Function ge(Time, 20)
      term: Before8pm Function lt(Time, 20)
    InputVariable: Room
      enabled: true
      range: 0.0 1.0
      term: Occupied Function eq(Room, 1.0)
      term: Empty Function ~eq(Room, 1.0)

    Then, the rules would be:

      conjunction: AlgebraicProduct
      activation: AlgebraicProduct
      rule: if Room is Occupied and Time is After6pm then AirCond is fullspeed

    The changes you need to make in fuzzylite are the following:

    In Operation.h, you need to create the following methods,

    static scalar gt(scalar a, scalar b); //greater than
    static scalar ge(scalar a, scalar b); //greater than or equal to
    static scalar eq(scalar a, scalar b); //equal to
    static scalar le(scalar a, scalar b); //less than or equal to
    static scalar lt(scalar a, scalar b); //less than
    static scalar Operation::logicalNot(scalar a) {
            return isEq(a, 1.0) ? 0.0 : 1.0; //Implementation in Operation.cpp

    and within the methods utilize the respective calls to Op::isGt, Op::isGE, etc.

    In Function.cpp, you need to add the new methods as follows:

    this->functions["gt"] = new BuiltInFunction("gt", &(fl::Op::gt));
    this->functions["ge"] = new BuiltInFunction("ge", &(fl::Op::ge));
    this->functions["eq"] = new BuiltInFunction("eq", &(fl::Op::eq));
    this->functions["le"] = new BuiltInFunction("le", &(fl::Op::le));
    this->functions["lt"] = new BuiltInFunction("lt", &(fl::Op::lt));
    int p = 7;
    this->operators["!"] = new Operator("!", &(fl::Op::logicalNot), p, 1);

    As for time, you will have to set the input value for Time manually before processing the Engine.

    I have already incorporated the aforementioned changes to fuzzylite v5.0, but I discourage you from using the master repository as it is work in progress and plenty of changes are being made.




    Hi Juan-

    I’ve just started looking but from what I’ve seen so far, this is a great package you’ve put together.

    In my experience with Fuzzy Logic, sometimes, some of the inputs are distinctly Boolean or Categorical (no Fuzziness about them) whereas the outputs are Fuzzy. It would be nice if one could incorporate that into the system with less effort. I see that it is possible with the current version, but it seems like one would have create a lot extra code to model the Boolean for each case.

    For instance, I would like to be able to do something like this:

    If FlagContainsBlue == True and FlagContainsRed == True and FlagContainsWhite == True then FrenchFlag is somewhat True
    If FlagContainsBlue == True and FlagContainsRed == True and FlagContainsWhite == True then BritishFlag is somewhat True
    If FlagContainsBlue == True and FlagContainsRed == True and FlagContainsWhite == True then DutchFlag is somewhat True
    If FlagContainsBlue == True and FlagContainsRed == True and FlagContainsWhite == True then RussianFlag is somewhat True
    If FlagContainsBlue == True and FlagContainsRed == True and FlagContainsWhite == True then AmericanFlag is somewhat True

    By “somewhat True”, I intend the meaning “Possible”.

    One way I think I could do this, is to define the term “True” to be a rectangle (1,1) and “False” to be a a rectangle (0,0), but I would have to do this for every variable (e.g. FlagContainsBlue). It there a way to have a global term “True” that needs to be defined only once?

    Or maybe what I really want is several inputs to share a same set of terms. (This has the advantage if you change one, the change will propagate throughout.) For example, if you have an input for X and Y coordinates, there may be situations that whatever input characteristics you have for X, you will want for Y. Copying this isn’t so bad with two variables, but for many more similar inputs, this would be a nuance.


    Juan Rada-Vilela
    Juan Rada-Vilela

    Hi CW,

    I apologize for such a late reply, but your message was filtered as spam.

    How about the rules as follow?

    if FlagContainsBlue is any and FlagContainsRed is any and FlagContainsWhite is any then FrenchFlag is somewhat True

    The hedge any will always evaluate to true, thereby having the behaviour that you expect. I am not sure if you can do if variable is not any then ... in fuzzylite 4.0, but surely you will in version 5.0 to be released in April (hopefully). As for the output variable FrenchFlag, you could define it with two Constants 0 and 1 (respectively) and use a Takagi-Sugeno engine with WeightedAverage defuzzifier.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.