Function behaves normally except when being called by another function
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
dpb
2019-3-15
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
2019-3-15
编辑:dpb
2019-3-15
"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...
回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!