Matlab遗传算法优化问题求解的示例代码

代码如下:

function m_main()
clear
clc
Max_gen = 100;% 运行代数
pop_size = 100;%种群大小
chromsome = 10;%染色体的长度
pc = 0.9;%交叉概率
pm = 0.25;%变异概率
gen = 0;%统计代数


%初始化
init = 40*rand(pop_size, chromsome)-20;
pop = init;
fit = obj_fitness(pop);
[max_fit, index_max] = max(fit);
maxfit = max_fit;
[min_fit, index_min] = min(fit);
best_indiv = pop(index_max, :);
%迭代操作
while genrand
                c_pt = round(8*rand+1);
                pop_tp1 = newpop(i, :);pop_tp2 = newpop(i+1, :);
                newpop(i+1, 1:c_pt) = pop_tp1(1, 1:c_pt);
                newpop(i, c_pt+1:chromsome) = pop_tp2(1, c_pt+1:chromsome);
            end
            
        end
        % 变异
        for i = 1:pop_size
            if pm>rand
                m_pt = 1+round(9*rand);
                newpop(i, m_pt) = 40*rand-20;
            end
        end
    end
end


你可能感兴趣的:(算法,Matlab,人工智能,科学计算)