Tuesday, May 27, 2025

Fuzzy Logic and Quake III Bots

Fuzzy logic is often used in decision-making systems where a detailed mathematical model of the system is unavailable or impractical. Instead of relying on equations, fuzzy logic encodes expert intuition into human-readable rules. These rules allow systems to make decisions based on approximate or linguistic input values, such as “low health” or “enemy nearby.”

For simple systems — say, with just one input and one output — fuzzy logic may be overkill. In those cases, a 1D interpolation (similar to proportional navigation) is often enough to generate smooth behavior transitions. But as systems grow more complex, fuzzy logic scales better than maintaining large interpolation grids or rigid condition trees.

While neural networks have become dominant in many domains, fuzzy logic still offers distinct advantages, especially in embedded or control-focused systems. Fuzzy logic requires structured human insight, while neural networks thrive on raw data and pattern discovery. For complex or poorly understood systems, writing fuzzy rules is impractical. Advantages of fuzzy logic over neural networks:

  1. Interpretability: Fuzzy rules are readable and understandable by developers and domain experts.
  2. Minimal training: Rules encode prior knowledge, reducing or eliminating the need for extensive data-driven training.
  3. Lightweight tuning: At most, fuzzy systems may require optimizing rule weights — a much simpler process than full network training.

One of the most interesting uses of fuzzy logic in gaming came from Quake III Arena. The bots in the game used fuzzy logic to evaluate possible behaviors — such as attack, search for health, search for a better weapon, retreat. Each action was assigned a desirability score based on fuzzy evaluations of current game state (e.g., health, distance to enemy, ammo). At each tick, the bot would choose the highest-scoring action.

To tune the bot parameters, the developers had bots play against each other and applied genetic algorithms to evolve the best-performing rule sets. Of course, they could not make the bots perfect because then a human player would never be able to win.

Monday, May 12, 2025

CPU, Analog, FPGA, or ASIC?

Algorithms can be implemented across a wide spectrum of hardware, each with its own trade-offs in speed, power, flexibility, cost, and scalability. Let’s compare the four main approaches:

1. Software on General-Purpose CPUs

Pros:

  • Easy to develop and debug: Rich toolchains, IDEs, and profiling tools.
  • Highly flexible: Reprogram anytime; modify algorithms at will.
  • Low development cost: No custom hardware needed; ready to run on PCs, servers, or microcontrollers.
  • Ecosystem and libraries: Access to optimized math libraries (e.g., FFTW, NumPy, BLAS).

Cons:

  • Latency and real-time constraints: OS overhead and unpredictable timing make hard real-time difficult. Soft real-time is achievable.
  • Performance limitations: Limited parallelism compared to hardware solutions.
  • High power consumption per operation: Especially inefficient for repetitive, simple tasks.

Ideal for general-purpose applications.

2. Analog Circuits

Pros:

  • Ultra-low latency: Signal is processed in real-time with no sampling delay.
  • Potentially high throughput: Continuous operation with no clock constraints.
  • Minimal power: No digital switching, especially useful in low-power sensors or RF front-ends.
  • No need for ADC/DAC: Processes raw analog signals directly.

Cons:

  • Limited precision: Susceptible to noise, drift, and component tolerances.
  • Hard to scale: Each additional function requires more physical components.
  • Difficult to tune or reconfigure: Redesign often requires physical changes.
  • No programmability: Once built, behavior is fixed or only marginally tunable.

Ideal for real-time sensing, analog filters, RF circuits, ultra-low power embedded front-ends.

3. Field-Programmable Gate Arrays (FPGAs)

Pros:

  • High parallelism: True concurrent execution of multiple operations.
  • Low deterministic latency: Ideal for real-time pipelines.
  • Reconfigurable hardware: Algorithms can be updated post-deployment.
  • Power-efficient: Much better performance-per-watt than CPUs for many tasks.

Cons:

  • Steep learning curve: Requires HDL knowledge (VHDL/Verilog) or high-level synthesis.
  • Toolchain complexity: Longer compile/synthesis times, debugging can be difficult.
  • Moderate development cost: More expensive than CPUs in small volumes.
  • Not optimal for floating-point math: Often better with fixed-point arithmetic.

Ideal for real-time video/audio processing, signal processing, robotics, hardware prototyping.

4. Custom Chips (ASICs)

Pros:

  • Maximum performance: Custom datapaths, memory layouts, and logic yield unmatched throughput.
  • Lowest power consumption: Fully optimized for the task at hand.
  • Smallest footprint: No unnecessary hardware or software overhead.
  • Production cost scales well: Extremely cheap per unit at high volumes.

Cons:

  • Astronomically high NRE (non-recurring engineering) cost: Millions of dollars just to reach first silicon.
  • Long time-to-market: Can take 6–24 months from design to tapeout.
  • Zero flexibility: Bugs in logic mean hardware re-spins.
  • High risk: A single design flaw can cost months of work and millions in losses.

Ideal for high-volume commercial products (e.g., smartphones, wireless chips), aerospace, medical devices, deep learning accelerators. Example: u-blox