Function behaves normally except when being called by another function

1 view (last 30 days)
I am writing a code to evaluate the fitness function for evolutionary optimization purposes. The fitness function for the feasible points are their objective function and for the infeasible points is the maximum of the objective function value of the feasible points plus the maximum constraint violation of the infeasible points.
When I call the function solely to evaluate the fitness function, it works properly and returns correct outputs. But when being called from the main function of the optimization algorithm, it throws error:
The logical indices in position 1 contain a true value outside of the array bounds.
It also works fine in debugger mode. Part of the fitness function code is as follows:
InfeasibleSolutions = ...
LinearConstraintViolation | ...
lbConstraintViolation | ...
ubConstraintViolation | ...
NonlinearConstraintViolation;
FeasibleSolutions = ~InfeasibleSolutions;
ObjectiveFunctionValues = zeros(options.PopulationSize,1);
ObjectiveFunctionValues(FeasibleSolutions,:) = ...
fun(x(FeasibleSolutions,:)); % error is thrown from this line
MaximumObjectiveFunctionValue = max(ObjectiveFunctionValues);
The problem lies in logical indexing but I was not able to figure it out.
  2 Comments
dpb
dpb on 15 Mar 2019
Looks like you created a logical vector then tried to use that as a logical addressing vector...but not sufficient information provided to tell for certain.
Set a breakpoint and use the debugger to track down your logic (so to speak :) ) error...
dpb
dpb on 15 Mar 2019
Edited: dpb on 15 Mar 2019
"It also works fine in debugger mode. ..."
Excepting for the fact it doesn't or you wouldn't have posted the problem. :)
dbstop on error
then start the command that generates the error.
You have still not provided nearly enough to be able to have any clue as to what any of the intermediate variables really are and not even the actual error in context so we can see what diagnostic information ML provided...
We can't debug by divining...

Sign in to comment.

Answers (1)

Abdolkarim Mohammadi
Abdolkarim Mohammadi on 15 Mar 2019
Sorry dpb, it was my fault. The fitness function is called more than one time in the main algorithm, the last of which passes a smaller population than normal iterations and the fitness function was not able to handle the difference. Thanks for your rapid response.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!