📄 genetic_operator.m
字号:
function f = genetic_operator(parent_pop,pc,pm,muc,mum) M = 2; V = 6; MIN = 0; MAX = 1; [N temp]= size(parent_pop); p = 1; was_crossover = 0; was_mutation = 0; for i = 1 : N if (rand < pc) parent_1 = []; parent_2 = []; child_1 = []; child_2 = []; parent_1 = round((N - 1)*rand) + 1; parent_2 = round((N - 1)*rand) + 1; while (isequal(parent_pop(parent_1,1 : V),parent_pop(parent_2,1 : V))) parent_2 = round((N - 1)*rand) + 1; end parent_1 = parent_pop(parent_1,1 : V); parent_2 = parent_pop(parent_2,1 : V); for k = 1 : V u(k) = rand; if u(k) <= 0.5 beita(k) = (2*u(k))^(1/(muc + 1)); else beita(k) = 1/((2*(1 - u(k)))^(1/(muc + 1))); end child_1(k) = 0.5*((1 - beita(k))*parent_1(k) + (1 + beita(k))*parent_2(k)); child_2(k) = 0.5*((1 + beita(k))*parent_1(k) + (1 - beita(k))*parent_2(k)); if (child_1(k) < MIN) child_1(k) = MIN; elseif (child_1(k) > MAX) child_1(k) = MAX; end if (child_2(k) < MIN) child_2(k) = MIN; elseif (child_2(k) > MAX) child_2(k) = MAX; end end child_1(:,V + 1 : M + V) = evaluate_objective(child_1); child_2(:,V + 1 : M + V) = evaluate_objective(child_2); was_crossover = 1; % pm = 0.1 执行变异操作 else parent_3 = []; child_3 = []; parent_3 = round((N - 1)*rand) + 1; parent_3 = parent_pop(parent_3,1 : V); for k = 1 : V r(k) = rand; if (r(k) < 0.5) delta(k) = (2*r(k))^(1/(mum + 1)) - 1; else delta(k) = 1 - (2*(1 - r(k)))^(1/(mum + 1)); end child_3(k) = parent_3(k) + (MAX - MIN)*delta(k); if (child_3(k) < MIN) child_3(k) = MIN; elseif (child_3(k) > MAX) child_3(k) = MAX; end end child_3(:,V + 1 : M + V) = evaluate_objective(child_3); was_mutation = 1; end if (was_crossover) child(p,:) = child_1; child(p + 1,:) = child_2; p = p + 2; was_crossover = 0; elseif(was_mutation) child(p,:) = child_3; p = p + 1; was_mutation = 0; end end f = child;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -