Skip to content

JakeIV/AI-Coursework

Repository files navigation

AI-Coursework

GENETIC ALGORITHM

To run the genetic algorithm open the GA.mlx file and run it, this will run the genetic algorithm and save the best gait as a video and also as files that can be used to train the neural network. The GA.m file is the same just saved in plain text format. The files we have generated are stored in the Genereated Files directory, there is a video of the gait we have generated. When the GA.mlx file is run, it will output those files in the Genetic algorithm directory, the dataset file can then be used to train the neural network.

Neural Network for Full Spider Gait

This part of the project trains a small neural network to reproduce the gait generated by the genetic algorithm in Part 1. The GA produces a full 300-frame walking cycle for all eight legs. The neural network then learns to map a single phase value (0 to 2π) to all 24 joint angles. The result is a compact model that can recreate the same gait without re-running the GA. A further explanation of the neural network architecture can be found in the NN explanation.txt file in the Neural network directory

How to train a model

file dataset_alllegs(1).mat needs to be in the same directory as train_nn_alllegs.m. In the MATLAB command window, run: train_nn_alllegs The script will:

load the dataset

build the training and validation splits

train the multi-layer perceptron

display the loss curves and prediction plots

save the trained model as nn_alllegs_simple.mat

After it finishes, you should see nn_alllegs_simple.mat appear in the folder.

How to run the predictor

Ensure that nn_alllegs_simple.mat exists (from the training step).

Make sure plot_spider_pose.m is also available in the same directory.

In the MATLAB command window, run:

predict_pose_nn_alllegs

This will: load the trained model generate the full 24-angle trajectory across a cycle show plots of each joint over time display one static spider pose predicted by the network Files used: train_nn_alllegs.m Trains the network using the dataset from the GA and saves the trained model as nn_alllegs_simple.mat.

predict_pose_nn_alllegs.m Loads the trained model and visualises the output. It shows the predicted joint angles over the gait cycle and can display a static spider pose for one frame.

dataset_alllegs(1).mat The dataset produced by run_ga_mirror_alllegs.m. It contains the phases for one full gait cycle and the corresponding 24 joint angles (three per leg).

nn_alllegs_simple.mat The trained neural network model. This file is created automatically by train_nn_alllegs.m.

plot_spider_pose.m A helper function that draws a 3D spider pose using a 24-angle vector. Used by the predictor. When running the Tenserflow files for the part of comparison with ML libraries , ensure it is version Python 3.12. Then run pip install -r requirements.txt.

Run spider_ga.py to produce dataset_alllegs.npz Run spider_nn.py to produce spider_nn.keras Run spider_video.py for visual representation of trained data.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors