【问题标题】:Matlab Optimisation - Minimise objective function using genetic algorithmMatlab 优化 - 使用遗传算法最小化目标函数
【发布时间】:2016-09-13 22:10:02
【问题描述】:

我想为包含大约 400 行脚本的函数设置通用算法。脚本本身是一个优化过程,我想使用遗传算法找到优化过程中的最佳输入参数(M 和OPratio)。 M 介于 0 and 10^7 和 OPratio 之间,介于 0 and 1 之间。 脚本的作用是:

 NPVtotal = cut_off_optimisation(M,OPratio)

为遗传算法设置:

nvars = 2;    % Number of variables
LB = [0 0];   % Lower bound
UB = [10000000 1];  % Upper bound
X0 = [6670000 0.45]; % Start point 
options.InitialPopulationMatrix = X0;
[M,OPratio,fval] = ga(cut_off_optimisation(M,OPratio),nvars,[],[],[],[],LB,UB)

我收到以下错误:

Undefined function or variable 'M'.

我是优化和遗传算法的新手,因此希望能提供任何帮助,如果需要更多信息,请告诉我。

【问题讨论】:

    标签: matlab genetic-algorithm


    【解决方案1】:

    首先我假设目标是最小化目标函数cut_off_optimisation。

    现在首先更新你的函数看起来像这样

    function y = cut_off_optimisation(x)    
    M=x(1);
    OPratio=x(2);
    
    %
    % paste body of your currently used function here
    %
    
    y=NPVtotal ;
    

    现在使用此代码最小化您的目标函数。

    nvars = 2;    % Number of variables
    LB = [0 0];   % Lower bound
    UB = [10000000 1];  % Upper bound
    X0 = [6670000 0.45]; % Start point 
    options = gaoptimset('PlotFcns',{@gaplotbestf},'Display','iter','InitialPopulation',X0);
    [x,fval] = ga(@cut_off_optimisation,nvars,[],[],[],[],...
                  LB,UB,[],options);
    M=x(1);
    OPratio=x(2);
    

    更新:如果您不想更新您的函数。只需运行此主要代码。将函数NPVtotal = cut_off_optimisation(M,OPratio)保存在与主代码相同的文件夹中。

    objectiveFunction=@(x)cut_off_optimisation(x(1),x(2));
    nvars = 2;    % Number of variables
    LB = [0 0];   % Lower bound
    UB = [10000000 1];  % Upper bound
    X0 = [6670000 0.45]; % Start point 
    options = gaoptimset('PlotFcns',{@gaplotbestf},'Display','iter','InitialPopulation',X0);
    [x,fval] = ga(objectiveFunction,nvars,[],[],[],[],...
                  LB,UB,[],options);
    M=x(1);
    OPratio=x(2);
    fval
    M
    OPratio
    

    更新:用于获取最终人口成员和健身值。将上面的 ga 函数调用语句替换为下面的语句。

    [x,fval,exitflag,output,population,score] = ga(objectiveFunction,nvars,[],[],[],[],LB,UB,[],options);
    M=x(1);
    OPratio=x(2);
    

    这里population 将拥有最终种群的成员,score 将拥有最终种群的适应度值。默认人口规模为20。所以你将在两个矩阵中都有20 rows。 population 中的列数将等于问题中的number of variables,score 将是一个列矩阵。您可以通过将选项PopulationSize 添加到gaoptimset 来更改人口规模。

    options = gaoptimset('PlotFcns',{@gaplotbestf},'Display','iter','InitialPopulation',X0,'PopulationSize',30);
    

    要了解更多关于gaoptimset 可用的options 以及他们预期的values 和他们的{default values}。转到 matlab 帮助并搜索 gaoptimset。在那里你会找到一张包含所有这些细节的表格。这是来自 matlab 网站http://in.mathworks.com/help/gads/gaoptimset.html 的链接。根据您的 matlab 版本,可能会有所变化。所以最好在matlab中使用帮助。

    【讨论】:

    • 感谢您的回答:在此处的 %past 函数体中......您是指函数的完整脚本还是只是函数?
    • 还有一个问题 - 我必须保存新函数还是我可以基本上在脚本中运行您的代码?
    • 好的,我会更新帖子,让它更容易实现
    • 使用答案更新部分的代码。无需更新任何其他内容。只需运行此代码。确保函数 NPVtotal = cut_off_optimisation(M,OPratio) 位于执行此代码的同一文件夹中。
    • 现在不需要定义 x1 和 x2 吗? ...我将在一秒钟内尝试更新的代码,但感谢您的帮助 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 2015-02-18
    相关资源
    最近更新 更多