A very simple question for GA

1 view (last 30 days)
Nagh Meh
Nagh Meh on 27 Jun 2012
Dear all, I have a very simple question as follows, I just want to clarify if I am doing right or not:
I have a function named: function [wRemAlo] = Resource allocation (x)
x: have three possible values which I am entering in command line before running the function e.g. I put "x = [90,20,0,10]" and
After receiving the input values of x, my function allocate resources to 16 variables v (v(1) to v(16)) and return the single variable of "wRemAlo".
I want ga to minimise the output of the function: "wRemAlo"
My function is working correct and return the correct value for "wRemAlo" whenever I run it.
I put constraints as follows to put restrictions on ga decisions and pass the handle for the function to ga solver as follows:
ObjectiveFunction = @ResourceAllocation;
nvars = 16; A=[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, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 ; 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 ; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 ; 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 ; 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 ; 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0 ; 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 ];
b=[105;90;20;0;10;0;0;45;60]; 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,[],[])
The fval value returns after running ga dose not make any sense! Should this value be as same as the value for the function output: "wRemAlo"
Please somebody tell me if I am doing correct or not??
regs, Melody

Accepted Answer

Seth DeLand
Seth DeLand on 27 Jun 2012
It looks like there is an issue with the way your decision variables are defined. The input to the objective function (which is currently x), should be a vector of the decision variables. If the v's are your decision variables that you want to optimize, then the objective function should look like
[wRemAlo] = ResourceAllocation(v)
If you also need to use the values in x inside ResourceAllocation, there's an example here that shows how to pass additional arguments (in the minimizing using additional arguments section).

More Answers (1)

Nagh Meh
Nagh Meh on 27 Jun 2012
Thanks a lot Seth,
I changed it to followings:
==================================== w1 = 0; w2 = 0; w3 = 45; w4 = 60;
x1 = 90; x2 = 20; x3 = 0; x4 = 10;
ObjectiveFunction = @(v) ResourceAllocation (v,x1,x2,x3,x4,w1,w2,w3,w4);
nvars = 16; A=[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, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 ;
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 ;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 ;
1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 ;
0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 ;
0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0 ;
0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 ];
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,[],[]) ====================================
I also need to clarify one thing:
In my fitness function:
[wRemAlo] = ResourceAllocation(v,x1,x2,x3,x4,w1,w2,w3,w4)
v(1), v(2), ..... v(16) are my decision variables that I want ga to find the best values for them with which "wRemAlo" would be minimised.
In my fitness function, I calculate the values for v through loops and fors and I assigned the calcuted value at the end of the function to v(1) to v(16) e.g. :
v(1) = H1_all_w1; v(2) = H1_all_w2; v(3) = H1_all_w3; . . . . .
With which I have mentioned how the values for all v() s will be calculated through the function.
and when I call the function from the CL:
[v] = ResourceAllocation(v,x1,x2,x3,x4,w1,w2,w3,w4)
the values for vs are:
Columns 1 through 9
0 0 15 20 0 0 15 5 0
Columns 10 through 16
0 0 0 0 0 10 0
I would like to know, am I doing correct or I do not need to assign the values for vs inside the pure function as it may give me the wrong answers?
regs, M.

Community Treasure Hunt

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

Start Hunting!