Optimisation application: how do I create the fitness function?

I have two functions: EndCarb and EndCost.
These functions use inputs of: ID [1 2] and Q [0.000001:0.000001:0.01], and give an output of carbon quantity and cost respectively. (EndCarb [ID,Q]; EndCost[ID,Q])
I want to use the gamultiobj tool to optimise the inputs of ID and Q to give the pareto optimal cost and carbon. So far I have been attempting to write a script along the lines of:
fun(1) = @(ID,Q) EndCost(ID,Q);
%parameterised function
Q=0.000001:0.000001:0.01;
%parameter
fun1a=@(ID) fun(1)(ID,Q);
%function of ID alone
ID=[1,2];
fun(2) = @(ID,Q) EndCarbon(ID,Q);
%parameterised function
Q=0.000001:0.000001:0.01;
%parameter
fun2a=@(ID) fun(2)(ID,Q);
%function of ID alone
ID=[1,2];
% Combine two objectives 'fun1' and 'fun2'
fun1and2 = @(ID,Q) [fun(1)(ID,Q) fun(2)(ID,Q)];
I'm not sure if this is the right track, as I am unable to get it to work. Can anybody advise anything?

 Respuesta aceptada

Hey Howard,
It looks like you are on the right track to defining your fitness function for "gamultiobj". I think the issues you are running into is that:
  1. The input to the fitness needs to be a single variable
  2. MATLAB does not allow for arrays of function handles
You can resolve the first issue by following the example at: http://www.mathworks.com/help/gads/computing-objective-functions.html
The example function there has 3 values, but only accepts one input argument
function z = my_fun(x)
z = zeros(1,3); % allocate output
z(1) = x(1)^2 - 2*x(1)*x(2) + 6*x(1) + 4*x(2)^2 - 3*x(2);
z(2) = x(1)*x(2) + cos(3*x(2)/(2+x(1)));
z(3) = tanh(x(1) + x(2));
You will need to do something similar where you combine "ID" and "Q" into a single variable that you pass to your objective function.
For the second issue, you can either use a cell array to define your function handles:
fun{1} = @(ID,Q) EndCost(ID,Q);
fun{2} = @(ID,Q) EndCarbon(ID,Q);
or just store them as separate variables.
Now, you can define your fitness function as something like the following:
fun1and2 = @(X) [fun{1}(X(1:2),X(3:end)) fun{2}(X(1:2),X(3:end))]; % ID and Q are "stacked" into X
Hope this helps get you on your way, and if this does not help then post the error(s) that you are seeing.
-Adam

2 comentarios

Hi Adam, thanks for the reply.
I'd like to ask a few questions to clarify what you are saying.
Firstly, for the first step am I looking for something along the lines of:
z(1)=ID=f(x)
z(2)=Q=f(x)
If this is the case, I am struggling to comprehend how to represent a variable that is either 1 or 2, and a variable that can be anything between 0.000001 to 0.01 using the term of X. Or am I approaching that incorrectly.
Secondly, once I succesfully complete the above, should I be substituting that into the
fun{1} = @(ID,Q) EndCost(ID,Q);
fun{2} = @(ID,Q) EndCarbon(ID,Q);
essentially re-writing it as:
fun{1} = @(x) EndCost(x);
fun{2} = @(x) EndCarbon(x);
Also, in the final section I am slightly unsure as to what the "X(1:2)" and "X(3:end)" are doing in the line "[fun{1}(X(1:2),X(3:end))"
and finally if "fun1and2" is my fitness function, does that mean my function should be something along the line of:
function fun1and2 = my_fun(x)
z(1)=ID=f(x)
z(2)=Q=f(x)
fun{1} = @(x) EndCost(x);
fun{2} = @(x) EndCarbon(x);
fun1and2 = @(X) [fun{1}(X(1:2),X(3:end)) fun{2}(X(1:2),X(3:end))];
Is this correct? And if so, how would I incorporate the z(1) and z(2)
Thanks again, and apologise for all the questions or if I have missed the point on some of what you have said.
Thanks for your help, managed to use what you said to approach the solution. Had a little issue but was just a simple error and a colleague spotted it, so it seems to be running smoothly now! Cheers again!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 10 de Ag. de 2015

Comentada:

el 20 de Ag. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by