how to fix the error "Your fitness function must return a scalar value."

5 views (last 30 days)
hi i am writting a program for profit mazimization using ga. i have tried many methods already suggested. none of them worked. the errors i am getting are:
Error using makeState (line 56)
Your fitness function must return a scalar value.
Error in gacon (line 39)
state = makeState(GenomeLength,SubFitness,Iterate,subtype,options);
Error in ga (line 405)
[x,fval,exitFlag,output,population,scores] = gacon(FitnessFcn,nvars, ...
functionm = profit(~)
pj= [2.5 2 1.5];%price of fish species j
Fi= [1 0.8 0.8];%fixed cost of boats of class i
Vi= [0.01 0.008 0.008];%variable costs of boats of class i
Qij= [0.0002 0.0001 0.0 0.0 0.0002 0.00005 0.0 0.0 0.0002];%Catchability coefficient of fish species j by boat class i
kj= [2000 2500 4000];%Carrying capacity of fish species j
Rj= [0.1 0.5 0.3];%Growth rate of fish species j
xb= [100 100 100];%constraints upper bound values
xe= [275 160 200];%constraints upper bound values
xl= [500 500 500];%constraints upper bound values
xfj=zeros(1,3);
%-----fishing mortality-------
fori=1:3
ifi >1
xfj(i)= xfj(i-1)+(Qij(i).*xb(i).*xe(i));
else
xfj(i)= (Qij(i).*xb(i).*xe(i));% else
end
end
%---then evaluates catch from this fishing mortality rate--
xcj=zeros(1,3);
forj=1:3
ifj>1
xcj(j)=xcj(j-1)+((kj(j).*xfj(j))-(kj(j)/Rj(j)).*(xfj(j).^2));
else
xcj(j)=(kj(j).*xfj(j)-(kj(j)/Rj(j)).*xfj(j).^2);
end
end
%max profit
a=zeros(1,3);
fork=1:3
ifk>1
a(k)= a(k-1)+(pj(k).*xl(k));
else
a(k)=(pj(k).*xl(k));
end
end
b=zeros(1,3);
forl=1:3
ifl>1
b(l)=b(l-1)+(Fi(l).*xb(l)-Vi(l).*xe(l).*xb(l));
else
b(l)=(Fi(l).*xb(l)-Vi(l).*xe(l).*xb(l));
end
end
n=a-b;
contraints
function[c,ceq] = constraints(xcj,xl)
xl= [500 500 500];
Qij= [0.0002 0.0001 0.0 0.0 0.0002 0.00005 0.0 0.0 0.0002];
xb= [100 100 100];
xe= [275 160 200];
kj= [2000 2500 4000];
Rj= [0.1 0.5 0.3];
xfj=zeros(1,3);
%-----fishing mortality-------
fori=1:3
ifi >1
xfj(i)= xfj(i-1)+(Qij(i).*xb(i).*xe(i));
else
xfj(i)= (Qij(i).*xb(i).*xe(i));% else
end
end
%---then evaluates catch from this fishing mortality rate--
xcj=zeros(1,3);
forj=1:3
ifj>1
xcj(j)=xcj(j-1)+((kj(j).*xfj(j))-(kj(j)/Rj(j)).*(xfj(j).^2));
else
xcj(j)=(kj(j).*xfj(j)-(kj(j)/Rj(j)).*xfj(j).^2);
end
end
c1=xcj;
c2=xl;
c=[c1 c2];
ceq=[];
ga function
x=ga(@profit,3,[],[],[],[],xb,xl,@constraints)

Answers (1)

Walter Roberson
Walter Roberson 2021年8月7日
functionm = profit(~)
ga() is going to be passing in a candidate configuration as a row vector of length 3. You need to evaluate the suitability of that particular row vector, and return a scalar "cost" associated with it.
You defined your function in a way that says that the cost to be returned is whatever is stored in the variable named m by the function. However, your code does not assign anything to the variable m . The error message you show us cannot occur with the code you posted: the code would have instead failed saying that m was not assigned to.
You assign to a variable n in your code, but n=a-b and a and b are both vectors, so n would be a vector. The cost you return must be a scalar.
If you are trying to minimize three different objectives simultaneously then you cannot use ga() for that, and need to instead use gamultiobj()
3 Comments
Walter Roberson
Walter Roberson on 8 Aug 2021
Your code currently ignores all inputs, and your code has no randomness in it. Your code currently always caclulates the same thing.
You need to rethink how your structure your calculations.
You should be thinking in terms like,
"I want to find out the best combination of (SOMETHING) to use, to maximize profits. The number of inputs I will have at a time is (NVARS). For these inputs, the lower bound for each of the inputs is (LOWER) and the upper bound for each of the inputs is (UPPER). If I were given a particular combination of inputs, then the way I would calculate the profits is (LIKE THIS CODE "profit") . But there are some combinations of input that are not valid, and given a particular combination of inputs, the way you can tell whether the combination is valid is by looking at the output of (THESE "constraints").
But ga() is not able to directly maximize anything, so the way I handle that is to have this little function "negprofit" that is negprofit = @(INPUTS)-profit(INPUTS) and I will ask ga() to minimize negprofit, since minimizing the negative of something is the same as mazimizing the thing."
Note:
Currently there are a lot of values that you recalculate every time, like your a 向量目前并不依赖the input. It is inefficient to recaclulate those every time. You can improve performance by calculating the values that are independent of the inputs before calling ga(), and passing the already-calcuated values into ga(); see //www.tianjin-qmedu.com/help/matlab/math/parameterizing-functions.html

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!