Ga does not find the optimal value

Hi Matlab comunity after I tried fmincon:
I now tried ga to solve my problem with integers
x=ga(@(x)fAraucoMarkt(x,AraucoEnergy),4,[],[],[],[],LB,RB,@(x)bedMarkt(x,AraucoEnergy),[2:4]);
I guess it works but I do not get the optimal solution:
x=ga(@(x)fAraucoMarkt(x,AraucoEnergy),4,[],[],[],[],LB,RB,@(x)bedMarkt(x,AraucoEnergy),[2:4]);
Optimization terminated: average change in the penalty fitness value less than options.FunctionTolerance
but constraints are not satisfied.
Is there anyway to get a better solution (I was thinking of random starting numbers and than a list of all found solutions) but as I can not define start point I do not know how to get this.
And while I am at it I had an nonlinear eauqlity constrain which was like this
ceq(1)=Restschuld-0;
I was thinking in changing it to a inequality constrain Restschuld is smaller than 1 and bigger than -1
but I dont know how to do this any idea?
Anyone an Idea
Greetings
Erik

2 Comments

Restschuld is smaller than 1 and bigger than -1
Those would be bounds constraints, which you would store in LB and RB
So my conditions are like this
function [c,ceq] = bedMarkt(x,AraucoEnergy)
%UNTITLED5 Summary of this function goes here
% Detailed explanation goes here
minSpeicher=fminSpeich(x,AraucoEnergy);
Restschuld=fRestschuld(x,AraucoEnergy);
c=0.1-minSpeicher;
ceq=Restschuld-0;
end
I tought that the bound constrains just adress the input parameters

Sign in to comment.

 Accepted Answer

You have 3 variables, each of which can only be the integers 2, 3, or 4. That is only 3^3 = 27 possibilities.
Just run all of those possibilities separately, in a loop, with those variables as constants for the duration of the run.
This would have the additional advantage that since you would no longer have integer constraints, you would be able to add your own nonlinear constraint function.

13 Comments

