找到“y=x*x”的最小值在Matlab中使用遗传算法

发布于 2024-12-28 04:25:33 字数 533 浏览 5 评论 0原文

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

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

    函数 y= 抛物线(x)
        y=x*x;
    结尾
    
  2. 然后我启动 GATOOL 并指定此函​​数,如 @parabola< /p>

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

    当我按开始按钮时,我看到结果:

    <块引用>

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

Would somebody help me please in this question. I'm new in Matlab... And it's a bit hard for me to understand how to create and use genetic algorithm in Matlab.
If anybody could help to write some very simple code for searching minimum/maximum of specified function.
I read that the gatool should be used for that... but I can't understand the examples of Matlab help network. I'm doing the next steps:

  1. In text editor I'm typing the next:

    function y= parabola(x)
        y=x*x;
    end
    
  2. Then I'm launching the GATOOL and specifying this function like @parabola

  3. Setting the number of variables (equals 2)
  4. Initial range = [-10;10].
  5. The other parameters are set as Default

    When I press Start Button I see a result:

    Error in fitnessfcn: Input argument "x" is undefined.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

困倦 2025-01-04 04:25:33

主要问题是您不了解工具箱是如何工作的。您应该参考文档来了解整个想法。

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

fitnessfcn

健身功能的句柄。适应度函数应该接受
长度为 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 文件中定义函数,您应该具有如下内容:

parabola.m

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

并且定义句柄,如 fh = @parabola.在上面的代码中,您将 parabola 替换为新句柄 fh

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

The main problem is that you don't understand how the toolbox works. You should refer to the documentation to get the whole idea.

So, the fitness function should be a function handle and should return a scalar.

fitnessfcn

Handle to the fitness function. The fitness function should accept a
row vector
of length nvars and return a scalar value.

First, your function is not well defined. If you want to define an anonymous function you should

% 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)

The same can be achieved with the GUI of GA. In case you want to define your function in an m file you should have something like:

parabola.m

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

And you define the handle like fh = @parabola. And in the code above you replace parabola for the new handle, fh.

I hope this help you get started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文