24小时热门版块排行榜    

查看: 4377  |  回复: 8

shu_fyh

新虫 (初入文坛)

[求助] matlab下用遗传算法求最小值的问题

刚刚接触matlab,有点问题求解答
现有这样一个方程:VRRm=0.2355-0.637*10-4x1+1.85875*10-4x2+0.82225*10-2x3-0.1715x4-1.6725*10-6x1*x2+0.966925*10-4x1*x3+0.403*10-4x2*x3-0.471*10-2x3*x4
x1、x2、x3、x4都有上下限,想借助matlab用遗传算法求VRR的最小值,自己编写一个程序麻烦吗,或者使用matlab遗传算法工具箱能实现吗?
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

shu_fyh

新虫 (初入文坛)

求人来解答啊。。
2楼2013-11-21 13:01:58
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

feifeiyin12

铁虫 (初入文坛)

【答案】应助回帖

感谢参与,应助指数 +1
可以实现,代码很简单,这个应该可以用非线性规划,随便找本matlab方面的书,半个小时可以搞定。手机不好发代码。你编程有问题可以去matlab论坛上问,那里这方面回答的人多些

[ 发自手机版 http://muchong.com/3g ]
勤学笃行,精雕细琢
3楼2013-11-21 13:28:04
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

shu_fyh

新虫 (初入文坛)

引用回帖:
3楼: Originally posted by feifeiyin12 at 2013-11-21 13:28:04
可以实现,代码很简单,这个应该可以用非线性规划,随便找本matlab方面的书,半个小时可以搞定。手机不好发代码。你编程有问题可以去matlab论坛上问,那里这方面回答的人多些
...

额,我说的是能不能用遗传算法搞定。。
4楼2013-11-21 14:08:03
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

月只蓝

主管区长 (职业作家)

【答案】应助回帖

感谢参与,应助指数 +1
引用回帖:
4楼: Originally posted by shu_fyh at 2013-11-21 14:08:03
额,我说的是能不能用遗传算法搞定。。...

可以用遗传算法来求解。
MATLAB、MS小问题、普通问题请发帖求助!时间精力有限,恕不接受无偿私信求助。
5楼2013-11-21 16:06:11
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

shu_fyh

新虫 (初入文坛)

引用回帖:
5楼: Originally posted by 月只蓝 at 2013-11-21 16:06:11
可以用遗传算法来求解。...

遗产算法的过程我已经写出来了,就是不知道怎么用matlab来实现
6楼2013-11-21 16:53:14
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

月只蓝

主管区长 (职业作家)

【答案】应助回帖

引用回帖:
6楼: Originally posted by shu_fyh at 2013-11-21 16:53:14
遗产算法的过程我已经写出来了,就是不知道怎么用matlab来实现...

MATLAB有自带的遗传算法函数ga,虽然说MATLAB的内置的遗传算法并不是最优秀的,但求解你的问题应该没问题。
ga函数调用格式很方便,见MATLAB help:
GA    Constrained optimization using genetic algorithm.
    GA attempts to solve problems of the form:
        min F(X)  subject to:  A*X  <= B, Aeq*X  = Beq (linear constraints)
         X                     C(X) <= 0, Ceq(X) = 0 (nonlinear constraints)
                               LB <= X <= ub

    X = GA(FITNESSFCN,NVARS) finds a local unconstrained minimum X to the
    FITNESSFCN using GA. NVARS is the dimension (number of design
    variables) of the FITNESSFCN. FITNESSFCN accepts a vector X of size
    1-by-NVARS, and returns a scalar evaluated at X.

    X = GA(FITNESSFCN,NVARS,A,b) finds a local minimum X to the function
    FITNESSFCN, subject to the linear inequalities A*X <= B. Linear
    constraints are not satisfied when the PopulationType option is set to
    'bitString' or 'custom'. See the documentation for details.

    X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq) finds a local minimum X to the
    function FITNESSFCN, subject to the linear equalities Aeq*X = beq as
    well as A*X <= B. (Set A=[] and B=[] if no inequalities exist.) Linear
    constraints are not satisfied when the PopulationType option is set to
    'bitString' or 'custom'. See the documentation for details.

    X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub) defines a set of lower and
    upper bounds on the design variables, X, so that a solution is found in
    the range lb <= X <= ub. Use empty matrices for lb and ub if no bounds
    exist. Set lb(i) = -Inf if X(i) is unbounded below;  set ub(i) = Inf if
    X(i) is unbounded above. Linear constraints are not satisfied when the
    PopulationType option is set to 'bitString' or 'custom'. See the
    documentation for details.

    X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub,NONLCON) subjects the
    minimization to the constraints defined in NONLCON. The function
    NONLCON accepts X and returns the vectors C and Ceq, representing the
    nonlinear inequalities and equalities respectively. GA minimizes
    FITNESSFCN such that C(X)<=0 and Ceq(X)=0. (Set lb=[] and/or ub=[] if
    no bounds exist.) Nonlinear constraints are not satisfied when the
    PopulationType option is set to 'bitString' or 'custom'. See the
    documentation for details.

    X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub,NONLCON,options) minimizes
    with the default optimization parameters replaced by values in the
    structure OPTIONS. OPTIONS can be created with the GAOPTIMSET function.
    See GAOPTIMSET for details.

    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>
           options: <Options structure created with GAOPTIMSET>
          rngstate: <State of the random number generator>

    [X,FVAL] = GA(FITNESSFCN, ...) returns FVAL, the value of the fitness
    function FITNESSFCN at the solution X.

    [X,FVAL,EXITFLAG] = GA(FITNESSFCN, ...) returns EXITFLAG which
    describes the exit condition of GA. Possible values of EXITFLAG and the
    corresponding exit conditions are

      1 Average change in value of the fitness function over
         options.StallGenLimit generations less than options.TolFun and
         constraint violation less than options.TolCon.
      3 The value of the fitness function did not change in
         options.StallGenLimit generations and constraint violation less
         than options.TolCon.
      4 Magnitude of step smaller than machine precision and constraint
         violation less than options.TolCon. This exit condition applies
         only to nonlinear constraints.
      5 Fitness limit reached and constraint violation less than
         options.TolCon.
      0 Maximum number of generations exceeded.
     -1 Optimization terminated by the output or plot function.
     -2 No feasible point found.
     -4 Stall time limit exceeded.
     -5 Time limit exceeded.

    [X,FVAL,EXITFLAG,OUTPUT] = GA(FITNESSFCN, ...) returns a
    structure OUTPUT with the following information:
              rngstate: <State of the random number generator before GA started>
           generations: <Total generations, excluding HybridFcn iterations>
             funccount: <Total function evaluations>
         maxconstraint: <Maximum constraint violation>, if any
               message: <GA termination message>

    [X,FVAL,EXITFLAG,OUTPUT,POPULATION] = GA(FITNESSFCN, ...) returns the
    final POPULATION at termination.

    [X,FVAL,EXITFLAG,OUTPUT,POPULATION,SCORES] = GA(FITNESSFCN, ...) returns
    the SCORES of the final POPULATION.


    Example:
      Unconstrained minimization of 'rastriginsfcn' fitness function of
      numberOfVariables = 2
       x = ga(@rastriginsfcn,2)

      Display plotting functions while GA minimizes
       options = gaoptimset('PlotFcns',...
         {@gaplotbestf,@gaplotbestindiv,@gaplotexpectation,@gaplotstopping});
       [x,fval,exitflag,output] = ga(@rastriginsfcn,2,[],[],[],[],[],[],[],options)

    An example with inequality constraints and lower bounds
     A = [1 1; -1 2; 2 1];  b = [2; 2; 3];  lb = zeros(2,1);
     % Use mutation function which can handle constraints
     options = gaoptimset('MutationFcn',@mutationadaptfeasible);
     [x,fval,exitflag] = ga(@lincontest6,2,A,b,[],[],lb,[],[],options);

      FITNESSFCN can also be an anonymous function:
         x = ga(@(x) 3*sin(x(1))+exp(x(2)),2)

    If FITNESSFCN or NONLCON are parameterized, you can use anonymous
    functions to capture the problem-dependent parameters. Suppose you want
    to minimize the fitness given in the function myfit, subject to the
    nonlinear constraint myconstr, where these two functions are
    parameterized by their second argument a1 and a2, respectively. Here
    myfit and myconstr are M-file functions such as

         function f = myfit(x,a1)
         f = exp(x(1))*(4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + a1);

    and

         function [c,ceq] = myconstr(x,a2)
         c = [1.5 + x(1)*x(2) - x(1) - x(2);
               -x(1)*x(2) - a2];
         % No nonlinear equality constraints:
          ceq = [];

    To optimize for specific values of a1 and a2, first assign the values
    to these two parameters. Then create two one-argument anonymous
    functions that capture the values of a1 and a2, and call myfit and
    myconstr with two arguments. Finally, pass these anonymous functions to
    GA:

      a1 = 1; a2 = 10; % define parameters first
      % Mutation function for constrained minimization
      options = gaoptimset('MutationFcn',@mutationadaptfeasible);
      x = ga(@(x)myfit(x,a1),2,[],[],[],[],[],[],@(x)myconstr(x,a2),options)
