How to formulate a fitness function cosists of sum of different variables?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sherif Shokry
am 23 Jan. 2018
Kommentiert: Sherif Shokry
am 8 Feb. 2018
In this fitness function I couldn't run the code. It don't know the appropriate way to formulate it.
function y= fitness(b,a,c,d,N,S)
y=2*sum (a1+b1+c1-d1+(N1/S)) + sum (a2+b2+c2-d2+(N2/S));
a1= [0 23];
a2= [0 38];
c1= [0 4];
c2= [0 4];
d1= [5 4];
d2= [4 6];
N1= [4 5];
N2= [6 3];
fun=@(b)fitness(b,[a1,a2],[c1,c2],[d1,d2],[N1,N2],S);
lb= [0 0];
ub= [10 10]
b= fmincon(fun,b0)
end
4 Kommentare
michio
am 24 Jan. 2018
Thanks for the comments. What is the reproduction step? Do you put all of the code into the command line? Do you define a function?
Akzeptierte Antwort
michio
am 24 Jan. 2018
Hmm, I'm still struggling to understand your situation. This is pure speculation, but I'd try the followings.
First, define a objective function. For example, save the following as fitness.m
function y= fitness(b,a,c,d,N,S)
y=2*sum (a+b+c-d+(N/S)) + sum(a+b+c-d+(N/S));
end
Then copy & paste the following as a script, say as samplefmincon.m.
a1= [0 23];
a2= [0 38];
c1= [0 4];
c2= [0 4];
d1= [5 4];
d2= [4 6];
N1= [4 5];
N2= [6 3];
fun=@(b)fitness(b,[a1,a2],[c1,c2],[d1,d2],[N1,N2],S);
lb= [0 0];
ub= [10 10]
b= fmincon(fun,b0)
Then run the samplefmincon.m script. In this case I get the error saying ""Undefined function or variable 'b0'". This is because I didn't define b0.
If you are not sure the difference between script and function, then please see https://jp.mathworks.com/help/matlab/learn_matlab/scripts-and-functions.html
Also there are several working example that uses fmincon. So it should be a good starting point. https://jp.mathworks.com/help/optim/ug/fmincon.html
3 Kommentare
michio
am 24 Jan. 2018
On the previous comment, I just wanted to give you an example that you'd better follow in posting questions, be specific so that others can reproduce your issues. I didn't meant it as a solution to your issues, because I still don't understand your problem.
Now, you, or we, need to review your objective function/script and check if it does what you want them to do. Obvious problem is this one.
fun=@(b)fitness(b,[a1,a2],[c1,c2],[d1,d2],[N1,N2],S);
In the script, a variable S is not defined. Also 4 input values are essentially 1x4 array, ie. [a1,a2], [c1,c2], [d1,d2],[N1,N2]), but not sure if b is 1x4 array or not. Whatever the size of b would be, you need to be very careful not to have dimension mismatch on the fitness.
I believe this is all I can say based on my understanding of your problem statement. Please refer to the documentation page of fmincon https://jp.mathworks.com/help/optim/ug/fmincon.html very carefully. Again you can find some "working" examples on the page.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu ソルバーベースの非線形最適化 finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!