fuzzylogic 0.2.1

Fuzzy Logic for Dart #

Build Status

This is a module for fuzzy logic in Dart. It takes some inspiration from the FCL (Fuzzy Control Language) IEC 1131-7 specification, but otherwise strives to be a 'Dart-y' way to create and work with fuzzy rules.

The goal of this project is to make it extremely easy to implement fuzzy logic when creating:

  1. Artificial intelligence in Web-based games.
  2. Intelligent user experience in websites.

Example of use #

Here's code that implements the "Designing FLVs for Weapon Selection" (pp. 425-437) fuzzy logic example from Mat Buckland's excellent book Programming Game AI by Example (2005).

// Set up variables.
var distanceToTarget = new Distance();
var bazookaAmmo = new Ammo();
var bazookaDesirability = new Desirability();

// Add rules.
var frb = new FuzzyRuleBase();
    (distanceToTarget.Far & bazookaAmmo.Loads) >> (bazookaDesirability.Desirable),
    (distanceToTarget.Far & bazookaAmmo.Okay) >> (bazookaDesirability.Undesirable),
    (distanceToTarget.Far & bazookaAmmo.Low) >> (bazookaDesirability.Undesirable),
    (distanceToTarget.Medium & bazookaAmmo.Loads) >> (bazookaDesirability.VeryDesirable),
    (distanceToTarget.Medium & bazookaAmmo.Okay) >> (bazookaDesirability.VeryDesirable),
    (distanceToTarget.Medium & bazookaAmmo.Low) >> (bazookaDesirability.Desirable),
    (distanceToTarget.Close & bazookaAmmo.Loads) >> (bazookaDesirability.Undesirable),
    (distanceToTarget.Close & bazookaAmmo.Okay) >> (bazookaDesirability.Undesirable),
    (distanceToTarget.Close & bazookaAmmo.Low) >> (bazookaDesirability.Undesirable)

// Create the placeholder for output.
var bazookaOutput = bazookaDesirability.createOutputPlaceholder();

// Use the fuzzy inference engine.
    inputs: [distanceToTarget.assign(200), bazookaAmmo.assign(8)], 
    outputs: [bazookaOutput]);


There are two main components to the code example above. The setup phase consists of setting up the fuzzy language variables (FLVs) and the rule set. This is normally done once per runtime only. The rest of the code is normally run periodically, or every time a decision is needed. It consists of creating placeholder variable(s) and resolving them using the rule set and given (crisp) values.

Fuzzy Language Variables and Values #

You can use the generic FuzzyVariable, but in most cases, you want to subclass it as follows:

class Distance extends FuzzyVariable<num> {
  var Close = new FuzzySet.LeftShoulder(0, 25, 150);
  var Medium = new FuzzySet.Triangle(25, 150, 300);
  var Far = new FuzzySet.RightShoulder(150, 300, 400);
  Distance() {
    sets = [Close, Medium, Far];

This creates a fuzzy language variable that can be then instantiated by calling distance = new Distance(). It's fuzzy sets are accessed via distance.Close, distance.Medium and distance.Far.

When decision is needed according to some crisp distance n, you create a fuzzy value from the fuzzy variable by calling distance.assign(n). This value is then passed to a FuzzyRuleBase resolve() method as input.

var currentDistance = distance.assign(200);  // We are 200 meters away.
    inputs: [currentDistance], 
    outputs: [bazookaOutput]);

Fuzzy Rules #

This library uses Dart's operator overloading for easier and more readable definition of fuzzy rules.

(distance.Far & ammo.Loads) >> (bazookaDesirability.Desirable)

Note that the overloaded operators are the bitwise ones, not the boolean ones. It's & for logical AND, | for logical OR, and ~ for logical NOT (as opposed to &&, || and !). This is because the boolean operators cannot be overridden, and – more importantly – the use of slightly different operands helps convey the fact that this is not boolean logic.

Dart will correctly issue a warning if you try to use the boolean operands to construct a fuzzy rule.

Also note the >> operand, meaning THEN. It was chosen for its resemblance to the mathematical implication symbol (⇒). Because the operand has low precedence, the antecedent and the conseqent (what comes before and after the symbol) need to be in brackets.

Operator overloading tends to be controversial and can be very confusing. I am hoping that in this case, its advantages clearly outweigh the disadvantages. You will be writing a lot of rules in your fuzzy language modules. The more terse the symbology, the more readable the rule.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  fuzzylogic: ^0.2.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:fuzzylogic/fuzzylogic.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Mar 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health suggestions

Fix lib/src/set.dart. (-6.31 points)

Analysis of lib/src/set.dart reported 13 hints, including:

line 3 col 1: Prefer using /// for doc comments.

line 12 col 3: Prefer using /// for doc comments.

line 27 col 3: Prefer using /// for doc comments.

line 38 col 3: Prefer using /// for doc comments.

line 51 col 3: Prefer using /// for doc comments.

Fix lib/src/ruleexpressions.dart. (-5.84 points)

Analysis of lib/src/ruleexpressions.dart reported 12 hints, including:

line 3 col 1: Prefer using /// for doc comments.

line 16 col 12: Unnecessary new keyword.

line 32 col 44: Unnecessary new keyword.

line 33 col 44: Unnecessary new keyword.

line 34 col 29: Unnecessary new keyword.

Fix lib/src/variable.dart. (-3.45 points)

Analysis of lib/src/variable.dart reported 7 hints, including:

line 3 col 1: Prefer using /// for doc comments.

line 9 col 3: Prefer using /// for doc comments.

line 14 col 3: Prefer using /// for doc comments.

line 21 col 3: Prefer using /// for doc comments.

line 27 col 22: Unnecessary new keyword.

Fix additional 7 files with analysis or formatting issues. (-11.40 points)

Additional issues in the following files:

  • lib/src/linearmanifold.dart (6 hints)
  • lib/src/value.dart (6 hints)
  • lib/fuzzyhedges.dart (4 hints)
  • lib/src/rulebase.dart (3 hints)
  • lib/src/rule.dart (2 hints)
  • lib/fuzzylogic.dart (1 hint)
  • lib/src/logging.dart (1 hint)

Maintenance issues and suggestions

Provide a file named CHANGELOG.md. (-20 points)

Changelog entries help developers follow the progress of your package. See the example generated by stagehand.

Package is getting outdated. (-67.67 points)

The package was last published 87 weeks ago.

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and fuzzylogic.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.24.0 <3.0.0
logging ^0.11.3 0.11.4
Dev dependencies
test ^1.3.0