How to formulate a fitness function cosists of sum of different variables?

5 Ansichten (letzte 30 Tage)
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
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?
Sherif Shokry
Sherif Shokry am 24 Jan. 2018
I type the code in the (editor) window and then try to run. I got this error in the command window.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

michio
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
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.
Sherif Shokry
Sherif Shokry am 8 Feb. 2018
Since the fmincon Fun is used to find a minimizer x of the function described
x = fmincon(fun,x0,A,b) starts at x0
So if I have to optimization variables, how can I modify fmincon fun to minimize both variables

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!