Problem using ga without equality constraints
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Antworten (1)
Walter Roberson
am 9 Nov. 2020
Bearbeitet: Walter Roberson
am 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 Kommentare
Walter Roberson
am 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
Siehe auch
Kategorien
Mehr zu Calculus finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!