0% found this document useful (0 votes)
7 views2 pages

GA Optimization

Uploaded by

andrew Smith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

GA Optimization

Uploaded by

andrew Smith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

% Load your data

load('successssss.mat'); % Load your dataset

% Define parameters
populationSize = 50; % Number of individuals in the population
numGenerations = 100; % Number of generations
numWeights = 11; % Number of weights to optimize (change as needed)
mutationRate = 0.1; % Probability of mutation

% Initialize population
population = rand(populationSize, numWeights) * 2 - 1; % Random weights between -1 and 1

% Fitness function (to minimize)


function fitness = evaluateFitness(weights, input, target)
net = feedforwardnet(numWeights);
net = configure(net, input, target);
net.IW{1} = weights'; % Set weights
output = net(input);
fitness = mean((output - target).^2); % Mean Squared Error
end

% Main GA loop
for generation = 1:numGenerations
fitnessScores = zeros(populationSize, 1);

% Evaluate fitness for each individual


for i = 1:populationSize
fitnessScores(i) = evaluateFitness(population(i, :), datanice, targetOutputs);
end

% Selection (tournament selection)


selectedIndices = tournamentSelection(fitnessScores, populationSize);
selectedPopulation = population(selectedIndices, :);

% Crossover
offspring = crossover(selectedPopulation);

% Mutation
offspring = mutate(offspring, mutationRate);

% Create new population


population = [selectedPopulation; offspring];

% Display best fitness for the generation


bestFitness = min(fitnessScores);
disp(['Generation ' num2str(generation) ', Best Fitness: ' num2str(bestFitness)]);
end

% Function for tournament selection


function selectedIndices = tournamentSelection(fitnessScores, populationSize)
tournamentSize = 5;
selectedIndices = zeros(populationSize, 1);

for i = 1:populationSize
competitors = randi(populationSize, tournamentSize, 1);
[~, bestIdx] = min(fitnessScores(competitors));
selectedIndices(i) = competitors(bestIdx);
end
end

% Function for crossover


function offspring = crossover(selectedPopulation)
[numSelected, numWeights] = size(selectedPopulation);
offspring = zeros(numSelected, numWeights);

for i = 1:2:numSelected-1
parent1 = selectedPopulation(i, :);
parent2 = selectedPopulation(i+1, :);

% One-point crossover
crossoverPoint = randi(numWeights - 1);
offspring(i, :) = [parent1(1:crossoverPoint), parent2(crossoverPoint + 1:end)];
offspring(i+1, :) = [parent2(1:crossoverPoint), parent1(crossoverPoint + 1:end)];
end
end

% Function for mutation


function mutatedPopulation = mutate(population, mutationRate)
mutatedPopulation = population;
[numIndividuals, numWeights] = size(population);

for i = 1:numIndividuals
for j = 1:numWeights
if rand < mutationRate
mutatedPopulation(i, j) = rand * 2 - 1; % New random weight between -1 and 1
end
end
end
end

You might also like