- The input to the fitness needs to be a single variable
- MATLAB does not allow for arrays of function handles
Optimisation application: how do I create the fitness function?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Howard Clapp
le 10 Août 2015
Commenté : Howard Clapp
le 20 Août 2015
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?
0 commentaires
Réponse acceptée
Adam Barber
le 12 Août 2015
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:
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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!