GA using arrays in objective function

조회 수: 1 (최근 30일)
Ulika Naidoo
Ulika Naidoo 2021년 1월 19일
댓글: Ulika Naidoo 2021년 1월 20일
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