How can I run this parfor loop?
4 views (last 30 days)
Show older comments
I want to use parfor loop. But, temporary variables error occurred. How can I fix that code?
- error variables are "old_pop", "old_fit", "old_obj"
% Input Data of Structural / (Import of Measured Displacement)
[NODE, ELEMENT, MATERIAL, SECTION, CONSTRAINT, SPRING, CONLOAD, displ_m] = ...
InputStructureData('InputStructureData2ndLC2.txt');
% (Generation of Initial Population, ramdomly)
old_pop = generation(pop_size, total_chrom_length, range_vars, digit_number);
% (Evaluation of the Fitness)
[old_obj, old_fit] = fitness(old_pop, pop_size, optimization_method, ...
penalty_coeff, displ_m, 1, NODE, ELEMENT, MATERIAL, SECTION, ...
CONSTRAINT, CONLOAD, SPRING);
parfor n = 1:max_generation
% (Selection)
cross_pool = selection( old_pop, old_fit, pop_size, ...
total_chrom_length, range_vars, selection_operator);
% (Crossover)
mutation_pool = crossover(cross_pool, pop_size, total_chrom_length, ...
range_vars, crossover_prob, max_generation, step_mg, n);
% (Mutation)
elite_pool = mutation(mutation_pool, pop_size, total_chrom_length, ...
range_vars, mutation_prob, max_generation, digit_number, step_mg, n);
% (Evaluation of the Fitness)
[new_obj, new_fit] = fitness(elite_pool, pop_size, ...
optimization_method, penalty_coeff, displ_m, n, ...
NODE, ELEMENT, MATERIAL, SECTION, CONSTRAINT, CONLOAD, SPRING);
% (Elitism)
[new_pop, new_obj, new_fit] = elite(old_pop, old_obj, old_fit, ...
elite_pool, new_obj, new_fit);
% (Print of the Result)
[Result(n,:), old_pop, old_obj, old_fit] = ...
result(new_pop, new_obj, new_fit);
waitbar(n/max_generation, progress, ...
sprintf('%d / %d Gen. (%.2f%%) Complete', n,...
max_generation, n/max_generation*100))
end
0 Comments
Answers (1)
Edric Ellis
on 12 Oct 2018
In this case, iteration i of your parfor loop is trying to use the values of old_pop and old_obj from iteration i-1. This is not allowed. To use parfor, you need to restructure your code so that the loop iterations are independent - this might not be possible in this case (there's no obvious easy way to do it that I can see).
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements 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!