# Fuzzy logic

## Overview ¶

You might want an agent to…

• Move forward as long as it is unimpeded.

• If it approaches an obstacle, turn left or right if there is room.

• If there is no room, slow down to a stop.

If you try to define this behavior using boolean logic, you will soon have to come up with hard definitions for “how blocked a path is”, “how much is enough room”, “how much to turn”, and so on. If you say “blocked” is when an obstacle is `distance <= 20m`, then the agent will not react to the obstacle at 20.1m.

With fuzzy logic, we can say that a distance of 20m has a “blocked” value of 1, and a distance of 40m has a blocked value of 0. The distances between 40m and 20m would have an increasing gradient of “blocked-ness”, instead of a hard flip from “not blocked” to “blocked” at 20m.

## Fuzzy logic basics ¶

Fuzzy logic is a useful tool for building complex crowd behaviors. Unlike boolean logic where each variable is either false or true, fuzzy logic allows a range of possible truth values between 0 (totally false) and 1 (totally true). It allows you to specify behaviors such as how to react to an obstacle relatively informally.

### Fuzzy sets ¶

In boolean logic, a statement is either true or false. For example, the statement “X is tall” is either true or false… X is either tall or not tall. To make the statement testable, we can set a cutoff, say 180cm, and only say greater than or equal to 180cm is “tall”.

However, this doesn’t capture how we really think about the world. It leads to unintuitive situations like two people who are extremely close in height (say 179cm and 180cm) being called “short” and “tall”. We consider people to be “tall” to varying degrees (for example, “not tall”, “almost tall”, “somewhat tall”, “very tall”) without clear lines between them. Our concept of “tall” is fuzzy.

 truth height

In fuzzy logic, a statement can have degrees of truth. We can express this as a function that takes an input value (such as height) and outputs a truth value that expresses how true the statement “X is tall” is for a given height. For example, a height of 1.6m is a bit “tall”, but less so than 1.8m.

We can also think of this as the degree to which X is a member of the “tall” set.

 truth height

A single input value can be a member of multiple sets to different degrees. For example, we might establish three sets: “short”, “medium”, and “tall”, with three functions for determining the membership of a given value.

Membership can overlap. For example, X might be “0.1 short and 0.9 medium” at the same time. (To make things easier to think about, the truth values of the different sets should always add up to 1.)

 truth height

Converting an input value into these truth values for each set is called fuzzifying the input.

### Rules ¶

We can derive other set memberships from initial memberships using if…then rules. For example, imagine we are building a control system for a train. We want a smooth ride for passengers, so we make the braking acceleration of the train dependent on the distance from the train to the next train in front, and the current speed of the train.

We have the two input fuzzy variables: distance and speed.

• Distance is the train’s distance from the next train. The fuzzy sets for distance are near, medium, and far.

• Speed is the current speed of the train. The fuzzy sets for speed are slow, normal, and fast.

We have a derived fuzzy variable: braking. Braking will be calculated from the distance and speed. Braking can be a member of the fuzzy sets none, low, medium, and high.

The following rules translate speed and distance into braking:

If…

Then

`speed` is `fast` and `distance` is `close`

`braking` is `high`

`speed` is not `slow` and `distance` is `far`

`braking` is `low`

`speed` is `normal` and (`distance` is `close` or `distance` is `medium`)

`braking` is `medium`

else

`braking` is `none`

Just like boolean logic can combine true/false values using And, Or, and Not, fuzzy logic has equivalent Fuzzy And, Fuzzy Or, and Fuzzy Not operations. Combining fuzzy inputs with fuzzy operators outputs fuzzy sets. For example, the output of the above rules for a given speed and distance might be that the braking variable is “0 none, 0.1 low, 0.7 medium, 0.2 high”.

### De-fuzzifying outputs ¶

In most cases, knowing the set memberships are not useful for directing behavior. Instead we want a hard number we can then use somewhere else. In the train braking example above, we need to translate the fuzzy braking sets into a single number for how much force to apply to the brakes (for example, in a range of 0-100). De-fuzzifying is the process to take fuzzy set memberships and translate them into a crisp value. For example, a fuzzy `braking` variable might be `0 low, 0.2 medium, 0.7 high`, which might translate to a crisp output of `braking = 60`.