MATLAB、MS小问题、普通问题请发帖求助!时间精力有限,恕不接受无偿私信求助。
7楼2013-11-21 17:05:00
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

木头919

银虫 (小有名气)

【答案】应助回帖

感谢参与,应助指数 +1
在命令窗口输入gatool,调出工具箱,一目了然
8楼2013-11-22 12:17:46
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

LS1234567

新虫 (初入文坛)

引用回帖:
7楼: Originally posted by 月只蓝 at 2013-11-21 17:05:00
MATLAB有自带的遗传算法函数ga,虽然说MATLAB的内置的遗传算法并不是最优秀的,但求解你的问题应该没问题。
ga函数调用格式很方便,见MATLAB help:
GA    Constrained optimization using genetic algorithm.
...

??? Undefined function or method 'myconstr' for input arguments of type 'double'.

Error in ==> @(x)myconstr(x,a2)


Error in ==> validate>@(x)nonlcon(x,NonconFcnArgs{:}) at 142
    nonlcon = @(x) nonlcon(x,NonconFcnArgs{:});

Error in ==> constrValidate at 17
        [cineq,ceq] = nonlcon(Iterate.x');

Error in ==> gacommon at 130
[LinearConstr, Iterate,nineqcstr,neqcstr,ncstr] = constrValidate(NonconFcn, ...

Error in ==> ga at 267
[x,fval,exitFlag,output,population,scores,FitnessFcn,nvars,Aineq,bineq,Aeq,beq,lb,ub, ...

Error in ==> Untitled at 6
      x = ga(@(x)myfit(x,a1),2,[],[],[],[],[],[],@(x)myconstr(x,a2),options)
Caused by:
    Failure in initial user-supplied nonlinear constraint function evaluation.



运行不了啊
没有困难 只有挑战
9楼2015-05-14 11:08:58
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 shu_fyh 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[基金申请] 今天基金会出结果吗?20260819 +14 kkkl_v 2026-08-19 15/750 2026-08-20 08:21 by sunzitan
[基金申请] 只有每年这种时候来逛逛小木虫 +4 yaoyewhu2008 2026-08-20 4/200 2026-08-20 08:01 by 201120518022
[基金申请] 欢迎发来filecode的Mz6后的代码验证其规律 +55 医学老男孩 2026-08-13 123/6150 2026-08-19 23:44 by stan_et
[基金申请] 时间戳变了,能看出什么问题? +12 基诺咪客 2026-08-17 16/800 2026-08-19 22:04 by kk 的小木虫
[基金申请] filecode,4个jtjc了 +11 ziyangfang 2026-08-19 13/650 2026-08-19 21:28 by aasahr
[基金申请] 今天放榜吗? +14 布布和一二 2026-08-19 15/750 2026-08-19 18:07 by gltch
[基金申请] filecode=后面第一个是大写字母 +10 wangze12014 2026-08-14 12/600 2026-08-19 16:56 by 苦难博士
[基金申请] 2027广东省杰青 +4 奶牛小黑 2026-08-15 10/500 2026-08-19 11:02 by wanfengnew
[基金申请] 朋友圈看到的 +6 wangzilk 2026-08-18 8/400 2026-08-19 10:55 by Haru815
[基金申请] 快农历七夕节了,轻松一下,男人悄悄话,女施主请不要进来。 +6 Tide man 2026-08-14 7/350 2026-08-19 09:56 by ZJTJZ
[基金申请] 重要消息,中午系统在维护 +10 yuleib84 2026-08-18 11/550 2026-08-19 07:30 by 羽毛枫f
[基金申请] 明天放榜? +5 Shxjjxjkx 2026-08-18 5/250 2026-08-18 18:14 by -大大大大大-
[论文投稿] 投稿咨询 +4 wwm09 2026-08-17 6/300 2026-08-18 15:36 by wwm09
[基金申请] 时间戳又变了8-15 +14 archvillain 2026-08-15 26/1300 2026-08-18 13:37 by phantomgost
[基金申请] 今天维护系统维护 祝所有人 高中 +8 gjjjzhong 2026-08-18 9/450 2026-08-18 13:01 by 家与远方
[基金申请] 哪位老哥知道今年的国自然具体哪一天放榜? +13 Ldrop2023 2026-08-13 16/800 2026-08-18 12:25 by 淀粉搬运工
[基金申请] 93BebMhtakh前后11位开头都是大写 +4 且听虎啸 2026-08-17 5/250 2026-08-18 00:49 by 蔡棒棒菂
[基金申请] 感觉是下周放榜了 +6 angus9576 2026-08-17 11/550 2026-08-17 23:57 by angus9576
[基金申请] 今天系统多次维护,明天很可能放榜! +8 zju2000 2026-08-16 9/450 2026-08-17 12:20 by lmz0216
[基金申请] 各位道友,我要去昆明玩几天,回来见。 +7 Tide man 2026-08-14 8/400 2026-08-15 01:11 by arzu_hma
信息提示
请填处理意见