Problem using ga without equality constraints

2 views (last 30 days)
Hello to all,
I am trying to optimize the geometry of N discreet systems using certain non-linear constraints (Inequalities). The inequalities are generated with the help of a seperate function (con_PK.m, as shown below).
function [c] = con_PK(Koordinaten,N,Xd,Yd,r,Rd)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
%Koordinaten = sym ('Koordinaten', [N,2]);
for i=1:N
for j=1:N
c1 (i,j) = 2*r-((Koordinaten(i,1)-Koordinaten(j,1))^2+(Koordinaten(i,2)-Koordinaten(j,2))^2)^0.5; %Constraint1
end
c1 (i,j+1) = ((Koordinaten(i,1)-Xd)^2+(Koordinaten(i,2)-Yd)^2)^0.5 - (Rd-r); %Constraint2
end
f=1;
for j=1:N
e=f+N-j-1;
c(f:e,1)=c1(j+1:N,j);
%f=f+(N-j);
f=e+1;
end
c(0.5*(N*(N-1)):0.5*(N*(N-1))+N-1,1)=c1(:,N+1);
end
and the main code as follows,
%Erstellt von Prathamesh
clc;
close all;
clear all;
%Eingabe von Parametern
%PK: Eine Notepaddatei mit allen Parametern ausgelesen
ID = importdata ('Input Data.txt');
Rd=ID(1,1);
r=ID(2,1);
N= ID(3,1);
%Erzeuge Kreis mit Radius Rd & Mittelpunkt (Rd,Rd)
%PK: Hier legen wir die 'Origin' fest
Xd=Rd;
Yd=Rd;
%PK: Als nächstes muss eine Basisgeometrie von Litzen erzeugt werden
%(willürlich)
th=0:pi/100:2*pi;
X=Rd*cos(th)+Xd;
Z=Rd*sin(th)+Yd;
%plot(X,Z);
%hold on
%Hier wird die Berechnung bassiert auf dem geneteschin Algorithmus gestartet
Koordinaten = sym ('x', [N,2]);
%Koordinaten (:,2)= sym ('y', [N,1]);
c1 = sym ('c',[0.5*(N^2+N-2),1]);
syms Lb
Function = @ (x) sum (Koordinaten (:,2)) >= 0;
variables = N;
c1 = con_PK(Koordinaten, N,Xd,Yd,r,Rd);
q = @(x) c1;
sol=ga(@Function,variables,[],[],[],[],[],[], q);
However, I always get following errors and unable to understand the reason. The most important thing to be noted is that, I do not have any equality constraints and do not know, if they can be categorically neglected by MATLAB 'ga' algorithm.
Error using Optimierung_PK>@(x)c1
Too many output arguments.
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in constrValidate (line 23)
[cineq,ceq] = nonlcon(Iterate.x');
Error in gacommon (line 125)
[LinearConstr, Iterate,nineqcstr,neqcstr,ncstr] = constrValidate(NonconFcn, ...
Error in ga (line 363)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in Optimierung_PK (line 39)
sol=ga(@Function,variables,[],[],[],[],[],[], q);
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation.
Thanking you in advance for any inputs.
Sincerely,
Prathamesh Khorgade.

Answers (1)

Walter Roberson
Walter Roberson on 9 Nov 2020
Edited: Walter Roberson on 9 Nov 2020
q = @(x) c1;
is wrong. It accepts a single input, and ignores the input, and instead returns whatever the content of c1 was without any substitution of values.
Instead
q = matlabFunction(c1, 'vars', {symvar(c1)});
  2 Comments
Prathamesh Khorgade
Prathamesh Khorgade on 9 Nov 2020
Thanks for your help
Now there is this error:
Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
Error in
symengine>@(in1)[-sqrt((in1(:,7)-in1(:,9)).^2+(in1(:,8)-in1(:,10)).^2)+7.6e+1./5.0;-sqrt((in1(:,7)-in1(:,11)).^2+(in1(:,8)-in1(:,12)).^2)+7.6e+1./5.0;-sqrt((in1(:,7)-in1(:,13)).^2+(in1(:,8)-in1(:,14)).^2)+7.6e+1./5.0;-sqrt((in1(:,7)-in1(:,15)).^2+(in1(:,8)-in1(:,16)).^2)+7.6e+1./5.0;-sqrt((in1(:,7)-in1(:,17)).^2+(in1(:,8)-in1(:,18)).^2)+7.6e+1./5.0;-sqrt((in1(:,7)-in1(:,19)).^2+(in1(:,8)-in1(:,20)).^2)+7.6e+1./5.0;-sqrt((in1(:,7)-in1(:,21)).^2+(in1(:,8)-in1(:,22)).^2)+7.6e+1./5.0;-sqrt((in1(:,7)-in1(:,23)).^2+(in1(:,8)-in1(:,24)).^2)+7.6e+1./5.0;-sqrt((in1(:,1)-in1(:,7)).^2+(in1(:,2)-in1(:,8)).^2)+7.6e+1./5.0;-sqrt((in1(:,3)-in1(:,7)).^2+(in1(:,4)-in1(:,8)).^2)+7.6e+1./5.0;-sqrt((in1(:,5)-in1(:,7)).^2+(in1(:,6)-in1(:,8)).^2)+7.6e+1./5.0;-sqrt((in1(:,9)-in1(:,11)).^2+(in1(:,10)-in1(:,12)).^2)+7.6e+1./5.0;-sqrt((in1(:,9)-in1(:,13)).^2+(in1(:,10)-in1(:,14)).^2)+7.6e+1./5.0;-sqrt((in1(:,9)-in1(:,15)).^2+(in1(:,10)-in1(:,16)).^2)+7.6e+1./5.0;-sqrt((in1(:,9)-in1(:,17)).^2+(in1(:,10)-in1(:,18)).^2)+7.6e+1./5.0;-sqrt((in1(:,9)-in1(:,19)).^2+(in1(:,10)-in1(:,20)).^2)+7.6e+1./5.0;-sqrt((in1(:,9)-in1(:,21)).^2+(in1(:,10)-in1(:,22)).^2)+7.6e+1./5.0;-sqrt((in1(:,9)-in1(:,23)).^2+(in1(:,10)-in1(:,24)).^2)+7.6e+1./5.0;-sqrt((in1(:,1)-in1(:,9)).^2+(in1(:,2)-in1(:,10)).^2)+7.6e+1./5.0;-sqrt((in1(:,3)-in1(:,9)).^2+(in1(:,4)-in1(:,10)).^2)+7.6e+1./5.0;-sqrt((in1(:,5)-in1(:,9)).^2+(in1(:,6)-in1(:,10)).^2)+7.6e+1./5.0;-sqrt((in1(:,11)-in1(:,13)).^2+(in1(:,12)-in1(:,14)).^2)+7.6e+1./5.0;-sqrt((in1(:,11)-in1(:,15)).^2+(in1(:,12)-in1(:,16)).^2)+7.6e+1./5.0;-sqrt((in1(:,11)-in1(:,17)).^2+(in1(:,12)-in1(:,18)).^2)+7.6e+1./5.0;-sqrt((in1(:,11)-in1(:,19)).^2+(in1(:,12)-in1(:,20)).^2)+7.6e+1./5.0;-sqrt((in1(:,11)-in1(:,21)).^2+(in1(:,12)-in1(:,22)).^2)+7.6e+1./5.0;-sqrt((in1(:,11)-in1(:,23)).^2+(in1(:,12)-in1(:,24)).^2)+7.6e+1./5.0;-sqrt((in1(:,1)-in1(:,11)).^2+(in1(:,2)-in1(:,12)).^2)+7.6e+1./5.0;-sqrt((in1(:,3)-in1(:,11)).^2+(in1(:,4)-in1(:,12)).^2)+7.6e+1./5.0;-sqrt((in1(:,5)-in1(:,11)).^2+(in1(:,6)-in1(:,12)).^2)+7.6e+1./5.0;-sqrt((in1(:,13)-in1(:,15)).^2+(in1(:,14)-in1(:,16)).^2)+7.6e+1./5.0;-sqrt((in1(:,13)-in1(:,17)).^2+(in1(:,14)-in1(:,18)).^2)+7.6e+1./5.0;-sqrt((in1(:,13)-in1(:,19)).^2+(in1(:,14)-in1(:,20)).^2)+7.6e+1./5.0;-sqrt((in1(:,13)-in1(:,21)).^2+(in1(:,14)-in1(:,22)).^2)+7.6e+1./5.0;-sqrt((in1(:,13)-in1(:,23)).^2+(in1(:,14)-in1(:,24)).^2)+7.6e+1./5.0;-sqrt((in1(:,1)-in1(:,13)).^2+(in1(:,2)-in1(:,14)).^2)+7.6e+1./5.0;-sqrt((in1(:,3)-in1(:,13)).^2+(in1(:,4)-in1(:,14)).^2)+7.6e+1./5.0;-sqrt((in1(:,5)-in1(:,13)).^2+(in1(:,6)-in1(:,14)).^2)+7.6e+1./5.0;-sqrt((in1(:,15)-in1(:,17)).^2+(in1(:,16)-in1(:,18)).^2)+7.6e+1./5.0;-sqrt((in1(:,15)-in1(:,19)).^2+(in1(:,16)-in1(:,20)).^2)+7.6e+1./5.0;-sqrt((in1(:,15)-in1(:,21)).^2+(in1(:,16)-in1(:,22)).^2)+7.6e+1./5.0;-sqrt((in1(:,15)-in1(:,23)).^2+(in1(:,16)-in1(:,24)).^2)+7.6e+1./5.0;-sqrt((in1(:,1)-in1(:,15)).^2+(in1(:,2)-in1(:,16)).^2)+7.6e+1./5.0;-sqrt((in1(:,3)-in1(:,15)).^2+(in1(:,4)-in1(:,16)).^2)+7.6e+1./5.0;-sqrt((in1(:,5)-in1(:,15)).^2+(in1(:,6)-in1(:,16)).^2)+7.6e+1./5.0;-sqrt((in1(:,17)-in1(:,19)).^2+(in1(:,18)-in1(:,20)).^2)+7.6e+1./5.0;-sqrt((in1(:,17)-in1(:,21)).^2+(in1(:,18)-in1(:,22)).^2)+7.6e+1./5.0;-sqrt((in1(:,17)-in1(:,23)).^2+(in1(:,18)-in1(:,24)).^2)+7.6e+1./5.0;-sqrt((in1(:,1)-in1(:,17)).^2+(in1(:,2)-in1(:,18)).^2)+7.6e+1./5.0;-sqrt((in1(:,3)-in1(:,17)).^2+(in1(:,4)-in1(:,18)).^2)+7.6e+1./5.0;-sqrt((in1(:,5)-in1(:,17)).^2+(in1(:,6)-in1(:,18)).^2)+7.6e+1./5.0;-sqrt((in1(:,19)-in1(:,21)).^2+(in1(:,20)-in1(:,22)).^2)+7.6e+1./5.0;-sqrt((in1(:,19)-in1(:,23)).^2+(in1(:,20)-in1(:,24)).^2)+7.6e+1./5.0;-sqrt((in1(:,1)-in1(:,19)).^2+(in1(:,2)-in1(:,20)).^2)+7.6e+1./5.0;-sqrt((in1(:,3)-in1(:,19)).^2+(in1(:,4)-in1(:,20)).^2)+7.6e+1./5.0;-sqrt((in1(:,5)-in1(:,19)).^2+(in1(:,6)-in1(:,20)).^2)+7.6e+1./5.0;-sqrt((in1(:,21)-in1(:,23)).^2+(in1(:,22)-in1(:,24)).^2)+7.6e+1./5.0;-sqrt((in1(:,1)-in1(:,21)).^2+(in1(:,2)-in1(:,22)).^2)+7.6e+1./5.0;-sqrt((in1(:,3)-in1(:,21)).^2+(in1(:,4)-in1(:,22)).^2)+7.6e+1./5.0;-sqrt((in1(:,5)-in1(:,21)).^2+(in1(:,6)-in1(:,22)).^2)+7.6e+1./5.0;-sqrt((in1(:,1)-in1(:,23)).^2+(in1(:,2)-in1(:,24)).^2)+7.6e+1./5.0;-sqrt((in1(:,3)-in1(:,23)).^2+(in1(:,4)-in1(:,24)).^2)+7.6e+1./5.0;-sqrt((in1(:,5)-in1(:,23)).^2+(in1(:,6)-in1(:,24)).^2)+7.6e+1./5.0;-sqrt((in1(:,1)-in1(:,3)).^2+(in1(:,2)-in1(:,4)).^2)+7.6e+1./5.0;-sqrt((in1(:,1)-in1(:,5)).^2+(in1(:,2)-in1(:,6)).^2)+7.6e+1./5.0;-sqrt((in1(:,3)-in1(:,5)).^2+(in1(:,4)-in1(:,6)).^2)+7.6e+1./5.0;sqrt((in1(:,7)-9.1e+1./2.0).^2+(in1(:,8)-9.1e+1./2.0).^2)-3.79e+2./1.0e+1;sqrt((in1(:,9)-9.1e+1./2.0).^2+(in1(:,10)-9.1e+1./2.0).^2)-3.79e+2./1.0e+1;sqrt((in1(:,11)-9.1e+1./2.0).^2+(in1(:,12)-9.1e+1./2.0).^2)-3.79e+2./1.0e+1;sqrt((in1(:,13)-9.1e+1./2.0).^2+(in1(:,14)-9.1e+1./2.0).^2)-3.79e+2./1.0e+1;sqrt((in1(:,15)-9.1e+1./2.0).^2+(in1(:,16)-9.1e+1./2.0).^2)-3.79e+2./1.0e+1;sqrt((in1(:,17)-9.1e+1./2.0).^2+(in1(:,18)-9.1e+1./2.0).^2)-3.79e+2./1.0e+1;sqrt((in1(:,19)-9.1e+1./2.0).^2+(in1(:,20)-9.1e+1./2.0).^2)-3.79e+2./1.0e+1;sqrt((in1(:,21)-9.1e+1./2.0).^2+(in1(:,22)-9.1e+1./2.0).^2)-3.79e+2./1.0e+1;sqrt((in1(:,23)-9.1e+1./2.0).^2+(in1(:,24)-9.1e+1./2.0).^2)-3.79e+2./1.0e+1;sqrt((in1(:,1)-9.1e+1./2.0).^2+(in1(:,2)-9.1e+1./2.0).^2)-3.79e+2./1.0e+1;sqrt((in1(:,3)-9.1e+1./2.0).^2+(in1(:,4)-9.1e+1./2.0).^2)-3.79e+2./1.0e+1;sqrt((in1(:,5)-9.1e+1./2.0).^2+(in1(:,6)-9.1e+1./2.0).^2)-3.79e+2./1.0e+1]
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in constrValidate (line 23)
[cineq,ceq] = nonlcon(Iterate.x');
Error in gacommon (line 125)
[LinearConstr, Iterate,nineqcstr,neqcstr,ncstr] = constrValidate(NonconFcn, ...
Error in ga (line 363)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in Optimierung_PK (line 39)
sol=ga(@Function,variables,[],[],[],[],[],[], q);
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation.
Walter Roberson
Walter Roberson on 9 Nov 2020
You are using u as a nonlinear constraint function. Such a function must return two values.
q1 = matlabFunction(c1, 'vars', {symvar(c1)});
q = @(x) deal(q1(x), []); %empty nonlinear equalities

Sign in to comment.

Categories

Find more on Systems of Nonlinear Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!