GA using arrays in objective function

2 次查看(过去 30 天)
Hi,
Can anyone assist with how to get my GA to stop throwing errors.
If you have an example on how to use an array in a GA that would be much appreciated.
I am trying to pass the X value of the GA, a defined value, and three arrays into a function, mincost. The PV array and the battery are 5 x 1 and are independent arrays, but eleccostla3 is a 5 * 5. Each column in eleccostla3 references the value in the PV array, and each row references the battery array. The sum of these values should be the "fval" of the GA.
How do I set x as an integer and remove the "for loop" and actually use the GA.
This is the code I have:
mincostx = 1;
pvpriceinvonly = [1; 2; 3; 4; 5];
battery = [6; 7; 8; 9; 10];
eleccost = [7 8 9 10 11; 8 9 10 11 12; 9 10 11 12 13; 10 11 12 13 14; 11 12 13 14 15];
nvars = 6 ;
f = @(x)objpv(x, mincostx, pvpriceinvonly, battery, eleccost);
IntCon = [1 5];
[x fval] = ga(f,nvars, [], [], [], [], [], [], [], IntCon, []); %ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
function mincost = objpv(x, mincostx, pvpriceinvonly, battery, eleccost)
mincostla = [];
for a = 1:5
for b = 1:5
Maintenance = (mincostx + pvpriceinvonly(a) * 0.05 * (1/(1.07)^20) * 20);
Capitalla = mincostx + pvpriceinvonly(a) + battery(b);
Elec = eleccost(a,b);
mincostla = (Capitalla + Maintenance + Elec);
mincost = min(mincostla);
end
end
end
Thanks.
  2 个评论
Alan Weiss
Alan Weiss 2021-1-20
We cannot run this code because we don't have the objpv function. Please give us the full copy-pasted errors thrown from MATLAB so that we have a chance of understanding what you are seeing.
Alan Weiss
MATLAB mathematical toolbox documentation
Ulika Naidoo
Ulika Naidoo 2021-1-20
Hi Alan,
I noticed a typo in the array. I've eddited that.
Can I not use the function defined after I use the GA function?
Thanks,
Ulika

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by