多目标遗传算法代码
% function nsga_2(pro)
%% Main Function
% Main program to run the NSGA-II MOEA.
% Read the corresponding documentation to learn more about multiobjective
% optimization using evolutionary algorithms.
% initialize_variables has two arguments; First being the population size
% and the second the problem number. '1' corresponds to MOP1 and '2'
% corresponds to MOP2.
%inp_para_definition=input_parameters_definition;
%% Initialize the variables
% Declare the variables and initialize their values
% pop - population
% gen - generations
% pro - problem number
%clear;clc;tic;
pop = 100; % 每一代的种群数
gen = 100; % 总共的代数
pro = 2; % 问题选择1或者2,见switch
switch pro
case 1
% M is the number of objectives.
M = 2;
% V is the number of decision variables. In this case it is
% difficult to visualize the decision variables space while the
% objective space is just two dimensional.
V = 6;
case 2
M = 3;
V = 12;
case 3 % case 1和case 2 用来对整个算法进行常规验证,作为调试之用;case 3 为本工程所需;
M = 2; %(output parameters 个数)
V = 8; %(input parameters 个数)
K = 10;
end
% Initialize the population
chromosome = initialize_variables(pop,pro);
%% Sort the initialized population
% Sort the population using non-domination-sort. This returns two columns
% for each individual which are the rank and the crowding distance
% corresponding to their position in the front they belong. 真是牛X了。
chromosome = non_domination_sort_mod(chromosome,pro);
%% Start the evolution process
% The following are performed in each generation
% Select the parents
% Perfrom crossover and Mutation operator
% Perform Selection
for i = 1 : gen
% Select the parents
% Parents are selected for reproduction to generate offspring. The
% original NSGA-II uses a binary tournament selection based on the
% crowded-comparision operator. The arguments are
% pool - size of the mating pool. It is common to have this to be half the
% population size.
% tour - Tournament size. Original NSGA-II uses a binary tournament
% selection, but to see the effect of tournament size this is kept
% arbitary, to be choosen by the user.
pool = round(pop/2);
tour = 2;
%下面进行二人锦标赛配对,新的群体规模是原来群体的一半
parent_chromosome = tournament_selection(chromosome,pool,tour);
% Perfrom crossover and Mutation operator
% The original NSGA-II algorithm uses Simulated Binary Crossover (SBX) and
% Polynomial crossover. Crossover probability pc = 0.9 and mutation
% probability is pm = 1/n, where n is the number of decision variables.
% Both real-coded GA and binary-coded GA are implemented in the original
% algorithm, while in this program only the real-coded GA is considered.
% The distribution indeices for crossover and mutation operators as mu = 20
% and mum = 20 respectively.
mu = 20;
mum = 20;
% 针对对象是上一步产生的新的个体parent_chromosome
%对parent_chromosome 每次操作以较大的概率进行交叉(产生两个新的候选人),或者较小的概率变异(一个新的候选人)操作,这样
%就会产生较多的新个体
offspring_chromosome = genetic_operator(parent_chromosome,pro,mu,mum);
% Intermediate population
% Intermediate population is the combined population of parents and
% offsprings of the current generation. The population size is almost 1 and
% half times the initial population.
[main_pop,temp]=size(chromosome);
[offspring_pop,temp]=size(offspring_chromosome);
intermediate_chromosome(1:main_pop,:)=chromosome;
intermediate_chromosome(main_pop+1:main_pop+offspring_pop,1:M+V)=offspring_chromosom e;
%intermediate_chromosome=inter_chromo(chromosome,offspring_chromosome,pro);
% Non-domination-sort of intermediate population
% The intermediate population is sorted again based on non-domination sort
% before the replacement operator is performed on the intermediate
% population.
intermediate_chromosome = ...
non_domination_sort_mod(intermediate_chromosome,pro);
% Perform Selection
% Once the intermediate population is sorted only the best solution is
% selected based on it rank and crowding distance. Each front is filled in
% ascending order until the addition of population size is reached. The
% last front is included in the population based on the individuals with
% least crowding distance
chromosome = replace_chromosome(intermediate_chromosome,pro,pop);
if ~mod(i,10)
fprintf('%d\n',i);
end
end
%% Result
% Save the result in ASCII text format.
save solution.txt chromosome -ASCII
%% Visualize
% The following is used to visualize the result for the given problem.
switch pro
case 1
plot(chromosome(:,V + 1),chromosome(:,V + 2),'y+');
title('MOP1 using NSGA-II');
xlabel('f(x_1)');
ylabel('f(x_2)');
case 2
plot3(chromosome(:,V + 1),chromosome(:,V + 2),chromosome(:,V + 3),'*');
title('MOP2 using NSGA-II');
xlabel('f(x_1)');
ylabel('f(x_2)');
zlabel('f(x_3)');
end
%disp('run time is:')
%toc; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%
function f = initialize_variables(N,problem)
% function f = initialize_variables(N,problem)
% N - Population size
% problem - takes integer values 1 and 2 where,
% '1' for MOP1
% '2' for MOP2
%
% This function initializes the population with N individuals and each
% individual having M decision variables based on the selected problem.
% M = 6 for problem MOP1 and M = 12 for problem MOP2. The objective space
% for MOP1 is 2 dimensional while for MOP2 is 3 dimensional.
% Both the MOP's has 0 to 1 as its range for all the decision variables.
min = 0;
max = 1;
switch problem
case 1
M = 6;
K = 8; % k=决策变量(M=6)+目标变量(K-M=2)=8
case 2
M = 12;
K = 15;
case 3 % case 1和case 2 用来对整个算法进行常规验证,作为调试之用;case 3 为本工程所需;
M = 8; %(input parameters 个数)
K = 10;
end
for i = 1 : N
% Initialize the decision variables
for j = 1 : M
f(i,j) = rand(1); % i.e f(i,j) = min + (max - min)*rand(1);
end
% Evaluate the objective function
f(i,M + 1: K) = evaluate_objective(f(i,:),problem);
end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%
function f = evaluate_objective(x,problem)
% Function to evaluate the objective functions for the given input vector
% x. x has the decision variables
switch problem
case 1
f = [];
%% Objective function one
f(1) = 1 - exp(-4*x(1))*(sin(6*pi*x(1)))^6;
sum = 0;
for i = 2 : 6
sum = sum + x(i)/4;
end
%% Intermediate function
g_x = 1 + 9*(sum)^(0.25);
%% Objective function one
f(2) = g_x*(1 - ((f(1))/(g_x))^2);
case 2
f = [];
%% Intermediate function
g_x = 0;
for i = 3 : 12
g_x = g_x + (x(i) - 0.5)^2;
end
%% Objective function one
f(1) = (1 + g_x)*cos(0.5*pi*x(1))*cos(0.5*pi*x(2));
%% Objective function two
f(2) = (1 + g_x)*cos(0.5*pi*x(1))*sin(0.5*pi*x(2));
%% Objective function three
f(3) = (1 + g_x)*sin(0.5*pi*x(1));
case 3
f = [];
%% Objective function one
f(1) = 0;
%% Objective function one
f(2) = 0;
end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%
%% Non-Donimation Sort %按照目标函数最小了好
% This function sort the current popultion based on non-domination. All the
% individuals in the first front are given a rank of 1, the second front
% individuals are assigned rank 2 and so on. After assigning the rank the
% crowding in each front is calculated.
function f = non_domination_sort_mod(x,problem)
[N,M] = size(x);
switch problem
case 1
M = 2;
V = 6;
case 2
M = 3;
V = 12;
case 3 % case 1和case 2 用来对整个算法进行常规验证,作为调试之用;case 3 为本工程所需;
M = 2; %(output parameters 个数)
V = 8; %(input parameters 个数)
K = 10;
end
front = 1;
% There is nothing to this assignment, used only to manipulate easily in
% MATLAB.
F(front).f = [];
individual = [];
for i = 1 : N
% Number of individuals that dominate this individual 支配i的解的个数
individual(i).n = 0;
% Individuals which this individual dominate 被i支配的解
individual(i).p = [];
for j = 1 : N
dom_less = 0;
dom_equal = 0;
dom_more = 0;
for k = 1 : M
if (x(i,V + k) < x(j,V + k))
dom_less = dom_less + 1;
elseif (x(i,V + k) == x(j,V + k))
dom_equal = dom_equal + 1;
else
dom_more = dom_more + 1;
end
end
% 这里只要考虑到求取得是函数的最小值就可以了,此时支配的概念就会变为谁的函数值小,谁去支配别的解
%——————————————————————
if dom_less == 0 & dom_equal ~= M % 举个例子,其中i=a,b; j= c,d; 如果i 中的a,b全部大于
individual(i).n = individual(i).n + 1; % 或者部分大于j中的c,d(但没有小于的情况),则称为i优于j,
elseif dom_more == 0 & dom_equal ~= M % 当i优于j的时候,则此时把individual(i)_n加1
individual(i).p = [individual(i).p j]; %如果i中的a,b全部小于或者部分小于j中的c,d(但没有大于的情况),则
end %则称为j优于i, 则把此时的j放入individual(i)_P中;
end %总
之,就是说两个目标变量必须全部大于或者全部小于才能对individual有效。
if individual(i).n == 0 % 如果没有劣于i的话,即F(front).f 存放的都是差的解.
x(i,M + V + 1) = 1;
F(front).f = [F(front).f i];
end
end
% Find the subsequent fronts
while ~isempty(F(front).f)
Q = [];
for i = 1 : length(F(front).f) %i表示最优解if ~isempty(individual(F(front).f(i)).p)
for j = 1 : length(individual(F(front).f(i)).p) % 被前端解i所支配的解得集合,这个集合由j构成,
individual(individual(F(front).f(i)).p(j)).n
=individual(individual(F(front).f(i)).p(j)).n - 1;
if individual(individual(F(front).f(i)).p(j)).n == 0
x(individual(F(front).f(i)).p(j),M + V + 1) =front + 1;
Q = [Q individual(F(front).f(i)).p(j)];
end
end
end
end
front = front + 1;
F(front).f = Q;
end
% function sort : sort(x_sequence)===>>return a increasing orer data sequence
[temp,index_of_fronts] = sort(x(:,M + V + 1));
for i = 1 : length(index_of_fronts)
sorted_based_on_front(i,:) = x(index_of_fronts(i),:); % 对解(染色体)进行排序,按照front分层
end
%到这里分层结束,下面就是计算距离了
current_index = 0;
% Find the crowding distance for each individual in each front
% ,计算不同层的拥挤距离是没有意义的
for front = 1 : (length(F) - 1)
objective = [];
distance = 0;
y = [];
previous_index = current_index + 1;
for i = 1 : length(F(front).f)
y(i,:) = sorted_based_on_front(current_index + i,:);
end
current_index = current_index + i;
% Sort each individual based on the objective
sorted_based_on_objective = [];
for i = 1 : M
[sorted_based_on_objective, index_of_objectives] = sort(y(:,V + i));
sorted_based_on_objective = [];
for j = 1 : length(index_of_objectives)
sorted_based_on_objective(j,:) = y(index_of_objectives(j),:);
end
f_max = ...
sorted_based_on_objective(length(index_of_objectives), V + i); %最大值f_min = sorted_based_on_objective(1, V + i); % 最小值
y(index_of_objectives(length(index_of_objectives)),M + V + 1 + i)...
= Inf; % 最大值放lnf
y(index_of_objectives(1),M + V + 1 + i) = Inf; % 最小值放lnf
for j = 2 : length(index_of_objectives) - 1
next_obj = sorted_based_on_objective(j + 1,V + i);
previous_obj = sorted_based_on_objective(j - 1,V + i);
if (f_max - f_min == 0)
y(index_of_objectives(j),M + V + 1 + i) = Inf;
else
y(index_of_objectives(j),M + V + 1 + i) = ...
(next_obj - previous_obj)/(f_max - f_min);
end
end
end
distance = [];
distance(:,1) = zeros(length(F(front).f),1);
for i = 1 : M
distance(:,1) = distance(:,1) + y(:,M + V + 1 + i);
end
y(:,M + V + 2) = distance;
y = y(:,1 : M + V + 2);
z(previous_index:current_index,:) = y;
end
f = z; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function f = replace_chromosome(intermediate_chromosome,pro,pop)
%% replace_chromosome(intermediate_chromosome,pro,pop)
% This function replaces the chromosomes based on rank and crowding
% distance. Initially until the population size is reached each front is
% added one by one until addition of a complete front which results in
% exceeding the population size. At this point the chromosomes in that
% front is added subsequently to the population based on crowding distance.
[N,V] = size(intermediate_chromosome);
switch pro
case 1
M = 2;
V = 6;
case 2
M = 3;
V = 12;
case 3 % case 1和case 2 用来对整个算法进行常规验证,作为调试之用;case 3 为本工程所需;
M = 2; %(output parameters 个数)
V = 8; %(input parameters 个数)
K = 10;
end
% Get the index for the population sort based on the rank
[temp,index] = sort(intermediate_chromosome(:,M + V + 1));
% Now sort the individuals based on the index
for i = 1 : N
sorted_chromosome(i,:) = intermediate_chromosome(index(i),:);
end
% Find the maximum rank in the current population
max_rank = max(intermediate_chromosome(:,M + V + 1));
% Start adding each front based on rank and crowing distance until the
% whole population is filled.
previous_index = 0;
for i = 1 : max_rank
current_index = max(find(sorted_chromosome(:,M + V + 1) == i));
if current_index > pop
remaining = pop - previous_index;
temp_pop = ...
sorted_chromosome(previous_index + 1 : current_index, :);
temp_pop_1=temp_pop*(-1);
[temp_sort_1,temp_sort_index] = ...
sort(temp_pop_1(:, M + V + 2)); %在编译的时候出现的问题找到了,主要是对sort(a,'descend')并不支持。
temp_sort=temp_sort_1*(-1);
for j = 1 : remaining
f(previous_index + j,:) = temp_pop(temp_sort_index(j),:);
end
return;
elseif current_index < pop
f(previous_index + 1 : current_index, :) = ...
sorted_chromosome(previous_index + 1 : current_index, :);
else
f(previous_index + 1 : current_index, :) = ...
sorted_chromosome(previous_index + 1 : current_index, :);
return;
end
previous_index = current_index;
end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function f = tournament_selection(chromosome,pool_size,tour_size)
% function selection_individuals(chromosome,pool_size,tour_size) is the
% selection policy for selecting the individuals for the mating pool. The
% selection is based on tournament selection. Argument 'chromosome' is the
% current generation population from which the individuals are selected to
% form a mating pool of size 'pool_size' after performing tournament
% selection, with size of the tournament being 'tour_size'. By varying the
% tournament size the selection pressure can be adjusted.
[pop,variables] = size(chromosome);
rank = variables - 1; % 表示代表层数的那一列的索引
distance = variables; % 表示代表拥挤度距离的那一列的索引
for i = 1 : pool_size
for j = 1 : tour_size % 这个loop目的是为了随机找出tour_size个相互不同的候选染色体candidate(j) = round(pop*rand(1));
if candidate(j) == 0
candidate(j) = 1;
end
if j > 1
while ~isempty(find(candidate(1 : j - 1) == candidate(j)))
candidate(j) = round(pop*rand(1));
if candidate(j) == 0
candidate(j) = 1;
end
end
end
end
for j = 1 : tour_size
c_obj_rank(j) = chromosome(candidate(j),rank); % 提取出阶数
c_obj_distance(j) = chromosome(candidate(j),distance); % 提取出距离end
min_candidate =find(c_obj_rank == min(c_obj_rank)); % 找出层数最小的那个候选人
if length(min_candidate) ~= 1 % 发现存在层数相同的候选人,就要比较拥挤度距离了
max_candidate =find(c_obj_distance(min_candidate) == max(c_obj_distance(min_candidate)));
% 返回具有同样最小层数的,且拥挤距离最大的那个候选人
if length(max_candidate) ~= 1
max_candidate = max_candidate(1); % 层数,拥挤距离都相同,随便选个就可以了
end
f(i,:) = chromosome(candidate(min_candidate(max_candidate)),:);
else
f(i,:) = chromosome(candidate(min_candidate(1)),:);
end
end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
%input_parameters=inp_para_definition; %定义了一个输入变量的结构,
[N,M] = size(parent_chromosome); % 注意这里的M其实是个垃圾,后面语句会对其重新赋值利用
switch pro
case 1
M = 2;
V = 6;
case 2
M = 3;
V = 12;
case 3 % case 1和case 2 用来对整个算法进行常规验证,作为调试之用;case 3 为本工程所需;
M = 2; %(output parameters 个数)
V = 8; %(input parameters 个数)
K = 10;
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;
遗传算法在多目标优化的应用:公式,讨论,概述总括
遗传算法在多目标优化的应用:公式,讨论,概述/总括 概述 本文主要以适合度函数为基础的分配方法来阐述多目标遗传算法。传统的群落形成方法(niche formation method)在此也有适当的延伸,并提供了群落大小界定的理论根据。适合度分配方法可将外部决策者直接纳入问题研究范围,最终通过多目标遗传算法进行进一步总结:遗传算法在多目标优化圈中为是最优的解决方法,而且它还将决策者纳入在问题讨论范围内。适合度分配方法通过遗传算法和外部决策者的相互作用以找到问题最优的解决方案,并且详细解释遗传算法和外部决策者如何通过相互作用以得出最终结果。 1.简介 求非劣解集是多目标决策的基本手段。已有成熟的非劣解生成技术本质上都是以标量优化的手段通过多次计算得到非劣解集。目前遗传算法在多目标问题中的应用方法多数是根据决策偏好信息,先将多目标问题标量化处理为单目标问题后再以遗传算法求解,仍然没有脱离传统的多目标问题分步解决的方式。在没有偏好信息条件下直接使用遗传算法推求多目标非劣解的解集的研究尚不多见。 本文根据遗传算法每代均产生大量可行解和隐含的并行性这一特点,设计了一种基于排序的表现矩阵测度可行解对所有目标总体表现好坏的向量比较方法,并通过在个体适应度定标中引入该方法,控制优解替换和保持种群多样性,采用自适应变化的方式确定交叉和变异概率,设计了多目标遗传算法(Multi Objective Genetic Algorithm, MOGA)。该算法通过一次计算就可以得到问题的非劣解集, 简化了多目标问题的优化求解步骤。 多目标问题中在没有给出决策偏好信息的前提下,难以直接衡量解的优劣,这是遗传算法应用到多目标问题中的最大困难。根据遗传算法中每一代都有大量的可行解产生这一特点,我们考虑通过可行解之间相互比较淘汰劣解的办法来达到最 后对非劣解集的逼近。 考虑一个n维的多目标规划问题,且均为目标函数最大化, 其劣解可以定义为: f i (x * )≤f i (x t ) i=1,2,??,n (1) 且式(1)至少对一个i取“<”。即至少劣于一个可行解的x必为劣解。 对于遗传算法中产生大量的可行解,我们考虑对同一代中的个体基于目标函数相互比较,淘汰掉确定的劣解,并以生成的新解予以替换。经过数量足够大的种群一定次数的进化计算,可以得到一个接近非劣解集前沿面的解集,在一定精度要求下,可以近似的将其作为非劣解集。 个体的适应度计算方法确定后,为保证能得到非劣解集,算法设计中必须处理好以下问题:(1)保持种群的多样性及进化方向的控制。算法需要求出的是一组不同的非劣解,所以计算中要防止种群收敛到某一个解。与一般遗传算法进化到
遗传算法在多目标优化中的作用 调研报告
遗传算法在多目标优化中的作用调研报告 姓名: 学院: 班级: 学号: 完成时间:20 年月日 目录 1 .课题分析................................................................................................................................ 0 2 .检索策略................................................................................................................................ 0 2.1 检索工具的选择................................................................................................................................ ......... 0 2.2 检索词的选择................................................................................................................................ ............. 0 2.3 通用检索式................................................................................................................................ .. 0 3.检索步骤及检索结果 0 3.1 维普中文科技期刊数据库 0 3.2 中国国家知识产权局数据
多目标遗传算法代码
. % function nsga_2(pro) %% Main Function % Main program to run the NSGA-II MOEA. % Read the corresponding documentation to learn more about multiobjective % optimization using evolutionary algorithms. % initialize_variables has two arguments; First being the population size % and the second the problem number. '1' corresponds to MOP1 and '2' % corresponds to MOP2. %inp_para_definition=input_parameters_definition; %% Initialize the variables % Declare the variables and initialize their values % pop - population % gen - generations % pro - problem number %clear;clc;tic; pop = 100; % 每一代的种群数 gen = 100; % 总共的代数 pro = 2; % 问题选择1或者2,见switch switch pro case 1 % M is the number of objectives. M = 2; % V is the number of decision variables. In this case it is % difficult to visualize the decision variables space while the % objective space is just two dimensional. V = 6; case 2 M = 3; V = 12; case 3 % case 1和case 2 用来对整个算法进行常规验证,作为调试之用;case 3 为本工程所需; M = 2; %(output parameters 个数) V = 8; %(input parameters 个数) K = 10; end % Initialize the population chromosome = initialize_variables(pop,pro); %% Sort the initialized population % Sort the population using non-domination-sort. This returns two columns % for each individual which are the rank and the crowding distance
多目标规划遗传算法
%遗传算法解决多目标函数规划 clear clc syms x; %Function f1=f(x) f1=x(:,1).*x(:,1)/4+x(:,2).*x(:,2)/4; %function f2=f(x) f2=x(:,1).*(1-x(:,2))+10; NIND=100; MAXGEN=50; NV AR=2; PRECI=20; GGPA=0.9; trace1=[]; trace2=[]; trace3=[]; FielD=[rep([PRECI],[1,NV AR]);[1,1;4,2];rep([1;0;1;1],[NV AR])]; Chrom=crtbp(NIND,NV AR*PRECI); v=bs2rv(Chrom,FielD); gen=1; while gen 多目标遗传算法优化 铣削正交试验结果 说明: 1.建立切削力和表面粗糙度模型 如: 3.190.08360.8250.5640.45410c e p z F v f a a -=(1) a R =此模型你们来拟合(上面有实验数据,剩下的两个方程已经是我帮你们拟合好的了)(2) R a =10?0.92146v c 0.14365f z 0.16065a e 0.047691a p 0.38457 10002/c z p e Q v f a a D π=-????(3) 变量约束范围:401000.020.080.25 1.0210c z e p v f a a ≤≤??≤≤??≤≤? ?≤≤? 公式(1)和(2)值越小越好,公式(3)值越大越好。π=3.14 D=8 2.请将多目标优化操作过程录像(同时考虑三个方程,优化出最优的自变量数值),方便我后续进行修改;将能保存的所有图片及源文件发给我;将最优解多组发给我,类似于下图(黄色部分为达到的要求) 遗传算法的结果: 程序如下: clear; clc; % 遗传算法直接求解多目标优化 D=8; % Function handle to the fitness function F=@(X)[10^(3.19)*(X(1).^(-0.0836)).*(X(2).^0.825).*(X(3).^0.564).*(X(4).^0. 454)]; Ra=@(X)[10^(-0.92146)*(X(1).^0.14365).*(X(2).^0.16065).*(X(3).^0.047691).*( X(4).^0.38457)]; Q=@(X)[-1000*2*X(1).*X(2).*X(3).*X(4)/(pi*D)]; 函数最值问题:F=X2+Y2-Z2, clear clc %%初始化 pc=0.9; %交叉概率 pm=0.05; %变异概率 popsize=500; chromlength1=21; chromlength2=23; chromlength3=20; chromlength=chromlength1+chromlength2+chromlength3; pop=initpop(popsize,chromlength);% 产生初始种群 for i=1:500 [objvalue]=calobjvalue(pop); %计算目标函数值 [fitvalue]=calfitvalue(objvalue);%计算个体适应度 [newpop]=selection(pop,fitvalue);%选择 [newpop1]=crossover(newpop,pc) ; %交叉 [newpop2]=mutation(newpop1,pm) ;%变异 [newobjvalue]=newcalobjvalue(newpop2); %计算最新代目标函数值 [newfitvalue]=newcalfitvalue(newobjvalue); % 计算新种群适应度值[bestindividual,bestfit]=best(newpop2,newfitvalue); %求出群体中适应值最大的个体及其适应值 y(i)=max(bestfit); %储存最优个体适应值 pop5=bestindividual; %储存最优个体 n(i)=i; %记录最优代位置 %解码 x1(i)=0+decodechrom(pop5,1,21)*2/(pow2(21)-1); x2(i)=decodechrom(pop5,22,23)*6/(pow2(23)-1)-1; x3(i)=decodechrom(pop5,45,20)*1/(pow2(20)-1); pop=newpop2; end %%绘图 figure(1)%最优点变化趋势图 i=1:500; plot(y(i),'-b*') xlabel('迭代次数'); ylabel('最优个体适应值'); title('最优点变化趋势'); legend('最优点'); 一种在复杂网络中发现社区的多目标遗传算法 Clara Pizzuti 摘要——本文提出了一种揭示复杂网络社区结构的多目标遗传算法。该算法优化了两个目标函数,这些函数能够识别出组内节点密集连接,而组间连接稀疏。该方法能产生一系列不同等级的网络社区,其中解的等级越高,由更多的社区组成,被包含在社区较少的解中。社区的数量是通过目标函数更佳的折衷值自动确定的。对合成和真实网络的实验,结果表明算法成功地检测到了网络结构,并且能与最先进的方法相比较。 关键词:复杂网络,多目标聚类,多目标进化算法 1、简介 复杂网络构成了表示组成许多真实世界系统的对象之间关系的有效形式。协作网络、因特网、万维网、生物网络、通信传输网络,社交网络只是一些例子。将网络建模为图,节点代表个体,边代表这些个体之间的联系。 复杂网络研究中的一个重要问题是社区结构[25]的检测,也被称作为聚类[21],即将一个网络划分为节点组,称作社区或簇或模块,组内连接紧密,组间连接稀疏。这个问题,如[21]指出,只有在建模网络的图是稀疏的时候才有意义,即边的数量远低于可能的边数,否则就类似于数据簇[31]。图的聚类不同于数据聚类,因为图中的簇是基于边的密度,而在数据聚类中,它们是与距离或相似度量紧密相关的组点。然而,网络中社区的概念并未严格定义,因为它的定义受应用领域的影响。因此,直观的理解是同一社区内部边的数量应该远多于连接图中剩余节点的边的数量,这构成了社区定义的一般建议。这个直观定义追求两个不同的目标:最大化内部连接和最小化外部连接。 多目标优化是一种解决问题的技术,当多个相互冲突的目标被优化时,成功地找到一组解。通过利用帕累托最优理论[15]获得这些解,构成了尽可能满足所有目标的全局最优解。解决多目标优化问题的进化算法取得成功,是因为它们基于种群的特性,同时产生多个最优解和一个帕累托前沿[5]的优良近似。 因此,社区检测能够被表述为多目标优化问题,并且帕累托最优性的框架可以提供一组解对应于目标之间的最佳妥协以达到最优化。事实上,在上述两个目标之间有一个折衷,因为当整个网络社区结构的外部连接数量为空时,那它就是最小的,然而簇密度不够高。 在过去的几年里,已经提出了许多方法采用多目标技术进行数据聚类。这些方法大部分在度量空间[14], [17],[18], [28], [38], [39], [49], [51]聚集目标,虽然[8]中给出了分割图的一个方法,并且在[12]中描述了网络用户会议的一个图聚类算法。 本文中,一个多目标方法,名为用于网络的多目标遗传算法(MOGA-Net),通过利用提出的遗传算法发现网络中的社区。该方法优化了[32]和[44]中介绍的两个目标函数,它们已被证实在检测复杂网络中模块的有效性。第一个目标函数利用了community score的概念来衡量对一个网络进行社区划分的质量。community score值越高,聚类密度越高。第二个目标函数定义了模块中节点fitness的概念,并且反复迭代找到节点fitness总和最大的模块,以下将这个目标函数称为community fitness。当总和达到最大时,外部连接是最小。两个目标函数都有一个正实数参数控制社区的规模。参数值越大,找到的社区规模越小。MOGA-Net利用这两个函数的优点,通过有选择地探索搜寻空间获得网络中存在的社区,而不需要提前知道确切的社区数目。这个数目是通过两个目标之间的最佳折衷自动确定的。 多目标方法的一个有趣结果是它提供的不是一个单独的网络划分,而是一组解。这些解中的每一个都对应两个目标之间不同的折衷,并对应多种网络划分方式,即由许多不同簇组成。对合成网络和真实网络的实验表明,这一系列帕累托最优解揭示了网络的分层结构,其中簇的数目较多的解包含在社区数目较少的解中。多目标方法的这个特性提供了一个很好的机会分析不同层级 龙源期刊网 https://www.360docs.net/doc/572315424.html, 遗传算法在多目标线性规划的应用 作者:陈紫电 来源:《新课程·上旬》2013年第11期 摘要:求解多目标线性规划的基本思想大都是将多目标问题转化为单目标规划,目前主 要有线性加权和法、最大最小法、理想点法等。然而实际问题往往是复杂的,究竟哪种方法更加有效,也是因题而异。因此,通过讨论各种方法,提出了一个对各种算法的优劣进行量化对比的方法,并运用Matlab软件设计了相应的遗传算法来实现求解。 关键词:多目标线性规划;Matlab;遗传算法 多目标线性规划是最优化理论的重要组成部分,由于各目标之间的矛盾性和不可公度性,要使所有目标均达到最优,基本上是不可能的,因此,多目标规划问题往往只是求其相对较优的解。目前,求解多目标线性规划问题的有效方法有理想点法、线性加权和法、最大最小法、目标规划法,然而这些方法对多目标偏好信息的确定、处理等方面的研究工作不够深入,本文对多目标线性规划各解法的优劣进行了量化比较,最后还设计了相应的遗传算法,并借助MATLAB实现求解。 一、多目标线性规划模型 多目标线性规划有着两个和两个以上的目标函数,且目标函数和约束条件全是线性函数,其数学模型表示为: 二、多目标线性规划的求解方法 1.理想点法 三、遗传算法 对于上述多目标规划问题的各种解法,都从一定程度上有各自的偏好。为此,我们提出了一种多目标规划问题的遗传算法。 本文对各分量都做了数据标准化,并以(1,1,…,1)为理想目标,再以目标值的距离为目标(此距离可以作为其他算法的评价),消除了各分量之间的不公平性,最后借助MATLAB软件,从结果上看最后得到了更为合理的目标值。 参考文献: [1]李荣钧.多目标线性规划模糊算法与折衷算法分析[J].运筹与管理,2001,10(3):13-18. % function nsga_2(pro) %% Main Function % Main program to run the NSGA-II MOEA. % Read the corresponding documentation to learn more about multiobjective % optimization using evolutionary algorithms. % initialize_variables has two arguments; First being the population size % and the second the problem number. '1' corresponds to MOP1 and '2' % corresponds to MOP2. %inp_para_definition=input_parameters_definition; %% Initialize the variables % Declare the variables and initialize their values % pop - population % gen - generations % pro - problem number %clear;clc;tic; pop = 100; % 每一代的种群数 gen = 100; % 总共的代数 pro = 2; % 问题选择1或者2,见switch switch pro case 1 % M is the number of objectives. M = 2; % V is the number of decision variables. In this case it is % difficult to visualize the decision variables space while the % objective space is just two dimensional. V = 6; case 2 M = 3; V = 12; case 3 % case 1和case 2 用来对整个算法进行常规验证,作为调试之用;case 3 为本工程所需; M = 2; %(output parameters 个数) V = 8; %(input parameters 个数) K = 10; end % Initialize the population chromosome = initialize_variables(pop,pro); %% Sort the initialized population % Sort the population using non-domination-sort. This returns two columns % for each individual which are the rank and the crowding distance % corresponding to their position in the front they belong. 真是牛X了。 chromosome = non_domination_sort_mod(chromosome,pro); %% Start the evolution process NSGA-II 算法实例 目前的多目标优化算法有很多, Kalyanmoy Deb 的带精英策略的快速非支配排序遗传算法(NSGA-II) 无疑是其中应用最为广泛也是最为成功的一种。本文用的算法是MATLAB 自带的函数gamultiobj ,该函数是基于NSGA-II 改进的一种多目标优化算法。 一、 数值例子 多目标优化问题 424221********* 4224212212112 12min (,)10min (,)55..55 f x x x x x x x x x f x x x x x x x x x s t x =-++-=-++-≤≤??-≤≤? 二、 Matlab 文件 1. 适应值函数m 文件: function y=f(x) y(1)=x(1)^4-10*x(1)^2+x(1)*x(2)+x(2)^4-x(1)^2*x(2)^2; y(2)=x(2)^4-x(1)^2*x(2)^2+x(1)^4+x(1)*x(2); 2. 调用gamultiobj 函数,及参数设置: clear clc fitnessfcn=@f; %适应度函数句柄 nvars=2; %变量个数 lb=[-5,-5]; %下限 ub=[5,5]; %上限 A=[];b=[]; %线性不等式约束 Aeq=[];beq=[]; %线性等式约束 options=gaoptimset('paretoFraction',0.3,'populationsize',100,'generations', 200,'stallGenLimit',200,'TolFun',1e-100,'PlotFcns',@gaplotpareto); % 最优个体系数paretoFraction 为0.3;种群大小populationsize 为100,最大进化代数generations 为200, % 停止代数stallGenLimit 为200, 适应度函数偏差TolFun 设为1e-100,函数gaplotpareto :绘制Pareto 前端 [x,fval]=gamultiobj(fitnessfcn,nvars,A,b,Aeq,beq,lb,ub,options) 这是我在解决电梯动力学参数写的简单遗传算法(程序带目标函数值、适应度值计算,但是我的适应度函数因为目标函数的计算很特殊,一起放在了程序外面计算,在此不提供)。 头文件: // CMVSOGA.h : main header file for the CMVSOGA.cpp // 本来想使用链表里面套链表的,程序调试比较麻烦,改为种群用链表表示 //染色体固定为16的方法。 #if !defined(AFX_CMVSOGA_H__45BECA_61EB_4A0E_9746_9A94D1CCF767_ _INCLUDED_) #define AFX_CMVSOGA_H__45BECA_61EB_4A0E_9746_9A94D1CCF767__INCLUDED _ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "Afxtempl.h" #define variablenum 16 class CMVSOGA { public: CMVSOGA(); void selectionoperator(); void crossoveroperator(); void mutationoperator(); void initialpopulation(int, int ,double ,double,double *,double *); //种群初始化 void generatenextpopulation(); //生成下一代种群 void evaluatepopulation(); //评价个体,求最佳个体 void calculateobjectvalue(); //计算目标函数值 void calculatefitnessvalue(); //计算适应度函数值 void findbestandworstindividual(); //寻找最佳个体和最差个体 void performevolution(); void GetResult(double *); void GetPopData(double **); void SetValueData(double *); void maxandexpectation(); private: struct individual { double chromosome[variablenum]; //染色体编码长度应该为变量的个数 double value; double fitness; //适应度 }; 硕士生考查课程考试试卷 考试科目:MATLAB教程 考生姓名:张宜龙考生学号:2130120033 学院:管理学院专业:管理科学与工程考生成绩: 任课老师(签名) 考试日期:年月日午时至时 《MATLAB 教程》试题: A 、利用MATLA B 设计遗传算法程序,寻找下图11个端点的最短路径,其中没有连接的端点表示没有路径。要求设计遗传算法对该问题求解。 a e h k B 、设计遗传算法求解f (x)极小值,具体表达式如下: 3 21231(,,)5.12 5.12,1,2,3 i i i f x x x x x i =?=???-≤≤=? ∑ 要求必须使用m 函数方式设计程序。 C 、利用MATLAB 编程实现:三名商人各带一个随从乘船渡河,一只小船只能容纳二人,由他们自己划行,随从们密约,在河的任一岸,一旦随从的人数比商人多,就杀人越货,但是如何乘船渡河的大权掌握在商人手中,商人们怎样才能安全渡河? D 、结合自己的研究方向选择合适的问题,利用MATLAB 进行实验。 以上四题任选一题进行实验,并写出实验报告。 选择题目: B 、设计遗传算法求解f (x)极小值,具体表达式如下: 3 21231(,,)5.12 5.12,1,2,3 i i i f x x x x x i =?=???-≤≤=? ∑ 要求必须使用m 函数方式设计程序。 一、问题分析(10分) 这是一个简单的三元函数求最小值的函数优化问题,可以利用遗传算法来指导性搜索最小值。实验要求必须以matlab 为工具,利用遗传算法对问题进行求解。 在本实验中,要求我们用M 函数自行设计遗传算法,通过遗传算法基本原理,选择、交叉、变异等操作进行指导性邻域搜索,得到最优解。 二、实验原理与数学模型(20分) (1)试验原理: 用遗传算法求解函数优化问题,遗传算法是模拟生物在自然环境下的遗传和进化过程而形成的一种自适应全局优化概率搜索方法。其采纳了自然进化模型,从代表问题可能潜在解集的一个种群开始,种群由经过基因编码的一定数目的个体组成。每个个体实际上是染色体带有特征的实体;初始种群产生后,按照适者生存和优胜劣汰的原理,逐代演化产生出越来越好的解:在每一代,概据问题域中个体的适应度大小挑选个体;并借助遗传算子进行组合交叉和主客观变异,产生出代表新的解集的种群。这一过程循环执行,直到满足优化准则为止。最后,末代个体经解码,生成近似最优解。基于种群进化机制的遗传算法如同自然界进化一样,后生代种群比前生代更加适应于环境,通过逐代进化,逼近最优解。 遗传算法是一种现代智能算法,实际上它的功能十分强大,能够用于求解一些难以用常规数学手段进行求解的问题,尤其适用于求解多目标、多约束,且目标函数形式非常复杂的优化问题。但是遗传算法也有一些缺点,最为关键的一点,即没有任何理论能够证明遗传算法一定能够找到最优解,算法主要是根据概率论的思想来寻找最优解。因此,遗传算法所得到的解只是一个近似解,而不一定是最优解。 (2)数学模型 对于求解该问题遗传算法的构造过程: (1)确定决策变量和约束条件; 非线性整数规划的遗传算法Matlab程序(附图) 通常,非线性整数规划是一个具有指数复杂度的NP问题,如果约束较为复杂,Matlab优化工具箱和一些优化软件比如lingo等,常常无法应用,即使能应用也不能给出一个较为令人满意的解。这时就需要针对问题设计专门的优化算法。下面举一个遗传算法应用于非线性整数规划的编程实例,供大家参考! 模型的形式和适应度函数定义如下: 这是一个具有200个01决策变量的多目标非线性整数规划,编写优化的目标函数如下,其中将多目标转化为单目标采用简单的加权处理。 function Fitness=FITNESS(x,FARM,e,q,w) %% 适应度函数 % 输入参数列表 % x 决策变量构成的4×50的0-1矩阵 % FARM 细胞结构存储的当前种群,它包含了个体x % e 4×50的系数矩阵 % q 4×50的系数矩阵 % w 1×50的系数矩阵 %% gamma=0.98; N=length(FARM);%种群规模 F1=zeros(1,N); F2=zeros(1,N); for i=1:N xx=FARM{i}; ppp=(1-xx)+(1-q).*xx; F1(i)=sum(w.*prod(ppp)); F2(i)=sum(sum(e.*xx)); end ppp=(1-x)+(1-q).*x; f1=sum(w.*prod(ppp)); f2=sum(sum(e.*x)); Fitness=gamma*sum(min([sign(f1-F1);zeros(1,N)]))+(1-gamma )*sum(min([sign(f2-F2);zeros(1,N)])); 针对问题设计的遗传算法如下,其中对模型约束的处理是重点考虑的地方function [Xp,LC1,LC2,LC3,LC4]=MYGA(M,N,Pm) %% 求解01整数规划的遗传算法 %% 输入参数列表 % M 遗传进化迭代次数 % N 种群规模 % Pm 变异概率 %% 输出参数列表 % Xp 最优个体 % LC1 子目标1的收敛曲线 % LC2 子目标2的收敛曲线 % LC3 平均适应度函数的收敛曲线 % LC4 最优适应度函数的收敛曲线 %% 参考调用格式[Xp,LC1,LC2,LC3,LC4]=MYGA(50,40,0.3) 非线性整数规划的遗传算法Matlab程序 通常,非线性整数规划是一个具有指数复杂度的NP问题,如果约束较为复杂,Matlab优化工具箱和一些优化软件比如lingo等,常常无法应用,即使能应用也不能给出一个较为令人满意的解。这时就需要针对问题设计专门的优化算法。下面举一个遗传算法应用于非线性整数规划的编程实例,供大家参考! 模型的形式和适应度函数定义如下: 这是一个具有200个01决策变量的多目标非线性整数规划,编写优化的目标函数如下,其中将多目标转化为单目标采用简单的加权处理。 function Fitness=FITNESS(x,FARM,e,q,w) %% 适应度函数 % 输入参数列表 % x 决策变量构成的4×50的0-1矩阵 % FARM 细胞结构存储的当前种群,它包含了个体x % e 4×50的系数矩阵 % q 4×50的系数矩阵 % w 1×50的系数矩阵 %% gamma=0.98; N=length(FARM);%种群规模 F1=zeros(1,N); F2=zeros(1,N); for i=1:N xx=FARM{i}; ppp=(1-xx)+(1-q).*xx; F1(i)=sum(w.*prod(ppp)); F2(i)=sum(sum(e.*xx)); end ppp=(1-x)+(1-q).*x; f1=sum(w.*prod(ppp)); f2=sum(sum(e.*x)); Fitness=gamma*sum(min([sign(f1-F1);zeros(1,N)]))+(1-gamma)*sum(min([sign(f2-F2);zeros(1,N)])); 针对问题设计的遗传算法如下,其中对模型约束的处理是重点考虑的地方 function [Xp,LC1,LC2,LC3,LC4]=MYGA(M,N,Pm) %% 求解01整数规划的遗传算法 %% 输入参数列表 % M 遗传进化迭代次数 % N 种群规模 % Pm 变异概率 %% 输出参数列表 % Xp 最优个体 % LC1 子目标1的收敛曲线 % LC2 子目标2的收敛曲线 % LC3 平均适应度函数的收敛曲线 % LC4 最优适应度函数的收敛曲线 %% 参考调用格式[Xp,LC1,LC2,LC3,LC4]=MYGA(50,40,0.3) %% 第一步:载入数据和变量初始化 load eqw;%载入三个系数矩阵e,q,w %输出变量初始化 Xp=zeros(4,50); LC1=zeros(1,M); LC2=zeros(1,M); LC3=zeros(1,M); LC4=zeros(1,M); Best=inf; %% 第二步:随机产生初始种群遗传算法多目标函数优化
遗传算法程序代码--多目标优化--函数最值问题
多目标遗传算法中文【精品毕业设计】(完整版)
遗传算法在多目标线性规划的应用
多目标遗传算法代码
多目标优化实例和matlab程序
多变量多目标的遗传算法程序
MATLAB课程遗传算法实验报告及源代码
非线性整数规划的遗传算法Matlab程序
非线性整数规划的遗传算法Matlab程序