Global variables in genetic algorithm

Im using ga toolbox with parallel computing .. I used a main code that uses global variables And calls the ga main code ... However the ga doesnt see the variable inside the fitness function

10 件のコメント

Stephan
Stephan 2019 年 10 月 2 日
編集済み: Stephan 2019 年 10 月 2 日
Star Strider
Star Strider 2019 年 10 月 2 日
Do not use global variables at all!
See the documentation section on Parameterizing Functions and Passing Extra Parameters.
alaa zarif
alaa zarif 2019 年 10 月 2 日
the nested and annonymous methods are not working, since i have multiple files (fitness,constraints) each one of them need to have the same inputs which the toolbox doesnt allow since they can only have an input of x
Walter Roberson
Walter Roberson 2019 年 10 月 2 日
You can use the anonymous function method to call functions defined in other files.
alaa zarif
alaa zarif 2019 年 10 月 3 日
now i need to change the massConst, aspectratio and freqmin each run,,, and these are data that i need in the fitness and constraint files...how exactly do i use an annonymous function ?
Main file code
numRun = 1;
[massConst,Aspectratio,Freqmin]= globvar(numRun)
Initial_Data
x=Optimal(numRun,massConst,Aspectratio,Freqmin); %this calls the GA main code
save('workspace_run1.mat');
numRun = 2;
[massConst,Aspectratio,Freqmin]= globvar(numRun)
Initial_Data
x=Optimal(numRun,massConst,Aspectratio,Freqmin); %this calls the GA main code
save('workspace_run1.mat');
Stephen23
Stephen23 2019 年 10 月 3 日
alaa zarif
alaa zarif 2019 年 10 月 5 日
I fixed it! I used anonymous function handle
Obj=@(x)fitness(x, a, b, c) where a, b, c are my variables and it worked!
Walter Roberson
Walter Roberson 2019 年 10 月 5 日
And no global variables needed!
Mitsu
Mitsu 2020 年 9 月 2 日
Hi Walter,
would there be a way to change a, b, c inside the fitness function so that the constraint function can also use it?
Basically, there is some information that depends on the solution, that I want to use in both the constraints and the fitness, without having to compute that information twice.
Walter Roberson
Walter Roberson 2020 年 9 月 2 日
Taro:
You would construct a function handle to compute the information based upon only the necessary inputs. You would memoize() that, and make the memoized function available to the fitness and constraint function, by using function parameterization like Star Strider linked to above. You would call the memoized function handle inside your fitness function and inside your constraint function.
What would then happen is that the first time that either of those functions called the memoized function, the memoized version would notice that it had not seen that combination of parameters (recently), and would do the calculation and would keep a copy of the result. Then when the other of the functions called the memoized function with the same parameters, the memoized function would look in its records and see that it had already computed those results and would use the already-calculated version instead of recalculating.
You can adjust the cache size on the memoized version.
You should do this rather than using global variables or persistent variables in the calculation function, because there are circumstances under which the fitness function is called multiple times in a row without calling the constraint function, and there are circumstances under which the constraint function is called multiple times in a row without calling the fitness function (especially if there are equality constraints.) You therefore cannot rely upon the sequence "fitness called, then constraint called with the same parameters", and you cannot rely upon the sequence "constraint called then fitness called with the same parameters".
You should probably make the cache size at least twice the size of your population.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics and Optimization についてさらに検索

質問済み:

2019 年 10 月 2 日

コメント済み:

2020 年 9 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by