Filter löschen
Filter löschen

GA does not solve problems with integer and equality constraints

12 Ansichten (letzte 30 Tage)
When I test this code which is shown below. I got an error of " GA does not solve problems with integer and equality constraints."
what should I change to solve this problem.
for i=1:2*ncap
if k==0
options = gaoptimset;
options = gaoptimset('PopulationSize', 50,'Generations', 500,'StallGenLimit',100,'TimeLimit', 500,'StallTimeLimit', 50,'PlotFcn',@gaplotbestf);
constraints = @(capplcsz_opt) constraint_GA(capplcsz_opt, Vmax, Vmin);
[capplcsz_opt,fval,exitflag]=ga(@power_flow_cap,2*ncap,[],[],[],[],lb,ub,constraints,[1 3 5 7],options);
capplcsz_opt([1 3 5 7])
[ Ploss,Busvoltage]=power_flow_cap(capplcsz_opt)

Akzeptierte Antwort

Torsten am 3 Mär. 2024
Bearbeitet: Torsten am 4 Mär. 2024
Your code from above works for me. Test it.
Because you only have inequality constraints, you must set h = [] instead of h = zeros(60,1) in function "constraint_ga".
  7 Kommentare
Torsten am 4 Mär. 2024
Bearbeitet: Torsten am 4 Mär. 2024
You want to have V=abs(Busvoltage) within Vmin and Vmax, and this is the case (see the code below).
[g,h]=constraint_GA(capplcsz_opt, Vmin, Vmax)
idx = find(g>0)
gives idx = [].
I don't know how you see that there is no improvement for fval.
Khlifi Wahid
Khlifi Wahid am 6 Mär. 2024
Actually, I want to minimize the fval value; it must be less than 0.0203.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

John D'Errico
John D'Errico am 3 Mär. 2024
Bearbeitet: John D'Errico am 3 Mär. 2024
The coupling of equality constraints with integer constraints makes a significant problem. For example, suppose I wanted to solve a problem where x and y were constrained to be integer, together with a rather simple equality constraint? Thus just require that
x^2 - 13*y^2 = 1
This is in fact known as a Pell equation. When you multiply y^2 there by 13, there are not too many integer solutions. The first integer solution does not arise until arise until you get to (x,y) == (649,180). And if we change the constraint to
x^2 - 109*y^2 = 1
the smallest positive integer solution apparently lives at (158070671986249,15140424455100).
Worse, if the factor applied to y is the square of an integer, then no solutions will ever exist. Should GA be able to know these facts about Pell equations?
So why am I pointing out Pell equations? Because they are a simple class of equation form where the solutions are not trivial to identify, AND the solutions often lie few and far between. Should GA know the necessary number theory to be able to recognize a Pell equation for what it is, then to solve a Pell equation as part of the constraints? OF COURSE NOT!
Even simpler, suppose the constraints were purely linear? Even there, things get tricky. Solving a system of linear Diophantine equations just to identify potential feasible solutions again requires an understanding of number theory, though much simpler in that case.
Maybe you think that GA should be able to look at sets of infeasible parameter values, and from that, know how to find a feasible solution? NOPE. Again, look at the Pell equation example. Given the constraint
x^2 - 109*y^2 = 1
and some set of sample values for x and y that fail to satisfy the constraint, can you simply identify a pair that does satisfy the constraint? Again, unless you understand Pell equations well enough to know how to solve for a general solution to that equation, nothing will easily help you.
I can give a huge variety of examples. Um, how about this simple constraint on the integer x:
isprime(x*2^x + 1) == true
that is, require that x is an integer, AND that x*2^x+1 is a prime number. This family of numbers are known as the Cullen numbers.
They are rather rarely prime. Sequence A005849 in the OEIS (Online Encyclopedia of Integer Sequences) lists the complete known set of values for x, such that a prime number arises is:
{1, 141, 4713, 5795, 6611, 18496, 32292, 32469, 59656, 90825, 262419, 361275, 481899, 1354828, 6328548, 6679881}
The point being, GA would need to be an expert in number theory to handle these problems. It is not so. The combination of equality constraints with integer variables simply makes the problem too complex.
So, to try to answer your actual question as to what should you change to resolve it, you could just try leaving the variables to be non-integer, only rounding them inside the objective function. GA might survive the discontinuities introduced in the objective function. Or, you might just test all possible sets of integer variables, taking the best option. That would completely avoid any optimizer.

Walter Roberson
Walter Roberson am 3 Mär. 2024
What you have to do is not declare [1 3 5 7] as being integer valued, and instead declare your own MutationFcn and CrossoverFcn and InitialPopulation, with those functions being defined in such a way that they "just happen" to always make [1 3 5 7] integers
ga() cannot handle the combination of integer valued variables together with nonlinear constraints, so you have to declare all the variables to be continuous but arrange the MutationFcn and CrossOverFcn to enforce integer values for the appropriate variables.
  5 Kommentare
Walter Roberson
Walter Roberson am 4 Mär. 2024
g(1)=V(1)-Vmax ;
g(12)= V(2)-Vmax;
g(3) = V(3)-Vmax;
You output to g(12) there instead of to g(2). You later output to g(12) without having outout to g(2)
g(1) =Vmin-V(1);
You overwrite what you have written to g(1) and the rest.
You could simply code
g = [V(:) - Vmax; Vmin - V(:)];
Khlifi Wahid
Khlifi Wahid am 6 Mär. 2024
Actually, the problem of limiting the values ​​between vmax and vmin has been solved but I did not find an optimal faval value and exitflag=-5, I want to minimize it less than 0.0203 can i change something to do it

Melden Sie sich an, um zu kommentieren.


Mehr zu Get Started with Optimization Toolbox 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!

Translated by