ga optimization changing answer

조회 수: 2 (최근 30일)
shirin mhd
shirin mhd 2022년 5월 11일
댓글: shirin mhd 2022년 5월 11일
Hi everyone
I have a question about GA optimization answer.
I want to optimize this function with 5 variable, as below:
Q=ga(@(b) -(-1e-6*((b(1))^2)+5.69e2*(b(1))+...
-1e-6*((b(2))^2)+8e2*(b(2))+...
-1e-6*((b(3))^2)+4e2*(b(3))+...
-1e-6*((b(4))^2)+4e2*(b(4))+...
-1e-6*((b(5))^2)+5e2*(b(5))),5)
at first i run the code and the answer was what you can see below:
Optimization terminated: maximum number of generations exceeded.
Q =
1.0e+03 *
1.6325 2.2597 1.5050 1.4811 1.4940
i run the code for second time and the answer changed to:
Optimization terminated: maximum number of generations exceeded.
Q =
1.0e+03 *
1.8045 2.3893 1.4345 1.5154 1.5075
and for the third time the answer was:
Optimization terminated: maximum number of generations exceeded.
Q =
1.0e+03 *
1.8864 2.3098 1.4213 1.5103 1.2786
as you can see, the answer for each run was changed. why is this happening? I know that ga uses several iterations to obtain the answer, but I thought that the final answer is unique and fixed. am I doing something wrong? could you please explain this result for me?

채택된 답변

Chunru
Chunru 2022년 5월 11일
GA need random initialization of population and it also involves random processing in generating children. Therefore, the results of different run may be different. To make the result reproducible, you need to reset the random generator before each run. For example:
rng default
Q=ga(@(b) -(-1e-6*((b(1))^2)+5.69e2*(b(1))+...
-1e-6*((b(2))^2)+8e2*(b(2))+...
-1e-6*((b(3))^2)+4e2*(b(3))+...
-1e-6*((b(4))^2)+4e2*(b(4))+...
-1e-6*((b(5))^2)+5e2*(b(5))),5)
Optimization terminated: maximum number of generations exceeded.
Q = 1×5
1.0e+03 * 1.8729 2.0826 1.6439 1.5220 1.7711
rng default
Q=ga(@(b) -(-1e-6*((b(1))^2)+5.69e2*(b(1))+...
-1e-6*((b(2))^2)+8e2*(b(2))+...
-1e-6*((b(3))^2)+4e2*(b(3))+...
-1e-6*((b(4))^2)+4e2*(b(4))+...
-1e-6*((b(5))^2)+5e2*(b(5))),5)
Optimization terminated: maximum number of generations exceeded.
Q = 1×5
1.0e+03 * 1.8729 2.0826 1.6439 1.5220 1.7711

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by