I think I have not dicribed it properly.
I have one variable x which is a (1,1:4) double. The first one x(1,1) is not an integer.
all the other are integers with the following left an right bounds: or lower and upper
%LeftBound
LB=zeros(1,4);
%Fremdkapitalanteil
LB(1,1)=0;
%Anzahl Elktrolyseure
LB(1,2)=0;
%Anzahl Speicher
LB(1,3)=1;
%Anzahl Ammoniaksynthese
LB(1,4)=0;
%RightBound
RB=zeros(1,4);
%Fremdkapitalanteil
RB(1,1)=1;
%Anzahl Elktrolyseure
RB(1,2)=15;
%Anzahl Speicher
RB(1,3)=30;
%Anzahl Ammoniaksynthese
RB(1,4)=70;
You are right, I did misread the bounds.
In any case, variable 2 is 0:15, #3 is 1:30, #4 is 0:70. That is only
16*30*71
ans = 34080
possibilities, which is feasible to run as a loop. But yes, that is getting up there, so perhaps using integer constraints is worth doing. Still, looping through all the combinations would give you more assurance that you have found the minimum value.
So you have integers of size
16*30*71
ans = 34080
That is still not too many optimization problems to solve, especially since the remaining problem is 1-D and bounded to [0 1] so you can use fminbnd (or fmincon if the problem is smoothly differentiable). I would expect a solver to take less than 0.1 s to solve each problem, so less than an hour of computation, maybe much less.
for i = 0:15
for j = 1:30
for k = 0:70
% Optimize fcn(x,i,j,k)
end
end
end
Alan Weiss
MATLAB mathematical toolbox documentation
Matt J
Matt J on 4 May 2022
Edited: Matt J on 4 May 2022
If possible, you could also run ga() without the integer constraints on x(2:4). Then, you could refine the result by applying Alan's recommendation, except you could search over a narrower range of integer i,j,k values near the continuous solution.
It is, however, unlikely that you will ever be able to satisfy nonlinear equality constraints when x(2:4) are integers. So, you should rethink that aspect of the problem.
Thanks a lot everybody you guys are really helping a lot thanks so much.
So first: I am trying your idea @Alan Weiss :
for i = 0:15
for j = 1:30
for k = 0:70
x=zeros(1,4);
%Fremdkapitalanteil
LBFK = LB(1,1);
RBFK = RB(1,1);
x(1,1) = (RBFK-LBFK).*rand(1,1) + LBFK;
x(1,2) = i;
x(1,3) = j;
x(1,4) = k;
x=fmincon(@(x(1))fAraucoMarkt(x,AraucoEnergy),x0,[],[],[],[],LB,RB,@(x)bedMarkt(x,AraucoEnergy));
end
end
end
is this how it should look like? my question would be how do I store all the results now? (sorry if thats a stupid question I am not sooo experianced jet) . But because I have 3 values how do I create a list which stores all the results.
@Matt J the narrowing worked already with fmincon once I dont use itegers the problem is that it is still quite volitile. But I know that there is a point where I can fulfill all my constrains also with the integers. before I did it now in matlab I tried myself a bit in excel and I found at least one set up that works. ( I hope I understood it corect)
thanks a lot again
Greetings
Erik
@Matt J what I tried was to let fmincon run and take the best solution and use this one as the startingvalue for ga. But it did not work.
xf = zeros(1,3);
LB = 0;
RB = 1;
optval = Inf;
for i = 0:15
for j = 1:30
for k = 0:70
%Fremdkapitalanteil
x0 = (RBFK-LBFK).*rand(1,1) + LBFK;
xf(1) = i;
xf(2) = j;
xf(3) = k;
[x,fval]=fmincon(@(x)fAraucoMarkt(x,xf,AraucoEnergy),x0,[],[],[],[],LB,RB,@(x)bedMarkt(x,xf,AraucoEnergy));
if fval < optval
optval = fval;
xopt = x;
xopt1 = i;
xopt2 = j;
xopt3 = k;
end
end
end
end
You can concatenate x and xf in fAraucoMarkt and bedMarkt to a single vector:
X = [x,xf];
but I would take the vector xf of integers out of the optimization vector.
@Torsten I used this script now and it runs but it did not create a list.
Before I used something like this:
Return=fAraucoMarkt(x,AraucoEnergy);
Results(i,1:4)=x;
Results(i,5)=Return;
Results(i,6)=fminSpeich(x,AraucoEnergy);
Results(i,7)=fRestschuld(x,AraucoEnergy);
maybe I am also not getting the solution
because if xopt are values that are not fulfilling the constraints
so my idea was to create a list with all the values and the results and kick out the once which do not not fulfill the constrains but I dont know how.
xf = zeros(1,3);
LB = 0;
RB = 1;
%optval = Inf;
index = 0;
for i = 0:15
for j = 1:30
for k = 0:70
%Fremdkapitalanteil
x0 = (RBFK-LBFK).*rand(1,1) + LBFK;
xf(1) = i;
xf(2) = j;
xf(3) = k;
[x,fval]=fmincon(@(x)fAraucoMarkt(x,xf,AraucoEnergy),x0,[],[],[],[],LB,RB,@(x)bedMarkt(x,xf,AraucoEnergy));
%if fval < optval
% optval = fval;
% xopt = x;
% xopt1 = i;
% xopt2 = j;
% xopt3 = k;
%end
index = index + 1;
Return=fAraucoMarkt(x,xf,AraucoEnergy);
Results(index,1:4)=[x,xf];
Results(index,5)=Return;
Results(index,6)=fminSpeich(x,xf,AraucoEnergy);
Results(index,7)=fRestschuld(x,xf,AraucoEnergy);
end
end
end
I used it and it works perfectly fine thanks a lot all of you !

Sign in to comment.

More Answers (1)

Matt J
Matt J on 4 May 2022
Edited: Matt J on 4 May 2022
I was thinking in changing it to a inequality constrain Restschuld is smaller than 1 and bigger than -1
I'm surprised you were even able to run the code in its current form. From the ga documentation:
"When intcon is nonempty, nonlcon must return empty for ceq. For more information on integer programming, see Mixed Integer ga Optimization."

1 Comment

@Matt J sorry I changed it
function [c,ceq] = fbedga(x,AraucoEnergy)
%UNTITLED5 Summary of this function goes here
% Detailed explanation goes here
minSpeicher=fminSpeich(x,AraucoEnergy);
Restschuld=fRestschuld(x,AraucoEnergy);
c(1)=0.1-minSpeicher;
c(2)=(abs(Restschuld))-1;
ceq=[];
end
that was my idea of solving it.

Sign in to comment.

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!