How can I run this parfor loop?

2 Ansichten (letzte 30 Tage)
SANGGWI YOON
SANGGWI YOON am 11 Okt. 2018
Bearbeitet: Edric Ellis am 12 Okt. 2018
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

Antworten (1)

Edric Ellis
Edric Ellis am 12 Okt. 2018
One of the primary requirements of a parfor loop is that the iterations must be order independent.
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).

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by