GA using arrays in objective function

3 visualizaciones (últimos 30 días)
Ulika Naidoo
Ulika Naidoo el 19 de En. de 2021
Comentada: Ulika Naidoo el 20 de En. de 2021
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 comentarios
Alan Weiss
Alan Weiss el 20 de En. de 2021
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 el 20 de En. de 2021
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

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Genetic Algorithm en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by