about gatool

Hello! I have a fitness functiion:
function z = my_fun(x)
Skorost8.input(1,1).mf(1,1).params=[x(1),x(2),x(3)];
Skorost8.input(1,1).mf(1,2).params=[x(4),x(5),x(6)];
Skorost8.input(1,1).mf(1,3).params=[x(7),x(8),x(9)];
Skorost8.input(1,1).mf(1,4).params=[x(10),x(11),x(12)];
Skorost8.input(1,2).mf(1,1).params=[x(13),x(14),x(15)];
Skorost8.input(1,2).mf(1,2).params=[x(16),x(17),x(18)];
Skorost8.input(1,2).mf(1,3).params=[x(19),x(20),x(21)];
load_system('petina.mdl');
sim('petina.mdl');
close_system('petina.mdl');
z=simout.signals.values;
And petina.mdl file, which it use. The mdl-file write to workspace a massive of values, which must be a minimal (z=simout.signals.values;)
When I try to use it with gatool I get:
>> x=ga(@my_fun,21);
??? Subscripted assignment dimension mismatch.
Error in ==> fcnvectorizer at 14
y(i,:) = feval(fun,(pop(i,:)));
Error in ==> makeState at 47
Score =
fcnvectorizer(state.Population(initScoreProvided+1:end,:),FitnessFcn,1,options.SerialUserFcn);
Error in ==> gaunc at 41
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ==> ga at 279
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Caused by:
Failure in user-supplied fitness function evaluation. GA cannot continue.
How to use correctly gatool in this case? thank you very much!

 Accepted Answer

Sean de Wolski
Sean de Wolski on 5 Jan 2012
dbstop if error
Which will stop deep inside of ga(). Now put a break point in first line of your objective function. Run the feval line that causes the error and step through your fitness function to find the error and analyze the inputs.

5 Comments

Natalia
Natalia on 5 Jan 2012
Thank you very much for your answer!
I have done dbstop if error and have put a break point.
And what I have...
>> dbstop if error
>> x=ga(@my_fun,21);
6 Skorost8.input(1,1).mf(1,1).params=[x(1),x(2),x(3)];
7 Skorost8.input(1,1).mf(1,2).params=[x(4),x(5),x(6)];
8 Skorost8.input(1,1).mf(1,3).params=[x(7),x(8),x(9)];
9 Skorost8.input(1,1).mf(1,4).params=[x(10),x(11),x(12)];
11 Skorost8.input(1,2).mf(1,1).params=[x(13),x(14),x(15)];
12 Skorost8.input(1,2).mf(1,2).params=[x(16),x(17),x(18)];
13 Skorost8.input(1,2).mf(1,3).params=[x(19),x(20),x(21)];
16 load_system('petina.mdl');
17 sim('petina.mdl');
18 close_system('petina.mdl');
19 z=simout.signals.values;
End of function my_fun.
End of function globaloptim\private\validate>@(x)fitness(x,FitnessFcnArgs{:}).
??? Subscripted assignment dimension mismatch.
Error in ==> fcnvectorizer at 14
y(i,:) = feval(fun,(pop(i,:)));
Error in ==> makeState at 47
Score =
fcnvectorizer(state.Population(initScoreProvided+1:end,:),FitnessFcn,1,options.SerialUserFcn);
Error in ==> gaunc at 41
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ==> ga at 279
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Caused by:
Failure in user-supplied fitness function evaluation. GA cannot continue.
>>
On the first step it generates input values and passes my function up to the end, substituting values into the fis-struct in workspace. Open and simulate mdl. Not clearly why there is an error. May be the resalt of function is a massiv of values?
Sean de Wolski
Sean de Wolski on 5 Jan 2012
How many outputs/what is the size of the output that 'myfun' generates? It should be one scalar output. For multiple objectives, you need gamultiobj (doc gamultiobj).
Natalia
Natalia on 5 Jan 2012
One output. Size of the output is 1x450. I need gamultiobj?
Sean de Wolski
Sean de Wolski on 5 Jan 2012
Yes, a 1x450 would be 450 objectives (which sounds intuitively like a lot (way too many) to me!). Especially since you only want to optimize 21 variables.
If you look in the doc for ga() on line one it explains the output:
"x = ga(fitnessfcn,nvars) finds a local unconstrained minimum, x, to the objective function, fitnessfcn. nvars is the dimension (number of design variables) of fitnessfcn. The objective function, fitnessfcn, accepts a vector x of size 1-by-nvars, and returns a scalar evaluated at x."
Could you explain what you're trying to optimize?
Natalia
Natalia on 5 Jan 2012
I try to optimize the parametrs of fuzzy-logic controller, which describe into the fis file and use in my mdl. gamultiobj has worked. I don't have a erorrs :) Thank you :)

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!