700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 整数规划的matlab计算 Matlab遗传算法解决整数规划问题

整数规划的matlab计算 Matlab遗传算法解决整数规划问题

时间:2022-06-16 11:08:32

相关推荐

整数规划的matlab计算 Matlab遗传算法解决整数规划问题

如下,要判断时候为正数,这个搞了好久

我本来是用lingo做的,lingo也是老报错

---------------------------------------------------

我奋斗了好久,只能说是书到用时方恨少.搜索了下,我用了lingo以及5月1号写的那些长长的程序后,没有什么效果,我用lingo老是出错,太久没用了,我也查不出哪里错了.lingo的程序在下楼.MATLAB之前写的程序也在下楼.

这次我是用遗传算法解决,可是每次运行的结果都不一样,而且结果也不是我要的.我乱套用这些,可是没办法.这个问题老是解决不了(问题function.jpg如图).下面是我的程序,不知道我的什么时候能做完,嗨,我的5.1节啊.天亮后去下图书馆,找下lingo该怎么解决.觉得MATLAB来解决这个问题太难啦~~~

%------------------------gainteger_GA.m--------------------------------------

function [x,fval] = gainteger_GA

%This is a function to solve the problem of interger programming using GA

%function in the Genetic Algorithm and Direct Search Toolbox

% Fitness function and numver of variables

fitnessFcn = @(x) gaf(x);

numberOfVariables = 12;

% If decision variables are bounded provide a bound e.g, LB and UB.

LB = -100*ones(1,numberOfVariables);

UB = 100*ones(1,numberOfVariables);

Bound = [LB;UB]; % 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});

[x,fval] = 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

%----------------------------------------------------------------------------------

另外一个函数:

%----------------------------gaf.m--------------------------------------------

function f=gaf(x)

v=1/2*[1.1 1.1 1.2 1.2 1.3 1.3];

xs=[42 32 41 67 25 29];

if((x(7)==49)&&(x(6)+x(12)-xs(6)==0)&&(x(8)-x(7)+xs(1)-x(1)==0) ...

&&(x(9)-x(8)+xs(2)-x(2)==0)&&(x(10)-x(9)+xs(3)-x(3)==0) ...

&&(x(11)-x(10)+xs(4)-x(4)==0)&&(x(12)-x(11)+xs(5)-x(5)==0) ...

&&(x(1)>=0)&&(x(2)>=0)&&(x(3)>=0)&&(x(4)>=0)&&(x(5)>=0)&&(x(6)>=0))

f=-(48*x(1)+48*x(2)+48*x(3)+48*x(4)+48*x(5)+48*x(6)-v(1)*x(1)^2 ...

-v(2)*x(2)^2-v(3)*x(3)^2-v(4)*x(4)^2-v(5)*x(5)^2-v(6)*x(6)^2 ...

-0.1*(x(7)>0)*x(7)-0.1*(x(8)>0)*x(8)-0.1*(x(9)>0)*x(9)-0.1*(x(10)>0)*x(10) ...

-0.1*(x(11)>0)*x(11)-0.1*(x(12)>0)*x(12));

else,f=10000;

end

[本帖最后由 fouvy 于 -5-2 03:13 编辑]

fd.jpg

(14.36 KB, 下载次数: 397)

-5-1 16:06 上传

function.jpg

(13.71 KB, 下载次数: 298)

-5-2 03:11 上传

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。