How can I run this parfor loop?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Edric Ellis
el 12 de Oct. de 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 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!