{"id":23107792,"url":"https://github.com/corzed/n-body-problem-ai","last_synced_at":"2025-08-04T02:14:13.903Z","repository":{"id":259255787,"uuid":"877430016","full_name":"Corzed/N-Body-Problem-AI","owner":"Corzed","description":"AI tries to solve the N-Body-Problem","archived":false,"fork":false,"pushed_at":"2024-11-13T20:03:40.000Z","size":47,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-03T22:44:04.205Z","etag":null,"topics":["3-body-problem","3bodyproblem","ai","n-body-problem","nbodyproblem","neat","neat-python","pygame"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Corzed.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-23T16:26:51.000Z","updated_at":"2024-11-13T20:03:43.000Z","dependencies_parsed_at":"2024-10-23T23:11:11.398Z","dependency_job_id":"dabb0167-77df-490e-b4ce-645910dc82e5","html_url":"https://github.com/Corzed/N-Body-Problem-AI","commit_stats":null,"previous_names":["corzed/n-body-problem-ai"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Corzed/N-Body-Problem-AI","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Corzed%2FN-Body-Problem-AI","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Corzed%2FN-Body-Problem-AI/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Corzed%2FN-Body-Problem-AI/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Corzed%2FN-Body-Problem-AI/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Corzed","download_url":"https://codeload.github.com/Corzed/N-Body-Problem-AI/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Corzed%2FN-Body-Problem-AI/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268639826,"owners_count":24282673,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-04T02:00:09.867Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["3-body-problem","3bodyproblem","ai","n-body-problem","nbodyproblem","neat","neat-python","pygame"],"created_at":"2024-12-17T01:16:09.206Z","updated_at":"2025-08-04T02:14:13.607Z","avatar_url":"https://github.com/Corzed.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# N-Body Problem Simulation with NEAT AI 🪐🤖\n\nWelcome to the **N-Body Problem Simulation with NEAT AI** project! This Python-based simulation leverages the power of the **NEAT (NeuroEvolution of Augmenting Topologies)** algorithm to evolve and simulate the motion of multiple planetary bodies under gravitational forces. This project uses Pygame for visualizations and NEAT-Python for neural network evolution.\n\n## Table of Contents\n- [Overview](#overview)\n- [Features](#features)\n- [Installation](#installation)\n- [Usage](#usage)\n- [Configuration](#configuration)\n- [Project Structure](#project-structure)\n- [How the AI Works](#how-the-ai-works)\n- [Visualization](#visualization)\n- [License](#license)\n\n## Overview 📜\nThis project simulates an **N-body gravitational system**, where planets are attracted to each other following Newton's law of gravitation. It uses a **neural network** evolved with **NEAT** to determine the initial conditions (position and velocity) for each planet. The goal is to evolve stable or interesting systems using fitness-based evaluations.\n\nThe core idea is to explore how neural networks can generate stable orbits or controlled planetary systems in a dynamically evolving gravitational context.\n\n## Features ✨\n- **Gravitational simulation** of multiple planetary bodies.\n- **NEAT-based neural network** evolution to discover optimal initial conditions.\n- **Fitness visualization** over generations.\n- **Pygame-based real-time visualization** of the simulation.\n- Configurable planet count and other simulation parameters.\n\n## Installation 🛠️\n\n1. **Clone the Repository**:\n   ```bash\n   git clone https://github.com/corzed/n-body-problem-ai.git\n   cd n-body-neat-simulation\n   ```\n\n2. **Create a Virtual Environment** (Optional but recommended):\n   ```bash\n   python -m venv env\n   source env/bin/activate   # On Windows, use `env\\Scripts\\activate`\n   ```\n\n3. **Install Dependencies**:\n   ```bash\n   pip install -r requirements.txt\n   ```\n\n   Make sure you have the following dependencies:\n   - `pygame`\n   - `neat-python`\n   - `numpy`\n   - `matplotlib`\n\n4. **Run the Simulation**:\n   ```bash\n   python ai.py\n   ```\n\n## Usage 🚀\nAfter running the script, the program will prompt you to select the number of planets to simulate. You can then choose to start a new training session or load from a saved checkpoint.\n\n### Keyboard Controls:\n- **Up/Down Arrow Keys**: Adjust simulation speed.\n- **Spacebar**: Pause or resume the simulation.\n\n### Replay Options:\n- **'r'**: Replay a specific genome using its ID.\n- **'b'**: Replay the best genome so far.\n- **'p'**: Plot the fitness history over generations.\n- **'q'**: Quit the program.\n\n## Configuration ⚙️\nThe configuration file for NEAT is automatically generated as `config-feedforward.txt` and contains adjustable parameters for:\n- Population size\n- Mutation rates\n- Number of inputs/outputs\n- Activation functions\n\nFeel free to modify this file as needed to experiment with different evolutionary parameters.\n\n## Project Structure 📂\n```plaintext\nn-body-neat-simulation/\n│\n├── ai.py                    # Main script for simulation and neural network evolution\n├── config-feedforward.txt   # Configuration file for NEAT\n├── requirements.txt         # Dependencies\n└── README.md                # Project documentation\n```\n\n## How the AI Works 🤖🧠\nThe core algorithm used in this simulation is **NEAT (NeuroEvolution of Augmenting Topologies)**, which evolves a neural network to optimize the initial conditions for the planetary bodies. Here's a deeper look into how this works:\n\n### 1. **Neural Network Structure**\nThe neural network evolved by NEAT takes a simple input vector to generate initial positions and velocities for each planet. The inputs consist of a **constant bias** (e.g., 1.0) to allow the neural network to generate consistent outputs. The outputs represent:\n\n- **X and Y positions** of each planet.\n- **X and Y velocities** of each planet.\n\n### 2. **Evolution via NEAT**\nThe NEAT algorithm optimizes the neural network's weights, connections, and even its topology to find the best configuration for creating stable planetary orbits. This is achieved through:\n\n- **Population-based search**: A population of neural networks is evolved simultaneously.\n- **Fitness Evaluation**: Each neural network's fitness is determined based on how well the simulated planetary system behaves. Stability and lifespan are key indicators of good performance.\n\n### 3. **Fitness Calculation**\nThe fitness function measures the stability and behavior of the planetary system:\n\n- **Longevity of the system**: The main objective is to evolve networks that can create planetary systems that exist without collisions or escapes for as long as possible.\n- **Penalties for collisions and escapes**: If planets collide or escape beyond a threshold, the simulation terminates early, and penalties are applied to the fitness score.\n\n### 4. **Mutation and Crossover**\nNEAT applies mutations and crossovers to generate a new population in each generation:\n\n- **Mutations** involve adding or removing connections between nodes, or adjusting weights and biases.\n- **Crossover** combines successful neural networks to create offspring with a mix of features from both parents.\n\n### 5. **Training and Visualization**\nThe neural networks are trained over multiple generations, with the fitness of the best neural network being plotted over time. You can visualize the results in real time using **Pygame**.\n\n## Visualization 🎨\nThe simulation is visualized using **Pygame**, which displays the current planetary system's state. Information such as generation, genome ID, fitness, and simulation speed is displayed in the Pygame window.\n\n![Simulation Screenshot](image_2024-10-23_123227855.png)\n\n## License 📜\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n\n---\n\nHappy Simulating! 😊🚀🌌\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorzed%2Fn-body-problem-ai","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcorzed%2Fn-body-problem-ai","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorzed%2Fn-body-problem-ai/lists"}