# Fuzzy

This is a proposal for an approach that is not yet part of GNUstep though there is source code

This page is for GNUstep developers. It explains the Fuzzy Logic system from the gnustep-fuzzy library for e.g. updating the GNUstep System such as drawing to screen. See the references immediately if you do not know anything about fuzzy logic systems. You should concur that you understand fuzzy logic before reading anything else on this page. Source code is provided at the bottom of the page.

## What is Fuzzy Logic ?

A fuzzy system is a rule-based systems with inputs and outputs. Each fuzzy rule is associated with a mathematical function. The system is a greedy decision tree algorithm. The rules of a fuzzy system are more random than a decision tree so that it is faster. A decision tree traverses at O(log(n)), the depth of the tree and O(n*log(n)) for a n-ary tree approximately.

The image displays the fuzzy logic function value increasing with temperature which is a linear algebraic function system. Values overlap here and there (see the color of the lines), this is where the name fuzzy comes from A function for cold would be f(x), for warm f(x*x) and for hot f(x*x*x). as f(X) increases the return value increases as does the temperature. Another solution would be to have the return value of a temperature fuzzy logic function increasing with temperature.

As the name fuzzy suggests it uses more general information systems. The functions of the fuzzy logic are most important. For example "extremely" associates with the value x * x * x where x is a floating point number. "a little bit" is associated with sqrt(x) / 1.3 and so on. The fuzzy rules are parsed in succession and yield a value for each natural language word. Thus you can reason as a human what the fuzzy logic should do after a fuzzy function is successful e.g. against some threshold, if x=0 then the "extremely" function i 0 * 0 * 0 = 0 and is smaller than threshold 0.001. The threshold is from Artificial Neural Networks for calculating success of the extremum sought by the network.

## Fuzzy Networks

If you implement all rules in a seperate graph node you can reason your way around a graph.

## Distributed fuzzy networks

The same as a fuzzy network but with a node for each fuzzy logic set or system of nodes of a fuzzy network.

## Inference engines

Inference engines infere rules based on logic such as reasoning with not, and, or and combinations of these logical comparators. You can negate rules and infere less rules this way.

## prolog

Prolog is a formal logic rule system which induces rules based on the logical input clauses and then reasons about them by parsing logic rules. You can ask the prolog interpreter questions about what he induced logically.

## progol

Progol inferes the most specicic clause by using a graph searching algorithm. It parses e.g. by negation so that input clauses can be ruled out. Progol deduces i.e. makes the most general logical assumptions.

## How does it apply to GNUstep ?

You can reason about what or how to draw to an X11/Xquartz window or GS subelement in the window such as a view, a button or a bitmap. You would use a fuzzy paint node which is in a distributed node system. If you switch nodes you either paint or wait (for example with an idle node) or reason (e.g. with an interrupt you must redraw your app screen) and so on. e.g. a mouse node would reason if you are about to click a button and access the region the mouse is in to reason further about what the painting algorithm of the window or rootwindow should do.

### gnustep-fuzzy

gnustep-fuzzy uses a paint node which reasons about when it should paint. It can idle for seconds (or milliseconds) if you like and inferes the rules of the paint node this way. There is a fuzzy function associated with each rule string and a rule associated with a drawing function e.g. paintOn: or paintIdle1Second:. This system can be further distributed from a fuzzy set to a fuzzy node for each rule.

## rule system code example

This is portable C and Objective-C :

```void makePaintRules(OpalFuzzyPaintRuleSystem* sys)
// sys is a node with a fuzzy set (or network in it)
{
/*
sys contains a canvas called _painter and adds here a rule
which redraws the screen, this method is paintOn: with the
canvas as an argument. There are 3 adapters inside sys : self,   painter  and parser
*/
[sys addRule:"paint extremely fast" : paintOn:];

/*
f is a functor object which contains the "extremely" reasoning fuzzy function, implemented as
return (x*x*x); where x is initialized to 0.0 thereunder. You change the x not the threshold
in sys, so if you have a threshold of 0.001, f(x)==0 < threshold, and the rule gets matched
by sys. The fuzzy parser of rules is now still simple. I mean the non-fuzzy function part.
*/
OpalFuzzyFunction *f = [OpalFuzzyFunctionExtremely new];
[f x:.0];
/* here's the same thing the paint thread of e.g. Xquartz event handling sleeps for 1 second */
[sys addRule:"paint not for 1 second" : paintIdle1SecondOn:];
f = [OpalFuzzyFunctionIdle new];
[f x:.0];
[sys addFunction:"paint not for 1 second" : f];
}
```

You can change the fuzzy function parameter so that the threshold should not change.

The threading model of this code is multi-threaded so e.g. drawing to screen happens with interrupts and is managed by a threadpool object which alters the state of your GS windows. Threads can be used to let the fuzzy set parse and change nodes when doing other stuff.

## gnustep-fuzzy API

There are three adapters (manipulators) for a rulesystem as used in the above C function. A rulesystem, painter and parser instance wrapper. These can combine the full features with rulesystem parse methods such as the PaintRuleSystem class:

OpalFuzzyRuleSystem API :

```@interface OpalFuzzyRuleSystem : NSObject
{
CGFloat _threshold;

// this is a dictionary with rule-paint method key-value pairs
OpalFuzzyDB *_rules;
// this is a dictionary with rule-fuzzy logic function key-value pairs
OpalFuzzyDB *_functions;
}

// parseFor:On: calls match:On:
-(void) parseFor:(OpalFuzzyPredicate*)predicate On:(id)o;
// On: is the painting system i.e. where you draw on. preficate gets matched against the rules.
- (id) match:(OpalFuzzyPredicate*)predicate On:(id)o;

// These are base class adapters :
- (id) createManipulator;
- (id) createArgumentManipulator;
- (id) createParserManipulator;
```

OpalFuzzyPaintRuleSystem API :

private _painter and _parser variables where the painter is something to paint or draw on, the parser is the paint rulesystem parser and belongs in a OpalFuzzyNode, which itself is a distributed node for painting or paint node.

Here is the interface :

```// painter and parser private variable get/set
- (id) new:(id)painter withParser:(id)parser;
- (void)setPaintSystem:(id)paintsystem;
- (id)painter;
- (void)setParser:(id)parser;
- (id)parser;

// this is for painting on itself, it is not used
- (void)paintOn;

// this paints on the painter adapter
- (void)paintOn:(id)painter;

// this idles updating with painting/drawing
- (void)paintIdle1SecondOn:(id)painter;

// this is the general predicate variable mathching then drawing on id o :
- (id) match:(OpalFuzzyPredicate*)predicate On:(id)o;

/* The following is from the code itself :
* NOTE :
* Call the createManipulator method and you can paint on this
* painter system with rules from the super class.

* The PaintManipulator then hashes the method paintOn: in the dictionary
* and paints on the _painter of the dispatched class due to a call
* of e.g. parseFor:"paint extremely". parseFor is in the super class and has On: for the
* the painter/canvas system to draw on. It gets passed to the match:On: method above.
*
* The super class parseFor:On: method can also be used as a rule (rules have
* a NSString key and a SEL in the form of performSelector:withObject:, so
* only a single argument in e.g. parseForOn: implemented with the API.
* Manipulator stands for adapter or wrapper design patterns.
*/
- (id) createManipulator;
- (id) createArgumentManipulator;
- (id) createParserManipulator;
```