MATLAB遗传算法函数ga()用法备忘

X = ga(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub,NONLCON,options) minimizes

   with the default optimization parameters replaced by values in OPTIONS.

   OPTIONS can be created with the OPTIMOPTIONS function. See OPTIMOPTIONS

   for details. For a list of options accepted by ga refer to the

   documentation.

X = ga(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a structure

   that has the following fields:

       fitnessfcn: <Fitness function>

            nvars: <Number of design variables>

            Aineq: <A matrix for inequality constraints>

            bineq: <b vector for inequality constraints>

              Aeq: <Aeq matrix for equality constraints>

              beq: <beq vector for equality constraints>

               lb: <Lower bound on X>

               ub: <Upper bound on X>

          nonlcon: <Nonlinear constraint function>

           intcon: <Index vector for integer variables>

          options: <Options created with optimoptions('ga',...)>

         rngstate: <State of the random number generator>

在做Unit电阻分配的问题时,进行使用。

结构稍微有些变动,option中的设置都是算法终止的条件。

options = gaoptimset('PopulationSize',20,'Generations',350 ...
    ,'StallGenLimit',Inf,'TolFun',1e-10);

%算法计算
[ord,fval,exitflag,output,population,scores]=ga(fun,len,[],[],[],[],ones(len,1),GroupNum*ones(len,1),[],[1:len],options)

ones(len,1),GroupNum*ones(len,1)——变量范围是由1到分组的数量

[1:len]——数据取整数,不能取小数


再举个例子

二次规划:已知

 function y = lincontest6(x)

p1=0.5;p2=6.0;y = p1*x(1)^2 + x(2)^2 -x(1)*x(2) -2*x(1) - p2*x(2);

主函数

A = [1 1; -1 2; 2 1];  b = [2; 2; 3];  lb = zeros(2,1);

    % Use mutation function which can handle constraints

    options = optimoptions('ga','MutationFcn',@mutationadaptfeasible);

    [x,fval,exitflag] = ga(@lincontest6,2,A,b,[],[],lb,[],[],options)


%仅为参考的用法
%fitnessfcn = @GADS;           % 适应度函数句柄
%nvars = 2;         % 个体的变量数目
%参数设置
%options = gaoptimset('PopulationSize',100,'EliteCount',10,'CrossoverFraction',0.75,'Generations',500,'StallGenLimit',500,'TolFun',1e-100,'PlotFcns',{@gaplotbestf,@gaplotbestindiv}); 
%[x_best,fval] =ga(fitnessfcn,nvars,[],[],[],[],[],[],[],options);   % 调用ga函数
 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值