How to formulate a fitness function cosists of sum of different variables?
5 views (last 30 days)
Show older comments
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 Comments
michio
on 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?
Accepted Answer
michio
on 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 Comments
michio
on 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.
More Answers (0)
See Also
Categories
Find more on ソルバーベースの非線形最適化 in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!