我用Matlab fminsearch来最小化一个含有两个变量的方程,sum((interval-5).^2, 2)*factor,区间是包含5个值的向量。在步长为1的情况下,只能依次从值1到30中选择它们,其因子为0.1至0.9。
代码在下面。我认为区间值是正确的,但因子值是错误的。
区间值:3 4 5 6 7要素值: 0.6最终产出:6
我认为要素值应该是0.1,最终产出应该是1作为全局最小值。
%% initialization of problem parameters
minval = 1;
maxval = 30;
step = 1;
count = 5;
minFactor = 0.1;
maxFactor = 0.9;
%% the objective function
fun = @(interval, factor) sum((interval-5).^2, 2)*factor;
%% a function that generates an interval from its initial value
getinterval = @(start) floor(start) + (0:(count-1)) * step;
getfactor =@(start2) floor(start2 * 10)/10;
%% a modified objective function that handles constraints
objective = @(start, start2) f(start, fun, getinterval, minval, maxval, getfactor, minFactor, maxFactor);
%% finding the interval that minimizes the objective function
start = [(minval+maxval)/2 (minFactor+maxFactor)/2];
y = fminsearch(objective, start);
bestvals = getinterval(y(1));
bestfactor = getfactor(y(2));
eval = fun(bestvals,bestfactor);
disp(bestvals)
disp(bestfactor)
disp(eval)代码使用以下函数f。
function y = f(start, fun, getinterval, minval, maxval, getfactor, minFactor, maxFactor)
interval = getinterval(start(1));
factor = getfactor(start(2));
if (min(interval) < minval) || (max(interval) > maxval) || (factor<minFactor) || (factor>maxFactor)
y = Inf;
else
y = fun(interval, factor);
end
end我按亚当的建议尝试了GA功能。考虑到我的变量来自不同的范围和步骤,我将其改为两个不同的集合。这是我的零钱。
step1 = 1;
set1 = 1:step1:30;
step2 = 0.1;
set2 = 0.1:step2:0.9;
% upper bound depends on how many integer used for mapping
ub = zeros(1, nvar);
ub(1) = length(set1);
ub(2) = length(set2); 然后,我改变了目标函数
% objective function
function y = f(x,set1, set2)
% mapping
xmap1 = set1(x(1));
xmap2 = set2(x(2));
y = (40 - xmap1)^xmap2;
end在我运行代码之后,我想我得到了我想要的答案。
发布于 2019-11-23 09:01:13
集上ga()优化的说明
目标函数
f = xmap(1) -2*xmap(2)^2 + 3*xmap(3)^3 - 4*xmap(4)^4 + 5*xmap(5)^5;设置
set = {1, 5, 10, 15, 20, 25, 30}这套程序集包含7个元素:
set(1)
set(7)ga的输入将在1 to 7范围内。
下界是1,上界是7。
遗传算法的优化是通过计算适应度函数来完成的:对输入变量求f。
这里的技巧将是使用integer as input,稍后在评估f时使用上面讨论的mapping。
代码如下
% settting option for ga
opts = optimoptions(@ga, ...
'PopulationSize', 150, ...
'MaxGenerations', 200, ...
'EliteCount', 10, ...
'FunctionTolerance', 1e-8, ...
'PlotFcn', @gaplotbestf);
% number of variable
nvar = 5;
% lower bound is 1
lb = ones(1, nvar);
step = 2.3;
set = 1:step:30;
limit = length(set);
% upper bound depends on how many integers are used for mapping
ub = limit.*lb;
% maximization used the opposite of f as ga only does minimization
% asking ga to minimize -f is equivalent to maximizing f
fitness = @(x)-1*f(x, step, set);
[xbest, fbest, exitflag] = ga(fitness,nvar, [], [], [], [], lb, ub, [], 1:nvar, opts);
% get the discrete integer value and find their corresponding value in the set
mapx = set(xbest)
% objective function
function y = f(x, step, set)
l = length(x);
% mapping
xmap = zeros(1, l);
for i = 1:l
xmap(i) = set(x(i));
end
y = xmap(1) -2*xmap(2)^2 + 3*xmap(3)^3 - 4*xmap(4)^4 + 5*xmap(5)^5;
endhttps://stackoverflow.com/questions/59002429
复制相似问题