Troubles with MATLAB genetic algorithm nonlinear constraint function

Hi everyone!
I have a question regarding an error I'm recieving regarding a nonlinear constraint function I am passing into MATLAB's genetic algorithm. This is the error message I get:
Error using ga (line 393)
Unable to perform assignment because the left and right sides have a different number of elements.
Error in OSCaR_GA_v2 (line 94)
[xf,fval,exitFlag,Output] = ga(f,m,[],[],[],[],lb,ub,nonlinconst,1:m,options);
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation.
I've made sure my nonlinear constraint accepts both vectors and arrays but I still can't pinpoint why I'm getting this error. I appreciate clearification on this issue!

回答(1 个)

If you re-run your code after executing,
>>dbstop if caught error
you will be able to trace the problem.

15 个评论

Thanks for the tip. I keep getting an error at line 21 in noncon2.m. I already knew thats where the issue is but I'm not sure why I'm getting that error. I've tested the function using different design variable arrays independently and it works every time. My confusion may be coming from not knowing how MATLAB's ga creates its initial design variable matrix. My thinking is that it'll creat a design variable array with my specified number of design variables by however large each population is but if that is the case II shouldn't be running into any errors.
I caught one error (in line 21 I was calling a specific parameter from array A when I should have just been calling a specific row of A) but now I'm getting a new error. Does anyone know what this may mean? I've attached the updated constraint function.
Caught-error breakpoint was hit in globaloptim\private\gadsplot at line 86. The error was:
Dot indexing is not supported for variables of this type.
86 position = plotState.(solver).position;
It means that either plotState or plotState.(solver) is not a structure variable. Therfore, Matlab doesn't understand why you are dot-indexing it.
The issue is that this is an error cropping up in MATLAB's ga so I don't know what in my code could be causing it. I don't build plotState or plotState.(solver) and I don't dot-index it. That's done when my parameters are passed into matlab ga. I was hoping someone with knowledge of matlab's ga may be able to provide insight as to why* that error occurs.
We need the code for dcosm_from_angles and find_LoN_Angles
I am not getting an error yet.
What are some sample inputs for the budget and the number of items?
budget is 1000 and try 3 for number of items
Note by the way that you coded
if size(unique(x),2) == size(x,2)
C(1:size(x,2)) = 0; % the condition is met, (C=<0)
else size(unique(x),2) < size(x,2);
C(1:size(x,2)) = 1; % the condition is not met, (C>0)
end
Notice that is not an "elseif" (and if it were an elseif you should have an "else" anyhow.)
By the way, your code also has a bug in the case where all of the selected masses are the same, which includes the case where only one target is selected.
Your program also doesn't notice negative budgets.
Your program does not have its own initial population, or controls on population, so it is able to receive the sample entries for all population elements, such as the x values all being 1. But when that happens then all of the masses are equal, and when you delete all of the entries equal to maximum pass, you are deleting all of the entries, which then leads you to problems in the nonlinear constraints.
Thank you for your comments, this makes sense now! For your second comment, I made some changes and I believe I have that issue solved now. I replaced that section with this:
norm_mass = mass;
x = find(mass == max(mass));
norm_mass(x(1)) = []; % remove the most massive object in the set
For your first comment, could you tell me in pseudo code what would fix this issue? I'm not great at coding so I only barely understand if statements, for loops, etc. This is basically what I want the code to do -
  • when the design variables have repeated numbers set the inequality vector to zero
  • when the design variables do not have repeated numbers set the inequality vector to one
C = zeros(size(x,2)+2,1);
%leave it at 0 if all the elements are unique
if size(unique(x),2) < size(x,2)
C(1:size(x,2)) = 1; % the condition is not met, (C>0)
end
Question: do you want all of the C entries to be set to 1 in that situation, or only the entries that are duplicates of another entry?
It doesn't much matter since the set will fail the constraints anyways. It seems like I'm still getting the same error as before even with these changes. Could the if else statement for the masses also be causing this problem?
The below is not erroring for me with 3 targets (it will still error with 1 target, so the one target is automatically the most massive and you are deleting it...)
%% Build inequality constraint vector
% Build corresponding mass vector
mass = zeros(1,size(x,2));
for i = 1:size(x,2)
mass(i) = A(x(i),7);
end
C = zeros(size(x,2)+2,1);
%leave it at 0 if all the elements are unique
if size(unique(x),2) < size(x,2)
C(1:size(x,2)) = 1; % the condition is not met, (C>0)
end
% Apply mass constraints based on tether allocation
if size(x,2) == 2 && mass(1) <= 500 && mass(2) <= 500 % a design targeting two objects with each less than 500 kg
C(size(x,2)+1,1) = max(mass) - 500; % the objects cannot exceed a mass of 500 kg
else
norm_mass = mass;
x = find(mass == max(mass));
norm_mass(x(1)) = []; % remove the most massive object in the set
C(size(x,2)+1,1) = max(mass) - 1000 + 250*(size(x,2)-1); % the largest object cannot exceed the maximum kg allowance for the number of tethers it uses
C(size(x,2)+2,1) = max(norm_mass) - 250; % all other objects cannot exceed the 250 kg limit of a single tether
end
I think maybe I was having issues with the debugger. I got it working! Thank you so much for your help! I think I will just set max mass to zero in the norm vector mass rather than deleting it so that it will still run for one object. You definitely are the mvp for matlab answers :)

请先登录,再进行评论。

类别

产品

版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by