Defuzzifying involves aggregating the fuzzy sets. In Houdini there are two methods to do this: use the function with the strongest truth value (maximum), or add the membership functions together (sum).

Before aggregation, each membership function is clipped by the truth value for that fuzzy set. Any value greater than the truth value is clamped to the truth value.

This means the aggregated fuzzy set is a function of both the system (the membership functions that are defined) and the input (the variables that infer the truth of each set).

The aggregate set forms as polygon, as seen in this graph. Houdini takes the centroid of the graph to get the crisp output value.

## Fuzzy logic in Houdini ¶

In Houdini, you can create fuzzy logic networks using VOPs. For example, you can use fuzzy logic nodes inside a POP VOP DOP to control attributes on agents in a crowd simulation.

For directing crowd state transitions, you can also use a Crowd Fuzzy Logic DOP which wraps a POP VOP DOP to automatically create Bind VOPs when a Crowd Trigger is wired into it. ### Fuzzy VOPs ¶

The following VOPs let you implement fuzzy logic networks.  Fuzzy Input VOP Fuzzifies crisp inputs into a truth value for a single fuzzy set. This node lets you choose from a set of preset membership functions for the new set, or you can draw a custom curve using a ramp parameter. Fuzzy Input outputs a truth value (a floating point number from 0-1) and a `FuzzySet` struct containing information about the fuzzy set (including the range, the membership function, and the truth value).  Fuzzy And  Fuzzy Or  Fuzzy Not Combine fuzzy variables. A network of these operations corresponds to the “If” part of a rule. The output is the strength of the rule.  Fuzzy Inference VOP Derives new fuzzy sets. This node corresponds to the “Then” part of a rule. Fuzzy Inference can aggregates multiple rule strengths using a function (maximum or sum). It then uses the rule strength and a membership function to derive a truth value in a new fuzzy set. The node outputs a truth value and a `FuzzySet` struct containing information about the new fuzzy set.  Fuzzy Inference Mirror Like Fuzzy Inference but it creates two new fuzzy sets: the one created by Fuzzy Inference and its inverse. This is useful when you want some behaviors to be based on how true something is, and other behaviors based on how untrue it is.  Fuzzy Defuzz De-fuzzifies a `FuzzySet` input into a crisp output.

## Using senses as fuzzy inputs ¶

The Fuzzy Obstacle Sense VOP sends out a number of rays within a view cone in front of an agent, out to a maximum distance. This produces an array of distances.

For convenience, the hit distance is subtracted from the maximum, so closer hits are higher numbers, and misses are `0`.

In this example, three rays hit nearby agents and two miss, producing an array `{20, 60, 0, 0, 30}`.

You can wire the array produced by Fuzzy Obstacle Sense into the Fuzzy Input VOP. The Fuzzy Input node uses a function or ramp parameter to weight the values in the array and convert them to a single truth value and fuzzy set.

In this example, the red bars are the array of “closeness” values from a sensor node. To create a “Left Side is Blocked” fuzzy set, we draw a ramp that weights angles on the left higher than angles on the right. The Fuzzy Input node essentially divides the area of the bars by the remaining area under the curve to get a single truth value.

# Crowd simulations

## Getting started ¶

• An overview of Houdini crowd simulation concepts.

• How to set up and edit a crowd simulation.

## The moving parts ¶

• About agents, the moving actors that make up a crowd simulation.

• About agent states, the virtual mood of each agent that controls the agent’s animation and the behaviors it runs.

• How to specify conditions that cause agents to change from one state to another.

## SOP crowds ¶

• How to use SOP motion paths to modify the behavior of a small number of agents.

• Attributes for SOP crowd motion paths.

## Behavior ¶

• Useful attributes for reading in triggers, or that you can set to affect behavior.

• How the Houdini crowd solver decides which behaviors to apply to an agent at each time step.

• Tips on setting up interaction between agents and other types of dynamics.

## Appearance ¶

• How to create a more realistic crowd by making agents look and act differently.

• You can add and constrain vellum cloth as part of agent shape geometry, and then simulate the cloth based on agent movements.

## Terrain ¶

• How to set up agents to adapt their animation to terrain and prevent skating.

• How to specify terrain geometry for agents to walk across.

• How to set up obstacles for agents to avoid.