只要 MATLAB ga optimtool 迭代,刷新额外变量的最佳方法是什么?
Which is the best way to refresh an extra variable as long as MATLAB ga optimtool iterates?
我使用 ga MATLAB optimtool 来最小化一个 objective 函数。
我在同一个脚本中创建了两个函数 main.m
。
如果我不使用变量 a
遗传算法
效果很好。当我引入变量 a
调用
它在 main.m
中的每次迭代 a = fcn( a );
然后我收到错误消息 输入参数不足。 (第 5 行)
%% main function
function [x,fval,a] = main()
nvars = 1; a = 0; % assign the number of values and the variable a
a = fcn( a,t ); % call the fitness fcn
[x,fval] = ga(@(t) fcn(t),nvars);
end
%% fitness function
function [ y,a ] = fcn( a,t )
y = abs( t - 1 ); % objective fcn
a = a + 1;
end
我提到a
是一个额外的变量,与ga的操作无关。
我意识到尽管 Ι 第一次刷新值 a
,但我没有
只要遗传算法迭代找到更好的值,就设法改变 a
。
有什么想法吗?提前致谢!
首先,如果您的目标只是计算 ga
对健身函数的调用次数,您已经可以通过 [=15= 的 output
输出访问它] :
[x,fval,exitflag,output] = ga(fitnessfcn,nvars,...) returns output, a
structure that contains output from each generation and other
information about the performance of the algorithm.
当你看这个output
变量里面是什么的时候,直接发现:
output =
problemtype: 'unconstrained'
rngstate: [1x1 struct]
generations: 56
funccount: 2850
message: 'Optimization terminated: average change in the fit...'
maxconstraint: []
并且可以通过调用 output.funccount
来访问函数的调用次数
如果问题更多是关于如何更新适应度函数中的任何额外变量,我认为你最好的选择是使用 global
个变量:
测试脚本
global a
a=0;
[x,fval,output] = main();
main
函数
function [x,fval,output] = main()
nvars = 1;
[x,fval,~,output] = ga(@fcn,nvars);
end
fcn
函数
function [ y ] = fcn(t)
global a
y = abs( t - 1 ); % objective fcn
a = a + 1;
end
产出
output =
problemtype: 'unconstrained'
rngstate: [1x1 struct]
generations: 100
funccount: 5050
message: 'Optimization terminated: maximum number of gener...'
maxconstraint: []
a =
5050
你可以看到 a
等于 output.funccount
,这意味着它在每次 fcn
调用时都会刷新
我使用 ga MATLAB optimtool 来最小化一个 objective 函数。
我在同一个脚本中创建了两个函数 main.m
。
如果我不使用变量 a
遗传算法
效果很好。当我引入变量 a
调用
它在 main.m
中的每次迭代 a = fcn( a );
然后我收到错误消息 输入参数不足。 (第 5 行)
%% main function
function [x,fval,a] = main()
nvars = 1; a = 0; % assign the number of values and the variable a
a = fcn( a,t ); % call the fitness fcn
[x,fval] = ga(@(t) fcn(t),nvars);
end
%% fitness function
function [ y,a ] = fcn( a,t )
y = abs( t - 1 ); % objective fcn
a = a + 1;
end
我提到a
是一个额外的变量,与ga的操作无关。
我意识到尽管 Ι 第一次刷新值 a
,但我没有
只要遗传算法迭代找到更好的值,就设法改变 a
。
有什么想法吗?提前致谢!
首先,如果您的目标只是计算 ga
对健身函数的调用次数,您已经可以通过 [=15= 的 output
输出访问它] :
[x,fval,exitflag,output] = ga(fitnessfcn,nvars,...) returns output, a structure that contains output from each generation and other information about the performance of the algorithm.
当你看这个output
变量里面是什么的时候,直接发现:
output =
problemtype: 'unconstrained'
rngstate: [1x1 struct]
generations: 56
funccount: 2850
message: 'Optimization terminated: average change in the fit...'
maxconstraint: []
并且可以通过调用 output.funccount
如果问题更多是关于如何更新适应度函数中的任何额外变量,我认为你最好的选择是使用 global
个变量:
测试脚本
global a
a=0;
[x,fval,output] = main();
main
函数
function [x,fval,output] = main()
nvars = 1;
[x,fval,~,output] = ga(@fcn,nvars);
end
fcn
函数
function [ y ] = fcn(t)
global a
y = abs( t - 1 ); % objective fcn
a = a + 1;
end
产出
output =
problemtype: 'unconstrained'
rngstate: [1x1 struct]
generations: 100
funccount: 5050
message: 'Optimization terminated: maximum number of gener...'
maxconstraint: []
a =
5050
你可以看到 a
等于 output.funccount
,这意味着它在每次 fcn
调用时都会刷新