Note: This GIT IS AI GENERATED ! This project demonstrates the use of a genetic algorithm to train a neural network controller for balancing an inverted pendulum on a cart. The AI learns to control the cart's movement in order to keep the pendulum upright.
-
Clone the repository:
git clone https://github.com/your-username/inverted-pendulum-ai.git -
Open the project in Godot Engine.
-
Build and run the project.
-
Open the project in Godot Engine.
-
Attach the
Main.gdscript to the main node in your Godot scene. -
Set the
pendulumandcartvariables in the editor to reference the corresponding nodes in your scene. -
Optionally, enable the
enable_visualizationflag in the editor to visualize the neural network weights. -
Run the project to start the AI training process.
-
The AI will automatically save the best model weights to a file named
best_model.resource. -
To load the best model weights, ensure that the
best_model.resourcefile exists in the project directory.
The Config.gd file contains various configuration parameters for the AI:
INPUT_SIZE: The number of input neurons in the neural network.HIDDEN_SIZE: The number of hidden neurons in the neural network.OUTPUT_SIZE: The number of output neurons in the neural network.POPULATION_SIZE: The size of the population in each generation.ELITE_COUNT: The number of elite individuals to preserve in each generation.MUTATION_RATE: The probability of mutation for each gene during reproduction.PENDULUM_LENGTH: The length of the pendulum.CART_WIDTH: The width of the cart.CART_HEIGHT: The height of the cart.THRESHOLD: The threshold angle for considering the pendulum balanced.
Modify these parameters in the Config.gd file to customize the AI behavior.
The project is structured as follows:
Main.gd: The main script that controls the overall flow of the program.Config.gd: Contains the configuration parameters for the AI.Population.gd: Handles the population management, evaluation, and evolution.NeuralNetwork.gd: Represents the neural network model.FitnessEvaluator.gd: Contains utility functions for evaluating the fitness of individuals.
The project includes an optional visualization feature that allows you to visualize the weights of the best individual's neural network. To enable visualization, set the enable_visualization flag to true in the editor for the main node.
When visualization is enabled, the weights of the best individual's neural network will be displayed as colored rectangles, with the color representing the weight value.
Contributions are welcome! If you find any issues or have suggestions for improvement, please open an issue or submit a pull request.
This project is licensed under the MIT License.