Help to Run Genetic Algorithm

Hi everyone, I am trying run a ga which were given in article that I am read, but the following error appears and I don't know how fix it and I will show the steps lower and I attached the page in this ask.
Optimization running.
Error running optimization.
Undefined function 'b' for input arguments of type 'double'.
Steps:
1 .Write a function to minimize the weight wich is given by:
function y = min_weight(x)
y = ((3.14*p)./4000).*(b*m.^2 *z(1).^2 * (1+a.^2))-((D(i).^2 - d(o).^2)*.(l-b(w))-(n*d(p).^2 .* b(w))-((d(1).^2 + d(2).^2).*b));
2. Write a function with constraints:
c = [b(1)-F(s); b(2)-(F(s)./F(p)); b(3)-d(1).^3; b(4)-d(2).^3; ((1+a).*m.*z(1)./2)-b(5)];
Ceq = [];
3rd, 4th and 5th steps I am ok.
6. I put the lower and upper bounds as [20;15;30;18;1] and [20;15;30;18;1] respectively;
However I tryed make the other steps and the message said before appeared.So I am also leaving the picture of the optimization tool as the attachment for help you to help me.
I appreciate your attention
Rafael Zanetti

5 Comments

Please attach the full Matlab code you have. It appears that the site 47 would be useful from the atached .pdf
Hi Stephan, I appreciate your help, so I attached the functions
Stephan
Stephan on 25 Nov 2020
Edited: Stephan on 25 Nov 2020
What about Site 47 of the document and please elaborate what relations are:
D(i)
d(o)
b(w)
d(p)
F(s)
F(p)
--> For Matlab they all are EITHER interpreted ans functions with input argument inside the brackets - this is not the case i suspect OR they are interpreted as arrays, where you are interested in the value at the index that is represented by the variable inside the brackets. I also assume this is not the case - i suspect they are all scalars (?)
Also you use a addtionally vector b of size 1x5 in your constraint function - it is undefined
None of those are declared as variables in your code.
From the pdf-attachment we can learn:
b = x(1);
d1 = x(2);
d2 = x(3);
z1 = x(4)
m = x(5);
Please clarify
Hi Stephan, I attached the complete paper , so we have five independent variables that has lower and upper boundaries:
20<=b>=35;
15<=d1>=32;
30<=d2>=50;
18<=Z1>=25;
1<=m>=4;
The constraints are:
  1. b(1)-F(s);
  2. b(2)-(F(s)./F(p));
  3. b(3)-d(1).^3;
  4. b(4)-d(2).^3;
  5. ((1+a).*m.*z(1)./2)-b(5)
And maybe I could have undertood the error mine, I had not define the variables (b,d1,d2,Z1,m) as a correct argument (x(1),x(2),x(3),x(4),x(5)).
You are correct about the suspicious, they are all scalars and I have to correct the multipliers in the function. I will try run with the corrections and I will bring if it works or not.
Once again, I thank you so much Stephan.
I think you missed to build in this informations:
So far the code looks like
options = optimoptions('ga');
% options = optimoptions(options,'PopulationType', 'custom');
options = optimoptions(options,'Display', 'off');
nvars = 5;
lb = [20,15,30,18,1];
ub = [35,32,50,25,4];
[x,fval,exitflag,output,population,score] = ...
ga(@min_weight,nvars,[],[],[],[],lb,ub,@gear_constraint,[],options);
function [c,ceq] = gear_constraint(x)
b = x(1);
d1 = x(2);
d2 = x(3);
Z1 = x(4);
m = x(5);
c = [b(1)-F(s); b(2)-(F(s)./F(p)); b(3)-d1.^3; b(4)-d2.^3; ((1+a).*m.*Z1./2)-b(5)];
ceq = [];
end
function y = min_weight(x)
b = x(1);
d1 = x(2);
d2 = x(3);
Z1 = x(4);
m = x(5);
y = ((pi*p)./4000).*(b*m.^2 *Z1.^2 * (1+a.^2))-((D(i).^2 - d(o).^2).*(l-b(w))-...
(n*d(p).^2 .* b(w))-((d1.^2 + d2.^2).*b));
end
Where now the missing values have to be inputed for example
bw = 3.5*m;
...
...
Get rid of the brackets for scalars like b(w) --> this will cause problems, use bw or b_w instead for all of those

Sign in to comment.

Answers (0)

Asked:

on 24 Nov 2020

Commented:

on 25 Nov 2020

Community Treasure Hunt

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

Start Hunting!