Trouble import my trained ANN model as fitness function in GA.

4 views (last 30 days)
Hi,
I am new to matlab code.
I have trained an ANN model for a fuel cell performance prediction, and I want to use this trained model as my fitness function in GA to find maximum power output.
Specification for my ANN model: Inputs: temperature, anode humidity, cathode humidity,voltage (4 inputs). Output: Current density(1 output)
Here, I have several questions regarding with fitness functin in GA:
  1. My output for this ANN model is a 1XQ (Q is number of sample) row vector. when I try to use GA, the error appears that "Your fitness function must return a scalar value." Is there any solution to deal with this issue. I have tried to vectorize my fitness function. I tried to vectorize my fitness function, but failed.
clear all
close all
clc
load net
x_New = csvread('Curve_Test_Input.csv');
x1 = x_New';
input_para = x1;
voltage = input_para(4,:);
objFcn = @(x1) -sim(net,x1').*voltage;
power = objFcn(x1');
%[xOpt fVal] = ga(objFcn,4)
2. I tried to vectorize my fitness function, and set lower and upper bound for my inputs parameters. But the error appears that "Matrix dimensions must agree."
However, when I comment my last line of code :
%[xOpt fVal] = ga(objFcn,nVars,A,b,Aeq,beq,lb,ub,nonlcon,options)
My program can run without problem. Therefore, I guess something must wrong when I import ANN model into GA as fitness function.
clear all
close all
clc
load net
x_New = csvread('Curve_Test_Input.csv');
x1 = x_New';
input_para = x1;
voltage = input_para(4,:);
objFcn = @(x1) -sim(net,x1').*voltage;
power = objFcn(x1');
options = optimoptions('ga','UseVectorized',true);
A = [];
b= [];
Aeq = [];
beq = [];
lb = [313,0.25,0.25,0.3];
ub = [333,0.9,0.9,0.9];
nVars = 4;
nonlcon = [];
[xOpt fVal] = ga(objFcn,nVars,A,b,Aeq,beq,lb,ub,nonlcon,options)
Could you give me some suggestions for how to import an ANN model with input of [4XQ] and output of [1XQ] matrix, into GA as my fitness function?
A screenshot of my workspace varaibles is also included.
Thank you very much~

Accepted Answer

Walter Roberson
Walter Roberson on 18 May 2020
when I try to use GA, the error appears that "Your fitness function must return a scalar value." Is there any solution to deal with this issue.
No, there is no solution. ga() is only ever suitable for optimizing one function at a time. You are trying to optimize one function per sample.
If you had information about the relative importance of the output of each sample, then you might be able to create a scalar weighted optiization measure, perhaps.
For example, suppose that you can increase the output for sample 17 by 0.2 by lowering the output for sample 23 by 0.1 . Is that worth doing? How does it compare to increasing the output for sample 17 by sqrt(2)/10 by lowering the output for sample 23 by sqrt(2)/10 ?
When you have multiple outputs and no weighting function, then the best you can do is use gamultiobj() to find pareto fronts.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!