Ruby test of fuzzy logic

Overview

PyPi module N/A
git repository https://bitbucket.org/arrizza-public/test-fuzzy-logic
git command git clone git@bitbucket.org:arrizza-public/test-fuzzy-logic.git
Verification Report https://arrizza.com/web-ver/test-fuzzy-logic-report.html
Version Info
OS Language #Runs Last Run Cov%
Ubuntu 24.04 noble Ruby 3.2.3 731 2025-04-14 93.82

Summary

This project uses fuzzy logic in a test scenario.

The test app attempts to steer left and right using fuzzy parameters to indicate which direction and how far to steer within the bounds of the parameter.

This is actually much easier done with a simple math formula. But I used fuzzy logic instead to determine if it can be used and also to ensure that it was easy to double-check the result was correct.

How it works

In short, it does work.

The basic idea is to use multiple overlapping fuzzy parameters to cover the span the steering has to work within. Each of the fuzzy parameters contributes to the total. The total is the amount the app has to steer to get to the wanted spot.

In this case 0.0 to 5.0 is the span the app should be able to steer within. It drives left to right and then flips right to left, adding some small random value each time it veers left or right.

It finds the correct value quickly and IMO steers like a human. It veers heavily at first and then nudges a bit more and then a bit more after that as needed to find the final line it needs to be on.

Conclusion

If this technique was used for a more complicated scenario e.g. finding a "comfortable" (what that means!) temperature for a thermometer, finding the right layout of the fuzzy parameters would be critical. The choices of those parameter defines what a good solution means so it must be chosen carefully.

For this example, finding the right 3 parameters was not straightforward but was simple enough for test purposes. A real-life application would, I believe, be much more difficult.

How to use

  • run
./doit

Typical output:

---> [ 0] start wanted :  0.00 curr:  7.10                   <== current posn is 7.10, but want to be at 0.0
     [ 0]   wanted:  0.00 actual:  5.10  diff:  5.10
     [ 1]   wanted:  0.00 actual:  3.10  diff:  3.10
     [ 2]   wanted:  0.00 actual:  1.10  diff:  1.10
     [ 3]   wanted:  0.00 actual:  0.27  diff:  0.27
     [ 4]   wanted:  0.00 actual:  0.09  diff:  0.09        <== takes 5 iterations to get there
     ------------
---> [ 1] start wanted :  1.70 curr:  0.09                  <== now steer towards 1.70, i.e. drive to the right
     [ 0]   wanted:  1.70 actual:  0.70  diff: -1.00
     [ 1]   wanted:  1.70 actual:  1.37  diff: -0.33
     [ 2]   wanted:  1.70 actual:  1.59  diff: -0.11
---> >>> at wanted:  1.70 actual:  1.59
     ------------
---> [ 2] start wanted :  3.42 curr:  1.59                  <== now steer towrds 3.42
<snip>
---> [ 4] start wanted :  3.36 curr:  4.99                  <== hit all the way right...
<snip>
---> [ 5] start wanted :  1.64 curr:  3.47                  <== now drive left
<snip>

- John Arrizza