Troubles with MATLAB genetic algorithm nonlinear constraint function
Mostrar comentarios más antiguos
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!
Respuestas (1)
Matt J
el 5 de Mzo. de 2021
If you re-run your code after executing,
>>dbstop if caught error
you will be able to trace the problem.
15 comentarios
Angeliek Devine
el 5 de Mzo. de 2021
Editada: Angeliek Devine
el 5 de Mzo. de 2021
Angeliek Devine
el 5 de Mzo. de 2021
Matt J
el 5 de Mzo. de 2021
It means that either plotState or plotState.(solver) is not a structure variable. Therfore, Matlab doesn't understand why you are dot-indexing it.
Angeliek Devine
el 5 de Mzo. de 2021
Editada: Angeliek Devine
el 5 de Mzo. de 2021
Walter Roberson
el 5 de Mzo. de 2021
We need the code for dcosm_from_angles and find_LoN_Angles
Angeliek Devine
el 5 de Mzo. de 2021
Walter Roberson
el 5 de Mzo. de 2021
I am not getting an error yet.
What are some sample inputs for the budget and the number of items?
Angeliek Devine
el 5 de Mzo. de 2021
Walter Roberson
el 5 de Mzo. de 2021
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.
Walter Roberson
el 5 de Mzo. de 2021
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.
Angeliek Devine
el 5 de Mzo. de 2021
Editada: Angeliek Devine
el 5 de Mzo. de 2021
Walter Roberson
el 5 de Mzo. de 2021
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?
Angeliek Devine
el 5 de Mzo. de 2021
Editada: Angeliek Devine
el 5 de Mzo. de 2021
Walter Roberson
el 6 de Mzo. de 2021
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
Angeliek Devine
el 6 de Mzo. de 2021
Categorías
Más información sobre Genetic Algorithm en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!