【问题标题】:Find the minimum of "y=x*x" using genetic algorithm in Matlab [closed]在Matlab中使用遗传算法找到“y = x * x”的最小值[关闭]
【发布时间】:2012-02-15 21:52:06
【问题描述】:

有人能帮我解决这个问题吗?我是 Matlab 的新手......而且我有点难以理解如何在 Matlab 中创建和使用遗传算法。 如果有人可以帮助编写一些非常简单的代码来搜索指定函数的最小值/最大值。 我读到应该使用 gatool ......但我无法理解 Matlab 帮助网络的示例。我正在做接下来的步骤:

  1. 在文本编辑器中,我正在输入下一个:

    function y= parabola(x)
        y=x*x;
    end
    
  2. 然后我启动 GATOOL 并指定此函​​数,如 @parabola

  3. 设置变量数(等于 2)
  4. Initial range = [-10;10]
  5. 其他参数设置为默认值

    当我按下Start 按钮时,我看到了一个结果:

    fitnessfcn 中的错误:输入参数“x”未定义。

【问题讨论】:

  • 这不是“为我编写程序”服务。
  • 我只是在寻求帮助...如您所见,我已经阅读了此任务的可能解决方案。他们没有工作......所以我决定在这里问

标签: matlab genetic-algorithm genetic-programming genetic


【解决方案1】:

主要问题是您不了解工具箱的工作原理。你应该参考the documentation 来了解整个想法。

因此,适应度函数应该是 function handle 并且应该返回一个标量。

健身

处理适应度函数。适应度函数应该接受一个 长度为 nvars 的行向量返回一个标量值

首先,您的功能定义不明确。如果你想定义一个匿名函数,你应该

% A function handle to an anonymous function that returns an scalar.
% You should change this function accordingly to your expectations.
% Also, note that this handle could be of a function defined in a file too.
parabola = @(x) prod(x);
% Parameters for the GA
optGA = gaoptimset('PlotFcns', @gaplotbestfun, 'PlotInterval', 10, 'PopInitRange', [-10 ; 10]);
[Xga,Fga] = ga(parabola,2,optGA)

使用 GA 的 GUI 也可以实现相同的目的。如果你想在 m 文件中定义你的函数,你应该有类似的东西:

抛物线.m

function [y] = parabola(x)
% This should return a scalar
y = prod(x);

你定义句柄就像fh = @parabola。在上面的代码中,您将parabola 替换为新句柄fh

我希望这可以帮助您入门。

【讨论】:

  • 非常感谢!!!这对我很有帮助!
  • 你应该使用 ga(@(x)parabola(x),1)。调用“ga”函数时,需要指定优化参数。
猜你喜欢
  • 2020-02-24
  • 2017-05-30
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
  • 2012-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多