A simple example in GA which is not working for me!! Anybody can answer, just have a look!!
1 view (last 30 days)
Show older comments
I have eight hungry sources (x1,x2,x3,x4,x5,x6,x7,x8) and four donors (w1,w2,w3,w4). ============================
I would like to allocate from donors to the hungry sources in the best manner with which the remained resource from all four donors will be as minimum as possible at the end of the allocation.
============================
I also need to indicate that what resource I can be allocated from each donor to each hungry source.
Therefore I have a matrix of 16 v [v(1), v(2), …. ,v(32)]. For instance the total allocated source from four donors :
to x1= v(1) + v(2)+v(3)+v(4)
to x2 = v(5) + v(6)+v(7)+v(8)
to x3 = v(9) + v(10)+v(11)+ v(12)
to x4 = v(13) + v(14)+v(15)+v(16) ......
============================
Fitness Function started here:
function [wRemAlo] = ResourceAllocationb(v,x1,x2,x3,x4,x5,x6,x7,x8,w1,w2,w3,w4)
%To minimise:
wRemAlo = (w1 + w2 + w3 + w4) - (v(1)+ v(2)+ v(3)+ v(4) + v(5)+ v(6) + v(7)+ v(8) + v(9) + v(10) + v(11) + v(12) + v(13)+ v(14) + v(15) + v(16) + v(17)+ v(18)+ v(19)+ v(20) + v(21)+ v(22) + v(23)+ v(24) + v(25) + v(26) + v(27) + v(28) + v(29)+ v(30) + v(31) + v(32)) ;
Fitness function ended here.
My constraints:
w1 = 0; w2 = 0; w3 = 0; w4 = 1958000;
x1 = 10; x2 = 10; x3 = 10; x4 = 10; x5 = 10; x6 = 10; x7 = 10; x8 = 10;
ObjectiveFunction = @(v) ResourceAllocationb (v,x1,x2,x3,x4,x5,x6,x7,x8,w1,w2,w3,w4);
nvars = 32; A=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ;
1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ;
0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,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1, 0, 0, 0, 1 ];
b=[w1+w2+w3+w4;x1;x2;x3;x4;x5;x6;x7;x8;w1;w2;w3;w4]; LB = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
[v,fval] = ga(ObjectiveFunction,nvars,A,b,[],[],LB,[],[])
%First line of the matrix: the allocated sources should not be more than the total available source on all donor (w1+w2+w3+w4)
%Second line of the matrix: the allocated source to x1 from all donors should not be more that what x1 is requested
%Third line of the matrix: the allocated source to x2 from all donors should not be more that what x2 is requested
%Forth line of the matrix: the allocated source to x3 from all donors should not be more that what x3 is requested
%Fifth line of the matrix: the allocated source to x4 from all donors should not be more that what x4 is requested ……
%Line 10 of the matrix: the allocated source to x1, x2, x3 to x8 from donor w1 should not be more that what w1 has
%Line 11 of the matrix: the allocated source to x1, x2, x3 to x8 from donor w2 should not be more that what w2 has
%Line 12 of the matrix: the allocated source to x1, x2, x3 to x8 from donor w3 should not be more that what w3 has
%Line 13 of the matrix: the allocated source to x1, x2, x3 to x8 from donor w4 should not be more that what w4 has
b=[w1+w2+w3+w4;x1;x2;x3;x4;w1;w2;w3;w4];
LB = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
[v,fval] = ga(ObjectiveFunction,nvars,A,b,[],[],LB,[],[])
End of my constraints
%The output I receive:
v =
Columns 1 through 12
0.0000 0.0002 0.0000 10.0000 0.0000 0.0003 0.0000 10.0000 0.0000 0.0001 0.0000 0.8140
Columns 13 through 24
0.0000 0.0000 0.0000 10.0000 0.0000 0.0000 0.0010 1.7436 0.0000 0.0002 0.0000 1.0021
Columns 25 through 32
0.0000 0.0000 0.0000 7.0965 0.0009 0.0000 0.0000 1.0625
fval =
1.9580e+006
WHY the output is not an optimal answer, I have lots of resources to allocate why GA did not consider that???
0 Comments
Answers (1)
Alan Weiss
on 5 Jul 2012
I think this is a linear programming problem, and you would do better to use linprog to solve it.
However, if you are just trying to understand how ga works, then I think the problem is in the initial population not being very widely dispersed. Perhaps give a few artificial upper bounds, or give an initial range that is commensurate with your problem, and certainly give a large population size, several hundred or a thousand.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!