⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genetic_operator.html

📁 NSGA-II多目标优化的matlab代码
💻 HTML
字号:
<html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">   <head>      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">         <!--This HTML is auto-generated from an M-file.To make changes, update the M-file and republish this document.      -->      <title>function f  = genetic_operator(parent_chromosome,pro,mu,mum);</title>      <meta name="generator" content="MATLAB 7.0">      <meta name="date" content="2006-03-07">      <meta name="m-file" content="genetic_operator"><style>body {  background-color: white;  margin:10px;}h1 {  color: #990000;   font-size: x-large;}h2 {  color: #990000;  font-size: medium;}p.footer {  text-align: right;  font-size: xx-small;  font-weight: lighter;  font-style: italic;  color: gray;}pre.codeinput {  margin-left: 30px;}span.keyword {color: #0000FF}span.comment {color: #228B22}span.string {color: #A020F0}span.untermstring {color: #B20000}span.syscmd {color: #B28C00}pre.showbuttons {  margin-left: 30px;  border: solid black 2px;  padding: 4px;  background: #EBEFF3;}pre.codeoutput {  color: gray;  font-style: italic;}pre.error {  color: red;}/* Make the text shrink to fit narrow windows, but not stretch too far in wide windows.  On Gecko-based browsers, the shrink-to-fit doesn't work. */ p,h1,h2,div {  /* for MATLAB's browser */  width: 600px;  /* for Mozilla, but the "width" tag overrides it anyway */  max-width: 600px;  /* for IE */  width:expression(document.body.clientWidth > 620 ? "600px": "auto" );}    </style></head>   <body>      <h1>function f  = genetic_operator(parent_chromosome,pro,mu,mum);</h1><pre class="codeinput"><span class="comment">% This function is utilized to produce offsprings from parent chromosomes.</span><span class="comment">% The genetic operators corssover and mutation which are carried out with</span><span class="comment">% slight modifications from the original design. For more information read</span><span class="comment">% the document enclosed.</span>[N,M] = size(parent_chromosome);<span class="keyword">switch</span> pro    <span class="keyword">case</span> 1        M = 2;        V = 6;    <span class="keyword">case</span> 2        M = 3;        V = 12;<span class="keyword">end</span>p = 1;was_crossover = 0;was_mutation = 0;l_limit = 0;u_limit = 1;<span class="keyword">for</span> i = 1 : N    <span class="keyword">if</span> rand(1) &lt; 0.9        child_1 = [];        child_2 = [];        parent_1 = round(N*rand(1));        <span class="keyword">if</span> parent_1 &lt; 1            parent_1 = 1;        <span class="keyword">end</span>        parent_2 = round(N*rand(1));        <span class="keyword">if</span> parent_2 &lt; 1            parent_2 = 1;        <span class="keyword">end</span>        <span class="keyword">while</span> isequal(parent_chromosome(parent_1,:),parent_chromosome(parent_2,:))            parent_2 = round(N*rand(1));            <span class="keyword">if</span> parent_2 &lt; 1                parent_2 = 1;            <span class="keyword">end</span>        <span class="keyword">end</span>        parent_1 = parent_chromosome(parent_1,:);        parent_2 = parent_chromosome(parent_2,:);        <span class="keyword">for</span> j = 1 : V            <span class="comment">% SBX (Simulated Binary Crossover)</span>            <span class="comment">% Generate a random number</span>            u(j) = rand(1);            <span class="keyword">if</span> u(j) &lt;= 0.5                bq(j) = (2*u(j))^(1/(mu+1));            <span class="keyword">else</span>                bq(j) = (1/(2*(1 - u(j))))^(1/(mu+1));            <span class="keyword">end</span>            child_1(j) = <span class="keyword">...</span>                0.5*(((1 + bq(j))*parent_1(j)) + (1 - bq(j))*parent_2(j));            child_2(j) = <span class="keyword">...</span>                0.5*(((1 - bq(j))*parent_1(j)) + (1 + bq(j))*parent_2(j));            <span class="keyword">if</span> child_1(j) &gt; u_limit                child_1(j) = u_limit;            <span class="keyword">elseif</span> child_1(j) &lt; l_limit                child_1(j) = l_limit;            <span class="keyword">end</span>            <span class="keyword">if</span> child_2(j) &gt; u_limit                child_2(j) = u_limit;            <span class="keyword">elseif</span> child_2(j) &lt; l_limit                child_2(j) = l_limit;            <span class="keyword">end</span>        <span class="keyword">end</span>        child_1(:,V + 1: M + V) = evaluate_objective(child_1,pro);        child_2(:,V + 1: M + V) = evaluate_objective(child_2,pro);        was_crossover = 1;        was_mutation = 0;    <span class="keyword">else</span>        parent_3 = round(N*rand(1));        <span class="keyword">if</span> parent_3 &lt; 1            parent_3 = 1;        <span class="keyword">end</span>        <span class="comment">% Make sure that the mutation does not result in variables out of</span>        <span class="comment">% the search space. For both the MOP's the range for decision space</span>        <span class="comment">% is [0,1]. In case different variables have different decision</span>        <span class="comment">% space each variable can be assigned a range.</span>        child_3 = parent_chromosome(parent_3,:);        <span class="keyword">for</span> j = 1 : V           r(j) = rand(1);           <span class="keyword">if</span> r(j) &lt; 0.5               delta(j) = (2*r(j))^(1/(mum+1)) - 1;           <span class="keyword">else</span>               delta(j) = 1 - (2*(1 - r(j)))^(1/(mum+1));           <span class="keyword">end</span>           child_3(j) = child_3(j) + delta(j);           <span class="keyword">if</span> child_3(j) &gt; u_limit               child_3(j) = u_limit;           <span class="keyword">elseif</span> child_3(j) &lt; l_limit               child_3(j) = l_limit;           <span class="keyword">end</span>        <span class="keyword">end</span>        child_3(:,V + 1: M + V) = evaluate_objective(child_3,pro);        was_mutation = 1;        was_crossover = 0;    <span class="keyword">end</span>    <span class="keyword">if</span> was_crossover        child(p,:) = child_1;        child(p+1,:) = child_2;        was_cossover = 0;        p = p + 2;    <span class="keyword">elseif</span> was_mutation        child(p,:) = child_3(1,1 : M + V);        was_mutation = 0;        p = p + 1;    <span class="keyword">end</span><span class="keyword">end</span>f = child;</pre><p class="footer"><br>         Published with MATLAB&reg; 7.0<br></p>      <!--##### SOURCE BEGIN #####%% function f  = genetic_operator(parent_chromosome,pro,mu,mum);

% This function is utilized to produce offsprings from parent chromosomes.
% The genetic operators corssover and mutation which are carried out with
% slight modifications from the original design. For more information read
% the document enclosed.

[N,M] = size(parent_chromosome);
switch pro
    case 1
        M = 2;
        V = 6;
    case 2
        M = 3;
        V = 12;
end
p = 1;
was_crossover = 0;
was_mutation = 0;
l_limit = 0;
u_limit = 1;
for i = 1 : N
    if rand(1) < 0.9
        child_1 = [];
        child_2 = [];
        parent_1 = round(N*rand(1));
        if parent_1 < 1
            parent_1 = 1;
        end
        parent_2 = round(N*rand(1));
        if parent_2 < 1
            parent_2 = 1;
        end
        while isequal(parent_chromosome(parent_1,:),parent_chromosome(parent_2,:))
            parent_2 = round(N*rand(1));
            if parent_2 < 1
                parent_2 = 1;
            end
        end
        parent_1 = parent_chromosome(parent_1,:);
        parent_2 = parent_chromosome(parent_2,:);
        for j = 1 : V
            % SBX (Simulated Binary Crossover)
            % Generate a random number
            u(j) = rand(1);
            if u(j) <= 0.5
                bq(j) = (2*u(j))^(1/(mu+1));
            else
                bq(j) = (1/(2*(1 - u(j))))^(1/(mu+1));
            end
            child_1(j) = ...
                0.5*(((1 + bq(j))*parent_1(j)) + (1 - bq(j))*parent_2(j));
            child_2(j) = ...
                0.5*(((1 - bq(j))*parent_1(j)) + (1 + bq(j))*parent_2(j));
            if child_1(j) > u_limit
                child_1(j) = u_limit;
            elseif child_1(j) < l_limit
                child_1(j) = l_limit;
            end
            if child_2(j) > u_limit
                child_2(j) = u_limit;
            elseif child_2(j) < l_limit
                child_2(j) = l_limit;
            end
        end
        child_1(:,V + 1: M + V) = evaluate_objective(child_1,pro);
        child_2(:,V + 1: M + V) = evaluate_objective(child_2,pro);
        was_crossover = 1;
        was_mutation = 0;
    else
        parent_3 = round(N*rand(1));
        if parent_3 < 1
            parent_3 = 1;
        end
        % Make sure that the mutation does not result in variables out of
        % the search space. For both the MOP's the range for decision space
        % is [0,1]. In case different variables have different decision
        % space each variable can be assigned a range.
        child_3 = parent_chromosome(parent_3,:);
        for j = 1 : V
           r(j) = rand(1);
           if r(j) < 0.5
               delta(j) = (2*r(j))^(1/(mum+1)) - 1;
           else
               delta(j) = 1 - (2*(1 - r(j)))^(1/(mum+1));
           end
           child_3(j) = child_3(j) + delta(j);
           if child_3(j) > u_limit
               child_3(j) = u_limit;
           elseif child_3(j) < l_limit
               child_3(j) = l_limit;
           end
        end
        child_3(:,V + 1: M + V) = evaluate_objective(child_3,pro);
        was_mutation = 1;
        was_crossover = 0;
    end
    if was_crossover
        child(p,:) = child_1;
        child(p+1,:) = child_2;
        was_cossover = 0;
        p = p + 2;
    elseif was_mutation
        child(p,:) = child_3(1,1 : M + V);
        was_mutation = 0;
        p = p + 1;
    end
end
f = child;##### SOURCE END #####-->   </body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -