Skip to content

Add Simple GUI for Fuzzy Logic Experimentation and Code Generation #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,47 @@ To make it possible to write fuzzy logic in the most pythonic and simplest way i
* Domain and Set uses an assignment trick to make it possible to instantiate Set() without passing domain and name over and over (yet still be explicit, just not the way one would normally expect). This also allows to call sets as Domain.attributes, which also normally shouldn't be possible (since they are technically not attributes). However, this allows interesting things like dangling sets (sets without domains) that can be freely combined with other sets to avoid cluttering of domain-namespaces and just have the resulting set assigned to a domain to work with.

# Installation
```
pip install fuzzylogic
```

Note: If you want to use the experimental GUI, you'll also need matplotlib:
```
pip install matplotlib
```

# GUI for Experimentation

The library now includes a web-based GUI for experimenting with fuzzy logic and generating code! This makes it easy to:

- Visually create and test fuzzy logic systems
- Experiment with different membership functions
- Generate Python code for your fuzzy logic setup
- Plot and visualize fuzzy sets

## Starting the GUI

```python
import fuzzylogic

# Start the GUI (opens browser automatically)
fuzzylogic.run_gui()

# Or from command line
# python -m fuzzylogic.gui.cli
```

![Fuzzy Logic GUI Screenshot](https://github.com/user-attachments/assets/3c3c4de2-0caf-4e29-9623-576be5c9a93b)

## GUI Features

- **Create Domains**: Define fuzzy logic domains with custom ranges
- **Add Fuzzy Sets**: Create sets using R (rising), S (falling), triangular, trapezoid, and rectangular functions
- **Visualization**: Real-time plotting of fuzzy sets
- **Test Values**: Interactive testing of input values
- **Code Generation**: Automatic Python code generation

# Documentation
Just enter
`python -m pip install fuzzylogic`
in a commandline prompt and you should be good to go!
Expand Down
15 changes: 14 additions & 1 deletion src/fuzzylogic/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
__version__ = (1, 5, 0)
__version__ = (1, 5, 0)

def run_gui(port=8000):
"""Start the fuzzy logic experimentation GUI.

Args:
port: Port to run the web server on (default: 8000)
"""
try:
from .gui.app import run_gui as _run_gui
_run_gui(port)
except ImportError as e:
print(f"GUI dependencies not available: {e}")
print("Please install matplotlib if you haven't already: pip install matplotlib")
88 changes: 88 additions & 0 deletions src/fuzzylogic/gui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Fuzzy Logic GUI

This directory contains a web-based GUI for experimenting with fuzzy logic and generating Python code.

## Features

- **Create Domains**: Define fuzzy logic domains with custom ranges and resolution
- **Add Fuzzy Sets**: Create fuzzy sets using various membership functions:
- R (Rising): Sigmoid-like function that rises from 0 to 1
- S (Falling): Sigmoid-like function that falls from 1 to 0
- Triangular: Triangle-shaped membership function
- Trapezoid: Trapezoid-shaped membership function
- Rectangular: Rectangular/plateau membership function
- **Visualization**: Plot domains and their fuzzy sets using matplotlib
- **Test Values**: Test input values against all sets in a domain
- **Code Generation**: Generate Python code that recreates your fuzzy logic setup

## Usage

### Command Line

Start the GUI from the command line:

```bash
# From the repository root
python -m fuzzylogic.gui.cli

# Or with custom port
python -m fuzzylogic.gui.cli --port 8080

# Don't open browser automatically
python -m fuzzylogic.gui.cli --no-browser
```

### Python API

Start the GUI programmatically:

```python
import fuzzylogic

# Start the GUI (will open browser automatically)
fuzzylogic.run_gui()

# Or with custom port
fuzzylogic.run_gui(port=8080)
```

### Direct Module Usage

```python
from fuzzylogic.gui.app import run_gui

# Start the server
run_gui(port=8000)
```

## Example Workflow

1. **Create a Domain**: Enter a name (e.g., "temperature"), set the range (0-40), and click "Create Domain"

2. **Add Fuzzy Sets**:
- Select the domain from the dropdown
- Enter a set name (e.g., "cold")
- Choose function type (e.g., "S" for falling)
- Set parameters (e.g., low=0, high=15)
- Click "Add Set"

3. **Visualize**: Select the domain and click "Plot Domain" to see a graph of all fuzzy sets

4. **Test Values**: Enter a test value and see the membership degrees for each set

5. **Generate Code**: Click "Generate Python Code" to get Python code that recreates your setup

## Implementation Details

The GUI is implemented as a simple HTTP server that serves a single-page web application. It uses:

- Pure Python with built-in `http.server` (no external web framework dependencies)
- Matplotlib for plotting (with Agg backend for server-side rendering)
- HTML/CSS/JavaScript for the frontend
- JSON API for communication between frontend and backend

## Files

- `app.py`: Main GUI application with web server and fuzzy logic interface
- `cli.py`: Command-line interface for starting the GUI
- `__init__.py`: Module initialization
1 change: 1 addition & 0 deletions src/fuzzylogic/gui/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""GUI module for fuzzy logic experimentation."""
9 changes: 9 additions & 0 deletions src/fuzzylogic/gui/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3
"""
Main entry point for the fuzzy logic GUI command.
"""

from fuzzylogic.gui.cli import main

if __name__ == '__main__':
main()
Loading