why the arrangement of elements are changing?

1 回表示 (過去 30 日間)
Sadiq Akbar
Sadiq Akbar 2024 年 2 月 27 日
回答済み: Steven Lord 2024 年 2 月 27 日
I have a function fNoise() and a main.m. Both are in the attchment. When I run the main several times, some time the arrangement of elements in vector "bSolution" change and some time it is the same. I want that the arrangement of elements in the vector "bSolution" remains the same as is the arrangement of elements in each u. For example, the arrangement of elements each u is as:
u1 = [-30 30]; % Define your first vector u here
u2 = [-41 41 51]; % Define your second vector u here
u3 = [-40 40 61 70]; % Define your third vector u here
But sometimes this arrangement is maintained in vector "bSolution and sometimes it is changed as:
u1: [-30 30]
Best estimated vector for u1: [30.1309582222756 -30.3646996283225]
Corresponding best fitness: 0.469151
u2: [-41 41 51]
Best estimated vector for u2: [40.4826960392027 -41.3736991569538 49.8771670393263]
Corresponding best fitness: 0.721330
u3: [-40 40 61 70]
Best estimated vector for u3: [39.795566278604 -39.1792888055757 62.4483232435389 69.5455521853215]
Corresponding best fitness: 0.687029
>>
As you can see that the arrangement in "Best estimated vector for u1" is changed i.e., [30.1309582222756 -30.3646996283225]. Likewise, the same happens for u2 also. sometimes it happens for all the three also.
What to do so that the arrangement remains the same like each u?

回答 (1 件)

Steven Lord
Steven Lord 2024 年 2 月 27 日
I think what you're asking is how you can force ga to always return b as an increasing vector ('ascend' using the wording from the issorted function.) If so you could do this using the A and b inputs to ga by imposing a constraint that (for example) b(2)-b(1) >= 0 (or replace 0 with some very small tolerance if you want b to be 'strictascend', with no duplicate elements.)
To require that b(2)-b(1) >= 0, rewrite it as b(1)-b(2) <= 0 (which turns into b(1) <= b(2), which is exactly the relationship you want, if you add b(2) to both sides.) That looks like one of the equations shown in the description of the A input argument on the ga documentation page.
Ainput = [1 -1];
binput = 0; % renaming to avoid confusion with your variable b
Add in additional rows to this Ainput and binput to require each element to be greater than (or greater than or equal to) the previous element then pass them into your ga call. Let's check symbolically that this imposes the relationship you want.
x = sym('x', [2, 1])
x = 
relationship = Ainput*x <= binput
relationship = 
Again, adding to both sides gives that .

Community Treasure Hunt

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

Start Hunting!

Translated by