Simple Question, Please Answer 3
1 view (last 30 days)
Show older comments
Guys, i have this code
global U L
for U = 1000:1000:2000
for L = 0:400:400
[G,maxfit]=Optimasi_HS3()
end
end
as you can see, this run my Optimasi_HS3 function 4 times, i do this to make sure my result of G is convergent with 4 types combination of U (initial upper bound limit) and L (initial lower bound limit)
the Optimasi_HS3 is genetic algorithm based code, which resulting in 6 different value of G. it has 50 populations per generation, and has 300 generations. my question is:
- when the program finished running, it only shows the last run value of G, is there any command to show 4 of them?
- do you know the command to plot the 4 graph of each run, the x value should be generation and y value should be maxfit. (usually when i only need to run 1 time, i just use plot(maxfit) and it showed up. but i also use: gg 1:300, plot(gg,maxfit)).
Thank you very much, please answer me if you know
3 Comments
Stephen23
on 24 Jan 2019
"do you know the example of the neat and efficient input arguments?"
"however, do you know how to do my 2nd question?"
If you want all of the data in one plot then ensure that the data is arranged in colums in one array and then you can use just one plot call, e.g:
plot(A)
How to get your data into one array will depend on the sizes and classes of that data, which you have not told us anything about.
Answers (1)
Guillaume
on 24 Jan 2019
Step 1: As Stephen said, get rid of the global variables. Modify that Optimasi_HS3 function so that it takes the G and U as input variables. There's zero reason to use global variables here. It just increases the risk that something will go wrong.
Step 2: decouple the generation of the combinations from the actual iteration. Then you can simply iterate over the combination and index the result at the same time. Probably the easiest way to do that
U = 1000:1000:2000; %U values to try
L = 0:400:400; %L values to try
[allU, allL] = ndgrid(U, L); %cartesian product of U and L set
G = cell(size(allU)); %using cell array for all G values to be safe as you haven't told us if each G is scalar, matrix or something else
maxfit = cell(size(allU)); %same
for idx = 1:numel(allU) %iterate over all combinations
G{idx} = Optimasi_HS3(U(idx), L(idx)); %NOT USING GLOBAL VARIABLES
end
The two arrays to store the outputs (G and maxfit) could possibly be 3D matrices or 2D matrices, depending on the size of the outputs of Optimasi_HS3 (and whether or not it's consistent from call to call), but you haven't said anything about that.
For plotting, you could just loop again, or if it's possible to store the outputs as matrices, pass these matrices directly to plot (possibly after a reshape).
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!