zhlong 发表于 2007-11-20 22:45

(转载)遗传算法整数规划问题

论坛有两个关于这方面的帖子,答案还不完整。今日搜得一文,还未及试运行,先与大家分享。
遗传算法如何进行整数规划问题?
如何在遗传算法中限定变量只取整数值?



Subject:

How can I perform integer programming using the GA function in the Genetic Algorithm and Direct Search Toolbox?

Problem Description:

I would like to perform integer programming or mixed integer programming using the GA function by constraining certain parameters to be integers.

Solution:

To perform integer programming using the GA function, create a custom mutation function and a creation function that generate only integer outputs.
Then, use the GAOPTIMSET function to set the "MutationFcn" and "CreationFcn" properties to the custom functions, as shown with the following code:
options = gaoptimset('CreationFcn',@your_creationfcn,'MutationFcn',@your_mutationfcn);
= ga(fitnessFcn,nVars,options);
See the attached example file "gainteger_demo.m".
Related Documents/Files:

[*]

源程序如下:
function = gainteger_demo
% Fitness function and numver of variables
fitnessFcn = @(x) norm(x);
numberOfVariables = 15;
% If decision variables are bounded provide a bound e.g, LB and UB.
LB = -5*ones(1,numberOfVariables);
UB = 5*ones(1,numberOfVariables);
Bound = ; % If unbounded then Bound = []
% Create an options structure to be passed to GA
% Three options namely 'CreationFcn', 'MutationFcn', and
% 'PopInitRange' are required part of the problem.
options = gaoptimset('CreationFcn',@int_pop,'MutationFcn',@int_mutation, ...
         'PopInitRange',Bound,'Display','iter','StallGenL',40,'Generations',150, ...
         'PopulationSize',60,'PlotFcns',{@gaplotbestf,@gaplotbestindiv});
= ga(fitnessFcn,numberOfVariables,options);
%---------------------------------------------------
% Mutation function to generate childrens satisfying the range and integer
% constraints on decision variables.
function mutationChildren = int_mutation(parents,options,GenomeLength, ...
         FitnessFcn,state,thisScore,thisPopulation)
shrink = .01;
scale = 1;
scale = scale - shrink * scale * state.Generation/options.Generations;
range = options.PopInitRange;
lower = range(1,:);
upper = range(2,:);
scale = scale * (upper - lower);
mutationPop =       length(parents);
% The use of ROUND function will make sure that childrens are integers.
mutationChildren =       repmat(lower,mutationPop,1) +       ...
         round(repmat(scale,mutationPop,1) .* rand(mutationPop,GenomeLength));
% End of mutation function
%---------------------------------------------------
function Population = int_pop(GenomeLength,FitnessFcn,options)
totalpopulation = sum(options.PopulationSize);
range = options.PopInitRange;
lower= range(1,:);
span = range(2,:) - lower;
% The use of ROUND function will make sure that individuals are integers.
Population = repmat(lower,totalpopulation,1) +       ...
         round(repmat(span,totalpopulation,1) .* rand(totalpopulation,GenomeLength));
% End of creation function

转自:http://www.mathworks.com/support/solutions/data/1-10PDHC.html?product=GD&solution=1-10PDHC
         http://hi.baidu.com/webooo99/blog/item/ba57ca4b17442cf382025c0b.html

[ 本帖最后由 zhlong 于 2007-11-20 22:47 编辑 ]

zwf100 发表于 2008-3-5 10:32

很好,谢谢楼主

lichunyang 发表于 2008-3-28 00:10

一直在找变量是整数时如何在matlab工具箱中进行分析,本以为是很简单的一个设置而已,原来这么复杂啊:@L

lichunyang 发表于 2008-3-28 09:35

利用上面的方法,设置种群类型时应该采用bitstring还是custom?
页: [1]
查看完整版本: (转载)遗传算法整数规划问题