# BGA_template.m - function [fitness, total_profit,...

• 2

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 1 - 2 out of 2 pages.

function [fitness, total_profit, total_cons_vio, best_solution] = BGA_template% Solving project selection problme using Binary GA% Generate 4 bits are enough for our problem: 4 projectsnum_bit = 4;% Parameterscrossover_prob = 0.85;mutation_prob = 1/num_bit;num_ind = 5;max_iter = 100;num_parents = floor(num_ind * 0.3);% Generate initial solution% Calculate fitness for the initial populationfitness = [];fitness = cal_fitness(pop);% Sort the individuals in the population according to their fitness values% Note: we are maximising profit (the more profit, the better the% individual)[~, sorted_idx] = sort(fitness, 'descend');pop = pop(sorted_idx,:);termination_flag = false;t=1;while termination_flag == false%Select parents from the population based on their fitness using truncationselectionparents = pop(1:num_parents,:);offerspring = parents;%% apply crossoverfor j=1:floor(num_parents/2)if rand(1) <crossover_prob% Randomly select two individuals from parents% Randomly select a bit as cross point% Swap the bits beyond the cross pointendend%% apply mutation.